var objInterval;
var objModalUpdateProgress = null;
var arrUpdatePanelIDs = null;
var objSrcElement = null;
var strUpdatePanelID = "";
var objSelectedTD = null;
var intTimer = -1;
var objNavigatePopUp = null;
var htWorkingDivs = new HashTable();

window.history.forward(-1);

Sys.Application.add_load(onLoad);


if (window.addEventListener) //DOM method for binding an event
window.document.addEventListener("click", fnOnClickHandler, false);
else if (window.attachEvent) //IE exclusive method for binding an event
window.document.attachEvent("onclick", fnOnClickHandler);

//window.document.attachEvent("onclick", fnOnClickHandler);
//window.onresize = fnArrageLayout;
function fnApplyTheme()
{    
//  window.setTimeout(function(){
//    fnReloadControl("ctl00_upThemes","updatetheme");
//  },1000);
//  return false;
}

function fnCheckForSessionOut()
{
   if($get('ctl00_hdntxtTimeOut') != null)
         intTimer = window.setTimeout("window.location='../ErrorPage.aspx?msg=timeout';",$get('ctl00_hdntxtTimeOut').value);
   
}

function onLoad()
{

    Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(EndRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
 //   fnArrageLayout();
}

function fnArrageLayout()
{
    var intScreenHeight = document.body.clientHeight;
    var intScreenWidth = document.body.clientWidth;
    if($get('contentArea'))
    {

        if($get('pagecontent'))
        {

            var objPageContentRect = $get('pagecontent').getBoundingClientRect();
            
              if((objPageContentRect.bottom - objPageContentRect.top) < (intScreenHeight ))
            {
            
                $get('pagecontent').style.width = intScreenWidth - 14;
                $get('pagecontent').style.height = intScreenHeight - 91;
            }
            else
            {
              
                $get('pagecontent').style.width = intScreenWidth - 34;
                $get('pagecontent').style.height = intScreenHeight - 91;                
            }
        }
        $get('contentArea').style.width = intScreenWidth-8;
        $get('contentArea').style.height = intScreenHeight - 85;
    }
 window.setTimeout(function(){fnArrageLayout();},500);
}

function BeginRequestHandler(sender, args)
{
    $get("UtilIFrame").src = "about:blank";
    window.clearTimeout(intTimer);
    fnCheckForSessionOut();     
    if (objSrcElement != null)
    {
        if(objSrcElement.type)
        {
            switch(objSrcElement.type)
            {
                case "Filter":
              
                    var objElement = objSrcElement.offsetParent.offsetParent;
                    var objLocation = new Object();
                    objLocation.ThisId = objSrcElement.ThisId;
                    //
                    if (document.getElementById && !document.all)  //For Mozilla
                        objLocation.Rect= objSrcElement.getBoxObjectFor(objSrcElement.documentElement);
                     else
                        objLocation.Rect =  objSrcElement.getBoundingClientRect();
   
                    
                    //objLocation.Rect =  objSrcElement.getBoundingClientRect();
                    objLocation.Left = objElement.offsetLeft + 2;
                    objLocation.Top = objElement.offsetTop + 21; 
                    objSrcElement.Location = objLocation;
                    break;
                case "GridCell":
                    if(objSrcElement.parentElement)
                    {
                        objSrcElement.GridID = objSrcElement.offsetParent.id;
                        objSrcElement.ScrollPosition = new Object();
                        objSrcElement.ScrollPosition.scrollTop = objSrcElement.offsetParent.parentElement.scrollTop;
                        objSrcElement.ScrollPosition.scrollLeft = objSrcElement.offsetParent.parentElement.scrollLeft;
                        //objSrcElement.ScrollPosition = objSrcElement.scrollTop;
                    }
                    break;
            }
        }
        
       
    }
  
    var strPostBackPanelID = sender._uniqueIDToClientID(sender._postBackSettings.panelID.split('|')[0]);
    if (window.fnBeforePostBack)
    {
       fnBeforePostBack(objSrcElement, strPostBackPanelID);
    }    
    //Stop for mozilla
    fnShowWorkingPopup(strPostBackPanelID);
}

function fnAdjustWokingDiv(strID)
{

    //var objPos = $get(strID).childNodes[0].childNodes[0].getBoundingClientRect();
     if (document.getElementById && !document.all)  //For Mozilla
        var objPos = $get(strID).ownerDocument.getBoxObjectFor($get(strID).ownerDocument.documentElement);
     else
        var objPos = $get(strID).getBoundingClientRect();
   
   // var objPos = $get(strID).getBoundingClientRect();
    var objWorkingIframe = $get(strID + "_WorkingIframe");
    var objWorkingDiv = $get(strID + "_WorkingDiv");
    
    objWorkingIframe.style.top = objPos.top + document.documentElement.scrollTop;
    objWorkingDiv.style.left = objPos.left + (((objPos.right - objPos.left)/2) - 50) + document.documentElement.scrollLeft;
    objWorkingIframe.style.left = objPos.left + document.documentElement.scrollLeft;
    objWorkingIframe.style.width = (objPos.right - objPos.left);
    objWorkingIframe.style.height = (objPos.bottom - objPos.top);
    objWorkingDiv.style.top = ((parseInt(objWorkingIframe.style.height) / 2) - 15) + parseInt(objWorkingIframe.style.top);
    
    objPos = null;
    objWorkingIframe = null;
    objWorkingDiv = null;
}

function fnShowWorkingPopup(strID)
{
    var objUtilIFrame = $get("UtilIFrame");
    var objWorkingIframe = objUtilIFrame.cloneNode(true);
    var objDummyDiv = $get("DummyDiv");
    var objWorkingDiv = objDummyDiv.cloneNode(true);
    
    objWorkingDiv.id = strID + "_WorkingDiv";
    objWorkingIframe.id = strID + "_WorkingIframe";
    objWorkingDiv.style.height = "";
    objWorkingDiv.style.width = "";
    objWorkingDiv.innerHTML = "<b>Working...</b>";
    objWorkingDiv.style.zIndex = "999996";
    objWorkingDiv.className = "WorkingDiv";
    objWorkingIframe.style.zIndex = "999995";
    objWorkingIframe.className = "WorkingIframe";
    
  //  var objPos = $get(strID).childNodes[0].childNodes[0].getBoundingClientRect();
   
     if (document.getElementById && !document.all)  //For Mozilla
        var objPos = $get(strID).ownerDocument.getBoxObjectFor($get(strID).ownerDocument.documentElement);
     else
        var objPos = $get(strID).getBoundingClientRect();
   
   //if ($get(strID).ownerDocument.getBoxObjectFor()
   //     var objPos = $get(strID).ownerDocument.getBoxObjectFor($get(strID).ownerDocument.documentElement);
  // else
    //     var objPos = $get(strID).getBoundingClientRect();
   
    var htWorkingDiv = new HashTable();
    htWorkingDiv.add('Position' , objPos);
    var intTimerID = window.setInterval("fnAdjustWokingDiv('" + strID + "');" , 200);
    htWorkingDiv.add('TimerID' , intTimerID);
    htWorkingDivs.add(strID , htWorkingDiv);
    
    objWorkingIframe.style.top = objPos.top + document.documentElement.scrollTop;
    objWorkingDiv.style.left = objPos.left + (((objPos.right - objPos.left)/2) - 50) + document.documentElement.scrollLeft;
    objWorkingIframe.style.left = objPos.left + document.documentElement.scrollLeft;
    objWorkingIframe.style.width = (objPos.right - objPos.left);
    objWorkingIframe.style.height = (objPos.bottom - objPos.top);
    objWorkingDiv.style.top = ((parseInt(objWorkingIframe.style.height) / 2) - 15) + parseInt(objWorkingIframe.style.top);
    objWorkingIframe.style.display = "inline";
    objWorkingDiv.style.display = "inline";
    document.body.appendChild(objWorkingDiv);
    document.body.appendChild(objWorkingIframe);
    
    objUtilIFrame = null;
    objWorkingIframe = null;
    objDummyDiv = null;
    objWorkingDiv = null;
    objPos = null;
    intTimerID = null;
    htWorkingDiv = null;
}

function fnHideWorkingPopup(strID)
{
    var objWorkingDiv = $get(strID + "_WorkingDiv");
    var objWorkingIframe = $get(strID + "_WorkingIframe");
    if(objWorkingDiv != null)
    {
        document.body.removeChild(objWorkingDiv);
    }
    if(objWorkingIframe != null)
    {
        document.body.removeChild(objWorkingIframe);
    }
    if(htWorkingDivs.get(strID) != null)
    {
        window.clearInterval(htWorkingDivs.get(strID).get('TimerID'));
    }
    htWorkingDivs.remove(strID);
    objWorkingDiv = null;
    objWorkingIframe = null;
}

function EndRequestHandler(sender, args)
{
    if(sender._panelsToRefreshIDs != null)
    {
         var strPostBackPanelID = sender._uniqueIDToClientID(sender._panelsToRefreshIDs[0]);
        fnHideWorkingPopup(strPostBackPanelID);
        if (objSrcElement)
        {
            if (objSrcElement.type)
            {
                switch(objSrcElement.type)
                {
                    case "Filter":
                        var objData = objSrcElement.Location;
                        if (objData)
                        {
                            fnInitializeFilterLOV(objData);
                            $get("DummyDiv").focus();	
                        }
                        objSrcElement = null;
                        break;
                    case "GridCell":
                        if(objSrcElement.ScrollPosition)
                        {
                            eval(objSrcElement.GridID).parentElement.scrollTop = objSrcElement.ScrollPosition.scrollTop;
                            eval(objSrcElement.GridID).parentElement.scrollLeft = objSrcElement.ScrollPosition.scrollLeft;
                        }
                        break;
                }
            }
            
            if(objSrcElement.id == "ctl00_ddlThemes")
            {
                window.location = location.href;
            }
        } 
        if (window.fnAfterPostBack)
        {
            fnAfterPostBack(objSrcElement, strPostBackPanelID);
        }
        fnSetFocusOnEditedRow(); 
    }   
    
}

function fnOnClickHandler(eve)
{
   if (window.attachEvent)
   objSrcElement = event.srcElement;
   else
   objSrcElement = eve.target;
}


function fnInitializeModalUpdateProgress()
{
    objModalUpdateProgress = new AjaxControlToolkit.ModalPopupBehavior($get("progressArea"));
    objModalUpdateProgress.set_BackgroundCssClass('modalBackground');
    objModalUpdateProgress.set_PopupControlID('progressArea');       
    objModalUpdateProgress.initialize();
}

function fnDisposeModalUpdateProgress()
{
    objModalUpdateProgress.dispose();
    objModalUpdateProgress = null;
}

function fnSetFocus(strElementID)
{
    objInterval = window.setInterval("$get('"+strElementID+"').focus();",100);
}

function fnRemoveFocus()
{
    window.clearInterval(objInterval);
}

function fnReloadControl(strUpdatePanelId, strEventArgument)
{

    Sys.WebForms.PageRequestManager.getInstance()._doPostBack(strUpdatePanelId, strEventArgument);
}
function fnExportToExcel(strParam,strMode)
{ 
 
    if(strMode=="ExportToExcel")
    {
        var gridObj = $get(strParam);   
        if(gridObj == null || $get(strParam+"_GridBar").outerText.indexOf("(0)") != -1)
        {
            alert("There are no records to export");
            return false;
        }
        if(confirm("Export Data to Excel?")==false)
        {
            return false;
        }
    }
    
    $get("UtilIFrame").style.display = "inline";
    $get("UtilIFrame").style.width = 0;
    $get("UtilIFrame").style.height = 0;
    $get("UtilIFrame").src = "about:blank";
    if(location.href.indexOf('?') > -1)
    {
        $get("UtilIFrame").src = location.href.replace('#','') + "&Mode=" + strMode + "&Param=" + strParam;
    }
    else
    {
        $get("UtilIFrame").src = location.href.replace('#','') + "?Mode=" + strMode + "&Param=" + strParam;
    }
    return false;
}

function fnPrint(strParam)
{
  
    var gridObj = $get(strParam);   
        if(gridObj == null || $get(strParam+"_GridBar").outerText.indexOf("(0)") != -1)
        {
            alert("There are no records to export");
            return false;
        }
    $get("UtilIFrame").style.display = "inline";
    $get("UtilIFrame").style.width = 0;
    $get("UtilIFrame").style.height = 0;
    $get("UtilIFrame").src = "about:blank";
    if(location.href.indexOf('?') > -1)
    {
        $get("UtilIFrame").src = location.href.replace('#','') + "&Mode=Print&Param=" + strParam;
    }
    else
    {
        $get("UtilIFrame").src = location.href.replace('#','') + "?Mode=Print&Param=" + strParam;
    }
    return false;
}

function fnMakePrint()
{
    if(document.title.toLowerCase() ==	"export to ccdb")
    {
        if($get("ctl00_hdntxtPrint").value == 1){
            $get("ctl00_hdntxtPrint").value = 0;
            window.frames[0].focus();window.frames[0].print();
        } 
    } 
    else
    {
        window.frames[0].focus();window.frames[0].print(); 
    }  
}

function fnSelectAll()
{
    if ($get("ctl00_ContentPlaceHolder1_grdCurrency_ctl01_chkSelectAll") != null)
    {
        if($get("ctl00_checkboxParentHidden").value == "parentChecked")
        {
            $get("ctl00_ContentPlaceHolder1_grdCurrency_ctl01_chkSelectAll").checked = true;
            fnSelectAllCheckboxes($get("ctl00_ContentPlaceHolder1_grdCurrency_ctl01_chkSelectAll"));
        }
        else if($get("ctl00_checkboxParentHidden").value == "parentNotChecked")
        {
            $get("ctl00_ContentPlaceHolder1_grdCurrency_ctl01_chkSelectAll").checked = false;
            fnSelectAllCheckboxes($get("ctl00_ContentPlaceHolder1_grdCurrency_ctl01_chkSelectAll"));
        }
    }
}

function fnSetFocusOnEditedRow()
{
    if (window.strSelectedTRID)
    {
        if (strSelectedTRID != "" && intSelectedCellIndex != -1)
        {
            objSelectedTD = $get(strSelectedTRID).cells[intSelectedCellIndex];
            if (objSelectedTD.childNodes.length > 0)
            {
                if (objSelectedTD.childNodes[0].type)
                {
                    for(intTDCntr = 0; intTDCntr < objSelectedTD.parentElement.cells.length; intTDCntr ++)
                    {
                        if(objSelectedTD.parentElement.cells[intTDCntr].childNodes.length > 0)
                        {
                            if(objSelectedTD.parentElement.cells[intTDCntr].childNodes[0].nodeName != "#text")
                            {
                                objSelectedTD.parentElement.cells[intTDCntr].childNodes[0].style.width = "98%";
                            }
                        }
                    }
                    window.setTimeout("objSelectedTD.childNodes[0].focus();",350,"javascript");
                    strSelectedTRID = "";
                }
            }
        }
    }
}

function fnCultureRelatedValue(strValue)
{
   var intIndexOf;
   
   intIndexOf = strValue.indexOf($get('ctl00_hdnGroupSeperator').value.trim());
   
   while(intIndexOf != -1)
   {
     strValue = strValue.replace($get('ctl00_hdnGroupSeperator').value.trim(),''); 
     intIndexOf = strValue.indexOf($get('ctl00_hdnGroupSeperator').value.trim());   
   }
   
   intIndexOf = strValue.indexOf($get('ctl00_hdnDecimalSeperator').value.trim());
   
   if($get('ctl00_hdnDecimalSeperator').value.trim() != ".")
   {
        while(intIndexOf != -1)
        {
             strValue = strValue.replace($get('ctl00_hdnDecimalSeperator').value.trim(),'.'); 
             intIndexOf = strValue.indexOf($get('ctl00_hdnDecimalSeperator').value.trim());   
        }
   }  
   
    return strValue; 
}

function fnAbortPostBack()
{
    var objInstances = Sys.WebForms.PageRequestManager.getInstance();
    if(objInstances != null)
        objInstances.abortPostBack();
}

function fnNavigatePage(pageurl)
{
  fnAbortPostBack();
  location.href=pageurl;
}

function fnNavigateTo(pageurl)
{
    fnAbortPostBack();
    objNavigatePopUp = new ModalPopUp();
    objNavigatePopUp.UniqueId = "NavigatePopUp";
    objNavigatePopUp.ForegroundElement = $get("divNavigate");
    objNavigatePopUp.Initialize();
    objNavigatePopUp.BackgroundElement.className = "NavigateIframe";
    objNavigatePopUp.Show();
    window.setTimeout('location.href ="' + pageurl + '";','500');
}

function fnOpenWindow(strWindowURL, strWindowName, intWindowHeight, intWindowWidth)
{
 var intLeft = (window.screen.width - intWindowWidth) / 2;
 var intTop = (window.screen.height - intWindowHeight) / 2;
 var objwindow = window.open(strWindowURL, strWindowName, 'Location=No,Menubar=No,Resizable=No,Scrollbars=Yes,Status=No,Toolbar=No,width=' + intWindowWidth + ',height=' + intWindowHeight + ',left=' + intLeft + ',top=' + intTop);
 objwindow.focus();
 return objwindow;
}

function fnSetGridWidth(strid)
{                  
 document.getElementById(strid.id).style.width = (screen.width/2) -30;
}

/* PopUp Functions*/
function onPopupLoad()
{

    Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(EndRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    fnArragePopupLayout();
}

function fnArragePopupLayout()
{
    var intScreenHeight = document.body.clientHeight;
    var intScreenWidth = document.body.clientWidth;
    if($get('popupwrapper'))
    {

        if($get('popupcontent'))
        {

            var objPageContentRect = $get('popupcontent').getBoundingClientRect();

              if((objPageContentRect.bottom - objPageContentRect.top) < (intScreenHeight ))
            {
        
                $get('popupcontent').style.width = intScreenWidth - 8;
                $get('popupcontent').style.height = intScreenHeight - 1;
         
            }
            else
            {
                $get('popupcontent').style.width = intScreenWidth - 14;
                $get('popupcontent').style.height = intScreenHeight - 1;                
            }
        }
        $get('popupwrapper').style.width = intScreenWidth-8;
        $get('popupwrapper').style.height = intScreenHeight-1;

    }
 window.setTimeout(function(){fnArragePopupLayout();},100);
}
