if(!document.all)
{
	Element.prototype.selectSingleNode = function (sXPath) 
	{
		var oEvaluator = new XPathEvaluator();
		var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		if (oResult != null) {
			return oResult.singleNodeValue;
		} else {
			return null;
		}              
	};
}
    
var Ajax = new Object();
Ajax.Variables = new Array();
Ajax.PostUrl = null;
Ajax.ResponseFormat = "text";  
Ajax.Response = null;
Ajax.PostProcess = null;
Ajax.Status = 0;
Ajax.OnError = null;
Ajax.Method = "GET";
Ajax.Async = true;

Ajax.Init = function()
{
	Ajax.Variables = new Array();
	Ajax.PostUrl = null;
	Ajax.ResponseFormat = "text";
	Ajax.Response = null;
	Ajax.PostProcess = function(){ if(Ajax.Status >= 2)alert("Data is returned but PostProcess method is not assigned!"); else{ alert("Data is not returned or PostProcess method is not assigned"); }}
	Ajax.OnError = function(){ alert("Data not found! Please check the URL."); }
	Ajax.Method = "GET";
	Ajax.Status = 1;
	Ajax.Async = true;
}

Ajax.Request = function()
{
	Ajax.oRequest = null;

	if (window.ActiveXObject) 
		Ajax.oRequest = new ActiveXObject("Microsoft.XMLHTTP");
	else
		Ajax.oRequest = new XMLHttpRequest();
	
	
	if(Ajax.oRequest) 
	{
		Ajax.oRequest.onreadystatechange = Ajax.OnReadyStateChange;
		Ajax.oRequest.open(Ajax.Method, Ajax.PostUrl, Ajax.Async);
		Ajax.oRequest.send(null);
		Ajax.Status = 2;
	}
	else
	{
		alert("Error processing asyncronous request! XMLHttpRequest is not supported. Please use a compatible browser.");
		return -1;
	}
}

Ajax.OnReadyStateChange = function()
{
	if (Ajax.oRequest.readyState == 4) 
	{
		if (Ajax.oRequest.status == 200) 
		{
			if(Ajax.ResponseFormat == "xml")
				Ajax.Response = Ajax.oRequest.responseXML;
			else
				Ajax.Response = Ajax.oRequest.responseText;
			
			Ajax.Status = 3;
			
			Ajax.PostProcess(Ajax.Response, Ajax.Variables);
			
			Ajax.Status = 4;
		}
		else
			eval("Ajax.OnError()");
	}
}

function FillDropDown(sXml, sDropDownName, sXPath, sDDValue, sDDName, bDefautValue, sFromDD)
{
	var xmlDoc;
		
		if (window.ActiveXObject) 
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		else
			xmlDoc = new XMLHttpRequest();
		
		
		xmlDoc.async=false;
		xmlDoc.loadXML("<root>" + sXml + "</root>");
	
		var oNodes = xmlDoc.documentElement.selectNodes(sXPath);
		
		//SelectedSubject
		var oSelectedItem = xmlDoc.documentElement.selectSingleNode("/root/SelectedSubject");
		
		var oDropDown = ""; 
		
		var oFromBox = document.getElementById(sFromDD);
		var bAlreadyExists = false;		
		
		
		if(document.getElementById(sDropDownName))
		{
			oDropDown = document.getElementById(sDropDownName);
			oDropDown.options.length = 0;
			
			if(bDefautValue) {			
				var oFOption = new Option(" ", "-1");
				oDropDown.options.add(oFOption);
			}
			
			for(z=0; z < oNodes.length; z++)
			{				
				oCurrentData = oNodes.item(z);
				var sVal = oCurrentData.getAttribute(sDDValue);
				var sSName = oCurrentData.getAttribute(sDDName);
				var oDefOption = new Option(sSName, sVal);
				
				var sSubj = sVal;
				var sCount = oCurrentData.getAttribute("ChildCount");
				//if(z==0) alert(sVal);
				aSubjectChildCount[z] = new Array();
				aSubjectChildCount[z][0] = sVal;
				aSubjectChildCount[z][1] = sCount;
				
				oDefOption.mySortValue = z;
				
				if(oSelectedItem)
				if(parseInt(sVal) == oSelectedItem.getAttribute("SubjectId"))
				{
					oDefOption.selected = true;
				}
				
				if(oFromBox)
				{
					for(var nIndex = 0; nIndex < oFromBox.options.length; nIndex++)
					{
						if(oFromBox.options[nIndex].value == sVal)
						{
							bAlreadyExists = true;
							break;
						}	
					}
				}
				
				if(!bAlreadyExists)
					oDropDown.options.add(oDefOption);
				
				bAlreadyExists = false;
			}
		}
}

function MoveSelectItemsRightLeft(bIsAssigning,sFromBox, sToBox, bIsNumber)
	{
		//Selectbox items !!  moves selected Items from one listbox to the other
		var oFromBox, oToBox;
		
		
			if(bIsAssigning)
			{
				oFromBox = document.getElementById(sFromBox);
				oToBox   = document.getElementById(sToBox);
			}	//end if bIsAssigning
			else
			{
				oFromBox = document.getElementById(sToBox);
				oToBox   = document.getElementById(sFromBox);
			}	//end else

			var nSelectedIndex = oFromBox.selectedIndex;
			var nInsertLocation = oToBox.length;	//default to putting item at end of list
			var sMySortValue = "";
			
		if(nSelectedIndex != -1)
		{			
			sMySortValue = oFromBox.options[nSelectedIndex].mySortValue;
			if (sMySortValue != null)
			{
				if(bIsNumber)
					sMySortValue = new Number(sMySortValue);
				for(var nIndex = 0; nIndex < oToBox.options.length; nIndex++)
				{
					var sNextSortValue = oToBox.options[nIndex].mySortValue;
					if(bIsNumber)
						sNextSortValue = new Number(sNextSortValue);
					if (sNextSortValue != null)
					{
						if (sMySortValue < sNextSortValue)	//Subject sort-values are strings
						{
							nInsertLocation = nIndex;
							break;
						}	//end if sMySortValue
					}	//end if sNextSortValue
				}	//end for nIndex
			}	//end if sMySortValue
			
			
			var bAlreadyExists = false;			
			if(bIsAssigning)
			{
				for(var nIndex = 0; nIndex < oToBox.options.length; nIndex++)
				{
					if(oToBox.options[nIndex].value == oFromBox.options[nSelectedIndex].value)
					{
						bAlreadyExists = true;
						break;
					}	
				}
			}			
			
			if(!bAlreadyExists)
			{			
					var oNewOption = new Option(oFromBox.options[nSelectedIndex].text, oFromBox.options[nSelectedIndex].value);		
					oNewOption.mySortValue = sMySortValue;
					oToBox.options.add(oNewOption, nInsertLocation);
					oFromBox.remove(nSelectedIndex);
					oToBox.options.selectedIndex = nInsertLocation;
			}
		}	//end if nSelectedIndex
		
}	//MoveSubjectsRightLeft

function fnFillDynamicDropDown(sXML, aARYParams)
{
	//alert(sXML);
			   //sXml,sDropDownName, sXPath,       sDDValue,     sDDName,     bDefautValue,  sFromDD					
	FillDropDown(sXML,aARYParams[0],aARYParams[1],aARYParams[2],aARYParams[3],aARYParams[4],aARYParams[5]);	
}

function FillDropDownValues(bDefautValue,oDropDown,oNodes,oSelectedItem,sDDValue,sDDName)
{
		//oDropDown = document.getElementById(sDropDownName);
		oDropDown.options.length = 0;
		
		if(bDefautValue) 
		{			
			var oFOption = document.createElement("OPTION");
			oDropDown.options.add(oFOption);
			oFOption.text="Select an item";
			oFOption.value= "-1";
		}
		
		for (var i = 0; i < oNodes.length; i++)
		{								
			if (oNodes[i].nodeName.toUpperCase() == "GPSSUBJECT")
			{	
					var oOption = document.createElement("OPTION");
					var sVal = oNodes[i].getAttribute(sDDValue);
					var sText = oNodes[i].getAttribute(sDDName);
					oDropDown.options.add(oOption);
					oOption.text = sText;
					oOption.value = sVal;
					oOption.mySortValue = i;										
					
					if(oSelectedItem)
						if(parseInt(sVal) == oSelectedItem.getAttribute("SubjectId"))
						{
							oDefOption.selected = true;
						}
			}	//end if .nodeName
		}	//end for i

}


