
var ajaxObj = new Array();
var t = new Array(); /*TempAjax*/
/*var ajaxProcess = new Array();*/
var isIE = false;

/*
* @Class Ajax
* @version 2.0 
* @author vasupat
* @include javascript.common.js
* @include foating.js
*/

function ajax(){
	
	/*this.progress = "<div style=\"text-align:center; margin:0px; padding:0px;\"><img src=\"http://localhost/project/keepmarket/_wwwwroot/_market/_files/images/icons/progress.gif\" width=\"16\" height=\"16\"></div>";*/
	this.xmlhttp;
	this.formElement;
	this.requestFile;
	this.URLString;
	this.innerDivID;
	this.method 		= 'GET';
	this.responseType 	= 'Text'; /*Text , XML*/
	this.cProcess 		= false;
	this.loadingType 	= 0;
	this.async 			= true;
	this.runloading		= false;
	this.onCompletion 	= function() { };
	this.onProcess 		= function() { };
	this.loading 		= function(v){
		var v;
		if(this.loadingType>0){
			if(this.loadingType==1){
				if(!g("loading")) {this.createLoaddingDiv();}
			}
			if(v==1){
				if(this.loadingType==1){ 
					g("loading").style.visibility = "visible";
				}else if(this.loadingType==3){ 
					if(this.runloading == false){
						myBlockUI.activate();
						this.runloading = true;
					}
				}
			}else{
				if(this.loadingType==1){ 
					g("loading").style.visibility = "hidden";
				}if(this.loadingType==3){ 
					myBlockUI.deactivate();
					this.runloading = false;
				}
			}
		}
	}
	
	this.ConnXmlHttp = function(){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				isIE = true;
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				xmlhttp = false;
			}
		}
	
		if(!xmlhttp && document.createElement){
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
	}
	this.getMultiple = function(ob){
		var multiParams = new Array();
		while (ob.selectedIndex != -1)
		{
			if (ob.selectedIndex != -1){
            	var multiParam = encodeURIComponent(ob.name);
               	multiParam += "=";
               	multiParam += encodeURIComponent(ob.options[ob.selectedIndex].value);
               	multiParams.push(multiParam);
			}
			ob.options[ob.selectedIndex].selected = false;
		}
		return multiParams.join("&");
	}
	this.getRequestBody = function(myForm) {
		var aParams = new Array();  
			for (var i=0 ; i < myForm.elements.length; i++) {
				
				var formElement = myForm.elements[i];
				if(formElement.type=='checkbox' && !formElement.checked){ continue;}
				if(formElement.type=='select-multiple'){aParams.push(this.getMultiple(g(formElement.id))); continue;}
				
            	var sParam = encodeURIComponent(myForm.elements[i].name);
               	sParam += "=";
               	sParam += encodeURIComponent(myForm.elements[i].value);
               	aParams.push(sParam);
           	}     
           	return aParams.join("&");        
	}
	
	this.loadXMLDoc = function() {
	
		var self = this;
		this.xmlhttp = this.ConnXmlHttp();

		
		if (this.method == "GET") {
			/*var totalurlstring = this.requestFile + "?" + this.URLString;*/
			this.xmlhttp.open(this.method, this.requestFile, this.async);
		}else{
			this.URLString = this.getRequestBody(this.formElement);
			/*alert(this.URLString);*/
			this.xmlhttp.open(this.method,this.requestFile, this.async);
		}
		if (this.method == "POST"){
  			try {
				this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
			} catch (e) {}
		}
		this.xmlhttp.onreadystatechange = function(){
			if(self.xmlhttp.readyState==4){
				if(self.loadingType==1){ self.loading(0);}
				if(self.loadingType==3){ self.loading(0);}
				/*self.cProcess=true; self.onProcess();*/
				if (self.xmlhttp.status == 200) {
					if(self.responseType == "Text"){
						self.response = self.xmlhttp.responseText;
					}else{
						self.response = self.xmlhttp.responseXML;
					}	
					
					self.onCompletion();
				}
			}else{
				/*self.onProcess();*/
				if(self.loadingType==1){ self.loading(1);}
				if(self.loadingType==3){ self.loading(1);}
				if(self.loadingType==2){ 
					self.response = "Loading...";
					/*self.response = self.progress;*/
					self.onCompletion();
				}
			}
		}
		this.xmlhttp.send(this.URLString);
	}
	
	/*
	* Loading
	*/
	
	this.createLoaddingDiv = function(){
	
		var connLoading = document.createElement("DIV")
			connLoading.id = "loading";
			connLoading.style.position="absolute";
			connLoading.style.left="0px";
			connLoading.style.top="0px";
			connLoading.style.width="185px";
			connLoading.style.height="15px";
			connLoading.style.zIndex=99999;
			connLoading.style.backgroundColor="#FF0000";
			connLoading.style.visibility = "visible";
			connLoading.style.font = '11px Lucida Sans Unicode';
			connLoading.style.filter = 'alpha(opacity=60)';
			connLoading.innerHTML = "<strong><font color=\"#FFFFFF\">&nbsp;working on your request...</font></strong>";
		document.body.appendChild(connLoading);
		
		if(g('loading')){
				if (document.all||document.getElementById||document.layers){
					setInterval("JSFX_FloatTopDiv()",20);
				}
		}else{
			alert('message error!');
		}
		
	}
}


function getRootNode(xmlHttpReq,tagName) {
	return xmlHttpReq.getElementsByTagName(tagName);
}
/*
* ex getNodeValue("","html_content","root[0]",0)
*/

function getNodeValue(prefix, local, parentElem, index) {
	try{
		var result = "";
		if (prefix && isIE) {
			/* IE/Windows way of handling namespaces */
			result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
		} else {
			result = parentElem.getElementsByTagName(local)[index];
		}
		if (result) {
			/* get text, accounting for possible */
			if (result.childNodes.length > 1) {
				return result.childNodes[1].nodeValue;
			} else {
				return result.firstChild.nodeValue;    		
			}
		} else {
			return "n/a";
		}
	}
	catch(e){
		alert("xml error");
	}
}

function tempObj(index,d){
	if(d==false){
		return t[index];
	}else{
		t[index] = d;
	}
}
function checkT(index){
	if(t[index]==null || t[index]=="" || t[index]==Undefined){
		return false;
	}else{
		return true;	
	}
}

function checkProcess(index){
	if(!ajaxProcess[index]){
		return false;
	}else{
		return true;
	}
}