//Common Functions V2.0
//Last Edited By: Roland Corbet, Cyber Media Solutions Ltd.
//Lasted Edited on: 13/03/2003

function errorMsg(msg, form_object) {
	// Purpose:		To show an error mesasge ad an alert and eturn false to setop further processing.
	// Parameters:	form_object	: Name of form element
	//				msg			: The Error to display

	alert(msg);
	if(form_object.type==undefined){
		form_object[0].focus();
	}
	else{
		form_object.focus();
	}
	
	if(form_object.type == "text") {
		form_object.select();
	}
	return false;
}

function TextBox(form_object, errormsg) {
	// Purpose:		To check that a Text Box is not empty.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display
	
	if (form_object.value == "") {
		return(errorMsg(errormsg, form_object));
	}
	return true;
}

function CheckBoxes(form_object, errormsg) {
	// Purpose:		To check that at least one check box has been selected.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display

	var obj_len = form_object.length;
	 
	if (typeof(obj_len) == "undefined") {
		if (form_object.checked) {
			return true;
		}
		else {
			return(errorMsg(errormsg, form_object));
		}
	}
	else {
		for (i=1; i < obj_len +1; i++) {
			if (form_object[i-1].checked) {
				return true;
			}
		}
		return(errorMsg(errormsg, form_object));
	}
}

function Radio(form_object, errormsg) {

	// Purpose:		To check that at radio button is selected in a group.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display

	var obj_len = form_object.length;
	 
	if (typeof(obj_len) == "undefined") {
		if (form_object.checked) {
			return true;
		}
		else {
			return(errorMsg(errormsg, form_object));
		}
	}
	else {
		for (i=1; i < obj_len +1; i++) {
			if (form_object[i-1].checked) {
				return true;
			}
		}
		return(errorMsg(errormsg, form_object));
	}
}

function Select(form_object, errormsg) {
	// Purpose:		To check that an entry has been selected in a single line drop-down field.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display
	if (form_object.selectedIndex == 0) {
		return(errorMsg(errormsg, form_object));
	}
	return true;
}

function PostCode(outcode, incode) {
	// Purpose:		To validate a postcode to E-Gof standards
	// Parameters:	outcode	: Form field containing left hand side of postcode
	//				incode	: Form field containing right hand side of postcode
	if (! PostCodeOutcode(outcode) ) {
		return(errorMsg("Please supply a valid left-hand section to the post code.", outcode));
	}
	else {
		if (! PostCodeIncode(incode) ) {
			return(errorMsg("Please supply a valid right-hand section to the post code.", incode));
		}
	}
	return true;
}

function PostCodeIncode(incode) {
	//E-Gif Compliant Validation
	
	//Capitalise chars
	incode.value = incode.value.toUpperCase();
	var s = incode.value;
	
	switch (s.length) {
	
		case 3:
			//Check Structure (3 Chars)
			//NAA
			re1 = /[0-9][A-Z][A-Z]/;
			if (!re1.test(s)) {
				return false;
			}
			break;
			
		default:
			return false;
			break;
	}
	
	//The second half of the Postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used. 
	re1 = /C|I|K|M|O|V/;
	if (re1.test(s)) {
		return false;
	}
	return true;
}

function PostCodeOutcode(outcode) {
	//E-Gif Compliant Validation
	var s = outcode.value;
	
	//Capitalise chars
	outcode.value = outcode.value.toUpperCase();
	var s = outcode.value;
	
	switch (s.length) {
			
		case 2:
			//Check Structure (2 Chars)
			//AN 
			re1 = /[A-Z][0-9]/;
			if (!re1.test(s)) {
				return false;
			}
			break;
			
		case 3:
			//Check Structure (3 Chars)
			//ANA
			//ANN 
			//AAN 
			re1 = /[A-Z][0-9][A-Z]|[A-Z][0-9][0-9]|[A-Z][A-Z][0-9]/;
			if (!re1.test(s)) {
				return false;
			}
			break;
			
		case 4:
			//Check Structure (4 chars)
			//AANN
			//AANA 
			re1 = /[A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][0-9][A-Z]/;
			if (!re1.test(s)) {
				return false;
			}
			break;
			
		default:
			return false;
			break;
	}
	
	//The letters Q, V and X are not used in the first position.
	re1 = /Q|V|X|[0-9]/;
	c = s.charAt(0);
	if (re1.test(c)) {
		return false;
	}
	
	//The letters I, J and Z are not used in the second position.
	re1 = /I|J|Z/;
	c = s.charAt(1);
	if (re1.test(c)) {
		return false;
	}
	
	//The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W. 
	if (s.length == 3) {
		re1 = /A|B|C|D|E|F|G|H|J|K|S|T|U|W|[0-9]/;
		c = s.charAt(2);
		if (! re1.test(c)) {
			return false;
		}
	}
	
	//The only letters to appear in the fourth position are A, B, E, H, M, N, P, R, V, W, X and Y.
	if (s.length == 4) { 
		re1 = /A|B|E|H|M|N|P|R|V|W|X|Y|[0-9]/;
		c = s.charAt(3);
		if (! re1.test(c)) {
			return false;
		}
	}
	return true;		 
}

function Email(form_object, errormsg) {
	// Purpose:		To check that a field contains a valid e-mail address.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display
	
	var supported = 0;
	
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) {
			supported = 1;
		}
	}

	if (!supported) {
    		return (form_object.indexOf(".") > 2) && (form_object.indexOf("@") > 0);
    }

	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	var r3 = new RegExp("\'|\"");
	
	if ((!r1.test(form_object.value) && r2.test(form_object.value) && !r3.test(form_object.value))){
		return true;
	}
	else{
		return(errorMsg(errormsg, form_object));
	}
}

function ChkLen(form_object, len, test, errormsg) {
	// Purpose:		To check the lenght of a string in an object.
	// Parameters:	form_object	: Name of form element
	//				len			: The lenght to test
	//				test		: the test to perform (eq = equal to, gt = greater than, ln = less than)			
	//				errormsg	: The Error to display
	switch (test) {
			
		case "eq":
			//String must be exactly given length
			if (form_object.value.length != len) {
				return(errorMsg(errormsg, form_object));
			}
			break;
			
		case "lt":
			//String must be less than given length
			if (form_object.length >= len) {
				return(errorMsg(errormsg, form_object));
			}
			break;
			
		case "gt":
			//String must be greater than length
			if (form_object.length <= len) {
				return(errorMsg(errormsg, form_object));
			}
			break;
	}
	
	return true;
}

function ChkType(form_object, type, errormsg) {

	// Purpose:		To check that a field only contains characters of a given type.
	// Parameters:	form_object	: Name of form element
	//				type		: Type of test to perform (A = Alpha, N = Numeric)
	//				errormsg	: The Error to display

	re1 = /[A-Z]|[a-z]/;
	re2 = /[0-9]/;
	
	switch (type) {
			
		case "A":
			//String must be numbersonly
			if ((!re2.test(form_object.value)) && (re1.test(form_object.value)) ) {
				return true;
			}
			else {
				return(errorMsg(errormsg, form_object));
			}
			break;
			
		case "N":
			//String must be numbersonly
			if ((re2.test(form_object.value)) && (!re1.test(form_object.value)) ) {
				return true;
			}
			else {
				return(errorMsg(errormsg, form_object));
			}
			break;
	}
	return true;
}

function URL(form_object, errormsg) {

	// Purpose:		To check that a url is formatted correctly.
	// Parameters:	form_object	: Name of form element
	//				errormsg	: The Error to display
	// Original					: http://sitebilder.com/design/tutorial/mini/js/RegExp/url.php
	
	//re1 = /^(file|http):\/\/\S+\/\S+\.(htm|html)$/i - use this if a filename is needed on end of url.
	
	re1 = /^(file|http):\/\/\S+$/i;
	
	if (! re1.test(form_object.value)) {
		return(errorMsg(errormsg, form_object));
	}
	return true;
}

function ChkDate(DayPart, MonthPart, YearPart) {
	// Purpose:    To check a date is valid
	// Parameters: 	DayPart		: Form field containing the day.
	//	       		MonthPart	: Form field containing the month.
	//	       		YearPart	: Form field containing the year.

	var minYear=2003;
	var maxYear=2010;

	// Check that the  date is a valid number	
	if ( isNaN(DayPart.value) ) {
		return(errorMsg('The day must be a number.', DayPart));
	}
	
	if (isNaN(MonthPart.value)) {
		return(errorMsg('The month must be a number.', MonthPart));
	}
	
	if (isNaN(YearPart.value)) {
		return(errorMsg('The year must be a number.', YearPart));
	}
	
	//Tidy the format of the string for single chars.
	if (DayPart.value >= 0  && DayPart.value <= 9 && DayPart.value.length == 1) {
		DayPart.value = "0" + DayPart.value;
	}
	
	//Tidy the format of the string for single chars.
	if (MonthPart.value >= 0  && MonthPart.value <= 9 && MonthPart.value.length == 1) {
		MonthPart.value = "0" + MonthPart.value;
	}

	// Check days, months and years are valid
	if (DayPart.value < 1 || DayPart.value > 31) {
		return(errorMsg('Please enter a valid day.', DayPart));
	}

	if (MonthPart.value < 1 || MonthPart.value > 12) {
		return(errorMsg('Please enter a valid month.', MonthPart));
	}

	if (YearPart.value.length != 4 || YearPart.value==0 || YearPart.value<minYear || YearPart.value>maxYear){
		return(errorMsg('Please enter a valid 4 digit year between ' + minYear + ' and ' + maxYear + '.', YearPart));
	}
	
	// Check the days of the month are valid
	if (DayPart.value > 30 && (MonthPart.value == 9 || MonthPart.value == 4 || MonthPart.value == 6 || MonthPart.value == 11)) {
		return(errorMsg('Please enter a valid day for this month.', DayPart));
	}

	// check for february 29th
	if (MonthPart.value == "2" || MonthPart.value == "02") {
		var isleap = (YearPart.value % 4 == 0 && (YearPart.value % 100 != 0 || YearPart.value % 400 == 0))
		if (DayPart.value >29 || (DayPart.value == 29 && !isleap)) {
			return(errorMsg('This year is a leap-year, please enter a valid day.', DayPart));
		}
	}
	
	return true;
}

function ChkTime(hour, minute) {
	// Purpose:    To check a time is valid
	// Parameters: 	hr	: Form field containing the hour.
	//	       		mn	: Form field containing the minutes.

	// Check that the hour and minute are valid numbers	
	if (isNaN(hour.value)) {
		return(errorMsg('The hour must be a number.', hour));
	}
	
	if (isNaN(minute.value)) {
		return(errorMsg('The minute must be a number.', minute));
	}
	
	//Tidy the format of the string for single chars.
	if (hour.value >= 0  && hour.value <= 9 && hour.value.length == 1) {
		hour.value = "0" + hour.value;
	}
	
	//Tidy the format of the string for single chars.
	if (minute.value >= 0  && minute.value <= 9 && minute.value.length == 1) {
		minute.value = "0" + minute.value;
	}

	//Check for valid number ranges
	if (hour.value == "" || hour.value < 0  || hour.value > 23) {
		return(errorMsg('Hour must be between 0 and 23.', hour));
	}
	
	if (minute.value == "" || minute.value < 0 || minute.value > 59) {
		return(errorMsg('Minute must be between 0 and 59.', minute));
	}
		
	return true;	
}

function CmpDateTime(y1, m1, d1, hr1, mn1, y2, m2, d2, hr2, mn2) {
	// Purpose:    To check if a date is greater than another date.
	// Parameters: 	y1	: Form field containing the year of date 1
	//	       		m1	: Form field containing the month of date 1
	//	       		d1	: Form field containing the day of date 1
	//				y2	: Form field containing the year of date 2
	//	       		m2	: Form field containing the month of date 2
	//	       		d2	: Form field containing the day of date 2
	
	//Set 1 day in milliseconds
	var one_day=1000*60*60*24
	
	var date1 = new Date(y1.value, m1.value-1, d1.value, hr1.value, mn1.value);
	var date2 = new Date(y2.value, m2.value-1, d2.value, hr2.value, mn2.value);
	
	var diff = Math.ceil((date2.getTime()-date1.getTime())/(one_day));
	
	if (diff <= 0) {
		return(errorMsg('The even must not end before it starts.', d2));
	}	
	return true;
}