/**********************************************************************
 section 1 - form validation
 **********************************************************************/
	

function echeck(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 Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}


function isNumeric(zipcheck){
	var numericExpression = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if(zipcheck.match(numericExpression)){
	return true;
	
	}
	
	else{
		alert("Invalid Zip Code");
		return false;
	}
}

function validateUSDate( strValue ) {

  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}


function ValidateForm(){
	var nameID=document.gcn.name
	var lnameID=document.gcn.lname
	var emailID=document.gcn.email
	var birthID=document.gcn.birth
	var cityID=document.gcn.city
	var zipID=document.gcn.zip
	var agreeID=document.gcn.agree
	
	if ((nameID.value==null)||(nameID.value=="")){
		alert("Please Enter your First Name")
		nameID.focus()
		return false
	}
	if ((lnameID.value==null)||(lnameID.value=="")){
		alert("Please Enter your Last Name")
		lnameID.focus()
		return false
	}
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	if ((birthID.value==null)||(birthID.value=="")){
		alert("Please Enter your Birthday")
		birthID.focus()
		return false
	}
	if (validateUSDate(birthID.value)==false){
		birthID.value=""
		birthID.focus()
		return false
	}
	if ((cityID.value==null)||(cityID.value=="")){
		alert("Please Enter your City")
		cityID.focus()
		return false
	}
	if ((zipID.value==null)||(zipID.value=="")){
		alert("Please Enter your Zip Code")
		zipID.focus()
		return false
	}
	if (isNumeric(zipID.value)==false){
		zipID.value=""
		zipID.focus()
		return false
	}
	if ((agreeID.checked==null)||(agreeID.checked=="")){
		alert("Please Agree to the Grocery Coupon Network Policy")
		return false
	}
	return true
 }