// JavaScript Document
function validateFormOnSubmit(theForm) 
{
//checks to see if everything was submitted. If not, concatenates a string to display in a popup box.
//back end validation will stop the user if javascript is disabled
//if javascript is disabled, uploads will not be saved on a refresh (i.e. unsuccesful submit)
  var popup="You did not complete all the required fields. Please correct the errors below:\n";
  var reasonName ="";
  var reasonPhone="";
  var reasonBody="";
  var reasonCap="";
  var reasonChecks="";
  var ia="";
  var ib="";
  var ic="";
  reasonName = validateEmpty(theForm.name, "n");
  reasonPhone = validateEmpty(theForm.telephoneNum, "tel");
  reasonBody= validateEmpty(theForm.news_body, "bod");
  reasonChecks= checkCheckBoxes();   
  reasonEmail=validateEmail(theForm.email);
  reasonCap=validateEmpty(theForm.recaptcha_response_field, "recap");
  
  if(reasonChecks!="")
   popup+=reasonChecks;
  if (reasonBody!="")
   popup+=("    A news item body " + reasonBody);
  if (reasonName != "") 
   popup+=("    A name " + reasonName);
  if (reasonPhone!="")
   popup+=("    A telephone number " + reasonPhone);
  if (reasonEmail!="")
   popup+=reasonEmail;
  if(reasonCap!="")
   popup+=("    Validation text " + reasonCap);
  
  if(theForm.img1.value!="")
  {	
	ia=checkFiles(theForm.img1.value, "ia");
  
    if(ia!="")
  		popup+=ia;
  }
 
  if(theForm.img2.value!="")
  {	
	ib=checkFiles(theForm.img2.value, "ib");
  
    if(ib!="")
  		popup+=ib;
  }
 
  if(theForm.img3.value!="")
  {	
	ic=checkFiles(theForm.img3.value, "ic");
  
    if(ic!="")
  		popup+=ic;
  }
 
  if (popup != "You did not complete all the required fields. Please correct the errors below:\n") {
  	alert(popup);
	return false;
  }
  else
  {
  	document.getElementById("submit").style.display = "none";
	document.getElementById("submitprogress").style.display = "block";
  }
  return true;
}//end validateFormOnSubmit(theForm) 

	function validateEmpty(fld, id) 
	{
		var error = "";
	 
		if (fld.value == "") {
			document.getElementById(id).style.display = "block";
			fld.style.border="3px solid red";
			error="is required.\n";
			
		} 
		else
		{
			document.getElementById(id).style.display = "none";
			fld.style.border="2px solid #a1b594";
		}
	   return error;
	}//end validateEmpty

	function checkCheckBoxes() 
	{
	var error = "";
	//making sure at least one element was checked
	if (!document.getElementById('C0').checked && !document.getElementById('C1').checked 
	&& !document.getElementById('C2').checked	&& !document.getElementById('C3').checked 
	&& !document.getElementById('C4').checked  && !document.getElementById('C5').checked 
	&& !document.getElementById('C6').checked  && !document.getElementById('C7').checked 
	&& !document.getElementById('C8').checked)
			{
			error="    At least one category selection is required.\n";
			document.getElementById('check').style.display = "block";
			}
			else
				document.getElementById('check').style.display = "none";
		return error;
	}//end checkCheckBoxes

	function trim(s)
	{
	  return s.replace(/^\s+|\s+$/, '');
	} //end trim
	
	function validateEmail(fld) 
	{
		var error="";
		var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
  
		if (fld.value == "") {
			error = "    Please enter an email address.\n";
			document.getElementById('em').style.display = "block";
			fld.style.border="3px solid red";
		} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
			error = "    Please enter a valid email address.\n";
			document.getElementById('em').style.display = "block";
			fld.style.border="3px solid red";
		}  else {
			document.getElementById('em').style.display = "none";
			fld.style.border="2px solid #a1b594";
		}
		return error;
	}
	function checkFiles(data, id)
	{
            var error="";
			data = trim(data); //trims string

            if (data.match(/([^\/\\]+)\.(jpg|png|bmp|gif)$/i) )
            {
			    document.getElementById(id).style.display = "none";
				return error;
            }
			else
            {
				error="    You are trying to submit an unacceptable image type.\n";
				document.getElementById(id).style.display = "block";
				return error;
			}
    }
	
	function showCaptcha(){
		document.getElementById('help_elm').style.display="block";
	}
