﻿function ReCi()
{ 

if(document.implementation && document.implementation.createDocument)
{   
    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var oNSResolver = this.createNSResolver(this.documentElement)
          var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
          var aResult = [];
          for( var i = 0; i < aItems.snapshotLength; i++)
          {
             aResult[i] =  aItems.snapshotItem(i);            
          }
          return aResult;
       }

       // prototying the Element
       Element.prototype.selectNodes = function(cXPathString)
       {
          if(this.ownerDocument.selectNodes)
          {
             return this.ownerDocument.selectNodes(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }  
    }
    
//        Element.prototype.text = function()
//       {       
//          return this.childNodes[0].nodeValue;        
//       }
    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var xItems = this.selectNodes(cXPathString, xNode);
          if( xItems.length > 0 )
          {
             return xItems[0];
          }
          else
          {
             return null;
          }
       }
       
       // prototying the Element
       Element.prototype.selectSingleNode = function(cXPathString)
       {    
          if(this.ownerDocument.selectSingleNode)
          {
             return this.ownerDocument.selectSingleNode(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }
}

var request = false;
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }
   if (!request)
     alert("Error initializing XMLHttpRequest!");
     
 function transformxml(str)
    {
        if(window.DOMParser)//firefox内核的浏览器
        {
            var p = new DOMParser();
            return p.parseFromString( str, "text/xml" );
        }
        else if( window.ActiveXObject )//ie内核的浏览器
        {
            var doc = new ActiveXObject( "Msxml2.DOMDocument" );
            doc.loadXML(str);
            return doc;
        }
        else
            return false;
    }
     
function getCustomerInfo(InputName,Url) 
{
     //var phone = document.getElementById("phone").value;
     //var url = "http://localhost/aa/search.asp" ;    
     //document.write(Url+escape(document.getElementById(InputName).value));
     request.open("GET", Url+escape(document.getElementById(InputName).value), true);
     request.onreadystatechange =function(){ updatePage(InputName);};
     request.send(null);
     
}

function updatePage(InputName) 
{
    if(request.readyState!=4) return;  
       
   var doc=transformxml(request.responseText); 
  
   var nodes = doc.selectNodes("response/result/item");
 

   document.getElementById(InputName+"UL").innerHTML="";
    for(var iii= 0;iii<nodes.length;iii++)
    {
//        array[iii]=nodes[iii].text;
        var temp;
        if (!document.all)
            temp=nodes[iii].childNodes[0].nodeValue;
        else
            temp=nodes[iii].text;
        if(iii==0)
        document.getElementById(InputName+"UL").innerHTML+=
	                            "<li onmouseover=\"clearkeyupcolor('"+InputName+"');this.style.background='#cce8cf';\" onmouseout=\"this.style.background='';\" onmousedown=\"getValue('"
	                                                                +InputName+"','"+temp+"');showAndHide('"+InputName+"MENU','hide');\">"+temp+"</li>";
        else
        document.getElementById(InputName+"UL").innerHTML+=
	                            "<li onmouseover=\"clearkeyupcolor('"+InputName+"');this.style.background='#cce8cf';\" onmouseout=\"this.style.background='';\" onmousedown=\"getValue('"
	                                                                +InputName+"','"+temp+"');showAndHide('"+InputName+"MENU','hide');\">"+temp+"</li>";//getValue('"+InputName+"','"+temp+"');
    }
//  alert(document.getElementById(InputName+"UL").innerHTML);
    if(nodes.length>0)    
       showAndHide(InputName+"MENU",'show');  
}
//var array=new Array();

//function bekeypress(event,obj,InputName)
//{    
//    
////    switch(event.keyCode)
////    {
////        case 13:
////             getValue(InputName,obj.innerHTML);
////             showAndHide(InputName+"MENU","hide");
////             break;
////        case 38:
////             var element=obj.previousSibling;
////             if (element.tagName=="li")
////             {
////                element.style.background='#cce8cf';
////                obj.style.background='';
////             }     
////             break;  
////        case 40:
////             var element=obj.nextSibling;
////             if (element.tagName=="li")
////             {
////                element.style.background='#cce8cf';
////                obj.style.background='';
////             }     
////             break;                
////    }      
//}

//beKeyUp事件。与服务器通信 
this.beKeyUp=function(InputName,Url,event)
{
	if(event.keyCode!=13&event.keyCode!=9&event.keyCode!=38&event.keyCode!=40)
	{ 
//		if (document.getElementById(InputName).value.length<1)
//		{
//			//document.getElementById(InputName+"search_suggest").className="suggest_hidden"; 
//		}
		if (document.getElementById(InputName).value.length>=1)
		{ 		
			getCustomerInfo(InputName,Url);	
			return;		
		} 
	}
	var objtemps=document.getElementById(InputName+"UL").childNodes;
	var liObj=new Array();
	var liindex=-1;
	
	for(var i=0;i<objtemps.length;i++)
	{
	    if (objtemps[i].tagName=="LI")
	    {
	        liObj.push(objtemps[i]);//alert(objtemps[i].style.backgroundColor);
	        //if(objtemps[i].style.backgroundColor=='#cce8cf'|| objtemps[i].style.backgroundColor=='rgb(204,232,207)') alert("xds"+objtemps[i].style.backgroundColor);
	        if(objtemps[i].style.backgroundColor!="")
	            liindex=liObj.length-1;
	    }
	}	//alert(liindex);
	switch(event.keyCode)
	{
	    case 13:	
	            if (liindex>=0)  
	            {          
	                showAndHide(InputName+"MENU","hide");
	                getValue(InputName,liObj[liindex].innerHTML);
	            }
	            break;
	   case 38:  
	            if (liindex>0)
	            {
	                liObj[liindex].style.backgroundColor='';	                
	                liObj[liindex-1].style.backgroundColor='#cce8cf';	                
	            }
	            break;
	   case 40:
	            if (liindex<liObj.length-1)
	            {
	                if (liindex>=0) liObj[liindex].style.backgroundColor='';	                
	                liObj[liindex+1].style.backgroundColor='#cce8cf';	                
	            }
	            break;	            
	}
} 
}


function clearkeyupcolor(InputName)
{
    var objtemps=document.getElementById(InputName+"UL").childNodes;	
	for(var i=0;i<objtemps.length;i++)
	{
	    if (objtemps[i].tagName=="LI")
	    {	       
	         if(objtemps[i].style.backgroundColor!="")
	           objtemps[i].style.backgroundColor='';
	    }
	}	
}

function getValue(obj,str)
    {
     var input=document.getElementById(obj);
      input.value=str;
      
    }
function showAndHide(obj,types)
    {
        var Layer=document.getElementById(obj);
        switch(types)
        {
       case "show":
         Layer.style.display="block";
       break;
       case "hide":
         Layer.style.display="none";
       break;
       }
    }
    
function hex2rgb(hex) 
{ 
var r = (0xFF0000 & hex) >> 16; 
var g = (0x00FF00 & hex) >> 8; 
var b = (0x0000FF & hex); 
return "rgb(" + r + "," + g + "," + b + ")"; 
} 

function rgb2hexColor(strRgb) 
{ 
var strColor; 
var ch; 
var arrColor = strRgb.substr(strRgb.indexOf("(") + 1, strRgb.indexOf(")") - 1).split(","); 
strColor = "0x"; 
for (var i = 0; i < 3; i++) 
{ 
ch = parseInt(arrColor, 10).toString(16); 
strColor += ch.length == 2 ? ch : "0" + ch; 
} 
return strColor; 
} 
