// -- This is to remove Google's Auto form field switching to yellow
// -- Will overide form checker switching fields to grey to indicate an error
if(window.attachEvent)
window.attachEvent("onload",setListeners);

function setListeners(){
inputList = document.getElementsByTagName("INPUT");
for(i=0;i<inputList.length;i++){
  inputList[i].attachEvent("onpropertychange",restoreStyles);
  inputList[i].style.backgroundColor = "";
}
selectList = document.getElementsByTagName("SELECT");
for(i=0;i<selectList.length;i++){
  selectList[i].attachEvent("onpropertychange",restoreStyles);
  selectList[i].style.backgroundColor = "";
}
}

function restoreStyles(){
if(event.srcElement.style.backgroundColor != "")
  event.srcElement.style.backgroundColor = "";
}







function validateEmpty(fld, msg) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = '#D1DAE3'; 
        error = msg + " field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 14)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
	var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function validateDate (day,month,year,msg) {

// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
	var error = "";
	fldDay = day.value;
	fldMonth = month.value;
	fldYear = year.value;
    fldYear = ((!fldYear) ? y2k(today.getYear()):fldYear);
    fldMonth = ((!fldMonth) ? today.getMonth():fldMonth-1);
    if (!fldDay)  error = msg + " is invalid.\n";
    var test = new Date(fldYear,fldMonth,fldDay);
    if ( (y2k(test.getYear()) == fldYear) &&
         (fldMonth == test.getMonth()) &&
         (fldDay == test.getDate()) ) {
		 //
		 day.style.background = 'White';
		 month.style.background = 'White';
		 year.style.background = 'White';
	}
    else {
		day.style.background = 'White';
		month.style.background = 'White';
		year.style.background = 'White';
        error = msg + " is invalid.\n";
	}
	return(error);
}

function validateFormOnSubmitContact(theForm) {
var reason = "";

  reason += validateEmail(theFormContact.email);
  reason += validateEmpty(theFormContact.fullname, "The NAME");
  reason += validateEmpty(theFormContact.enquiry, "The ENQUIRY");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateFormOnSubmitCallback(theForm) {
var reason = "";

  reason += validateEmpty(theFormContact.fullname, "The FULL NAME");
  reason += validateEmpty(theFormContact.telday, "The TELEPHONE NUMBER");
  reason += validateEmpty(theFormContact.calltype, "The NATURE OF CALL");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateFormOnSubmitMember(theForm) {
var reason = "";

  reason += validateEmpty(theFormContact.delegate_firstname, "The DELEGATE FIRST NAME");
  reason += validateEmpty(theFormContact.delegate_surname, "The DELEGATE SURNAME");
  reason += validateEmpty(theFormContact.delegate_passportnumber, "The DELEGATE PASSPORT NUMBER");
  reason += validateDate(theFormContact.delegate_dob_day, theFormContact.delegate_dob_month, theFormContact.delegate_dob_year, "The DELEGATE DATE OF BIRTH");
  reason += validateEmpty(theFormContact.delegate_cob, "The DELEGATE COUNTRY OF BIRTH");
  reason += validateEmpty(theFormContact.delegate_nationality, "The DELEGATE NATIONALITY");
  reason += validateDate(theFormContact.delegate_passportdate_day, theFormContact.delegate_passportdate_month, theFormContact.delegate_passportdate_year, "The DELEGATE PASSPORT DATE OF ISSUE");
  reason += validateDate(theFormContact.delegate_passportexpiry_day, theFormContact.delegate_passportexpiry_month, theFormContact.delegate_passportexpiry_year, "The DELEGATE PASSPORT DATE OF EXPIRY");
  reason += validateEmpty(theFormContact.delegate_passporteissue, "The DELEGATE PASSPORT PLACE OF ISSUE");
  
  reason += validateEmpty(theFormContact.correspondence_address_ln1, "The CORRESPONDENCE ADDRESS");
  reason += validateEmpty(theFormContact.correspondence_address_town, "The CORRESPONDENCE TOWN");
  reason += validateEmpty(theFormContact.correspondence_address_county, "The CORRESPONDENCE COUNTY");
  reason += validateEmpty(theFormContact.correspondence_address_postcode, "The CORRESPONDENCE POSTCODE");
  
  reason += validateEmpty(theFormContact.nextofkin_name, "The NEXT OF KIN NAME");
  reason += validateEmpty(theFormContact.nextofkin_number, "The NEXT OF KIN NUMBER");
  reason += validateEmpty(theFormContact.nextofkin_relationship, "The NEXT OF KIN RELATIONSHIP");
  

  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}




























