
// This file contains the following functions
// validateEmail()	-	Validates an email address
// checkForAlphaError() - Validates an feild to check a number has been entered
// decimalise() - Returns the passed number to 2 decimal places (rounded down to the nearest 0.01) 
// formatAsMoney() - returns the passed number pluss .00 if whole number
// textCounter(feild,number) - Enforces the lengh of the Textarea


function validateEmail(emailField){	
	//#############################################################################
	// Validate the format of the email address i.e. it should contain only one '@' and at least one '.' afterwards
	
	var boolEmailError = false;
	var strEmailError = "\nThe email address that you have supplied is not valid for the following reason:\n";
	
	if(emailField.value != ""){
		var firstAt	= emailField.value.indexOf("@")			//Find if there are any @ symbols
		var secondAt	= emailField.value.indexOf("@", firstAt + 1)	//And then check if there are any more
		var dot		= emailField.value.indexOf(".", firstAt + 1)	//And there should be at least one dot after the @
		
		if(firstAt == 0){
			boolEmailError = true;
			strEmailError = strEmailError + "  the @ symbol cannot be the first character\n";
		}
		if(firstAt == -1){
			boolEmailError = true;
			strEmailError = strEmailError + "  it does not contain an @ symbol\n";
		}
		if(secondAt != -1){
			boolEmailError = true;
			strEmailError = strEmailError + "  it contains more than one @ symbol\n";
		}
		if((dot == -1) && (firstAt != -1)){
			boolEmailError = true;
			strEmailError = strEmailError + "  there are no full stops after the @ symbol\n";
		}
		if((dot != -1) && (dot == firstAt + 1)){
			boolEmailError = true;
			strEmailError = strEmailError + "  the full stop cannot appear immediately after the @ symbol\n";
		}
		if((dot != -1) && (dot + 1 == emailField.value.length)){
			boolEmailError = true;
			strEmailError = strEmailError + "  the full stop is at the end of the address\n";
		}
	
		if(boolEmailError == true){
			alert(strEmailError);
			emailField.select();
			emailField.focus();
		}
	}
	
	//#############################################################################
	
}

function checkForAlphaError(field){ 
	if((isNaN(field.value) == true) || (field.value.indexOf(" ") > -1)){ 
	//Displays the standard error for a non-numeric value or space, clears the offending field and gives it focus again 
	alert("Numeric values only please"); 
	field.value = 0; 
	field.focus();
	} 
} 

//#############################################################################


function decimalise(number){ 
//Returns the passed number to 2 decimal places (rounded down to the nearest 0.01) 
	return parseInt((number * 100)) / 100 
} 

//#############################################################################

function formatAsMoney(moneyField) {
	var mnt = moneyField.value;

	mnt -= 0;
	mnt = (Math.round(mnt*100)) / 100;
	(mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);

	moneyField.value = mnt;
}


//#############################################################################


function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) 
	field.value = field.value.substring(0, maxlimit);
	}
