	var categoryField = document.product.product_type;
	var productField = document.product.product_name;
	var myTNBObj = new TNB();
	
	function ajaxQuery(url,callback){
	   var xmlhttp = false;
	   if (window.XMLHttpRequest)  {// code for IE7, Firefox, Opera, etc.
			xmlhttp=new XMLHttpRequest();
		}else if (window.ActiveXObject)  {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp!=null){
			
			xmlhttp.onreadystatechange=function(){state_Change(xmlhttp,callback)};
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
			
		}else{
			alert("Your browser does not support XMLHTTP.");
		}
	   
	}
	
	function state_Change(xmlhttp,callbackF){
		if (xmlhttp.readyState==4){// 4 = "loaded"
		
			if (xmlhttp.status==200){// 200 = "OK"
			
				try{
				     
				        var out = xmlhttp.responseText;
					callbackF(out);
				     	
				}catch(ex){
					alert(ex);
				}		
			}else{
				
			}
		}
	}

	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);
	}
	
	var setSupportURL = function(json){
		
		if(json['product'].length == 1){
		   	
		   var prodNode = json.product[0];
		   document.getElementById('supportLink').href = prodNode.supporturl;
		   document.getElementById('supportLink').innerHTML = prodNode.name;
		   
		}
	       document.getElementById('supportOptions').style.display = "inline";
	}
	
	
	
	var setOnlineSupportOptions = function(html){
	   document.getElementById("onlineOptions").innerHTML = html;
        }
	
	var setAdditionalSupportOptions = function(html){
	   document.getElementById("additionalOptions").innerHTML = html;
        }
	
	function loadAllProducts(){
	   var selectedCategory = categoryField.options[categoryField.selectedIndex].value;
	   removeOptions(productField);
	   productField[0] = new Option(selectString,"");		
	   productField.disabled=true;
	   document.getElementById('supportOptions').style.display = "none";
	   
	   if(selectedCategory.length > 0){			
		loading();
		myTNBObj.getAllProducts(selectedCategory,setProducts);
	   }
	}
	
	function getSupportURL(){
	  var selectedProduct = productField.options[productField.selectedIndex].value;
	  
	  if(selectedProduct.length > 0){
	    myTNBObj.getProductWithoutBlocking(selectedProduct,setSupportURL);
	  }
	}
	
	                                                          
	function loadSupportOptions(){
	    var selectedProduct = productField.options[productField.selectedIndex].value;	
	    getSupportURL();
	    
	    var online = "/tryandbuy/core/supportOptions.jsp?uuid="+selectedProduct+"&type=online";       
	    ajaxQuery(online,setOnlineSupportOptions);
	    var additional = "/tryandbuy/core/supportOptions.jsp?uuid="+selectedProduct+"&type=additional";       
	    ajaxQuery(additional,setAdditionalSupportOptions);
	    
	}
	
	function removeOptions(obj)
	{
		var objSelect = obj;
		for ( var i = 0; i < objSelect.options.length; i++) {
			objSelect.remove(i);
		}
		objSelect.innerHTML = "";
	}
	
	function loading(){
		document.getElementById('loading-icon').style.display="inline";
	}
	function finishedLoading(){
		document.getElementById('loading-icon').style.display="none";
	}
