﻿function AjaxSearchCombo(ClassID, DivID, txtSearchID, txtValueID, tblResultID, CurrentClass, SplashScreenId, SplashScreenPath)
{
this.divSearch = document.getElementById(DivID);
this.txtSearch = document.getElementById(txtSearchID);
this.txtValue = document.getElementById(txtValueID);
this.tblResult = null;
this.SplashScreenId = SplashScreenId;
this.SplashScreenPath = SplashScreenPath;
this.Opened = false;    
this.ManuelFocus = false;
this.Handled = false;
this.FirstWidth = null;
this.FirstHeight = null;
this.ID = ClassID;
this.CurrentClass = CurrentClass;
this.AllowHide = function (p_ID)
{
if(p_ID == this.txtSearch.id || p_ID == this.divSearch.id)
{return false;}
return true;
}
this.StartSearch = function (evnt)
{
if(this.txtSearch.value != this.strSearch)
{
setTimeout(this.ID + ".SendSearchString()",150);
}
return false;
}
this.StopSearch = function ()
{
this.Opened = false;
this.PrepareDDLs(false);
setTimeout(this.ID + ".HidePanel()",200);
}
this.Waiting = function ()
{
if(this.SplashScreenId)
{
LoadingInDiv(this.divSearch.id, this.SplashScreenId, this.SplashScreenPath);
}
}

this.strSearch ="";

this.SendSearchString = function ()
{
var strNewSearch = this.txtSearch.value;
if(strNewSearch.length < 2)
{
this.PrepareDDLs(false);
this.HidePanel();
this.divSearch.innerHTML = "";
this.txtValue.value ="";
this.strSearch = "";
this.HasData = false;
return;
}

this.ShowPanel();

if(strNewSearch != this.strSearch)
{
this.Waiting()
this.txtValue.value ="";
//this.txtSearch.className = "InValidSearchTextBox_" + this.CurrentClass;
eval("CallServer_" + this.ID + "(strNewSearch,'1')");
this.strSearch = strNewSearch;
}
}

this.HasData = false;

this.ReceiveServerData = function (args, context)
{
this.divSearch.innerHTML = "";

if(args.length==0)
{
this.PrepareDDLs(false);
this.HidePanel();
this.HasData = false;
return;
}

this.HasData = true;

this.divSearch.innerHTML = args;

this.tblResult = document.getElementById(tblResultID);
}

this.HandleArrowKeys = function (evt)
{
this.Handled=false;

tableObj = this.tblResult;
//ClearCells(tableObj);

evt = evt || window.event;

if (evt && tableObj) {

if(tableObj.HighLightRow)
{
tableObj.HighLightRow.className = tableObj.HighLightRowOldStyle;
}

if(tableObj.RowIndex == null)
{
tableObj.RowIndex = -1;
}

switch (evt.keyCode) {
case 38:
this.Handled=true;
this.Handled=true;
if(tableObj.rows.length==0)
{break}
if(tableObj.RowIndex==-1)
{
tableObj.RowIndex = 0;
tableObj.rows[tableObj.RowIndex].className = this.CurrentClass + "_HighLight";
}
else
{
tableObj.rows[tableObj.RowIndex].className = (tableObj.RowIndex % 2==0)? this.CurrentClass + "_ItemStyle": this.CurrentClass + "_AlternatingItemStyle";
tableObj.RowIndex--;
if(tableObj.RowIndex<0)
{
    tableObj.RowIndex = tableObj.rows.length-1;
}

tableObj.rows[tableObj.RowIndex].className = this.CurrentClass + "_HighLight";
}
break;
case 40:
if(tableObj.RowIndex>=0 && tableObj.rows.length>tableObj.RowIndex)
{
tableObj.rows[tableObj.RowIndex].className = (tableObj.RowIndex % 2==0)? this.CurrentClass + "_ItemStyle": this.CurrentClass + "_AlternatingItemStyle";
}
tableObj.RowIndex++; 
if(tableObj.rows.length<=tableObj.RowIndex)
{tableObj.RowIndex = 0;}
this.Handled=true;
if(tableObj.rows.length==0)
{break}
tableObj.rows[tableObj.RowIndex].className = this.CurrentClass + "_HighLight";
break;
case 9:
case 13:
if(!this.Opened)
{break;}

this.Handled=true;

if(tableObj.RowIndex>-1 && tableObj.RowIndex<tableObj.rows.length)
{
if(evt.keyCode==13)
{
if (evt.cancelBubble)
{
evt.returnValue = false;
evt.cancelBubble = true;
}
if (evt.stopPropagation) 
{
evt.stopPropagation();
evt.preventDefault();
}
}
tableObj.rows[tableObj.RowIndex].onclick();
}
else
{
this.Handled=false;
}

if(evt.keyCode==9)
{
this.Handled=false;
this.StopSearch();
}

break;
default:
this.Handled=false;
break;
}

if(this.Handled && tableObj.RowIndex>=0 && tableObj.RowIndex < tableObj.rows.length)
{
if(tableObj.rows[tableObj.RowIndex].offsetTop < this.divSearch.scrollTop)
{
this.divSearch.scrollTop = tableObj.rows[tableObj.RowIndex].offsetTop;
}
else if((tableObj.rows[tableObj.RowIndex].offsetTop + tableObj.rows[tableObj.RowIndex].offsetHeight) > (this.divSearch.scrollTop + this.divSearch.offsetHeight))
{
this.divSearch.scrollTop = tableObj.rows[tableObj.RowIndex].offsetTop;
}
tableObj.HighLightRow = tableObj.rows[tableObj.RowIndex];
tableObj.HighLightRowOldStyle = tableObj.rows[tableObj.RowIndex].className;
}
}
return !this.Handled;
}

this.ShowPanel = function ()
{
if(this.Opened)
    return;

AddDocumentListeners(this);
this.divSearch.style.display = 'block';

this.Opened = true;

if(this.FirstWidth)
{
this.divSearch.style.width = this.FirstWidth;
this.divSearch.style.height = this.FirstHeight;
}
else
{
this.FirstWidth = this.divSearch.offsetWidth;
this.FirstHeight = this.divSearch.offsetHeight;
}

var coors = findPos(this.txtSearch);
var LeftPos = coors[0];
var TopPos = coors[1] + this.txtSearch.offsetHeight;
var RigthPos = LeftPos + this.divSearch.offsetWidth;
var BottomPos = TopPos + this.divSearch.offsetHeight;

if(RigthPos>getInsideWindowWidth())
{
LeftPos = getInsideWindowWidth() - this.divSearch.offsetWidth - 5;
if(LeftPos < 5)
{
LeftPos = 5;
this.divSearch.style.width = getInsideWindowWidth() - 10;
}
}

if(BottomPos>getInsideWindowHeight())
{
this.divSearch.style.height = getInsideWindowHeight() - TopPos - 5;
}

this.divSearch.style.left = LeftPos + 'px';
this.divSearch.style.top = TopPos + 'px';
this.PrepareDDLs(true);
if(this.tblResult)
{
ClearCells(this.tblResult);
}
}

this.HidePanel = function ()
{
this.Opened = false;
RemoveDocumentListeners(this);
this.divSearch.style.display='none';
}

this.SelectedItem = function (value,text)
{
this.txtValue.value = value;
this.txtSearch.value = text;
this.strSearch = text;
//this.txtSearch.className = 'ValidSearchTextBox_' + this.CurrentClass;
this.ManuelFocus = true;
this.txtSearch.focus();
this.StopSearch();
}

this.AjaxSearchCombo_MouseDown = function (e)
{
if(this.Opened)
{
var obj,len,el,i;

if(!e && event) e = event;

if(e.target) el = e.target;
else el = e.srcElement;

var elcl = (el.id) ? el.id : (el.parentNode!=null &&  el.parentNode.id) ? el.parentNode.id : "";

if(this.AllowHide(elcl))
{
this.StopSearch();
}
}
}

this.AjaxSearchCombo_InFocus = function (e)
{
if(this.Opened)
{
var obj,len,el,i;

if(!e && event) e = event;

if(e.target) el = e.target;
else el = e.srcElement;

var elcl = (el.id) ? el.id : (el.parentNode!=null && el.parentNode.id) ? el.parentNode.id : "";
if(this.AllowHide(elcl))
{
this.StopSearch();
}
}
}

this.PrepareDDLs = function(p_Hidden)
{
var DDls=document.getElementsByTagName('SELECT');
for(i=0;i<DDls.length;i++)
{
DDls[i].style.visibility = p_Hidden?"hidden":"visible";
}
}

}//End Class **********************************************************************

function AddDocumentListeners(pAjaxSearchCombo)
{
var AjaxSearchCombo_MouseDownfnc = function (evnt){ return pAjaxSearchCombo.AjaxSearchCombo_MouseDown(evnt);}
var AjaxSearchCombo_InFocusfnc = function (evnt){ return pAjaxSearchCombo.AjaxSearchCombo_InFocus(evnt);}
if(document.attachEvent)
{ // IE
document.attachEvent('onmousedown', AjaxSearchCombo_MouseDownfnc);
document.attachEvent('onfocusin', pAjaxSearchCombo.AjaxSearchCombo_InFocus);
}
else 
{ // Mozilla, Netscape, Firefox
window.addEventListener('mousedown', AjaxSearchCombo_MouseDownfnc, true);
window.addEventListener('focus', AjaxSearchCombo_InFocusfnc, true);
}
}

function RemoveDocumentListeners(pAjaxSearchCombo)
{
var AjaxSearchCombo_MouseDownfnc = function (evnt){ return pAjaxSearchCombo.AjaxSearchCombo_MouseDown(evnt);}
var AjaxSearchCombo_InFocusfnc = function (evnt){ return pAjaxSearchCombo.AjaxSearchCombo_InFocus(evnt);}
if(document.detachEvent)
{ // IE
document.detachEvent('onmousedown', AjaxSearchCombo_MouseDownfnc);
document.detachEvent('onfocusin', pAjaxSearchCombo.AjaxSearchCombo_InFocus);
}
else
{ // Mozilla, Netscape, Firefox
window.removeEventListener('mousedown', AjaxSearchCombo_MouseDownfnc, true);
window.removeEventListener('focus', AjaxSearchCombo_InFocusfnc, true);
}
}

function AddListeners(pAjaxSearchCombo)
{
    var HandleArrowKeysfnc = function (evnt){ return pAjaxSearchCombo.HandleArrowKeys(evnt);}
    var StartSearchfnc = function (evnt) { return pAjaxSearchCombo.StartSearch(evnt); }
    if (pAjaxSearchCombo.divSearch) {
        if (document.attachEvent) { // IE
            pAjaxSearchCombo.divSearch.style.overflowX = 'hidden';
            pAjaxSearchCombo.divSearch.style.overflowY = 'auto';
            pAjaxSearchCombo.txtSearch.attachEvent('onkeyup', function (evnt) { return StartSearchfnc(evnt) });
            pAjaxSearchCombo.txtSearch.attachEvent('onkeydown', function (evnt) { return HandleArrowKeysfnc(evnt) });
            pAjaxSearchCombo.divSearch.attachEvent('onkeydown', function (evnt) { return HandleArrowKeysfnc(evnt) });
            pAjaxSearchCombo.txtSearch.autocomplete = 'off';
        }
        else { // Mozilla, Netscape, Firefox
            pAjaxSearchCombo.divSearch.style.overflow = 'auto';
            pAjaxSearchCombo.txtSearch.addEventListener('keyup', StartSearchfnc, true);
            pAjaxSearchCombo.txtSearch.addEventListener('keypress', HandleArrowKeysfnc, true);
            pAjaxSearchCombo.divSearch.addEventListener('keypress', HandleArrowKeysfnc, true);
            pAjaxSearchCombo.txtSearch.setAttribute("autocomplete", "off");
        }
    }
}

var CurHighLight='';
var OldStyle='';
function tr_OnMouseOut(obj)
{
var tableObj;
if(obj.parentElement)
{//IE
tableObj = obj.parentElement.parentElement;
}
else
{//Firefox
tableObj = obj.parentNode.parentNode;
}

//ClearCells(tableObj);
if (tableObj.HighLightRow)
{
tableObj.HighLightRow.className = tableObj.HighLightRowOldStyle;
}
}

function tr_OnMouseOver(obj)
{
var tableObj;
if(obj.parentElement)
{//IE
tableObj = obj.parentElement.parentElement;
}
else
{//Firefox
tableObj = obj.parentNode.parentNode;
}

if (tableObj.RowIndex != null)
{
tableObj.RowIndex = null;
ClearCells(tableObj);
}

ClassPreFix = tableObj.className.substr(0,tableObj.className.indexOf('_'));
//ClearCells(tableObj);
CurHighLight = obj;
tableObj.HighLightRow = obj;
tableObj.HighLightRowOldStyle = obj.className;
obj.className = ClassPreFix + "_HighLight";
}

function ClearCells(tableObj)
{
if (tableObj != null)
{
ClassPreFix = tableObj.className.substr(0,tableObj.className.indexOf('_'));
for(i=0;i<tableObj.rows.length;i++)
{
tableObj.rows[i].className = (i % 2==0)? ClassPreFix + "_ItemStyle":ClassPreFix + "_AlternatingItemStyle";
}
}
}

function getInsideWindowWidth() {
if (window.innerWidth) {
return window.innerWidth;
} else if (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) {
return document.body.parentElement.clientWidth;
} else if (document.body && document.body.clientWidth) {
return document.body.clientWidth;
}
return 0;
}

function getInsideWindowHeight( ) {
if (window.innerHeight) {
return window.innerHeight;
} else if (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) {
return document.body.parentElement.clientHeight;
} else if (document.body && document.body.clientHeight) {
return document.body.clientHeight;
}
return 0;
}

function getX(obj){return obj.offsetLeft + (obj.offsetParent ? getX(obj.offsetParent) : obj.x ? obj.x : 0); }

function getY(obj){return (obj.offsetParent ? obj.offsetTop + getY(obj.offsetParent) : obj.y ? obj.y : 0); }

function findPos(obj) 
{
//var curleft = curtop = 0;
//if (obj.offsetLeft) 
//{
//curleft = obj.offsetLeft;
//curtop = obj.offsetTop;
//while (obj = obj.offsetParent) 
//{
//curleft += obj.offsetLeft;
//curtop += obj.offsetTop;
//}
//}

    return [getX(obj),getY(obj)];
}

function LoadingInDiv(divObjId, SplashScreenId, ImagePath)
{
var HTML;
var divObj = document.getElementById(divObjId);

try
{

if(window.frames[SplashScreenId])
{  
HTML=window.frames[SplashScreenId].document.body.innerHTML;
}
else if (window.parent.frames[SplashScreenId])
{  
HTML=window.parent.frames[SplashScreenId].document.body.innerHTML;
}
else
{
return;
}
}
catch(err)
{
return;
} 


while((HTML.indexOf('{imagepath}/')>=0) || (HTML.indexOf('{imagepath}')>=0))
{
HTML=HTML.replace('{imagepath}/',ImagePath+'/');
HTML=HTML.replace('{imagepath}',ImagePath+'/');
}

divObj.innerHTML = HTML;
}