/*
Javascript Error Checking Routine - V 1.3
Created Jan 2004 by Anthony B. Earles of TND Internet Solutions Ltd.
Web: www.tndnet.co.uk
Email: tee@tndnet.co.uk
Updated Feb 2006
Update April 2008 - added "onload" event listeners to remove the need to change the body tag
                    removed the neccessity for "onSubmit" in the form tag
                    added the ability to disable the submit button after clicking to prevent multiple submissions
You may freely reuse this code providing this notice is kept with it and placed within the webpage body or external js file (the more sensible way)

#######################################################################################################
This code can be used on any webform and is designed to check that required information is filled in. A few of the useful features are:

    * Highlighting of required fields by using a different border colour for the text area
    * Highlighting of which field is not filled in if an error pops up
    
To do

    * regexp checking of valid email address format
    * isDDFirstNull function to accept null <option> values in <select> fields

A hidden field is required within the form body called "requiredfields", the values must be comma separated field names -
 eg. <input type="hidden" name="requiredfields" value="name,address,telephone">
 You can use multi-word fieldnames (eg "your query") using "_" as a separator (ie "your_query"). The code will display the name correctly on any error messages. 
 The information provided in "requiredfields" must match the actual field names used on the form.

#######################################################################################################

1) Setup Visual Effects
   These are boolean values
   showReqOnline = true/false - highlight the required text field borders
   showErrorHighlight = true/false - if a required field is not filled in then change the background colour to "highLightError"
                     as well as displaying the error message. 
   showAlerts = true/false - display pop-up alert windows.
   disableButton = true/false - disables the "submit" button to prevent it being clicked twice before the form is processed.
                                It is enabled again if data has to be resubmitted.


2) Setup Variables

   requiredFieldColor - set this to the hex RGB value of the border color for the required field
   highLightError - RGB value of the field background if it's required and left blank and showErrorHighlight=true
   isDDFirstNull - set to TRUE if the first value of any dropdowns is blank or NULL (Default = false) - NOTE: not yet implemented
   checkEmailFormat - set to TRUE if you want to verify the user's email as being in a correct format (it cannot verify whether or
              not an email address itself is valid). - NOTE: not yet implemented 

*/

// 1) setup visual effects

var showReqOnline = false;
var showErrorHighlight = false;
var showAlerts = true;
var disableButton = true;


// 2) setup variables

var requiredFieldColor = "#cc0000";
var highLightError ="#dddddd";
var isDDFirstNull = false;
var checkEmailFormat = false;

//do not change anything below this line, it WILL affect functionality

function SetReq() {
var objForm = document.getElementsByTagName('form');
for (iCounter=0; iCounter<objForm.length; iCounter++) { 
objForm[iCounter].onsubmit = function(){return CheckInfo(this);}

if (showReqOnline) {
with (objForm[iCounter]) {
var reqF = requiredfields.value.split(",");
for (a=0;a<reqF.length;a++) {
var checkF = eval(reqF[a]);
checkF.style.borderColor=requiredFieldColor;
}}}}}//end all

function CheckInfo(w) {
if (disableButton) {//switch of the submit button so it can't be pressed twice by accident (if the "disableButton" variable is set to "true).
buttonOff();
}
//extract required fields data into an array
var reqF = w.requiredfields.value.split(",");
for (i=0;i<reqF.length;i++) {
var typestr = eval("w."+reqF[i]+".type");//extract field type
var checkstr = eval("w."+reqF[i]+".value");
if (!checkstr) {
var a=ConvertCase(reqF[i]);
var message= "Please fill in the \""+a+"\" field";
if (showAlerts) {alert(message);}
var focusfield = eval("w."+reqF[i]);
focusfield.style.backgroundColor=(showErrorHighlight) ? highLightError : focusfield.style.backgroundColor;
focusfield.focus();
buttonOn();
return false;
}//end if
}//end for
return true;
}//end function

function ConvertCase(c) {
var lettlen = c.length;
var newstr = c.substring(1,lettlen);
var lett=c.substring(0,1);//extract first letter
var lett=lett.toUpperCase();//convert to capital letter
var newWord = lett+newstr;
//split multiword fieldnames at "_";
var newWord = newWord.replace("_"," ");
return newWord;
}//end function

function buttonOff() {
//add code here to iterate the form and select the element by type then disable it
document.getElementById('submit').disabled = true;

}
 
function buttonOn() {
//add code here to iterate the form and select the element by type then enable it
document.getElementById('submit').disabled = false;
}
//add event listener for when the page loads and set it to trigger subevents related to the form
window.addEventListener?window.addEventListener("load",SetReq,false):window.attachEvent("onload",SetReq);
/*
End of checking routine
*/

