
var loc_obj = new location_class();

function location_class()
{
	this.district1 = new Array();
}


function district1(id, name)
{
	this.id = id;
	this.name = name;
	this.district2 = new Array();
}


function district2(id, name)
{
	this.id = id;
	this.name = name;
	this.district3 = new Array();
}


function district3(id, name)
{
	this.id = id;
	this.name = name;
}


function upd_d1(id, text)
{
	loc_obj.district1[id] = new district1(id, text);
}


function upd_d2(d1, id, text)
{
	loc_obj.district1[d1].district2[id] = new district2(id, text);
}


function upd_d3(d1, d2, id, text)
{
	loc_obj.district1[d1].district2[d2].district3[id] = new district3(id, text);
}


function get_district1_list(selected_d1)
{
	var country_index = document.this_page.country[document.this_page.country.selectedIndex].value;
	document.this_page.district1.selectedIndex = 0;
	
	if (country_index == 'DK')
	{
		document.this_page.district1.length = 1;
		for (var d1 in loc_obj.district1)
		{
			var id = loc_obj.district1[d1].id;
			var text = loc_obj.district1[d1].name;
			document.this_page.district1.options[document.this_page.district1.length] = new Option(text, id);
			
			// Set selected District1
			if (id == selected_d1)
				{ document.this_page.district1.options[document.this_page.district1.length-1].selected = 1; }
		}
		document.getElementById("section_d1").style.display = "inline";
	}
	else
	{
		document.this_page.district1.length = 1;
		document.this_page.district2.length = 1;
		document.this_page.district3.length = 1;
		document.getElementById("section_d1").style.display = "none";
		document.getElementById("section_d2").style.display = "none";
		document.getElementById("section_d3").style.display = "none";
	}
}


function get_district2_list(selected_d2)
{
	var d1_id = document.this_page.district1[document.this_page.district1.selectedIndex].value;
	if (d1_id != "" && loc_obj.district1[d1_id].district2.length > 0)
	{
		document.this_page.district2.length = 1;
		for (var d2 in loc_obj.district1[d1_id].district2)
		{
			var id = loc_obj.district1[d1_id].district2[d2].id;
			var text = loc_obj.district1[d1_id].district2[d2].name;
			document.this_page.district2.options[document.this_page.district2.length] = new Option(text, id);
			
			// Set selected District2
			if (id == selected_d2)
				{ document.this_page.district2.options[document.this_page.district2.length-1].selected = 1; }
		}
		document.getElementById("section_d2").style.display = "inline";
		document.this_page.district3.length = 1;
		document.getElementById("section_d3").style.display = "none";
	}
	else
	{
		document.this_page.district2.length = 1;
		document.this_page.district3.length = 1;
		document.getElementById("section_d2").style.display = "none";
		document.getElementById("section_d3").style.display = "none";
	}
}


function get_district3_list(selected_d3)
{
	try
	{
		var d1_id = document.this_page.district1[document.this_page.district1.selectedIndex].value;
		var d2_id = document.this_page.district2[document.this_page.district2.selectedIndex].value;
		
		if (d1_id != "" && d2_id != "" && loc_obj.district1[d1_id].district2[d2_id].district3.length > 0)
		{
			document.this_page.district3.length = 1;
			for (var d3 in loc_obj.district1[d1_id].district2[d2_id].district3)
			{
				var id = loc_obj.district1[d1_id].district2[d2_id].district3[d3].id;
				var text = loc_obj.district1[d1_id].district2[d2_id].district3[d3].name;
				document.this_page.district3.options[document.this_page.district3.length] = new Option(text,id);
				
				// Set selected District3
				if (id == selected_d3)
					{ document.this_page.district3.options[document.this_page.district3.length-1].selected = 1; }
			}
			document.getElementById("section_d3").style.display = "inline";
		}
		else
		{
			document.this_page.district3.length = 1;
			document.getElementById("section_d3").style.display = "none";
		}
	}
	catch(e)
	{ }
}


function set_location(d1, d2, d3)
{
	get_district1_list(d1);
	get_district2_list(d2);
	get_district3_list(d3);
}


function relocate_to(url, target_ref)
{
	var hash = "";
	
	if (url.indexOf("#") >= 0)
	{
		hash = url.substring(url.indexOf("#"));
		url = url.substring(0, url.indexOf("#"));
	}
	
	if (url.indexOf("?") == -1)
		url += "?";

	if (url.indexOf("?") != (url.length - 1))
		url += "&";

	var target;
	if (target_ref == null)					target = "top";
	else if (typeof target_ref == "object")	target = target_ref.name;
	else if (typeof target_ref == "string")	target = target_ref;
	
	var obj = eval(target);
	
	if (typeof sc != "undefined")
		url += "sc=" + sc + "&";
	
	obj.location.href = url + hash;
	
	return true;
}


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('Feltet "%s" må ikke være blankt.', name));
		focus_field(field);
		return false;
	}
	return true;
}


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 = "Tegnet @ mangler i e-mail adressen.";
	}
	
	// CHECK: there must be at least one character preceeding and one character following @
	if (!msg)
	{
		if ((before.length < 1) || (after.length < 1))
		{
			msg = "E-mail adressen skal indeholde mindst et tegn før @.";
		}
	}
	
	// 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 = "Du har skrevet et ulovligt tegn i e-mail adressen.";
				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 = "Et punktum må ikke stå umiddelbart efter @.";
		}
	}
	
	// CHECK: at least one "." is required in the host section
	if (!msg)
	{
		if (after.indexOf(".") < 0)
		{
			msg = "Der er intet punktum i din e-mailadresse.";
		}
	}
	
	// CHECK: "." cannot be the last character
	if (!msg)
	{
		if (after.lastIndexOf(".") == (after.length - 1))
		{
			msg = "Et punktum må ikke være sidste tegn i en e-mailadresse";
		}
	}
	
	// CHECK: the "-." combination is illegal following @
	if (!msg)
	{
		reg = /\\-{1}\\.{1}/;
		if (reg.test(after))
		{
			msg = "En bindestreg må ikke stå umiddelbart foran et punktum i en e-mailadresse.";
		}
	}
	
	// CHECK: "-" cannot be the last character
	if (!msg)
	{
		if (after.lastIndexOf("-") == (after.length - 1))
		{
			msg = "En bindestreg må ikke være sidste tegn i en e-mailadresse.";
		}
	}
	
	// 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 = "Du har skrevet et tegn i din e-mailadresse som ikke må anvendes foran @.";
		}
	}
	
	// CHECK: the characters following @ are restricted
	// ASCII Characters below 32, not allowed
	if (!msg)
	{
		reg = new RegExp("^[\\x00-\\x20]*\$");
		if (reg.test(after))
		{
			msg = "Du har skrevet et tegn i din e-mailadresse som ikke må anvendes efter @";
		}
	}
	
	// If the address is illegal
	if ((msg) && (display_error = true))
	{
		alert(format_str("Email adressen '%s' er forkert:\n%s\n\nE-mail-adresser har almindeligvis følgende format:\n - fornavn.efternavn@virksomhed.dk\n - brugernavn@internetudbyder.dk\n - initialer@serviceudbyder.com\n   o.s.v.", name, msg));
		focus_field(field);
		return false;
	}
	
	// The address seems to be in order
	return true;
}



var focus_field_name = "";

function focus_field(field, frame_ref)
{				
	if (typeof field == "string")
	{
		if (field != "")
		{
			focus_field_name = field;
			return true;
		}
		if (focus_field_name == "")
		{
			for (var i = 0; i < frame_ref.document.this_page.elements.length; i++)
			{
				var obj = frame_ref.document.this_page.elements[i];
				if ((obj.name != "") && (obj.type != "hidden"))
				{
					focus_field_name = obj.name;							
					if (obj.length != 0)
					{
						if ((obj.type == "radio") || (obj.type == "checkbox"))
						{
							focus_field_name = obj.name +"[0]";
						}
					}
					break;
				}
			}
			if (focus_field_name == "")
			{
				return true;
			}
		}
		field = frame_ref.eval("document.this_page."+ focus_field_name);
	}
	if (typeof field == "object")
	{
		self.focus();
		field.focus();
		if (field.type == "text")
		{
			field.select();
		}
		if ((field.type == "select-one") || (field.type == "select-multiple"))
		{
			if ((field.selectedIndex == -1) && (field.length != -1))
			{
				field.selectedIndex = 0;
			}
		}
		focus_field_name = "";
		return true;
	}
	return true;
}


function format_str(str)
{
	var i = 1, pos = str.toUpperCase().indexOf("%S");
	while ((pos != -1) && (i < format_str.arguments.length))
	{
		var value = format_str.arguments[i++];
		if (value == null)
			value = "";
		str = str.substring(0, pos) + value + str.substring(pos + 2);
		pos = str.toUpperCase().indexOf("%S", pos + value.length);
	}
	return str;
}


function on_enter(event, function_name)
{
	var isMAC = ((navigator.userAgent.toLowerCase().indexOf("mac") >= 0) ||
					(navigator.userAgent.toLowerCase().indexOf("powerpc") >= 0));
					
	// Accept all other characters than ENTER (and also SUBMIT on MAC)
	var char_code = (navigator.appName == "Netscape") ? event.which : event.keyCode;
	var is_ENTER = ((char_code == 13) || ((isMAC == 1) && (char_code == 3)));
	if (!is_ENTER)
		return true;

	// Run code
	eval(function_name);
	return false;
}


function list_add_item(name)
{
	var obj_ref_source 	  	= eval("document.this_page." + name + "_src");
	var obj_ref_destination = eval("document.this_page." + name + "_dest");
	
	if (obj_ref_source.selectedIndex == -1)	
	{
		return false;
	}
	
	for(var i=0; i<obj_ref_source.options.length; i++)
	{
		if (obj_ref_source.options[i].selected == 1)
		{
			var id = obj_ref_source.options[i].value;
			var exists = 0;
			
			for(var j=0; j<obj_ref_destination.options.length; j++)
			{
				if (obj_ref_destination.options[j].value == id)
				{
					exists = 1;
					break;
				}
			}
			
			if (!exists)
			{
				var value = obj_ref_source.options[i].value;
				var text  = obj_ref_source.options[i].text;
				obj_ref_destination.options[obj_ref_destination.options.length] = new Option(text, value);
			}
			
			obj_ref_source.options[i].selected = 0;
		}
	}
}


function list_remove_item(name)
{
	var obj_ref_destination = eval("document.this_page." + name + "_dest");
	var items_value = new Array();
	var items_text 	= new Array();
	
	if (obj_ref_destination.selectedIndex == -1)
	{
		return false;
	}
	
	for(var i=0; i<obj_ref_destination.options.length; i++)
	{
		if (obj_ref_destination.options[i].selected == 0)
		{
			items_value[items_value.length] = obj_ref_destination.options[i].value;
			items_text[items_text.length]  = obj_ref_destination.options[i].text;
		}
	}
	
	obj_ref_destination.options.length = 0;
	
	for(var i=0; i<items_value.length; i++)
	{
		var value = items_value[i];
		var text  = items_text[i];
		obj_ref_destination.options[obj_ref_destination.options.length] = new Option(text, value);
	}
}



