//
// Bugninja AJAX Framework v0.1 BETA
// Copyright 2006
// 
// This file may not be used without the permission of Seltice Systems LLC
//

function loadDoc(docname,querystring,methodtype,rType,timer,responseFunc) {

	var MAXIMUM_WAITING_TIME=10000;
	var timeoutId=0;
	var requestTimer=0;

	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();
		  
		} catch (e) {
		  xmlhttp = false;
		  alert('No XML');
		  
		}

	}

	
	if (timer>0)
	{
		MAXIMUM_WAITING_TIME=timer;
	}

	if ( callInProgress(xmlhttp) ) {
		alert('Please wait, script is still running');
	} else {

     	if (xmlhttp) {

			if (methodtype=="GET")
			{
				xmlhttp.open("GET",docname+'?'+querystring,true);
			}

			if (methodtype=="POST")
			{
				xmlhttp.open("POST",docname,true);
				xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');			
				xmlhttp.send(querystring);
			}

						
			requestTimer = setTimeout(function() {
				xmlhttp.abort();
				alert('Script timed out, please try again.');
				}, MAXIMUM_WAITING_TIME);

			
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState!=4) { 
					// document.form1.textArea1.value=document.form1.textArea1.value+xmlhttp.readyState;
					return; 
				}
				clearTimeout(requestTimer);	
				// document.form1.textArea1.value=document.form1.textArea1.value+xmlhttp.readyState;			
				var fCall=responseFunc;
				if (typeof responseFunc!="function") 
					fCall = new Function(responseFunc);
				
				if (rType=='STR')
				{
					fCall(xmlhttp.responseText);
				} else if (rType=='XML')
				{
					fCall(xmlhttp.responseXML);
				}
			}

			if (methodtype=="GET")
			{
				xmlhttp.send(null);
			}

			

			
		} else {
			alert('No XML Available');
		}
	
	}

}


function callInProgress(xmlhttp) {
    switch ( xmlhttp.readyState ) {
		case 1, 2, 3:			
            return true;
        break;
        default:			
            return false;
        break;
    }	
}