function isValidEmail(e)
{
	// assume an email address cannot start with an @ or white space, but it
	// must contain the @ character followed by groups of alphanumerics and '-'
	// followed by the dot character '.'
	// It must end with 2 or 3 alphanumerics.
	
	var alnum="a-zA-Z0-9";
	exp="^[^@\\s]+@(["+alnum+"+\\-]+\\.)+["+alnum+"]["+alnum+"]["+alnum+"]?$";
	emailregexp = new RegExp(exp);

	result = e.match(emailregexp);
	if (result != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// ---
function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
	{
		num = "0";
	}
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
	{
		cents = "0" + cents;
	}
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
		num = num.substring(0,num.length-(4*i+3))+''+ num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + num + '.' + cents);
}

// new date validation scripts added by BM 18/05/2006
function isValidDate(strDate) 
{
	var strMatch = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
	
	if(strMatch.test(strDate)){
		// set date value as string
		var inputStr  = strDate;
		var inputTimeStr = "00:00";
		var strError = "";		
			 var delim1 = inputStr.indexOf("/");
			 var delim2= inputStr.lastIndexOf("/");
			  
			 if (delim1 != -1 && delim1 == delim2) 
			 { // there is only 1 delimiter in the string
			  	return false;
			 }
			 // get mm dd yyyy
			 if(delim1 != -1)
			 {
			 	var dd = parseInt(inputStr.substring(0,delim1),10);
				var mm = parseInt(inputStr.substring(delim1 + 1,delim2),10);
				var yyyy = parseInt(inputStr.substring(delim2 + 1,inputStr.length),10);
			 					
			 }
			 // check for no numerics in fields
			 if (isNaN(mm) || isNaN(dd) || isNaN(yyyy))
			 {
			 	return false;
			 }
			 
			 
			 if (mm < 1 || mm > 12)
			 {
			 	// months can only be between 1 and 12
				return false;
				
			 }
			 
				// check mm and number of days		 
			 if (!checkMonthLength(mm,dd))
			 {
			 	return false;
			 }
			 // check for febuary and leap years 
			 if (mm == 2)
			 {
				 if(!checkLeapYear(mm,dd,yyyy))
				 {
				 	return false;
				 }
			}
			
			//  check for Time Format
			var delim3 = inputTimeStr.indexOf(":");
			
			if (delim3 == -1 || inputTimeStr.length !=5)
			{
				return false;	
			}
			else
			{
				var hHours = inputTimeStr.substring(0,2);
				var mMins =  inputTimeStr.substring(3,5)				
			}			
				
			// check for non numerics	
			if (isNaN(hHours) || isNaN(mMins) )
			{
				return false;	
			}
			else
			{
				// check hours less than 24
				if (hHours > 23 )
				{
					 return false;	
				}
				// check mins less than or equal to 59
				if (mMins > 59 )
				{
					return false;	
				}
			
			
			}
										
			return true;
		}
		else
		{
			return false;	
		}
	}	
		
	function checkLeapYear(mm,dd,yyyy)
	{
			
			if(yyyy % 4 > 0 && dd > 28)
			{
				 return false
			}
			else if (dd > 29 )
			{
				return false
			}
		return true
	}

	function checkMonthLength(mm,dd)
	{
	var months = new Array ("","January","Febuary","March","April","May","June","July","August","September","October","November","December");
					
		if((mm == 4 || mm ==6 || mm== 9 || mm == 11) && dd > 30) 
		{
			return false
		}
		else if (dd > 31)
		{
			return false
		}
	
			return true;
	}

// END



// ---
function isValidURL(strURL)
{
	var strMatch = /^http|https|ftp/;
	
	return strMatch.test(strURL);
}

// ---
function isValidDirectory(strURL)
{
	var strMatch = /^[a-zA-Z0-9_]+$/;
	
	return strMatch.test(strURL);
}

// ---
function isValidFileType( strFileName, arrExt ) {
	if (!strFileName.length) 	{
		return( false );
	}
	strFileName = strFileName.toLowerCase();
	for ( var j=0; j<arrExt.length; j++ ) {
		if ( strFileName.substring( strFileName.length, strFileName.indexOf( arrExt[j].toLowerCase() ) ) == arrExt[j].toLowerCase() ) {
			return( true );
		}
	}
	return( false );
}

// ---
function isValidField(s, strType, arrAllowFields) {
	for (var i=0; i<arrAllowed.length; i++) {
		if (arrAllowed[i][0].toLowerCase() == s.toLowerCase()) {
			if (strType == "any") {
				return true;
			}
			else if (arrAllowed[i][1] == strType) {
				return true;
			}
		}
	}
	return false;
}

// ---
function containsValidField(s, arrAllowFields) {
	var strCurrent = "";
	var blnReturn = true;
	
	var fullStr = s + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9<>]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	
	var arrWords = cleanedStr.split(" ");
	
	for (var j=0; j<arrWords.length; j++) {
		strCurrent = arrWords[j];
		
		if ((strCurrent.substring(0, 2) == "<<") && (strCurrent.substring(strCurrent.length-2) == ">>")) {
			blnReturn = false; // found a field, assume failure
			blnReturn = isValidField(strCurrent, "any", arrAllowFields);
		}
	}
	return blnReturn;
}

// ---
function checkFormFields(strFormName, arrFields, arrTypes, arrOutput, arrArgs)
{
	/*
	*  arrFields = new Array("txtFirstName", "txtDescription", "txtEmail", "txtPassword");
	*  arrTypes = new Array("string", "longstring(MAX_CHARS)", "email", "password");
	*  arrOutput = new Array("First Name", "Description", "Email Address", "Password");
	*/
	
	var objForm = document.forms[strFormName];
	var retval = true;
	var strError = "";	
	
	// clear all row colours
	for (var j=0; j<arrFields.length; j++)
	{
		document.getElementById(arrFields[j] + "_row").className = "";
	}
	
	// check all fields
	for (var j=0; j<arrFields.length; j++)
	{
		if (arrTypes[j] == "string")
		{
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j].substring(0, 10) == "longstring")
		{
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				// arrTypes[j] is passed in as longstring(x) where x is
				// the maximum character limit. ie longstring(500);
				var strTemp = arrTypes[j].substring(10);
				var lngMaxLength = strTemp.substring(1, strTemp.length-1);
				
				if (objForm.elements[arrFields[j]].value.length > lngMaxLength)
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Limit the '" + arrOutput[j] + "'\nfield to " + lngMaxLength + " characters or less.\n\nCurrent characters : " + document.getElementById(arrFields[j]).value.length + "\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "email")
		{
			// check valid email address
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				if (!isValidEmail(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Enter a valid email address in the '" + arrOutput[j] + "' field.\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "dynamicemail") {
			if (objForm.elements[arrFields[j]].value.length < 1) {
				document.getElementById(elements[arrFields[j]] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else if (!isValidField(objForm.elements[arrFields[j]].value, "email", arrArgs[j])) {
				if (!isValidEmail(objForm.elements[arrFields[j]].value)) {
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Please enter either a valid email address or a dynamic field in the '" + arrOutput[j] + "' field.\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "emailbody") {
			if (objForm.elements[arrFields[j]].value.length < 1) {
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else if (!containsValidField(objForm.elements[arrFields[j]].value, arrArgs[j])) {
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* One or more dynamic fields you entered into the '" + arrOutput[j] + "' field is invalid.\n\nPlease check all spelling is correct and try again.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j] == "password")
		{
			// password (needs to be at least 5 characters)
			if (objForm.elements[arrFields[j]].value.length < 5)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field with at least 5 characters.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				// make the passwords lower case
				objForm.elements[arrFields[j]].value = objForm.elements[arrFields[j]].value.toLowerCase();
				objForm.elements[arrFields[j] + "_confirm"].value = objForm.elements[arrFields[j] + "_confirm"].value.toLowerCase();
			}
			
			// check passwords match
			if (objForm.elements[arrFields[j]].value != objForm.elements[arrFields[j] + "_confirm"].value)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Your passwords do not match!\n\nPlease enter the same value in both\nthe '" + arrOutput[j] + "' and 'Confirm " + arrOutput[j] + "' fields.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j] == "InUrl")
		{
			// check valid internal URl (i.e. no 'http://' or 'https:// or ftp://')
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				if (isValidURL(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Enter a valid internal url in the '" + arrOutput[j] + "' field. Internal Links do not need to start with either, http://, https:// or ftp://.\n";
					objForm.elements[arrFields[j]].value = '';
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "ExUrl")
		{
			// check valid external URl (i.e. 'http://' or 'https:// or ftp://')
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				if (!isValidURL(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Enter a valid external url in the '" + arrOutput[j] + "' field. External links need to start either, http://, https:// or ftp://.\n";
					objForm.elements[arrFields[j]].value = '';
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "numeric")
		{
			// check valid numeric value
			if (objForm.elements[arrFields[j]].value.length < 1)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Complete the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
			else
			{
				if (isNaN(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					
					strError += "\n* Enter a valid numeric value in the '" + arrOutput[j] + "' field.\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "dropdown")
		{
			// check valid drop down menu
			if (objForm.elements[arrFields[j]].options[0].selected)
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Select from the '" + arrOutput[j] + "' field.\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j] == "date")
		{
			// check valid dates
			if (objForm.elements[arrFields[j]].value.length > 0 && !isValidDate(objForm.elements[arrFields[j]].value))
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Enter a valid date in the '" + arrOutput[j] + "' field (in the format dd/mm/yyyy).\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j] == "datereq")
		{
			// check valid dates
			if (!isValidDate(objForm.elements[arrFields[j]].value))
			{
				document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
				
				strError += "\n* Enter a valid date in the '" + arrOutput[j] + "' field (in the format dd/mm/yyyy).\n";
				if (retval == true) {
					objForm.elements[arrFields[j]].focus();
				}
				retval = false;
			}
		}
		else if (arrTypes[j] == "image")
		{
			if (objForm.elements[arrFields[j]].value.length)
			{
				var arrAllowedExt = new Array(".jpg", ".jpeg", ".gif", ".png", ".swf");
				if (!isValidFileType(objForm.elements[arrFields[j]].value, arrAllowedExt))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					document.getElementById(arrFields[j]).value = '';
					strError += "\n* Please only upload image files in one of the following formats: " + arrAllowedExt.join(", ") + "\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "file")
		{
			if (objForm.elements[arrFields[j]].value.length)
			{
				var arrAllowedExt = new Array(".pdf", ".doc", ".ppt");
				if (!isValidFileType(objForm.elements[arrFields[j]].value, arrAllowedExt))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					document.getElementById(arrFields[j]).value = '';
					strError += "\n* Please only upload document files in one of the following formats: " + arrAllowedExt.join(", ") + "\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
		else if (arrTypes[j] == "dir")
		{
			if (objForm.elements[arrFields[j]].value.length)
			{
				if (!isValidDirectory(objForm.elements[arrFields[j]].value))
				{
					document.getElementById(arrFields[j] + "_row").className = "highlightedyellow";
					document.getElementById(arrFields[j]).value = '';
					strError += "\n* Please only use alphanumeric characters for the directory name.\n";
					if (retval == true) {
						objForm.elements[arrFields[j]].focus();
					}
					retval = false;
				}
			}
		}
	}
	
	if (strError != "") {
		strError = "An error has occurred.\n\nPlease follow these instructions to correct the error.\n\n" + strError;
		
		alert(strError);
	}
	return retval;
}
