var runOnChange;

/*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();
	}
}

/*Check validity of an integer-only (no other characters allowed) field*/
function checkint(field) {
	var nonIntegerPattern = /[\D]/;
	if (nonIntegerPattern.test(field.value))  {
		alert("Invalid entry! Please enter only a full number, \n with no decimal places");
	    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();
	}
}

/* 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 = ""
		}
	}
}

//calculates a running total of a set of fields named with the same prefix and then differentiated by a letter and an optional suffix 
function calcTotal(fieldNamePrefix, fieldSuffix, noOfFields)
{
	var charPattern = "0abcdefghijklmnopqrstuvwxyz";
	var total = 0;
	var i;
	for (i = 1; i <= noOfFields; i++) {
		//check the value (minus commas) of each specified element is numeric and if it is add it to the total
		if (!isNaN(parseFloat(document.getElementById(fieldNamePrefix + charPattern.charAt(i) + fieldSuffix).value.replace(/\,/g,''))))
			total += parseFloat(document.getElementById(fieldNamePrefix + charPattern.charAt(i) + fieldSuffix).value.replace(/\,/g,''));
	}
	
	return total.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
}

//global variable to track whether the format function is currently running
//this prevents a second instance starting (problem in FF) causing 2 alert boxes
var formatFunctionRunning;

/*Comma format a number field on the fly (doesn't deal with decimals)*/ 
function format(input){
	if (! formatFunctionRunning) { //prevents multiple instances of the function running in FF
		formatFunctionRunning = true;
		var num = input.value.replace(/\,/g,''); //lose the commas
		var validNumberPattern = /(^-?\d*$)/;
		if(validNumberPattern.test(num)){ //if valid, add commas in the correct places
			input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^(\-)?\,/,'$1');
		}
	 	else{ //if invalid, alert then strip bad characters, add commas and refocus
			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');
			setTimeout(function(){input.focus()}, 10);
		 }
		formatFunctionRunning = false;
	}
}
/*Comma format a number field on the fly (copes with decimals)*/ 
function formatwithcommas(input){
   var num = input.value.replace(/\,/g,'');
    if(!isNaN(num)){
      if(num.indexOf('.') > -1){
         num = num.split('.');
         num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
        if(num[1].length > 2){
           alert('You may only enter two decimal places!');
           num[1] = num[1].substring(0,num[1].length-1);
        }  input.value = num[0]+'.'+num[1];       
      } else{ input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') };
    }
    else{ alert('You may enter only numbers in this field!');
          input.value = input.value.substring(0,input.value.length-1);
    }
  } 

//Count the number of characters in the field and display how many are left to type
function textCounter(field, countfield, maxlimit) { 
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
else 
countfield.value = maxlimit - field.value.length;
}

