
//Global XMLHTTP Request object
var XmlHttp;
var isSkill = 0;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Gets called when category combo box selection changes
function CategoryListOnChange() 
{
    //alert('CategoryListOnChange');
    isSkill = 0;
	var categoryPositionList = document.getElementById("categoryPositionList");

	//Getting the selected category from category combo box.
	var selectedCcategory = categoryPositionList.options[categoryPositionList.selectedIndex].value;
	
	var selectedIndex = categoryPositionList.selectedIndex;	
	
	var categoryList = document.getElementById("categoryList");	
	
	//categoryList.options[selectedIndex].selected = true;
	PopulateCategorySkillsAndPosition(selectedCcategory);
}

function PopulateCategorySkillsAndPosition(category)
{


// URL to get skills for a given category
	var requestUrl = AjaxServerPageName + "?SelectedCategoryPosition=" + encodeURIComponent(category);
	
    CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


//Gets called when category combo box selection changes
function CategorySkillListOnChange() 
{
   isSkill =1;
   
	var categoryPositionList = document.getElementById("categoryList");	

	//Getting the selected category from category combo box.
	var selectedCcategory = categoryPositionList.options[categoryPositionList.selectedIndex].value;	
	
	var selectedIndex = categoryPositionList.selectedIndex;	
	
	var categoryList = document.getElementById("categoryPositionList");	
	
	categoryList.options[selectedIndex].selected = true;
	
	document.getElementById("otherTextbox").value = '';
	document.getElementById("otherTextbox").disabled=true;
	    
	PopulateCategorySkillsAndPosition(selectedCcategory,1);
}



//Called when response comes back from server
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
		       if(isSkill == 1)
		       {
			   ClearAndSetPositionListItems(XmlHttp.responseText);			  		    
			   ClearAndSetSkillListItems(XmlHttp.responseText);
			   }else
			   {
			      ClearAndSetPositionListItems(XmlHttp.responseText);	
			   }
			
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetPositionListItems(countryNode)
{
   var positionText = countryNode.split('||')[0];
   var doc = LoadXml(positionText);
    var positionList = document.getElementById("positionList");
	//Clears the state combo box contents.
	for (var count = positionList.options.length-1; count >-1; count--)
	{
		positionList.options[count] = null;
	}
   // var position = 
	var stateNodes = doc.getElementsByTagName('position');
	var textValue; 
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText(stateNodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		positionList.options[positionList.length] = optionItem;
	}
}


//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetSkillListItems(countryNode)
{
    var skillText = countryNode.split('||')[1];
    var doc = LoadXml(skillText);
    var skillList = document.getElementById("skillList");
	//Clears the state combo box contents.
	for (var count = skillList.options.length-1; count >-1; count--)
	{
		skillList.options[count] = null;
	}

	var stateNodes = doc.getElementsByTagName('skill');
	var textValue; 
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText(stateNodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		skillList.options[skillList.length] = optionItem;
	}
}



//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function LoadXml(text)
{
    var xmlDoc = null;
  if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text); 
  } 
  return xmlDoc;
}








