﻿/*
//The XMLHTTP object is supposed to cater to the need of AJAX across the organization.
//The following properties / Methods would be available for the user to set
////Properties
	/
	-ReadyStateChangeEventHandler
		Mention the client side callback event that you would like to be called when the XMLHTTP request ends
	-Async
		Mention if the request to web should be done asynchronously or synchronously
	
////Methods
	-SendRequest
		Method to send the request. The function accepts the following parameters	
		-sUrl - The URL path to which the request should be sent to
		-sQueryStr - The data to be sent across to the server
		-sMethod - Accepts: "POST" / "GET".
			* If "POST" then the data would be available as a form variable on the server
			* If "GET" then the data would be available as a query string variable on the server.
*/
function ActlXmlHttp()
{
	var userAgent = navigator.userAgent.toLowerCase();
	var __sBrowserType = "";
	var IsFormSubmitted = false;
	var sCallback = "";
	var sDefaultCallback = "fnACTLEvtHandler";
	var sCallbackTag = "ACTLEvtHandler";
	var sReadyStateChange = "";
	
	this.Init_XmlHttp = __Init_XmlHttp;
	this.SendRequest = __SendRequest;
	this.Async = true;
	
	this.ReadyStateChangeEventHandler = null;


	function __Message(str)
	{
		alert(str);
	}

	function __Init_XmlHttp() 
	{
 		var A;
		try 
		{
			A=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try 
			{
				A=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (oc)
			{
				A=null;
			}
		}
		if(!A && typeof XMLHttpRequest != "undefined")
		{
			A = new XMLHttpRequest();
		}
		return A;
	}

	if( eval( userAgent.indexOf('msie') != -1 ) )
	{
		__sBrowserType = "IE";
	}
	else if( eval(navigator.product == 'Gecko') )
	{
		__sBrowserType = "FF";
	}


	/*
	// this.SendRequest(sUrl, sQueryStr, sMethod)
	// sUrl -  Full URL for the request to be submitted
	// sQueryStr - The query string to be submitted via GET or POST
	// sMethod - POST / GET based on whether you would like a Form submit or a Querystring posted to your page
	*/
	function __SendRequest(sUrl, sQueryStr, sMethod)
	{
		var s="";
		var numargs = arguments.length;
		var expargs = __SendRequest.length;
		var formData;
		var xmlDocument;

		if (expargs != numargs)
		{
			__Message("Insufficient no. of arguments to send XmlHttp request.");
			return;
		}
	
		
		var xmlhttp;
		
		if (this.ReadyStateChangeEventHandler != null)
		{
			sQueryStr = sQueryStr + "&ACTLEvtHandler=" + this.ReadyStateChangeEventHandler
			sReadyStateChange = this.ReadyStateChangeEventHandler;
		}


		formData = sQueryStr;
		
		var oXmlHttp = __Init_XmlHttp();		
		var bCallBackFound = false;
		var oError = null;
		
		if (oXmlHttp) 
		{
			switch (sMethod.toUpperCase())
			{
				case "POST":
				{
					formData = sQueryStr;		
					IsFormSubmitted = true;
					break;
				}
				case "GET":
				{
					formData = null;
					if( sUrl.indexOf( "?" ) == -1 )
					{
						sUrl = sUrl + "?" + sQueryStr;
					}
					else
					{
						sUrl = sUrl + "&" + sQueryStr;
					}
					break;
				}

			}
			

			oXmlHttp.open(sMethod, sUrl, this.Async); // true --> Asynchronous | false --> Synchronous				
			
			if (IsFormSubmitted)
			{
				oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			
			//if the return type has to be xml
			//oXmlHttp.overrideMimeType('text/xml');

			/*Event for the XMLHTTP object readystatechange*/
			oXmlHttp.onreadystatechange = function() {
				
				if (oXmlHttp.readyState == 4) 
				{
					// only if "OK"
					if (oXmlHttp.status == 200) 
					{
						// ...processing statements go here...
						try
						{							
							switch (String(__sBrowserType).toLowerCase())
							{
								//Check Whether responseXML is WellFormed or not.
								case "ff":
								{
									var parser = new DOMParser();
									var doc = parser.parseFromString(oXmlHttp.responseText, "text/xml");
									var roottag = doc.documentElement;			
									
									if (roottag.tagName == "parsererror")
									{
										oError = roottag.childNodes[0].textContent;
									}
									else
									{
										this.sCallback = oXmlHttp.responseXML.getElementsByTagName(sCallbackTag)[0].textContent;										
										bCallBackFound = true;
									}
									break;									
								}
								
								case "ie":
								{
									if(oXmlHttp.responseXML.parseError.errorCode != 0)
									{
										oError = oXmlHttp.responseXML.parseError.reason; 
									}
									else
									{
										this.sCallback = oXmlHttp.responseXML.getElementsByTagName(sCallbackTag)[0].text;
										bCallBackFound = true;
									}
									break;
								}
								default :
									break;							
							}
						}
						catch (ex)
						{
								
						}
						finally
						{
							if(!bCallBackFound)
							{
								if (sReadyStateChange != "")
								{	
									this.sCallback = sReadyStateChange;
								}
								else
								{
									this.sCallback = sDefaultCallback;
								}
							}							
							this.sCallback = this.sCallback + "(oXmlHttp, oError)";			
						}
						eval(this.sCallback)	
					} 
					else 
					{
						//generic function for alert. Write debug/error handling/timeout messages here
						alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
					}
				}
			}
		
			oXmlHttp.send(formData);
			delete oXmlHttp;
		} 
		else
		{
			__Message("Could not create connection object.");
		}

	}
}
