//javascript validation script

/*
--------------Instructions-------------

for the form you need something like this
<form name="demo" onsubmit="return validateFormOnSubmit(this)" action="test.htm">
first name:
<input type="text" name="first_name" size="40">
<span id="first_name_error"></span>

which calls something like

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

  reason += validateEmpty(theForm.first_nam);
  set_data(reason,first_name_error);

  //repeat for each field

  return true;
}

*/


function set_data(data_input,element_id)
{
	document.getElementById(element_id).innerHTML=data_input;
}

//------------Not Empty------------

//The function below checks if a required field has been left empty.
function validateEmpty(fld, fld_name) {
	var error = "";

	if (fld.value.length == 0) {
		fld.style.background = 'Yellow';
		error = "The required field "+ fld_name +" has not been filled in. <br> \n";
		return error;
	} else {
		fld.style.background = 'white';
		return "";
	}
}

//------------Email---------------

// Next we want to see if the email address the user entered is real.
// This means that the input data must contain at least an @ sign and
// a dot (.). Also, the @ must not be the first character of the email
// address, and the last dot must at least be one character after the @ sign.

// At first we check if the user entered anything at all in the
// email field. Next, we use regular expression and the test() method
// to check the field for a compliance. Also we will use trim()
// function that will trim leading whitespace off the string.
// This won’t be perfect validation — it is possible to slip not
// compliant addresses by it — but it's normally good enough.

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld, fld_name) {
	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. <br> \n";
	} else if (!emailFilter.test(tfld)) { //test email for illegal characters
		fld.style.background = 'Yellow';
		error = "Please enter a valid email address. <br> \n";
	} else if (fld.value.match(illegalChars)) {
		fld.style.background = 'Yellow';
		error = "The email address contains illegal characters. <br> \n";
	} else {
		fld.style.background = 'white';
		return "";
	}

    return error;
}

//------------Phone---------------

// The function below checks if the phone number is valid.
// At first we use regular expression and the replace() method
// to clear out any spacer characters. Next, we use the isNaN()
// function to check if the phone number contain only numbers.
// At last we check the length of the string and permit only phone
// numbers with 10 digits.

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

	if (fld.value == "") {
		error = "You didn't enter a phone number. <br> \n";
		fld.style.background = 'Yellow';
	} else if (isNaN(parseInt(stripped))) {
		error = "The phone number contains illegal characters. <br> \n";
		fld.style.background = 'Yellow';
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code. <br> \n";
		fld.style.background = 'Yellow';
	} else {
		fld.style.background = 'white';
		return "";
	}

    return error;
}


//------------Is Numeric---------------

// If the element's string matches the regular expression it is all numbers
function isNumeric(fld, fld_name){
	error="";
	var numericExpression = /^[0-9]+$/;
	if(!fld.value.match(numericExpression)){
		error = "Please enter only numbers for "+ fld_name +". <br> \n";
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'white';
		return "";
	}
}

//------------Is Alpha---------------

// If the element's string matches the regular expression it is all letters
function isAlphabet(fld, fld_name){
	error="";
	var alphaExp = /^[a-zA-Z]+$/;
	if(!fld.value.match(alphaExp)){
		error = "Please enter only letters for "+ fld_name +". <br> \n";
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'white';
		return "";
	}
}

//------------Is Alpha Numeric---------------

// If the element's string matches the regular expression it is numbers and letters
function isAlphanumeric(fld, fld_name){
	error="";
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(!fld.value.match(alphaExp)){
		error = "Please enter only letters or numbers for "+ fld_name +". <br> \n";
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'white';
		return "";
	}
}

//------------Length Resitriction---------------

//restrict the number of characters a user can enter into a field
function lengthRestriction(fld, fld_name, min, max){
	error="";

	var uInput = fld.value;
	if(!(uInput.length >= min && uInput.length <= max)){
		error = fld_name +" should have between "+ min +" and "+ max +" characters <br> \n";
		fld.style.background = 'Yellow';
		return error;
	} else {
		fld.style.background = 'white';
		return "";
	}
}

//------------Password---------------

//checks that the two password fields match
//check that the password is at least min long
//check that the password is only letters and numbers
function checkPasswords(pw1, pw2, min, fld_name) {
	error="";
	var alphaExp = /^[0-9a-zA-Z]+$/;

	if(pw1.value!=pw2.value){
		error = "The passwords do not match, please try re-entering them. <br> \n";
		pw1.style.background = 'Yellow';
		pw2.style.background = 'Yellow';
		return error;
	} else if(!(pw1.value.length >= min)) {
		//alert(pw1.value.length);
		error = "The password is to short, please make it at least "+ min +" characters <br> \n";
		pw1.style.background = 'Yellow';
		pw2.style.background = 'Yellow';
		return error;
	} else if(!pw1.value.match(alphaExp)){
		error = "Please enter only letters or numbers for your password <br> \n";
		pw1.style.background = 'Yellow';
		pw2.style.background = 'Yellow';
		return error;
	} else {
		pw1.style.background = 'white';
		pw2.style.background = 'white';
		return "";
	}
}

//------------isDate---------------
function isDate(fld,fld_name)
{
	error="";
	// Regular expression used to check if date is in correct format
	//yyyy-mm-dd
	var pattern = "(19|20)[0-9][0-9]-(0|1)[0-9]-[0-3][0-9]";

	if(fld.value.match(pattern)) {
		var date_array = fld.value.split('-');
		var day = date_array[2];
		// Attention! Javascript consider months in the range 0 - 11
		var month = date_array[1]-1;
		var year = date_array[0];

		var error = "";

		// This instruction will create a date object
		source_date = new Date(year,month,day);
		//alert(source_date);

		//an error in the day or month will cause
		//js to bump the date ahead which will cause
		//one or more of the other fields to fail
		//even though technically they are fine

		if(year != source_date.getFullYear())
		{
			error += "Please enter only a valid year for the date in "+ fld_name +"<br> \n";
			fld.style.background = 'Yellow';
		}

		if(month != source_date.getMonth())
		{
			error += "Please enter only a valid month for the date in "+ fld_name +"<br> \n";
			fld.style.background = 'Yellow';
		}

		if(day != source_date.getDate())
		{
			error += "Please enter only a valid day for the date in "+ fld_name +"<br> \n";
			fld.style.background = 'Yellow';
		}

		if(error.length>0) {
			return error;
		} else {
			fld.style.background = 'white';
			return "";
		}

	} else {
			error = "Please enter a valid date in the format of yyyy-mm-dd for "+ fld_name +"<br> \n";
			fld.style.background = 'Yellow';
			return error;
	}
}

//------------isVersion---------------
function isVersion(fld,fld_name)
{
	error="";

	// Regular expression used to check if verson is in correct format
	//##.##.##.##
	var pattern = /^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})$/;

	if(!(fld.value.match(pattern))) {
		error = "Please enter a valid version in the format of ##.##.##.## for "+ fld_name +"<br> \n";
		fld.style.background = 'Yellow';
	} else
		fld.style.background = 'white';

	return error;
}

//------------isMoney---------------
//validates that a give number is in the following format
//#.## the amount before the . can be as long as it wants to
//be including nothing at 0 on ... but after the decimal
//it can only have 2 numbers
function isMoney(fld,fld_name)
{
	error="";

	var pattern = /^([0-9]{0,})\.([0-9][0-9])$/;

	if(!(fld.value.match(pattern))) {
		error = "Please enter a valid dollar amount in "+ fld_name +"<br> \n";
		fld.style.background = 'Yellow';
	} else
		fld.style.background = 'white';

	return error;
}

//------------check url---------------
//thus function will validate if a given field
//is a valid http, https, ftp, and mailto:
//url if not it returns an error

function checkUrl(fld, fld_name)
{
	error="";

	if(fld.value.match(/^(http|https|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#\=]\w+)*\/?$/i) ||
	fld.value.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i)) {
		fld.style.background = 'white';

	} else {
		error = "Please enter a valid url in "+ fld_name +"<br> \n";
		fld.style.background = 'Yellow';
	}

	return error;
}

