function validateForm (theForm) {
	var reasonField = "";

  reasonField += validateEmpty(theForm.contact_name);
  reasonField += validateEmail(theForm.contact_email);
	if (theForm.contact_telephone.value.length != 0) {
	  reasonField += validatePhone(theForm.contact_telephone);
	}
  reasonField += validateDayOfMonth(theForm.contact_doyDay);
      
  if (reasonField != "") {
    alert("Some fields need correction:\n" + reasonField);
    return false;
  }

  return false;
}

function validateResForm (theForm) {
	var reasonField = "";

  reasonField += validateEmpty(theForm.guestName);
  reasonField += validateEmpty(theForm.guestAddress);
  reasonField += validateEmpty(theForm.guestCity);
  reasonField += validateEmpty(theForm.guestProv);
  reasonField += validateEmpty(theForm.guestPostal);
  reasonField += validateEmpty(theForm.guestCountry);
  reasonField += validateEmpty(theForm.guestPhone);
  reasonField += validateEmail(theForm.guestEmail);
	if (theForm.guestPhone.value.length != 0) {
	  reasonField += validatePhone(theForm.guestPhone);
	}
  reasonField += validateEmpty(theForm.arrival);
  reasonField += validateEmpty(theForm.departure);
  reasonField += validateEmpty(theForm.numGuests);
  reasonField += validateEmpty(theForm.billName);
  reasonField += validateEmpty(theForm.ccNumber);
  reasonField += validateEmpty(theForm.ccExp);
  reasonField += validateEmpty(theForm.ccCCV);
      
  if (reasonField != "") {
    alert("Some fields need correction:\n" + reasonField);
    return false;
  }

  return true;
}

function validateEmpty (fld) {
    var error = "";
		
		var fldName = fld.name.replace('contact_', '');
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Please complete the " + fldName + " field.\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 = "Please 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 validatePhone (fld) {
   var error = "";
   var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10 || stripped.length == 11)) {
        error = "The phone number is the wrong length. Please be sure to include an area code.\n";
        fld.style.background = 'Yellow';
    } else {
				fld.value = stripped;
		}
    return error;
}

function validateDayOfMonth (fld) {
		var error = "";
		
		if (fld.value > 31) {
			error = "Please enter a valid day of the month.\n";
			fld.style.background = 'Yellow';
		}
		
    return error;
}

function validateRetreatForm (theForm) {
	var reasonField = "";

  reasonField += validateEmpty(theForm.rtrtLength);
  reasonField += validateEmpty(theForm.arrival);
  reasonField += validateEmpty(theForm.guestName);
  reasonField += validateEmpty(theForm.guestAddress);
  reasonField += validateEmpty(theForm.guestCity);
  reasonField += validateEmpty(theForm.guestProv);
  reasonField += validateEmpty(theForm.guestPostal);
  reasonField += validateEmpty(theForm.guestCountry);
  reasonField += validateEmpty(theForm.guestPhone);
  reasonField += validateEmail(theForm.guestEmail);
	if (theForm.guestPhone.value.length != 0) {
	  reasonField += validatePhone(theForm.guestPhone);
	}
  reasonField += validateEmpty(theForm.numGuests);
  reasonField += validateEmpty(theForm.billName);
  reasonField += validateEmpty(theForm.ccNumber);
  reasonField += validateEmpty(theForm.ccExp);
  reasonField += validateEmpty(theForm.ccCCV);
      
  if (reasonField != "") {
    alert("Some fields need correction:\n" + reasonField);
    return false;
  }

  return true;
}

