//AJAX 送出查詢字串到指定檔案 以 option組合*****
function AjaxLoadClass2(PKey){
	removeOptions('Class2');
	if (document.getElementById('Class3'))
		removeOptions('Class3');
	if (document.getElementById('Product_PKey'))
		removeOptions('Product_PKey');	
	AjaxLoadSuccess("Class2",PKey)
}


//AJAX 送出查詢字串到指定檔案 以 option組合*****
function AjaxLoadClass3(PKey){
	removeOptions('Class3');
	AjaxLoadSuccess("Class3",PKey);
	if (document.getElementById('Product_PKey')){
		removeOptions('Product_PKey');
		AjaxLoadSuccess("Product_PKey",PKey)
	}
}



$(function(){
	//產生類別2選單
	$("#Class1").change(function(){
		var PKey = $("#Class1").val();
		
		if ($("#Class2").length > 0 ) { 
		AjaxLoadSuccess("Class2",PKey);
		removeOptions('Class2');}
		
		if ($("#Class3").length > 0 ) { 
		removeOptions('Class3');}
		
		if ($("#Class4").length > 0 ) { 
		removeOptions('Class4');}
		
		if ($("#Class5").length > 0 ) { 
		removeOptions('Class5');}
		
	});	
		
	//產生類別3選單
	$("#Class2").change(function(){
		var PKey = $("#Class2").val();
		if ($("#Class3").length > 0 ) {
		AjaxLoadSuccess("Class3",PKey); 
		removeOptions('Class3');}
		
		if ($("#Class4").length > 0 ) { 
		removeOptions('Class4');}
		
		if ($("#Class5").length > 0 ) { 
		removeOptions('Class5');}
	});
		
	//產生類別4選單
	$("#Class3").change(function(){
		var PKey = $("#Class3").val();
		if ($("#Class4").length > 0 ) {
		AjaxLoadSuccess("Class4",PKey); 
		removeOptions('Class4');}
		
		if ($("#Class5").length > 0 ) { 
		removeOptions('Class5');}
		
	});
	
	//產生類別5選單
	$("#Class4").change(function(){
		var PKey = $("#Class4").val();
		if ($("#Class5").length > 0 ) { 
		AjaxLoadSuccess("Class5",PKey);
		removeOptions('Class5');}
		
	});
	
	//產生品牌系列選單
	$("#Brand").change(function(){
		var PKey = $("#Brand").val();
		if ($("#Series").length > 0 ) { 
		AjaxLoadSuccess("Series",PKey);
		removeOptions('Series');}
	});
});	
	
function AjaxLoadSuccess(selectbox,PKey){
	$(function(){
		$.ajax({
		   type: "POST",
		   url: "ajax/_ajax.asp",
		   data: "PKey="+ PKey + "&RType=" + selectbox ,
		   success: function(json){
			    onComplete(selectbox,json);
			  }
		});
	});	
}

//完成後執行的函數
function onComplete(selectbox,val)
{
	var str = val;
	if (typeof(val) !== 'undefined' && val !== ''){
			str = eval("(" + str + ")");	
			for(var i = 0; i < str.data.length; i++){
				document.getElementById(selectbox).options[i + 1] = new Option(str.data[i].Name,str.data[i].ID);
			};
		}
	
	
}

//移除指定的options
function removeOptions(selectbox)
{
	var i;
	for(i=document.getElementById(selectbox).options.length-1;i>0;i--)
	{
		//selectbox.options.remove(i);
		document.getElementById(selectbox).remove(i);
	}
}
