﻿// To select All grid checkbox

var columnLenth = 3;

function SelectAll(id, gridName)
{
    //get reference of GridView control
    var grid = document.getElementById(gridName);
    //variable to contain the cell of the grid
    var cell;
    
    if (grid.rows.length > 0)
    {
        //loop starts from 1. rows[0] points to the header.
        for (i=0; i<grid.rows.length; i++)
        {
            //get the reference of first column
            
            for(column = 0; column < columnLenth; column++)
            {
                cell = grid.rows[i].cells[column];
                
                //loop according to the number of childNodes in the cell
                for (j=0; j<cell.childNodes.length; j++)
                {           
                    //if childNode type is CheckBox                 
                    if (cell.childNodes[j].type =="checkbox")
                    {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        cell.childNodes[j].checked = document.getElementById(id).checked;
                    }
                }
             }
        }
    }
 }
 
 // To select or deselect check all option.
 function chkSelectAll(id, gridName)
 {
    var selectAllValue = true;
     var grid = document.getElementById(gridName);
    //variable to contain the cell of the grid
    var cell;
    
    if (grid.rows.length > 0)
    {
        //loop starts from 1. rows[0] points to the header.

        for (i=0; i<grid.rows.length; i++)
        {
            //get the reference of first column
            
            for(column = 0; column < columnLenth; column++)
            {
                if(!selectAllValue)
                {
                    break;
                }
                cell = grid.rows[i].cells[column];
                
                //loop according to the number of childNodes in the cell
                for (j=0; j<cell.childNodes.length; j++)
                {           
                    //if childNode type is CheckBox                 
                    if (cell.childNodes[j].type =="checkbox")
                    {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        if(! cell.childNodes[j].checked)
                        {
                            selectAllValue = false;
                            break;
                        }
                    }
                }
             }
        }
        document.getElementById(id).checked = selectAllValue;
    }
 }
 
 // For Text/SMS and Fax
function bindValue(ctrl)
{
    var chkName = "chk" + ctrl;
    var hdnName = "hdn" + ctrl + "Value";
    var txtName = "txt" + ctrl;
    
    var chkCtrl = document.getElementById(chkName);
    if(chkCtrl.checked)
    {
        var TextSMSValue = document.getElementById(hdnName).value;
        if(TextSMSValue.length >= 10)
        {
            var txt1 = TextSMSValue.substring(0,3);
            var txt2 = TextSMSValue.substring(3,6);
            var txt3 = TextSMSValue.substring(6,10);
            document.getElementById(txtName +"1").value = txt1;
            document.getElementById(txtName +"2").value = txt2;
            document.getElementById(txtName +"3").value = txt3;
        }
    }
    else
    {
        document.getElementById(txtName +"1").value = "";
        document.getElementById(txtName +"2").value = "";
        document.getElementById(txtName +"3").value = "";
    }
}
