function relocate(url, target)
{
	eval(format_str("%s.location.href=url", target));
}

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 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;
}

function search_jobs(page)
{
	// categori
	var cat			= document.this_page.cat.value;

	// location
	var locations	= document.this_page.locations.value;

	// salary
	var salary		= document.this_page.salary.value;

	// job type
	var job_type	= document.this_page.job_type.value;

	// free text
	var free_text 	= document.this_page.free_text.value;
	free_text 	  	= escape(format_free_text(free_text));
	
	// list page
	var list_page= "";
	
	// ads on list
	var ads_on_list = "";
	if (typeof(document.this_page.ads_on_list) != 'undefined')
	{ads_on_list = document.this_page.ads_on_list.value;}

	page = (page == undefined || page == "")? "1" : page;
	
	// build Search Parameters
	var search_params  = new Array();
	search_params[search_params.length] = "search_type=jobs";
	search_params[search_params.length] = "cat=" + cat;
	search_params[search_params.length] = "location=" + locations;
	search_params[search_params.length] = "salary=" + salary;
	search_params[search_params.length] = "job_type=" + job_type;
	search_params[search_params.length] = "free_text=" + free_text;
	search_params[search_params.length] = "page=" + page;
	search_params[search_params.length] = "ads_on_list=" + ads_on_list;
	search_params[search_params.length] = "sort_by=";

	str_params = search_params.join("&");

document.this_page.action = "search_list_jobs.asp?" + str_params;
document.this_page.submit();
}

// get_date_format
function get_date_format(year_string,date_string,month_string)
{
	// Get date created as object
	var today 			= new Date();
	var date_created	= new Date(year_string,month_string,date_string);
	var str_day	 	 	= new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var str_month	 	= new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var str_date 	 	= str_day[date_created.getDay()] + ", " + date_created.getDate() + " " + str_month[date_created.getMonth()];

	
	// Check today
	if (today.getDate() == date_created.getDate())
	{
		str_date = "Today ";// + str_hours + ":" + str_minutes;
	}
	// Check yesterday
	else if ( (today.getDate()-1 == date_created.getDate()) && 
			  (today.getMonth() == date_created.getMonth()) && 
			  (today.getFullYear() == date_created.getFullYear()) )
	{
		str_date = "Yesterday";// + str_hours + ":" + str_minutes;
	}

	document.write(str_date);
}

function save_job(job_id)
{
	// Save Job Ad
	document.this_page.command.value = "save_job_ad";
	document.this_page.job_id.value = job_id;
	document.this_page.submit();
}



function validate_username_password()
{

	var frm = document.this_page;
	var username_label = "Username";
	var password_1_label = "Password";
	var password_2_label = "Repeat password";

	// Define illegal characters for usr + pwd
	var illegal_char_array = new Array("<", ">", "'", '"', "\\\\");

	// USERNAME
	if (!validate_not_blank(frm.username, username_label))
		return false;
		
	if (frm.username.value.indexOf(" ") >= 0)
	{
		alert("User name cannot contain spaces.");
		focus_field(frm.username);
		return false;
	}
	
	if (frm.username.value.length > 200)
	{
		alert("Your user name is too long. Max. 200 characters.");
		focus_field(frm.username);
		return false;
	}
	
	if (frm.username.value.length < 6)
	{
		alert("Your username must contain at least 6 characters.");
		focus_field(frm.username);
		return false;
	}
	
	for (var i = 0; i < illegal_char_array.length; i++)
		if (frm.username.value.indexOf(illegal_char_array[i]) >= 0)
		{
			alert(format_str("Your user name cannot contain the character '%s'.", " " + illegal_char_array[i] + " "));
			focus_field(frm.username);
			return false;
		}
	
	// PASSWORD
	if (!validate_not_blank(frm.password_1, password_1_label))
		return false;
	if (!validate_not_blank(frm.password_2, password_2_label))
		return false;
	
	if (frm.password_1.value.indexOf(" ") >= 0)
	{
		alert("Password cannot contain spaces.");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	if (frm.password_1.value.length > 10)
	{
		alert("Your password is too long. Max. 10 characters.");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	if (frm.password_1.value.length < 6)
	{
		alert("Your password must contain at least 6 characters.");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	if (frm.password_1.value != frm.password_2.value)
	{
		alert("Passwords were not identical.");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	for (var i = 0; i < illegal_char_array.length; i++)
	{
		if (frm.password_1.value.indexOf(illegal_char_array[i]) >= 0)
		{
			alert(format_str("Your password cannot contain the character '%s'.", illegal_char_array[i]));
			frm.password_1.value = "";
			frm.password_2.value = "";
			focus_field(frm.password_1);
			return false;
		}
	}
	
	// CAPS-LOCK on both username and password is not accepted
	// - also handling extended characters like "æøå" which JavaScript ignores in Case functions
	if (frm.username.value == frm.username.value.toUpperCase())
		if (frm.username.value.toLowerCase() != frm.username.value.toUpperCase())
			if (frm.password_1.value == frm.password_1.value.toUpperCase())
				if (frm.password_1.value.toLowerCase() != frm.password_1.value.toUpperCase())
				{
					alert("Neither the user name nor the password can be written solely using upper-case letters.\\nTurn off the Caps-Lock facility by pressing the Caps-Lock key.");
					frm.password_1.value = "";
					frm.password_2.value = "";
					focus_field(frm.password_1);
					return false;
				}
	
	// USERNAME == PASSWORD
	if (frm.username.value.toUpperCase() == frm.password_1.value.toUpperCase())
	{
		alert("Your username and password cannot be identical.");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	// PASSWORD == "PASSWORD"
	if ( frm.password_1.value.toUpperCase() == "PASSWORD" )
	{
		alert("Your password cannot be \"password\".");
		frm.password_1.value = "";
		frm.password_2.value = "";
		focus_field(frm.password_1);
		return false;
	}
	
	return true;
}




function validate_user_data()
{
	var frm = document.this_page;
	var firstname_label 	= "Firstname";
	var surname_label 		= "Surname";
	var address1_label		= "Address";
	var postcode_label		= "Postcode";
	var town_label			= "Town";
	var country_label		= "Country";

	
	if (!validate_not_blank(frm.firstname, firstname_label))
		return false;
	if (!validate_not_blank(frm.surname, surname_label))
		return false;
	if (!validate_not_blank(frm.address1, address1_label))
		return false;
	if (!validate_not_blank(frm.postcode, postcode_label))
		return false;
	if (!validate_not_blank(frm.town, town_label))
		return false;
	if (!validate_not_blank(frm.country, country_label))
		return false;


	
	return true;
}

function show_tip_friend(job_id)
{
 var width = 460;
 var height = 260;
 var top = 50;
 var left = 50;
 window.open("tip_friend.asp?sa_id=" + job_id, "", "toolbar=no,directories=no,menubar=no,scrollbars=yes,status=yes,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
}
 
 
 
 
 
function show_print_job(job_id)
{
 var width = 600;
 var height = 500;
 var top = 50;
 var left = 50;
 window.open("print.asp?job_id=" + job_id, "", "toolbar=no,directories=no,menubar=no,scrollbars=yes,status=yes,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
}

function remove_job(job_id)
{
	if (confirm("You are about to delete a saved job.\n" +
				  "Do you wish to delete the job?"))
	{
	// Remove Job Ad
	document.this_page.command.value = "remove_job_ad";
	document.this_page.job_id.value = job_id;
	document.this_page.submit();
	}
}
