var err = '';
var under18 = false;
var isCookied = true;
function IsNumeric(strString)
   //  check for valid numeric strings
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
 
   function doSubmit(){
      isCookied = true;
      err = "";
      if (document.myForm.password.value=='') {
          err+='Password is a required field.\n'; 
		  isCookied = false;
		  document.myForm.password.focus();
      } else if (document.myForm.password.value.length<6) {
          err+='Password must be at least 6 characters long.\n'; 
 		  isCookied = false;
		  document.myForm.password.focus();
      }
      if (document.myForm.again.value=='') {
          err+='Verify Password is a required field.\n'; 
		  isCookied = false;
		  document.myForm.password.focus();
	  }
      else if (document.myForm.again.value!=document.myForm.password.value) {
          err+='Verify Password must match the Password field.\n'; 
  		  isCookied = false;
		  document.myForm.again.focus();
      }
      if (document.myForm.email.value=='')
      {
          err+='EMail is a required field and must be of the form "aaaa@bbb.ccc".\n';
		  isCookied = false;
          document.myForm.email.focus();
      }
      else
      {
          var at=document.myForm.email.value.indexOf('@');
          var dot=document.myForm.email.value.lastIndexOf('.');
          if(at==-1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". "@" is missing.\n';
			  isCookied = false;
              document.myForm.email.focus();

          }
          else if(dot==-1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". "." is missing.\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(at>=dot)
          {
              err+='Email must be of the form "aaaa@bbb.ccc"."@" is not before "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(at==0)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character before "@" .\n';
				isCookied = false;
              document.myForm.email.focus();

          }
          else if(at+1==dot)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character between "@" and "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(document.myForm.email.value.length==document.myForm.email.value.lastIndexOf(".")+1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character after "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
      }
      if (document.myForm.last.value==    '') { err+='Last name is a required field.\n';isCookied = false;    document.myForm.last.focus();  }
      if (document.myForm.first.value==   '') { err+='First name is a required field.\n';isCookied = false;   document.myForm.first.focus(); }

      if(document.myForm.birthMonth)
      {
        if (document.myForm.birthMonth.value==   '' || document.myForm.birthMonth.value==   'Birth Month') { err+='Birth Month is a required field.\n';isCookied = false;   document.myForm.birthMonth.focus(); }
      }
      if(document.myForm.birthYear)
      {
        if (document.myForm.birthYear.value== '' || document.myForm.birthYear.value== 'Birth Year') { err+='Birth Year is a required field.\n';isCookied = false;   document.myForm.birthYear.focus(); }
      }
      
	  /*if((document.myForm.birthMonth)&& (document.myForm.birthYear))
      {
          if(document.myForm.birthMonth.value!=''&&document.myForm.birthYear.value!='')
          {
              var dateToCheck = new Date();
              dateToCheck.setYear(parseInt(document.myForm.birthYear.value)+18);
              dateToCheck.setMonth(parseInt(document.myForm.birthMonth.value));
              dateToCheck.setDate(1);
              var checkDate = dateToCheck.getTime();
              var now = new Date().getTime()
              if(now < checkDate)
              {
                  err+='Age not greater than 18.\n';
              }
          }
      }*/
	  
      
      if(document.myForm.zipcode)
      {
          if (document.myForm.zipcode.value==   '') { err+='Zipcode is a required field.\n';isCookied = false;   document.myForm.zipcode.focus(); }
          else if(IsNumeric(document.myForm.zipcode.value)== false) { err+='Zipcode should have only numbers.\n';isCookied = false;   document.myForm.zipcode.focus(); }
          else if(document.myForm.zipcode.value.length!=5)
          {
              err+='Zipcode should have five numbers.\n';isCookied = false;
              document.myForm.zipcode.focus();
          }
      }
	  
	  var mydate = new Date();	 
	  if(getCookie("under18")!="")
	  {
			err += "You need to be at least 18 to use this site ";
	  }else
	  {
		  var d = new Date();
		  var cd = new Date();
		  cd.setMonth(parseInt(document.myForm.birthMonth.value));
		  cd.setYear(parseInt(document.myForm.birthYear.value));
		  cd.setDate(1);
		  var days = (d-cd) /(24*60*60*1000);
		  	
		  //if((mydate.getYear()-document.myForm.birthYear.value)>18)
		  //if days == 6564 then it means user is 18 year old
		  if(days>6564)
		  {
				if (!document.myForm.agree.checked)
				{
					err+='Agreeing to terms of service is required to signup.\n';   
					document.myForm.agree.focus();
				}

		  }
		  else 
		  {

			  if (isCookied)
			  {
				err += "You need to be at least 18 to use this site  \n";
				setCookie("yes");
			  }
		  }
	  }	
      document.myForm.exec.value='register';
      if (err.length > 0) 
	  { 
			
			alert(err.substring(0, err.length-1)); 
			return false; 
	  }
	  else {
		  return true;
	  }
  }

  function getCookie(Name) {
		var search = Name + "="
		var returnvalue = "";
		if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search)
			// if cookie exists
			if (offset != -1) { 
				offset += search.length
			// set index of beginning of value
				end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
				returnvalue=unescape(document.cookie.substring(offset, end))
			}
		}
		return returnvalue;
	}
	function setCookie(what){
		document.cookie="under18="+what
	}


function validate(){	  
      isCookied = true;
      err = "";
	  var illegalChars = /\W/;
	  
      	   //validitaing First and Last name
     if (document.myForm.fname.value=='') { err+='First name is a required field.\n';isCookied = false; document.myForm.fname.focus();}
	 else if(illegalChars.test(document.myForm.fname.value)) { err+='Please enter valid name.\n';isCookied = false;   document.myForm.fname.focus();}
	  
      if (document.myForm.lname.value==    '') { err+='Last name is a required field.\n';isCookied = false;    document.myForm.lname.focus();  }
      else if(illegalChars.test(document.myForm.lname.value)) { err+='Last name is not valid.\n';isCookied = false;   document.myForm.lname.focus();}


		   //validitaing email
	  if (document.myForm.email.value=='')
      {
          err+='EMail is a required field and must be of the form "aaaa@bbb.ccc".\n';
		  isCookied = false;
          document.myForm.email.focus();
      }
      else
      {
          var at=document.myForm.email.value.indexOf('@');
          var dot=document.myForm.email.value.lastIndexOf('.');
          if(at==-1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". "@" is missing.\n';
			  isCookied = false;
              document.myForm.email.focus();

          }
          else if(dot==-1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". "." is missing.\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(at>=dot)
          {
              err+='Email must be of the form "aaaa@bbb.ccc"."@" is not before "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(at==0)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character before "@" .\n';
				isCookied = false;
              document.myForm.email.focus();

          }
          else if(at+1==dot)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character between "@" and "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
          else if(document.myForm.email.value.length==document.myForm.email.value.lastIndexOf(".")+1)
          {
              err+='Email must be of the form "aaaa@bbb.ccc". There should be at least one character after "." .\n';
			  isCookied = false;
              document.myForm.email.focus();
          }
      }


	 //For validating Password	  
      if (document.myForm.password.value=='') {
          err+='Password is a required field.\n'; 
		  isCookied = false;
		  document.myForm.password.focus();
      } else if (document.myForm.password.value.length<6) {
          err+='Password must be at least 6 characters long.\n'; 
 		  isCookied = false;
		  document.myForm.password.focus();
      }
      if (document.myForm.vpassword.value=='') {
          err+='Confirm Password is a required field.\n'; 
		  isCookied = false;
		  document.myForm.vpassword.focus();
	  }
      else if (document.myForm.vpassword.value!=document.myForm.password.value) {
          err+='Confirm Password must match the Password field.\n'; 
  		  isCookied = false;
		  document.myForm.vpassword.focus();
      }
		
	  //For validating Address	  
	  if (document.myForm.add1.value=='') {
          err+='Address is a required field.\n'; 
		  isCookied = false;
		  document.myForm.add1.focus();
      }
	  
	  //For validating City	  
	  if (document.myForm.city.value=='') {
          err+='City is a required field.\n'; 
		  isCookied = false;
		  document.myForm.city.focus();
      }
		
	   //For validating State	  
	  if (document.myForm.state.value=='') {
          err+='State is a required field.\n'; 
		  isCookied = false;
		  document.myForm.state.focus();
      }

     //For validating zip code	  
      
      if(document.myForm.zip)
      {
          if (document.myForm.zip.value==   '') { err+='Zipcode is a required field.\n';isCookied = false;   document.myForm.zip.focus(); }
          else if(IsNumeric(document.myForm.zip.value)== false) { err+='Zipcode should have only numbers.\n';isCookied = false;   document.myForm.zip.focus(); }
          else if(document.myForm.zip.value.length!=5) { err+='Zipcode should have five numbers.\n';isCookied = false; document.myForm.zip.focus(); }
		  else if(document.myForm.zip.value==0) { err+='Zipcode cant be zero, enter valid Zipcode.\n';isCookied = false; document.myForm.zip.focus(); }
      }

	  		   
     //for validating Birth Day.
	  if(document.myForm.bmonth)
       {
        if (document.myForm.bmonth.value==   '' || document.myForm.bmonth.value== 'Month') { err+='Birth Month is a required field.\n';isCookied = false;   document.myForm.bmonth.focus(); }
      }
      if(document.myForm.byear)
      {
        if (document.myForm.byear.value== '' || document.myForm.byear.value== 'Year') { err+='Birth Year is a required field.\n';isCookied = false;   document.myForm.byear.focus(); }
      }

			 //for validating Age
 if((document.myForm.bmonth)&& (document.myForm.byear))
      {
          if(document.myForm.bmonth.value!=''&&document.myForm.byear.value!='')
          {
              var dateToCheck = new Date();
              dateToCheck.setYear(parseInt(document.myForm.byear.value)+18);
              dateToCheck.setMonth(parseInt(document.myForm.bmonth.value));
              dateToCheck.setDate(1);
              var checkDate = dateToCheck.getTime();
              var now = new Date().getTime()
              if(now < checkDate)
              {
                  err+='Please enter a valid entry.\n';
              }
          }
      }
	  
	 //validating  terms and condition
	  if(document.myForm.agree)
      {
        if (!document.myForm.agree.checked){err+='Agreeing to terms of service is required to signup.\n';   document.myForm.agree.focus();}
      }
	 
     document.myForm.exec.value='register';
	  
      if (err.length > 0) 
	  { 
			
			alert(err.substring(0, err.length-1)); 
			return false; 
	  }
	  else {
		  return true;
	  }
  }
