// JavaScript Document
//function to reset a form
// call looks like: <a href="javascript:resetForm('content','common','flag1,flag2')"> -- !!no spaces in flag argument, necessary for hidden fields!!
function resetForm(lyrID,formID,flags){
	if (flags != null)
	{
       var flagsArray = flags.split(',');
	   var numOfFlags = flagsArray.length;
	}

	   var formName = eval("document."+formID);                                                              

	   var formLength = formName.elements.length;
	   for(var i=0; i<formLength; i++){
	      var resetField = true;
	      var required = formName.elements[i]; 
          if(required.type != "hidden"){ // hidden fields are not reset.
		     for(var x=0; x<numOfFlags; x++){ //iterates through the flags and doesnt reset fields passed as flags
			    var tempFlag = flagsArray[x];
				if(tempFlag == required.name){
				   var resetField = false;
				}
			 }
			 if(resetField == true){				
			    if(required.type == "text"){ // tests for a text field    
		           required.value = "";
                }	
	            if(required.type == "textarea"){ // tests for a text area
		           required.value = "";
                }	
                if(required.type == "password"){ // tests for a password
		           required.value = "";
                }	
                if(required.type == "checkbox"){ // tests for a checkbox
		           required.checked = false;
                }	
                if(required.type == "select-one"){//test for a select box
			       required.selectedIndex = required[0].value;
                }
                if(required.type == "select-multiple"){//test for a select box
			       required.selectedIndex = required[0].value;
                }
                if (required.type == "radio"){ //test for radio groups
		           required.checked = false;
                }				  
	         }
	     }
	  }
	 for(var i=0; i<formLength; i++){ //places focus in the first non-visible field found
	   if(formName.elements[i].type != "hidden"){
	     formName.elements[i].focus();
	     break;
	   }
	 }	
}

// function call in this format: 
// <textarea name=message cols=55 rows=20 onKeyDown="textCounter(this.form.message,this.form.remLen,2000);" onKeyUp="textCounter(this.form.message,this.form.remLen,2000);" wrap="OFF"></textarea>
function textCounter(field, countfield, maxlimit) {

if (field.value.length > maxlimit){ // if too long...trim it!
//alert("Warning: You have exceeded the "+maxlimit+" character limit.")
var errorStr = "<ul><li class='red'>You have exceeded the "+maxlimit+" character limit.</li></ul>";
errorBuilder(errorStr, "errorDisplay");
field.value = field.value.substring(0, maxlimit);
}
else // otherwise, update 'characters left' counter
countfield.value = maxlimit - field.value.length;
}
 
  function checkEMail(field){
	var str = field.value;
	var strLen = str.length;
	if (isEmpty(str)) return true;
	var pattern = /^[\w\._-]+@[\w\._-]+(\.\w{2,4})$/; //(2,4) indicates min,max range of length of extension
	var result = pattern.test(str);
	  for(var i=0; i<strLen; i++){
	    var tempFlag = str.charAt(i); 	    
	    if(tempFlag == '.' || tempFlag =='@'){
		  if(i==0){ //throws an error if e-mail address begins with a .
		    result = false;	
		  }	  
	      if(str.charAt(i-1)=='.'){ // throws an error if the char before is a .
	        result = false;
	      }
	      if(str.charAt(i+1)=='.'){ // throws an error if the char after is a .
	        result = false;
	      }
		}
      }	 	
	  return result;
  }
	
  // Validate a field with alphanumeric only, no special characters
  function checkAlphaNumeric(field){
  	  <!-- var pattern = /^\w*$/; -->
  	  var pattern = /[^a-zA-Z0-9]/;
  	  if (pattern.test(field)){
  		  return (false);
  	  }
  	  return(true);
  }
   // Checks string to ensure that data exists. Also checks to ensure data presented is not just spaces
   function isEmpty(str){
	   if (str == null){
		   return false;
	   }
	   for (var intLoop = 0; intLoop < str.length; intLoop++){
		   if (" " != str.charAt(intLoop)){
			   return false;		    
		   }
	   }
	   return true;
   }
   
   // checks the loop hole in isEmpty() where a carriage return is accepted as data entry in a text area
   function isLineBreak(str){ 
	 if (str == null){
		return false;
	 }
	 for (var i = 0; i < str.length; i++){
	    if (" " != str.charAt(i)){
            if ((str.charCodeAt(i) != 10) && (str.charCodeAt(i) != 13)){			
			    return false;		    
		    }	
	    }
	 }
	   return true;
   }	

   // compares two strings and returns true if they are the same, false if they are different
   // call should be: if (stringCompare(mainForm.field1.value,mainForm.field2.value))
   function stringCompare(str1, str2){ 
	   if (str1 == str2){
		   return true;
	   }else	
	     return false;
   }	
   
   // Validate a field with alphanumeric only, no special characters
   function checkAlphaNumericPhoneNO(field){
     var alphaFlag = false;
     var numericFlag = false;
     var str = field;
     var strLen = str.length;
     var AlphaPattern = /^[A-Za-z]+$/;
       for(var i=0; i<strLen; i++){
   	  var tempVal = str.charAt(i);
   	  if(tempVal == " "){
   	      return(false);;
   	  }	
   	  var alphaResult = AlphaPattern.test(tempVal);
   	  var numericResult =! (isNaN(tempVal));
   	    if(alphaResult != false){
   		  var alphaFlag = true;
   		}
   		else if(numericResult != false){
   		  var numericFlag = true;
   		}
   		else{
   	      return(false);
   		}
   	} //for loop	
   	return true;	
   }

	
 function validateForm(mainForm){
   var valid = true;
   var errorStr = "<ul>";
   var errorFlag = "false";
   var email = mainForm.email.value;
   var name = mainForm.contactName.value;
   var emailAddy = mainForm.contactEmail;
   var inquiry = mainForm.contactInquiry;
   var contType = mainForm.contactType.options[mainForm.contactType.selectedIndex].value;
   
   if(contType == "missions"){
	   mainForm.recipient.value="missions@centerbaptist.com";
   }else if(contType == "reelmen"){
	   mainForm.recipient.value="reelMen@centerbaptist.com";
   }else if(contType == "vbs"){
	   mainForm.recipient.value="vbs@centerbaptist.com";
   }else if(contType == "pastor"){
	   mainForm.recipient.value="pastormichael@centerbaptist.com";
   }else if(contType == "webmaster"){
	  mainForm.recipient.value="webmaster@centerbaptist.com";
   }else{
	   mainForm.recipient.value="pastormichael@centerbaptist.com";
   }

	 //validates for name
	 if(isEmpty(name)){
	   errorStr = errorStr + "<li class='red'>Please enter your Name.</li>";
		 errorFlag = "true";
	 }
	 
	 //validates for email
	 valid = checkEMail(emailAddy);
     if(isEmpty(emailAddy.value)){
	   errorStr = errorStr + "<li class='red'>Please enter your E-Mail Address.</li>";
	   errorFlag = "true";
	 }else if (valid != true && errorFlag == "false") {
	   errorStr = errorStr + "<li class='red'>Please make sure your email address is in the correct format (username@domainname.com).</li>";
		 errorFlag = "true";
     }
	 
     //validate Inquiry
	 if(isEmpty(inquiry.value)){
	   errorStr = errorStr + "<li class='red'>Please fill in the Inquiry field.</li>";
	   errorFlag = "true";
     } 
     else if(isLineBreak(inquiry.value)){
	   errorStr = errorStr + "<li class='red'>Please fill in the Inquiry field.</li>";
	   errorFlag = "true";
     }
	 
	 //validate Contact Type
     if(contType == "0"){
	   errorStr = errorStr + "<li class='red'>Please indicate type of Contact</li>";
	   errorFlag = "true";
     }
		
		
	 //if an error is found fire error display function, else return true;
	 if(errorFlag == "true"){
	   errorStr = errorStr + "</ul>";
		 errorBuilder(errorStr, "errorDisplay");
		 window.location.href="#error";
		 return false
	 }else{
	   return true;
	 }	   	 
 }

 function errorBuilder(errorStr,errorID){
   var errorCell = errorID;
   var errorFeed = document.getElementById(errorID); 
   var tStr="";
   tStr+='<table width="450" border="0" cellspacing="0" cellpadding="0">\n';
   tStr+='<tr><td class="redLarge" style="padding-top:10px;">Your submission has the following errors:</td>\n';
   tStr+='</tr><tr><td>\n';
   tStr+=errorStr+'\n';
   tStr+='</td></tr></table>\n';
   errorFeed.innerHTML=tStr;
 }