/*Open a popup help window named helpxxx.htm*/
function popUpWindow(helpnum) 
{
  window.open("help/help"+helpnum+".htm","Window2","status=no,height=470,width=570,resizable=yes,left=100,top=100,scrollbars=yes");
}

/*Check the length of a field (in characters) and alert the user if exceeding a maximum*/
function checklen(field,maxlen) {
	var intLen = field.value.length;
	if (intLen > maxlen) {
	    alert("You have typed more than the maximum permitted \n number of characters in the field. \n Maximum allowed (inc. spaces):\t " + maxlen + "\n Current total (inc. spaces): \t" + intLen + "\n Please delete part of the entry.");
	    setTimeout(function(){field.focus()}, 10);
	}
}

/*Check validity (though not existence) of an email address*/
function emailCheck(emailStr) {
	var emailPattern = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i;
	if (! emailPattern.test(emailStr))  {
		alert("Your email address appears incorrect.  Please try again (check the '@' and '.'s in the email address)");
		setTimeout(function(){field.focus()}, 10);
	    field.select();
		return false;
	}
	else {
		return true;
	}
}

/*Check validity of an integer-only (no other characters allowed) field*/
function checknum(field) {
	var nonIntegerPattern = /[\D]/;
	if (nonIntegerPattern.test(field.value))  {
		alert("Invalid entry! Please enter only a full number of pounds, \n with no pound sign, no spaces, no commas and no pence");
	    setTimeout(function(){field.focus()}, 10);
	    field.select();
	}
}

/*Alert user if a NaN is inputted in a field*/
function checkfloat(field) {
	if (isNaN(field.value)) {
	    alert("Invalid entry!\n You may only enter a number here.");
	    setTimeout(function(){field.focus()}, 10);
	    field.select();
	}
}

/*Comma format a number field on the fly (doesn't deal with decimals)*/ 
function format(input){
	var num = input.value.replace(/\,/g,'');
	var validNumberPattern = /(^-?\d*$)/;
	if(validNumberPattern.test(num)){
		input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
	}
 	else{
		alert("Invalid entry! Please enter only a full number of pounds, \n with no pound sign, no spaces, and no pence");
	 	var temp = input.value;
	 	var tempBadCharsRemoved
	 	tempBadCharsRemoved = temp.replace(/[^-\d]*/g,'');
	 	input.value = tempBadCharsRemoved.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
	}
}

/* Produce a comma-formatted version of a number field in an (adjacent) named target field*/
function format2(input){
	var num = input.value.replace(/\,/g,'');
	var targetField = document.getElementById(input.getAttribute("id") + "2");
	var validNumberPattern = /(^-?\d*$)/;
	if(validNumberPattern.test(num)){
		targetField.value = "£" + num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
		if (targetField.value.length == 1) {
			targetField.value = ""
		}
	}
	else{

		var temp = input.value;
		input.value = temp.replace(/[^-\d]*/g,'');
		targetField.value = "£" + input.value.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
		if (targetField.value.length == 1) {
			targetField.value = ""
		}
	}
}

