function ChangePeriod() {
	// Called only on onchange event, ensures Period Input Box contains an integer
    var v = parseInt(document.quote.term.value, 10);
 	if (isNaN(v)) {v = 36;}  // 36 months as default
 	if (v < 0) {v = 36;}  // 36 months as default
	document.quote.term.value = v;
	return 0;
}


function CheckPPPValue(strDefaultPPPScheme) {
	// Check range of PPPScheme and adjust if necessary
	// Sets to a default from defaults.inc if any other bad value in the input box
	// Also now sets No. of Lives insured, dependant on whether insurance is picked.

	strPPPScheme = document.quote.PPPScheme.value

	strPrefix = strPPPScheme.charAt (0);
	strSuffix = strPPPScheme.charAt (strPPPScheme.length - 1);
	strPercentage = strPPPScheme.substring(1,strPPPScheme.length - 1);
	dPercentage = parseFloat(strPercentage);

	var bPPPvalid;
	bPPPValid = false;
	if (document.quote.PPPScheme.value == 'A0T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A500T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A1000T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A1500T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A2000T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A2500T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A3000T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A3500T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A4000T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A4500T') {bPPPValid = true;}
	if (document.quote.PPPScheme.value == 'A5000T') {bPPPValid = true;}
	if (bPPPValid == false) {document.quote.PPPScheme.value = strDefaultPPPScheme ;}
	return 0;
}

function CheckSchemeRefValue(strDefaultSchemeRef) {
	// Check range of txtSchemeRef and adjust if necessary
	// Sets to a default from defaults.inc if any other bad value in the input box
	strSchemeRef = document.quote.flatrate.value;
	strPrefix = strSchemeRef.charAt (0);
	strSuffix = strSchemeRef.charAt (strSchemeRef.length - 1);
	strPercentage = strSchemeRef.substring(1,strSchemeRef.length - 1);
	dPercentage = parseFloat(strPercentage);
	var bSchemeRefValid;
	bSchemeRefValid = true;
	if (strPrefix != 'R' && strPrefix != 'r') {bSchemeRefValid = false;}
	if (strSuffix != 'D' && strSuffix != 'd') {bSchemeRefValid = false;}
	//if (strPercentage != parseInt(strPercentage,10)) {bSchemeRefValid = false;}
	if (bSchemeRefValid == false) {document.quote.flatrate.value = strDefaultSchemeRef;}
	return 0;
}

function OnKeyUp() {

	// Define some hot keys to update percentage values

	// DONE more validation on the string to make sure its reasonable before we split it up
	// in CheckSchemeRefValue() - see below - sets default if garbage entered
	strSchemeRef = document.quote.flatrate.value;
	strPrefix = strSchemeRef.charAt (0);
	strSuffix = strSchemeRef.charAt (strSchemeRef.length - 1);
	strPercentage = strSchemeRef.substring(1,strSchemeRef.length - 1);
	dPercentage = parseFloat(strPercentage);
	
	
	// Page Up
	if (event.keyCode==33) {
	    if (isNaN(dPercentage)) dPercentage = 750;
	    dPercentage = Math.round(dPercentage / 25) * 25;
		dPercentage = (dPercentage + 25);
		strPercentage = dPercentage.toString();
		document.quote.flatrate.value = strPrefix + strPercentage + strSuffix;
		CheckSchemeRefValue();
	}

	// Page Down
	if (event.keyCode==34) {
	    if (isNaN(dPercentage)) dPercentage = 750;
	    dPercentage = Math.round(dPercentage / 25) * 25;
		dPercentage = (dPercentage - 25);
		if (dPercentage < 0) {
			dPercentage = 0;
		}
		strPercentage = dPercentage.toString();
		document.quote.flatrate.value = strPrefix + strPercentage + strSuffix;
		CheckSchemeRefValue();
	}
	
	// More validation on the string to make sure its reasonable before we split it up
	// in CheckPPPValue() - see below - sets default if garbage entered
	strPPPScheme = document.quote.PPPScheme.value
	strPrefix = strPPPScheme.charAt (0);
	strSuffix = strPPPScheme.charAt (strPPPScheme.length - 1);
	strPercentage = strPPPScheme.substring(1,strPPPScheme.length - 1);
	dPercentage = parseFloat(strPercentage);
	
	// F8
	if (event.keyCode==119) {
	    if (isNaN(dPercentage)) dPercentage = 3000;
	    dPercentage = Math.round(dPercentage / 500) * 500;
		dPercentage = (dPercentage + 500);
		if (dPercentage > 5000) {
			dPercentage = 5000;
		}
		strPercentage = dPercentage.toString();
		document.quote.PPPScheme.value = strPrefix + strPercentage + strSuffix;
		CheckPPPValue();
	}

	// F7
	if (event.keyCode==118) {
	    if (isNaN(dPercentage)) dPercentage = 3000;
	    dPercentage = Math.round(dPercentage / 500) * 500;
		dPercentage = (dPercentage - 500);
		if (dPercentage < 0) {
			dPercentage = 0;
		}
		strPercentage = dPercentage.toString();
		document.quote.PPPScheme.value = strPrefix + strPercentage + strSuffix;
		CheckPPPValue();
	}
	
		return 0;
}

function Val( strCurrency ) {
	var result,c;
	result = "";
	
	for ( i = 0; i < strCurrency.length; i++ ) {
		c = strCurrency.charAt(i);
		if (((c >= "0") && (c <= "9")) || (c==".") ) {
			result = result + c
		}
	}
	
	return result;
}
