function Checksform(){
    var SearchKey=document.getElementById("SearchKey").value.replace(/(^\s*)|(\s*$)/g, "");
	var OrderType=document.getElementById("OrderType").value;
	var href="Search-";
	
	if(SearchKey==""){
		alert("Pls input Search key!");
		return false;
	}
	if(OrderType=="ProductID" || OrderType=="OrderID"){
	    if(isNumber(SearchKey)==false){
			alert("ID must be number!");
			return false;
    	}		
 	}	
	var keys=SearchKey.split(' ');
	SearchKey="";
	for(i=0;i<keys.length;i++){
		if(keys[i]!="")SearchKey+=keys[i]+"_";
	}
	SearchKey=SearchKey.substring(0,SearchKey.length-1);
	SearchKey=SearchKey.replace(/ /,'-');


	href+=SearchKey+".html";
	if(OrderType=="OrderID")href="searchOrder-"+SearchKey+".html";
	if(OrderType=="ProductID")href=SearchKey+"-product.html";
	parent.document.location.href=href;	
} 
function isNumber(str)
{
	var re = new RegExp("[^0-9-]","ig");
	var result = re.test(str);
	if(result!=true)
	{
		var ii=str*1
		if(ii)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
function onkeydownGo(obj){
    if(event.keyCode==13&&obj.value.replace(/(^\s*)|(\s*$)/g, "")!=""){//按下回车键		
        Checksform();
    }
}
///////////////////////////////////////////////////////////////
function Catalog_Control_Checksform(){
    var SearchKey=document.getElementById("Catalog_Control1_KeyWord").value.replace(/(^\s*)|(\s*$)/g, "");
	var href="Search-";
	
	if(SearchKey==""){
		alert("Pls input Search key!");
		return false;
	}
	
	var keys=SearchKey.split(' ');
	SearchKey="";
	for(i=0;i<keys.length;i++){
		if(keys[i]!="")SearchKey+=keys[i]+"_";
	}
	SearchKey=SearchKey.substring(0,SearchKey.length-1);
	SearchKey=SearchKey.replace(/ /,'-');

	href+=SearchKey+".html";
	parent.document.location.href=href;	
} 

function Catalog_Control_onkeydownGo(obj){
    if(event.keyCode==13&&obj.value.replace(/(^\s*)|(\s*$)/g, "")!=""){//按下回车键		
        Catalog_Control_Checksform();
    }
}
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
//事件实现
function getKeyWords(obj){
	if(obj.value!=""){
		//alert("star");
		///开始ajax操作
		$.post("AjaxKeyWords.aspx",
		      {Action:"post",KeyWords:obj.value},
        	  function(reMsg,textStatus){
				  //alert(reMsg.msg);
				  showKeyWordDiv(obj,reMsg.msg);
	          },
			  "json" );  
	}
}

var up_dw_Count=1;
var mouseOver=0;
var KeyWordArray="41|42|43|44|45|46|47".split('|');
var KeyWordDivCount=0;
function showKeyWordDiv(theObj,KeyWords){
	KeyWordArray=KeyWords.split('|');
	KeyWordDivCount=KeyWordArray.length;	
	
	var KeyWordDiv=document.getElementById("KeyWordDiv");
	var point=getObjPoint(theObj);
	var rightOffset=0;
	var topOffset=0;
	var bs=getBrowseVersion();
	var dWidth=200;
	if(theObj.id=="SearchKey")dWidth=220;
	else point.x-=16;
	if(bs=="FireFox"){
		KeyWordDiv.style.top=point.y+20+"px";
		KeyWordDiv.style.left=point.x+"px";
	}
	else{
		KeyWordDiv.style.top=point.y+20;
		KeyWordDiv.style.left=point.x;
	}

	KeyWordDiv.style.display="";	
	if(KeyWords=="")KeyWordDiv.style.display="none";
	
	var msg="";
	for(var i=0;i<KeyWordDivCount;i++){
		msg+="<div id=\"KeyWordDiv"+(i-1+2)+"\" onclick=\"clickSet(this,'"+theObj.id+"');\" onmouseover=\"mouseoverSet(this);\" style=\"width:"+dWidth+"px; line-height:20px; border-bottom:1px #CCCCCC dashed; float:left\">"+KeyWordArray[i]+"</div>";
	}
	KeyWordDiv.innerHTML=msg;
	
	for(var i=0;i<KeyWordDivCount;i++){
		document.getElementById("KeyWordDiv"+(i-1+2)).style.backgroundColor="#ffffCC";
	}
	if(up_dw_Count>0 && up_dw_Count<=KeyWordDivCount){
		var subDiv=document.getElementById("KeyWordDiv"+up_dw_Count);
		subDiv.style.backgroundColor="#FFCCCC";
	}
}

var sFlag=0;
function keyupSet(theObj){	
	var subDiv=document.getElementById("KeyWordDiv"+up_dw_Count);
	if(event.keyCode==13){//按下回车键		
		if(getBrowseVersion()=="FireFox"){
			theObj.value=subDiv.innerHTML;//选中值	
			closeKeyWordDiv();	
			if(theObj.id=="SearchKey")onkeydownGo(theObj);
			else Catalog_Control_onkeydownGo(theObj);			
			return;
		}
		
		if(KeyWordDiv.style.display==""){
			theObj.value=subDiv.innerHTML;//选中值			
		}
		else{//提交查询
			if(theObj.id=="SearchKey")onkeydownGo(theObj);
			else Catalog_Control_onkeydownGo(theObj);
		}
		closeKeyWordDiv();		
		return;
	}
	if(event.keyCode==40){//按下向下键
		if(up_dw_Count<=KeyWordDivCount)up_dw_Count++;	
		if(	up_dw_Count==KeyWordDivCount+1)up_dw_Count=1;
		for(var i=0;i<KeyWordDivCount;i++){
			document.getElementById("KeyWordDiv"+(i-1+2)).style.backgroundColor="#ffffCC";
		}
		if(up_dw_Count>0 && up_dw_Count<=KeyWordDivCount){
			var subDiv=document.getElementById("KeyWordDiv"+up_dw_Count);
			subDiv.style.backgroundColor="#FFCCCC";
		}
		return;
	}
	if(event.keyCode==38){//按下向上键
		if(up_dw_Count>0)up_dw_Count--;
		if(up_dw_Count==0)up_dw_Count=KeyWordDivCount;
		for(var i=0;i<KeyWordDivCount;i++){
			document.getElementById("KeyWordDiv"+(i-1+2)).style.backgroundColor="#ffffCC";
		}
		if(up_dw_Count>0 && up_dw_Count<=KeyWordDivCount){
			var subDiv=document.getElementById("KeyWordDiv"+up_dw_Count);
			subDiv.style.backgroundColor="#FFCCCC";
		}
		return;
	}
	
	getKeyWords(theObj);
}

function clickSet(theObj,inputID){
	document.getElementById(inputID).value=theObj.innerHTML;
	mouseOver=0;
	closeKeyWordDiv();
}
function mouseoverSet(theObj){
	mouseOver=1;
	for(var i=0;i<KeyWordDivCount;i++){
		document.getElementById("KeyWordDiv"+(i-1+2)).style.backgroundColor="#ffffCC";
	}
	theObj.style.backgroundColor="#FFCCCC";
}
function closeKeyWordDiv(){
	if(mouseOver==0){
		var KeyWordDiv=document.getElementById("KeyWordDiv");
		KeyWordDiv.style.display="none";
		up_dw_Count=1;
	}
}
//取得对像的绝对坐标point
function getObjPoint(e){
   var t=e.offsetTop;
   var l=e.offsetLeft;
   while(e=e.offsetParent){ 
		t+=e.offsetTop;
		l+=e.offsetLeft;
   }			
	var point = new Object;
	point.x=l;
	point.y=t;
	return point;
}

function getBrowseVersion()
{
	var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;

    //以下进行测试
	if(Sys.ie)return 'IE:'+Sys.ie; 
	if(Sys.firefox)return "FireFox"; //('Firefox: '+Sys.firefox);
	if(Sys.chrome)return "Chrome";//('Chrome: '+Sys.chrome);
	if(Sys.opera)return "Opera";//('Opera: '+Sys.opera);
	if(Sys.safari)return "Safari";//('Safari: '+Sys.safari);	
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// JavaScript Document
//发送请求取得子目录
function AjaxMethod_Request(ClassID,ElementID,Level){         
	var theLoad=document.getElementById(ElementID);
	if(document.getElementById("L_"+ElementID)==null){
	    theLoad.innerHTML=theLoad.innerHTML+"<div id='L_"+ElementID+"' style='margin-top:-15px; margin-bottom:-13px;'><p align='center' style='line-height:20px;'>Loading....<br><img src='images/loading2.gif'></p></div>";
	}
	var fatherElement=document.getElementById(ElementID);
	
	//alert(ClassID+"-"+ElementID+"_"+Level);
	///开始ajax操作
	$.post("AjaxCatalog.aspx",
	      {Action:"post",theClassID:ClassID,theElementID:ElementID,theLevel:Level},
          function(reMsg,textStatus){
          //alert(reMsg.msg);
		      CallbackMethod_CheckState(reMsg.msg);                  
          },
		  "json" );   
}
    
//当状态改变时（例如收到数据时），CallbackMethod_CheckUpState将激发
function CallbackMethod_CheckState(response) { 
    if (response.length > 0) {
  		var ds=new myDataSet(); 
        ds.setValues(response);
		var theLoad=document.getElementById("L_"+ds.ElementID);
		theLoad.style.display="none";
        showSubList(ds); 
    } 
} 
    

function addList(ID,ParentID,ClassName,LeafFlag,ul,Level,NumID){        
    var li=document.createElement("div");
    li.id=NumID;
	if(LeafFlag!="1"){
    	var a = document.createElement("a");
    	a.className="cta_"+Level+"_Plus";
    	a.id="a_"+NumID;
	    Level=Number(Level)+1;
		a.href = "javascript:CatalogItemClick("+NumID+","+Level+");";
    	a.innerHTML="&nbsp;";
    	li.appendChild(a);	

		Level=Number(Level)-1;
		var b = document.createElement("a");
    	b.className="cta_"+Level+"_Text";
	    Level=Number(Level)+1;
	    var theCN=ClassName.replace(/ /g,'-');
	    theCN=theCN.replace(/&/g,'-');
	    theCN=theCN.replace('/','-');


		//b.href = "c"+ID+"-"+theCN+".html";
		b.href="javascript:CatalogItemClick("+NumID+","+Level+");";
	    b.innerHTML=ClassName;
    	li.appendChild(b);
	}else{
		var a = document.createElement("a");
	    Level=Number(Level)+1;	    
	     var theCN=ClassName.replace(/ /g,'-');
	    theCN=theCN.replace(/&/g,'-');
	    theCN=theCN.replace('/','-');

		a.href = "showList.aspx?ID="+ID;  
		a.className="Leval_"+(Level-1)+"_Leaf";
		a.innerHTML=ClassName;
		li.appendChild(a);
	}
	
    ul.appendChild(li);            
}

function showSubList(ds){
    var fatherElement=document.getElementById(ds.ElementID);
    var ul=document.createElement("div");
    ul.id="ul_"+ds.ElementID;
    fatherElement.appendChild(ul);
    for(var i=0;i<ds.length;i++){
        addList(ds.ID[i],ds.ParentID[i],ds.ClassName[i],ds.LeafFlag[i],ul,ds.Level,ds.NumID[i]);
    } 
}
function myDataSet(){
    this.ID;
    this.NumID;
    this.ParentID;
    this.ClassName;
    this.LeafFlag;
    this.length;   
    this.ElementID;
    this.Level;
    this.setValues = function(msg){
        var msg=msg.split('^');
        this.ID=msg[0].split('|');
        this.NumID=msg[1].split('|');
        this.ParentID=msg[2].split('|');
        this.ClassName=msg[3].split('|');
        this.LeafFlag=msg[4].split('|');
        this.ElementID=msg[5];
        this.Level=msg[6];
        this.length=this.NumID.length;                  
    }     
}

function CatalogItemClick(ID,Level){   	
    var ul=document.getElementById("ul_"+ID);
	var a=document.getElementById("a_"+ID);
	var theLoad=document.getElementById("L_"+ID);
    if(ul!=null){
		//if(theLoad!=null){//已下加载完
        	if(ul.style.display=="none"){
				ul.style.display="";
				a.className="cta_"+(Level-1)+"_Line";
			}
        	else {
				ul.style.display="none";
				a.className="cta_"+(Level-1)+"_Plus";
			}
		//}
    }else {
		if(theLoad==null){
			a.className="cta_"+(Level-1)+"_Line";
			AjaxMethod_Request(ID,ID,Level);
		}
	}
}

