function validateFormOnSubmit(theForm) {
	var reason = "";

  reason += validateEmpty(theForm.last_name, "Last Name");
  reason += validateEmpty(theForm.first_name, "First Name");
  reason += validateEmpty(theForm.addr_line_1, "Address line 1");
  reason += validateEmpty(theForm.city, "City");
  reason += validateEmpty(theForm.province, "Province");
  reason += validateEmpty(theForm.postcode, "Postcode");
  reason += validatePassword(theForm.pass1,7);
  reason += validatePassword(theForm.pass2,7);
  if (theForm.pass1.value != theForm.pass2.value)
	reason += "Passwords do not match. \n";
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone_office, "Work");
  reason += validatePhone(theForm.phone_cell, "Cell");
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
	}
  else {
	theForm.pass1.value = hex_md5(theForm.pass1.value);
	theForm.pass2.value = hex_md5(theForm.pass2.value);
  }

  return true;
}

function validateRegFormOnSubmit(theForm) {
	var reason = "";

  reason += validateEmpty(theForm.last_name,  "Last Name");
  reason += validateEmpty(theForm.first_name, "First Name");
  reason += validateEmpty(theForm.adate,      "Arrival Date");
  reason += validateEmpty(theForm.ddate,      "Departure Date");

  if (!isInteger(theForm.num_adults.value)){
	reason += "Number of adults is not a valid number.\n";
	theForm.num_adults.style.background = 'Yellow';
  }
  reason += validateEmpty(theForm.num_adults, "Number of adults");
  
  if (!isInteger(theForm.num_kids.value)) {
	reason += "Number of children is not a valid number.\n";
	theForm.num_kids.style.background = 'Yellow';
  }
  reason += validateEmpty(theForm.addr_line_1, "Address");
  reason += validateEmpty(theForm.city,        "City");
  reason += validateEmpty(theForm.province,    "Province");
  reason += validateEmpty(theForm.postcode,    "Postcode");

  if (!isInteger(theForm.veg.value)){
	reason += "Number of vegetarians is not a valid number.\n";
	theForm.veg.style.background = 'Yellow';
  }

  if (!isInteger(theForm.non_veg.value)){
	reason += "Number of non-vegetarians is not a valid number.\n";
	theForm.non_veg.style.background = 'Yellow';
  }

var rdo1 = document.form1.elements.memb_yn;
var chkd1 = false;
for (var i=0; i<rdo1.length; i++){
	if(rdo1[i].checked){
		chkd1 = true;
	}
}
  if (!chkd1)
    reason += "Select if you are a member.\n";

var rdo = document.form1.elements.new_memb;
var chkd = false;
for (var i=0; i<rdo.length; i++){
	if(rdo[i].checked){
		chkd = true;
	}
}
  if (!chkd)
    reason += "Select if you are a new member.\n";

  if (theForm.email.type != 'hidden')
     reason += validateEmail(theForm.email);
	 
  reason += validatePhone(theForm.phone_office, "Work");
  reason += validatePhone(theForm.phone_cell, "Cell");

  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
	}

  return true;
}

function validateLoginForm(theForm) {
	var reason = "";
	reason += validateEmail(theForm.email);
	reason += validatePassword(theForm.pwd, 3);
	
	if (reason != "") {
		alert("Some fields need correction:\n\n" + reason);
		return false;
	}
	else{
		theForm.pwd.value = hex_md5(theForm.pwd.value);
	}
	return true;
}

function validateForgotForm(theForm) {
	var reason = "";
	reason += validateEmail(theForm.email);
	
	if (reason != "") {
		alert("Some fields need correction:\n\n" + reason);
		return false;
	}
	return true;
}

function validateCPwdForm(theForm) {
	var reason = "";

	reason += validateEmail(theForm.email);
	reason += validatePassword(theForm.pwd, 3);
	reason += validatePassword(theForm.newpwd1, 7);
	reason += validatePassword(theForm.newpwd2, 7);
	if (theForm.newpwd1.value != theForm.newpwd2.value)
		reason += "Passwords do not match. \n";

	if (reason != "") {
		alert("Some fields need correction:\n\n" + reason);
		return false;
	}
	else {
		theForm.pwd.value = hex_md5(theForm.pwd.value);
		theForm.newpwd1.value = hex_md5(theForm.newpwd1.value);
		theForm.newpwd2.value = hex_md5(theForm.newpwd2.value);
	}
	return true;
}

function validateContactFormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.First_Name, "First Name");
	reason += validateEmpty(theForm.Last_Name, "Last Name");
	reason += validateEmail(theForm.Email);
	reason += validateEmpty(theForm.Comments, "Comments");

	if (reason != "") {
		alert("Some fields need correction:\n\n" + reason);
		return false;
	}
}


function validateEmpty(fld, fname) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = fname+" cannot be empty.\n"
    }
    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 < 7) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "Username must be between 7 and 15 letters.\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 validatePassword(fld, siz) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < siz) || (fld.value.length > 15)) {
        error = "Password must be between 7 and 15 letters. \n";
        fld.style.background = 'Yellow';
//    } else if (illegalChars.test(fld.value)) {
    } else if (fld.value.match(illegalChars)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } 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";
    }
    return error;
}

function validatePhone(fld, fname) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a " + fname + " phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The " + fname + " phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The " + fname + " phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

function initCap(str) {
var str = str.substring(0,1).toUpperCase() + str.substring(1,str.length).toLowerCase();

return str;
}

function isNumber(str) {
var IsFound = /^-?\d+$/.test(NumberToTest);
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

