/**
Name: SAL - Simple Ajax Lib. 
Desc: 
Author: Eli Itzhak
History:
	Date:		  Version:		Changes:
	'----------------------------------------------------------------------------------------------------------
    17-Oct-2006		v1.0		First Version
    11-Dec-2006		v1.1		Added status (0 - no change, -1 - error, 1 - started, 2 - ok), OnError fuction
*/

var debug = 0;
var statusOfAjaxResponse = 0;
var onErrorAjaxResponseFuction = "";

/**
<summary>
Check status function.
Returns the current status of XMLHttpRequest 
</summary>
*/
function GetAjaxResponseStatus() {
	return statusOfAjaxResponse;
}

/**
<summary>
Status changed function.
<param name="newStatus">The new status of AjaxResponse
</summary>
*/
function ChangedAjaxResponseStatus(newStatus) {
	statusOfAjaxResponse = newStatus;
	if(onErrorAjaxResponseFuction != "") {
		if(statusOfAjaxResponse == -1) {
			eval(onErrorAjaxResponseFuction + "()");
		}
	}
}

/**
<summary>
Set OnError function.
<param name="onErrorFunction">The function to call if 
the response has been finished with error.
</summary>
*/
function OnErrorAjaxResponse(onErrorFunction) {
	onErrorAjaxResponseFuction = onErrorFunction;
}

/**
<summary>
Browser Compatability function.
Returns the correct XMLHttpRequest 
  depending on the current browser.
</summary>
*/
function GetXmlHttp() {
  var xmlhttp = false;
  if (window.XMLHttpRequest)
  {
    xmlhttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject)// code for IE
  {
    try
    {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
      try
      {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
      } catch (E) {
        xmlhttp=false
      }
    }
  }
  return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls
   the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request
            to get the response data.</param>
<param name="callbackFunction">The function to call after 
the response has been recieved.
The response <b>must</b> always 
be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters 
you want to pass to the functions.
(Note: only constants/strings/globals can be passed
       as params, most variables will be out of scope.)
</param>
<example>
    <code>
    

PassAjaxResponseToFunction("?getsomehtml=1", "FunctionToHandleTheResponse", "'div1','div2','div3'" );

    
PassAjaxResponseToFunction('?getsomehtml=1', 
              'FunctionToHandleTheResponse', 
              "\'div1\',\'div2\',\'div3\'');

FunctionToHandleTheResponse2(response, 'div1','div2','div3') {
}

function FunctionToHandleTheResponse(response, d1, d2, d3) {
    var data = response.split(';');
    document.getElementById(d1).innerHTML = data[0];
    document.getElementById(d2).innerHTML = data[1];
    document.getElementById(d3).innerHTML = data[2];
}
    </code>
</example>
*/

function PassAjaxResponseToFunction(url, callbackFunction, params) {
	var xmlhttp = new GetXmlHttp();
	//now we got the XmlHttpRequest object, send the request.
	if (xmlhttp)  {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp && xmlhttp.readyState==4) { //we got something back..
				if (xmlhttp.status==200)  {
					var response = xmlhttp.responseText;
					var functionToCall = callbackFunction + '(response, '+params+')';
					if(debug) {
						alert(response);
						alert(functionToCall);
					}
					eval(functionToCall);
					ChangedAjaxResponseStatus(2);
                } 
                else {
					ChangedAjaxResponseStatus(-1);
					if(debug) { document.write(xmlhttp.responseText); }
				}
			}
        }
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
		ChangedAjaxResponseStatus(1);
	}
	else {
		ChangedAjaxResponseStatus(-1);
	}
}


/**
<summary>
Sets the innerHTML property of obj_id with the response from the passed url.
</summary>
<param name="url">The url to make the request 
to get the response data.</param>
<param name="obj_id">The object or the id of 
the object to set the innerHTML for.</param>
<param name="run_nfc">The function to run
after insert the data</param>
*/
function SetInnerHTMLFromAjaxResponse(url, obj_id, run_nfc) {
	var xmlhttp = new GetXmlHttp();
	 //now we got the XmlHttpRequest object, send the request.
	if (xmlhttp) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp && xmlhttp.readyState==4) {//we got something back..
				if (xmlhttp.status==200) {
					if(debug) { 
						alert(xmlhttp.responseText);
					}
					//if(typeof obj_id == 'object') {
					//	obj_id.innerHTML = xmlhttp.responseText;
					//} 
					//else {
						document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
						
						try{
							if(document.getElementById("Ajax_Run_Counter")){
								var scriptcount = document.getElementById("Ajax_Run_Counter").value;
								for(i=1 ; i<=scriptcount; i++){
									var tmpHTML = document.getElementById("ajaxRunScript_" + i);
									eval(tmpHTML.innerHTML);
								}
							}
						}
						catch(e){}
						
						if(run_nfc){
							eval(run_nfc);
						}
					//}
					ChangedAjaxResponseStatus(2);
                } 
                else {
					ChangedAjaxResponseStatus(-1);
					if(debug){ document.Write(xmlhttp.responseText); }
				}
			}
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
		ChangedAjaxResponseStatus(1);
	}
	else {
		ChangedAjaxResponseStatus(-1);
	}
}

/**
<summary>
Sets the innerHTML property of obj_id with the response from the passed url.
</summary>
<param name="url">The url to make the request 
to get the response data.</param>
<param name="obj_id">The object or the id of 
the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponse_WithXSL(url, obj_id, XSLfile) {
	var xmlhttp = new GetXmlHttp();
	 //now we got the XmlHttpRequest object, send the request.
	if (xmlhttp) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp && xmlhttp.readyState==4) {//we got something back..
				if (xmlhttp.status==200) {
					if(debug) { 
						alert(xmlhttp.responseText);
					}
					if(typeof obj_id == 'object') {
						obj_id.innerHTML = xmlhttp.responseText;
					} 
					else {
						document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
					}
                } else 
					if(debug){ document.Write(xmlhttp.responseText); }
			}
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}


/**
<summary>
Sets the value of javascript varialble with the response from the passed url.
</summary>
<param name="url">The url to make the request 
to get the response data.</param>
<param name="Variable">The object or the id of the object to set the innerHTML for.</param>
to be finished 
*/
function SetTextFromAjaxResponse(url, jsVariable) {
	var xmlhttp = new GetXmlHttp();
	 //now we got the XmlHttpRequest object, send the request.
	if (xmlhttp) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp && xmlhttp.readyState==4) {//we got something back..
				if (xmlhttp.status==200) {
					if(debug) { 
						alert(xmlhttp.responseText);
					}
					
					eval(jsVariable +"='"+ xmlhttp.responseText +"'" ); 
					ChangedAjaxResponseStatus(2);
                } 
                else {
					ChangedAjaxResponseStatus(-1);
					if(debug){ document.Write(xmlhttp.responseText); }
				}
			}
		}
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		ChangedAjaxResponseStatus(1);
	}
	else {
		ChangedAjaxResponseStatus(-1);
	}
}


