/**********************************************************************
 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 validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.gcn.user_pass.value;
var pw2 = document.gcn.user_pass_confirm.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your password twice.');
return false;
}
// check for minimum length
if (document.gcn.user_pass.value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.gcn.user_pass.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same new password twice. Please re-enter your password.");
return false;
}

   }
}

function ValidateForm(){
	var signup_usernameID=document.gcn.signup_username
	var user_passID=document.gcn.user_pass
	var user_pass_confirmID=document.gcn.user_pass_confirm
	var emailID=document.gcn.email
	var zipID=document.gcn.zip
	var agreeID=document.gcn.agree
	
	if ((signup_usernameID.value==null)||(signup_usernameID.value=="")){
		alert("Please Enter A Username")
		signup_usernameID.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 ((user_passID.value==null)||(user_passID.value=="")){
		alert("Please Enter A Password")
		user_passID.focus()
		return false
	}
	if ((user_pass_confirmID.value==null)||(user_pass_confirmID.value=="")){
		alert("Please Verify Your Password")
		user_pass_confirmID.focus()
		return false
	}
	if (validatePwd(user_passID.value)==false){
		user_passID.value=""
		user_passID.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
 }
