// SW - 21/05/07 - Fixed so this now works with field arrays

// will only work if browser can store permanent cookies

// loads the fields we're interested in if not already loaded
function load_fields()
{
	if (typeof(a_rm_fields) == 'undefined') {
	
		// expiry date for remember me details
		days_to_expire = 365;

		// name of cookie to store
		cookie_name = 'hx_remember_me';

		// fields we're interested in remembering...
		// must correspond exactly to the form element names
		a_rm_fields = new Array('Email', 'NumberOfPax', 'Title', 'Initial', 'DataProtection', 'Surname', 'Address[]', 'Address[][0]', 'Address[][1]', 'Address','Address[0]','Address[1]', 'Address0', 'Address1', 'Address2', 'Town', 'DayPhone', 'PostCode', 'EvePhone', 'Registration', 'CarMake', 'CarModel', 'CarColour', 'chk_remember', 'County', 'Country', 'passTitle[]', 'passFirstName[]', 'passLastName[]', 'dobDay[]', 'dobMonth[]', 'dobYear[]', 'gender[]', 'Adults', 'Children', 'Infants');
	}
}

// gets a cookie
function get_cookie(name)
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

// sets a cookie
function set_cookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires.toGMTString() : "") +
		((path)    ? ";path=" + path : "") +
		((domain)  ? ";domain=" + domain : "") +
		((secure)  ? ";secure" : "");
}

// checks if the passed in element name is in the fields array
function interested_in(name)
{
	for (a = 0; a < a_rm_fields.length; a++) {
		if (name == a_rm_fields[a]) {
			return true;
		}
	}
	return false;
}

// store or delete the remember me details
function remember_me_store(chkbox)
{
	load_fields();
	if (chkbox.checked) {
		
		// store form details		
		var frm = chkbox.form;

		// loop through form elements and find those we need to store
		var cookie_value = '';
		for (e = 0; e < frm.elements.length; e++) {
			var el = frm.elements[e];
			if (interested_in(el.name)) {
				cookie_value += el.name + ';';
				if (el.type == 'text') {
					if(el.name == 'PostCode') {
					el.value = el.value.replace(' ', ''); // remove blank space from postcode 
					}
					cookie_value += 'text;' + el.value;
				} else if (el.type == 'select-one') {
					cookie_value += 'select-one;' + el.options.selectedIndex;
				} else if (el.type == 'checkbox') {
					cookie_value += 'checkbox;' + el.checked;
				} else {
					cookie_value += 'invalid;';
				}
				cookie_value += '|';
			}
		}

		// set the cookie if we have a cookie_value
		if (cookie_value != '') {

			// trim final | char
			cookie_value = cookie_value.substring(0, cookie_value.length - 1);
			// encrypt it
			cookie_value = cookie_value;
			// set the expiry date
			var today = new Date();
			var expires = new Date(today.getTime() + (days_to_expire * 86400000));
			// set the cookie for the current domain
			set_cookie(cookie_name, cookie_value, expires, '/', '', '');
		}

	} else {

		// delete form details cookie
		var today = new Date();
		var expires = new Date(today.getTime() - (365 * 86400000));		
		set_cookie(cookie_name, '', expires, '/', '', '');
	}
	
}

// try to retrieve the remember me details
function remember_me_retrieve(frm)
{
	load_fields();
	
	// Field count array
	var aFieldCount = new Array();
	
	// attempt to get the cookie
	var cookie_value = get_cookie(cookie_name);
	if (cookie_value != null) {
		// decrypt it
		cookie_value = cookie_value;
		// split into fields
		var a_cookie_fields = cookie_value.split('|');
		if (a_cookie_fields.length > 0) {
			for (c = 0; c < a_cookie_fields.length; c++) {
				// split into field data
				var a_field_data = a_cookie_fields[c].split(';');
				if (a_field_data.length == 3) {
					var fld_name = a_field_data[0];
					var fld_type = a_field_data[1];
					var fld_data = a_field_data[2];
					if (interested_in(fld_name) && fld_data.toUpperCase() != 'TBC' && fld_data.toUpperCase() != 'TBA') {
						// field must exist in form
						if (typeof(frm.elements[fld_name]) == 'object') {
							var element = frm.elements[fld_name];
							// must be right type
							if (element.type == fld_type) {
								// set the field from the cookie
								if (element.type == 'text') {
									// text field
									element.value = fld_data;
								} else if (element.type == 'select-one') {
									// select box field
									if ((fld_data >= 0) && (fld_data < element.options.length)) {
										element.options.selectedIndex = fld_data;
									}
								} else if (element.type == 'checkbox') {
									element.checked = fld_data;
								}
							} else {
								// Code to check the count in the field array of the current field name
								var currFldCount = 0;
								if ( typeof( aFieldCount[fld_name] ) == 'number' ) { 
									aFieldCount[fld_name] = aFieldCount[fld_name] + 1;
									currFldCount = aFieldCount[fld_name];
								} else {
									aFieldCount[fld_name] = 0;
								}
								if ( element[currFldCount] ) {
									if ( element[currFldCount].type == fld_type ) {
										// set the field from the cookie
										if (element[currFldCount].type == 'text') {
											// text field
											element[currFldCount].value = fld_data;
										} else if (element[currFldCount].type == 'select-one') {
											// select box field
											if ((fld_data >= 0) && (fld_data < element[currFldCount].options.length)) {
												element[currFldCount].options.selectedIndex = fld_data;
											}
										} else if (element[currFldCount].type == 'checkbox') {
											element[currFldCount].checked = fld_data;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

// encrypt a text string
function encrypt(the_text)
{
	output = new String;
	temp = new Array();
	temp2 = new Array();
	text_size = the_text.length;
	for (i = 0; i < text_size; i++) {
		rnd = Math.round(Math.random() * 122) + 68;
		temp[i] = the_text.charCodeAt(i) + rnd;
		temp2[i] = rnd;
	}
	for (i = 0; i < text_size; i++) {
		output += String.fromCharCode(temp[i], temp2[i]);
	}
	return output;
}

// decrypt a text string
function decrypt(the_text)
{
	output = new String;
	temp = new Array();
	temp2 = new Array();
	text_size = the_text.length;
	for (i = 0; i < text_size; i++) {
		temp[i] = the_text.charCodeAt(i);
		temp2[i] = the_text.charCodeAt(i + 1);
	}
	for (i = 0; i < text_size; i = i+2) {
		output += String.fromCharCode(temp[i] - temp2[i]);
	}
	return output;
}

