var myResponse=null;
var xmlHttp=null;
var DropDownField=null;

function PopulateDropDown(URL, PassedDropDownField) {

	DropDownField = PassedDropDownField;
//	alert(DropDownField);

	GetXmlHttpObject();
	myResponse=null;

	xmlHttp.onreadystatechange=stateChanged;        //Important statements for Ajax.
	xmlHttp.open("GET",URL,true);				    //Important statements for Ajax.
	xmlHttp.send(null);									//Important statements for Ajax.

	if(DropDownField != null) {
		PopulateDropDownActually(DropDownField);
	}

//	window.setTimeout("alert(myResponse);", 1000);
}

var t;
function PopulateDropDownActually()
{
//	alert(DropDownField);

	if(myResponse == null) {
		t=window.setTimeout("PopulateDropDownActually();", 1000);
	} else {
		clearTimeout(t);
		var regexp = /\|/;
		var newArray = myResponse.split(regexp);
		DropDownField.length=0;
		DropDownField.length=newArray.length+1;
		DropDownField[0].text = "--- Select ---";
		DropDownField[0].value = "";
		for(var i=0; i<newArray.length; i++) {
			DropDownField[i+1].text = newArray[i];
			DropDownField[i+1].value = newArray[i];
		}
		if(newArray.length == 0) {
			DropDownField.length=1;
			DropDownField[0].text = "--- Nothing to Select! ---";
			DropDownField[0].value = "";
		}
	}
}

function stateChanged()
{
	// alert(xmlHttp.readyState);           // Check for value=4.
	// alert(xmlHttp.status);			   // Check for value=200.	
	
	if (xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			// alert(xmlHttp.readyState);           // Check for value=4.
			// alert(xmlHttp.status);			   // Check for value=200.	
			// alert(xmlHttp.responseText);
			
			myResponse = xmlHttp.responseText;   //Important statements for Ajax.Returns actual string value from the server.
			// alert("inside " + myResponse);

		}
	}
}

function GetXmlHttpObject()							//Checks for user's browser.
{
	xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	// alert(xmlHttp);
	return xmlHttp;
} 
