﻿var path = '/';

function InitAjax(){
	
	var ajax = false;

	if(window.XMLHttpRequest){
		
		ajax = new XMLHttpRequest();
		if(ajax.overrideMimeType){
			ajax.overrideMimeType("text/xml");
		}
	}else if(window.ActiveXObject){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){}
		}
	}
	
	if(ajax){
		
		return ajax;
		
	}else{
		
		alert("不能创建XMLHttpRequest对象实例，请使用IE、Mozilla、Firefox等支持XMLHTTP浏览器");
		return false;
		
	}
	
}

/* 载入产品一、二级列表 */
function InitProductClass(){
	
	var load = document.getElementById('loading');
	var http_request = InitAjax();
	var parentid;
	var url = path + 'initproductclass.asp';
	var arglength;
	var i,j;
	var aclass,aclassitem;
	var returntext;
	
	var sel1 = document.getElementById('selectproductclass1');
	var sel2 = document.getElementById('selectproductclass2');
	var sel3 = document.getElementById('selectproductclass3');
	
	arglength = arguments.length;
	if(arglength == 0){
		parentid = 0;
	}else{
		parentid = arguments[0];
	}
	
	url += '?id=' + parentid;
	
	http_request.onreadystatechange = function(){
		if(http_request.readyState == 4){
			if(http_request.status == 200){
				
				HideLoading();
				
				returntext = http_request.responseText;
				
				aclass = returntext.split('|');
				
				if(parentid == 0){
					for(i = sel1.options.length ; i > 0 ; i --){
						sel1.remove(i);
					}
				}
				for(i = sel2.options.length ; i > 0 ; i --){
						sel2.remove(i);
					}
					for(i = sel3.options.length ; i > 0 ; i --){
						sel3.remove(i);
					}
				for(i = 0 ; i < aclass.length ; i ++){
					aclassitem = aclass[i].split(',');
					
					var text = document.createTextNode(aclassitem[1]);
					var option = document.createElement('option');
					option.appendChild(text);
					option.setAttribute('value' , aclassitem[0]);
					
					if(parentid == 0){
						sel1.appendChild(option);
					}else{
						sel2.appendChild(option);
					}
				}
			}else{
				alert('载入失败');
			}
		}else{
			ShowLoading();
		}
	}
	
	http_request.open('GET' , url , true);
	http_request.send(null);
	
}


/* 载入产品列表 */
function InitProduct(){
	
	var http_request = InitAjax();
	var classid;
	var url = path + 'initproduct.asp';
	var arglength;
	var i,j;
	var aproduct,aproductitem;
	var returntext;
	
	var sel3 = document.getElementById('selectproductclass3');
	
	classid = arguments[0];
	url += '?id=' + classid;
	
	http_request.onreadystatechange = function(){
		if(http_request.readyState == 4){
			if(http_request.status == 200){
				
				HideLoading();
				
				returntext = http_request.responseText;
				aproduct = returntext.split('|');
				
				for(i = sel3.options.length ; i > 0 ; i --){
					sel3.remove(i);
				}
				for(i = 0 ; i < aproduct.length ; i ++){
					aproductitem = aproduct[i].split(',');
					
					var text = document.createTextNode(aproductitem[1]);
					var option = document.createElement('option');
					option.appendChild(text);
					option.setAttribute('value' , aproductitem[0]);
					
					sel3.appendChild(option);
				}
			}else{
				alert('载入失败');
			}
		}else{
			ShowLoading();
		}
	}
	
	http_request.open('GET' , url , true);
	http_request.send(null);
	
}