// Resets error images for the passed in object array
function resetAlerts(oarrInput){
	for (i=0; i < oarrInput.length; i++) {
		errorNameToReset = oarrInput[i] + '_error';
		
		if (document.getElementById(errorNameToReset)){
			document.getElementById(errorNameToReset).style.visibility = 'hidden';
		}
	}
}

// Checks if the passed in date is prior to todays date
function checkPastDate(day,month,year) {
	var today = new Date();
	var strToday = today.getDate();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	if (year > strYear) {
		return true;
	} 
	else if (strYear == year) {
		if (month > strMonth) {
			return true;
		} 
		else if (strMonth == month) {
			if (day > strToday) {
				return true;
			}
		}
	}

	return false;
}

// Checks if a date is in the future
function checkBirthDate(day,month,year) {
	var today = new Date();
	var strToday = today.getDate();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	if (year > strYear) {
		return true;
	} 
	else if (strYear == year) {
		if (month > strMonth) {
			return true;
		} 
		else if (strMonth == month) {
			if (day > strToday) {
				return true;
			}
		}
	}

	return false;
}

// Checks if a date is in the future
function checkFutureDate(day,month,year) {
	var today = new Date();
	var strToday = today.getDate();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	if (year < strYear) {
		return true;
	} 
	else if (strYear == year) {
		if (month < strMonth) {
			return true;
		} 
		else if (strMonth == month) {
			if (day < strToday) {
				return true;
			}
		}
	}

	return false;
}

// Check if date is in the last 5 years and not in the future
function checkIncidentDate(day, month, year) {
	// Work out if in future first
	var today = new Date();
	var strToday = today.getDate();
	var strMonth = today.getMonth() + 1;
	var strYearEnd = today.getYear();
	var strYearStart = strYearEnd - 5;

	if ( strYearStart < 1000 ) {
		strYearStart = strYearStart + 1900;
		strYearEnd = strYearEnd + 1900;
	}

	if ( (year < strYearEnd) && (year > strYearStart) ) {
		return true;
	} 
	else if ( strYearEnd == year ) {
		if ( month < strMonth ) {
			return true;
		} 
		else if ( strMonth == month ) {
			if ( day <= strToday ) {
				return true;
			}
		}
	}
	else if ( strYearStart == year ) {
		if ( month > strMonth ) {
			return true;
		} 
		else if ( strMonth == month ) {
			if ( day > strToday ) {
				return true;
			}
		}
	}

	return false;
}

// Check if first date is greater than second date
function checkDateDiff(day, month, year, dayEnd, monthEnd, yearEnd) {
    if (year < yearEnd) {
		return true;
    } 
    else if (yearEnd == year) {
		if (month < monthEnd) {
			return true;
		} 
		else if (monthEnd == month) {
			if (day < dayEnd) {
				return true;
			}
		}
    }

    return false;
}

// Check if first date is greater than or equal to second date
function checkDateDiffEqual(day, month, year, dayEnd, monthEnd, yearEnd) {
    if (year < yearEnd) {
		return true;
    } 
    else if (yearEnd == year) {
		if (month < monthEnd) {
			return true;
		} 
		else if (monthEnd == month) {
			if (day <= dayEnd) {
				return true;
			}
		}
    }

    return false;
}

// SL6873 - NEWC1 - 21/02/2007 - Function updated to use Regular Expressions
function checkMail(emailAddress) {  
	// Set up regular expression
	var regExp = /^([\w])([\w!#.%'\*\+-/=\?\^{}\|~])*@(([\w][-\w]*[\w]\.)|([\w]\.))+([a-zA-Z]{2,9})$/
		     

	// Test e-mail address
	var isMatch = regExp.test(emailAddress);

	// If we have a match return true
	if ( isMatch ) {
		return false;
	}
	else {
		return true;
	}
}

// Checks if a radio button is checked or not
function checkRadio(oRadioToCheck){
	var radioError = 1
	
	for (r=0; r < oRadioToCheck.length; r++){
		if (oRadioToCheck[r].checked){
			radioError = 0;
			return;
		}
	}
	
	if (radioError) {
		return true;
	}
}
  
function checkNumber(oStringToCheck){
	var myNumber = Number(oStringToCheck.replace(/,/, ""));
	if (isNaN(myNumber)) return true;
}
  
function checkDay(oStringToCheck){
	var myNumber = Number(oStringToCheck)
	if (isNaN(myNumber) || myNumber < 0 || myNumber > 31) return true;
}
  
function checkMonth(oStringToCheck){
	var myNumber = Number(oStringToCheck)
	if (isNaN(myNumber) || myNumber < 0 || myNumber > 12) return true;
}
  
function checkYear(oStringToCheck){
	var myNumber = Number(oStringToCheck)
	if (isNaN(myNumber) || myNumber < 00 || myNumber > 99) return true;
}
  
function checkFullYear(oStringToCheck){
	var myNumber = Number(oStringToCheck)
	if (isNaN(myNumber) || myNumber < 1900 || myNumber > 2100) return true;
}

function checkWeek(oStringToCheck){
	var myNumber = Number(oStringToCheck)
	if (isNaN(myNumber) || myNumber < 0 || myNumber > 52) return true;
}
  
function checkEmpty(oStringToCheck){
	var charError = 0;
	
	if (oStringToCheck == '') {
		charError++;
	}
	
	if (charError > 0) {
		return true;
	}
	else {
		return false;
	}
}
  
function checkCreditCardNumber(oStringToCheck){
	var myNumber = Number(oStringToCheck.replace(/(\s)/g, ""));
	if (isNaN(myNumber)) return true;
}
  
function checkTotalNumber(string1, string2, string3){
	var myNumber1 = Number(string1.replace(/(,|\.)/g, ""));
	var myNumber2 = Number(string2.replace(/(,|\.)/g, ""));
	var myNumber3 = Number(string3.replace(/(,|\.)/g, ""));
	
	var total = myNumber1 + myNumber2 + myNumber3;
	
	if (total < 1) {
		return true;
	}
}
  
function checkOver21(day,month,year) {
	var today = new Date();
	var strToday = today.getDate();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	strYear = strYear - 21;
	if (year > strYear) {
		return true;
	} 
	else if (strYear == year ) {
		if (month > strMonth) {
			return true;
		} 
		else if (strMonth == month) {
			if (day > strToday) {
				return true;
			}
		}
	}

	return false;
}
  
function checkIllegalChars(oStringToCheck){
	var charError = 0;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'-";
	
	for (n = 0; n < oStringToCheck.length; n++){
		if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
			charError++;
		}
	}
	
	if (charError > 0){
		return true;
	}
	else{
		return false;
	}
}

function checkIllegalCharsAllowSpace(oStringToCheck){
	var charError = 0;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'- ";
	
	for (n = 0; n < oStringToCheck.length; n++){
		if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
			charError++;
		}
	}
	
	if (charError > 0){
		return true;
	}
	else{
		return false;
	}
}

function checkAllChars(oStringToCheck){
	var charError = 0;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'-";
	
	for (n = 0; n < oStringToCheck.length; n++){
		if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
			charError++;
		}
	}
	
	if (charError > 0){
		return true;
	}
	else{
		return false;
	}
}

function checkAllCharsAllowSpace(oStringToCheck){
	var charError = 0;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'- ";
	
	for (n = 0; n < oStringToCheck.length; n++){
		if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1){
			charError++;
		}
	}
	
	if (charError > 0){
		return true;
	}
	else{
		return false;
	}
}
  
function checkNumberError(oStringToCheck){
	var charError = 0;
	var checkOK = "0123456789 ";
	
	for (n = 0; n < oStringToCheck.length; n++){
		if (checkOK.indexOf(oStringToCheck.charAt(n)) == -1 || oStringToCheck.length < 10 || oStringToCheck.length > 12){
			charError++;
		}
	}
	
	if (charError > 0){
		return true;
	}
	else{
		return false;
	}
}
  
function checkPostcodeError(oStringToCheck){
	var postcodeError = 0;

	if (oStringToCheck.length < 2 || oStringToCheck.length > 8){
		postcodeError++;
	}
	
	if (postcodeError > 0){
		return true;
	}
	else{
		return false;
	}
}

// Displays an array of error icons
function showErrors(myErrorArray){
	for (t=0;t<myErrorArray.length; t++){
		myErrorName = myErrorArray[t];

		if (document.getElementById){
			document.getElementById(myErrorName).style.visibility = 'visible';
		}
	}
}

// Display a single error icon
function showError(errorName){
    myErrorName = errorName + '_error';
    
    if (document.getElementById){
		document.getElementById(myErrorName).style.visibility = 'visible';
    }
}
  
function startHelp() {
	showHelp('Default',1); //show a div on page load (and init with a fresh value of 1)
}

function showHelp(id,fresh) {
	if (fresh==1) {
		prevId = 'Default';
	}
	
	if (typeof(prevId) != "undefined") {
		if (prevId){
			document.getElementById('lyr_'+prevId).style.visibility = 'hidden';
		}
		
		document.getElementById('lyr_'+id).style.visibility = 'visible';
		prevId = id;
	}
}
  
function IsDateInPast(inputDate) {
	var tempDay=new Date();	
	var today=new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);	

	var substrings=inputDate.split('/');

	substrings[1]--;

	var inputDate = new Date(substrings[2],substrings[1],substrings[0]);

	return(today>inputDate);		
}

function IsDateInFuture(inputDate) {
	var tempDay=new Date();	
	var today=new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);	

	var substrings=inputDate.split('/');

	substrings[1]--;

	var inputDate = new Date(substrings[2],substrings[1],substrings[0]);

	return(inputDate>today);		
}

function IsDateWithin30Days(inputDate) {
	var tempDay=new Date();	
	var futureDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
	futureDate.setHours(0);
	futureDate.setMinutes(0);
	futureDate.setSeconds(0);
	//Advance today by 30 days.
	futureDate.setDate(futureDate.getDate()+30);

	var substrings=inputDate.split('/');

	substrings[1]--;

	var inputDate = new Date(substrings[2],substrings[1],substrings[0]);
	return(inputDate<=futureDate);		
}

function IsDateWithin365Days(inputDate) {
	var tempDay=new Date();	
	var futureDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
	futureDate.setHours(0);
	futureDate.setMinutes(0);
	futureDate.setSeconds(0);
	//Advance today by 30 days.
	futureDate.setDate(futureDate.getDate()+365);

	var substrings=inputDate.split('/');

	substrings[1]--;

	var inputDate = new Date(substrings[2],substrings[1],substrings[0]);
	return(inputDate<=futureDate);		
}

function CalcNoOfDays(startDate, endDate) {
	var substringsStart = startDate.split('/');
	var substringsEnd = endDate.split('/');
	
	if (substringsStart[1] == 1)
	{
		// javascript Date() seems to use February as month 1 and January of the following year as month 12
		// ie, 01/01/2008 is 1st Feb 2008, 31/12/2008 is 31st Jan 2009
		substringsStart[1] = 12;
		substringsStart[2]--;
	}
	else
	{
		substringsStart[1]--;
	}
	if (substringsEnd[1] == 1)
	{
		// javascript Date() seems to use February as month 1 and January of the following year as month 12
		// ie, 01/01/2008 is 1st Feb 2008, 31/12/2008 is 31st Jan 2009
		substringsEnd[1] = 12;
		substringsEnd[2]--;
	}
	else
	{
		substringsEnd[1]--;
	}
	
	var startDate = new Date(substringsStart[2],substringsStart[1],substringsStart[0]);
	var endDate = new Date(substringsEnd[2],substringsEnd[1],substringsEnd[0]);
	var difference = Date.UTC(endDate.getFullYear(),endDate.getMonth(),endDate.getDate(),0,0,0) -
		Date.UTC(startDate.getFullYear(),startDate.getMonth(),startDate.getDate(),0,0,0);
	
	return ((difference/1000/60/60/24) + 1);
}

function Left(str, n){
	if (n <= 0)
	    return '';
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return '';
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function dateAdd(inputDate, noOfDays) {
	// THIS IS USED BY THE TRAVEL SITE. noOfDays IS DECREMENTED BY ONE AS FOR TRAVEL BOTH THE START DATE AND END DATE ARE
	// INCLUDED IN THE NUMBER OF DAYS AND TRADITIONAL DATEADD FUNCTIONALITY WOULD CREATE A TIME PERIOD 1 DAY LONGER THAN
	// WE REQUIRE
	
	var substrings = inputDate.split('/');
	if (substrings[1] == 1)
	{
		// javascript Date() seems to use February as month 1 and January of the following year as month 12
		// ie, 01/01/2008 is 1st Feb 2008, 31/12/2008 is 31st Jan 2009
		substrings[1] = 12;
		substrings[2]--;
	}
	else
	{
		substrings[1]--;
	}
	
	var days = Number(noOfDays.replace(/(,|\.)/g, ""));
	days--;
	var futureDate = new Date(substrings[2],substrings[1],substrings[0]);
	if (days == 0)
	{
		futureDate.setDate(futureDate.getDate());
	}
	else
	{
		//Advance today by noOfDays
		futureDate.setDate(futureDate.getDate()+days);
	}
	var month;
	var year;
	year = futureDate.getFullYear();
	month = futureDate.getMonth();
	if (month == 12)
	{
		month = 1;
		year++;
	}
	else
	{
		month++;
	}
	return(futureDate.getDate() + '/' + month + '/' + year);
}

function FormatDate(inputDate) {
	return(inputDate.getDate() + '/' + inputDate.getMonth() + '/' + inputDate.getFullYear());
}

function ValidDate(inputDate) {
	//Assumes dd/mm/ccyy as format
	rx=new RegExp('^\\d{1,2}\\/\\d{1,2}\\/\\d{4}$');
	
	if(rx.exec(inputDate)==null){
		return false;
	}
	
	var substrings=inputDate.split('/');
	var strDay = substrings[0]
	var strMonth = substrings[1]
	var strYear = substrings[2]
	var strDate = strMonth + "/" + strDay + "/" + strYear
	var testDate = new Date(strDate);
	
	return (testDate.getFullYear()==substrings[2] && (testDate.getMonth()+1)==substrings[1] && testDate.getDate()==substrings[0])
}

function NumberOfDaysInMonth (year, month) {
     return 32 - new Date(year, month, 32).getDate();
}
 
function validateInt(string) {
	rx=new RegExp('[^0-9]');
	return rx.exec(trim(string))==null;
}

function trim(string) {
	return string.replace(/^\W+/,'').replace(/\W+$/,'');
}


function validateCurrency(string) {
	rx=new RegExp('^(\\d+\\.{1}\\d{0,2}|\\d+)$');
	return rx.exec(trim(string))==null;
}

function validateTelephoneNumber(string) {
	rx=new RegExp('^[\\d* *]+$');
	return (rx.exec(string)!=null);	
}

function ValidCharacters(string) {
	//rx=new RegExp('[^A-Za-zƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ-\\s\\r\\f]');
	rx=new RegExp('[^A-Za-zƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ\'-\\s\\r\\f]');
	return (rx.exec(string)!=null);
}

function validateEmail(string) {
	rx=new RegExp('^([\\w-]+\\.{1}|[\\w-]+)*[\\w]+\\@([\\w-]+\\.{1}|[\\w-]+)*[\\w]+$');
	return (rx.exec(string)!=null);	
}

//Call this function inline via the blur event, passing in the control to convert to upper case.
function toUpper(InputControl) {
	var strValue = new String(InputControl.value);
	InputControl.value = strValue.toUpperCase();
}

function checkCreditCardExpiry(month, year) {
	// Convert to int and add 2000
	year1 = Number(year);
	year1 = year1 + 2000;

	month1 = Number(month);

	var today = new Date();
	var todayMonth = Number(today.getMonth() + 1);
	var todayYear  = Number(today.getYear());

	if (todayYear < 1000) {
		todayYear = todayYear + 1900;
	}

	if (year1 > todayYear) {
		return false;
	} 
	else if (todayYear == year1) {
		if (month1 >= todayMonth) {
			return false;
		}
	}

	return true;
}
  
function checkCreditCardValidFrom(month, year) {
	// Convert to int and add 2000
	year1 = Number(year);
	year1 = year1 + 2000;

	month1 = Number(month);

	var today = new Date();
	var todayMonth = Number(today.getMonth() + 1);
	var todayYear  = Number(today.getYear());

	if (todayYear < 1000) {
		todayYear = todayYear + 1900;
	}

	if (year1 < todayYear) {
		return false;
	} 
	else if (todayYear == year1) {
		if (month1 <= todayMonth) {
			return false;
		}
	}

	return true;
}
  
function checkPhoneNumber(oStringToCheck){
	var charError = 0;
	var checkOK = "0123456789";
	var myNumber = oStringToCheck.replace(/(,|\-|\(|\)|\+|\s)/g, "");

	for (n = 0; n < myNumber.length; n++){
		if (checkOK.indexOf(myNumber.charAt(n)) == -1 || myNumber.length < 10 || myNumber.length > 20){
			charError++;
		}
	}
	if (charError > 0){
		return true;
	}
	else {
		return false;
	}
}
  
function checkPetDates(acquired_year, acquired_month, age_years, age_months) {
	var today = new Date();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	age_years = strYear - Number(age_years);
	age_months = strMonth - Number(age_months);

	if (age_months < 1) {
		age_years = age_years - 1;
		age_months = 11 + age_months;
	}

	if (age_years > acquired_year) {
		return true;
	} 
	else if (acquired_year == age_years) {
		if (age_months > acquired_month) {
			return true;
		}
	}

	return false;
}
  
function checkPostcodeError(oStringToCheck){
	var postcodeError = 0;

	// strip spaces & convert to upper case
	oStringToCheck = oStringToCheck.replace(/ /g, ""); 
	oStringToCheck = oStringToCheck.toUpperCase();

	size = oStringToCheck.length;

	// incode is the last 3 chars
	var incode  = oStringToCheck.substr(size-3, 3);

	// outcode is everything except the last 3 chars
	var outcode = oStringToCheck.substr(0, size-3);

	// incode must equal 9AA format
	if (incode.match(/[0-9][A-Z][A-Z]/)) { 
		//alert("incode success");
	} 
	else {
		//alert("incode failure");
		postcodeError++;
	}

	// incode must equal A9, A99, AA9, AA99, AA9A, A9A or AAA format
	if (outcode.match(/^[A-Z][0-9]$/)           ||
		outcode.match(/^[A-Z][0-9][0-9]$/)      ||
		outcode.match(/^[A-Z][A-Z][0-9]$/)      ||
		outcode.match(/^[A-Z][A-Z][0-9][0-9]$/) ||
		outcode.match(/^[A-Z][A-Z][0-9][A-Z]$/) ||
		outcode.match(/^[A-Z][0-9][A-Z]$/)      ||
		outcode.match(/^[A-Z][A-Z][A-Z]$/) ) {

		//alert("outcode success");
	}  
	else {
		//alert("outcode failure");
		postcodeError++;
	}

	if (postcodeError > 0){
		return true;
	} 
	else{
		return false;
	}
}
  
  
function checkValidDate(strToday,strMonth,strYear) {
	if (strMonth > 12 || strMonth < 1) {
		return true;
	}
	if ((strMonth == 1 || strMonth == 3 || strMonth == 5 
			|| strMonth == 7 || strMonth == 8 || strMonth == 10 
			|| strMonth == 12) && (strToday > 31 || strToday < 1)) {
		return true;
	}
	if ((strMonth == 4 || strMonth == 6 || strMonth == 9 
			|| strMonth == 11) && (strToday > 30 || strToday < 1)) {
		return true;
	}
	if (strMonth == 2) {
		if (strToday < 1) {
			return true;
		}
		if (LeapYear(strYear) == true) {
			if (strToday > 29) {
				return true;
			}
		} else {
			if (strToday > 28) {
				return true;
			}
		}
	}
	return false;
}
  
function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	} 
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	
	return false;
} 
  
function checkPastYear(year) {
	var today = new Date();
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	if (year > strYear) {
		return true;
	} 
	else {
		return false;
	}
}
  
function checkPastMonthYear(month, year) {
	var today = new Date();
	var strMonth = today.getMonth() + 1;
	var strYear = today.getYear();
	
	if (strYear < 1000) {
		strYear = strYear + 1900;
	}

	if (year > strYear) {
		return true;
	} 
	else if (strYear == year) {
		if (month > strMonth) {
			return true;
		}
	}

	return false;
}

function checkPostcodeErrorOld(oStringToCheck){
	var postcodeError = 0;
	
	if (oStringToCheck.length < 2 || oStringToCheck.length > 8){
		postcodeError++;
	}
	
	if (postcodeError > 0){
		return true;
	} 
	else{
		return false;
	}
}
  
   
function ConvertToDateObject(inputDate){
	//Assumes dd/mm/ccyy as format
	rx=new RegExp('^\\d{1,2}\\/\\d{1,2}\\/\\d{4}$');
	if(rx.exec(inputDate)==null)
		return false;

	var substrings=inputDate.split('/');

	if(substrings.length!=3)
		return false;

	return(new Date(substrings[2],substrings[1],substrings[0]));
}
  
function removeSpaces(oStringToCheck){
	//removes spaces from oStringToCheck
	var myString = oStringToCheck.replace(/ /g,"");
		return myString;
}
