// JavaScript Document
/*
======================================================================================================
  Name:  AJAXManager.js
======================================================================================================
  Date:   12/06/06
  Notes:  JavaScript class definition for handling AJAX calls 
  Author: Viorel

======================================================================================================
  Viorel		12/06/06			Created
======================================================================================================
  Observations:
  In order to use AJAXManager first of all must be created an AJAXManager object by calling the AJAXManager constructor, e.g.:
  		var oReqManager=new AJAXManager(0,'myajaxpage.asp',false,null,null,'showLoadMess','hideLoadMess','processReq');
  The class constructor take a range of parameters each of which are explained below:
	  ->reqType(integer)-request type; possible value are:
			0-page call using GET method;
			1-page call using POST method;
			2-.NET web services call;
			-if other values are passed the AJAXManager will fail to respond.
	  ->url(string)-the url of the page or webservice to be opened via AJAXManager object
	  ->async(boolean)-sets the way in which the AJAX request will be performed; possible values are:
			true-corresponding to a asynchronous request
		    false-corresponding to a synchronous request
	  ->webservNS(string)-the webservice namespace; this parameter is mandatory when the reqType is 2 otherwise webservNS will be ignored 
	  ->webservMTD(string)-the webservice method name; this parameter is mandatory when the reqType is 2 otherwise will be ignored 
	  ->fLoadName(string)-the name of a user defined function which will be executed when the AJAX request begin
	  ->fEndName(string)-the name of a user defined function which will be executed when the AJAX request ends
	   		Note:fLoadName,fEndName params will take effects just when the async param is set to true, 
		  		 otherwise neither fLoadName function or fEndName function will not be executed 
	  ->fProcessName(string)-the name of a user defined function that will process the response. 
	  		This function must be always defined with 2 parameters.The first one represent the responseText and the second one the responseXML.
	  
   AJAXManager methods:
	  ->setRequest()-create a XMLHttpRequest object. This function is automatically called when an AJAXManager object is created,
	  so is no need to be express called.
	  ->addParams(key,value)-via this method, parameters (qs params,form params or webserv method params) (if any) can be added to your request.
		   	e.g. :
		  	oReqManager.addParams('pname1','pval1');
		  	oReqManager.addParams('pname2','pval2');.....
	  ->sendRequest()-this function must be called in order to send the request and to obtain the response (and process it - if fProcessName is specified)
					 -in additional to fProcessName (or if fProcessName is null) the responseXML and responseText will be available via XMLData and TEXTData AJAXManager object members
			Note: if async is set to true the XMLData and TEXTData AJAXManager object members will not be available immediately; 
		  		  you must build a recursive function (or find another way) in order to determine when the request is done and XMLData and TEXTData are defined.
				  Instead you can use fProcessName.
*/


var req;
var funcEnd;
var funcProcess;
var flAsync;

var TEXTData;
var XMLData;

//AJAXManager constructor
function AJAXManager(reqType,url,async,webservNS,webservMTD,fLoadName,fEndName,fProcessName){
	
	this.reqType=reqType;
	
	if (reqType==0){
		this.url=url + "?rnd=" + parseInt(Math.random()*99999999);
	}
	else{
		this.url=url;
	}
	
	if(async){
		this.async=async;
	}
	else{
		this.async=false;
	}
	
	this.fLoadName=fLoadName;
	this.fEndName=fEndName;
	this.fProcessName=fProcessName;
	
	this.input=null;	
	this.request;	
	
	this.webservNS=webservNS;
	this.webservMTD=webservMTD;
	
	this.setRequest();		
}

//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;
	}	
}

//get the response
function getResponse(){	
	//process the request only if readyState is 4 and status is ok (200)	
	if (req.readyState==4){		
		if(req.status==200){
			AJAXManager.prototype.TEXTData=req.responseText;			
			try{
				AJAXManager.prototype.XMLData=req.responseXML;
			}
			catch(err){
				errDisplay("Err: "+err);
			}
			//eval process function if any 
			if(funcProcess){
				eval(funcProcess+'(req.responseText,req.responseXML)');				
			}
			//eval "end response" function if is specified and if asyc property is set to true
			if (funcEnd && flAsync==true){			
				eval(funcEnd+"();");
			}					
		}
		else{
			//is status is not 200 display the status
			errDisplay("Status Err:"+req.status);
		}		
	}	
}

//setup the request object and sent the request
AJAXManager.prototype.sendRequest=function(){
	req=this.request;
	flAsync=this.async;
	
	if(this.async==true && this.fLoadName){
		eval(this.fLoadName+"();");		
	}	
	if (this.fEndName){
		funcEnd=this.fEndName;
	}	
	if (this.fProcessName){
		funcProcess=this.fProcessName;		
	}
	
	switch (this.reqType) {
		//Page GET request
		case 0:		
			//open request by passing the corresponding method, url and the async flag 				
			req.open('GET',this.url,this.async);			
			req.onreadystatechange = getResponse;			
			break;
		//Page POST request
		case 1:
			//open request by passing the corresponding method, url and the async flag
			req.open('POST',this.url,this.async);
			req.onreadystatechange = getResponse;
			//set the request header conform to the POST method
		 	req.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
			req.open('POST',this.url,this.async);
			req.onreadystatechange = getResponse;
			//set the request header conform to the Web Serv method
			req.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
			req.setRequestHeader("SOAPAction",name);
			break;
	}
	req.send(this.input);	
				
}

//pass the response text to a div inner content
AJAXManager.prototype.fillContainer=function(oContainer,flAppend){
	if (this.TEXTData){
		if(flAppend){
			oContainer.innerHTML=oContainer.innerHTML+this.TEXTData;
		}
		else{
			oContainer.innerHTML=this.TEXTData;
		}
	}	
}

function errDisplay(errText){
		alert(errText);
}




 