/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

window.debug = false;
/**
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;
}


/**
<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)
{		
  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(window.debug){
				                                alert(response);
				                                alert (functionToCall);
			                                }
			                                eval(functionToCall);
		                                } else if(window.debug){
			                                document.write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",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)
{		
    
  

  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..
		                           //alert(xmlhttp.status+" "+url);
		                                if (xmlhttp.status==200)
		                                {
			                                try{
			                                    if(typeof obj_id == 'object')
			                                    {
                                                    obj_id.innerHTML = xmlhttp.responseText;
			                                    } else {
				                                    document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
			                                    }
			                                }catch(ex){}
		                                } else if(window.debug){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}


function UpdateResponseScript(url)
{
  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(window.debug){
//				                                alert(xmlhttp.responseText);
//		
//			                                }
                                            try{
			                                    eval(xmlhttp.responseText);
			                                }catch(exce){alert("error : " + exce); alert(xmlhttp.responseText);}
		                                } else if(window.debug){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

function UpdateUserControl(url, obj_id)
{		
  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 rstring=xmlhttp.responseText;
			                                var fromindex = rstring.indexOf('<br style="display:none;ajax:start" />');
			                                if(fromindex >= 0 )
			                                {
			                                   rstring=rstring.substring(fromindex,rstring.indexOf('<br style="display:none;ajax:end" />'));
    		                                }
    		                                else
    		                                {
    		                                    fromindex = rstring.indexOf('<form ');
    		                                    if(fromindex>=0)
    		                                    {
    		                                        fromindex = rstring.indexOf('>',fromindex+1);
    		                                        rstring=rstring.substring(fromindex+1,rstring.indexOf('</form>'));
    		                                    }
    		                                }

				                    
			                                if(typeof obj_id == 'object'){
				                                obj_id.innerHTML = rstring
			                                } else {
				                                document.getElementById(obj_id).innerHTML = rstring
			                                }
			                                try{
			                            	    var cookiebegin = rstring.indexOf("<cookie");
			                                    if(cookiebegin >= 0)
			                                    {
			                                        rstring = rstring.substring(cookiebegin+8,rstring.indexOf(">",cookiebegin+1));
                                                    cookiebegin=rstring.indexOf("\"");                                    			                                        
			                                        rstring = rstring.substring(cookiebegin+1,rstring.indexOf("\"",cookiebegin+1));
			                                        var cokkienamevalue = rstring.split(":");
			                                        SetCookie(cokkienamevalue[0],cokkienamevalue[1],cokkienamevalue[2]);
			                                    }
			                                  }catch(e){}
		                                } else if(window.debug==true){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
  return false;
}

function SetCookie(cookieName,cookieValue,nDays) {
     var today = new Date();
     var expire = new Date();
     if (nDays==null || nDays==0) nDays=1;
     expire.setTime(today.getTime() + 3600000*24*nDays);
     document.cookie = cookieName+"="+escape(cookieValue)
				     + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
     var theCookie=""+document.cookie;
     var ind=theCookie.indexOf(cookieName);
     if (ind==-1 || cookieName=="") return ""; 
     var ind1=theCookie.indexOf(';',ind);
     if (ind1==-1) ind1=theCookie.length; 
     return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
function clearCombo(cboX){
    for(i = cboX.options.length; i > 0; i--){
        cboX.options[i]=null;
    }  
}  