
/***

	This library takes 2 parameters which can be set in your code:

		svc_url := Web Service URL
	
		payload := HTTP POST variables
	
	
	The following id tag has to be used for the response DOM obj:
	
		wrapr
	
	
	This library also depends on the JSON javascript library as of version 1/2010:
	
		json_parse(text, reviver)
	
***/



var objXmlHttp,wrapr,isIE,svc_url,payload;

//This function initializes the XML Http Request Objects for various browser versions.

function ajaxStart(){
	
	var objXMLHttp;
	
	try{
						
		objXMLHttp = new XMLHttpRequest();	
		return(objXMLHttp);
		
	}
	catch(e){
		
		try{

			objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE		
			return(objXMLHttp);

		}
		catch(e){
			
			try{
				
				objXMLHttp = new ActiveXObject("MSXML2.XMLHTTP"); //later IE
				return(objXMLHttp);
				
			}
			catch(e){
				
				try{
					
					objXMLHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0"); //later IE					
					return(objXMLHttp);			
					
				}
				catch(e){
					
					objXMLHttp = null;
					wrapr.innerHTML = "?";		
					return(false);
					
				}
					
			}
			
		}
		
	}
	
}


/* This function handles various xmlhttprequest messages (events) and create the post and request methods in order to complete the asyncronous connection. */
function ajaxEngine(){

	var rnum;
	
	rnum = Math.round(((Math.random()*100)+1));
		
	if(objXmlHttp){
	
		objXmlHttp.onreadystatechange = function(){
							
				switch(objXmlHttp.readyState){
					
					case 0:
					
//						wrapr.innerHTML = "Please Wait ..."; //debug
						break;
		
					case 1:
		
//						wrapr.innerHTML = "Initialized and Opened, Please Wait ..."; //debug
						break;
		
					case 2:
		
//						wrapr.innerHTML = "Sent, Please Wait ...";  //debug
						break;
		
					case 3:
		
//S						wrapr.innerHTML = "Receiving, Please Wait ...";  //debug

						if(!isIE){
							
							try{
	
								switch(objXmlHttp.status)
								{
	
	
									case 404:
							
//										wrapr.innerHTML = "Failure -- Code 404: "+objXmlHttp.statusText; //debug
										break;
									
									case 200:
						
										//wrapr.innerHTML = "Status: Transmission Complete!"; //debug
											
	//									wrapr.innerHTML = "Response is :"+objXmlHttp.responseText; //Debug
	
										callback(json_parse(objXmlHttp.responseText));
										
										objXmlHttp.close;		
										
										break;
								
									
									default:
									
//										wrapr.innerHTML = "Failure -- Code "+objXmlHttp.status+": "+objXmlHttp.statusText;	
									break;	
	
		
								}
								
							}
							catch(e){
							   //	
							}
							
						}
						
					break;
						
					case 4:
					
					
						if(isIE){
								
							try{
									if(objXmlHttp.status == 200){
																	
										//wrapr.innerHTML = "Status: Transmission Complete!"; //debug

//										wrapr.innerHTML = "Response is :"+objXmlHttp.responseText; //debug

										callback(json_parse(objXmlHttp.responseText));
									}
									objXmlHttp.close;
								}
								catch(e){
								 //
								}
								
						}
					
					
						break;							
				}
				
		}
		  objXmlHttp.open("POST",svc_url,true);					  
		  objXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");			 

		  objXmlHttp.send(payload);
	}
}




/* This function handles the response array that his returned from the ajax request method.
*/
function callback(obj){
	
	wrapr.innerHTML = obj.objArray;
	
}	


/*** This function gets the DOM objects and create an instance of the xmlhttprequest object as well as invoke the ajax engine ***/
function init(){

	isIE = (window.navigator.userAgent.indexOf("MSIE") != -1) ? true : false;

	wrapr = document.getElementById("wrapr");

	objXmlHttp = ajaxStart();
	
	ajaxEngine();

}

