﻿//----------------------------------------------Common-----------------------------------------------------------------------
function HandleEnterPress(e)
{
    if(!e) e=window.event;
    var target = e.target || e.srcElement;
    if((e.keyCode==13)&&(target.tagName.toLowerCase()!='a')&&(target.tagName.toLowerCase()!='textarea'))
        return false;
}

function GenerateUniqueId()
{
    var d = new Date();
    return ""+parseInt(Math.random()*100000)+d.getHours()+d.getMinutes()+d.getMilliseconds();
}
//----------------------------------------------------------------------------------------------------------------------------


//--------------------------------------------Edit Form-----------------------------------------------------------------------
function AlignElement(element,sender,boundsElement)
{
    try
    {
        element.style.display="block";
        var bounds = Sys.UI.DomElement.getBounds(sender);
        var boundsPanel = Sys.UI.DomElement.getBounds(element);
        var y=bounds.y+bounds.height+1;
        bounds=Sys.UI.DomElement.getBounds(boundsElement);
        var x=bounds.x-parseInt(boundsPanel.width/2)+parseInt(bounds.width/2);
        Sys.UI.DomElement.setLocation(element,x,y);
        var ScrollTop = document.body.scrollTop;
        if (ScrollTop == 0)
        {
            if (window.pageYOffset)
                ScrollTop = window.pageYOffset;
            else
                ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
        var windowHeight;
        if (self.innerHeight) { // all except Explorer
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
           windowHeight = document.body.clientHeight;
        }
        
        boundsPanel = Sys.UI.DomElement.getBounds(element);
        if (boundsPanel.y+boundsPanel.height>ScrollTop+windowHeight)
            window.scrollTo(0,boundsPanel.y+boundsPanel.height-windowHeight+5);
    }
    catch(e){}
}
 
function HandleRowClick(e,sender)
{
    try
    {
        var target = e.target || e.srcElement;
        
        if((target.tagName.toLowerCase()!='a')&&(target.tagName.toLowerCase()!='img')&&(target.tagName.toLowerCase()!='input'))
        {
            if(sender.cells[parseInt(sender.getAttribute("ActiveCell"))].firstChild.click)
                sender.cells[parseInt(sender.getAttribute("ActiveCell"))].firstChild.click();
            else
                sender.cells[parseInt(sender.getAttribute("ActiveCell"))].childNodes[1].onclick();
        }
    }
    catch(e){}
}
//----------------------------------------------------------------------------------------------------------------------------
 
 
//--------------------------------------------------------Validators----------------------------------------------------------
function EnableAllValidators()
{
    for(i=0;i<Page_Validators.length;i++)
        ValidatorEnable(Page_Validators[i],true);
}

function DisableAllValidators()
{
    for(i=0;i<Page_Validators.length;i++)
        ValidatorEnable(Page_Validators[i],false);
}

function HideCurrentValidatorCallout()
{
    if(AjaxControlToolkit.ValidatorCalloutBehavior != null)
        if(AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout != null)
            AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();
}
//---------------------------------------------------------------------------------------------------------------------------- 
 
 
//--------------------------------------------------------Async Updates-------------------------------------------------------
var senderRow=null;
var senderElement=null;
var iAciveUpdates=0;
var prevState=new Object();

var nolaunch=false;

function FakeNolaunch()
{
    nolaunch=true;
    window.setTimeout("nolaunch=false",500);
}

function CheckUpdateInProgress()
{
    if ((iAciveUpdates!=0)&&(!nolaunch))
        return "\nOk to exit without update result, Cancel to wait.\n";
    nolaunch=false;
}

function SaveAsyncSenderAndRow(element,row)
{
    senderElement=element;
    senderRow=row;
}

function GetOperationInfo(prevState)
{
    var operationInfo=new Object();
    operationInfo.sender=senderElement;
    operationInfo.row=senderRow;
    operationInfo.prevState=prevState;
    operationInfo.message="";
    return operationInfo;
}

function StartAsyncProgress(operationInfo)
{
    iAciveUpdates++;    //Uvelichivaem schetchik tekuschih updates.
    DisableTableRow(operationInfo.row);     //Delaem disable vseh controls na row.
    window.onbeforeunload=CheckUpdateInProgress;
    if(document.all)
    {
        operationInfo.row.style.position="relative";
        operationInfo.row.style.top="-1px";
    }
    operationInfo.row.style.background="url(images/progress.png) repeat-x -1000px center";
    var index=GenerateUniqueId();
    operationInfo.row.id=index;
    operationInfo.interval=window.setInterval('$get("'+index+'").style.background="url(images/progress.png) repeat-x " +(parseInt($get("'+index+'").style.background.replace("url(images/progress.png) repeat-x ","").replace("transparent scroll ",""))+20) +"px center";',30);
}

function StopUpdate(operationInfo)
{
    EnableTableRow(operationInfo.row)
    operationInfo.row.style.background="";
    operationInfo.row.style.backgroundColor="";
    window.clearInterval(operationInfo.interval);
    iAciveUpdates--;
    
}

function ReopenEditForm(operationInfo)
{
    if(operationInfo.sender.click)
        operationInfo.sender.click();
    else
        operationInfo.sender.onclick();
}

function InsertClonedRowToTop(table)
{
    var row = table.rows[table.rows.length-1].cloneNode(true);
    row.style.display="";
    table.rows[0].parentNode.insertBefore(row,table.rows[1]);
    return row;
}

function DisableTableRow(row)
{
    for(i=0;i<row.cells.length;i++)
        for(j=0;j<row.cells[i].childNodes.length;j++)
        {
            var child=row.cells[i].childNodes[j];
            if(child.tagName&&((child.tagName.toLowerCase()=="a")||(child.tagName.toLowerCase()=="input")))
            {
                child.disabled=true;
                child.tempclick=child.onclick;
                child.onclick=new Function("return false");
            }
        }
}

function EnableTableRow(row)
{
    for(i=0;i<row.cells.length;i++)
        for(j=0;j<row.cells[i].childNodes.length;j++)
        {
            var child=row.cells[i].childNodes[j];
            if(child.tagName&&((child.tagName.toLowerCase()=="a")||(child.tagName.toLowerCase()=="input")))
            {
                child.disabled=false;
                if(child.tempclick)child.onclick=child.tempclick;
            }
        }
}

function RemoveRow(row)
{
    row.parentNode.removeChild(row);
}

function GetCellValue(tc)
{
    if(tc.children)
        if(tc.children.length>0)
            return tc.children[0].innerText;
        else
            return tc.innerText;
    else
        for(i=0;i<tc.childNodes.length;i++)
        {
            var child=tc.childNodes[i];
            if(child.tagName&&(child.tagName.toLowerCase()=="span"))
            {
                if(child.nodeValue)
                {
                    return child.nodeValue;
                }
                else
                {
                    var res="";
                    for(j=0;j<child.childNodes.length;j++)
                    {
                        var spanChild=child.childNodes[j];
                        if(spanChild.nodeType&&(spanChild.nodeType==3))
                            res+=spanChild.nodeValue;
                    }
                    return res;
                }
            }
        }
}
        
function SetCellValue(tc,value)
{
    if(tc.children)
        if(tc.children.length>0)
            tc.children[0].innerText=value;
        else
            return tc.innerText=value;
    else
        for(i=0;i<tc.childNodes.length;i++)
        {
            var child=tc.childNodes[i];
            if(child.tagName&&(child.tagName.toLowerCase()=="span"))
            {
                child.innerHTML=value;
            }
        }
}

function RefreshRowStyles(rows,rowClass,altRowClass)
{
    var ind=0;
    for (var i=1;i<rows.length;i++)
    {
      if (rows[i].style.display!='none')
      {
        ind++;
        if (ind%2==1)
          rows[i].className=rowClass;
        else 
          rows[i].className=altRowClass;      
      }  
    } 
}

 
 
