//////////////////
// Helper Funcs //
//////////////////
// used to find the Automation object name

function LoadHtml(url,ele){ 
var xh=window.Event?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP") ;
xh.open("GET",url,true) ;
if(window.Event) 
{xh.overrideMimeType("text/xml;charset=gb2312");}

window.gb2utf8=function(data){ 
var glbEncode=[],t,i,j,len ;gb2utf8_data=data;execScript("gb2utf8_data = MidB(gb2utf8_data, 1)+' '", "vbscript");
t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");t=t.split("@");i=0;len=t.length ;
while(++i<len){j=t[i].substring(0,4);if(!glbEncode[j]){gb2utf8_char = eval("0x"+j);execScript("gb2utf8_char=Chr(gb2utf8_char)","vbscript");glbEncode[j]=escape(gb2utf8_char).substring(1,6);} 
t[i]=glbEncode[j]+t[i].substring(4);} 
gb2utf8_data=gb2utf8_char=null;return unescape(t.join("%")).slice(0,-1);
} 
xh.onreadystatechange=function(){if(xh.readyState!=4) return;document.getElementById(ele).innerHTML=window.Event?xh.responseText:window.gb2utf8(xh.responseBody);} 

xh.send(null) 
} 


function GetHtml(url,ele)
{
var xmlHttp = XmlHttpPool.pick();	
if(url.indexOf("?")>=0) url+="&ran="+Math.random();else url+="?ran="+Math.random();
xmlHttp.open("GET", url, true); 
xmlHttp.send(null);
xmlHttp.onreadystatechange = function(){if (xmlHttp.readyState == 4) 
{
	var htm=typeof(xmlHttp.responseText)=="undefined"?"&nbsp;":xmlHttp.responseText;
	if(htm.indexOf("404")>0 && htm.indexOf("javascript:history.back(1)")>0) htm="";
	if(ele==null) document.write(htm);else document.getElementById(ele).innerHTML= htm;
	//if(xmlHttp.responseText.indexOf("404")>0 && xmlHttp.responseText.indexOf("javascript:history.back(1)")>0) return "";
	//if(ele==null) document.write(xmlHttp.responseText);else document.getElementById(ele).innerHTML= typeof(xmlHttp.responseText)=="undefined"?"&nbsp;":xmlHttp.responseText;
}
}
}

function GetJS(url)
{
var xmlHttp = XmlHttpPool.pick();	
if(url.indexOf("?")>=0) url+="&ran="+Math.random();else url+="?ran="+Math.random();
xmlHttp.open("GET", url, true); 
xmlHttp.send(null);
xmlHttp.onreadystatechange = function(){if (xmlHttp.readyState == 4)try{eval(xmlHttp.responseText);}catch(e){}}
}


function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XMLHttp object");
}


//////////////////////////
// Start the Real Funcs //
//////////////////////////

// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		// NS & MOZ
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			
			return req;
		}
		// IE
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// Fail
	throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() {}

XmlDocument.create = function () {
	try {
		// DOM2, MOZ & NS
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
			
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlDocument objects");
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	//XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
			
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	
	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}

/*
 * xmlHttp Pool
 * 
 * userage: var xmlhttpObj = XmlHttpPool.pick()
 */
var XmlHttpPoolArr = new Array();
var XmlHttpPoolSize = 100;
var XHPCurrentAvailableID = 0;

function XmlHttpPool() {}

XmlHttpPool.pick = function() {
	var pos = XHPCurrentAvailableID;
	XmlHttpPoolArr[pos] =  XmlHttp.create();
	
	XHPCurrentAvailableID >= (XmlHttpPoolSize-1) ? 0 : XHPCurrentAvailableID++
	
	return XmlHttpPoolArr[pos];
}




var selectrow=-1;
var totalrow=0;
function startsearch(obj){

if(totalrow>0 && (event.keyCode==37 ||event.keyCode==39) ) {stopsearch();return;}
if(totalrow>0 && (event.keyCode==38 || event.keyCode==40))
{
for(i=0;i<totalrow;i++)document.getElementById('qtr'+i).style.backgroundColor='';
if(event.keyCode==38) {if(selectrow>0) selectrow--;if(selectrow==-1) selectrow=totalrow-1;
	document.getElementById('qtr'+selectrow).style.backgroundColor='#E0F4FF'; 
	document.getElementById('k').value=document.getElementById('qtd'+selectrow).innerHTML;
	}
if(event.keyCode==40) {if(selectrow<totalrow-1) selectrow++;
	document.getElementById('qtr'+selectrow).style.backgroundColor='#E0F4FF';
	document.getElementById('k').value=document.getElementById('qtd'+selectrow).innerHTML;
	}
return;
}	
	
	
	
	
var k=obj.value;
var w=obj.style.width;
if(w.indexOf("px")>0) w=(w.substring(0,w.length-2))*1-1;
if(k=="") {stopsearch();return;}
var u="/member/quicksearchjs.aspx?r="+Math.random()+"&t=item&o=id&k="+k;

var xx = XmlHttpPool.pick();	
var tx="";
xx.open("GET", u, true); 
xx.send(null);
xx.onreadystatechange = function(){if (xx.readyState == 4){
if(xx.responseText=="") {stopsearch();return;}
var tt=xx.responseText.split('|');

totalrow=tt.length;
var html="<table width="+w+" id=searchtb bgcolor=#FFFFFF style='border:solid 1px #aaaaaa;'>";
for(i=0;i<tt.length;i++){html+="<tr id=\"qtr"+i+"\" onmouseover=\"this.style.backgroundColor='#E0F4FF';selectrow="+i+";document.getElementById('"+obj.id+"').value='"+tt[i]+"'\"  onmouseout=this.style.backgroundColor='' onclick=\"document.getElementById('k').value='"+tt[i]+"'\"><td id=\"qtd"+i+"\">"+tt[i]+"</td></tr>";}
html+="</table>";
document.getElementById('quicksearch').innerHTML=html;
document.getElementById('quicksearch').style.display='';
document.getElementById('quicksearch').style.top=gety(obj)+20;
document.getElementById("quicksearch").style.left=getx(obj);
//document.getElementById('searchtb').width=obj.style.width;

}}

}

function stopsearch(){
totalrow=0;
document.getElementById('quicksearch').innerHTML='';
document.getElementById('quicksearch').style.display='none';
selectrow=-1;
	}
