//TextBox Validation javascript File 
//TextBox Validation javascript File 
function isNumberKey(evt)
{
 	
 	var charCode = document.all?event.keyCode:evt.charCode;
    //var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode >= 48 && charCode <= 57) || (charCode==46) || (charCode==0))
    {
         return true;
    }
    else
    {
         return false;
    }
     
} 
function isNotDot(evt)
{
 	
 	var charCode = document.all?event.keyCode:evt.charCode;
    //var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode==46)
    {
         return false;
    }
    else
    {
         return true;
    }
     
} 
function isNumberKeyGrd(evt)
{
 	
    var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode >= 48 && charCode <= 57))
    {
            return true;
          //document.getElementById("flag").value=1;
          //document.getElementById("value").value=charCode;
          //document.getElementById("from1").submit();
    }
    else
    {
         return false;
    }
     
} 
window.onload= function(){
        
      var o=document.getElementById("txtSearch"); 
       if(o != null)
       {       
            var r = o.createTextRange(); 
            r.collapse(false);//r.collapse(true); //to get the cursor at the left edge 
            r.select(); 
       }   
}

//function window.onload() 
//{ 
//       var o=document.all("txtSearch"); 
//       if(o != null)
//       {
//            var r = o.createTextRange(); 
//            r.collapse(false);//r.collapse(true); //to get the cursor at the left edge 
//            r.select(); 
//       }
//}
function isLetterKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode >=65  && charCode <= 91) || (charCode >=97 && charCode <=122) || (charCode==32)|| (charCode==45) ||(charCode==46))
    {
        return true;
    }
    else
    {
          return false;
    }
}
function isLetterKeyGrd(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode >=65  && charCode <= 91) || (charCode >=97 && charCode <=122) || (charCode==32)|| (charCode==45) || (charCode==46) || (charCode >= 48 && charCode <= 57) || (charCode==13)|| (charCode==47) || (charCode ==8))
    {              
          document.getElementById("flag").value=1;
       
          document.getElementById("value").value=charCode;
       
          //from11.submit();
          document.getElementById("form1").submit();
    }
    else
    {
          return false;
    }
}
function leftclick(e)
{ 
    if (document.all)
    { 
        if(event.button == 1) 
        { 
            document.getElementById("transvalue").value=1; 
        }
    }
}

//Javascript function to calculate column value and display sum on gridfooter 
//Total will be displayed in double format
function CalculateDoubleSum(grd, targetControl, controlColumnIndex)
{
    var grid = document.getElementById(grd);
    if (grid == null) return;
    var total = 0;
    for (var intCount=1; intCount<grid.rows.length-1;intCount++)
    {
        var val = 0;
        //if grid cell has label
        var contr = grid.rows[intCount].cells[controlColumnIndex].getElementsByTagName('span')[0];
        if(contr == null)
        {
            //if grid cell has textbox
            contr = grid.rows[intCount].cells[controlColumnIndex].getElementsByTagName('input')[0];
            if(contr == null)
            {   
                //grid cell contains value 
                if(grid.rows[intCount].cells[controlColumnIndex].value != "")
                {
                    grid.rows[intCount].cells[controlColumnIndex].value = parseFloat(grid.rows[intCount].cells[controlColumnIndex].value).toFixed(2);
                    val = parseFloat(grid.rows[intCount].cells[controlColumnIndex].value);
                }
                else
                {
                    grid.rows[intCount].cells[controlColumnIndex].value != "0.00";
                }
            }
            else
            {
                //input
                if(contr.value != "")
                {
                    contr.value = parseFloat(contr.value).toFixed(2);
                    val = parseFloat(contr.value);    
                }
                else
                {
                    contr.value = "0.00";
                }
            }
        }
        else
        {
            //Label
            if(contr.innerHTML != "")
            {
                contr.innerHTML = parseFloat(contr.innerHTML).toFixed(2);
                val = parseFloat(contr.innerHTML);
            }    
            else
            {
                contr.innerHTML = "0.00";
            }
        }
    
        total += parseFloat(val);
    }
    if(document.getElementById(targetControl).type == 'text')
    {
        document.getElementById(targetControl).value = total.toFixed(2);
    }
    else
    {
        document.getElementById(targetControl).innerHTML = total.toFixed(2);
    }
}

//Javascript function to calculate and display sum on gridfooter
//Total will be displayed in integer format 
function CalculateIntegerSum(grd, targetControl, controlColumnIndex)
{   
    var grid = document.getElementById(grd);
    if (grid == null) return;
    var total = 0;
    for (var intCount=1; intCount<grid.rows.length-1;intCount++)
    {
        var val = 0;
        //if grid cell has label
        var contr = grid.rows[intCount].cells[controlColumnIndex].getElementsByTagName('span')[0];
        if(contr == null)
        {
            //if grid cell has textbox
            contr = grid.rows[intCount].cells[controlColumnIndex].getElementsByTagName('input')[0];
            if(contr == null)
            {   
                //grid cell contains value 
                if(grid.rows[intCount].cells[controlColumnIndex].value != "")
                {
                    val = parseInt(grid.rows[intCount].cells[controlColumnIndex].value);
                }
            }
            else
            {
                //input
                if(contr.value != "")
                {
                    val = parseInt(contr.value);    
                }
                else
                {
                    contr.value = "0";
                }
            }
        }
        else
        {
            //Label
            if(contr.innerHTML)
            {
                val = parseInt(contr.innerHTML);
            }
            else
            {
                contr.innerHTML = "0";
            }    
        }
    
        total += parseInt(val);
    }
    
    if(document.getElementById(targetControl).type == 'text')
    {
        //grid footer cell has textbox
        document.getElementById(targetControl).value = parseInt(total);
    }
    else
    {
        //if grid footer cell has label
        document.getElementById(targetControl).innerHTML = parseInt(total);
    } 
}

// Calculate Amount by Qty * Rate formaula   
function MultiplyQtyAndRate(txtQty,txtRate,txtAmount)
{
    var Qty = 0;
    var Rate = 0;
    if(document.getElementById(txtQty).value != "")
    {
        Qty = parseInt(document.getElementById(txtQty).value);
    }
    else
    {
        document.getElementById(txtQty).value = "0";
    }
    if(document.getElementById(txtRate).value != "")
    {
        document.getElementById(txtRate).value = parseFloat(document.getElementById(txtRate).value).toFixed(2);
        Rate = parseFloat(document.getElementById(txtRate).value);
    }
    else
    {
        document.getElementById(txtRate).value = "0.00";
    }
    
    var Amount = Qty * Rate;
    if(document.getElementById(txtAmount).type == 'text')
    {
        document.getElementById(txtAmount).value = parseFloat(Amount).toFixed(2);
    }
    else
    {
        document.getElementById(txtAmount).innerHTML = parseFloat(Amount).toFixed(2);
    }
}

/* Functions for maintaining the scroll positions of the divs on the page after postback */
//Two hidden fields for each div whose position is to be maintained 
//if div's id is 'div1' then hidden fields should be 'hidLeft_div1' and 'hidTop_div1' strictly 
//else this function won't work    
//then call this function maintainScrollPositions(); on each page at the bottom
//and write this line in the page_load event in the .aspx.cs file:
/*
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "script", "reStorePositions();", true);
*/
function maintainScrollPositions()
{
    storePositions();
    if (!window.onload)
        window.onload = storePositions;
    if (!window.onscroll)    
        window.onscroll = storePositions;
    if (!window.onclick)    
        window.onclick = reStorePositions;
    if (!window.onkeypress)    
        window.onkeypress = reStorePositions;
}
function storePositions()
{
    var arrDiv = new Array();
    arrDiv = document.getElementsByTagName("div");
    for(var i=0; i<arrDiv.length;i++)
    {
       if (arrDiv[i].id)
       {
            var intLeft = arrDiv[i].scrollLeft;
            var intTop = arrDiv[i].scrollTop;
            var objLeft = document.getElementById('hidLeft_' + arrDiv[i].id);
            var objTop = document.getElementById('hidTop_' + arrDiv[i].id);
            if (objLeft != null && objTop != null)
            {
                objLeft.value=intLeft;
                objTop.value = intTop;
            }
       }         
    }
}

function reStorePositions()
{
    //To work around maintain a scroll postition, Div must assign a id
    var arrDiv = new Array();
    arrDiv = document.getElementsByTagName("div");
    for(var i=0; i<arrDiv.length;i++)
    {
       if (arrDiv[i].id)
       {
            var objLeft = document.getElementById('hidLeft_' + arrDiv[i].id);
            var objTop = document.getElementById('hidTop_' + arrDiv[i].id);
            if (objLeft != null && objTop != null)
            {
                var intLeft = objLeft.value;
                var intTop = objTop.value;
                window.scrollTo(0,0);
                arrDiv[i].scrollLeft=intLeft;
                arrDiv[i].scrollTop=intTop;
            }
       }         
    }
}

function GridHeaderClick(MainCheckBoxID, grdViewID, TargetCheckBox)
{
    //Get target base & child control.
    var TargetBaseControl = document.getElementById(grdViewID);
    var MainCheckBox = document.getElementById(MainCheckBoxID);
    
    //Get all the control of the type INPUT in the base control.
    var Inputs = TargetBaseControl.getElementsByTagName("input");
    
    //Checked/Unchecked all the checkBoxes in side the GridView.
    for(var n = 0; n < Inputs.length; ++n)
        if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetCheckBox,0) >= 0)
             Inputs[n].checked = MainCheckBox.checked;
    //Reset Counter
    //Counter = CheckBox.checked ? TotalChkBx : 0;
}
 function DateFun(id) 
            {
	            var my = id;
	
	            if(my.value.length == 2 || my.value.length == 5)
                {
                my.value=my.value+"/";
                } 
            }
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
function chkDoneInAll_Click(blnChecked,grid)
{
    if (grid!= null && grid.rows.length>0)
    {
        for (var intRows =0;intRows<grid.rows.length;intRows++)
        {
            var arrInput = grid.rows[intRows].cells[0].getElementsByTagName('input');
            for(var i=0; i< arrInput.length;i++)
            {
                if(arrInput[i].type.toLowerCase() == 'checkbox')
                {
                    arrInput[i].checked = blnChecked;
                }
            }
        }
    }
}
function fnRowClickGroup(objChk,grid)
{

    var objHdrChk;
    var blnIsCheck = true;
    if (grid!= null && grid.rows.length>0)
    {
        var arrInput = grid.rows[0].getElementsByTagName('input');
	    for(var i=0; i< arrInput.length;i++)
        {
            if(arrInput[i].type.toLowerCase() == 'checkbox')
            {
                objHdrChk = arrInput[i];
            }
        }
        for(var intRows=1; intRows< grid.rows.length;intRows++)
        {
            var arrInputChild = grid.rows[intRows].cells[0].getElementsByTagName('input');
            var arrChilds = grid.rows[intRows].getElementsByTagName('table');
            if (arrChilds.length > 1)
            {
                    var arrChkBoxes = arrChilds[1].getElementsByTagName('input');
                    
                    for(var intChilds = 0; intChilds<arrChkBoxes.length; intChilds++)
                    {
                        if(arrChkBoxes[intChilds].type=='checkbox')
                        {
                            arrChkBoxes[intChilds].checked = objChk;
                        }
                    }
            }
            
            for(var intC = 0; intC<arrInputChild.length; intC++)
            {
                if(arrInputChild[intC].type.toLowerCase() == 'checkbox')
                {
                    if(!arrInputChild[intC].checked)
                    {
                        blnIsCheck = false;
                        objHdrChk.checked = blnIsCheck;
                        //return;
                    }
                }
            }
        }
        objHdrChk.checked = blnIsCheck;
    }
}

function chkAllGrid(gridId)
    {
        var objHdrChk;
        var blnIsCheck = true;
        var grid = document.getElementById(gridId);
        if (grid!= null && grid.rows.length>0)
        {
            var arrInput = grid.rows[0].getElementsByTagName('input');
	        for(var i=0; i< arrInput.length;i++)
            {
                if(arrInput[i].type.toLowerCase() == 'checkbox')
                {
                    objHdrChk = arrInput[i];
                }
            }
            for(var intRows=1; intRows< grid.rows.length;intRows++)
            {
                var arrInputChild = grid.rows[intRows].cells[0].getElementsByTagName('input');
                for(var intC = 0; intC<arrInputChild.length; intC++)
                {
                    if(arrInputChild[intC].type.toLowerCase() == 'checkbox')
                    {
                        if(!arrInputChild[intC].checked)
                        {
                            blnIsCheck = false;
                            objHdrChk.checked = blnIsCheck;
                            return;
                        }
                    }
                }
            }
            objHdrChk.checked = blnIsCheck;
        }
    }
