// JavaScript Document

function validateReservation(theForm) {
	var reason = "";
	
	reason += validateFName(theForm.FirstName);
	reason += validateLName(theForm.LastName);
	reason += validateRoom(theForm.Room);
	reason += validateEmail(theForm.Email);
	reason += validateDateFrom(theForm.From);
	reason += validateDateTo(theForm.To);
	
	if (reason != "") {
	  alert("Some fields need correction:\n" + reason);
	  return false;
	}
	  alert("Thank You for your interest in our place,\nwe'll get back to you within 24 hours. Thank You!");
	  return true;
}

function validateEnquiry(theForm) {
	var reason = "";
	
	reason += validateName(theForm.Name);
	reason += validateEmail(theForm.Email);
	reason += validateMessage(theForm.Message);
	
	if (reason != "") {
	  alert("Some fields need correction:\n" + reason);
	  return false;
	}
	  alert("Thank You for your Enquiry,\nwe'll get back to you within 24 hours. Thank You!");
	  return true;
}

 function validateName(fld) {
	var error = "";
	var illegalChars = /\W/;	// allow letters, numbers, and underscores
	
	if (fld.value == "") {
	  fld.style.background = 'Yellow'; 
	  error = "- Please enter your Name.\n";
	} else if (illegalChars.test(fld.value)) {
	  fld.style.background = 'Yellow'; 
	  error = "- Your Name contains illegal characters.\n";
	} else {
	  fld.style.background = 'White';
	} 
	return error;
}
            
function validateMessage(fld) {
	var error = "";
	var illegalChars = /\W/;	// allow letters, numbers, and underscores
	
	if (fld.value == "") {
	  fld.style.background = 'Yellow'; 
	  error = "- Please enter your Enquiry.\n";
	} else {
	  fld.style.background = 'White';
	} 
	return error;
}

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

function validateFName(fld) {
    var error = "";
    var illegalChars = /\W/;	// allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- Please enter your First Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- Your First Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLName(fld) {
    var error = "";
    var illegalChars = /\W/;	// allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- Please enter your Last Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- Your Last Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateRoom(fld) {
    var error = "";
    if (fld.value == "") {
       fld.style.background = 'Yellow'; 
       error = "- Please choose a Room.\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 your 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 = "- Your Email Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateDateFrom(fld) {
	var error="";
	var today = new Date();
	var mm = today.getMonth() + 1;
	var dd = today.getDate();
	var yy = today.getFullYear();
	var now = mm + "/" + dd + "/" + yy;

	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "- Please choose a Date You want to Stay-in.\n";
	} else if (Date.parse(fld.value) < Date.parse(now)) {
		fld.style.background = 'Yellow';
		error = "- From Date must be greater than the Current Date.\n"
	} else if (Date.parse(fld.value) == Date.parse(now)) {
		fld.style.background = 'Yellow';
		error = "- From Date must be greater than the Current Date.\n"
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateDateTo(fld) {
	var error="";
	var today = new Date();
	var mm = today.getMonth() + 1;
	var dd = today.getDate();
	var yy = today.getFullYear();
	var now = mm + "/" + dd + "/" + yy;
	
	var sDate = document.getElementById('From').value;
		
	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "- Please choose a Date You want to end your Stay-in.\n";
	} else if (Date.parse(fld.value) < Date.parse(now)) {
		fld.style.background = 'Yellow';
		error = "- End Date must be greater than the Current Date.\n";
	} else if (Date.parse(fld.value) < Date.parse(sDate)) {
		fld.style.background = 'Yellow';
		error = "- End Date must be greater than or equal to From Date.\n";
	} else if (Date.parse(fld.value) == Date.parse(now)) {
		fld.style.background = 'Yellow';
		error = "- End Date must be greater than the Current Date.\n"
	} else {
		fld.style.background = 'White';
	}
	return error;
}