

  
 // Validate Email

function IsEmail(field) {

	var emailStr=field.value
	var emailPat=/^(.+)@(.+)$/;
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; //not valid
	var reg2 = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(com|net|org|edu|int|inc|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/; //valid
    var matchArray=emailStr.match(emailPat);

    if (emailStr.length == 0) 
		{
			field.style.background ="";
			return true;
	    }
	else
    	{ 
	if (matchArray==null) {
        alert("Email address seems incorrect (check @ and .'s)");
		field.focus();
		field.style.background ="#FF0033";
		return false;
		//field.style.border ="1px solid ";
    }
    if (!reg1.test(emailStr) && reg2.test(emailStr)) 
		{
		}
		else
		{
		field.focus();
		field.style.background ="#FF0033";
		alert("The Email address contains invalid characters.");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
	}
return true;
}


// Validate Email Required!

function IsEmailRequired(field) {
  
	var emailStr=field.value
	var emailPat=/^(.+)@(.+)$/;
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; //not valid
	var reg2 = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(com|net|org|edu|int|inc|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/; //valid
    var matchArray=emailStr.match(emailPat);
		
    if (matchArray==null) {
        field.focus();
		field.style.background ="#FF0033";
		alert("Email address seems incorrect (check @ and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
    }
    if (!reg1.test(emailStr) && reg2.test(emailStr)) 
		{
		}
		else
		{
		field.focus();
		field.style.background ="#FF0033";
		alert("The Email address contains invalid characters.");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
}


// Validate Number Only Any format

function IsNumber(field) {

	var numberStr=field.value
	
    if (numberStr.length == 0) 
		{
			field.style.background ="";
			return true;
	    }
	else
    	{ 
	if ( isNaN(numberStr) ) {
		field.focus();
		field.style.background ="#FF0033";
		alert("The number format seems incorrect (check $ and ,'s and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
	}
field.style.background ="";
return true;
}


// Validate Number Any Value - Required!

function IsNumberRequired(field) {

	var numberStr=field.value
	
	if (numberStr.length == 0) 
		{
		field.focus();
		field.style.background ="#FF0033";
		alert("A numeric value without special characters is required (check $ and ,'s and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	    }
	if ( isNaN(numberStr) ) {
		field.focus();
		field.style.background ="#FF0033";
		alert("The number format seems incorrect (check $ and ,'s and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
field.style.background ="";
return true;
}




// Validate Number - Decimal Format Only!

function IsDecimal(field) {

	var decimalStr=field.value
	var decimalPat="^[\\d]+[\\d\.]+$";
	var matchArray=decimalStr.match(decimalPat);

    if (decimalStr.length == 0) 
		{
			field.style.background ="";
			return true;
	    }
	else
    	{ 
	if (matchArray==null) {
        field.focus();
		field.style.background ="#FF0033";
		alert("The decimal value seems incorrect (check $ and ,'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
    }
    if ( isNaN(decimalStr) ) {
		field.focus();
		field.style.background ="#FF0033";
		alert("The decimal value seems incorrect (check $ and ,'s and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
    }
field.style.background ="";
return true;
}


// Validate Number - Decimal Format Only - Required!

function IsDecimalRequired(field) {

	var decimalStr=field.value
	var decimalPat="^[\\d]+[\\d\.]+$";
	var matchArray=decimalStr.match(decimalPat);

    if (matchArray==null) {
        field.focus();
		field.style.background ="#FF0033";
		alert("The decimal value seems incorrect (check $ and ,'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
    }
    if ( isNaN(decimalStr) ) {
		field.focus();
		field.style.background ="#FF0033";
		alert("The decimal value seems incorrect (check $ and ,'s and .'s)");
		//field.style.border ="1px solid ";
		//setTimeout("field.focus()", 1);
        return false;
	   }
field.style.background ="";
return true;
}

// validate alpha characters only
function IsAlphaOnly(field) {

var alphaStr=field.value
var IS_ALPHA_ONLY=/^[a-zA-Z]+([a-zA-Z]+)*$/; //valid

if (!IS_ALPHA_ONLY.test(alphaStr)) 
 {
field.focus();
field.style.background ="#FF0033";
alert('Only alphabetical characters are allowed!');
return false;
 }
field.style.background ="";
return true;
 }

// Validate Date

  var dateformat = 'The date format should be : mm/dd/yyyy.' ;
  var errvalidmonth ='Please enter a valid month.' ;
  var errvalidday ='Please enter a valid day.' ;
  var errvalidyear ='Please enter a valid four (4) digit year.' ;
  var errvalidate ='Please enter a valid date.' ;
  var errdateformat='The date format should be : mm/dd/yyyy.' ;
  var errFutureDatesNotAllowed='Future dates are not allowed.' ;
  var errCantGoBackMoreThanAYear="";
  var dtCh= "/";
  var minYear=1900;
  var maxYear=2100;
  
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

 
  function isDate(dateControl)
   {
   	var dtStr       = dateControl.value;
   	var daysInMonth = DaysArray(12)
   	var pos1=dtStr.indexOf(dtCh)
   	var pos2=dtStr.indexOf(dtCh,pos1+1)
   	var strMonth=dtStr.substring(0,pos1)
   	var strDay=dtStr.substring(pos1+1,pos2)
   	var strYear=dtStr.substring(pos2+1)
   	strYr=strYear
   	
   	 
   	
   	if ( strDay.length<2 || strMonth.length<2 ){
	   		alert(errdateformat)
	   		dateControl.focus()
	   		return false
   	}
   	
    
   	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
   	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
   	for (var i = 1; i <= 3; i++) {
   		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
   	}
   	month=parseInt(strMonth)
   	day=parseInt(strDay)
   	year=parseInt(strYr)
   	
   	var userGivenDate=new Date()
   	
	userGivenDate.setFullYear(year,month-1,day)
	
	var today = new Date()
		 
	var ayearAgo = new Date()
	var year     = today.getYear();
	year         = year -1;
	
	ayearAgo.setYear(year);  
	
   	if (pos1==-1 || pos2==-1 ){
   		alert(errdateformat)
   		dateControl.focus()
   		return false
   	}else  	
   	if (strMonth.length<1 || month<1 || month>12){
   		alert(errvalidmonth )
   		dateControl.focus()
   		return false
   	}
   	else
   	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
   		alert(errvalidday)
   		dateControl.focus()
   		return false
   	}
   	else
   	if (strYear.length != 4 || year==0 ){
   	         
   		alert(errvalidyear )
   		dateControl.focus()
   		return false
   	}
   	else
   	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
   		alert(errvalidate)
   		dateControl.focus()
   		return false
   	}else
   	if (userGivenDate>today)
	{
		  dateControl.focus()
	   	  alert(errFutureDatesNotAllowed)
		  return false
	}else
	if (errCantGoBackMoreThanAYear != ""   && userGivenDate < ayearAgo)
	{
	   alert(errCantGoBackMoreThanAYear)
	   dateControl.focus()
	   return false
	}
	 
   	return true
 }

function ValidateFormDate(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
  
  
 
