
//Global XMLHTTP Request object
var XmlHttp;

//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();
	}
}

function ListOnChangeNr() 
{
	var productNr = document.getElementById("productNr");
	var productClass2 = document.getElementById("productClass2");
	
	if (productNr.selectedIndex >= 0) {
	    var selectedValue = productNr.options[productNr.selectedIndex].value;
	    for (var i = 0; i < productClass2.options.length; i++) {
	        if (productClass2.options[i].value == selectedValue) {
	            productClass2.selectedIndex = i;
	            break;
	        }
	    }
	}
	else {
    	productClass2.selectedIndex = 0;
	}

	ListOnChange() 
}

//Gets called when the combo box selection changes
function ListOnChange() 
{
	
	var lang = document.getElementById("langAjax").value;
	var issuer = document.getElementById("issuer");
	var productClass1 = document.getElementById("productClass1");
	var productClass2 = document.getElementById("productClass2");
	var starFeature = document.getElementById("starFeature");
	var productName = document.getElementById("productName");

	//Getting the selected items from combo box.
	var selectedIssuer;
	if (issuer.selectedIndex >= 0)
		selectedIssuer = issuer.options[issuer.selectedIndex].value;
	else
		selectedIssuer = "";
		
	var selectedProductClass1;
	if (productClass1.selectedIndex >= 0)
		var selectedProductClass1 = productClass1.options[productClass1.selectedIndex].value;
	else
		var selectedProductClass1 = "";
	
	var selectedProductClass2;
	if (productClass2.selectedIndex >= 0)
		selectedProductClass2 = productClass2.options[productClass2.selectedIndex].value;
	else
		selectedProductClass2 = "";
	
	var selectedStarFeature;
	if (starFeature.selectedIndex >= 0)
		selectedStarFeature = starFeature.options[starFeature.selectedIndex].value;
	else
		selectedStarFeature = "";
		
	var selectedProductName;
	if (productName.selectedIndex >= 0)
		selectedProductName = productName.options[productName.selectedIndex].value;
	else
		selectedProductName = "";
		
		
	// URL to get states for a given country
	var requestUrl = "product_finder_selections.aspx?lang=" + lang + "&issuer=" + encodeURIComponent(selectedIssuer)+ "&";
	requestUrl += "productClass1=" + encodeURIComponent(selectedProductClass1) + "&";
	requestUrl += "productClass2=" + encodeURIComponent(selectedProductClass2) + "&";
	requestUrl += "starFeature=" + encodeURIComponent(selectedStarFeature) + "&";
	requestUrl += "productName=" + encodeURIComponent(selectedProductName);

	var dateNow = new Date();
	//alert (requestUrl)
	requestUrl += "&timestamp=" + dateNow.getTime();

	//alert(requestUrl);
	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);		
	}
}

//ListOnChangeEditing
function ListOnChangeShowing(issuer, pc1, pc2, pn, sf) 
{
	var lang = document.getElementById("langAjax").value;
	// URL to get states for a given country
	var requestUrl = "product_finder_selections.aspx?lang=" + lang + "&issuer="+ issuer +"&productClass1=" + pc1 + "&";
	requestUrl += "productClass2=" + pc2 + "&starFeature=" + sf +"&productName=" + pn;
	
	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);		
	}
}



//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)
		{			
			ClearAndSetListItems(XmlHttp.responseXML.documentElement);
		}
		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 ClearAndSetListItems(node)
{
    var issuer = document.getElementById("issuer");
	var productClass1 = document.getElementById("productClass1");
	var productClass2 = document.getElementById("productClass2");
	var productNr = document.getElementById("productNr");
	var starFeature = document.getElementById("starFeature");
	var productName = document.getElementById("productName");

	//Clears the state combo box contents.
	for (var count = issuer.options.length-1; count >-1; count--)
	{
		issuer.options[count] = null;
	}
	for (var count = productClass1.options.length-1; count >-1; count--)
	{
		productClass1.options[count] = null;
	}
	for (var count = productClass2.options.length-1; count >-1; count--)
	{
		productClass2.options[count] = null;
	}
	
	for (var count = productNr.options.length-1; count >-1; count--)
	{
		productNr.options[count] = null;
	}
	
	for (var count = starFeature.options.length-1; count >-1; count--)
	{
		starFeature.options[count] = null;
	}
	for (var count = productName.options.length-1; count >-1; count--)
	{
		productName.options[count] = null;
	}
	
	var textValue; 
	var optionItem;
	var id;
	var issuerNodes = node.getElementsByTagName('issuer');
	//Add new states list to the state combo box.
	for (var count = 0; count < issuerNodes.length; count++)
	{
   		textValue = GetInnerText(issuerNodes[count]);
   		id = issuerNodes[count].getAttribute("id");
   		selected = issuerNodes[count].getAttribute("selected");   		
   		if(selected == "1")
   			optionItem = new Option( textValue, id,  false, true);   			
   		else
   			optionItem = new Option( textValue, id,  false, false);
		issuer.options[issuer.length] = optionItem;
	}
	
	var productClass1Nodes = node.getElementsByTagName('productClass1');
	//Add new states list to the state combo box.
	for (var count = 0; count < productClass1Nodes.length; count++)
	{
   		textValue = GetInnerText(productClass1Nodes[count]);
   		id = productClass1Nodes[count].getAttribute("id");
   		selected = productClass1Nodes[count].getAttribute("selected");   		
   		if (selected == "1") {
   			optionItem = new Option( textValue, id,  false, true);
   		}
   		else {
			optionItem = new Option( textValue, id,  false, false);
		}
		productClass1.options[productClass1.length] = optionItem;
	}
	
	var productClass2Nodes = node.getElementsByTagName('productClass2');
	//Add new states list to the state combo box.
	for (var count = 0; count < productClass2Nodes.length; count++)
	{
   		textValue = GetInnerText(productClass2Nodes[count]);
   		id = productClass2Nodes[count].getAttribute("id");
   		selected = productClass2Nodes[count].getAttribute("selected");   		
   		if(selected == "1")
			optionItem = new Option( textValue, id,  false, true);
		else
			optionItem = new Option( textValue, id,  false, false);
		productClass2.options[productClass2.length] = optionItem;
	}
	
	var productNrNodes = node.getElementsByTagName('productNr');
	//Add new states list to the state combo box.
	for (var count = 0; count < productNrNodes.length; count++)
	{
   		textValue = GetInnerText(productNrNodes[count]);
   		id = productNrNodes[count].getAttribute("id");
   		selected = productNrNodes[count].getAttribute("selected");   		
   		if(selected == "1")
			optionItem = new Option( textValue, id,  false, true);
		else
			optionItem = new Option( textValue, id,  false, false);
		productNr.options[productNr.length] = optionItem;
	}
	
	var starFeatureNodes = node.getElementsByTagName('starFeature');
	//Add new states list to the state combo box.
	for (var count = 0; count < starFeatureNodes.length; count++)
	{
   		textValue = GetInnerText(starFeatureNodes[count]);
   		id = starFeatureNodes[count].getAttribute("id");
   		selected = starFeatureNodes[count].getAttribute("selected"); 
   		  		
   		if(selected == "1")
			optionItem = new Option( textValue, id, false, true);
		else
			optionItem = new Option( textValue, id, false, false);
	    
		starFeature.options[starFeature.length] = optionItem;
	}
	
	var productNameNodes = node.getElementsByTagName('productName');
	//Add new states list to the state combo box.
	for (var count = 0; count < productNameNodes.length; count++)
	{
   		textValue = GetInnerText(productNameNodes[count]);
   		id = productNameNodes[count].getAttribute("id");
   		selected = productNameNodes[count].getAttribute("selected");   
   		if(selected == "1")
   		{
			optionItem = new Option( textValue, id,  false, true);			
		}
		else
		{
			optionItem = new Option( textValue, id,  false, false);
		}
		productName.options[productName.length] = optionItem;
	}

}


//Returns the node text value 
function GetInnerText (node)
{
	var text = "";
	
	text = (node.textContent || node.innerText || node.text);
	if(!text) {
		if(node.childNodes && node.childNodes.length > 0 && node.childNodes[0].data) {
			text = node.childNodes[0].data;
		}
	}
	
	//return (node.textContent || node.innerText || node.text) ;
	return text;
}

