
// 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
// CheckDate(CONTROL) - Checks for a valid date
// showhide - for hiding and showing items 

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 checkForAlphaErrorD(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();
	} else
	{
	return parseInt((field.value * 100)) / 100;
	}
} 

//#############################################################################


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);
	}

//#############################################################################


function CheckDate(CONTROL){
//var strFormat = theForm.DateFormats.options[theForm.DateFormats.selectedIndex].value;
var strFormat = "dd/mmm/yyyy"

	if(!isDate(CONTROL.value)){
	alert("Invalid Date")
	CONTROL.focus();
	return;
	}

CONTROL.value = FormatDate(CONTROL.value,strFormat);

}

function isDate(DateToCheck){
if(DateToCheck==""){return true;}
var m_strDate = FormatDate(DateToCheck);
if(m_strDate==""){
return false;
}
var m_arrDate = m_strDate.split("/");
var m_DAY = m_arrDate[0];
var m_MONTH = m_arrDate[1];
var m_YEAR = m_arrDate[2];
if(m_YEAR.length > 4){return false;}
m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
var testDate=new Date(m_strDate);
if(testDate.getMonth()+1==m_MONTH){
return true;
} 
else{
return false;
}
}//end function




function FormatDate(DateToFormat,FormatAs){
if(DateToFormat==""){return"";}
if(!FormatAs){FormatAs="dd/mm/yyyy";}

var strReturnDate;
FormatAs = FormatAs.toLowerCase();
DateToFormat = DateToFormat.toLowerCase();
var arrDate
var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var strMONTH;
var Separator;

while(DateToFormat.indexOf("st")>-1){
DateToFormat = DateToFormat.replace("st","");
}

while(DateToFormat.indexOf("nd")>-1){
DateToFormat = DateToFormat.replace("nd","");
}

while(DateToFormat.indexOf("rd")>-1){
DateToFormat = DateToFormat.replace("rd","");
}

while(DateToFormat.indexOf("th")>-1){
DateToFormat = DateToFormat.replace("th","");
}

if(DateToFormat.indexOf(".")>-1){
Separator = ".";
}

if(DateToFormat.indexOf("-")>-1){
Separator = "-";
}


if(DateToFormat.indexOf("/")>-1){
Separator = "/";
}

if(DateToFormat.indexOf(" ")>-1){
Separator = " ";
}

arrDate = DateToFormat.split(Separator);
DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
		DateToFormat += arrDate[iSD] + Separator;
		}
	}
DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
arrDate = DateToFormat.split(Separator);

if(arrDate.length < 3){
return "";
}

var DAY = arrDate[0];
var MONTH = arrDate[1];
var YEAR = arrDate[2];




if(parseFloat(arrDate[1]) > 12){
DAY = arrDate[1];
MONTH = arrDate[0];
}

if(parseFloat(DAY) && DAY.toString().length==4){
YEAR = arrDate[0];
DAY = arrDate[2];
MONTH = arrDate[1];
}


for(var iSD = 0;iSD < arrMonths.length;iSD++){
var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
var MonthPosition = DateToFormat.indexOf(ShortMonth);
	if(MonthPosition > -1){
	MONTH = iSD + 1;
		if(MonthPosition == 0){
		DAY = arrDate[1];
		YEAR = arrDate[2];
		}
	break;
	}
}

var strTemp = YEAR.toString();
if(strTemp.length==2){

	if(parseFloat(YEAR)>40){
	YEAR = "19" + YEAR;
	}
	else{
	YEAR = "20" + YEAR;
	}

}


	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function

function showhide(id){ 
if (document.getElementById){ 
obj = document.getElementById(id); 
if (obj.style.display == "none"){ 
obj.style.display = ""; 
} else { 
obj.style.display = "none"; 
} 
} 
} 


