
var district1_UK_id 		= new Array();
var district1_UK_name 		= new Array();
var district2_UK_ALL_id 	= new Array();
var district2_UK_ALL_name 	= new Array();

district1_UK_id[0] 		 	= "";
district1_UK_name[0] 	 	= "The whole country";
district2_UK_ALL_id[0] 	 	= "";
district2_UK_ALL_name[0] 	= "The whole area";



function write_district1_options(cid)
{
	var id_array = eval("district1_" + cid + "_id");
	var name_array = eval("district1_" + cid + "_name");
	for (var i = 0; i < id_array.length; i++)
		document.write('<option value="' + id_array[i] + '">' + name_array[i] + '</option>');
}





function write_district2_options(cid, d1)
{
	var id_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_id");
	var name_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_name");
	for (var i = 0; i < id_array.length; i++)
		document.write('<option value="' + id_array[i] + '">' + name_array[i] + '</option>');
}





function set_district2_options(cid, d1, obj)
{
	var id_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_id");
	var name_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_name");
	obj.length = id_array.length;
	for (var i = 0; i < id_array.length; i++)
		obj[i] = new Option(name_array[i], id_array[i]);
	obj.selectedIndex = 0;
}





function cv_change_district1()
{
	var obj_id   = eval("district2_" + document.this_page.district1.options[document.this_page.district1.selectedIndex].value + "_id_array");
	var obj_name = eval("district2_" + document.this_page.district1.options[document.this_page.district1.selectedIndex].value + "_name_array");

	document.this_page.district2.length = (obj_id.length);
	document.this_page.district2[0] = new Option("The whole area", "");

	for (var i = 1; i < (obj_id.length); i++)
		document.this_page.district2[i] = new Option(obj_name[i], obj_id[i]);
}

function validate_not_blank(field, name)
{
	// strip all spaces at start and end of line
	var regx_output = new String(field.value);
	var re1 = new RegExp("^[ ]*|([ ]*)\$", 'g');
	field.value = regx_output.replace(re1, "");
	
	if (field.value == "")
	{
		alert(format_str('The "%s" field cannot be blank.', name));
		focus_field(field);
		return false;
	}
	return true;
}




function strip_html(txt)
{
	txt = txt + "";
	
	var re = new RegExp();
	re = /(<.+>)/gi;
	txt = txt.replace(re, "");
	
	return txt;
}




function validate_email_address(field, name, allow_name_in_address, display_error)
{
	if (typeof display_error == "undefined")
	{
		display_error = true;
	}
	
	// Get field value
	var fieldval = field.value;
	
	// Remove spaces before and after email
	fieldval = fieldval.replace(/^\\s*/, "");
	fieldval = fieldval.replace(/\\s*\$/, "");
	
	// Update field
	field.value = fieldval;
	
	// If "name <email>" format is available - get email address
	if (allow_name_in_address)
	{
		var a = fieldval.indexOf("<");
		var b = fieldval.indexOf(">");
		if (a < b)
		{
			fieldval = fieldval.substring(a + 1, b);
		}
	}
	
	// Initialize
	var msg		= "";
	var last_at	= fieldval.lastIndexOf("\@");
	var before	= fieldval.substr(0, last_at);
	var after	= fieldval.substr(last_at + 1, fieldval.length - last_at - 1);
	var reg;
	
	// CHECK: the @ character must exist
	if (last_at < 0)
	{
		msg = "There is no @ in your e-mail address.";
	}
	
	// CHECK: there must be at least one character preceeding and one character following @
	if (!msg)
	{
		if ((before.length < 1) || (after.length < 1))
		{
			msg = "Your e-mail address must contain at least 1 character in front of @.";
		}
	}
	
	// CHECK: characters below 32 are illegal
	if (!msg)
	{
		for (var i = 0; i < fieldval.length; i++)
		{
			var code = fieldval.charCodeAt(i);
			if (code < 32)
			{
				msg = "You have used an invalid character in your email address.";
				break;
			}
		}
	}
	
	// CHECK: the ".@" combination is illegal
	if (!msg)
	{
		if (before.substr(before.length - 1, 1) == ".")
		{
			msg = "Du har skrevet et ulovligt tegn før @.";
		}
	}
	
	// CHECK: the "@." combination is illegal
	if (!msg)
	{
		if (after.substr(0, 1) == ".")
		{
			msg = "A full stop cannot be the first character after @.";
		}
	}
	
	// CHECK: at least one "." is required in the host section
	if (!msg)
	{
		if (after.indexOf(".") < 0)
		{
			msg = "There is no full stop in your e-mail address.";
		}
	}
	
	// CHECK: "." cannot be the last character
	if (!msg)
	{
		if (after.lastIndexOf(".") == (after.length - 1))
		{
			msg = "A full stop cannot be the last character in an e-mail address.";
		}
	}
	
	// CHECK: the "-." combination is illegal following @
	if (!msg)
	{
		reg = /\\-{1}\\.{1}/;
		if (reg.test(after))
		{
			msg = "A dash  (-) cannot be placed in front of a full stop (.)";
		}
	}
	
	// CHECK: "-" cannot be the last character
	if (!msg)
	{
		if (after.lastIndexOf("-") == (after.length - 1))
		{
			msg = "A dash (-) cannot be the last character in an e-mail address.";
		}
	}
	
	// CHECK: certain characters preceeding @ must be escaped
	if (!msg)
	{
		// 1) remove all escaped characters
		// 2) all remaining special characters were not escaped - which is illegal
		before = before.replace(/\\\\{1}.{1}/g, "");
		reg = /[\\s<>()\\[\\]\\\\,;:@\\"]{1,}/;
		if (reg.test(before))
		{
			msg = "You have used a character that cannot be used in front of the @.";
		}
	}
	
	// CHECK: the characters following @ are restricted
	// ASCII Characters below 32, not allowed
	if (!msg)
	{
		reg = new RegExp("^[\\x00-\\x20]*\$");
		if (reg.test(after))
		{
			msg = "You have used a character that cannot be used after the @.";
		}
	}
	
	// If the address is illegal
	if ((msg) && (display_error = true))
	{
		alert(format_str("The e-mail address '%s' is incorrect:\n%s\n\nE-mail addresses are usually formatted in the following way:\n - name.surname@company.co.uk\n - username@internetserviceprovider.com\n - initials@serviceprovider.com\n   etc.", name, msg));
		focus_field(field);
		return false;
	}
	
	// The address seems to be in order
	return true;
}



