var popWin;
var indexs=Array("keyword","row1","row2","row3","row4","row5","row6","row7","row8","row9");
var currentIndex=indexs[0];
function setNextIndex(index){
	for(i in indexs){
		if(indexs[i]==index) {
			if(i<9) { 			
				while(++i<=9&&trim(document.getElementById(indexs[i]).innerHTML)==""){  }
				if(i>9) {currentIndex=indexs[0];return;}
				currentIndex=indexs[i]; return;
			}
			else {currentIndex=indexs[0];return;}
		}
	}	
}
function setPreviousIndex(index){
	for(i in indexs){
		if(indexs[i]==index) {
			if(i>0) { 
				while(--i>=0&&trim(document.getElementById(indexs[i]).innerHTML)==""){  }
				if(i<0) {currentIndex=indexs[0];return;}			
				currentIndex=indexs[i]; return;
			}
			else {
				i=9;
				if(trim(document.getElementById(indexs[i]).innerHTML)!=""){ currentIndex=indexs[9];return;}
				while(--i>=0&&trim(document.getElementById(indexs[i]).innerHTML)==""){  }
				if(i<0) {currentIndex=indexs[0];return;}			
				currentIndex=indexs[i]; return;
			}
		}
	}	
}
function keepPop(){
	window.clearTimeout(popWin);
}
function trim(str) {
 var m = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);
 return (m == null) ? "" : m[1];
}
function replaceSpe(str){
  var result=str.replace('&amp;','&').replace('&lt;','<').replace('&gt;','>');
  return result;
}
function closePop(){
	document.getElementById("popDiv").style.display='none';
}	
function closePopDelay(){
	popWin=window.setTimeout("closePop();",0.5*1000);		
}
function setOverColor(elementId){
	if(elementId!=indexs[0])	document.getElementById(elementId).bgColor="#CCCCFF";		
}
function setOutColor(elementId){
	if(elementId!=indexs[0])  document.getElementById(elementId).bgColor="#FFFFFF";
}
function setPopPosition(){
	var spElement=document.getElementById(indexs[0]);
	var topPx , leftPx;
	topPx=spElement.offsetTop+20;
	leftPx=spElement.offsetLeft;
	// if IE then spElement.offsetParent.offsetTop>0 ,...
	if(spElement.offsetParent.offsetTop>0) {
		document.getElementById("popTable").width = 273;
		leftPx += 1;
		topPx+=2;
	}
	while(spElement.offsetParent){ 
		topPx += spElement.offsetParent.offsetTop;
		leftPx +=	spElement.offsetParent.offsetLeft;	
		spElement=spElement.offsetParent;
	}	
	document.getElementById("popDiv").style.top  = topPx+'px';
	document.getElementById("popDiv").style.left = leftPx+'px';
}
function displayPop() {
	if(arguments[0]!=undefined){
		var event=arguments[0];
		if (event.keyCode==13) submitKeywordForm();
		if (event.keyCode==40) {
			setOutColor(currentIndex);
			setNextIndex(currentIndex);
			setKeyWord(currentIndex);
			setOverColor(currentIndex);
		}
		if (event.keyCode==38) {
			setOutColor(currentIndex);
			setPreviousIndex(currentIndex);
			setKeyWord(currentIndex);
			setOverColor(currentIndex);
		}
	}
	window.clearTimeout(popWin);
	setPopPosition();
	document.getElementById("popDiv").style.display='';	
	return;
}
function setKeyWord(elementId){		
	document.getElementById("keyword").value=replaceSpe(document.getElementById(elementId).innerHTML);
	document.getElementById("keyword").focus();
}	
function setCookie(name,value,expires)
{
	//alert("setCookie");
	var expDays = expires*24*60*60*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime()+expDays);
	var expString = ((expires==null) ? "" : (";expires="+expDate.toGMTString()));
	document.cookie = name + "=" + escape(value) + expString ;	
} 
function getCookie(name)
{
	var result = null;
	var myCookie = document.cookie + ";";
	var searchName = name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1)
	{
		startOfCookie += searchName.length;
		endOfCookie = myCookie.indexOf(";",startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
} 
function initPopCookies(){
	for(i=1;i<10;i++){
		var tmpCookie=null;
		var id="row"+i;
		if((tmpCookie=getCookie("pop"+i))!==null) {document.getElementById("row"+i).innerHTML=tmpCookie;}
		else document.getElementById("row"+i).innerHTML="";			
		if(trim(document.getElementById("row"+i).innerHTML)=="") {document.getElementById("rowTR"+i).style.display='none';}
	}		
}
function setPopCookie(){
	if(document.cookie.indexOf("="+escape(trim(document.getElementById("keyword").value))+";")==-1){ 			
		var popId=null;
		if((popId=getCookie("popId"))!=null){
			popId=parseInt(popId);
			if(popId>=9) popId=0;
			popId+=1;
		}	else popId=1;	
		setCookie("popId",popId,365);		
		var name="pop"+popId;
		setCookie(name,trim(document.getElementById("keyword").value),365);		
	}
	return true;
}		
function submitKeywordForm(){
	
	if(!validateSearchForm(document.getElementById("searchForm"))) return;
	if(setPopCookie()) {
		 document.getElementById("searchForm").submit();
	}
}

function checkKeyword(){
	if(trim(document.getElementById("keyword").value)==''){
		alert('Keyword is required.');
		return false;
	}
	return true;
}
function setMouseOver(elementId){
	setOutColor(currentIndex);
	currentIndex=elementId;
	setOverColor(currentIndex);
}
