/****************************************************************************/
/***		Client-side JavaScript function library														***/
/***		Copyright Apache Solutions Ltd 2005																***/
/***		(unauthorised use of this code will result in prosecution by law)	***/
/****************************************************************************/

//
//	Returns true if the given variable is blank:
//
function IsBlank(value) {
	// cast to string.
	var str = "" + value;
	return ((str == "") || (str == " ") || (str == "undefined") || (str == "null") || (str == null));
}

//
//	Normalises the text in the field (i.e. makes first char of every word uppercase):
//
//	N.B	Case of characters other than 1st letter are unaffected, e.g. MA (for state) remains as is.
//	Also removes leading spaces.
//
function Normalise(field) {
	var retval = '';
	var set_upper = false;
	var first_char = false;
	for (i=0; i < field.value.length; i++) {
		var c = field.value.charAt(i);
		if (c == ' ') {
			if (first_char == false) {
				continue;
			}
			set_upper = false;
		}
		if (set_upper == false && c != ' ') {
			c = c.toUpperCase();
			set_upper = true;
			first_char = true;
		}
		retval += c;
	}
	return retval;
}

//
//	Opens the popup window.
//
function Open_PDF(pdf_no) {
	var url = '../inc/pdfdownload.asp?pdfid='+pdf_no
	var popupwindow = window.open(url, 'popupwindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=800,height=600');
	popupwindow.focus();
}
//-->

//
//	Checks whether the user has entered data in the given field, displaying the given message,
//	focusing the field and returning false if not:
//
function CheckMandatory(field, msg) {
	if (IsBlank(field.value)) {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}	

//
//	Checks to see if a valid postcode has been entered.
//
function Make_Uppercase(field) {
	// change the postcode to uppercase.
	var retval = field.value;
	retval = retval.toUpperCase();
	return retval;
}

//
//	The CheckEmail function has been moved to the head.inc file as it is used in the footer.
//
//
//	Checks whether the user has entered a valid email address in the given field, displaying an appropriate message,
//	focusing the field and returning false if not:
//
//	N.B Use with 'CheckMandatory' fn above if the field is also mandatory.
//
function CheckEmail(field) {
	if (!IsBlank(field.value)) {
		// ensure that only valid characters have been used.
		var ok = "1234567890qwertyuiopasdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for (i=0; i < field.value.length; i++) {
			if (ok.indexOf(field.value.charAt(i)) < 0) {
				alert("The email address you have entered contains an invalid character.\nIt should look something like 'name@company.com'");
				field.focus();
				return false;
			}
		}
		// on IE4+, perform additional format validation.
		if (document.images) {
	  	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	  	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			if (!(!field.value.match(re) && !(!field.value.match(re_two)))) {
				alert("The email address you have entered is invalid.\nIt should look something like 'name@company.com'");
				field.focus();
				return false;
			}
	 	}
		// otherwise, just make sure the at sign isn't at the start or end.
		else {
			var atPos = field.value.indexOf('@');
			if (atPos < 1 || atPos == (field.value.length - 1)) {
				alert("The email address you have entered is invalid.\nIt should look something like 'name@company.com");
				field.focus();
				return false;
			}
		}
	}
	return true;	
}

//
//	Checks whether the user has selected an option from the list, focusing the field
//	and returning false if not:
//
function CheckMandatoryList(field, msg) {
	if (field.options[field.selectedIndex].value == 0) {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}	

//
//	Checks whether the user has checked a checkbox, focusing the field
//	and returning false if not:
//
function CheckMandatoryCheckBox(field, msg) {
	if (field.checked == false) {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}	

//
//	Checks whether the user has selected a radio button in the given radio group, displaying the given message,
//	focusing the first radio button in the group and returning false if not:
//
function CheckMandatoryRadios(field, msg) {
	for (var n=0; n < field.length; n++) {
		if (field[n].checked) {
			return true;
		}
	}
	alert(msg);
	field[0].focus();
	return false;
}

//
//	Checks whether the user has entered data in the given fields, and if so checks that the same data
//	has been entered in both, and the data is of the a minimum length, displaying the appropriate message,
//	focusing the appropriate field and returning false if not:
//
//	N.B Minimum length set at five characters.
//
function CheckPasswords(field1, field2) {
	if (field1.value != field2.value) {
		alert("Please check that you typed the same password into the second box.");
		field2.focus();
		return false;
	}
	else if (!IsBlank(field1.value) && (field1.value.length < 5 || field2.value.length < 5)) {
		alert("Please ensure your password is at least five characters long.");
		field1.focus();
		return false;
	}
	return true;
}	

//
//	Checks whether the user has entered an acceptable telephone or fax number,
//	focusing the field and returning false if not:
//
//	N.B Acceptable characters set as "0-9" and spaces.
//			Use with 'CheckMandatory' fn if the field is also mandatory.
//
function CheckTel(field) {
	for (i=0; i < field.value.length; i++) {
		var c = field.value.charAt(i);
		if ((c < '0' || c > '9') && c != ' ') {
			alert("Please limit your entry to the following characters: '0-9'");
			field.focus();
			return false;
		}
		if (field.value.length < 7) {
			alert("Please check you have entered all the correct digits (including your area code).");
			field.focus();
			return false;
		}
	}
	return true;
}

//
//	Checks whether the user has entered an acceptable length for a particular field.
//	useful for sort-codes, account numbers etc.
//	focusing the field and returning false if not:
//
//	N.B Acceptable characters set as "0-9" and spaces.
//			Use with 'CheckMandatory' fn if the field is also mandatory.
//
function CheckFieldLength(field, lenlimit, msg) {
	for (i=0; i < field.value.length; i++) {
		var c = field.value.charAt(i);
		if ((c < '0' || c > '9') && c != ' ') {
			alert("Please limit your entry to the following characters: '0-9'");
			field.focus();
			return false;
		}
		if (field.value.length != lenlimit) {
			alert(msg);
			field.focus();
			return false;
		}
	}
	return true;
}

//
//	Checks to see if a valid postcode has been entered.
//
function CheckPostcode(field) {
	// check a postcode has been entered.
	if (IsBlank(field.value)) {
		alert("Please enter a postcode");
		field.focus();
		return false;
	}
	else {
		// correct and check the format of the postcode.
		
		var the_postcode = ""; var done_check = false;
		// strip out leading spaces.
		for (i=0; i < field.value.length; i++) {
			var c = field.value.charAt(i);
			if (!IsBlank(c)) {
				the_postcode += c;
				done_check = true;
			}
			else if (done_check == true) {
				the_postcode += c;
			}
		}
		// change the postcode to uppercase.
		the_postcode = the_postcode.toUpperCase();
		
		// do postcode format checks - checks are based on royal mail description of postcode (seen below).
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Part 1 - The first one or two letters is the postcode area and it identifies the main Royal Mail sorting office 
		//				  which will process the mail. In this case EC would go to the Mount Pleasant sorting office in London.
		//
		// Part 2 - The second part is usually just one or two numbers but for some parts of London it can be a number and a
		//				  letter. This is the postcode district and tells the sorting office which delivery office the mail should go to.
		//
		// Part 3 - This third part is the sector and is usually just one number. This tells the delivery office which local 
		//				  area or neighbourhood the mail should go to.
		//
		// Part 4 - The final part of the postcode is the unit code which is always two letters. This identifies a group of up to
		//				  80 addresses and tells the delivery office which postal route (or walk) will deliver the item.
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		// check for correct length.
		if (the_postcode.length < 6 || the_postcode.length > 8) {
			alert("The postcode format is invalid - it is the wrong length");
			field.focus();
			return false;
		}
		
		// check that the first character is not a number.
		if (!isNaN(the_postcode.charAt(0))) {
			alert("The postcode format is invalid - it cannot start with a number");
			field.focus();
			return false;
		}
		
		// check that the first character of second part of the postcode is a number.
		if (isNaN(the_postcode.charAt(the_postcode.length-3))) {
			alert("The postcode format is invalid - second section must start with a number");
			field.focus();
			return false;
		}
		
		// check that the last two characters are letters.
		if (!isNaN(the_postcode.charAt(the_postcode.length-1)) && !isNaN(the_postcode.charAt(the_postcode.length - 2))) {
			alert("The postcode format is invalid - the last two digits must be letters");
			field.focus();
			return false;
		}
		
		// check that the postcode space is in the correct position.
		if (the_postcode.charAt(the_postcode.length-4) != " " && the_postcode.charAt(the_postcode.length-5) != " ") {
			alert("The postcode format is invalid - there is no space or the space is in the wrong position");
			field.focus();
			return false;
		}
		
		// check that there is only one space in the postcode.
		var no_spaces = 0;
		for (i=0; i < the_postcode.length; i++) {
			var c = the_postcode.charAt(i);
			if (IsBlank(c)) {
				no_spaces++;
			}
		}
		if (no_spaces > 1) {
			alert("The postcode format is invalid - only one space is allowed");
			field.focus();
			return false;
		}

		// return the newly amended postcode.
		field.value = the_postcode;
	}
	return true;
}	

//
//	Checks textarea for strings of over passed value len characters to prevent SQL database errors:
//
function CheckTextareaLength(field, type, thelen) {
	if (field.value.length > thelen) {
		var characters = (thelen > 1 ? ' characters':' character');
		alert("Please restrict the " + type + " to a maximum of " + thelen + characters + " - it is currently " + field.value.length + " characters long.");
		field.focus();
		return false;
	}
	return true;
}

//
//	Checks whether the user has entered acceptable characters, displaying the given message,
//	focusing the field and returning false if not:
//
//	N.B Acceptable characters set as "a-z A-Z 0-9 -" and optionally spaces too.
//			Use with 'CheckMandatory' fn if the field is also mandatory.
//
function CheckCharacters(field, msg, accept_spaces) {
	for (i=0; i < field.value.length; i++) {
		var c = field.value.charAt(i);
		if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && (c != '-') && ((accept_spaces && c != ' ') || !accept_spaces)) {
			alert(msg);
			field.focus();
			return false;
		}
	}
	return true;
}

//
//	Checks whether the user has entered acceptable numeric characters, displaying the message,
//	focusing the field and returning false if not:
//
//	N.B Acceptable characters set as "1-9" and optionally "0" and "." and "-" too.
//
function CheckNumbers(field, accept_zeros, accept_fractions, accept_minus) {
	for (i=0; i < field.value.length; i++) {
		var c = field.value.charAt(i);
		if (((accept_minus && c != '-') || !accept_minus) && (c < '1' || c > '9') && ((accept_zeros && c != '0') || !accept_zeros) && ((accept_fractions && c != '.') || !accept_fractions)) {
			alert("Please limit your entry to the following characters: '0-9'");
			field.focus();
			return false;
		}
	}
	return true;
}

//
//	Checks a currency value, ensuring it is between 0 and 214,748.3647
//	(the maximum that an SQL Server currency field can handle), returning
//	false if not:
//
//	N.B Use with 'CheckMandatory' fn if the field is also mandatory.
//
function CheckCurrency(field) {
	if (!IsBlank(field.value)) {
		var value = parseFloat(field.value);
		// n.b last check required as 'isNAN' returns false for numbers plus characters (eg; '123abc').
		if (isNaN(value) || value < 0 || value > 214748.3647 || value != field.value) {
			alert("Please enter only numbers and limit your numbers between (0 - 99999).");
			field.focus();
			return false;
		}
	}
	return true;
}

//
//	Checks whether the user has entered zero:
//
function CheckZero(field) {
	var c = field.value;
	if ((c == '0')) {
		alert("The level must be higher than zero.");
		field.focus();
		return false;
	}
	return true;
}

//
//	Checks to see if the year is a leap year, returning true if it is.
//
function IsLeapYear(theyear) {
	if (theyear % 4 == 0) {
		if (theyear % 100 == 0) {
			if (theyear % 400 == 0) {
	    	LeapYear = true;
			}
			else {
	    	LeapYear = false;
			}
		}
		else {
			LeapYear = true;
		}
	}
	else {
		LeapYear = false;
	}
	return LeapYear;
}

// If not mandatory if true then dont perform the check if all elements of the date are 0.
//
function CheckDate(year, month, day, field, not_mandatory) {
	// create a date object and check whether it sets the same date.
	var perform_check = true;
	if (not_mandatory == true) {
		if (year == "0" && month == "-1" && day == "0") perform_check = false;
	}
	
	if (perform_check == true) {
		var date = new Date(year, month, day);
		if (year != date.getFullYear() || month != date.getMonth() || day != date.getDate()) {
			alert("The date you have specified is invalid - please check it and try again.");
			field.focus();
			return false;
		}
	}
	return true;		
}

//
//	Opens a popup window.
//
function OpenPopupWindow(url, width, height) {
	var popupwindow = window.open(url, 'popupwindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
	popupwindow.focus();
}

// changes the number of days listed in the given 'day' list according to the month chosen in the given 'month' list:
// takes into account leap years where february is one day longer in length.
function ChangeDateSelect(theday, themonth, theyear, title) {
	if (IsIE4Plus()) {
		if (title == true) {
			if (document.all[themonth].value == "2" || document.all[themonth].value == "4" || document.all[themonth].value == "6"
				|| document.all[themonth].value == "9" || document.all[themonth].value == "11") {
				if (document.all[themonth].value == "2") {
					if (IsLeapYear(document.all[theyear].value )) {
						document.all[theday].length = "30";
						document.all[theday].options[29].value = "29";
						document.all[theday].options[29].text = "29th";
					}
					else {
						document.all[theday].length = "29";
					}
				}
				else {
					document.all[theday].length = "31";
					document.all[theday].options[29].value = "29";
					document.all[theday].options[29].text = "29th";
					document.all[theday].options[30].value = "30";
					document.all[theday].options[30].text = "30th";
				}												
			}
			else {
				document.all[theday].length = "32";
				document.all[theday].options[29].value = "29";
				document.all[theday].options[29].text = "29th";
				document.all[theday].options[30].value = "30";
				document.all[theday].options[30].text = "30th";
				document.all[theday].options[31].value = "31";
				document.all[theday].options[31].text = "31st";
			}
		}
		else {
			if (document.all[themonth].value == "2" || document.all[themonth].value == "4" || document.all[themonth].value == "6"
				|| document.all[themonth].value == "9" || document.all[themonth].value == "11") {
				if (document.all[themonth].value == "2") {
					if (IsLeapYear(document.all[theyear].value )) {
						document.all[theday].length = "29";
						document.all[theday].options[28].value = "29";
						document.all[theday].options[28].text = "29th";
					}
					else {
						document.all[theday].length = "28";
					}
				}
				else {
					document.all[theday].length = "30";
					document.all[theday].options[28].value = "29";
					document.all[theday].options[28].text = "29th";
					document.all[theday].options[29].value = "30";
					document.all[theday].options[29].text = "30th";
				}												
			}
			else {
				document.all[theday].length = "31";
				document.all[theday].options[28].value = "29";
				document.all[theday].options[28].text = "29th";
				document.all[theday].options[29].value = "30";
				document.all[theday].options[29].text = "30th";
				document.all[theday].options[30].value = "31";
				document.all[theday].options[30].text = "31st";
			}
		}
	}
}