var XmlHttpObj;
var subCat;
var prodID;

function CreateXmlHttpObj() {

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		XmlHttpObj = new XMLHttpRequest();
	
	}
	
	if (window.ActiveXObject) {
		// code for IE6, IE5
		XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		
	}
	

/*	try {
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	
	}
	
	catch(e) {
		try {
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		
		}
		
		catch(oc) {
			XmlHttpObj = null;
			
		}
		
	}
	
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") {
		XmlHttpObj = new XMLHttpRequest();
		
	}
*/
}

function showCat(getId, subId) {
    var continentList = document.getElementById("prod_kolor" + getId);
    
    var selectedContinent = continentList.options[continentList.selectedIndex].value;
    
    var requestUrl;
    
    var master = getId>0 ? getId : encodeURIComponent(selectedContinent);
	
	subCat = subId;
	prodID = getId;
	
	requestUrl = "ajax.php?data=group&gid=" + getId + "." + master;

	CreateXmlHttpObj();
	

	if(XmlHttpObj) {
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		XmlHttpObj.open("GET", requestUrl,  true);
		
		XmlHttpObj.send(null);
	
	}

}


function StateChangeHandler() {

	if(XmlHttpObj.readyState == 4) {

		if(XmlHttpObj.status == 200) {
			//alert(XmlHttpObj.responsetext);
			PopulateCountryList(XmlHttpObj.responseXML.documentElement);
		
		} else {
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		
		}
		
	}
	
}


function PopulateCountryList(countryNode) {
	var countryList = document.getElementById("prod_rozmiar" + prodID);

	for (var count = countryList.options.length-1; count >-1; count--) {
		countryList.options[count] = null;
		
	}
	
	var countryNodes = countryNode.getElementsByTagName('sub');

	var idValue;
	var textValue; 
	var optionItem;

	//optionItem = new Option( '', '0',  false, false);
	//countryList.options[countryList.length] = optionItem;
	
	for (var count = 0; count < countryNodes.length; count++) {
		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		
		if(idValue==subCat) {
			optionItem = new Option( textValue, idValue,  false, true);
		
		} else {
			optionItem = new Option( textValue, idValue,  false, false);
		
		}
		
		countryList.options[countryList.length] = optionItem;
	
	}

}

function GetInnerText (node) {
	 return (node.textContent || node.innerText || node.text) ;

}
