/******************************************************************************************
General purpose validation routines for HTML forms.
There are two functions here:
VALIDATEFIELD:

Check the contents of a text box or text area for incorrect characters

IN: field - the control object to be checked
    validationType - choose from:
                     Alpha - allows all letters plus a space character
                     Numeric - allows all numbers plus a space character
                     SpecialAlpha - allows all characters in Aplha plus those passed 
                                    in as the custom parameter
                     SpecialNumeric - allows all characters in Numeric plus those passed
                                      in as the custom parameter
                     Any            - allows any characters
    custom - allows additional illegal characters to be entered (NOTE: use \\ for a backslash character)

USAGE: from the blur (lost focus) event of a control.
       onBlur="validatefield(this, 'Alpha','')" - allows only letters or spaces
       onBlur="validatefield(this, 'SpecialNumeric', '+-*')" - allows numbers, spaces or + or -
                                                               or *

NOTES: the function allows for the fact that a user moving to another box by clicking the mouse
       will trigger an endless loop because the blur event will fire for both boxes. This doesn't
       happen with the TAB key.
       In addition if the page contains a JavaScript array called requiredFields then as the
       focus leaves an empty text box it will be checked to see if it is required or not. Note
       that the VERIFY function will pick up any controls not even entered (in other words not
       triggering a blur event).

VALIDATEFORM:

Check through the required fields on a form before submission

IN:    the name of the form to be checked
USAGE: from the click event of a submit button.
       onSubmit="return validateform(this)"

NOTES: the function requires two arrays on the page being submitted.
requiredFields contains the fields that must have an entry and fieldNames contains friendly names for these fields.
Unfortunately this then means that only one form can be submitted from the same page
 - hopefully this can be dealt with in a later version of the function.
*******************************************************************************************/
//colours for incorrect text boxes or textareas
var invalidcolor = "yellow"
var validcolor = "white"

//trigger to turn off checking if the mouse is used to move between boxes
//otherwise an endless loop is created
var trigger = "yes"

function validatefield(field, validationType, custom) {

if (trigger=="yes"){
if (field.value==""){ //check if this is a required field
var required = false;
if (typeof requiredFields!="undefined"){
for (var i=0; i<requiredFields.length; i++) {
if (requiredFields[i]==field.name){
required=true;
}
}
}
if (required==true){
alert("Invalid Entry!\nThis is a required field");
field.style.backgroundColor= invalidcolor;
trigger="no";
field.focus();
field.select();
   }
else {
field.style.backgroundColor= validcolor;
trigger="yes";
}
}
else {
switch(validationType){

case "Alpha":
var valid = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;

case "Numeric":
var valid = " 0123456789";
break;

case "SpecialAlpha":
var valid = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + custom;
break;

case "SpecialNumeric":
var valid = " 0123456789" + custom;
break;

default:
var valid = "";
}

var ok = "yes";
if (valid!=""){
var temp;
var illegal = "";
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") {
ok = "no";
illegal = illegal + temp;
   }
}
}
if (ok == "no") {
alert("Invalid entry!\nThe following character(s) entered in this box are not permitted!\n" + illegal);
trigger="no";
field.style.backgroundColor= invalidcolor;
field.focus();
field.select();
   }
else {
field.style.backgroundColor= validcolor;
trigger="yes";
}
}
}
else trigger="yes";
}

function validateform(formname) {

var firstField = ""
var themessage = "You are required to complete the following fields: ";
var objType;

//turn off trigger
trigger="no";
for (var i=0; i<requiredFields.length; i++) { //start of outer for

try
{
objType = eval("formname." + requiredFields[i] + ".type");

switch(objType) { //start of switch
case "text":
if (eval("formname." + requiredFields[i] + ".name.substring(0,4) == 'new_'") && eval("formname." + requiredFields[i] + ".value=='Enter new value here or select from list below'")) {
var l = eval("formname." + requiredFields[i] + ".name.length");
var n = "cbo" + eval("formname." + requiredFields[i] + ".name.substring(4, l)");
if (eval("formname." + n + ".value==0")){
if (firstField == "") {
firstField = n;
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + n + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
}
else if (eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;

case "password":
if (eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;

case "select-multiple":
if (eval("formname." + requiredFields[i] + ".value=='0'") | eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;

case "select-one":
if (eval("formname." + requiredFields[i] + ".value=='0'") | eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;

case "textarea":
if (eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;

case "file":
if (eval("formname." + requiredFields[i] + ".value==''")) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
eval("formname." + requiredFields[i] + ".style.backgroundColor= invalidcolor");
eval("formname." + firstField + ".focus()");
}
else {
eval("formname." + requiredFields[i] + ".style.backgroundColor= validcolor");
}
break;
default:

var radioValue = false;
for (var j=0; j<eval("formname." + requiredFields[i] + ".length"); j++) {
if (eval("formname." + requiredFields[i] + "[" + j + "].checked")==true) {
radioValue = true;
}
}
if (radioValue == false) {
if (firstField == "") {
firstField = requiredFields[i];
}
themessage = themessage + " - " + fieldNames[i];
}
}
}

catch(e)
{
	continue;
}
}
//alert if fields are empty and cancel form submit
if (themessage=="You are required to complete the following fields: "){
return true;
}
else{
alert(themessage);

return false;
}
trigger="yes";
}

/******************************************************************************************
CONVERTCASE:

Convert the case of text fields after entry

IN: fieldname - the control object to be checked
    casetype - choose from:
                     Lower - converts all characters in the control to lower case
                     Upper - converts all characters in the control to upper case
                     Proper - converts the text in the control to proper case

USAGE: from the blur (lost focus) event of a control.
       onBlur="convertcase(this, 'Upper')" - text is converted to upper case (eg Postcode field)
       onBlur="convertcase(this, 'Proper')" - text is converted to proper case (eg Name, address etc)

NOTES: The proper case conversion allows for multiline text in a text area control.
       This can be used alongside the validatefield function.
	   onBlur="validatefield(this, 'Alpha','');convertcase(this, 'Proper')"
*******************************************************************************************/
function convertcase(fieldname, casetype) {

var stringvalue = fieldname.value;
var stringcharacter;
var space;

switch (casetype){ //check which case to convert to Lower, Upper or Proper
case "Lower":
fieldname.value=stringvalue.toLowerCase();
break;
case "Upper":
fieldname.value=stringvalue.toUpperCase();
break;
case "Proper":
//get last occurence of a space in the string
var lastspace = stringvalue.lastIndexOf(" ");
var start = 0;
do //loop through converting each character after a space to a capital
{
space=stringvalue.indexOf(" ",start);
if (space!=-1){
stringcharacter=stringvalue.substr(space+1,1);
stringvalue=stringvalue.substring(0,space+1)+stringcharacter.toUpperCase()+stringvalue.substr(space+2);
start=space+1
}
}
while (space<lastspace&space!=-1); //stop after last space
//now repeat for line feeds
lastspace = stringvalue.lastIndexOf("\n");
start = 0;
do
{
space=stringvalue.indexOf("\n",start);
if (space!=-1){
stringcharacter=stringvalue.substr(space+1,1);
stringvalue=stringvalue.substring(0,space+1)+stringcharacter.toUpperCase()+stringvalue.substr(space+2);
start=space+1
}
}
while (space<lastspace&space!=-1);
//convert first letter to upper case
stringcharacter=stringvalue.substr(0,1);
stringvalue=stringcharacter.toUpperCase()+stringvalue.substr(1);
//put new string back into control
fieldname.value=stringvalue;
break;
default:
}
}
