/**
 * Generic forms include.
 *
 * @author		Owen Blacker
 * @author		Oliver Cope
 * @version		2.2.2, 2003-09-12
 * @see			<http://www.marksandspencer.com/>
 * @see			<http://www2.marksandspencer.com/>
 *
 * Copyright © Wheel:, London 1999-2003.
 * Licensed to Marks & Spencer plc.
 */

var submitted = false;

function validate(n, toValidate, errString) {
	if (submitted) return false;
	if (('' + n) == "undefined") n = 0;

	if (arguments.length < 2) {
		return oldValidate(n);
	} // if (arguments.length < 2)

	if (arguments.length < 3) {
		var errString = "Please check the following fields:\n";
	} // if (arguments.length < 3)

	if (toValidate.length == 0) {
		void document.forms[n].submit();
		submitted = true;
		return true;
	} // if (toValidate.length == 0)

	var formOkay = true;
	for (var ix = 0; ix < toValidate.length; ix++) {
		if (!checkField(n, toValidate[ix])) {
			formOkay = false;
			if (errString.indexOf("     -  " + toValidate[ix].longname) < 0) {
				errString += "     -  " + toValidate[ix].longname + "\n";
				if (toValidate[ix].explanation != '') {
					errString += "         " + toValidate[ix].explanation + "\n";
				} // if (toValidate[ix].explanation != '')
			} // if (errString.indexOf(...) < 0)
		} // if (!checkField(n, toValidate[ix]))
	} // for (ix)

	if (!formOkay) {
		void alert(errString);
		submitted = false;
	} else {
		void document.forms[n].submit();
		return true;
	} // if (!formOkay)

	return false;
} // public function Boolean validate(Integer n, ValidatedField[] toValidate, String errString)
  // public function Boolean validate(String  n, ValidatedField[] toValidate, String errString)
  // public function Boolean validate(Integer n, ValidatedField[] toValidate)
  // public function Boolean validate(String  n, ValidatedField[] toValidate)

/*-------------------------------------------------------------------------------------*/

function ValidatedField(name, longname, expr, explanation) {
	this.name = name + '';

	if (longname + '' == "undefined" && longname + '' == '' && longname + '' == "null") {
		this.longname = subst(this.name.substring(0,1).toUpperCase() + this.name.substring(1,this.name.length), "_", " ");
	} else {
		this.longname = longname + '';
	} // if (longname ...)

	if (expr + '' != "undefined" && expr + '' != '' && expr + '' != "null") {
		this.expr = expr + '';
	} else {
		this.expr = '';
	} // if (expr ...)

	if (explanation + '' != "undefined" && explanation + '' != '' && explanation + '' != "null") {
		this.explanation = explanation + '';
	} else {
		this.explanation = '';
	} // if (explanation ...)

	var debugStr = "ValidatedField:\n";
	debugStr += "\n    - " + this.name;
	debugStr += "\n    - " + this.longname;
	debugStr += "\n    - " + this.expr;
	debugStr += "\n    - " + this.explanation;
//	void alert(debugStr);
	return this;
} // public constructor ValidatedField(String name, String longname, String expr, String explanation)

/*-------------------------------------------------------------------------------------*/
function checkField(n,field) {
	var theValue;
	var theForm = document.forms[n];

	// alert("Processing checkField(" + n + ", " + field + ")...");

	if ('' + field.name == "undefined" || theForm.elements[field.name] + '' == "undefined") {
		alert(field + " does not exist");
		return false;
	} // if (theForm.elements[field.name] + '' == "undefined")

	var thisElement = document.forms[n][field.name];
	if (navigator.userAgent.indexOf("MSIE 3.") > -1) {
		theValue = thisElement.value + '';
		if (theValue == "undefined") return true;
	} else {
		if (thisElement.type == "text" || thisElement.type == "textarea" || thisElement.type == "hidden") theValue = thisElement.value;
		if (('' + thisElement.type).indexOf("select") > -1) theValue = thisElement.options[thisElement.selectedIndex].value;

		if (thisElement.type + "" == "undefined" && thisElement.length + "" != "undefined") {
			theValue = '';
			for (var ix = 0; ix < thisElement.length; ix++) {
				if (thisElement[ix].type != "radio") continue;
				if (thisElement[ix].checked) theValue = thisElement[ix].value;
			} // for (ix)
		} // if (thisElement.type == "radio")
	//	if (thisElement.type == "radio") {
	//		theValue = '';
	//		for (var ix < 0; ix <= thisElement.length; ix++) {
	//			if (thisElement[ix].checked) theValue = thisElement[ix].value;
	//		} // for (ix)
	//	} // if (thisElement.type == "radio")
	} // if (navigator.userAgent.indexOf("MSIE 3.") > -1)

	var result = true;
	if (field.expr == '') {
		if ('' + theValue != "undefined" && theValue.replace(/\s/gi,'') == '') result = false;
	} else {
		result = eval(field.expr);
	} // if (field.expr == '')

	return result;
} // public function Boolean checkField(Integer n, ValidatedField field)
  // public function Boolean checkField(String  n, ValidatedField field)

/*-------------------------------------------------------------------------------------*/

function isInt(s) {
	s = '' + s;
	var allowedChars = "-0123456789,";
	var result = true;
	for (var ix = 0; ix < s.length; ix++) result = result && (allowedChars.indexOf(s.substring(ix,ix+1)) > -1);
	result = result && (s.length > 0)
	return result;
} // public function Boolean isInt(Object s)

function isFloat(s) {
	s = '' + s;
	var allowedChars = "-0123456789.,";
	var result = true;
	for (var ix = 0; ix < s.length; ix++) result = result && (allowedChars.indexOf(s.substring(ix,ix+1)) > -1);
	result = result && (s.length > 0)
	return result;
} // public function Boolean isFloat(Object s)

function isPhone(s) {
	s = '' + s;
	var allowedChars = "()+-0123456789. ";
	var result = true;
	for (var ix = 0; ix < s.length; ix++) result = result && (allowedChars.indexOf(s.substring(ix,ix+1)) > -1);
	result = result && (s.length > 0)
	return result;
} // public function Boolean isPhone(Object s)

function isCurrency(s) {
	s = '' + s;
	var allowedChars = "£$€-0123456789., ";
	var result = true;
	for (var ix = 0; ix < s.length; ix++) result = result && (allowedChars.indexOf(s.substring(ix,ix+1)) > -1);
	result = result && (s.length > 0)
	return result;
} // public function Boolean isCurrency(Object s)

function isYear(s) {
	s = '' + s;
	var allowedChars = "0123456789";
	var result = true;
	for (var ix = 0; ix < s.length; ix++) result = result && (allowedChars.indexOf(s.substring(ix,ix+1)) > -1);

	result = result && (s.length == 4)
	result = result && (s.substring(0,2) == "19" || s.substring(0,2) == "20");
	return result;
} // public function Boolean isYear(Object s)

function isemail(s) {
	return isEmail(s);
} // public function Boolean isemail(Object s)

function isEmail(s) {
	var invalid = "<>:;!\"£#$^*()'[]{}~+=?/,`|\\";       // invalid email address characters
	var result = true;
	for (var i = 0; i < invalid.length; i++) {
		result = result && (s.indexOf(invalid.substring(i,i+1)) < 0)
	} // for (i)
	if (s.indexOf("@") == -1 || s.indexOf(".") == -1 || s.indexOf("@") != s.lastIndexOf("@")) result = false;
	return result;
} // public function Boolean isEmail(Object s)

function isTextField(s) {
	var result = true;
	if (s = "")
	{
		result = false;
	}
	return result;
} // public function Boolean isEmail(Object s)

function isEmail_RE(s) {
	s += '';
	var re = /^[\w!#$%'*+\-\/=?^`{}|~][\w\.!#$%'*+\-\/=?^`{}|~]+\@[A-Z0-9\-]+\.[A-Z0-9\.\-]+[A-Z]$/i;
	return (s != '' && s.search(re) != -1);
} // public function Boolean isEmail_RE(String s)

function isPostCode(s) {
	// Convert postcode to upper case
	s += '';
	s  = s.toUpperCase();
	var whitespace = " \t\n\r";

	// Array of valid postcode formats
	var valid = "AANANaa,AANNNaa,AANNaa,ANANaa,ANNNaa,ANNaa".split(",");

	// Strip out any whitespace
	var strippedPcode = "";
	for (var ix = 0; ix < s.length; ix++) {
		if (whitespace.indexOf(s.charAt(ix)) < 0) strippedPcode += s.charAt(ix);
	} // for (ix)

	var k;
	for (k = 0; k < valid.length && !postcode_testPattern(strippedPcode, valid[k]); k++);
	if (k >= valid.length) return false;
	return true;
} // public function Boolean isPostCode(Object s)

function postcode_testPattern(c, pattern) {
	// Tests if string c validates against the given pattern
	if (c.length != pattern.length) return false;
	var ix;
	for (ix = c.length-1; ix >= 0 && postcode_testChar(c.charAt(ix), pattern.charAt(ix), ix); ix--);
	return (ix < 0);
} // private function Boolean postcode_testPattern(String c, String pattern)

function postcode_testChar(c, targetType) {
	var types = new Array();
	types["A"] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	types["N"] = "1234567890";
	types["a"] = "ABDEFGHJLNPQRSTUWXYZ";
	return (types[targetType].indexOf(c) > -1);
} // private function Boolean postcode_testChar(String c, String targetType)

function isPostcode(s) {
	return isPostCode(s);
} // public function Boolean isPostcode(Object s)

function countWords(s) {
	var words = 0;
	var separators = ".,;:!? \t\n\r";

	var isWord = false;

	for (var ix = 0; ix < s.length; ix ++) {
		if (isWord) {
			if (separators.indexOf(s.substring(ix,ix+1)) > -1) {
				words++;
				isWord = false;
			} // if (separators.indexOf(...) > -1)
		} else {
			if (separators.indexOf(s.substring(ix,ix+1)) < 0) {
				isWord = true;
			} // if (separators.indexOf(...) < 0)
		} // if (isWord)
	} // for (ix)

	if (isWord) words++;
	return words;
} // public function Integer countWords(String s)

/* ######################################################################################################################### */

/**
 * Old forms.js functions, for backwards compatibility
 * Copyright © The Presentation Company, London 1998.
 */

// Track whether form has been submitted yet
var submitted = false;

// There's some images we want to have preloaded.
if ('' + document.images != "undefined") {
	var theImage = new Image();
	theImage.src = "/images/formhandler/aniclock.gif";
	var theLogo  = new Image();
	theLogo.src  = "/images/formhandler/logo.gif";
	var thankYou = new Image();
	thankYou.src = "/images/formhandler/thank-you.gif";
} // if ('' + document.images != "undefined")

function oldValidate(formnumber) {
	// Check to make sure we don't do multiple form submits
	if (submitted) return false;
	if (navigator.userAgent.indexOf("MSIE 3") != -1) return true;
	var submitted = false;
	if (('' + formnumber) == "undefined") formnumber = 0;

	var empty = plural = error = required = '';

	if ('' + document.forms[formnumber].elements["required"] != "undefined"
	 && '' + document.forms[formnumber].elements["required"] != '') {
		required = "," + subst('' + document.forms[formnumber].elements["required"].value, " ", '') + ",";
	} // if (...)

	var thisElement;

	for (var i = 0; i < document.forms[formnumber].elements.length; i++) {
		thisElement = document.forms[formnumber].elements[i];
		if ((thisElement.name.charAt(thisElement.name.length - 1) == "*" || required.indexOf("," + thisElement.name + ",") > -1) && thisElement.value == '') {
			// Add name of empty field to string
			empty += " " + thisElement.name + ",";
			i = document.forms[formnumber].elements.length;
		} // if (...)
	} // for (i)

	if (empty != '') {
		// Remove trailing comma
		empty = empty.substring(0, empty.length - 1);
		if (empty.indexOf(",") != -1) {
			empty = empty.substring(0,empty.lastIndexOf(",", empty.length)) + " and" +
				empty.substring(empty.lastIndexOf(",") + 1, empty.length);
			plural = "s";
		} // if (empty.indexOf(",") != -1)

		// Strip off the * from the alert
		if (empty.charAt(empty.length-1) == "*") empty = empty.substring(0,empty.length-1);
		error = "You have not supplied any information in the" + empty + " field" + plural;
		alert(error);
	} else if (checkemail(formnumber) == '') {
		submitted = true;
		if ('' + document.forms[formnumber].nosubmit == "undefined") {
			document.forms[formnumber].submit();
		} // if ('' + document.forms[formnumber].nosubmit == "undefined")
	} // if (empty != '')

	return (submitted);
} // public function Boolean oldValidate(Integer formnumber) @deprecated
  // public function Boolean oldValidate(String  formnumber) @deprecated

function checkemail(formnumber) {
	if ('' + document.forms[formnumber].email == "undefined") {
		// There's no email field, so don't complain about it being invalid...
		return '';
	} // if ('' + document.forms[formnumber].email == "undefined")

	var to = document.forms[formnumber].email.value			// pick up the email value in our form.
	var invalid = "<>:;!\"£#$^*()'[]{}~+=?/,`|\\";       // invalid email address characters
	for (var i = 0; i < invalid.length; i++) {
		if (to.indexOf(invalid.substring(i,i+1)) >= 0) {
			error = (navigator.appName == "Netscape" && navigator.appVersion.substring(0,1) < "3")
			? '' : "contains invalid characters";
		} // if (to.indexOf(invalid.substring(i,i+1)) >= 0)
	} // for (i)

	if (to.indexOf("@") == -1 || to.indexOf(".") == -1 || to.indexOf("@") != to.lastIndexOf("@")) error = "is not valid";
	if (error != '') {
		error = "Your email address " + error;
		alert(error);
	} // if (error != '')

	return error;
} // public function String checkemail(Integer formnumber) @deprecated
  // public function String checkemail(String  formnumber) @deprecated


function clear(formnumber) {
	if (('' + formnumber) == "undefined") formnumber = 0;
	if (navigator.appName == "Netscape") reply = document.forms[formnumber];
	void reply.reset();
} // public function void clear(Integer formnumber) @deprecated
  // public function void clear(String  formnumber) @deprecated

function dependent(formnumber) {
	// Check to make sure we don't do multiple form submits.
	if (submitted) return;

	if (('' + formnumber) == "undefined") formnumber = 0;
	if (navigator.appName == "Netscape") reply = document.forms[formnumber];

	var check = "chargecard*";
	var dependent = "Account number";

    for (j = 0; j < reply.elements.length; j++) {
		if (check == reply.elements[j].name) checkpos = j;
		if (dependent == reply.elements[j].name) dependentpos = j;
	} // for (j)

	if (navigator.appVersion.charAt(0) > "3") {
		if (reply.elements[checkpos].checked == 0 && reply.elements[dependent].value == '') {
			alert("Your account number field is incomplete.");
		} else {
			validate(formnumber);
		} // if (...)
	} else {
		submitted = true;
		document.forms[formnumber].submit();
	} // if (navigator.appVersion.charAt(0) > "3")
} // public function void dependent(Integer formnumber) @deprecated
  // public function void dependent(String  formnumber) @deprecated

function formsinit() {
	// Empty function to avoid errors if still called
} // public function void formsinit(void) @deprecated

// Works just like s in Perl
function subst(expr, pattern, replace) {
    var newString = '';

    for (var i = 0; i < expr.length; i++) {
        if (expr.substring(i, i + pattern.length) == pattern) {
            newString += replace;
            i += pattern.length - 1;
        } else {
            newString += expr.charAt(i);
        } // if (expr.substring(i, i + pattern.length) == pattern)
    } // for (i)

    return newString;
} // public function String subst(String expr, String pattern, String replace)

// Creates an indexed array of strings from a delimited (by delim) list string (expr)
function split(delim, expr) {
	this.length = 0;
	if (expr.substring(0, delim.length) == delim) expr = expr.substring(delim.length, expr.length);
	if (expr.substring((expr.length - delim.length), expr.length) != delim) expr += delim;

	for (var i = 0; i < expr.length; i++) {
		this[this.length] = expr.substring(i, expr.indexOf(delim, i + delim.length));
		i = expr.indexOf(delim, i + delim.length) + delim.length - 1;
		this.length++;
	} // for (i)

	return this;
} // public function String[] split(String delim, String expr)


function debug(obj) {
	var theStr = "";
	properties = new Array();
	var pageLength = 10;
	for (var ix in obj) {
		properties[properties.length] = ix + " : " + obj[ix];
	}
	if (properties.length == 0) alert("Object has no properties!");
	for (var ix = 0; ix < properties.length; ix += pageLength) {
		theStr = properties.slice(ix, ix+pageLength).join("\n");
		alert (theStr);
	}
}