function focusNext(form, elemName, evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
        form.elements[elemName].focus( );
        return false;
    }
    return true;
}

function forceMass(elemName) {
  if ( elemName.value != 'MA' ) { 
    alert("We are currently only accepting participants\nfrom Massachusetts.")
    elemName.value='MA';
    return false;
  }
  alert(elemName.value);
}

function autofocus(field, limit, next, evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && field.value.length == limit) {
        field.form.elements[next].focus( );
    }
}

function validate_process () {
  var doc = document.main_form;
  var invalid = 0;
  var alertstr = '';


  if ( !is_empty(doc.txt_livedyears.value) ) {
    if (!is_numeric(doc.txt_livedyears.value)){
      invalid++; alertstr += "You must enter a numeric number of years you have lived at your current residence.\n"
    }
}

  if ( !is_empty(doc.txt_livedmonths.value) ) {
    if (!is_numeric(doc.txt_livedmonths.value)){
      invalid++; alertstr += "You must enter a numeric number of months you have lived at your current residence.\n"
    }
   else {
      var molived = get_numerical(doc.txt_livedmonths.value);
      if (molived < 0 || molived > 12){
        invalid ++; alertstr += "Months lived at residence must be between 0 and 12.\n"  
      }
   }
  }

   var dateval = [
               [doc.txt_chld1yob, doc.txt_chld1mob, doc.txt_chld1dob],
               [doc.txt_chld2yob, doc.txt_chld2mob, doc.txt_chld2dob],
               [doc.txt_chld3yob, doc.txt_chld3mob, doc.txt_chld3dob],
               [doc.txt_chld4yob, doc.txt_chld4mob, doc.txt_chld4dob],
               [doc.txt_chld5yob, doc.txt_chld5mob, doc.txt_chld5dob],
             ];



for (var j = 0; j < 5 ; j++) {
  var yearof = dateval[j][0].value;
  var daysof = dateval[j][2].value;
  var monthof = dateval[j][1].value;
    
  if ( is_empty(yearof) && is_empty(daysof) && is_empty(monthof) ){ break; }
  if ( !is_numeric(daysof) || !is_numeric(yearof)  || !is_numeric(monthof) ) { invalid++; alertstr += "'Birthdate' does not look correct\n" }
  else { 
     var year = get_numerical(yearof);
     var days = get_numerical(daysof);
     var month = get_numerical(monthof);
  }   
  var date = new Date();
  var daytoday = date.getDate()
  var daymonth = date.getMonth()
  var dayyear = date.getFullYear()

  if ( (year > dayyear) || ( year < (dayyear - 25)) ){
   invalid++; alertstr += "Year is invalid, either it is below "+(dayyear-25)+" or above "+(dayyear)+"\n"
  }
  if ( ((dayyear) <= year) && ((daymonth +1 ) >= month) && (daytoday <= (days - 1) )) {
   invalid++; alertstr += "Date for birth of child is in the future, enter a valid time\n"
  } 
  if ( ((dayyear - 25) >= year) && ((daymonth + 1) >= month) && (daytoday >= days )) {
   invalid++; alertstr += "Child is over the age of 25\n"
  } 
  if ( month > 0 && month < 13 ) { 
    if ( month == 2 && (year % 4 == 0) && (!(year % 100 == 0) || (year % 400 == 0) ) ) { if ( days < 1 || days > 29 ) { invalid++; alertstr += "Birthdate is Invalid\n"; }  } 
    else {
      if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) { if ( days < 1 || days > 31 ) { invalid++; alertstr += "Birthdate is Invalid\n" } }
      else { 
        if ( month == 2  &&  ( days < 1 || days > 28) ) { invalid++; alertstr += "Birthdate is Invalid\n" } 
       else { if ( days < 1 || days > 30 ) { invalid++; alertstr += "Birthdate is Invalid\n" }  }
      }
    } 
  } 
  else { invalid++; alertstr += "Month is out of Range (1 - 12)\n" } 
}  

 if (!is_empty(doc.num_employeesall.value)){
  if ( !is_numeric(doc.num_employeesall.value))
     {invalid++; alertstr +="Number of all employees at your organization is not a number.\n"}
 }

 if (!is_empty(doc.num_employeeshere.value)){
  if ( !is_numeric(doc.num_employeeshere.value))
     {invalid++; alertstr +="Number of employess at your location is not a number.\n"}
 }
 var emphere = get_numerical(doc.num_employeeshere.value);
 var empall = get_numerical(doc.num_employeesall.value);

 if (!is_empty(doc.num_employeeshere) && !is_empty(doc.num_employeesall.value)){
  if ( emphere > empall)
     {invalid++; alertstr +="Number of employees at your location exceeds the total number of employees in your organization.\n"}
 }

 if (!is_empty(doc.num_workcomp.value)){
  if (!is_numeric(doc.num_workcomp.value)) {invalid++; alertstr +="Number of hours on a computer in a week is not a number.\n"}
  else {
      var weekly = get_numerical(doc.num_workcomp.value);
     if (weekly > 167){
        invalid++; alertstr +="Hours you work on a computer in a week exceeds the time allowed in a given week.\n"}
    }
  }

   if ( invalid > 0 || alertstr != '' ) {
   alert ("There were "+invalid+" error(s) with your submission:\n\n"+alertstr+"\nPlease correct these error(s) and resubmit.\n\n");
   return false; 
  }
  return true;
}

function master_hide ()
     {
      show_smokes ();
      music_type ();
      radio_other ();
      car1_other ();
      car2_other ();
      beer_drinker ();
      beer_type ();
      medic_condition ();
      other_cigs ();
     }

function other_cigs ()
     {
      if (document.main_form.cig_brand[324].selected)
         {document.getElementById('hidesmokes').style.display = 'block'}
      else {document.getElementById('hidesmokes').style.display = 'none'}
     }

function medic_condition ()
     {
      if (document.main_form.chk_med[2].checked)
         {document.getElementById('txt_medic').style.display = 'block'}
      else {document.getElementById('txt_medic').style.display = 'none'}
     }


function show_smokes ()
     { 
      if (document.main_form.r_smokes[1].checked) 
       {document.getElementById('hide_cigs').style.display = 'block'}
     else {document.getElementById('hide_cigs').style.display = 'none'}
     }

function music_type ()
     {
      if (document.main_form.select_music[27].selected)
         {document.getElementById('musichide').style.display = 'block'}
      else {document.getElementById('musichide').style.display = 'none'}
     }


function radio_other ()
     { 
      if (document.main_form.select_station[1].selected)
         {document.getElementById('radiohide').style.display = 'block'}
      else {document.getElementById('radiohide').style.display = 'none'}
     }

function car1_other ()
     {
      if (document.main_form.make[45].selected)
          {document.getElementById('hideveh1').style.display = 'block'}
      else {document.getElementById('hideveh1').style.display = 'none'}
     }

function car2_other ()
     {
      if (document.main_form.make2[45].selected)
          {document.getElementById('hideveh2').style.display = 'block'}
      else {document.getElementById('hideveh2').style.display = 'none'}
     }

function beer_drinker ()
     {
      if (document.main_form.drinks[0].checked)
         {document.getElementById('hidebeer').style.display = 'block'}
      else {document.getElementById('hidebeer').style.display = 'none'}
     }

function beer_type ()
     {
      if (document.main_form.beer_brand[1].selected || document.main_form.beer_brand[2].selected)
         {document.getElementById('hidebeerbrand').style.display = 'block'}
      else   {document.getElementById('hidebeerbrand').style.display = 'none'}
      }

function age_break ()
     {
      if ((document.main_form.txt_nop.value) > 1)
         {document.getElementById('hidenop').style.display = 'block'}
      else {document.getElementById('hidenop').style.display = 'none' }

     }


function validate_mainpage () {
  var doc = document.main_form;
  var invalid = 0;
  var alertstr = '';
  
  if ( is_empty(doc.txt_fname.value) )    { invalid++; alertstr+= "'First Name' is missing.\n" }
  if ( is_empty(doc.txt_lname.value) )    { invalid++; alertstr+= "'Last Name' is missing.\n" }
  if ( is_empty(doc.txt_address1.value) ) { invalid++; alertstr+= "'Mailing Address' is missing.\n" }

  if ( is_empty(doc.txt_zip.value) )      { invalid++; alertstr+= "'Zip Code' is missing.\n" }
  else {
    if ( !looks_like_zip(doc.txt_zip.value) ) { invalid++; alertstr +=  "'Zip Code' is in an incorrect format. Please use ##### or #####-####\n" } 
  }
 
  if ( !doc.Counties.value ) { invalid++; alertstr += "Please choose 'County'\n" }
  if ( is_empty(doc.txt_ac.value) || is_empty(doc.txt_exchange.value) ||  is_empty(doc.txt_phone) ) { invalid++; alertstr += "Some 'Home Phone' Fields are Missing\n" }
  else {
    if ( !is_numeric(doc.txt_ac.value) || doc.txt_ac.value.length != 3 || 
       !is_numeric(doc.txt_exchange.value) || doc.txt_exchange.value.length != 3  || 
       !is_numeric(doc.txt_phone.value) || doc.txt_phone.value.length != 4 ) { invalid++; alertstr += "'Home Phone' does not look correct\n" }
  }

  if ( !chosen(doc.r_gender) ) { invalid++; alertstr += "Please choose 'Gender'\n" } 

  if ( is_empty(doc.txt_mob.value) || is_empty(doc.txt_dob.value) ||  is_empty(doc.txt_yob.value) ) { invalid++; alertstr += "Some 'Birthdate' Fields are Missing\n" }
  else {
    if ( !is_numeric(doc.txt_mob.value) || !is_numeric(doc.txt_dob.value)  || !is_numeric(doc.txt_yob.value) ) { invalid++; alertstr += "'Birthdate' does not look correct\n" }
    else {

       var year = get_numerical(doc.txt_yob.value);
       var days = get_numerical(doc.txt_dob.value);
       var month =get_numerical(doc.txt_mob.value);
   
       if (year < 1000) {invalid++; alertstr += "Enter the year you were born in yyyy format.\n"}     
       
       var cur_date = new Date();
       var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

       if ( year > cur_date.getFullYear() - 18  ||
	    ( (year == cur_date.getFullYear() - 18) && (month > cur_date.getMonth() + 1) ) ||
	    ( year == cur_date.getFullYear() - 18 && month == cur_date.getMonth() + 1 && days > cur_date.getDate() ) 
	  ) { 
          invalid++; 
	  alertstr += "You must have been born before " 
		    + m_names[ cur_date.getMonth() ] 
		    + " " 
		    + ( cur_date.getDate() )
		    + " ," 
		    + (cur_date.getFullYear() - 18) 
		    +", in order to be registered in our database.\n"
       }
       else {
         if ( month > 0 && month < 13 ) { 
           if ( month == 2 && (year % 4 == 0) && (!(year % 100 == 0) || (year % 400 == 0) ) ) { 
             if ( days < 1 || days > 29 ) { invalid++; alertstr += "Birthdate is Invalid\n"; }  
           } 
           else {
            if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) { if ( days < 1 || days > 31 ) { invalid++; alertstr += "Birthdate is Invalid\n" } }
            else { 
              if ( month == 2  &&  ( days < 1 || days > 28 ) ) { invalid++; alertstr += "Birthdate is Invalid\n" } 
              else { if ( days < 1 || days > 30 ) { invalid++; alertstr += "Birthdate is Invalid\n" }  }
            }
          }
        } 
        else { invalid++; alertstr += "Month is out of Range (1 - 12)\n" }
      } 
    }
  }
  if (doc.txt_nop.value < 1) {invalid++; alertstr += "Number of people in your home must be greater than 0.\n"}
  if (doc.txt_nop.value > 1) {
   if (!is_numeric(doc.txt_under5.value) || is_empty(doc.txt_under5.value)){invalid++; alertstr += "Number of people under 5 is not numeric.(If none put 0)\n"; }
   if (!is_numeric(doc.txt_6to12.value) || is_empty(doc.txt_6to12.value)){invalid++; alertstr += "Number of people from 6 to 12 is not numeric.(If none put 0)\n"; }
   if (!is_numeric(doc.txt_13to17.value) || is_empty(doc.txt_13to17.value)){invalid++; alertstr += "Number of people from 13 to 17 is not numeric.(If none put 0)\n"; }
   if (!is_numeric(doc.txt_18to24.value) || is_empty(doc.txt_18to24.value)){invalid++; alertstr += "Number of people from 18 to 24 is not numeric.(If none put 0)\n"; }
   if (!is_numeric(doc.txt_25older.value) || is_empty(doc.txt_25older.value)){invalid++; alertstr += "Number of people over 25 is not numeric.(If none put 0)\n"; }

   var under = get_numerical(doc.txt_under5.value);
   var sixto = get_numerical(doc.txt_6to12.value);
   var thirtto = get_numerical(doc.txt_13to17.value);
   var eightto = get_numerical(doc.txt_18to24.value);
   var twentyolder = get_numerical(doc.txt_25older.value);

   if ((under + sixto + thirtto + eightto + twentyolder) != doc.txt_nop.value) {invalid++; alertstr += "Number of people in your house must equal the number of people in various age groups.\n"; }
  }

  if ( is_empty(doc.txt_nop.value) ) { invalid++; alertstr += "'Number of People in your home' is missing\n" }
  else {
    if ( !is_numeric(doc.txt_nop.value) ) { invalid++; alertstr += "'Number of People in your home' contains an invalid value\n" }
  }

  if ( !doc.s_income.value ) { invalid++; alertstr += "Please choose 'Annual Household Income'\n" }

  if ( !chosen(doc.r_ethnicity) ) { invalid++; alertstr += "Please select 'Ethnicity'\n" }
  else {
    if ( is_empty(doc.txt_othrace.value) && doc.r_ethnicity[5].checked ) { invalid++; alertstr += "Please Specify 'Other Ethnicity'\n" }
  }
  if ( !chosen(doc.r_ed) ) { invalid++; alertstr += "Please select 'level of education'\n" }
  else {
    if ( doc.r_ed[6].checked && is_empty(doc.txt_othed.value) ) { invalid++; alertstr += "Please Specify 'Other Education'\n" }
  }
  if ( !chosen(doc.r_marital) ) { invalid++; alertstr += "Please select 'Marital Status'\n" }
  if ( invalid > 0 || alertstr != '' ) {
   alert ("There were "+invalid+" error(s) with your submission:\n\n"+alertstr+"\nPlease correct these error(s) and resubmit.\n\n");
   return false; 
  }
  return true;
}



function looks_like_zip (x) {
  var re = /^\d{5}(-\d{4})?$/
  if ( re.test(x) ) { return true; }
  else { return false; }
}

function is_empty(x) {
  var re2 = /\S/;
  if ( !re2.test(x) ) { return true; }
  else { return false; }
}

function is_numeric(x) {
  var re = /^\s*\d+\s*$/;
  if ( re.test(x) ) { return true; }
  else { return false; }
}

function zip_chars(x){
  var re = /^\d{5}(-\d{4})?$/
  if ( re.test(x)) { return true }
  else { return false }
}

function is_all_numeric(x) {
  var re = /^\d+$/;
  if ( re.test(x) ) { return true; }
  else { return false; }
}

function is_letters(x) {
  var re = /^\s*[ A-Za-z\-\'\.]+\s*$/;
  if ( re.test(x) ) { return true; }
  else { return false; }
}

function contains_illegal_chars(x) {
  var re=/[^a-zA-Z0-9 \-\'\.]/;
  if ( re.test(x) ) { return true; }
  else { return false; }
}

function chosen (x) {
  var chosen_var = false ;
  for ( var loop = 0; loop < x.length; loop++ ) {
    if ( x[loop].checked ) { chosen_var = true; } 
  } 
  return chosen_var;
}

function get_numerical(x) {
  while ( x.charAt(0) == "0" && x.length > 1 )
    x = x.substring(1,x.length)
  return(parseInt(x));
}



