//verifies that the element has a value
function hasValue(theElement) {
   if (theElement.value == "") {
      theElement.style.background = "#ffcccc";
      return false;
   }
   return true;
}

function is_todays_date(end) {
   end.value = trim(end.value);
   var enddate = end.value;
   enddate = Date.parse(enddate.replace(/-/g,"/"));

   var d= new Date();

   var month = d.getMonth()+1;
   var day = d.getDate();
   var year = d.getFullYear();

   var today = month + "/" + day +"/"+ year;
   
   todaydate = Date.parse(today);

   if (enddate > todaydate) {
      end.style.background = "#ffcccc";
     return false; 
   }
   return true;
}

function is_same_years(bio,end) {
   bio.value = trim(bio.value);
   end.value = trim(end.value);
   var biodate = bio.value;
   var enddate = end.value;
   biodate = biodate.replace(/-/g,"/");
   enddate = enddate.replace(/-/g,"/");

   var b = new Date(biodate);
   var e = new Date(enddate);

   var byear = b.getFullYear();
   var eyear = e.getFullYear();

   if (!(byear == eyear)) {
      bio.style.background = "#ffcccc";
      end.style.background = "#ffcccc";
      return false;
   }
   return true;
}

function is_valid_dates(bio,end) {
   bio.value = trim(bio.value);
   end.value = trim(end.value);
   var biodate = bio.value;
   var enddate = end.value;
   biodate = Date.parse(biodate.replace(/-/g,"/"));
   enddate = Date.parse(enddate.replace(/-/g,"/"));

   if (biodate > enddate) {
      bio.style.background = "#ffcccc";
      end.style.background = "#ffcccc";
      return false;
   }
   return true;
}

//validates the username
function is_valid_username(target) {
   target.value = trim(target.value);
   var filter = RegExp("^[a-zA-Z][a-zA-Z0-9]{4,14}$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//validates the phone number
function is_phone(target) {
   target.value = trim(target.value);
   var filter = RegExp("^[1-9]{1}[0-9]{2}\-[0-9]{3}\-[0-9]{4}$"); //("^[\(]{1}[1-9]{1}[0-9]{2}[\)]{1}\-[0-9]{3}\-[0-9]{4}$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//validates the email
function is_email(target) {
   target.value = trim(target.value);
   var filter = RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//validates the zip code
function is_zipcode(target) {
   target.value = trim(target.value);
   var filter = RegExp("^[0-9]{5}$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//verifies that the value is a number(1-99)
function is_number(target) {
   target.value = trim(target.value);
   var filter = RegExp("^[1-9]{1}[0-9]{0,1}$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

function is_dateOrNot (target) {
   target.value = trim (target.value);
   var filter = RegExp("^(0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-](19|20)[0-9]{2}$");
   if (target.value == "" || filter.test(target.value))
      return true;
   target.style.background = "#ffcccc";
   return false;
}

function is_date (target) {
   target.value = trim (target.value);
   var filter = RegExp("^(0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-](19|20)[0-9]{2}$");
   if (filter.test(target.value))
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//trims the string
function trim(strText) {
   //this will get rid of leading spaces
   while (strText.substring(0,1) == ' ')
      strText = strText.substring(1, strText.length);

   //this will get rid of trailing spaces
   while (strText.substring(strText.length-1,strText.length) == ' ')
      strText = strText.substring(0, strText.length-1);

   return strText;
}

function writeError(theError) {
   var id = "error";
   var x;
   if(document.getElementById) {
      x = document.getElementById(id);
      x.innerHTML = "";
      x.innerHTML = theError;
   } else if (document.all) {
      x = document.all[id];
      x.innerHTML = theError;
   }
}

//runs validation on the userForm (users.php)
function userCheck() {
   var noError = true;
   var theform = document.getElementById('userForm');
   var theError = "";
   var len = theform.length;
   for (var n=0; n < len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "password") || (theform[n].type == "select-one"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.nusername) || !is_valid_username(theform.nusername))
      noError = false;
   if (!hasValue(theform.npassword))
      noError = false;
   if (!hasValue(theform.name))
      noError = false;
   if (!hasValue(theform.access))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//second validation on the pestForm
function pestDateCheck() {
   var noError = true;
   var theform = document.getElementById('pestForm');
   var theError="";
   if (!is_todays_date(theform.enddate)) {
      noError = false;
      theError += "The End Date is greater than today.";
   }
   if (!is_valid_dates(theform.biodate, theform.enddate))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("Biofix Date is after End Date.");
   }
   return noError;
}

function pestYearCheck() {
   var noError = true;
   var theform = document.getElementById('pestForm');
   var theError="";
   if (!is_same_years(theform.biodate, theform.enddate))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("Biofix and End Dates must be the same year.");
   }
   return noError;
}

//stationstuff file, check when performing an update
function stationValidate() {
   var noError = true;
   var theform = document.getElementById('dStationForm');
   var theError = "";
   if(!hasValue(theform.sname) || !hasValue(theform.snum) || !hasValue(theform.slat) || !hasValue(theform.slong) || !hasValue(theform.selev)) {
      noError = false;
      theError = "*Please fill in all required fields.";
   } 
   if(!noError) {
      if(theError) {
         writeError(theError)
      }
   }
   return noError;
}

//stationstuff file, check when getting year data export
function pestDataYearCheck() {
   var noError = true;
   var theform = document.getElementById('dStationForm');
   if (!hasValue(theform.year))
      noError = false;
   if (!noError) {
      alert("You must pick a year to Get Data.");
   }
   return noError;
}

//first validation on the pestForm
function pestCheck() {
   var noError = true;
   var theform = document.getElementById('pestForm');
   var theError="";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "select-one"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.pest))
      noError = false;
   if (!hasValue(theform.biodate))
      noError = false;
   if (!is_date(theform.biodate)) {
      noError = false;
      theError += "Your Biofix Date is invalid. Please use calendar ";
   }
   if (!hasValue(theform.enddate) || !is_date(theform.enddate))
      noError = false;
   if (!is_date(theform.enddate)) {
      noError = false;
      theError += "Your End Date is invalid. Please use calendar ";
   }
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("Please fill in all required fields.");
   }
   return noError;
}

//first validation on the pestForm
function bioCheck() {
   var noError = true;
   var theform = document.getElementById('bioForm');
   var theError="";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type == "text"))
         theform[n].style.background = "white";
   }
   if (!is_dateOrNot(theform.biofix))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("The Biofix Date is invalid. Please use calendar.");
   }
   return noError;
}

//runs validation on the FeedbackForm
function feedCheck() {
   var noError = true;
   var theform = document.getElementById('feedForm');
   var theError="";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "select-one") || (theform[n].type == "textarea"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.euse))
      noError = false;
   if (!hasValue(theform.data))
      noError = false;
   if (!hasValue(theform.useful))
      noError = false;
   if (!hasValue(theform.right))
      noError = false;
   if (!hasValue(theform.improve))
      noError = false;
   if (!hasValue(theform.validate))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//runs validation on the FeedbackForm
function feedCheckFirst() {
   var noError = true;
   var theform = document.getElementById('feedForm');
   var theError="";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "select-one") || (theform[n].type == "textarea"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.myname))
      noError = false;
   if (!hasValue(theform.feedback))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//verifies that the value is a number(0-9999)
function is_Number(target) {
   target.value = trim(target.value);
   var filter = RegExp("^[0-9]{1,4}$");
   if(filter.test(target.value))//test is used to search for a match of a regular expression in a string
      return true;
   target.style.background = "#ffcccc";
   return false;
}

//runs validation on the pestInfoForm (pests.php)
function pestInfoCheck() {
   var noError = true;
   var theform = document.getElementById('pestInfoForm');
   var theError = "";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type =="text"))
         theform[n].style.background = "white";
   }
   if(!hasValue(theform.name))
      noError = false;
   if(!hasValue(theform.lower_temp) || !is_Number(theform.lower_temp))
      noError = false;
   if(!hasValue(theform.upper_temp) || !is_Number(theform.lower_temp))
      noError = false;
   if(!hasValue(theform.bf_date) || !is_date(theform.bf_date)) {
      noError = false;
      theError = "*Invalid date.";
   } if(!noError) {
      if(theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//runs validation on the pestAddActionForm (pest_phen_act.php)
function addActionCheck() {
   var noError = true;
   var theform = document.getElementById('pestAddActionForm');
   var theError = "";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if ((theform[n].type =="text"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.action2))
      noError = false;
   if (!hasValue(theform.start2)) {
      if(!is_Number(theform.start2))
         theError="*Start must be a valid number of degree days.";      
      noError = false;
   }
   if (theform.end2.value != "")
      if(!is_Number(theform.end2)) {
         noError = false;
         theError="*End must be a valid number of degree days.";
      }
   if(!noError) {
      if(theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//runs validation on the updateActionForm (pest_phen_act.php)
function updateActionCheck() {
   var noError = true;
   var theform = document.getElementById('updateActionForm');
   var theError = "";
   var len = theform.length;
   for (var n=0; n<len; n++) {
      if((theform[n].type == "text"))
         theform[n].style.background = "white";
   }
   if(!hasValue(theform.action) || !hasValue(theform.start))
      noError=false;
   else if(!is_Number(theform.start) || !is_Number(theform.end)) {
      noError=false;
      theError = "*Start and End must be numbers. ";
   }
   if(!noError){
      if(theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//runs validation on the conForm
function conCheck() {
   var noError = true;
   var theform = document.getElementById('conForm');
   var theError = "";
   var len = theform.length;
   for (var n=0; n< len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "select-one"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.fname))
      noError = false;
   if (!hasValue(theform.lname))
      noError = false;
   //if (!hasValue(theform.address))
      //noError = false;
   if (!hasValue(theform.city))
      noError = false;
   if (!hasValue(theform.state))
      noError = false;
   if (!hasValue(theform.zip) || !is_zipcode(theform.zip))
      noError = false;
   //if (!hasValue(theform.email) || !is_email(theform.email))
      //noError = false;
   //if (!hasValue(theform.phone) || !is_phone(theform.phone))
      //noError = false;
   if (!hasValue(theform.session))
      noError = false;
   if (!hasValue(theform.pnum) || !is_number(theform.pnum))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//runs validation on newsdata(news1.php)
function newsCheck() {
   var noError = true;
   var theform = document.getElementById('newsdata');
   var theError = "";
   var len = theform.length;
   for (var n=0; n < len; n++) {
      if ((theform[n].type == "text") || (theform[n].type == "textarea"))
         theform[n].style.background = "white";
   }
   if (!hasValue(theform.ntitle))
      noError = false;
   if (!hasValue(theform.summary))
      noError = false;
   if (!hasValue(theform.news))
      noError = false;
   if (!noError) {
      if (theError)
         writeError(theError);
      else
         writeError("*Please fill in all required fields.");
   }
   return noError;
}

//confirm box to delete a user
function deleteUser(user) {
   var answer = confirm("Do you really want to delete this user?");
   if(answer)
      document.location.href="loader.php?deluser=" + user;
}

//confirm box to delete a news article
function deleteNews(news) {
   var answer = confirm("Do you really want to delete this news article?");
   if(answer)
      document.location.href="loader.php?delnews=" + news;
}
//confirm box to delete a pdf file
function deletePDF(pdf) {
   var answer = confirm("Do you really want to delete this pdf file?");
   if(answer)
        ajaxDel(pdf);

}


//confirm box to delete a symposium participant
function deleteConf(conf) {
   var answer = confirm("Do you really want to delete this participant?");
   if(answer)
      document.location.href="loader.php?delconf=" + conf;
}

//confirm box to add a end_date to a biofix which will show that is is deleted
//for this time period.
function deletePest(pest_sp_id) {
   var answer = confirm("Do you really want to delete this pest?");
   if(answer)
      document.location.href="loader.php?pest_sp_id=" + pest_sp_id;
}
