var errMsg = "";
function closeDiv() {
	if(document.getElementById("popupdiv") != null && 
		typeof(document.getElementById("popupdiv")).tagName != 'undefind') {
		document.getElementById("popupdiv").style.display='none';
	}
	if(document.getElementById("cityenvelopdiv") != null && 
		typeof(document.getElementById("cityenvelopdiv")).tagName != 'undefind') {
		document.getElementById("cityenvelopdiv").style.display='none';	
	}
	if(document.getElementById("popupUpdateBarDiv") != null && 
		typeof(document.getElementById("popupUpdateBarDiv")).tagName != 'undefind') {
		document.getElementById("popupUpdateBarDiv").style.display='none';
	}
}
 var   HH,LL;   
  function   getBrowser(e){   
      var   t=e.offsetTop;   
      var   l=e.offsetLeft;   
      while(e=e.offsetParent){   
          t+=e.offsetTop;   
          l+=e.offsetLeft;   
          }   
  HH   =   t;   
  LL   =   l;       
  }   
  var stockNamePub = "";
  var pubAutoComFlag = true;
  var pubFormName = "";
  function   showDiv(tt,frm)   
  {   
	  var elCheck = /^[0-9]+$/;
	  var engCheck=/^[~`!@#$%^&*(){}\[\]\\:\'\",\/.<>?;-_=+]+$/;
	  if(tt.value.trim() != "") {
	    if(engCheck.test(tt.value.trim().toLowerCase().substring(0,1)) == false) {
		  if(elCheck.test(tt.value.trim()) == false) {
		    if(stockNamePub != tt.value.trim()) {
			  getBrowser(tt);   
			  var   da   =   document.getElementById("popupdiv").style;   
			  da.top   =   HH   +   4   +   tt.clientHeight;   
			  da.left=   LL;   
			  da.display   =   "";   
			  var da1 = document.getElementById("cityenvelopdiv").style;
			  da1.top   =   HH   +   4   +   tt.clientHeight;   
			  da1.left=   LL;
			  if(da.height == "") {
				da.height = document.getElementById("popupdiv").offsetHeight;
			  }
			  da1.height = da.height;
			  document.getElementById("envelopifame").style.height = da.height;
			  da1.display   =   "";
			  var da2 = document.getElementById("popupUpdateBarDiv").style;
			  da2.top   =   HH   +   4   +   tt.clientHeight;   
			  da2.left=   LL;   
			  
			  var common_req;
			  arrBarDiv[arrIndex] = 'popupUpdateBarDiv';
			  arrListDiv[arrIndex] = 'showValueDiv';
			  arrReq[arrIndex] = common_req;
			  pubFormName = frm;
			  if(pubAutoComFlag) {
				  common_showfiles1('stocksearchboxajax.action?stockName='+encodeURIComponent(tt.value.trim())+
				  		'&formName='+frm+'&iIndexNo='+arrIndex, arrIndex);
				  arrIndex++;
				  stockNamePub = tt.value.trim();
			  }
			}
		  }
		} 
	 } else {
	 	stockNamePub = "";
	 	closeDiv();
	 }
  }   
  
  String.prototype.trim = function() 
{ 
  return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

  function common_showfiles1(url, aIndex) {
  		document.getElementById(arrListDiv[aIndex]).style.display = 'none';
		document.getElementById(arrListDiv[aIndex]).innerHTML = '';
		document.getElementById(arrBarDiv[aIndex]).style.display = '';
		arrReq[aIndex]=common_createXMLHttpRequest();
		common_runAjax(arrReq[aIndex],url,common_showpass1,'POST',aIndex);
		pubAutoComFlag = false;
  }
  
  function common_showpass1(arrIndex) {
  	var req = arrReq[arrIndex];
		var str;
		pubAutoComFlag = true;
		if(req.readyState==4){
			if(req.status==200) {
				var showDivFlag = false;
				var formName = document.forms[pubFormName].name;
				if (formName == 'searchDividendForm') {
					if(stockNamePub != document.forms[pubFormName].stockCodePara.value.trim()) {
						showDiv(document.forms[pubFormName].stockCodePara,pubFormName);
						showDivFlag = true;
					}
				}  else if(formName == 'compfrm') {
					if(stockNamePub != document.forms[pubFormName].coname.value.trim()) {
						showDiv(document.forms[pubFormName].coname,pubFormName);
					    showDivFlag = true;
					}
				
				} else {
					if(stockNamePub != document.forms[pubFormName].stockCode.value.trim()) {
						showDiv(document.forms[pubFormName].stockCode,pubFormName);
						showDivFlag = true;
					}
				}

				if(!showDivFlag) {
					str=req.responseText.trim();
					if(str.indexOf('false') < 0) {
						var arrs = str.substring(str.lastIndexOf(",")+1,str.length).replace(/\^/g,",");
						if(arrs == stockNamePub.replace(/\^/g,",")) {
							document.getElementById(arrBarDiv[arrIndex]).style.display = 'none';
							document.getElementById(arrListDiv[arrIndex]).style.display = '';
							document.getElementById(arrListDiv[arrIndex]).innerHTML = str.substring(0,str.lastIndexOf(","));
							
							if(document.getElementById("recordTab") != null && typeof(document.getElementById("recordTab")).tagName != "undefind") {
								if(document.getElementById("recordTab").style.height != "") {
									document.getElementById("cityenvelopdiv").style.height = document.getElementById("recordTab").style.height;
									document.getElementById("envelopifame").style.height = document.getElementById("recordTab").style.height;
									document.getElementById(arrListDiv[arrIndex]).style.height = document.getElementById("recordTab").style.height;
								} else {
									document.getElementById("cityenvelopdiv").style.height = document.getElementById("recordTab").offsetHeight;
									document.getElementById("envelopifame").style.height = document.getElementById("recordTab").offsetHeight;
									document.getElementById(arrListDiv[arrIndex]).style.height = document.getElementById("recordTab").offsetHeight;
								}
							}
						}
					} else {
						var arrs = str.split(",");
						if(arrs[1].replace(/\^/g,",") == stockNamePub.replace(/\^/g,",")) {
							document.getElementById(arrBarDiv[arrIndex]).style.display = 'none';
							//document.getElementById(arrListDiv[arrIndex]).style.display = 'none';
							closeDiv();
						}
					}
				}
			} else {
				document.getElementById(arrBarDiv[arrIndex]).style.display = 'none';
				closeDiv();
			}      
		}
  }

function selectValue(frm,value) {
	var formName = document.forms[frm].name;

	if (formName == 'searchDividendForm') {
		document.forms[frm].stockCodePara.value = value;
		closeDiv();
	} else if(formName == 'compfrm') {
		document.forms[frm].coname.value = value;
		closeDiv();	
		document.forms[frm].submit();
	}else {
		document.forms[frm].stockCode.value = value;
		closeDiv();
		document.forms[frm].submit();
	}
}
document.onclick=closeDiv;
