var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function getRequestBody(oForm) {
    var aParams = new Array();
    
    for (var i=0 ; i < oForm.elements.length; i++) {
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
    } 
    
    return aParams.join("&");
}

function getType(products){

	if(products!="" && products!=0){//---------------
		var pForm = document.forms[0];
	    var pBody = getRequestBody(pForm);
		var url = "getType.php?products="+products;
		createXMLHttpRequest();
		
		xmlHttp.onreadystatechange = function () {
	   document.getElementById("loading_type").style.display='inline';	
	   document.getElementById("loading_type").innerHTML='<img src="images/loader.gif"/>';
			 
		if(xmlHttp.readyState == 4) {
		  	if(xmlHttp.status == 200) {
				var CList = xmlHttp.responseText;  
				var Type=document.getElementById("txttype");
				
				while(Type.childNodes.length>0){
					Type.removeChild(Type.childNodes[0]);
				}
				var  option=null;
				var  xxx=CList.split("!#!#!");
				if(xxx=="No"){
					var title="----------------------Not Specified--------------------";
				}else{
					var title="----------------------Select Type--------------------";
				}
				var  option=document.createElement("option");
				option.appendChild(document.createTextNode(title)); 
				option.value=0;
				Type.appendChild(option);
				var re_count=xxx.length-1
					for(var i=0;i < re_count ;i++){
						v=xxx[i].split("!*!");
						option=document.createElement("option");
						option.appendChild(document.createTextNode(v[1])); 
						option.value=v[0];
						Type.appendChild(option);
					}//-----end for-------------------------------
														
				document.getElementById("loading_type").style.display='none';
			}else{
				alert("พบข้อผิดพลาดในการแสดงข้อมูล");
			}
		}
	}
	xmlHttp.open("POST", url, true);
	xmlHttp.send(pBody);
	}//-----------end  if(products!="" && products!=0){
}

function getSeries(type){
	//alert(type);
	if(type!="" && type!=0){
		var pForm = document.forms[0];
	    var pBody = getRequestBody(pForm);
		var url = "getSeries.php?type="+type;
		createXMLHttpRequest();
		
		xmlHttp.onreadystatechange = function () {
	   document.getElementById("loading_series").style.display='inline';	
	   document.getElementById("loading_series").innerHTML='<img src="images/loader.gif"/>';
			 
		if(xmlHttp.readyState == 4) {
		  	if(xmlHttp.status == 200) {
				var CList = xmlHttp.responseText;  
				var Series=document.getElementById("txtseries");
				
				while(Series.childNodes.length>0){
					Series.removeChild(Series.childNodes[0]);
				}
				var  option=null;
				var  xxx=CList.split("!#!#!");
				if(xxx=="No"){
					var title="----------------------Not Specified--------------------";
				}else{
					var title="----------------------Select Series--------------------";
				}
				var  option=document.createElement("option");
				option.appendChild(document.createTextNode(title)); 
				option.value=0;
				Series.appendChild(option);
				var re_count=xxx.length-1
					for(var i=0;i < re_count ;i++){
						v=xxx[i].split("!*!");
						option=document.createElement("option");
						option.appendChild(document.createTextNode(v[1])); 
						option.value=v[0];
						Series.appendChild(option);
					}//-----end for-------------------------------
														
				document.getElementById("loading_series").style.display='none';
			}else{
				alert("พบข้อผิดพลาดในการแสดงข้อมูล");
			}
		}
	}
	xmlHttp.open("POST", url, true);
	xmlHttp.send(pBody);
	}//----end if(type!="" && type!=0){
}

function resetm(){
	document.getElementById('txtproducts').value=0;
	ClearSelect(document.getElementById('txttype'),"type");
	ClearSelect(document.getElementById('txtseries'),"series");
	document.getElementById('txtsearch').value="--------------   Please Type Keyword   --------------";
}

function ClearSelect(obj,type){
	var obj;
	if(type=="type"){
		var title="----------------------Select Type--------------------";
	}else if(type=="series"){
		var title="----------------------Select Series--------------------";
	}
	while(obj.childNodes.length>0){
		obj.removeChild(obj.childNodes[0]);
	}
	var  option=document.createElement("option");
	option.appendChild(document.createTextNode(title)); 
	option.value=0;
	obj.appendChild(option);
}

