//index page scripts

var theCount=0; var picksOfTheList = new Array(10);
document.cookie = 'foresee.repeatdays=365;expires=;path=/;domain=epa.gov;';

function getTheCount(){
 var total = 0; var qq=document.pick5form;
 for (var ii = 0; ii < qq.pick5item.length; ii++) {
  if (eval("qq.pick5item[" + ii + "].checked") == true){ 
   total+=1; 
   picksOfTheList[ii]=1;
  }else{ picksOfTheList[ii]=0; }
 }
 return total;
}

function checkEmailAddress(theEmail){
 if (theEmail){
 var theChar =
/^(([^<>()[\]\\.,;:\s@\”]+(\.[^<>()[\]\\.,;:\s@\”]+)*)|(\”.+\”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  if( theChar.test(theEmail) == false ){
   alert('Please enter a valid email address.'); 
   return false;
  }
 }
}

function updateCount() {
 var oChild = document.getElementById("theCountNode");
 var oNewChild = document.createElement('P');
 oNewChild.id = oChild.id;
 oNewChild.style.color="#000000";
 oNewChild.style.fontWeight="normal";
 theCount = getTheCount(); var thePunct="...";
 if(theCount >= 5){ thePunct = "!"; oNewChild.style.fontWeight="bold"; }
 theCountText="You've picked " + theCount + thePunct;
 oNewChild.innerHTML = theCountText; 
 oChild.parentNode.replaceChild(oNewChild,oChild);
}

function theValidate(){
 var qq=document.pick5form; 
 if(getTheCount() < 5){
  alert("Please pick at least 5.\n\nYou've picked only " + getTheCount() + ".");
  return false;
 }
 if(qq.zipcode.value){
	if ( qq.zipcode.value.length < 5 || !Number(qq.zipcode.value) ){
    alert('Please enter a 5-digit ZIP code.');
	return false;
 }}
 if (qq.emailaddress.value){
  if ( qq.emailaddress.value.indexOf("@") <= 0 || qq.emailaddress.value.indexOf(".") <= 2 ){ 
   alert('Please enter a valid email address.');
   return false;
 }}
 //OK TO PLEDGE!
 var picksPasser="";
 for (var ii = 0; ii < qq.pick5item.length; ii++) {
  picksPasser += picksOfTheList[ii];
 }
 var sessCook = "EPAPick5=" + picksPasser + "; expires=";
 document.cookie=sessCook;
 var zippart = qq.zipcode.value; if(zippart=="" ){ zippart="-"; }
 var emailpart = qq.emailaddress.value; if(emailpart=="" ){ emailpart="-"; }
 qq.Subject.value = "pick5submittal|" + picksPasser +"|"+ zippart +"|"+ emailpart;
}

