﻿// JScript File
//Create Date 25/12/2009 by Ravinder Singh in Stellar Data Solutions (P) Ltd. http://www.stellarshell.com/
//*******************How call function in Html with Master Page*********************
//<script type="text/javascript">
//function validateFormOnSubmit(theForm) {
//var reason = "";
//    M_nofocus("No");
//      reason += validateEmpty(document.getElementById('<%=txtname.ClientID %>'),"You didn't enter name !");
//      reason += validateUsername(document.getElementById('<%=txtUsername.ClientID %>')");
//      reason += validatePassword(document.getElementById('<%=txtPassword.ClientID %>'));
//      reason += validateEmail(document.getElementById('<%=txtEmail.ClientID %>')");
//      reason += validatePhone(document.getElementById('<%=txtPhone.ClientID %>')");
//      reason += IsNumeric(document.getElementById('<%=txtnum.ClientID %>'),"enter only Number !");
//      reason += chekBox(document.getElementById('<%=chk.ClientID %>'),"test");
//     reason += CheckDrp(document.getElementById('<%=drp.ClientID %>'),"Select one !");
//  if (reason != "") {
//    alert("Some fields need correction:\n" + reason);
//    return false;
//  }
//  return true;
//}
////</script>
///***********************Call On Button onClientClick*************

//<asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="return validateFormOnSubmit('aspnetForm');" />


function checkall(grid,chk,cl)
{

      var ceklist=document.getElementById(chk);
      var ISCheck=ceklist.checked;
      var cell;
       if (grid.rows.length > 0)
            {
            if(ISCheck==1)
            {
              for(var x=1; x<grid.rows.length+1; x++)
                    {
                       cell = grid.rows[x].cells[cl];
                        //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 =1;
                            }
                        }
                    }
            }
            else if (ISCheck==0)
            {
             for(var x=1; x<grid.rows.length+1; x++)
                    {
                        cell = grid.rows[x].cells[cl];
                        //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 = 0;
                            }
                        }
                    }
               
            }
            else
            {
            }
            }
}
var _focus="No";
function M_nofocus(a)
{
_focus=a;
}
function setfocus(fld)
{
if(_focus=="No")
{
_focus="Yes"
fld.focus();
}

}
function validateEmpty(fld,_error) 
{
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#E2EFF8';
        if(_focus=="No")
        setfocus(fld);
        error = _error+".\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

//Vald for checkbox ischecked or not 
function chekBox(fld,_error) 
{ var error = "";
   
    if(fld.checked == 0)
    {
    error = _error+".\n"
    }
    else
    {
    error = "";
    }
   return error;   
}

//#################################Validation for Check enter only Numeric###################
function IsNumeric(fld,_error) //  check for valid numeric strings	
{  
 var error = "";
	    if(!/\D/.test(fld.value))
	    { 
	    error = "";//IF NUMBER
	    }
	    else if(/^\d+\.\d+$/.test(fld.value))
	    {
	    fld.style.background = '#E2EFF8';
        setfocus(fld);
	    error = _error+".\n"//IF A DECIMAL NUMBER HAVING AN INTEGER ON EITHER SIDE OF THE DOT(.)
	    }
	    else 
	    { 
	    fld.style.background = '#E2EFF8';
        setfocus(fld);
	    error = _error+".\n"
	    }
	    return error;   
}

//#################################//Validation for Check Drop Down Selected or not######################"Please select an Item."
function CheckDrp(fld,_error)
{
    var error = "";
    if(fld.selectedIndex==0)
    {
     setfocus(fld);
     fld.style.background = '#E2EFF8';
    error = _error+".\n"
    }
    else
    {
    error = "";
    }
   return error;   
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores// fld.value="field required"; 
 
    if (fld.value == "") {
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#E2EFF8'; 
         setfocus(fld);
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#E2EFF8'; 
        setfocus(fld);
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
        fld.value="";
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#E2EFF8';
         setfocus(fld);
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "The password is the wrong length. \n";
    } 
//    else if (illegalChars.test(fld.value))
//     {
//        setfocus(fld);
//        error = "The password contains illegal characters.\n";
//        fld.style.background = '#E2EFF8';
//    }
     else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "The password must contain at least one numeral.\n";
        
    } else {
        fld.style.background = 'White';
    }
   return error;
}  



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#E2EFF8';
        setfocus(fld);
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone(fld){
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        setfocus(fld);
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#E2EFF8';
    } else if (isNaN(parseInt(stripped))) {
        setfocus(fld);
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#E2EFF8';
    } else if (!(stripped.length == 11)) 
    {
        setfocus(fld);
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#E2EFF8';
    } 
    return error;
}



