

////////////////////////////////////////////////////////////////////////////////
//time validation too for HH:MM format
////////////////////////////////////////////////////////////////////////////////
function isTime(time) {
	if (time == null) return false;

	if (time.search(/[0-9]\:[0-5][0-9]/) != -1 && time.length == 4) {
		return true;
	}
	if (time.search(/[0-2][0-9]\:[0-5][0-9]/) != -1 && time.length == 5) {
		if (parseInt(time.substring(0, 1)) == 2 && parseInt(time.substring(1, 2)) > 3) {
			return false;
		}
		return true;
	}
	return false;
}
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// appends :00 to the end of time fields to speed user input
////////////////////////////////////////////////////////////////////////////////
function QuickTimeEntry(timeEl, isTime) {	
	var el = document.getElementById(timeEl);
	if (el.value.length == 1) {
		if (isTime) {
			el.value = "0" + el.value + ":00";
		} else {
			el.value = el.value + ":00";
		}
	} else if (el.value.length == 2) {
		el.value = el.value + ":00";
	} else if (el.value.length == 4 && isTime) {
		if (el.value.indexOf(":") > 0) {
			el.value = "0" + el.value;
		} else {
			el.value = el.value.substring(0, 2) + ":" + el.value.substring(2, 4);
		}
	} else if (el.value.length == 3 && isTime) {
		if (el.value.indexOf(":") < 0) {
			el.value = "0" + el.value.substring(0, 1) + ":" + el.value.substring(1, 3);
		}
	}	
}


function QuickDateEntry(strDateFieldName) 
{	
	var txtDateField = document.getElementById(strDateFieldName);
	var dateObj = new Date();
	var strYear = new String(dateObj.getYear());
	
	
	if(txtDateField)
	{
		//if a valid date is already present don't alter the value
		if(!isDateSilent(txtDateField.value))
		{
			//check if two '/' chars are already present
			if((txtDateField.value.charAt(2)=="/")&&(txtDateField.value.charAt(5)=="/"))
			{
				//check for date format DD/MM/YY
				if (txtDateField.value.length == 8) 
				{
					//if all date elements are numeric then update
					if(	(!isNaN(txtDateField.value.substr(0,2))) &&
						(!isNaN(txtDateField.value.substr(3,2))) &&
						(!isNaN(txtDateField.value.substr(6,2))) )
					{					
						txtDateField.value = txtDateField.value.substr(0,6) + 
							strYear.substr(0,2) + txtDateField.value.substr(6);
					}
				}
			}
			
			// check for date format DDMMYYYY
			else if (txtDateField.value.length == 8) 
			{
				//check all date chars are numeric
				if(!isNaN(txtDateField.value.substr(0,8)))
				{
					txtDateField.value = 
						txtDateField.value.substr(0,2) + "/" +
						txtDateField.value.substr(2,2) + "/" +
						txtDateField.value.substr(4);
				}
			}
			
			// check for date format DDMMYY
			else if (txtDateField.value.length == 6) 
			{
				//check all date chars are numeric
				if(!isNaN(txtDateField.value.substr(0,6)))
				{
					txtDateField.value = 
						txtDateField.value.substr(0,2) + "/" +
						txtDateField.value.substr(2,2) + "/" +
						strYear.substr(0,2) + txtDateField.value.substr(4);
				}
			}			
		}
	}
}



////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
//Currency Validation
////////////////////////////////////////////////////////////////////////////////
function isCurrency(strCurrencyToValidate)
{
var regExp = /\d*\.?/

	strCurrencyToValidate = strCurrencyToValidate.replace(",","");
	strCurrencyToValidate = strCurrencyToValidate.replace("£","");


	if(strCurrencyToValidate.indexOf(".")!=-1)
	{
		if(strCurrencyToValidate.length - strCurrencyToValidate.indexOf(".")>3)
		{
			return false;
		}		
	}
	
	if(strCurrencyToValidate.length==0)
	{
		return false;
	}	
	
	if(regExp.test(strCurrencyToValidate) && !isNaN(strCurrencyToValidate))
	{
		return true;
	}
	return false;
}
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// returns true if the string contains only space characters
////////////////////////////////////////////////////////////////////////////////
function isAllSpaces(strToCheck)
{
	//replaces all space chars with nothing, then tests if there are any chars left
	//	in the string
	if(strToCheck.replace(/\s+/,"")=="")
	{
		return true;
	}
	else
	{
		return false;
	}
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Email Validation Function
////////////////////////////////////////////////////////////////////////////////

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
function isEmail(email) {
	if (email == "") return false;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return (true)
	}
	return (false)
}


////////////////////////////////////////////////////////////////////////////////
//Date Validation Functions
////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////
//returns true if the date specified is on a weekend (accepts date in UK dd/mm/yyyy format)
function isOnAWeekend(strDateToCheck)
{	
	var dateObj = new Date(swap_date(strDateToCheck))
	
	if((dateObj.getDay() == 0)||(dateObj.getDay() == 6))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//////////////////////////////////////////////////////////////////////////////////////







/*
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2030;


//var datesLookup = new Date();
//used for testing the date passed is in not more than 364 days in the future 
//	(prevents putting in todays date with the wrong year)
//var maxDate = new Date((datesLookup.getYear() + 1),datesLookup.getMonth(),(datesLookup.getDate() - 1));
//minYear = datesLookup.getFullYear() - 1;
//maxYear = datesLookup.getFullYear() + 1;
	


function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
		
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length < 2 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
	
	/*month = month - 1; //Date Constructor starts at 0 for January!
	
	var tempDate = new Date(year,month,day);
		
	//finally check the date is no more than 364 days in the future
	if (tempDate > maxDate)
	{
		alert("Dates cannot be added more than one year in advance");
		return false;
	}*/
	
return true
}



//copy of above function, but without alerts. Really should be combined into a single function when possible
function isDateSilent(dtStr){
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
		
	if (pos1==-1 || pos2==-1){		
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){		
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){		
		return false;
	}
	if (strYear.length < 2 || year==0 || year<minYear || year>maxYear){		
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){		
		return false;
	}
	
	/*month = month - 1; //Date Constructor starts at 0 for January!
	
	var tempDate = new Date(year,month,day);
		
	//finally check the date is no more than 364 days in the future
	if (tempDate > maxDate)	{	
		return false;
	}*/	
	return true
}



//swaps a dd/mm/yyyy date to mm/dd/yyyy, or vice versa
function swap_date(dtStr)
{

	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	
	var newDate = month + dtCh + day + dtCh + year;
	
	return newDate;

}

function getWeekDayNameJS(dtStr)
{
	myDays = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"]

	myDate = new Date(eval('"' + swap_date(dtStr) + '"'))

	return myDays[myDate.getDay()];
}


//function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
function formatNumber(num,decimalNum)
{

/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
 
 //ted - note taken the following parameters out and hardcoded them
 var bolLeadingZero = true;
 var bolParens = false;
 var bolCommas = false;
 

    if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {			
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!

}


