<!-- hide from JavaScript-challenged browsers
	
	/* =============================================================================
	This file contains all the common/generic javascript functions that can
	be used on the website. If adding a function, please preceed each function with 
	the function info (as set out below), leaving five blank lines between each 
	function.
	If you are wishing to access any of the functions below you must place the
	following (simply copy):
	<script language="JavaScript" src="/_js/fns_javascript.js" type="text/javascript"></script>
	and place in the head section of the web page.
	============================================================================= */



	/* =============================================================================
	Function:	
	Purpose:
	Parameters:	
	Returns:	
	Calling:	
	Notes:		
	============================================================================= */



	
	/* =============================================================================
	Function:	checkBlank
	Purpose:	Checks whether form text field(s) are blank or not.
	Parameters:	* form field name [req] - the field from the form you want to test
				* "error message" [req] (string) - display text
	Returns:	Boolean. True if field is not blank, false if field is blank
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
  					var isOK = checkBlank(form.name, "your name.",
										  form.address, "your address.");
  					if (!isOK) {
    					return false;
  					} 
				}
	Notes:		Multiple pairs can be entered, you must have a comma separating all arguments.
				Limitations - Returns true if just a whitespace has been entered.
	============================================================================= */
	function checkBlank() {
  		for (var i = 0; i < arguments.length; i += 2) {
			if (!arguments[i].value) {
				alert("Please enter " + arguments[i+1]);
      			arguments[i].focus();
      			return false;
    		}
  		}
  		return true;
	}





	/* =============================================================================
	Function:	checkList
	Purpose:	Checks whether an item has been selected from a drop-down list.
	Parameters:	* form field name [req] - the field from the form you want to test
				* "error message" [req] (string) - display text must be enclosed in double quotation marks.
	Returns:	Boolean. True if an item has been selected, False if no item has been selected.
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
				  var isSelected = checkList(form.state, "your state.");
				  if (!isSelected) {
				    return false;
				  } 
				}
	Notes:		Limitations - You must have a null option in the first position e.g. "-select-" or ""
	============================================================================= */
	function checkList() {
	  if (arguments[0].selectedIndex == 0) {
	    alert ("Please select " + arguments[1]);
	    arguments[0].focus();
	    return false;
	  }
	  else return true;
	}




	/* =============================================================================
	Function:	checkNum
	Purpose:	Checks whether form text field(s) contain a number or not.
	Parameters:	* form field name [req] - the field from the form you want to test
				* "error message" [req] (string) - display text
	Returns:	Boolean. True if field is a number, false if field is not a number.
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
  					var isNum = checkNum(form.amount1, "your amount1.",
										  form.amount2, "your amount2.");
  					if (!isNum) {
    					return false;
  					} 
				}
	Notes:		Multiple pairs can be entered, you must have a comma separating all arguments.
				Limitations - Returns true if just a whitespace has been entered.
	============================================================================= */
	function checkNum() {
		for (var i = 0; i < arguments.length; i += 2) {
			if (arguments[i].value) {
				if (isNaN(arguments[i].value)) {
					alert("Please enter a number only for " + arguments[i+1]);
					arguments[i].focus();
					return false;
				}
			}
		}
		return true;
	}




	/* =============================================================================
	Function:	checkRadio
	Purpose:	Checks whether a radio button has been selected from a group.
	Parameters:	* form field name [req] - the radio button group you want to test
            	* "error message" [req] (string) - display text must be enclosed in double quotation marks.
	Returns:	Boolean. True if an radio button has been selected, False if no radio button has been selected.
	Calling:	Usually used in the form validate function: 
				<form method="post" action="destination.asp" onsubmit="return validate(this)">
				* Validate function code
				function validate(form) {
				  var isSelected = checkRadio(form.radioGroupName, "an option from the radio button group.");
				  if (!isSelected) {
				    return false;
				  }
				}
	Notes:		Limitations - This function is only useful if no radio button has been pre-selected when page is loaded.
	============================================================================= */
	function checkRadio() {
	  var checkedButton = false;
	  var question = arguments[0];
	  var message = arguments[1];
	  for (var b = 0; b < question.length; b++) {
		if (question[b].checked) {
		  checkedButton = true;
		}
	  }
	  if (checkedButton == false) {
		alert("Please select " + message);
		question[0].focus();
	  }
	  return checkedButton;
	} //end of function checkRadio



	/* =============================================================================
	Function:	maintainCheck
	Purpose:	Maintains only one checkbox is selected from a group of checkboxes.
	Parameters:	* checkbox number [req] - starts with 1
	Returns:	Boolean. True if an radio button has been selected, False if no radio button has been selected.
	Calling:	* You call the maintainCheck function using the onClick event when adding checkboxes to the form:
  				<input type="checkbox" name="checkBox1" value="1" onClick="maintainCheck(1)">
  				<input type="checkbox" name="checkBox2" value="1" onClick="maintainCheck(2)">
  				<input type="checkbox" name="checkBox3" value="1" onClick="maintainCheck(3)">
				* You must declare an array with all the CheckBox names to be included in the group
  				var checkBoxArray = new Array (checkBox1, checkBox2, checkBox3);
	Notes:		
	============================================================================= */
	function maintainCheck(checkThis){
		for (var i=1; i<=checkBoxArray.length; i++) { // uncheck all boxes that are checked
			if(eval("document.form.checkBox" + i + ".checked")){
				eval("document.form.checkBox" + i + ".checked = false");
			}
		}
		eval("document.form.checkBox" + checkThis + ".checked = true"); // check the check box that was selected
	}




	/* =============================================================================
	Function:	emailCheck
	Purpose:	Checks whether the form email address submitted is valid or not.
	Parameters:	emailStr - the value of the email field.
	Returns:	Boolean. True if email address is value, false if invalid.
	Calling:	Usually used in the form validate function: 
			<form method="post" action="destination.asp" onsubmit="return validate(this)">
			* Validate function code
			function validate(form) {
				if (!emailCheck(form.email.value)){
  					form.email.focus();
  					return false;
  				}
			}
	Notes:		Taken from www.goonline.com
	============================================================================= */
	//function emailCheck(emailStr) {
	//	re = new RegExp("^([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*\@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*)$");
	//	if (!re.exec(emailStr)) {
	//		alert('Your email address is invalid. It must be in the format user@server.domain');
	//		return false;
	//	}
	//	return true;
	//}
	function emailCheck(emailStr) {
		var checkTLD=1;

		//Not currently using the known domain names as it is too hard to accurately maintain
		//Update the list of known domain names here
		//var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|info|name|pro|museum|coop|aero)$/;

		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("Your email address seems incorrect (please check for an @ symbol, and full stops.)\n\nPlease check and try again.");
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				alert("The username in the Email address contains invalid characters.\n\nPlease check and try again.");
				return false;
			}
		}
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				alert("The domain name in the Email address contains invalid characters.\n\nPlease check and try again.");
				return false;
			}
		}
		if (user.match(userPat)==null) {
			alert("The username in your Email address doesn't seem to be valid.\n\nPlease check and try again.");
			return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					alert("The destination IP address in your Email address is invalid.\n\nPlease check and try again.");
					return false;
				}
			}
			return true;
		}
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				alert("The domain name in your Email address does not seem to be valid.\n\nPlease check and try again.");
				return false;
			}
		}
		//if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		if (checkTLD && domArr[domArr.length-1].length<2) {
			//alert("Your Email address must end in a well-known domain or two letter " + "country code.\n\nPlease check and try again.");
			alert("Your Email address must end in a domain or two letter country code.\n\nPlease check and try again.");
			return false;
		}
		if (len<2) {
			alert("Your Email address is missing either a hostname, domain or country code!\n\nPlease check and try again.");
			return false;
		}
		return true;
	}





	/* =============================================================================
	Function:	requiredFields
	Purpose:	Checks for required fields and focuses the first one.
	Parameters:	* 
	Returns:	.
	Calling:	* 
  				
	var fieldRequired = Array("Policy_Number", "Policy_Name");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Policy number or CWT reference", "Policy Name");
	// dialog message

	function validate(form){

	// dialog message
	var blnTest1, blnTest2;
	
	blnTest1 = requiredFields(form);
	if (!blnTest1) {
		return false
	}
	
	blnTest2 = vali(form) 
	if (!blnTest2) {
		return false
	}
	}


	Notes:		
	============================================================================= */
	function requiredFields(formobj){
	// Enter name of mandatory fields
	
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	var obj_now = "";
	
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
					if (obj_now == "") {
						obj_now = formobj.elements[fieldRequired[i]];
					}			
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
					if (obj_now == "") {
						obj_now = formobj.elements[fieldRequired[i]];
					}
				}
				break;
				case "radio":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
					if (obj_now == "") {
						obj_now = formobj.elements[fieldRequired[i]];
						
					}
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
					if (obj_now == "") {
						obj_now = formobj.elements[fieldRequired[i]];
					}
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
					if (obj_now == "") {
						obj_now = formobj.elements[fieldRequired[i]];
						obj_now[0].focus();
					}
				}
			}
		}
	}

	if (alertMsg.length != l_Msg){
		if (obj_now[0] == undefined){
			obj_now.focus();
			alert(alertMsg);
		}else{
			obj_now[0].focus();
			alert(alertMsg);
		}
	return false;	
	}else{
	return true;
		
	}
}		
	//
	
	



// done hiding -->
