// 					Copyright Nitin Vericherla
// 		May not be reproduced without express written permission

//________________________________________________________________________________________ 
//________________________________________________________________________________________ AJAX encapsulation

/*
 * Usage :	.startGET(the page, "TEXT" or "XML" , "" or "function to call when finished()", "" or "DOM ID for notification textbox");
 * 			.startPOST(the page, "TEXT" or "XML" , "" or "function to call when finished()", "" or "DOM ID for notification textbox");
 * 
*/

function NV_ajax() {
	this.sendGET = NV_ajax_startSend;
	this.sendPOST = NV_ajax_startSendOnly;
	this.get_HttpObject = NV_ajax_getHttpObject;
	this.retrieveXML = NV_ajax_retrieveXML;
	this.retrieveText = NV_ajax_retrieveText;
	this.inProgress = NV_ajax_inProgress;
	this.handleResponse = NV_ajax_handleResponse;
	
	var httpConn = NV_ajax_getHttpObject(); // Connection object
	var F_working = false; // Flag for 'in progress'
	var xmlDoc = ""; // Stores responseXML object if in XML mode
	var textDoc = ""; // Stores responseText if in Text mode
	var the_datatype = ""; // 'XML' or 'TEXT'
	var the_finisher = ""; // function to call when finished
	var the_notifier = ""; // the element that relays progress information
	
	function NV_ajax_retrieveXML() {
		return xmlDoc;
	}
	
	function NV_ajax_retrieveText() {
		return textDoc;
	}
	
	function NV_ajax_inProgress() {
		return F_working;
	}
	
	function NV_ajax_handleResponse() {

		if (httpConn.readyState == 4) {
			
			if (the_datatype == "XML")
				xmlDoc = httpConn.responseXML;
			else
				textDoc = httpConn.responseText;
			
			if (the_notifier != "")
				document.getElementById(the_notifier).value = "";
			
			F_working = false;
			delete httpConn;
			
			if (the_finisher != "")
				eval("window." + the_finisher + ";");
			
		}
	}
	
	
	function NV_ajax_startSend(thestr,thetype,thefinisher,thenotifier) {
		if (httpConn == null)
			httpConn = NV_ajax_getHttpObject();
		
		if (!F_working && httpConn) {
			if (thenotifier != "")
				document.getElementById(thenotifier).value = " Working ...";
			
			// Add random number to prevent caching in IE
			if (thestr.indexOf('?') == -1)
				thestr += "?the_sand=" + Math.random();
			else
				thestr += "&the_sand=" + Math.random();
			
			//httpConn.clear_cache();
			httpConn.open("GET", thestr, true);
			
			the_datatype = thetype;
			the_finisher = thefinisher;
			the_notifier = thenotifier;
			
			httpConn.onreadystatechange = NV_ajax_handleResponse;
			
			F_working = true;
			httpConn.send(null);
		}
	}
	
	function NV_ajax_startSendOnly(thestr,thetype,thefinisher,thenotifier) {
		
		if (httpConn == null)
			httpConn = NV_ajax_getHttpObject();
		
		if (!F_working && httpConn) {
			if (thenotifier != "")
				document.getElementById(thenotifier).value = " Working ...";
			
			var thestr_parts = thestr.split("?");
			var the_vars = "";
			
			if (thestr_parts.length > 1)
				the_vars += "&" + thestr_parts[1];
			
			httpConn.open("POST", thestr_parts[0] + "?the_sand=" + Math.random(), true);
			httpConn.onreadystatechange = NV_ajax_handleResponse;
			
			the_datatype = thetype;
			the_finisher = thefinisher;
			the_notifier = thenotifier;
			
			httpConn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			F_working = true;
			httpConn.send(the_vars);
		}
	}
	
	function NV_ajax_getHttpObject() {
		var xmlhttp;
		/*@cc_on
		@if (@_jscript_version >= 5)
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					xmlhttp = false;
				}
			}
		@else
			xmlhttp = false;
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			try {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/xml"); 
			} catch (e) {
				xmlhttp = false;
			}
		}
	
		return xmlhttp;
	}
}

