	var countryField = document.productselection.country;
	var categoryField = document.productselection.product_type;
	var productField = document.productselection.product_name;

	var myTNBObj = new TNB();
	
	var setCategories = function(json){		
				
		if(json['try-and-buy-products'].category != null){
			
			for(i=0; i<json['try-and-buy-products'].category.length; i++) {
				var thisCat = json['try-and-buy-products'].category[i];
				var newOpt = new Option(thisCat.name, thisCat.uuid);
				categoryField[i+1]=newOpt;
			}
			categoryField.disabled=false;
		}else{
		}
		finishedLoading();
		setTimeout("setSelectedCategory()", 100);
		setTimeout("loadProds()",110);
	}
	
	var setProducts = function(json){
		
		
		if(json['try-and-buy-products'].category != null){
			for(i=0; i<json['try-and-buy-products'].category.length; i++){
				
				var thisCatObject = json['try-and-buy-products'].category[i];
				
					//find whether or not there are subcats
					if(thisCatObject.subcategory != null){
						for(var s=0; s < thisCatObject.subcategory.length; s++){
							var thisSubCat = thisCatObject.subcategory[s];
							var optGroup = document.createElement('optgroup')
							optGroup.style.fontStyle='normal';
							optGroup.label = thisSubCat.name; 
							
							productField.appendChild(optGroup);
							var prodsForSubCat = thisSubCat.product;
							
							for(var p=0; p < prodsForSubCat.length; p++){
								var thisProd = prodsForSubCat[p];
								var prodOption = document.createElement('option');
								prodOption.value=thisProd.uuid;
								
								var optionText = document.createTextNode(thisProd.name);
								prodOption.appendChild(optionText);							
								optGroup.appendChild(prodOption);
								//If this is the prod the user was after, select it by default
								
							}
							
						}
					}else{
						var prodsForCat = thisCatObject.product;
							for(var p=0; p < prodsForCat.length; p++){
								var thisProd = prodsForCat[p];
								var prodOption = document.createElement('option');
								prodOption.value=thisProd.uuid;
								var optionText = document.createTextNode(thisProd.name);
								prodOption.appendChild(optionText);
								
								productField.appendChild(prodOption);
							}
						
					}
					productField.disabled=false;
				
				
			}
			
	
		}else{
			
		}
		finishedLoading();
		setTimeout("setSelectedProduct()", 100);
	}
	
	function loadCats(){
		
		var selectedCountry = countryField.options[countryField.selectedIndex].value;
		removeOptions(categoryField);
		categoryField[0] = new Option(selectString,"");
		categoryField.disabled = true;
		
		removeOptions(productField);
		productField[0] = new Option(selectString,"");		
		productField.disabled=true;
		if(selectedCountry.length > 0){
			loading();
			myTNBObj.getCountryCategories(selectedCountry,setCategories,false);
		}
	}
	
	function loadProds(){
		
		var selectedCountry = countryField.options[countryField.selectedIndex].value;
		var selectedCategory = categoryField.options[categoryField.selectedIndex].value;
		removeOptions(productField);
		productField[0] = new Option(selectString,"");		
		productField.disabled=true;
		
		if(selectedCountry.length > 0 && selectedCategory.length > 0){			
			loading();
			myTNBObj.getCountryCategoryProducts(selectedCountry,selectedCategory,setProducts,false);
		}
	}
		
	function removeOptions(obj)
	{
		var objSelect = obj;
		for ( var i = 0; i < objSelect.options.length; i++) {
			objSelect.remove(i);
		}
		objSelect.innerHTML = "";
	}
	
	function sendValue(country, ptype, pname) {
		var countryValue = country.options[country.selectedIndex].value;
		var categoryValue = ptype.options[ptype.selectedIndex].value;
		var productValue = pname.options[pname.selectedIndex].value;
		var countryError = country.name + "_error";
		var categoryError = ptype.name + "_error";
		var productError = pname.name + "_error";
		document.getElementById(countryError).style.display="none";
		document.getElementById(categoryError).style.display="none";
		document.getElementById(productError).style.display="none";
		if(countryValue.length < 1 ) document.getElementById(countryError).style.display = "inline";
		else if(categoryValue.length < 1 ) document.getElementById(categoryError).style.display="inline";
		else if(productValue.length < 1 ) document.getElementById(productError).style.display="inline";
		if(countryValue.length > 0 && categoryValue.length > 0 && productValue.length > 0 ) {
			document.getElementById("puuid").value= productValue;
			document.productselection.submit();
		}
		
	}
	
	function loading(){
		document.getElementById('loading-icon').style.display="inline";
	}
	function finishedLoading(){
		document.getElementById('loading-icon').style.display="none";
	}
	
	function setOmnitureVars(){
		try{
			s_linkType='o';
			s_linkName='Try_and_Buy_View_Configurations';
			s_products=';' + document.productselection.product_name[document.productselection.product_name.selectedIndex].childNodes[0].nodeValue;
			s_events='event4';
			s_eVar3=s_siteid;
			s_linkTrackVars='events,products,eVar3';
			s_linkTrackEvents='event4';
			s_lnk=s_co(this);
			s_gs(s_account);
		}catch(e){
			
		}
	}
	
	function setSelectedProduct(){
		//alert(selectedProduct);
		//Loop over the entries of the select box and set the one with the correct uuid to be selected		
		for (i=0; i <= productField.options.length - 1; i++){
			
			var val = productField.options[i].value;
			if(val == selectedProduct){
				productField.selectedIndex = i;
				productField.options[i].defaultSelected = true;
			}
			
		}
	
	}
	function setSelectedCategory(){
		//alert(selectedCategory);
		//Loop over the entries of the select box and set the one with the correct uuid to be selected		
		for (i=0; i <= categoryField.options.length - 1; i++){
			
			var val = categoryField.options[i].value;
			if(val == selectedCategory){
				categoryField.selectedIndex = i;
				categoryField.options[i].defaultSelected = true;				
			}
			
		}
	
	}
	loadCats();
