var okColour = "#9b9d98";
var standardColour = "#9b9d98";
var okWhiteColour = "#FFFFFF";
var errorColour = "#FF0000";
var errColour = "#FF0000";
var test;

function preloadSpacer(){

 
  spacerIMG = new Image();
  spacerIMG.src = "/images/spacer.gif";

  //We also load the cursor of the menu
  menuBulletImg = new Image();
  menuBulletImg = "/images/menuBullet.jpg";

}

function getObject(name){


  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }


}


function isObjectVisible(objectName){
//Returns true or false using the Dixtsplay CSS attribute to determine if an object is visible or not
    var curObj = new getObject(objectName);
  
    return curObj.style.display != "none";
}


function changeTextColour(objectName,newColour){

    var curObj = new getObject(objectName);
    curObj.style.color = newColour;

}


   function checkCompulsoryTextFields(frmName){
    
        var frm = document.forms[frmName];
        
        var currentFieldValue;
        
        
            for (var i=0;i<textFieldLabels.length;i++){
                currentFieldValue = frm.elements[textFieldLabels[i]].value;
                
                    if(isEmpty(currentFieldValue) || isWhitespace(currentFieldValue)){
                        textFieldErrors[i] = true;
                        emptyFieldsExist = true;
                        changeTextColour(textFieldLabels[i],errorColour);
                    }
                    else {
                        textFieldErrors[i] = false;
                        changeTextColour(textFieldLabels[i],okColour);
                    }
            }
            
            
            
         
    
    }
    
    
    
    function checkCompulsorySelectFields(frmName){
        
         var frm = document.forms[frmName];
    
          var selectedIndex; 
          var selectedValue; 
          
            for (var i=0;i<selectFieldLabels.length;i++){
            
                selectedIndex = frm.elements[selectFieldLabels[i]].selectedIndex;
                selectedValue = frm.elements[selectFieldLabels[i]].options[selectedIndex].value;
                               
                    if(selectedValue == 0){
                        selectFieldErrors[i] = true;
                        emptyFieldsExist = true;
                        changeTextColour(selectFieldLabels[i],errorColour);
                    }
                    else {
                        selectFieldErrors[i] = false;
                        changeTextColour(selectFieldLabels[i],okColour);
                    }
            }
            
            
            
         
    }

    function checkEmailFields(frmName){

         var frm = document.forms[frmName];

         var currentFieldValue;
        
        
            for (var i=0;i<emailFieldLabels.length;i++){
            	
                currentFieldValue = frm.elements[emailFieldLabels[i]].value;
                
                    if(!emailCheck(currentFieldValue,true) ||!isEmail(currentFieldValue,true)){
                        emailFieldErrors[i] = true;
                        emailErrors = true;
                        changeTextColour(emailFieldLabels[i],errorColour);
                    }
                    else {
                        emailFieldErrors[i] = false;
                        changeTextColour(emailFieldLabels[i],okColour);
                    }
            }
            

    }




function checkAlphabetic(formName){
//This function checks if alphabetic fields were  filled in correclty, i.e. with 
//capital or lower case arguments only. If some of the alphabetic fields were not it gives an
// error message and returns false otherwise it returns true.  The first argument is the form name
// on the document and the rest  of the arguments are alphabetic fields' names.  
//At least one alphabetic field should exist (otherwise there is no point to call this function).

//The n (n odd, so that n-1 is even) arguments to be passed are as follows:
//argument 0: form name
//arguments 1 to (n-1)/2: field names
//arguments (n-1)/2 + 1 to n-1: field labels

var errorFields = "";
var noFields  = (arguments.length  - 1)/2;



	for (var curFieldIndex = 1; curFieldIndex <= noFields;curFieldIndex++) {
		
		if (!isAlphabetic(document.forms[formName].elements[arguments[curFieldIndex]].value,true)) {
			changeTextColour(arguments[curFieldIndex],errColour);
			errorFields=errorFields + "\n   _ " + arguments[curFieldIndex + noFields];
		}
		else {
			changeTextColour(arguments[curFieldIndex],standardColour );
		}
	}


	if (errorFields != "") {
		alert("The following fields should only contain alphabetic characters, i.e. not numerical digits or other characters, but they do so:" +errorFields + ".\n\nPlease correct before you can proceed.");
		return false;
	}
	else {
		return true;
	}


}


function checkAlphabeticPlusOther(formName,otherValidChars){
//Works similar to checkAlphabetic with the addition of one argument

var errorFields = "";
var noFields  = (arguments.length  - 2)/2;



	for (var curFieldIndex = 2; curFieldIndex <= noFields+1;curFieldIndex++) {
		if (!isAlphabeticPlusOther(document.forms[formName].elements[arguments[curFieldIndex]].value,otherValidChars,true)) {
			changeTextColour(arguments[curFieldIndex],errColour);
			errorFields=errorFields + "\n   _ " + arguments[curFieldIndex + noFields];
		}
		else {
			changeTextColour(arguments[curFieldIndex],standardColour );
		}

	}


	if (errorFields != "") {
		alert("The following fields can only contain letters and space, dash (-), dot (.) and single quote ('):" +errorFields + ".\n\nPlease correct before you can proceed.");
		return false;
	}
	else {
		return true;
	}


}

function checkAlphanumericPlusOther(formName,otherValidChars){
//Works similar to checkAlphabetic with the addition of one argument

var errorFields = "";
var noFields  = (arguments.length  - 2)/2;



	for (var curFieldIndex = 2; curFieldIndex <= noFields+1;curFieldIndex++) {
		if (!isAlphaNumericPlusOther(document.forms[formName].elements[arguments[curFieldIndex]].value,otherValidChars,true)) {
			changeTextColour(arguments[curFieldIndex],errColour);
			errorFields=errorFields + "\n   _ " + arguments[curFieldIndex + noFields];
		}
		else {
			changeTextColour(arguments[curFieldIndex],standardColour );
		}

	}


	if (errorFields != "") {
		alert("The following fields can only contain alphanumeric characters, comma (,), dot (.), dash (-), space ( ) and the And sign (&):" +errorFields + ".\n\nPlease correct before you can proceed.");
		return false;
	}
	else {
		return true;
	}


}


function checkNumericPlusOther(formName,otherValidChars){
//Works similar to checkAlphabetic with the addition of one argument

var errorFields = "";
var noFields  = (arguments.length  - 2)/2;


	for (var curFieldIndex = 2; curFieldIndex <= noFields+1;curFieldIndex++) {
		if (!isNumericPlusOther(document.forms[formName].elements[arguments[curFieldIndex]].value,otherValidChars,true)) {
			changeTextColour(arguments[curFieldIndex],errColour);
			errorFields=errorFields + "\n   _ " + arguments[curFieldIndex + noFields];
		}
		else {
			changeTextColour(arguments[curFieldIndex],standardColour );
		}

	}


	if (errorFields != "") {
		alert("The following fields can only contain numeric characters, the Plus sign (+) and dash (-):" +errorFields + ".\n\nPlease correct before you can proceed.");
		return false;
	}
	else {
		return true;
	}


}

function checkAlphanumeric(formName){
//Works similar to checkAlphabetic

var errorFields = "";
var noFields  = (arguments.length  - 1)/2;



	for (var curFieldIndex = 1; curFieldIndex <= noFields;curFieldIndex++) {
		if (!isAlphanumeric(document.forms[formName].elements[arguments[curFieldIndex]].value,true)) {
			changeTextColour(arguments[curFieldIndex],errColour);
			errorFields=errorFields + "\n   _ " + arguments[curFieldIndex + noFields];
		}
		else {
			changeTextColour(arguments[curFieldIndex],standardColour );
		}

	}


	if (errorFields != "") {
		alert("The following fields can only contain alphanumeric characters:" +errorFields + ".\n\nPlease correct before you can proceed.");
		return false;
	}
	else {
		return true;
	}


}
