// JavaScript Document

function validateContactForm(form2) {
  var reason = "";
  
	reason += validateFName(form2.Name);
	reason += validateEmail(form2.Email);
	reason += validateEmpty(form2.Message);
	
	if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  alert("Thank you for your inquiry. We'll be getting back to you as soon as possible.");
  return true;
}


function validateEnquiry(form) {
  var reason = "";
  
	reason += validateSeats(form.Seats);
	reason += validateFName(form.FirstName);
	reason += validateLName(form.LastName);
	reason += validateEmail(form.Email);
	reason += validateMobile(form.MobileNumber);
	reason += validateEmpty(form.Know);
	
	if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  alert("Thank you for your interest on our program. You'll be receiving our confirmation email in a while.\nPlease help us to forward this email to people whom will benefit from the seminar.\nYour support is much appreciated.");
  return true;
}

function validateEnquiry2(form) {
  var reason = "";
  
	reason += validateSeats(form.Seats);
	reason += validateFName(form.FirstName);
	reason += validateLName(form.LastName);
	reason += validateEmail(form.Email);
	reason += validateMobile(form.MobileNumber);
	reason += validateChoice(form.Know);
	reason += validateBox(form.Box);
	
	if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  alert("Thank you for your interest on our program. You'll be receiving our confirmation email in a while.\nPlease help us to forward this email to people whom will benefit from the seminar.\nYour support is much appreciated.");
  return true;
}

function validateChoice(fld) {
    var error = "";

	if (fld.value == "") {
       fld.style.background = 'Yellow'; 
       error = "Please choose one.\n";
	} else {
       fld.style.background = 'White';
    }     
	return error;
}

function validateBox(fld) {
    var error = "";
	var know = document.getElementById('Know').value;
	
	if (know == "Others -") {
		fld.style.visibility = 'visible';
		if (fld.value == "") {
			fld.style.background = 'Yellow'; 		
			error = "The required field has not been filled in.\n";
		} else {
			fld.style.background = 'White';
		}
	} else {
		fld.style.visibility = 'hidden';
		fld.value = "";
		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 validateSeats(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter number of seat/s.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The seat number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } 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 = "You didn't enter your First Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your First Name theFormtains 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 = "You didn't enter your Last Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Your Last Name theFormtains 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 theFormtains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateMobile(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter your contact number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The contact number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 8)) {
        error = "The contact number is the wrong length.\n";
        fld.style.background = 'Yellow';
    } else {
		fld.style.background = 'White';
	}
    return error;
}
