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


  reason += validateEmail(theForm.email);
  reason += validatePassword(theForm.password);   
  reason += validateEmpty(theForm.first_name);
  reason += validateEmpty(theForm.surname);
  reason += validateDate(theForm.dob);
  reason += validateEmpty(theForm.address_ln1);
  reason += validateEmpty(theForm.town);
  reason += validatePostcode(theForm.postcode);  
      
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}
function validateNewPassword(theForm) {
var reason = "";

	reason += validatePassword(theForm.new_pw);  
	reason += validatePassword(theForm.password);
	reason += comparePasswords(theForm.password, theForm.new_pw);

  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}

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

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

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#FFDFDF';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 8) || (fld.value.length > 16)) {
        error = "Your password must be between 8 and 16 characters long and contain only letters and numbers. \n";
        fld.style.background = '#FFDFDF';
    } else if (illegalChars.test(fld.value)) {
        error = "Your password must be between 8 and 16 characters long and contain only letters and numbers.\n";
        fld.style.background = '#FFDFDF';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "Your password must contain at least one number.\n";
        fld.style.background = '#FFDFDF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   
function comparePasswords(fld, fld2) {
	var error = "";
	
	if (fld.value !== fld2.value) {
        fld.style.background = '#FFDFDF';
		fld2.style.background = '#FFDFDF';
        error = "Passwords don't match";
	} else {
		fld.style.background = 'White';
		fld2.style.background = 'White';
	}
	return error;
}

function validatePostcode(fld) {
	var error = "";
	var illegalChars = /[\W_]/;
	var fld = trim(fld.value);   
	
	if (fld.value == "") {
		fld.style.background = '#FFDFDF';
		error = "You didn't enter a postcode.\n";
	} else if ((fld.value.length < 6) || (fld.value.length > 8)) {
		fld.style.background = '#FFDFDF';
		error = "Your postcode must be between 6 and 8 characters long and contain only letters and numbers. \n";
	} else if (illegalChars.test(fld.value)) {
		fld.style.background = '#FFDFDF';
		error = "Your postcode must be between 6 and 8 characters long and contain only letters and numbers. \n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function validateDate(fld){
	
	var error="";	
	var fldStr = fld.value;
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	


	var daysInMonth = DaysArray(12)

	var pos1=fldStr.indexOf(dtCh)
	
	var pos2=fldStr.indexOf(dtCh,pos1+1)
	var strDay=fldStr.substring(0,pos1)
	var strMonth=fldStr.substring(pos1+1,pos2)
	var strYear=fldStr.substring(pos2+1)
	strYr=strYear	

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){
		fld.style.background = '#FFDFDF';
		error = "The date format should be : dd/mm/yyyy.\n";
	}else if (strMonth.length<1 || month<1 || month>12){
		fld.style.background = '#FFDFDF';
		error = "Please enter a valid month.\n";
	}else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		fld.style.background = '#FFDFDF';
		error = "Please enter a valid day.\n";
	}else if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		fld.style.background = '#FFDFDF';
		error = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+".\n";
	}else if (fldStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(fldStr, dtCh))==false){
		fld.style.background = '#FFDFDF';
		error = "Please enter a valid date.\n";

	}else {
		fld.style.background = '#FFFFFF';
	}
return error;
}