
	function ValueCheck(StrValue,CheckValue, strMsg)
	{
		if (StrValue == CheckValue)
		{ 
			if (fCheck)
			{                     
				alert(strMsg)
				fCheck = false
			}
		}
	}

	function NONValueCheck(StrValue,CheckValue, strMsg)
	{
		if (StrValue != CheckValue)
		{ 
			if (fCheck)
			{                     
				alert(strMsg)
				fCheck = false
			}
		}
	}

	function PasswordCheck(StrValue,CheckValue, strMsg)
	{
		if (StrValue != CheckValue)
		{ 
			if (fCheck)
			{                     
				alert(strMsg)
				fCheck = false
			}
		}
	}

	function IntCheck(IntValue, strMsg)
	{
		if (isNaN(IntValue))
		{   
			if (fCheck)
			{                    
				alert(strMsg)
				fCheck = false
			}
		}
	}


	function LengthCheck(StrValue, FieldName, MaxLength)
	{
		if (StrValue.length > MaxLength)
		{   
			if (fCheck)
			{   
				var substr = StrValue.substring(StrValue.length,MaxLength) 
             
				alert("you have exceeded the " + MaxLength + " character limit for the " + FieldName + " field\nthe following text will need to be removed:\n\n" + substr)
				fCheck = false
			}
		}
	}


	function SelectCheck(OptionValue,CheckValue, strMsg)
	{
		if (OptionValue == CheckValue)
		{ 
			if (fCheck)
			{                     
				alert(strMsg)
				fCheck = false
			}
		}
	}

	function IsHttp(StrValue, strMsg)
	{

		var reg_exp1 = new RegExp("http://")
		var reg_exp2 = new RegExp("https://")
		var reg_exp3 = new RegExp("ftp://")

		if ((StrValue.search(reg_exp1) != 0) && (StrValue.search(reg_exp2) != 0) && (StrValue.search(reg_exp3) != 0))
		{ 
			if (fCheck)
			{                     
				alert(strMsg)
				fCheck = false
			}
		}
	}


	function testUrl(which_field)
	{
		fCheck = true
		// Which_field is compiled on the page as this.form.field_name
		ValueCheck(which_field.value, "", "You must first enter a url to test");
		IsHttp(which_field.value, "Your url must be prefixed by one of the following protocols in order to link correctly\n\nA. 'http://'\nB. 'https://'\nC. 'ftp://'");

		if (fCheck)
		{
			window.open(which_field.value , "displaywindow", "width=400,height=300,toolbar=no,scrollbars=yes,resizable=yes,menubar=no,status=no,directories=no,location=yes");
		}
	}


	function PhoneNumCheck(strFieldValue, FieldName)
	{

		var PhonePat1 = /\d{2}\s\d{4}\s\d{4}/ 		// XX XXXX XXXX
		var PhonePat2 = /\d{4}\s\d{3}\s\d{3}/ 		// XXXX XXX XXX
		var PhonePat3 = /\d{2}\s\d{2}\s\d{2}/		// XX XX XX

		// if (str is blank or ((is 12 digit matching XX XXXX XXXX) OR (is 12 digit matching XXXX XXX XXX) OR (is 8 digit matching XX XX XX)))

	 	if ((strFieldValue == "") || ((strFieldValue.search(PhonePat1) == 0) && (strFieldValue.length == 12)) || ((strFieldValue.search(PhonePat2) == 0) && (strFieldValue.length == 12)) || ((strFieldValue.search(PhonePat3) == 0) && (strFieldValue.length == 8)))
		{    
			return
		} else {                  
			if (fCheck)
			{
				alert(FieldName + " must: \n\nA. contain numbers and spaces only\nB. conform to one of the following patterns:\n\n1. 00 0000 0000\n2. 0000 000 000\n3. 000 000")
				fCheck = false
			}
		}
	}


	function ISBNCheck(strFieldValue)
	{

		var myReg = /[0-9]{9}[0-9Xx]/ 		// 0123456789X
		var sub_msg = ""

	 	if ( strFieldValue.search(myReg) == 0 && strFieldValue.length == 10 )
		{    
			return
		} else {                  
			if (fCheck)
			{
				if (strFieldValue.length != 10) {
					sub_msg = "\nISBN must be 10 digits long, The number you provided was " + strFieldValue.length + " digits long"
				}
				alert("Please enter the complete ISBN without spaces or hyphens" + sub_msg)
				fCheck = false
			}
		}
	}


	function CreditCardCheck(strFieldValue)
	{

		var myReg = /[0-9]{16}/ 		// XXXXXXXXXXXXXXXX
		var sub_msg = ""

	 	if ( strFieldValue.search(myReg) == 0 && strFieldValue.length == 16 )
		{    
			return
		} else {                  
			if (fCheck)
			{
				if (strFieldValue.length != 16) {
					sub_msg = "\nCredit card number must be 16 digits long, The number you provided was " + strFieldValue.length + " digits long"
				}
				alert("Please enter the complete credit card number without spaces" + sub_msg)
				fCheck = false
			}
		}
	}

	function EmailCheck(emailStr) {

	if ((fCheck != false) && (emailStr !="")) {
		fCheck = false

		var emailPat		= /^(.+)@(.+)$/
		var specialChars	= "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars		= "\[^\\s" + specialChars + "\]"
		var quotedUser		= "(\"[^\"]*\")"
		var ipDomainPat		= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom			= validChars + '+'
		var word			= "(" + atom + "|" + quotedUser + ")"
		var userPat 		= new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat		= new RegExp("^" + atom + "(\\." + atom +")*$")

		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
			alert("email address seems incorrect\ncheck that it contains one '@' and at least one full stop character")
			return false
		}

		var user 	= matchArray[1]
		var domain 	= matchArray[2]
		if (user.match(userPat)==null) {
	    	// user is not valid
		    alert("the username doesn't seem to be valid")
		    return false
		}

		var IPArray = domain.match(ipDomainPat)
		if (IPArray!=null) {
		    // this is an IP address
			  for (var i=1;i<=4;i++) {
			    if (IPArray[i]>255) {
			        alert("destination IP address is invalid")
				return false
			    }
		    }
		    return true
		}

		var domainArray = domain.match(domainPat)
		if (domainArray==null) {
			alert("The domain name doesn't seem to be valid.")
		    return false
		}

		var atomPat = new RegExp(atom,"g")
		var domArr 	= domain.match(atomPat)
		var len		= domArr.length
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
		   alert("The address must end in a three letter domain, or two letter country code")
		   return false
		}

		if (len<2) {
		   alert("This address appears to be missing a hostname")
		   return false
		}

		fCheck = true
		return true;
		}
	}

	function Validate_date(date_field)
	{
		if (fCheck){

			var my_arr 		= date_field.split("/");
			var format_msg 	= "\n\ndates must be in the format DD/MM/YYYY"

			if (my_arr.length != 3){
				if (my_arr.length == 1){
					alert("the date string provided did not contain any slashes [/] which are used to seperate day/month/year values\nthe date was unable to be validated");
				} else {
					alert("the date string provided contained " + (my_arr.length - 1) + " slashes [/] instead of 2\nthe date was unable to be validated");
				}
				fCheck = false;
				return;
			} 

			// COMFIRM THAT PROVIDED VALUES ARE ALL INTEGERS
			for(var m = 0; m < my_arr.length; m++){
				if (isNaN(my_arr[m])){
					alert("the date string must contain only numbers, please change '" + my_arr[m] + "'");
					fCheck = false;
					return;
				}
			}

			// initialise
			this_day	= my_arr[0];
			this_month	= my_arr[1];
			this_year	= my_arr[2];

			month_arr 	= ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']

			if (this_year.length != 4){
				alert("please enter a four digit year" + format_msg);
				fCheck = false;
				return;
			}

			if (this_month > 12 || this_month < 1){
				alert("there can only be 12 months in a year, please use a number from 1 - 12" + format_msg);
				fCheck = false;
				return;
			}

			if (this_day < 1){
				alert("the day value must be a number greater than 0" + format_msg);
				fCheck = false;
				return;
			}

			if (this_month == 2){
				if (this_year % 4 == 0 && (this_year % 100 != 0 || this_year % 400 == 0)){
					if (this_day > 29){
						alert("there can only be 29 days in " + month_arr[this_month-1] + " on a leap year");
						fCheck = false;
						return;
					}
				} else {
					if (this_day > 28){
						alert("there can only be 28 days in " + month_arr[this_month-1]);
						fCheck = false;
						return;
					}
				}	
			}

			if (((this_month == 4) || (this_month == 6) || (this_month == 7) || (this_month == 11)) && (this_day > 30)) {
				alert("there can only be 30 days in " + month_arr[this_month-1]);
				fCheck = false;
				return;
			}

			if (this_day > 31){
				alert("there can not be more that 31 days in " + month_arr[this_month-1]);
				fCheck = false;
				return;
			}		
		}

	}

	function Validate_datediff(start_date_name, start_date_value, end_date_name, end_date_value, error_msg){

		Validate_date(start_date_value);
		Validate_date(end_date_value);

		if (fCheck) {

			var start_arr 		= start_date_value.split("/");
			var end_arr 		= end_date_value.split("/");

			// initialise
			var is_error = 1
			var error_str = ""

			start_day		= parseInt(start_arr[0],10);
			start_month		= parseInt(start_arr[1],10);
			start_year		= parseInt(start_arr[2],10);

			end_day			= parseInt(end_arr[0],10);
			end_month		= parseInt(end_arr[1],10);
			end_year		= parseInt(end_arr[2],10);

			if (start_year > end_year){
				error_str = "year"
			} else {
				if ((start_month > end_month) && (start_year == end_year)){
					error_str = "month"
				} else {
					if ((start_day > end_day) && (start_month == end_month) && (start_year == end_year)){
						error_str = "day"
					} else {
						is_error = 0
					}
				}
			}
			if (is_error == 1){
				alert(error_msg + "the selected " + start_date_name + " (" + start_date_value + ") occurs after " + end_date_name + " (" + end_date_value + ")");
				//alert(error_msg + "the selected " + start_date_name + " (" + start_date_value + ") occurs after " + end_date_name + " (" + end_date_value + ") \nthe offending value is the " + error_str + " field");
				fCheck = false;
				return;
			}
		}
	}


	function CheckRanking(which_form, field_name, exclusion_str) {

		loop_count = 0
		fCheck = true

		with (window.document) {
			this_value = which_form.elements.length;
			final_array = new Array();

			for ( i=0; i < this_value; i++ ) {
				item_name = which_form.elements[i].name;

				if (item_name == field_name) {

					for(var m = 0; m < which_form.elements[i].options.length; m++){
						if(which_form.elements[i].options[m].selected){
							item_value = which_form.elements[i].options[m].value
							//	alert("item_value = " + item_value);
						}
					}

					tempstr = item_value;
					finalstr = tempstr.split("_");

					if (finalstr[1] != exclusion_str) {

						final_array[loop_count] = finalstr[1];
    
						for ( n=0; n < loop_count; n++) {
							if (fCheck){
	
								if (final_array[n] == finalstr[1]) {
									fCheck = false;
									alert("there is more than one item ranked in position " + finalstr[1] + "\nplease make sure that all rankings are unique and try again");
									//	alert("failed test\n" + final_array[n] + " = " + finalstr[1]);
								}
								//else {
									//	alert("passed test\n" + final_array[n] + " != " + finalstr[1]);
								//}
							}
						}
						loop_count++;

					} // finalstr[1] is not equal to the exclusion_str value [999]
				}
			}
		}
		if (fCheck) {
			which_form.submit()
		}
	}

	function AtLeastOneSelected(which_form, element_name, check_mode, alert_message){
		if (fCheck) {
			fCheck = false;
			with (window.document) {
				for ( i=0; i < which_form.elements.length; i++ ) {
					current_name = which_form.elements[i].name
					if (current_name.substring(0,element_name.length) == element_name) {
						if ((check_mode == "checkbox" && which_form.elements[i].checked == true) || (check_mode == "text_field" && which_form.elements[i].value != "")) {
							fCheck = true;
							break;
						}
					}
				}
			}
			if (fCheck == false){

				alert(alert_message);
			}
		}
	}