/*
******************************************************************************
' Name:	ajax.js
'
' Shared: Yes
'
' Application: ?
'
' Purpose: To encapsulate AJAX JavaScript functions.
'
' Linked From: parent
'
' Requires:
'
' Linked To:  
'
' Created: SR 12/06/2005
'
' History: 
'       ~ 1 DC  12/08/2009 - #115915 Added ability to not auto-select if drop-down list has only 1 item.
******************************************************************************
*/

//Global XMLHTTP Request object
var XmlHttp;

function CreateXmlHttp() 
{
    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttp && typeof XMLHttpRequest != "undefined")
    {
        XmlHttp = new XMLHttpRequest();
    }

    // If we still don't have an object reference, fall back to IFRAME approach
    if(!XmlHttp)
    {
        XmlHttp = new XMLHttpRequest_iframe();
    }    
}

/*
coded by Kae - http://verens.com/
use this code as you wish, but retain this notice
*/

function XMLHttpRequest_iframe() 
{
	var i = 0;
	var url = "";
	var responseText = "";
	this.onreadystatechange = function() {return false;}
	this.open = function(method,url)
		{
		//TODO: POST methods
		this.i = ++gsi_instances; // id number of this request
		this.url = url;
		var iframe = document.createElement('<iframe id="gsi_iframe_' + this.i + '" type="text/plain" style="display:none"></iframe>');
		document.body.appendChild(iframe);
		}
	this.send = function(postdata)
		{
		//TODO: use the postdata
		var el = document.getElementById('gsi_iframe_' + this.i);
		// SR 05/03/2006 - added form data to url
		if (postdata && postdata.length > 0)
		{
			this.url += '&' + postdata;
		}
		el.src = this.url;
		gsi_objs[this.i] = this;
		setTimeout('XMLHttpRequest_checkState(' + this.i + ')',500);
		}
	// SR 05/03/2006 - added property
	this.setRequestHeader = function() {return true;}
	return true;
}

function XMLHttpRequest_checkState(inst)
{
	var el = document.getElementById('gsi_iframe_' + inst);
	if (el.readyState == "complete")
	{
		var responseText = window.frames['gsi_iframe_' + inst].document.body.childNodes[0].data;
		gsi_objs[inst].responseText = responseText;
		gsi_objs[inst].readyState = 4;
		gsi_objs[inst].status = 200;
		gsi_objs[inst].onreadystatechange();
		el.parentNode.removeChild(el);
	}
	else
	{
		setTimeout('XMLHttpRequest_checkState(' + inst + ')',500);
	}
}
var gsi_instances = 0;
var gsi_objs = [];
var autoselect = true;

function HandleResponse_select(reload_object, progress)
{
	if(XmlHttp.readyState == 4)
	{
		if(XmlHttp.status == 200)
		{
			if (XmlHttp.responseText.length > 0)
			{
				// Remove the items from the linked drop-down
				for(var a = reload_object.length - 1; a > 0; a--) {reload_object.options[a] = null;}
				
				// Get an array of children associated with selected parent
				myArrayOfOptions = XmlHttp.responseText.split("||");
				
				for (var b = 0; b <= myArrayOfOptions.length - 1; b++)
				{
					OptionString = myArrayOfOptions[b];
					// Get an array of children code value and code description
					myArrayOfOptionValues = OptionString.split(":");
					// Build new option for child select
					newOption = new Option(myArrayOfOptionValues[1], myArrayOfOptionValues[0]);
					reload_object.options[reload_object.options.length] = newOption;	
				}
				if (b == 1 && autoselect)
				{
				    reload_object[b].selected = true;
				}

				// Hide progress status
				if (progress) {progress.display = "none";}
			}
			else
			{
				// Remove the items from the linked drop-down
				for(var a = reload_object.length - 1; a > 0; a--) {reload_object.options[a] = null;}
				if (progress) {progress.display = "none";}			
			}
		}
		else
		{
			if (progress) {progress.display = "none";}
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function HandleResponse_text(reload_object, progress)
{
	if(XmlHttp.readyState == 4)
	{
		if(XmlHttp.status == 200)
		{
			if (XmlHttp.responseText.length > 0)
			{
				reload_object.value = XmlHttp.responseText;
			}
			// Hide progress status
			if (progress) {progress.display = "none";}
		}
		else
		{
			if (progress) {progress.display = "none";}
			alert("There was a problem retrieving data from the server." );
		}
	}
}

