﻿var curThreadId=0;

function AJAXCache(_b4){
	var _b5=new Array();
	var _b6=_b4;
	this.load=function(_b7,_b8){
		try{
			var _b9=_b7.toString();
			if(typeof (_b5[_b9])!="undefined"){
				return;
			}
			_b5[_b9]=new Array();
			_b5[_b9]["id"]=_b7;
			_b5[_b9]["val"]=_b8;
			_b5[_b9]["orgval"]=_b8;
		}catch(error){
			processException("E304","MAX");
		}
	};
	this.write=function(_ba,_bb){
		try{
			var _bc=_ba.toString();
			if(typeof (_b5[_bc])=="undefined"){
				return;
			}
			_b5[_bc]["val"]=_bb;
		}catch(error){
			processException("E304","MAX");
			return;
		}
	};
	this.read=function(_bd){
		try{
			var _be=_bd.toString();
			if(typeof (_b5[_be])!="undefined"){
				return _b5[_be]["val"];
			}else{
				return;
			}
		}catch(error){
			processException("E304","MAX");
			return;
		}
	};
	this.readAll=function(){
		try{
			var _bf=new Array();
			for(var i in _b5){
				if(typeof (_b5[i])!="undefined"){
					_bf.push([_b5[i]["id"],_b5[i]["val"]]);
				}
			}
			return _bf;
		}catch(error){
			processException("E304","MAX");
			return null;
		}
	};
	this.getDirty=function(){
		try{
			var _c1=new Array();
			for(var i in _b5){
				if(typeof (_b5[i])!="undefined"){
					if(!equal(_b5[i]["val"],_b5[i]["orgval"])){
						_c1.push([_b5[i]["id"],_b5[i]["val"]]);
					}
				}
			}
			return _c1;
		}catch(error){
			processException("E304","MAX");
			return null;
		}
	};
	this.clear=function(){
		try{
			_b5=new Array();
		}catch(error){
			processException("E304","MAX");
		}
	};
}

function ajax_operation(url, callback, img_type){
	/*curThreadId+=1;
	if(curThreadId>=1000){
		curThreadId = 0;
	}
	this.threadId=curThreadId;*/
	this.url = url;
	this.callback = callback;
	this.imgType = img_type;
	this.request = null;
	this.show_loading = false;
	this.debug = false;
	this.init();
}

ajax_operation.prototype={// Pre-defined loading image type
	imgType_none:0,
	imgType_loading:1,
	imgType_analyse:2,
	init:function(){// Initialization
		var r=false;
		try{// Firefox-like browsers
			r = new XMLHttpRequest();
		}catch (ie){ 
  			try{// Microsoft IE
				r = new ActiveXObject("Msxml2.XMLHTTP");
			}catch (other_microsoft){
    			try{// Other browsers and versions of browsers
      				r = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (failed){
					r = false;
				}
			}
		}
		if (r){// AJAX created successfully
			this.request = r;
			var loader = this;
			this.request.onreadystatechange = function() {
				loader.processRequest();
			}
		}else{
			this.hideLoading();
			processException("E301","MID");
		}
	},

	doGet: function() {// HTTP GET request
		try {
			var surl = this.url;// Stamp the url to avoid IE caching the request
			if (surl.search(/\?/) < 0) {
				surl = surl + "?";
			} else {
				surl = surl + "&";
			}
			surl = surl + "stamp=" + Math.random();
			this.showLoading();// Show the loading prompt
			this.request.open("GET", surl, true);
			this.request.send(null);
		}catch (error){
			this.hideLoading();
			processException("E302","MIN");
		}
	},
	
	xmlGet: function() {
		try {
			var surl = this.url;// Stamp the url to avoid IE caching the request
			this.showLoading();	// Show the loading prompt
			try {
			    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			} catch (e) {
				alert("Permission UniversalBrowserRead denied.");
			}
			var loader = this;// Change the handler (temp)
			this.request.onreadystatechange = function() {
				loader.processXMLRequest();
			}
			this.request.open("GET", surl, true);
			this.request.send(null);
		} catch (error) {
			this.hideLoading();
			processException("E302","MIN");
		}
	},
	
	doGetSyn: function() {
		try {
			this.request.open("GET", this.url, false);
			this.request.send(null);
			if (this.request.status == 200) {
				this.handleResponseException(this.request.responseText);
				if (this.callback){
					this.callback(this.request.responseText);
				}
			}else{
				throw new JSException("E303","MIN");
			}
		}catch (error){
			if ( error instanceof JSException ){
				processException(error.getNo(), error.getLevel());
			} else {
				processException("E302","MIN");				
			}
		}
	},
    
	doPost: function(body) {// HTTP POST request
		try{
			this.showLoading();// Show the loading prompt
			this.request.open("POST", this.url, true);
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.request.send(body);
		}catch (error){
			this.hideLoading();
			processException("E302","MIN");
		}
	},
	
	processRequest: function() {// Request processing
		try {
			if (this.request.readyState == 4) {
				this.hideLoading();
				/*if (this.threadId != curThreadId){
					return ;
				}*/
					
				if (this.request.status == 200 || this.request.status == 0) {
					if (this.debug) {
						alert(this.request.responseText);
					}
					this.handleResponseException(this.request.responseText);
					if (this.callback){
						this.callback(this.request.responseText);//if do not have exception, call back it.
					}
				} else {
					throw new JSException("E303","MID");
				}
			}
		} catch (error) {
			this.hideLoading();
			if ( error instanceof JSException ){
				processException(error.getNo(), error.getLevel());
			} else {
				processException("E302","MIN");				
			}
		}
	},
	
	processXMLRequest: function() {// Request processing
		try {
			if (this.request.readyState == 4) {
				if (this.request.status == 200 || this.request.status == 0) {
					if (this.callback){
						this.callback(this.request.responseXML);//if do not have exception, call back it.
					}
					this.hideLoading();
				}else{
					throw new JSException("E303","MIN");
				}
			}
		}catch (error){
			this.hideLoading();
			if ( error instanceof JSException ){
				processException(error.getNo(), error.getLevel());
			} else {
				processException("E302","MIN");				
			}
		}
	},

	showLoading: function() {
		try{
			var loader = this;// Set the show_loading timer
			this.show_loading = window.setTimeout(function() {loader.showLoadingInterval();}, 0);
		} catch (error){
			processException("E400","MID");
		}
	},
	
	showLoadingInterval: function() {
		try {
			var la = this.getLoading();
			if (la != null) {//267
				this.paintLoading(la);
				
				la.style.visibility = "visible"; // Show the loading image
//				if(!xIE) {
//					la.style.position = "fixed";	//IE does not support position=fixed.
//				}
			}
		} catch (error) {
			processException("E400","MID");
		}
	},

	repaintLoading: function() {
		try {
			var la = this.getLoading();
			if (la.style.visibility == "visible") { // Update the loading image if it's currently shown
				this.paintLoading(la);
			} else {
				processException("E400","MID");
			}
		} catch (error) {
			processException("E400","MID");
		}
	},

	hideLoading: function() {
		try {
			window.clearTimeout(this.show_loading);// Unset the show_loading timer
			var la = this.getLoading();
			if (la != null) {
				la.style.visibility = "hidden"; // Hide the loading image
//				if(!xIE) {
//					la.style.position = null;
//				}
			}
		} catch (error) {
			processException("E400","MID");//303
		}
	},

	getLoading: function() {
		try {
			var la = null;
			if (this.imgType == this.imgType_loading) {
				la = document.getElementById("loading_area");
			} else if (this.imgType == this.imgType_analyse) {
				la = document.getElementById("analysing_area");
			} else {
				la = null; // No need to show any loading image
			}
			
			return la;
		} catch (error) {
			processException("E400","MID");
		}
	},

	paintLoading: function(la) {
		try {
			if (la != null) {
				var size = window.common.getSize();// Calculate where we shall display the loading image
				var winWidth = size[0];
				var winHeight = size[1];
				var scrollX = document.body.scrollLeft;
				var scrollY = document.body.scrollTop;
				la.style.left = winWidth/2 - la.offsetWidth/2 + scrollX;// Specify location of the loading image
				la.style.top = winHeight/2 - la.offsetHeight + scrollY;
			} else {
				processException("E400","MID");
				return null;
			}
		} catch (error) {
			processException("E400","MID");
			return null;
		}		
	},
	
	handleResponseException: function(response_text) {
		var text = response_text.replace(/\s/gi,"");
		if (text.length >= 4){
			text_prefix = text.substring(0,4);
			if (text_prefix.match( /^E\d\d\d/ )){
				throw new JSException(text_prefix,"MID");
			}
		}			
	},
	
	destroyAll: function() {
		try {
			this.hideLoading();
			this.request = null;
			this.callback = null;
			this.imgType = null;
		} catch(error) {
			processException("E316","MID");
		}
	},
	
	processException: function(obj1,obj2) {
	
	}
}

function grr(rp,str){
	if(RegExp.$1)/(.*)/.exec("");
	if (typeof(str)!="undefined")
	{
	   var re=new RegExp("<"+str+">(.*)<\/"+str+">");
	}
	else
	{
	   var re=new RegExp("<result>(.*)<\/result>");
	};
	re.exec(rp);
	if(RegExp.$1)return RegExp.$1;
	return "";
}

