﻿fpSuggest=function(qID,url,sb){
	this.qID=qID;
	this.id=document.getElementById(qID);
	this.url=url;
	this.sb=sb;
	this.div='_fpd';
	this.suggest='_fps';
	this.overBGColor='#3366FF';
	this.overTextColor='#FFF';
	this.outBGColor='#FFFFFF';
	this.outTextColor='#000';
	this.xmlhttp=this.createXHR();
	this.interval=null;
	this.lV=null;
	this.vC=true;
	this.vS='';
	var p =this;
	this.id.onkeyup = function(e){
	  document.onmouseup = function(e) {
		if (sb) {
		  var tgt;
		  if (!e) var e=window.event;
		  if (e.target) tgt = e.target;
		  else if (e.srcElement) tgt = e.srcElement;
		  if (tgt.nodeType==3){tgt=tgt.parentNode;}
		  if (tgt.id) {
		    if (tgt.id=='bt1'){p.clearDiv();changebt('g');}
		    if (tgt.id=='bt2'){p.clearDiv();changebt('c');}
		  }
		}
		p.clearDiv();
		 
	   }
	   return p.onKeyUp(e);
		
	}
	this.id.onkeydown = function(e){
		//document.onmousedown = function(e){p.clearDiv();};
		return p.onKeyDown(e);
	}
}

fpSuggest.prototype.onKeyUp=function(e){
	var p=this;
	var key = (!e) ? event.keyCode : e.keyCode;
	var ckey = (!e) ? event.ctrlKey : e.ctrlKey;
	switch(key){
		case 18:
			break;
		case 27:
			this.clearDiv();
			break;
		case 13:
			this.clearDiv();
			break;
		case 38:
			if (this.selectOBJ!=null){p.selectList('38');}
			break;
		case 40:
			if (this.selectOBJ!=null){p.selectList('40');}
			break;
		default:
			if (key>=112 && key <=123){
				break;
			}else if (ckey){
				break;
			}else{
				this.sendRequest();
				break;
			}
			break;
	}
}

fpSuggest.prototype.onKeyDown=function(e){
	var p=this;
	var key = (!e) ? event.keyCode : e.keyCode;
	var ckey = (!e) ? event.ctrlKey : e.ctrlKey;
	switch(key){
		case 197:
			if (p.interval==null){
				p.interval=setInterval(function(){p.IMEDetected();},500);
			}
			break;
		case 229:
			if (p.interval==null){
				p.interval=setInterval(function(){p.IMEDetected();},500);
			}
			break;
		default:
			if (p.interval!=null){
				window.clearInterval(p.interval);
				p.interval=null;
				break;
			}
			break;
	}
}

fpSuggest.prototype.selectList=function(direct){
	var _id=this.selectOBJ.getAttribute('id');
	var sib=(direct=='40')?'nextSibling':'previousSibling';
	if (_id=='_c_p'){
		this.selectOBJ.setAttribute('id',null);
		this.selectOBJ.style.backgroundColor=this.outBGColor;
		this.selectOBJ.style.color=this.outTextColor;
		if (this.selectOBJ[sib]){
			this.selectOBJ=this.selectOBJ[sib];
		}
	}
	if (this.selectOBJ.nodeName.toLowerCase()=='li'){
		this.selectOBJ.style.backgroundColor=this.overBGColor;
		this.selectOBJ.style.color=this.overTextColor;
		this.id.value=this.selectOBJ.firstChild.nodeValue;
		this.selectOBJ.setAttribute('id','_c_p');
	}
}

fpSuggest.prototype.IMEDetected=function(){
	var p=this;
	var currValue = this.id.value;
    var lV = this.lV;
  	if(currValue != lV) {
   		this.lV=currValue;
       	this.sendRequest();
   	}
}

fpSuggest.prototype.createLayer=function(){
	var divName=this.div;
	var sName=this.suggest;
	var obj=this.id;
	var oLeft,oDiv;
	if (document.getElementById(divName)==null){
		var div=document.createElement('div');
		div.setAttribute('id',divName);
		div.style.position='absolute';
		div.style.width=obj.offsetWidth+'px';
		div.style.visibility='hidden';
		obj.parentNode.insertBefore(div,obj);
		oDiv=obj;
		oLeft=oDiv.offsetLeft;
		while (oDiv.offsetParent){
			oDiv=oDiv.offsetParent;
			oLeft+=oDiv.offsetLeft;
		}
		div.style.top=(div.offsetTop+obj.offsetHeight)+'px';
		div.style.left=oLeft+'px';
		div.style.background='#FFF';
		div.style.border='#AAA 1px solid';
		//div.style.textAlign='left';
		div.style.fontFamily='verdana;arial';
		div.style.fontSize='11pt';
		div.style.lineHeight='18px';
		div.style.visibility='visible';
		this.suggestDiv=div;
	}
}

fpSuggest.prototype.showSuggest=function(va){
	var obj=document.getElementById(this.div);
	var ul=document.createElement('ul');
	var p=this;
	for (var i=0;i<va.length;i++){
		var li=document.createElement('li');
		var sText=document.createTextNode(va[i][1]);
		li.appendChild(sText);
		li.style.textAlign='left';
		li.style.cursor='default';
		li.style.padding=2;
		li.onmouseover=function(){
			p.selectOBJ.style.backgroundColor=p.outBGColor;
			p.selectOBJ.style.color=p.outTextColor;
			p.selectOBJ = this;
			this.setAttribute('id','_c_p');
			this.style.backgroundColor=p.overBGColor;
			this.style.color=p.overTextColor;
			};
		li.onmouseout=function(){
			this.style.backgroundColor=p.outBGColor;
			this.style.color=p.outTextColor;
			};
		li.onclick=function(){
			p.id.value=this.firstChild.nodeValue;
			p.clearDiv();
			form1.submit();
			};
		li.onmousedown=function(){
			p.id.value=this.firstChild.nodeValue;
			p.clearDiv();
			form1.submit();
			};			
		ul.appendChild(li);
	}
	ul.setAttribute('id',this.suggest);
	ul.style.margin=0;
	ul.style.padding=0;
	ul.style.listStyle='none';
	if (p.sb) {
    var divb=document.createElement('div');
	divb.setAttribute('id','bdiv');
	divb.style.margin='10px';
	divb.style.textAlign='center';
	divb.style.visibility='visible';
	var b1=document.createElement("input");
	b1.setAttribute('id','bt1');
	b1.setAttribute('name','bt1');
	b1.setAttribute('type','button');
	b1.setAttribute('value',' 商品搜尋 ');
    b1.style.fontSize='11pt';
	b1.style.lineHeight='20px';
	var b2=document.createElement("input");
    b2.setAttribute('id','bt2');
	b2.setAttribute('name','bt2');    	
	b2.setAttribute('type','button');
	b2.setAttribute('value',' 目錄搜尋 ');
    b2.style.fontSize='11pt';
	b2.style.lineHeight='20px';
    divb.appendChild(b1);
	divb.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;';
	divb.appendChild(b2);
	}
	if (this.id.value!=''){
		if (document.getElementById(this.suggest)){
			obj.replaceChild(ul,document.getElementById(this.suggest));
		}else{
			obj.appendChild(ul);
			if (p.sb)
			  obj.appendChild(divb);			
		}
		this.selectOBJ=ul.firstChild;
	}
}

fpSuggest.prototype.clearDiv=function(){
	if (document.getElementById(this.div)!=null){
		var obj=this.id;
		this.selectOBJ=null;		
		obj.parentNode.removeChild(this.suggestDiv);
	}
}

fpSuggest.prototype.createXHR=function(){
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}

	if (!xmlhttp) {
		return false;
	}else{
		return xmlhttp;
	}
}

fpSuggest.prototype.sendRequest=function(){
	var p=this;
    if(p.id.value.length <= 20 && p.id.value.length >0) {
      if (!p.vC && p.id.value.indexOf(p.vS)==0) {}
      else {
  	    var url=p.url+'?'+p.qID+'='+escape(p.id.value);
  	    p.vS=p.id.value; 
	    p.xmlhttp.open('GET',url,true);
	    p.xmlhttp.onreadystatechange=function(){p.catchXML()};
	    p.xmlhttp.send('');
	  }
	}
	else {this.clearDiv();}
}

fpSuggest.prototype.catchXML=function(){
    var p=this;
    p.vC = false;
	if (this.xmlhttp.readyState==4){
		xml=this.xmlhttp.responseXML;
		if (this.xmlhttp.status == 200) {
			var va=getNodeContent(xml);
			if (va.length!=0){
				this.createLayer();
				    p.vC = true;
    				this.showSuggest(va);
    			}else{
    				this.clearDiv();
    			}
		}
	}
}

Array.prototype.indexOf = function( v, b, s ) {
	for( var i = +b || 0, l = this.length; i < l; i++ ) {
		if( this[i]===v || s && this[i]==v ){
			return i;
		}
 	}
	return -1;
};

if( typeof Array.prototype.push==='undefined' ) {
	Array.prototype.push = function() {
		for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
			this[b+i] = a[i];
		}
		return this.length;
	};
}

Array.prototype.unique = function( b ) {
	var a = [], i, l = this.length;
	for( i=0; i<l; i++ ) {
		if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
	}
	return a;
};

function getNodeContent(xmldoc,key,label) {
	var na=[],va=[],l;
	var outstr='';
	key=(typeof key=='undefined')?getNodeName(xmldoc,false):key;
	for (var i=0;i<xmldoc.getElementsByTagName(key).length;i++){
		var xml=xmldoc.getElementsByTagName(key)[i];
		na=getNodeName(xml,false);
		for (var j=0;j<na.length;j++){
			var xmlk=xml.getElementsByTagName(na[j]);
			for (var k=0;k<xmlk.length;k++){
				l=Math.max(va.length,va.length-1);
				if(typeof label!='undefined'){
					if(typeof xmlk[k].getAttribute(label)!=null && typeof xmlk[k].getAttribute(label)!=''){
						va[l]=[xmlk[k].getAttribute(label),xmlk[k].childNodes[0].nodeValue];
					}else{
						va[l]=[na[j],xmlk[k].childNodes[0].nodeValue];
					}
				}else{
					va[l]=[na[j],xmlk[k].childNodes[0].nodeValue];
				}
				outstr+=va[l]+'\n';
			}
		}
	}
	return va
}

function getNodeName(xmldoc,duplicate){
	var d;
	var na=[];
	d=(typeof duplicate=='undefined')?true:duplicate;
	for (var i=0;i<xmldoc.childNodes.length;i++){
		if (xmldoc.childNodes[i].hasChildNodes()){
			na=na.concat(xmldoc.childNodes[i].tagName);
		}
	}
	na=(d)?na:na.unique();
	return na
}

function changebt(value){
  document.form1.s.value=value;
  if(value=="c"||value=="g"){
    document.form1.submit();
  }
}
function SearchforG()
{document.form1.s.value="g";}
function SearchforG1()
{document.form1.s.value="g";document.form1.pq.value="";}
function SearchforP()
{document.form1.s.value="p";}
function SearchforC()
{document.form1.s.value="c";}

