/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
var animationRequests = 1;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
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;
}

function TimingUrl(url) {
	if (url.indexOf('?')>-1)
		return (url + "&timestamp=" + (new Date()).toUTCString());
	else
		return (url + "?timestamp=" + (new Date()).toUTCString());
}


/**
<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>
</summary>
<example>
	<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'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)
{	
  showLoadingAnimation();
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (debug)
  	alert(url);
  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);
		                                } else if(debug){
			                                document.write(xmlhttp.responseText);
		                                }
										hideLoadingAnimation();
	                                }									
                                }
    xmlhttp.open("GET",TimingUrl(url),true);
    xmlhttp.send(null);
  }
}


/**
///<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(url, obj_id, callbackFunction)
{		
  showLoadingAnimation();
  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 out = xmlhttp.responseText;
			                                if(debug){
				                                alert(out);
			                                }
			                                if(typeof obj_id == 'object'){
				                                obj_id.innerHTML = out;
			                                } else {
				                                document.getElementById(obj_id).innerHTML = out;
			                                }
		                                    if (out.indexOf('// [jsruntime begin]')>-1 && 
		                                        out.indexOf('// [jsruntime end]') > out.indexOf('// [jsruntime begin]')) {
		                                        var jscode = out.substring(out.indexOf('// [jsruntime begin]') + 20,
		                                            out.indexOf('// [jsruntime end]'));
		                                        eval(jscode);
		                                    }
		                                } else if(debug){
			                                //document.Write(xmlhttp.responseText);
		                                }
										if (callbackFunction)
											eval(callbackFunction + "()");
										hideLoadingAnimation();
	                                }									
                                }
    xmlhttp.open("GET",TimingUrl(url),true);
    xmlhttp.send(null);
  }
}

function showLoadingAnimation() {
	animationRequests++;
	$('loading_animation').style.display='block';
}

function hideLoadingAnimation() {
	animationRequests--;
	if (animationRequests<=0) {
		$('loading_animation').style.display='none';
		animationRequests=0;
	}
}

function stopEvent(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}


//------------------------------------------------------------------------------------------------

function getEventTarget(mEvent)
{
  // Internet Explorer
  if (mEvent.srcElement)
  {
    return mEvent.srcElement;
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
    return mEvent.target;
  }
}

function parseResult(r) {
	var obj=new Object();
	var pars = r.split('&')
	for (i=0;i<pars.length;i++) {
    	var p = pars[i].split('=')[0];
        var v = pars[i].split('=')[1];
		obj[p]=v;
	}
	return obj;
}
