/*  These scripts are Copyright, Rich Harding
		http://www.csscomputing.co.uk, 2002-5
		You may re-use them free of charge
		as long as you retain this notice   */
// Find out what browser functionality is available:

var NN4 = document.layers? true : false; //Netscape Navigator 4.x.
var IE4 = document.all? true : false; // IE version 4 and above.
var XX7 = document.getElementById? true : false; // Up to date browsers.

function noGo() {
// empty
}

var tNow;

function showDIV(sNo,bTimed) {
	if (sNo < lngNC1) { // >
		if (XX7) {
			if (iDIV != -1) {
				document.getElementById('dd' + iDIV).style.visibility = 'hidden';
			}
			if (sNo != -1) {
				document.getElementById('dd' + sNo).style.visibility = 'visible';
			}
		}
		else if (IE4) {
			if (iDIV != -1) {
		   	document.all['dd' + iDIV].style.visibility = 'hidden';
			}
			if (sNo != -1) {
	   		document.all['dd' + sNo].style.visibility = 'visible';
			}
		}
		else if (NN4) {
			if (iDIV != -1) {
		   	document.layers['dd' + iDIV].visibility = 'hide';
			}
			if (sNo != -1) {
	   		document.layers['dd' + sNo].visibility = 'show';
			}
		}
		iDIV = sNo
		if (bTimed) {
			tNow = new Date();
			setTimeout("showNothing();",5000);
		}
	}
}

function showNothing() {
	var tNew = new Date();
	if ((tNew - tNow) > 5000) {
		showDIV(-1);
	}
}

function createMail(who,what,where,why) {

	var strWhere = where;
	if (strWhere == '') { strWhere = MAIL_DOMAIN; }

	var strWhat = what;
	if (strWhat == '') { strWhat = 'E-Mail ' + SITE_FROM + ' ' + MAIL_SITENAME + ' Website'; }

	var strWhy = why;

	var strTo = '';
	var strCc = '';

	var strWho = who;
	if (strWho == '') {
		strTo = MAIL_WEBMASTER;
		strTo = strTo + '@' + MAIL_DOMAIN;
		strWhat = 'Incorrect mail link on website';
		strWhy = 'Please tell the webmaster where you found this link as it is incorrectly formatted.';
	}
	else {
		var lngComma = strWho.indexOf(',')
		if (lngComma > 0) {
			var strAddy = strWho.split(',');
			var strDom = strWhere.split(',');
			for (x=0;x<strAddy.length;x++) {
				if (strDom[x] == null) { strDom[x] = strDomain; }
				if (x == 0) {
					strTo = strAddy[x] + '@' + strDom[x];
				}
				else {
					if (x == 1) {
						strCc = strAddy[x] + '@' + strDom[x];
					}
					else {
						strCc = strCc + ',' + strAddy[x] + '@' + strDom[x];
					}
				}
			}
		}
		else {
			strTo = strWho + '@' + strWhere;
		}
	}

	var to = strTo;
	var cc = strCc;
	var subject = strWhat;
	var body = strWhy;
	var doc = "mailto:" + to + '?';
	if (cc != '') {
		doc += "cc=" + cc + '&';
	}
	doc += "subject=" + escape(subject);
	doc += "&body=" + escape(body);
	window.location = doc;
}

/* Check types for checkForm
0:  Text Boxes
1:  Drop-Downs
2:  Radio Buttons
3:  E-Mail Addresses
4:  Phone
5:  Number
6:  E-Mail Addresses that can be omitted
7:  Number that can be omitted
8:  Password
9:  Phone that can be omitted
10: Screen Name - min 6 chars
11: Date
12: Date that can be omitted
13: Decimal
14: Single Check Box
15: Password that can be omitted
16: Time
17: Time that can be omitted
18: Bank Sort Code
19: Date (4-digit year)
20: Three-DD Date - remember it's mm/dd/yyyy
21: Checkboxes which must be ticked
22: Post Code
23: Number - 4 digits or can be omitted
24: Phone - minimum 9 digit
25: Drop-Downs - Vehicle Waterfall 'Choose your'
26: Registration number
27: Registration number which can be omitted
28: Compare 2 Dates
29: Credit/Debit card number (16 digits, with possible spaces)
30: Number - not zero
*/

function checkForm(form){
	var dF = form;
	sAlert = '';
	for(i=0;i<arrCheck.length;i++){
		if(sAlert.length != 0){
			break;
		}//end for(i=0;i<arrCheck.length;i++)
		switch(arrType[i]){
			case 0: // Text Boxes
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			break;
			case 1: // Drop-Downs
//			if (dF.elements[arrCheck[i]].length != 0) {
//				if (dF.elements[arrCheck[i]][dF.elements[arrCheck[i]].selectedIndex].value == 0) {
//					iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
//				}
//			}
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			break;
			case 2: // Radio Buttons
// Finds the name of the radio button and looks through the form for elements with the same name
// Then checks that atleast one of those elements is Checked. Will error if none are.
			if (dF.elements[arrCheck[i]].checked == false) {
				rbname = dF.elements[arrCheck[i]].name;
				rbc = false;
				for(j=0; j<dF.elements.length; j++){
					if((dF.elements[j].name == rbname)&&(dF.elements[j].checked == true)){
						rbc = true;
					}
				}
				if(!rbc){
					iAlert = arrCheck[i]; sAlert = arrAlert[i] + '.';
				}
			}
// Will only check 2 radio buttons which are next to each other
//			if (dF.elements[arrCheck[i]].checked == false && dF.elements[arrCheck[i] + 1].checked == false) {
//				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
//			}
			break;
			case 3: // E-Mail Addresses
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validEmail(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 4: // Phone
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validTel(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 5: // Number
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (isNaN(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = arrAlert[i] + SITE_NUMBER;
			}
			break;
			case 6: // E-Mail Addresses that can be omitted
			if (dF.elements[arrCheck[i]].value != '') {
				if (!validEmail(dF.elements[arrCheck[i]].value)) {
					iAlert = arrCheck[i]; sAlert = 'dummy';
				}
			}
			break;
			case 7: // Number that can be omitted
			if (isNaN(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = arrAlert[i] + SITE_NUMBER;
			}
			break;
			case 8: // Password
			if (dF.elements[arrCheck[i]].value != dF.elements[arrCheck[i]+1].value) {
				iAlert = arrCheck[i]; sAlert = SITE_PASSWORD1;
			}
			if (!validPass(dF.elements[arrCheck[i]].value,dF.elements[arrCheck[i]+1].value)) {
				iAlert = arrCheck[i]; sAlert = SITE_PASSWORD2;
			}
			break;
			case 9: // Phone that can be omitted
			if (dF.elements[arrCheck[i]].value != '') {
				if (!validTel(dF.elements[arrCheck[i]].value)) {
					iAlert = arrCheck[i]; sAlert = 'dummy';
				}
			}
			break;
			case 10: // Screen Name - min 6 chars
			if (!validScreen(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = SITE_SCREEN;
			}
			break;
			case 11: // Date
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validDate(dF.elements[arrCheck[i]].value,2)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 12: // Date that can be omitted
			if (dF.elements[arrCheck[i]].value != '') {
				if (!validDate(dF.elements[arrCheck[i]].value,2)) {
					iAlert = arrCheck[i]; sAlert = 'dummy';
				}
			}
			break;
			case 13: // Decimal
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validDecimal(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = SITE_INVALID + arrAlert[i] + '.';
			}
			break;
			case 14: // Single Check Box
			if (dF.elements[arrCheck[i]].checked == false) {
				iAlert = arrCheck[i]; sAlert = arrAlert[i] + '.';
			}
			break;
			case 15: // Password that can be omitted
			if (dF.elements[arrCheck[i]].value != '' && dF.elements[arrCheck[i]+1].value != '') {
				if (dF.elements[arrCheck[i]].value != dF.elements[arrCheck[i]+1].value) {
					iAlert = arrCheck[i]; sAlert = SITE_PASSWORD1;
				}
				if (!validPass(dF.elements[arrCheck[i]].value,dF.elements[arrCheck[i+1]].value)) {
					iAlert = arrCheck[i]; sAlert = SITE_PASSWORD2;
				}
			}
			break;
			case 16: // Time
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validTime(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 17: // Time that can be omitted
			if (dF.elements[arrCheck[i]].value != '') {
				if (!validTime(dF.elements[arrCheck[i]].value)) {
					iAlert = arrCheck[i]; sAlert = 'dummy';
				}
			}
			break;
			case 18: // Bank Sort Code
			if (!validSortCode(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 19: // Date (4-digit year)
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validDate(dF.elements[arrCheck[i]].value,4)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 20: // Three-DD Date - remember it's mm/dd/yyyy
			var strDay = dF.elements[arrCheck[i]].value;
			var strMonth = dF.elements[arrCheck[i] + 1].value;
			var strYear = dF.elements[arrCheck[i] + 2].value;
			if (!validDateMac(dF.elements[arrCheck[i]][dF.elements[arrCheck[i]].selectedIndex].value,dF.elements[arrCheck[i] + 1][dF.elements[arrCheck[i] + 1].selectedIndex].value,dF.elements[arrCheck[i] + 2][dF.elements[arrCheck[i] + 2].selectedIndex].value)) {
				iAlert = arrCheck[i]; sAlert = 'Date: ' + strDay + ' ' + strMonth + ' ' + strYear + ' This is not a valid Date!';
			}
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = 'Please Choose a Date.';
			}
			if (dF.elements[arrCheck[i] + 1].value == '') {
				iAlert = arrCheck[i]; sAlert = 'Please Choose a Date.';
			}
			if (dF.elements[arrCheck[i] + 2].value == '') {
				iAlert = arrCheck[i]; sAlert = 'Please Choose a Date.';
			}
			break;
			case 21: // Checkboxes which must be ticked
			if (dF.elements[arrCheck[i]].checked == false) {
				iAlert = arrCheck[i]; sAlert = arrAlert[i] + '.';
			}
			break;
			case 22: // Post Code
			if (!validPostcode(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 23: // Number - 4 digits or can be omitted
		    $lRequired = 0;
		    $lNumLength = 4;
			  if (!validNumLength(dF.elements[arrCheck[i]].value, $lRequired, $lNumLength)) {
				  iAlert = arrCheck[i]; sAlert = 'dummy';
			  }
			break;
			case 24: // Phone - minimum 9 digit
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (!validTel(dF.elements[arrCheck[i]].value, 9)) {
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}
			break;
			case 25: // Drop-Downs - Vehicle Waterfall 'Choose your'
			if (dF.elements[arrCheck[i]].value == 'nothingSelected') {
				iAlert = arrCheck[i]; sAlert = 'Please Choose your Vehicle\'s ' + arrAlert[i] + '.';
			}
			break;
			case 26: // Registration number
			var regcheck = checkregistrationplate(dF.elements[arrCheck[i]].value);
			if(regcheck==false){
				iAlert = arrCheck[i]; sAlert = 'dummy';
			}else{
				dF.elements[arrCheck[i]].value = regcheck;
			}
			break;
			case 27: // Registration number which can be omitted
			if(dF.elements[arrCheck[i]].value!=''){
				var regcheck = checkregistrationplate(dF.elements[arrCheck[i]].value);
				if(regcheck==false){
					iAlert = arrCheck[i]; sAlert = 'dummy';
				}else{
					dF.elements[arrCheck[i]].value = regcheck;
				}
			}
			break;
			case 28: // Compare 2 Dates
			if(!compareDates(arrCheck[i],dF)){
				iAlert = 'no_focus'; sAlert = arrAlert[i] + '.';
			}			
			break;
			case 29: //16 digits, with spaces (as per credit cards)
			  if(!validCC(dF.elements[arrCheck[i]].value)) {
				  iAlert = arrCheck[i]; sAlert = 'Please recheck your credit/debit card number';
			  }
			break;
			case 30: // Number
			if (dF.elements[arrCheck[i]].value == '') {
				iAlert = arrCheck[i]; sAlert = SITE_PLEASE_TELL + arrAlert[i] + '.';
			}
			if (isNaN(dF.elements[arrCheck[i]].value)) {
				iAlert = arrCheck[i]; sAlert = arrAlert[i] + SITE_NUMBER;
			}
			if (dF.elements[arrCheck[i]].value == '0') {
				iAlert = arrCheck[i]; sAlert = 'Value must be greater than zero';
			}
			
		}// end switch
	}// end for(i=0;i<arrCheck.length;i++)

	if (sAlert.length == 0){
		//dF.submit();
		return true;
	}else{
		if (sAlert != 'dummy') {
			alert(sAlert);
		}
		if(iAlert!='no_focus'){//quick get out clause for case 28
			dF.elements[iAlert].focus();
		}
		return false;
	}
}

function validCC(strVal) {
	var numNums = 0;
	validChars = " 0123456789";
	if(strVal.length == 0) {
		return false;
	}
	for (k=0; k<strVal.length; k++) {
		testChar = strVal.charAt(k);
		if (validChars.indexOf(testChar,0) == -1) {
			return false;
		} else {
			if(testChar != " " && testChar != "-") {
				numNums++;
			}
		}
	}
	if(numNums != 16) {
		return false;
	}
	return true;
}

function validDecimal(fldVal) {
	validChars = "0123456789.,-"
	for (k=1; k<fldVal.length; k++) {
		testChar = fldVal.charAt(k)
		if (validChars.indexOf(testChar,0) == -1) {
			return false;
		}
	}
	return true;
}

function validPostcode(fldVal) {
	vC1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; vC2 = '0123456789'; vC3 = ' ';
	lng1 = 0; lng2 = 0;
	for (j=0;j<fldVal.length;j++) {
		if (vC1.indexOf(fldVal.charAt(j)) != -1) {
			lng1++;
		}
		else if (vC2.indexOf(fldVal.charAt(j)) != -1) {
			lng2++;
		}
		else if (vC3.indexOf(fldVal.charAt(j)) == -1) {
			return false;
			break;
		}
	}
	if (lng1 < 3 || lng2 < 2) {
		alert(SITE_POSTCODE);
		return false;
	}
	else {
		return true;
	}
}

function validScreen(fldVal) {
	if (fldVal.length < 6) { // >
		return false;
	}
	return true;
}

function validSortCode(fldVal) {
	if (fldVal.length < 6) { // >
		alert(SITE_SORT_CODE);
		return false;
	}
	if (isNaN(fldVal)) { // >
		alert(SITE_SORT_CODE);
		return false;
	}
	return true;
}

function validPass(fldVal) {
	vC1 = 'abcdefghijklmnopqrstuvwxyz'; vC2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; vC3 = '0123456789'
	lng1 = 0; lng2 = 0; lng3 = 0;
	for (j=0;j<fldVal.length;j++) {
		if (vC1.indexOf(fldVal.charAt(j)) != -1) {
			lng1++
		}
		else if (vC2.indexOf(fldVal.charAt(j)) != -1) {
			lng2++
		}
		else if (vC3.indexOf(fldVal.charAt(j)) != -1) {
			lng3++
		}
		else {
			return false;
			break;
		}
	}
	if (lng1 == 0 || lng2 == 0 || lng3 == 0) {
		return false;
	}
	else {
		return true;
	}
}

function validTel(strVal) {
  lNum = 10;
	if (arguments[1]) {
	  lNum = arguments[1];
	}
	if (strVal.charAt(0) != '0' && strVal.charAt(0) != '+') {
		alert(SITE_TELNO1)
		return false;
	}
	validChars = " 0123456789-/"
	for (k=1; k<strVal.length; k++) {
		testChar = strVal.charAt(k)
		if (validChars.indexOf(testChar,0) == -1) {
			alert(SITE_TELNO2 + testChar)
			return false;
		}
	}
	if (strVal.length < lNum) {
			alert(SITE_TELNO3 + lNum + SITE_TELNO4)
		return false;
	}
	return true;
}

function validNumLength(strVal, lRequired, lNumLength) {
	if (lRequired == 0 && strVal.length == 0) {
		return true;
	}
	if (strVal.length < lNumLength) {
	  alert(arrAlert[i] + SITE_LENGTH1 + lNumLength + SITE_LENGTH3)
		return false;
	}
	validChars = " 0123456789"
	for (k=1; k<strVal.length; k++) {
		testChar = strVal.charAt(k)
		if (validChars.indexOf(testChar,0) == -1) {
	    alert(arrAlert[i] + SITE_NUMBER)
			return false;
		}
	}
	return true;
}

function validEmail(strVal) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(strVal))
		return true;
	
	alert("You must enter a valid email address");
	
	return false;
	
	/* Original EMail validation code by Rich
	strInv = ' /:,;'
	for (l=0;l<strInv.length;l++) { // >
		strBad = strInv.charAt(l)
		if (strVal.indexOf(strBad,0) > -1) { // <
			alert(SITE_EMAIL1 + strBad + '\'')
   		return false;
		}
	}
	atPos = strVal.indexOf('@');
	if (atPos == -1) {
		alert(SITE_EMAIL2)
 		return false;
	}
	if (strVal.indexOf('@',atPos+1) > -1) { // <
		alert(SITE_EMAIL3)
		return false;
	}
	dotPos = strVal.indexOf('.',atPos)
	if (dotPos == -1) {
		alert(SITE_EMAIL4)
		return false;
	}
	if (dotPos+3 > strVal.length) { // <
		alert(SITE_EMAIL5)
		return false;
	}
	return true;
	*/
}

function validDate(dteVal,lY) {

	if (lY == 2) {
		var datePat = /^(\d{2})(\/|-)(\d{2})(\/|-)(\d{2})$/;
	}
	else if (lY == 4) {
		var datePat = /^(\d{2})(\/|-)(\d{2})(\/|-)(\d{4})$/;
	}
	else {
		alert("Date Checker incorrectly configured.");
		return false;
	}

	var matchArray = dteVal.match(datePat); // is the format ok?

	if (matchArray == null) {
		if (lY == 2) {
			alert("Please enter date as either dd/mm/yy or dd-mm-yy");
		}
		else if (lY == 4) {
			alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy");
		}
		return false;
	}
	return validDateNumerical(matchArray[5], matchArray[3], matchArray[1]);
}

function validDateNumerical(year, month, day) {

	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!");
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

// Returns true if the supplied date is in the future. False otherwise.
function futureDate(year, month, day) {

	var today  = new Date();
	var date = new Date();
	date.setFullYear(year,month-1,day);	// Month is between 0 and 11.

	return date > today;
}

function validDateMac(sD,sM,sY) {
	if (sD.charAt(0) == "0") {
		sD = sD.charAt(1)
	}
	if (sM.charAt(0) == "0") {
		sM = sM.charAt(1)
	}
	lD = parseInt(sD);
	lM = parseInt(sM);
	lY = parseInt(sY);
	if (lD > 31 || lM > 12 || lY > 2030) {
		return false;
	}
	switch (lM) {
	case 4:
	case 6:
	case 9:
	case 11:
		if (lD == 31) {
			return false;
		}
		break;
	case 2:
		var isLeap = (lY % 4 == 0 && (lY % 100 != 0 || lY % 400 == 0));
		if (lD > 29 || (lD == 29 && !isLeap)) {
			return false;
		}
		break;
	}
	return true; // date is valid
}

function validTime(sTime) {
	var msg = ''
	var ok = true
	var h1 = sTime.slice(0, 2)
	var m1 = sTime.slice(3, 5)
	var sa1 = sTime.slice(2, 3)
	if (isNaN(h1) || isNaN(m1)) {
		ok = false;
	}
	else {
		if (h1 < 0 || h1 > 23) {
			ok = false;
		}
		if (m1 < 0 || m1 > 59) {
			ok = false;
		}
	}
	if (sa1 != ':') {
		ok = false;
	}
	if (!ok) {
		alert(SITE_TIME)
 		return false;
	}
	return true;
}

function validateNos(fldVal) {
	if (fldVal != '') {
   	var valid = '0123456789'
    var temp;
		for (j=0; j<fldVal.length; j++) { // >
    	temp = '' + fldVal.substring(j, j+1);
      if (valid.indexOf(temp) == -1) {
				if (j == 0 && temp != '-') {
	      	return false;
				}
				else {
	      	return false;
				}
			}
		}
		return true;
	}
	else {
   	return true;
	}
}

function validChars(fldVal) {
	vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
	for (j=0;j<fldVal.length;j++) {
		if (vChars.indexOf(fldVal.charAt(j)) == -1) {
			ivC = fldVal.charAt(j)
			if (ivC == ' ') {
				ivC = 'Spaces'
			}
			return false
			break;
		}
	}
	return true
}

function validCharsName(fldVal) {
	vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -'
	for (j=0;j<fldVal.length;j++) {
		if (vChars.indexOf(fldVal.charAt(j)) == -1) {
			return false
			break;
		}
	}
	return true
}

function validFileChars(fldVal) {
	vChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._'
	for (k=0;k<fldVal.length;k++) {
		if (vChars.indexOf(fldVal.charAt(k)) == -1) {
			ivC = fldVal.charAt(k)
			if (ivC == ' ') {
				ivC = 'Spaces'
			}
			return false
			break;
		}
	}
	return true
}

function validCardNo(fldVal) {

    var pattern = /\d{13,16}/;

    if(fldVal.match(pattern)) {
        return true;
    }
    else {
        alert('Card number must be 13 to 16 digits, with no spaces or other characters present.');
        return false;
    }
}

function validCardDate(fldVal) {
    if(fldVal.match(/\d{4}/)) {
        return true;
    }
    else {
        alert('Expiry date must number must be 4 digits - MMYY');
        return false;
    }
}

function infoWindow(htmlFile,w,h,t) {
	var r = Math.random();
	var fN = htmlFile;
	if (fN.indexOf('?') == -1) {
		fN += '?r=' + r;
	}
	else {
		fN += '&r=' + r;
	}
	if (w==null || w=="") {
		w = 500;
	}
	if (h==null || h=="") {
		h = 500;
	}

	if (t==null || t=="" || t==0) {
		t = 'no';
	}
	else {
		t = 'yes';
	}
	var xPos = screen.width - (w + 10);
	var ranWin = 1;
	var myWin = window.name;
	if (myWin.indexOf('infWin') != -1) {
		myNum = myWin.substr(6);
		while (ranWin == myNum) {
			ranWin = Math.round(Math.random()*1000);
		}
	}
	infWindow = window.open(fN, 'infWin' + ranWin, 'toolbar=' + t + ',location=' + t + ',resizable=yes,status=yes,scrollbars=yes,width='+w+',height='+h+',left='+xPos+',top=0')
	if (infWindow != null) {
		infWindow.resizeTo(w,h);
		infWindow.moveTo(xPos,0);
		setTimeout('reFocus(infWindow)', '1000')
	}
	else {
		if (IE4) {
			sAlert = POPUP_REASON + ' Your Internet Explorer browser or other software has blocked a necessary pop-up window from this site. Please enable pop-ups for this site.';
		}
		else if (XX7 || NN4) {
			sAlert = POPUP_REASON + ' Your browser or other software has blocked a necessary pop-up window from this site. Please enable pop-ups for this site.';
		}
		alert(sAlert);
	}
}

function printWindow(htmlFile,w,h) {
	var r = Math.random();
	var fN = htmlFile;
	if (fN.indexOf('?') == -1) {
		fN += '?r=' + r;
	}
	else {
		fN += '&r=' + r;
	}
	if (w==null || w=="") {
		w = 500;
	}
	if (h==null || h=="") {
		h = 500;
	}
	var xPos = screen.width - (w + 10);
	prtWindow = window.open(fN, 'printWin', 'toolbar=no,location=no,resizable=yes,menubar=yes,status=no,scrollbars=yes,width=657,height=800,left='+xPos+',top=0');
	if (prtWindow != null) {
		prtWindow.resizeTo(w,h);
		prtWindow.moveTo(xPos,0);
		setTimeout('reFocus(prtWindow)', '1000')
	}
	else {
		if (IE4) {
			sAlert = POPUP_REASON + ' Your Internet Explorer browser or other software has blocked a necessary pop-up window from this site. Please enable pop-ups for this site.';
		}
		else if (XX7 || NN4) {
			sAlert = POPUP_REASON + ' Your browser or other software has blocked a necessary pop-up window from this site. Please enable pop-ups for this site.';
		}
		alert(sAlert);
	}
}

function reFocus(winObj) {
	winObj.focus();
}

function loadMainFromPopup(htmlFile, reload) {
	if (reload==null) {
		window.opener.location = htmlFile;
	}
	else {
		window.opener.reload();
	}
}

function confDel(sMsg,lID1,lID2,lStep) {
	if (confirm('Are you sure you want to ' + sMsg + '?')) {
		self.location ='?step=' + lStep + '&id=' + lID1 + '&id2=' + lID2;
	}
}

function showInfo(sText) {
	alert(sText);
}

function jumpSelect(fE,lID,lStep) {
	var lThis = fE[fE.selectedIndex].value;
	if (lThis != -1) {
		self.location ='?step=' + lStep + '&id=' + lID + '&ddid=' + lThis;
	}
}

function getHeight() {
	if (XX7 || IE4) { iHeight = document.body.clientHeight; }
	else if (NN4) { iHeight = window.innerHeight; }
	return Math.max(iHeight,minH);
}

function getWidth() {
	if (XX7 || IE4) { iWidth = document.body.clientWidth; }
	else if (NN4) { iWidth = window.innerWidth; }
	return Math.max(iWidth, minW);
}

function getDIVLeft(divID) {
	if (XX7) { return document.getElementById(divID).style.left; }
	else if (IE4) { return document.all[divID].style.left; }
	else if (NN4) { return document.layers[divID].left; }
}

function getDIVTop(divID) {
	if (XX7) { return document.getElementById(divID).style.top; }
	else if (IE4) { return document.all[divID].style.top; }
	else if (NN4) { return document.layers[divID].top; }
}

function getDIVHeight(divID) {
	if (XX7) { return document.getElementById(divID).offsetHeight; }
	else if (IE4) { return document.all[divID].style.height; }
	else if (NN4) { return document.layers[divID].height; }
}

function getDIVWidth(divID) {
	if (XX7) { return document.getElementById(divID).offsetWidth; }
	else if (IE4) { return document.all[divID].style.width; }
	else if (NN4) { return document.layers[divID].width; }
}

function positionDIV(divID,divLeft,divTop,divWidth,divHeight) {
	if (XX7) {
		if (divLeft != -1) { document.getElementById(divID).style.left = divLeft; }
		if (divTop != -1) { document.getElementById(divID).style.top = divTop; }
		if (divWidth != -1) { document.getElementById(divID).style.width = divWidth; }
		if (divHeight != -1) { document.getElementById(divID).style.height = divHeight; }
	}
	else if (IE4) {
		if (divLeft != -1) { document.all[divID].style.left = divLeft; }
		if (divTop != -1) { document.all[divID].style.top = divTop; }
		if (divWidth != -1) { document.all[divID].style.width = divWidth; }
		if (divHeight != -1) { document.all[divID].style.height = divHeight; }
	}
	else if (NN4) {
		if (divLeft != -1) { document.layers[divID].left = divLeft; }
		if (divTop != -1) { document.layers[divID].top = divTop; }
		if (divWidth != -1) { document.layers[divID].width = divWidth; }
		if (divHeight != -1) { document.layers[divID].height = divHeight; }
	}
}

function clearDD(eDD) {
	eDD.options.length = 0;
}

function popDD(eDD,aNames,aVals) {
	for(i=0;i<aNames.length;i++) {
		newOpt = new Option(aNames[i], aVals[i], false, false);
		eDD.options[eDD.length] = newOpt;
	}
}

function setDDbyIdx(eDD,iSet) {
	if (iSet != -1) {
		eDD.selectedIndex = iSet;
	}
}

function setDDbyTxt(eDD,sSet,bIpt) {
	if (sSet != -1) {
		var sIpt;
		if (bIpt == true) {
			sSet = nReplace(sSet.toUpperCase(),' ','');
		}
		for (i=0;i<eDD.length;i++) {
			sIpt = eDD[i].text;
			if (bIpt == true) {
				sIpt = nReplace(sIpt.toUpperCase(),' ','');
			}
			if (sIpt == sSet) {
				eDD.selectedIndex = i;
				break;
			}
		}
	}
}

function setDDbyPartTxt(eDD,sSet,bIpt) {
	var uI = -1;
	var uC = 0;
	if (sSet != -1) {
		var sIpt;
		if (bIpt == true) {
			sSet = nReplace(sSet.toUpperCase(),' ','');
		}
		for (i=0;i<eDD.length;i++) {
			sIpt = eDD[i].text;
			if (bIpt == true) {
				sIpt = nReplace(sIpt.toUpperCase(),' ','');
			}
			if (sIpt == sSet) {
				uI = i;
				break;
			}
			else {
				for (c=uC;c<sIpt.length;c++) {
					if (sSet.substring(0,c) == sIpt.substring(0,c)) {
						uI = i;
					}
				}
			}
		}
	}
	if (uI.length != -1) {
		eDD.selectedIndex = uI;
	}
}

function setDDbyVal(eDD,iSet) {
	if (iSet != -1) {
		eDD.value = iSet;
	}
}

function nReplace(sVal,need,hay){
	var sHold = sVal;
	var j = sHold.indexOf(need);
	while (j > -1) {
		sHold = sHold.replace(need, hay);
		j = sHold.indexOf(need, j + hay.length + 1);
	}
	return sHold;
}

/* scaleText written by Rupert Jabelman (rupert@jabelman.org) for eGroup Ltd */

function scaleText(minWidth, currentWidth) {

	// This doesn't work under the KHTML engine (Safari, Konqueror), so don't do it.
	var KHTML = navigator.userAgent.indexOf('KHTML') > 0;

	if(!KHTML) {

		// Don't scale if we're below min width... Otherwise everything gets crazy small...
		if(currentWidth < minWidth) {
			currentWidth = minWidth;
		}

        /*
        The number in here is an arbitrary scaling factor to stop things growing too fast.
        Otherwise the text would be twice the size at a screen width of 2*minwidth.
        */
		var scale = (currentWidth/minWidth) * 0.7;

		var quote = document.getElementById('quote');
		
		if(!quote) {
		  var quote = document.getElementById('contentquote');
		}

		if(document.defaultView) { // W3C DOM
			var sSize = document.defaultView.getComputedStyle(quote,null).fontSize;
		}
		else { // IE
			var sSize = quote.currentStyle.fontSize;
		}

		if(sSize.indexOf('%') > 0) {
			var unitLength = 1;
		}
		else {
			// For px, em, pt, etc...
			var unitLength = 2;
		}

		var lSize = sSize.substring(0, sSize.length - unitLength);
		var sUnit = sSize.substring(sSize.length - unitLength, sSize.length);
		var newSize = lSize * scale;

		// Half a pixel won't work, but em's are ok.
		if(sUnit == 'px') newSize = Math.round(newSize);


		var cells = quote.getElementsByTagName('td');
		for(var i=0; i<cells.length; i++) {
			cells[i].style.fontSize = newSize+sUnit;
		}

		var pagenav = document.getElementById('pagenav');
		if(pagenav) pagenav.style.fontSize = newSize+sUnit;
		
		var contentquote = document.getElementById('contentquoteinner');
		if(contentquote) contentquote.style.fontSize = newSize+sUnit;
		// alert('resizing');
	}
}

function confirmdelete(what,number)
{
	var answer = confirm('Are you sure you want to delete ' + what + ' number ' + number + '?');
	if (answer){
		document.remtask.additional.value = what + '-' + number;
		document.remtask.submit();
//		return false;
	}else{
//		return false;
	}
}
// The back button
function goBack(where,drivernum)
{
	var dF = document.backForm;
	dF.whereto.value = where;
	dF.additional.value = drivernum;
	if(where!=''){
		dF.submit();
		return false;
	}else{
		return false;
	}
}

//Function to save and close quotes.
function saveAndClose()
{
	var dF = document.frmQuote;
	dF.whereto.value = 'saveandclose';
	dF.submit();
	return false;
}
//Function to save and close quotes, once values are quoted.
function saveAndCloseValues()
{
	var dF = document.frmQuote;
	dF.whereto.value = 'saveandclosevalues';
	dF.submit();
	return false;
}
// The navigation bar
function navigate(where)
{
	var dF = document.topnav;
	dF.whereto.value = where;
	if(where!=''){
		dF.submit();
		return false;
	}else{
		return false;
	}
}
// The Recalculate Quotes Button
function recalculateQuotes()
{
	var sF = document.frmQuote;
	var dF = document.recalculate;
	dF.volxs.value = sF.volxs.value;
	dF.protectncb.value = sF.tempprotectncb.value;
	dF.tmpLegal.value = sF.legal.checked;
	dF.tmpKeycare.value = sF.keycare.checked;
	if(dF.volxs.value!=''){
		dF.submit();
		return false;
	}else{
		return false;
	}
}
// Registration Check
function checkregistrationplate(fldVal){
	//Set the valid flag to false by default
	var validRegNo = false;
	//Get the value entered by the user
	var regNo = fldVal.toUpperCase();
	// itteration counter
	var regcount = 0;
	var patternMatches = new Array();

	//This matches the current (AB 05 DEF) format
	patternMatches[0] = /\b[A-Z]{2}[\d\d]{2}[A-Z]{3}\b/;
	//This matches the previous (A 123 BCD) format
	patternMatches[1] = /\b[A-HJ-NP-Y]\d{1,3}[A-Z]{3}\b/;
	//This matches the older (ABC 123 D) format
	patternMatches[2] = /\b[A-Z]{3}\d{1,3}[A-HJ-NP-Y]\b/;
	//Now the even older (ABC 123) format
	patternMatches[3] = /\b(?:[A-Z]{1,2}\d{1,4}|[A-Z]{3}\d{1,3})\b/;
	//And the x2 even older (123 ABC) format
	patternMatches[4] = /\b(?:\d{1,4}[A-Z]{1,2}|\d{1,3}[A-Z]{3})\b/;

	//Now check it against our regular expressions
	//Setup a for loop to go through all 5 legitimate styles
	//of number plate
	for(regcount=0;regcount<5;regcount++){
		//Check to see if the reg number entered is valid
		//If it is set the validRegNo flag to true
		//If it isn't we leave it as false
		if(regNo.match(patternMatches[regcount])){
			//We have a match, set the valid flag to true
			validRegNo = true;
			break;
		}
	}
	//That's the checks done.
	if(!validRegNo){
		alert(SITE_REGISTRATION);
		return validRegNo;
	}else{
		// send the upper case version back
		return regNo;
	}
}
// Recieves Year, Month & Day Values and returns one whole number value
function PDCount(Y, M, D) {
    return (Y * 20 + +M) * 50 + +D;
}
// datestring should contain someting like d-c-5
// the first char is what to compare;
//      d = 17yr gap between the two dates
//      p = that the second date isn't older than the first
// the second and third chars are the first and second dates to be compared;
//      c = current date
//      number = the form element containing the day value (so +1 is month and +2 is year)
function compareDates(datestring,dF){
	var fYear, fMonth, fDay, tYear, tMonth, tDay, CompareDates, rvalid=false;
	with (new Date()) {// Setup the current date
		var cYear = getFullYear(), cMonth = getMonth() + 1, cDay = getDate();
	}
	var SplitDates = datestring.split("-");
	// Setup the from and to dates
	if(SplitDates[1]=='c'){
		fYear = cYear, fMonth = cMonth, fDay = cDay;
	}else{
		fYear = dF.elements[parseInt(SplitDates[1]) + 2].value;
		fMonth = dF.elements[parseInt(SplitDates[1]) + 1].value;
		fDay = dF.elements[parseInt(SplitDates[1])].value;
	}
	if(SplitDates[2]=='c'){
		tYear = cYear, tMonth = cMonth, tDay = cDay;
	}else{
		tYear = dF.elements[parseInt(SplitDates[2]) + 2].value;
		tMonth = dF.elements[parseInt(SplitDates[2]) + 1].value;
		tDay = dF.elements[parseInt(SplitDates[2])].value;
	}
	CompareDates = (PDCount(fYear, fMonth, fDay) - PDCount(tYear, tMonth, tDay)) / 1000;
	if(SplitDates[0]=='d'){
		if (CompareDates >= 17){
			rvalid = true;
		}else{
			rvalid = false;
		}
	}else if(SplitDates[0]=='p'){
		if (CompareDates <= 0){
			rvalid = true;
		}else{
			rvalid = false;
		}
	}
	return rvalid;
}
// Check if value has 2 decimal places, and convert if not
function setTwoDP(netValue){
	// explicitly convert the net value to a string
	netValue = String(netValue);
	// we only want two decimal places displayed
	// see if there is a decimal point
	var stringArray = netValue.split(".");
	if(stringArray[1]!=null)
	{
		// the string value before the decimal point
		var stringValue = stringArray[0];
		// the string value after the decimal point
		var decimalValue = stringArray[1];
		// make sure that we have two decimal places
		if(decimalValue.length<2)
		{
			netValue = stringValue + "." + decimalValue + "0";
		}else{
		// drop the decimal places after the first two
			netValue = stringValue + "." + decimalValue.substr(0,2);
		}
	}
	return netValue;
}
function opennotes(helpcode){
window.open("notes.php?helpcode="+helpcode+"","notes","height=600,width=600,resizable=yes,toolbar=0,status=0,menubar=0,location=no,scrollbars=yes");
}

function check_registration_plate(fieldName)
{

	var patternMatches = new Array();
	var qplateMatch = new Array();

	//This matches the current (AB 05 DEF) format
	patternMatches[0] = /\b[A-Z]{2}[\d\d]{2}[A-Z]{3}\b/;
	//This matches the previous (A 123 BCD) format
	patternMatches[1] = /\b[A-HJ-NP-Y]\d{1,3}[A-Z]{3}\b/;
	//This matches the older (ABC 123 D) format
	patternMatches[2] = /\b[A-Z]{3}\d{1,3}[A-HJ-NP-Y]\b/;
	//Now the even older (ABC 123) format
	patternMatches[3] = /\b(?:[A-Z]{1,2}\d{1,4}|[A-Z]{3}\d{1,3})\b/;
	//And the x2 even older (123 ABC) format
	patternMatches[4] = /\b(?:\d{1,4}[A-Z]{1,2}|\d{1,3}[A-Z]{3})\b/;

	//Pattern match that checks for a Q plate
	qplateMatch[0] = /\b^Q\w+\b/;

	//Set the valid flag to false by default
	var validRegNo = false;

	var frmValue = "regNo = document.frmQuote." + fieldName + ".value.toUpperCase()";
	var errMsg = "";
			

	eval(frmValue);

	//Get the value entered by
	//the user
	//regNo = document.form1.eval(fieldName).value.toUpperCase();

	//Now check it against our regular expressions
	//Setup a for loop to go through all 5 legitimate styles
	//of number plate
	for (i=0;i<5 ;i++ )
	{
		//Check to see if the reg number entered is valid
		//If it is set the validRegNo flag to true
		//If it isnt we leave it as false
		if(regNo.match( patternMatches[i] ))
		{
			//We have a match, set the valid flag to true
			validRegNo = true;
		}
				
	}

	if(!validRegNo)
	{
		//Its an invalid registration.
		errMsg = "Sorry you're registration is invalid.  Please check it and try again.";
	}else
	{
		//Its a valid registration so we need to check if its
		//a Q plate.  If it is we need to set the error message
		//to say Q plates are not allowed
		if(regNo.match( qplateMatch[0] ))
		{
			errMsg = "Sorry we cannot cover Q Plate cars";
		}
	}

	//Thats the checks done.  The valid flag will tell us if the
	//reg number is valid.  If it isnt we want to display a yes/no box
	//asking if theyre using the VIN number.
	if(errMsg != "")
	{
		alert( errMsg );
	}else
	{
		return changepage(true,'paymentopt');
	}
}