﻿//Dropdown list related variables
var rwSug = null; //Current highlighted row
var curRwNo = 0; //Current highlighted Row index
var msgFrWait = true;
var curKyCde = null;
var blockSrch =false; //to prevent submit on enter key

addEvent(document,"click",HideSuggestBox);

var txtDestCltID = null;
var hdLocID = null;
var trHide = null;
var fType = null;
var ddState = new Array();
function AutoSuggestTyping(evnt, txtBxId, hdnLocId, trToHd, fTyp)
{
    if(blockSrch==true) return;

    arrTextBoxesIdsToFillDesc=txtBxId.split(',');   
    txtDestCltID = document.getElementById(arrTextBoxesIdsToFillDesc[0]);     
      
    arrHiddenControlsIdsToFillCode=hdnLocId.split(',');
    hdLocID=document.getElementById(arrHiddenControlsIdsToFillCode[0]);   
    trHide = document.getElementById(trToHd);
    fType = fTyp;
    
    curKyCde = CurrKyCde(evnt);
    
    if (txtDestCltID.value.length < 3)
        return;
 
    
    if (curKyCde != 38 && curKyCde != 40 && curKyCde != 13 && curKyCde != 9)
    {
        GtSgsts();
        hdLocID.value="";
    }
}

function AutoSuggestArrowKeys(evnt, txtBxId, hdnLocId, trToHd, fTyp)
{
     if(blockSrch==false)
    {

        arrTextBoxesIdsToFillDesc=txtBxId.split(',');   
        txtDestCltID = document.getElementById(arrTextBoxesIdsToFillDesc[0]); 
            
        arrHiddenControlsIdsToFillCode=hdnLocId.split(',');
        hdLocID=document.getElementById(arrHiddenControlsIdsToFillCode[0]);   
        
        fType = fTyp;
        
        curKyCde = CurrKyCde(evnt);
       
        if (txtDestCltID.value.length < 3)
            return;
            
        if (curKyCde == 38 || curKyCde == 40 || curKyCde == 13 || curKyCde == 9)
        {
            var ulSgstDDL = document.getElementById("ulSuggest");
            if (ulSgstDDL == null || ulSgstDDL.childNodes.length == 0 || actBox.style.display == "none")
                return;

            SgstChkKy();
        }
    }
    else
    {
        blockSrch = false;
    }
}

function GtSgsts()
{   
	if (msgFrWait)
	    DispSgst("<span style='font: Arial, Helvatica, Sans-serif 12px !important; line-height:18px;'><strong>Please wait...</strong></span>");
	 
	MakeRequest(urlLS + "?prefix=" +  txtDestCltID.value + "&type=" + fType, "DispSgstLst");
}

function DispSgstLst(sugLstRes)
{
	DispSgst(unescape(sugLstRes));
	RstSelSgstRw();
	msgFrWait = false;   //Diplay Please wait only the first time user types in
}

function CurrKyCde(evnt)
{
    if (!evnt) var evnt = window.event
	if (evnt.keyCode) var keyCode = evnt.keyCode;
	else if (evnt.which) var keyCode = evnt.which;
	return keyCode;
}

function RstSelSgstRw()
{
    rwSug = null;
	curRwNo = 0;
}

function DispSgst(msge)
{
	if (msge == null || msge.length == 0)
		return;

	msg.innerHTML = msge;
	
	actBox.style.position = "absolute";
	actBox.style.left		= absXFrmLft(txtDestCltID) + "px";
	    
	var ulSgstDDL = document.getElementById("ulSuggest");
	
	actBox.style.top = absYFrmTp(txtDestCltID)+ txtDestCltID.clientHeight + "px";
    actBox.style.display	= "";

    hideSelForIE6(actBox, trHide, "visible");
    if(ulSgstDDL != null)
        hideSelForIE6(actBox, trHide, "hidden");

}

function HideSuggestBox()
{
	if(typeof hideHoverInfo == 'function')
		hideHoverInfo();

	if (actBox != null)
	    actBox.style.display	= "none";
	
	if(typeof hideSelForIE6 == 'function')
        hideSelForIE6(actBox, trHide, "visible");
        
    blockSrch = curKyCde==13? true : false;
    
}

function SetLocationStringsForRadio(str,tbID,hdnCntl)
{
    document.getElementById(hdnCntl).value = str.replace(/<\/?[^>]+(>|$)/g, "");
    document.getElementById(tbID).value = str.split("***")[1].replace(/<\/?[^>]+(>|$)/g, "");
}

function SetLocationStrings(str)
{
    hdLocID.value = str.replace(/<\/?[^>]+(>|$)/g, "");
    txtDestCltID.value = str.split("***")[1].replace(/<\/?[^>]+(>|$)/g, "");

    for(i=1;i<arrTextBoxesIdsToFillDesc.length;i++)
    {
        if(document.getElementById(arrTextBoxesIdsToFillDesc[i])!=null)
        {
            document.getElementById(arrTextBoxesIdsToFillDesc[i]).value=txtDestCltID.value;
        }
        if(document.getElementById(arrHiddenControlsIdsToFillCode[i])!=null)
        {
            document.getElementById(arrHiddenControlsIdsToFillCode[i]).value=hdLocID.value;
        }
    }
}

function SgstChkKy() 
{  
	switch (curKyCde)
	{
		case 38:
			SgstGoUp();
			break;
		case 40:
			SgstGoDwn();
			break;
		case 13: case 9:
		    if (rwSug != null)
		        SetLocationStrings(rwSug.id);
		    HideSuggestBox();
		    break;
		default:
			break;
	}
}

function SgstGoDwn()
{
    var ulSgstDDL = document.getElementById("ulSuggest");
    
    if (rwSug == null)
    {
        rwSug = ulSgstDDL.childNodes[0].childNodes[0];
        rwSug.style.backgroundColor = "#e5e5e5";
    }
    else if (ulSgstDDL.childNodes[curRwNo+1] != null)
    {
        ulSgstDDL.childNodes[curRwNo].childNodes[0].style.backgroundColor = "white";
		rwSug = ulSgstDDL.childNodes[curRwNo+1].childNodes[0];
        rwSug.style.backgroundColor = "#e5e5e5";
        
        
        curRwNo++;
    }
}

function SgstGoUp()
{
    var ulSgstDDL = document.getElementById("ulSuggest");
    
    if (rwSug == null)
        return;
    if (curRwNo == 0)
    {
        rwSug.style.backgroundColor = "white";
        rwSug = null;
    }
    else if (ulSgstDDL.childNodes[curRwNo-1] != null)
    {
        ulSgstDDL.childNodes[curRwNo].childNodes[0].style.backgroundColor = "white";
		rwSug = ulSgstDDL.childNodes[curRwNo-1].childNodes[0];
        rwSug.style.backgroundColor = "#e5e5e5";
        
        
        curRwNo--;
    }
}

function SuggestMouseOver(liElm)
{
    if (rwSug != null)
        rwSug.style.backgroundColor = "";
    rwSug = liElm;
    curRwNo = GtRwIdx(rwSug.id);
    rwSug.style.backgroundColor = "#e5e5e5";
}

function GtRwIdx(val)
{
    var ulSgstDdl = document.getElementById("ulSuggest");
    var rowNo = 0;
    for (var i=0; i<ulSgstDdl.childNodes.length; i++)
        if (ulSgstDdl.childNodes[i].childNodes[0].id == val)
            return rowNo;
        else
            rowNo++;    
}

function addEvent(obj,event_name,func_name){
	if (obj.attachEvent){
		obj.attachEvent("on"+event_name, func_name);
	}else if(obj.addEventListener){
		obj.addEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = func_name;
	}
}

function absYFrmTp(el)
{
    var absY = 0;
    var actElm = el;
    
    while(actElm)
    {
	    absY += actElm.offsetTop;
	    actElm = actElm.offsetParent;
    }
    return absY;
}

function absXFrmLft(el)
{
    var absX = 0;
    var actElm = el;
    while(actElm)
    {
	    absX += actElm.offsetLeft;
	    actElm = actElm.offsetParent;
    }
    return absX;
}

function hideSelForIE6(actDiv, objHaveSel, status)
{
var ieversion= -1
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number
  
 if (!(navigator.appName == 'Microsoft Internet Explorer' &&  ieversion < 7))
        return;
	if(objHaveSel == null || actDiv == null) return;

	if(status == "hidden")
	{
		var selInTR = objHaveSel.getElementsByTagName("select");

		if(selInTR == null || selInTR == undefined || selInTR.length == 0) return;
		var actTop = absYFrmTp(actDiv);
		var actBxHt = actTop + actDiv.offsetHeight;
		var actLft = absXFrmLft(actDiv);
		var actBxWd = actLft + actDiv.offsetWidth;
		ddState = new Array()       
		var selHt = 0;
		for(i=0;i<selInTR.length;i++)
		{

			selTp = absYFrmTp(selInTR[i]);
			selHt = selTp + selInTR[i].offsetHeight;
			selLft = absXFrmLft(selInTR[i]);
			selWd = selLft + selInTR[i].offsetWidth;
			if(((selLft > actLft && selLft < actBxWd) || (selWd > actLft && selWd < actBxWd)) && ((selTp > actTop && selTp < actBxHt) || (selHt > actTop && selHt < actBxHt)))
			{
				if(selInTR[i].style.visibility != "hidden")
				{
					ddState[ddState.length] = selInTR[i];       
					selInTR[i].style.visibility = status;
				}
			}
		}
	}
	else
	{
		if(ddState==null) return;
		for(i=0;i<ddState.length;i++)
		{
			ddState[i].style.visibility = "visible";
		}        
	}
}