function removeAllOptions(selectbox) {
	var i;
	if (selectbox != null) {
		for(i=selectbox.options.length-1;i>=0;i--)	{
			selectbox.remove(i);
		}
	}
}

function updateSubCategories(parent_id, selected_id) {
    var xmlHttp = getXmlHttpObject();

    if (xmlHttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="ajax_subcategories.php";
    url=url+"?parent_id="+parent_id+"&selected_id="+selected_id;
	url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=function() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            //divide up the string and then add select options
			var response = xmlHttp.responseText;
			
			var arrOptions = Array();
			arrOptions = response.split('---');
			
			var subcategory = document.getElementById('subcategory');
			if(subcategory == null) {
				return false;	
			}
			removeAllOptions(subcategory);
			
			//add a default option at the top with value =0
			option = new Option("please select", "0");
			subcategory.options[subcategory.length] = option;
			
			for (var x = 0; x<arrOptions.length; x++) {
				var arrItem = arrOptions[x].split(':::');
				var id = arrItem[0];
				var name = arrItem[1];
				var selected = arrItem[2];
				if (name != null) {
					//add to selectjavascript	
					option = new Option(name, id);
						
					if(selected == "selected") {
						option.selected = true;
					} 	
					
					subcategory.options[subcategory.length] = option;
				}	
			}
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);		
}
