// JavaScript Document

//AjaxManager constructor
function AjaxManager(reqType,url,async,fBeforeSend,fAfterSend,fProcess){
	
	this.reqType=reqType;
	
	if (reqType==0){
		this.url=url + "?rnd=" + parseInt(Math.random()*99999999);
	}
	else{
		this.url=url;
	}
	
	this.async=async;	
	
	this.fBeforeSend=fBeforeSend;
	this.fAfterSend=fAfterSend;
	this.fProcess=fProcess;
	
	this.input=null;	
	this.request;	
	this.webservNS;
	this.webservMTD;
	
	this.setRequest();	
	var current=this;
	//get the response
	this.getResponse=function(){	
	
		//process the request only if readyState is 4 and status is ok (200)
		if (current.request.readyState==4){
			if(current.request.status==200){
				AjaxManager.prototype.TEXTData=current.request.responseText;			
				try{
					AjaxManager.prototype.XMLData=current.request.responseXML;
				}
				catch(err){
					errDisplay("Err: "+err);
				}
				//eval process function if any				
				if(current.fProcess){					
					eval(current.fProcess+'(current.request.responseText,current.request.responseXML)');				
				}
				//eval "end response" function if is specified and if asyc property is set to true
				if (current.fAfterSend){			
					eval(current.fAfterSend+"();");
				}					
			}
			else{
				//is status is not 200 display the status
				errDisplay("Status Err:"+current.request.status);
			}		
		}			
	}
	
}

//create XMLHttpRequest object
AjaxManager.prototype.setRequest=function () {	
	try {
		//native object(Mozilla,Opera,NN)
		this.request = new XMLHttpRequest();
	} 
	catch (err1) {
		//ActiveX object IE
		try {
			this.request = new ActiveXObject("Msxml2.XMLHTTP");			
		} 
		catch (err2) {
			try {
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (err3) {
				//null if unable to be created + display error message
				this.request = null;
				errDisplay('Err: Unable to create request object');
      		}
		}
	}
}

//setup the request parameters if any
AjaxManager.prototype.addParams=function(key,value){	
	switch (this.reqType) {
		//Page GET request 
		case 0:
			this.url=this.url + "&" + key + "=" + value;
			break;
		//Page POST request
		case 1:		
			if (this.input=="" || this.input==null){
				this.input=key + "=" + value;
			}
			else{
				this.input=this.input + "&" + key + "=" + value;
			}
			break;
		//Web Serv request
		case 2:
			if (this.input=="" || this.input==null){
				this.input='<'+key + ">" + value + '</'+key+'>';
			}
			else{
				this.input=this.input + '<'+key + '>' + value + '</'+key+'>';
			}			
			break;
	}	
}

//setup the request object and sent the request
AjaxManager.prototype.sendRequest=function(manager){
		
	if(this.fBeforeSend){
		eval(this.fBeforeSend+"();");		
	}		
	
	switch (this.reqType) {
		//Page GET request
		case 0:		
			//open request by passing the corresponding method, url and the async flag 
			this.request.open('GET',this.url,this.async);					
			break;
		//Page POST request
		case 1:
			//open request by passing the corresponding method, url and the async flag
			this.request.open('POST',this.url,this.async);			
			//set the request header conform to the POST method
		 	this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
			break;
		//Web Serv request
		case 2:
			//open request by passing the corresponding method, url and the async flag
			this.request.open('POST',this.url,this.async);		
			//set the request header conform to the Web Serv method
			this.request.setRequestHeader("Content-Type","text/xml");
			if(this.webservNS.lastIndexOf('/')==this.webservNS.length-1){
				name=this.webservNS + this.webservMTD;
			}
			else{
				name=this.webservNS + '/' + this.webservMTD;
			}
			//create the evelope according to the SOAP standard
			this.input='<?xml version="1.0" encoding="utf-8"?>'+
				'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
				  '<soap:Body>'+
					'<'+this.webservMTD+' xmlns="'+this.webservNS+'">'+
						this.input+
					'</'+this.webservMTD+'>'+
				  '</soap:Body>'+
				'</soap:Envelope>';
			//set the request header conform to the Web Serv method
			this.request.setRequestHeader("SOAPAction",name);
			break;
	}
	if (this.async){
		this.request.onreadystatechange = this.getResponse;
	}
	
	this.request.send(this.input);	
	
	if (!this.async){		
		this.getResponse();		
	}				
}

function errDisplay(errText){
		alert(errText);
}




 