//Blank field checking
function chk_blank(ctl,msg)
	{
		if(msg=="")
		 {
		  msg="This field cannot be blank";
		 }
		if (ctl.value=="")
		 {
			alert(msg);
			ctl.focus();
			return (false);
		 }
		else
		 return (true);
	}

/*-----------------------------------------------------*/
//Valid Email Checking
function isEmailAddr(email)
  {
   var result = false
   var theStr = new String(email)
   var index = theStr.indexOf("@");
   if (index > 0)
    {
     var pindex = theStr.indexOf(".",index);
     if ((pindex > index+1) && (theStr.length > pindex+1))
     result = true;
    }
   return result;
  }
// Calling function
function chk_email(ctl,msg)
 {
 	if(msg=="")
	 {
	  msg="Please enter a valid email address";
	 }

	if (!isEmailAddr(ctl.value))
	 {
		alert(msg);
		ctl.value="";
		ctl.focus();
		return (false);
	 }
	else
	 return (true);
 }
/*-----------------------------------------------------*/

// Numeric checking
function chk_numeric(ctl,msg)
 {
 	if(msg=="")
	 {
	  msg="Please enter valid numeric data";
	 }

	if (isNaN(ctl.value))
	 {
		alert(msg);
		ctl.value="";
		ctl.focus();
		return (false);
	 }
	else
	 return (true);
 }
/*-----------------------------------------------------*/

// Maxlength checking
function chk_maxlength(ctl,max,msg)
 {
 	if(msg=="")
	 {
	  msg="Please enter atmost"+ max +"characters";
	 }

	if (ctl.value.length > max)
	 {
		alert(msg);
		ctl.focus();
		return (false);
	 }
	return (true);
 }
/*-----------------------------------------------------*/



// Minimum Length checking
function chk_minlength(ctl,min,msg)
 {
 	if(msg=="")
	 {
	  msg="Please enter more than"+ max +"characters";
	 }

	if (ctl.value.length < min)
	 {
		alert(msg);
		ctl.focus();
		return (false);
	 }
	return (true);
 }
/*-----------------------------------------------------*/


// String checking
function chk_string(ctl,msg)
 {
  	if(msg=="")
	 {
	  msg="This field can never have numbers";
	 }

 	if (ctl.value!="")
	 {
		for(var i=0;i<9;i++)
		 {
			var pos=ctl.value.indexOf(i);
			if (pos!=-1)
			 {
				alert(msg);
				ctl.value="";
				ctl.focus();
				return (false);
			 }
		 }
	 }
	
	return (true);
}
/*-----------------------------------------------------*/
// Numeric checking
function chk_numeric(ctl,msg)
 {
 	if(msg=="")
	 {
	  msg="Please enter valid numeric data";
	 }

	if (isNaN(ctl.value))
	 {
		alert(msg);
		ctl.value="";
		ctl.focus();
		return (false);
	 }
	else
	 return (true);
 }
/*-----------------------------------------------------*/


//Equal Checking
function chk_equal(ctl1,ctl2,msg)
 {
   	if(msg=="")
	 {
	  msg="Value of these fields should be same";
	 }
	if(ctl1.value!=ctl2.value)
	 {
		alert(msg);
		ctl2.value="";
		ctl2.focus();
		return (false);
	 }
	 return (true);
 }
/*-----------------------------------------------------*/

//Option button & checkbox validation
function check(ctl,n,msg)      
	{
   	if(msg=="")
	 {
	  msg="Please select a value";
	 }

	var ctr=0;

		for(i=0;i<n;i++)
		 {
			if(ctl(i).checked==false)
			 {
				ctr=ctr+1;
			 }
		 }
			if (ctr==n)
			 {
				alert(msg);
				ctl(0).focus();
				return false;
			 }
		return true;
	}
/*-----------------------------------------------------*/
//Option button & checkbox value checking
function checkval(ctl,n,msg)      
	{
   	if(msg=="")
	 {
	  msg="Please select a value";
	 }

	var ctr=0;
    var strv=""
		for(i=0;i<n;i++)
		 {
           for(i=0;i<2;i++)
		     {		 
			    if(ctl(i).checked==true)
			     {
				  strv=i
				  ctr=ctr+1;
			     }
		     }	 
		 }
			if (strv != "")
			 {
				alert(msg);
				ctl(0).focus();
				return false;
			 }
			strv="" 
		return true;
	}
/*-----------------------------------------------------*/


//Popup window
function popup(theURL,winName,features) 
	{
		window.open(theURL,winName,features);
	}
/*-----------------------------------------------------*/

//Credit card validation script
	function cardval(s) 
	 {
		// remove non-numerics
		var v = "0123456789";
		var w = "";
		for (i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
		}
		// validate number
		j = w.length / 2;
		if (j < 6.5 || j > 8 || j == 7) return false;
		k = Math.floor(j);
		m = Math.ceil(j) - k;
		c = 0;
		for (i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
		}
		for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
		return (c%10 == 0);
	 }
/*-----------------------------------------------------*/

function checkemail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}




// Character counter
// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}


// Example of calling the script 
//<strong>textarea 1</strong><br>
//<textarea name="taMessage" id="taMessage" cols="40" rows="5"
//onkeyup="CheckFieldLength(taMessage, 'charcount', 'remaining', 20);" onkeydown="CheckFieldLength(taMessage, 'charcount', 'remaining', 20);" onmouseout="CheckFieldLength(taMessage, 'charcount', 'remaining', 20);"></textarea><br>
//<small><span id="charcount">0</span> characters entered.   |   <span id="remaining">20</span> characters remaining.</small><br>

function padout(number) { 
	return (number < 10) ? '0' + number : number; 
}

