//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(){
	//產生中類選單
	$("#Class1").change(function(){
		var PKey = $("#Class1").val();
		removeOptions('Class2');
		if ($("#Class3").length > 0 ) { 
		removeOptions('Class3');}
		AjaxLoadSuccess("Class2",PKey);
	});	
		
	//產生小類選單
	$("#Class2").change(function(){
		var PKey = $("#Class2").val();
		if ($("#Class3").length > 0 ) {
		AjaxLoadSuccess("Class3",PKey);}
	});		
});	
	
function AjaxLoadSuccess(selectbox,PKey){
	$(function(){
		$.ajax({
		   type: "POST",
		   url: "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);
	}
}
