var ci;                        // loop index for error and validity checking
var currentElem;                         // element currently being checked
var errorBox;                            // where messages a displayed (and erased)
var strT="";
var strC="";
var strM="";
var saveDetails = false;
var fieldNames = [];
var contactForm;
var pN=0;

function formOnLoad() {
         contactForm = document.details;                           // cache form 'element'
         errorBox = document.getElementById("msgText");            // cache error/message box element
		 
// 1     Get field names and read cookies
         k=0;
	     for (var i=0; i<contactForm.elements.length; i++) {
		    var elem = contactForm.elements[i];
			n = elem.id.match(/[\d]/)[0];
	        switch (elem.type) {
	           case "text":       
			   case "textarea":
				  elem.onkeyup = clearThisField;
			   case "checkbox":  
				  fieldNames[n] = elem.name;
   				  document.getElementById("pf"+n).innerHTML = elem.name.unCamelCap();
				  break;
					 
			   case "reset":
			      elem.onclick = clearBoxes;
			   case "button": 
   				  document.getElementById("bf"+n).innerHTML = elem.name.unCamelCap();
			  
			   case "submit":
				  elem.onmouseover = doMouseIn;
			      elem.onmouseout = doMouseOut; 
				  elem.onmousedown = doMouseDown;
				  elem.onmouseup = doMouseUp; 
		   }
		} 
	     strC = document.cookie;
		 saveDetails = (getCookie("SaveD") == "Yes");
		 if (!saveDetails) return;
		 
	     var x = fieldNames.length;
		 var N = (pN == 0)? x-2 : x-1;
		 for (i=1; i<=N; i++) {
		    var val = getCookie(fieldNames[i]);
		    document.getElementById("ip"+i).value = (val==null)? "" : val; 
		 }
		 contactForm.RememberMe.checked = saveDetails;
//		 strM = getCookie(fieldNames[i]);
		 if (pN == 0) {
		   errorBox.innerHTML = "";
		 }

}

onunload = function() {
		 setCookie("SaveD",((saveDetails)? "Yes" : "No"),30);  
         for (var i=1; i<=5; i++) 
		   setCookie (fieldNames[i], ((saveDetails)? document.getElementById("ip"+i).value : " "), 30);
}

function setData(v) {                                             // included for test purposes
	 var contactForm = document.forms[0];
		 for (var i=0; i<v.length; i++)
		   contactForm.elements[i].value = v[i];
}
function $(id) { return document.getElementById(id); }
function doMouseIn()   { $(this.id).style.setAttribute ("backgroundPosition","0 -24px"); }
function doMouseOut()  { $(this.id).style.setAttribute ("backgroundPosition","0 0");     }
function doMouseDown() { $(this.id).style.setAttribute ("backgroundPosition","0 -48px"); }
function doMouseUp()   { $(this.id).style.setAttribute ("backgroundPosition","0 -24px"); }

function clearMessage() { contactForm.Message.value = ""; }

var flpe = ["Last Name", "First Name", "Phone Number", "Email Address", "Message"];
var reg  = [/(\w|\W)(\W+)/, /(\w|\W)(\W+)/, /^[\d]{3}.{3,}/, /(\w+\.)*\w+@(\w+\.)+\w+/, /.*/];

// Each regexp is used to test the input.
// TorF represents the test condition for an error in the input

var TorF = [true,true,false,false,false];

function validateFields() {
     var msgElem = document.getElementById("msgText");                              
	 var numberOfInputs = flpe.length;
	     strT = "";
     
         for (ci=1; ci<=fieldNames.length-1; ci++) {
           inputElem = document.getElementById("ip"+ci);                           // cache input box/textarea
           strT = inputElem.value.trim();                                          // get text and trim
		   
    	   if (strT.length == 0) {                                                 // if no data entered ...
	 	     errorBox.style.color = "red";
		     errorBox.style.fontSize = "11px";
		     msgElem.innerHTML = "Please enter your <br /><span class='e'>" +fieldNames[ci].unCamelCap()+ "</span>"; 
             inputElem.style.backgroundColor = "#ffffd0";
			 return false;
		   }
		   
		   if (reg[ci-1].test(strT) == TorF[ci-1]) {                               // in invalid characters entered ...
	 	     errorBox.style.color = "red";
		     errorBox.style.fontSize = "11px";
             msgElem.innerHTML = "You entered an invalid <br /><span class='e'>" +fieldNames[ci].unCamelCap()+ "</span>"; 
             inputElem.style.backgroundColor = "#ffd0d0";
 		     return false; 
 		   } 

// condition names (first letter calitalised) and phone numbers (separated by dashes)
		   
		   switch (ci-1) {
			 case 0:
			 case 1:
               inputElem.value = strT.substring(0,1).toUpperCase()+ strT.substring(1,strT.length );      // change first letter to Upper Case
		       break;
			 case 2:
			   inputElem.value = strT.replace(/[\(\)\.\-]/g," ").trim().replace(/[\s]+/g,"-");           // separate digit groups by dashes
			}
         }
     return true;
}

function setState(y) { saveDetails = y; } 

function clearThisField() {
//         if (inputElem.value.length > 1) { return; }               // avoid unnecessary activity
         errorBox.innerHTML = "";                                  // erase message
		 inputElem.style.backgroundColor = "ffffff";               // return background to white
         return false;
}

function clearBoxes() {
         errorBox.innerHTML = "";
         for (var i=1; i<=5; i++) {
		   var box=document.getElementById("ip"+i).style;
		   if (box.backgroundColor != "white") box.backgroundColor = "white";
		 }
		 return true;
}

function thankYou() {
     var elem = document.getElementById('msgText');
	     elem.style.color = "blue";
		 elem.style.fontSize = "13px";
         elem.innerHTML = "Thank You<br /><span class='e'>We will contact you as soon as possible</span>";
		 pN = 1;
}
