//创建XMLHttp
function createXMLHttpRequest(){
	var xmlHttp=null;
	if(window.ActiveXObject){
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		if(!xmlHttp){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
	}else if(window.XMLHttpRequest){
		xmlHttp=new XMLHttpRequest()
	}
	return xmlHttp
}

//处理xml绑定到select控件上面
function XmlToSelect(xmlHttp,objSelect){
    
    var xmlDoc=xmlHttp.responseXML;
	var items=xmlDoc.getElementsByTagName("item");
    objSelect.options.length=items.length+1;
    //objSelect.options[0].selected=true;
	for(var i=0;i<items.length;i++){
	    var id=items[i].getElementsByTagName("id")[0].firstChild.nodeValue;
	    var name=items[i].getElementsByTagName("name")[0].firstChild.nodeValue;
	    objSelect.options[i+1].text=name;
	    objSelect.options[i+1].value=id;
	}
}



function TextToObject(xmlHttp,objSet){
    objSet.InnerHTML=xmlHttp.responseText;
}

function ReadToObject(URLStr,objSet){
    var xmlHttp=createXMLHttpRequest();alert(URLStr);
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
			    
				    TextToObject(xmlHttp,objSet);
				}
		}
	}
	xmlHttp.send(null)		
}
/************************* 应用处理分类选择 **************************/
var catalog_levelNum=4;
//利用Ajax处理信息
function selectCatalogRequest(URLStr,objSelect,objSet,objHid){
    var xmlHttp=createXMLHttpRequest();
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				handleSelectCatalog(xmlHttp,objSelect,objSet,objHid);
				}
			}
	}
	xmlHttp.send(null)		
}

function handleSelectCatalog(xmlHttp,objSelect,objSet,objHid){
	//控件ID
	var clientStr=objSet.id;
	//控件前缀长度如"CatalogSelectCtrl1_CatalogSelect_Catalog1"的前缀为“CatalogSelectCtrl1_CatalogSelect_Catalog”

	var clientStrPrefixLen=clientStr.length-1
	var objSet_B=objSet.id.substr(0,clientStrPrefixLen);
	var objSet_Number=0;
	objSet_Number=parseInt(objSet.id.substr(clientStrPrefixLen,1));
	//alert(objSet_Number);
	for(var k=1;k<=catalog_levelNum;k++){//将所有下级都置空
	    if(k>=objSet_Number){
	       var childID=objSet_B+k;
	       var objChild=document.getElementById(childID);
	       //清除select的options,并剩下一项

	       clearOptions(objChild,k);
	    }
	}

	var xmlDoc=xmlHttp.responseXML;
	var items=xmlDoc.getElementsByTagName("item");
    objSet.options.length=items.length+1;
    //objSet.options[0].selected=true;
	for(var i=0;i<items.length;i++){
	    var id=items[i].getElementsByTagName("id")[0].firstChild.nodeValue;
	    var name=items[i].getElementsByTagName("name")[0].firstChild.nodeValue;
	    objSet.options[i+1].text=name;
	    objSet.options[i+1].value=id;
	}
	if(objSet.options.length<=1){
	    objHid.value=objSelect.options[objSelect.selectedIndex].value;
	}else{
	    objHid.value="";
	}
}

//catalog_select
function catalogRequest(URLStr,objSet){
    var xmlHttp=createXMLHttpRequest();
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				    XmlToSelect(xmlHttp,objSet);
				}
			}
	}
	xmlHttp.send(null)		
}


//清除select的options,并剩下一项

function clearOptions(objSet,num){
    var numStr;
    switch(num){
        case 1:numStr="------------- 一级分类 -------------";break;
        case 2:numStr="------------- 二级分类 -------------";break;
        case 3:numStr="------------- 三级分类 -------------";break;
        case 4:numStr="------------- 四级分类 -------------";break;
        default:break;
    }
     objSet.options.length=1;
     /*var option=document.createElement("option");
     var optionText=document.createTextNode(numStr);
     option.value=0;
     option.appendChild(optionText);
     objSet.appendChild(option);*/
}

/********************** 处理区域选择 ************************/
/***** 省－市－区 *****/
function AreaRequest(URLStr,objSelect){
    var xmlHttp=createXMLHttpRequest();
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				    XmlToSelect(xmlHttp,objSelect);
				}
			}
	}
	xmlHttp.send(null)		
}

//应用到货运路线信息的“省份城市联动”

function AreaInputRequest(URLStr,objSelect,objValue){
    var xmlHttp=createXMLHttpRequest();
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				XmlToSelect(xmlHttp,objSelect);
				//设置选择的项到objValue
				if(objSelect.options.length!=0&&objSelect.options[0].value!="0"){
				    objValue.value=objSelect.options[objSelect.selectedIndex].text;
				}else{
				    objValue.value="";
				}
			}
		}
	}
	xmlHttp.send(null)	
}
/**************************/

function IsRunOk(URLStr){
    var xmlHttp=createXMLHttpRequest();
	xmlHttp.open("GET",URLStr,true);
	if(xmlHttp){
		xmlHttp.onreadystatechange=function(){alert("ok");
			if(xmlHttp.readyState==4){
			    if(xmlHttp.status == 200 || xmlHttp.status == 0){
			        alert("ok");
			        //alert(xmlHttp.status);
			    }
			}
		}
	}
}

//function actionInsert(actionID,valueTypeID,val){
//    var domain=window.document.domain;
//    var URLStr="http://"+domain+"/handle/TraceLogHandle.aspx?actionRecordID="+actionID+"&&recordTypeID="+valueTypeID+"&&domain="+domain;
//    
//    var xmlHttp=createXMLHttpRequest();
//	xmlHttp.open("GET",URLStr,true);
//	if(xmlHttp){
//		xmlHttp.onreadystatechange=function(){
//			if(xmlHttp.readyState==4){
//				
//			}
//		}
//	}
//	xmlHttp.send(null);	
//}

