// Password strength meter v2.0
// Matthew R. Miller - 2007
// www.codeandcoffee.com
// Based off of code from:
//  http://www.intelligent-web.co.uk
//  http://www.geekwisdom.com/dyn/passwdmeter

/*
	Password Strength Algorithm:
	
	Password Length:
		5 Points: Less than 4 characters
		10 Points: 5 to 7 characters
		25 Points: 8 or more
		
	Letters:
		0 Points: No letters
		10 Points: Letters are all lower case
		20 Points: Letters are upper case and lower case

	Numbers:
		0 Points: No numbers
		10 Points: 1 number
		20 Points: 3 or more numbers
		
	Characters:
		0 Points: No characters
		10 Points: 1 character
		25 Points: More than 1 character

	Bonus:
		2 Points: Letters and numbers
		3 Points: Letters, numbers, and characters
		5 Points: Mixed case letters, numbers, and characters
		
	Password Text Range:
	
		>= 90: Very Secure
		>= 80: Secure
		>= 70: Very Strong
		>= 60: Strong
		>= 50: Average
		>= 25: Weak
		>= 0: Very Weak
		
*/


// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~"

// Check password
function checkPassword(strPassword)
{
	// Reset combination count
	var nScore = 0;
	
	// Password length
	// -- Less than 4 characters
	if (strPassword.length < 5)
	{
		nScore += 5;
	}
	// -- 5 to 7 characters
	else if (strPassword.length > 4 && strPassword.length < 8)
	{
		nScore += 10;
	}
	// -- 8 or more
	else if (strPassword.length > 7)
	{
		nScore += 25;
	}

	// Letters
	var nUpperCount = countContain(strPassword, m_strUpperCase);
	var nLowerCount = countContain(strPassword, m_strLowerCase);
	var nLowerUpperCount = nUpperCount + nLowerCount;
	// -- Letters are all lower case
	if (nUpperCount == 0 && nLowerCount != 0) 
	{ 
		nScore += 10; 
	}
	// -- Letters are upper case and lower case
	else if (nUpperCount != 0 && nLowerCount != 0) 
	{ 
		nScore += 20; 
	}
	
	// Numbers
	var nNumberCount = countContain(strPassword, m_strNumber);
	// -- 1 number
	if (nNumberCount == 1)
	{
		nScore += 10;
	}
	// -- 3 or more numbers
	if (nNumberCount >= 3)
	{
		nScore += 20;
	}
	
	// Characters
	var nCharacterCount = countContain(strPassword, m_strCharacters);
	// -- 1 character
	if (nCharacterCount == 1)
	{
		nScore += 10;
	}	
	// -- More than 1 character
	if (nCharacterCount > 1)
	{
		nScore += 25;
	}
	
	// Bonus
	// -- Letters and numbers
	if (nNumberCount != 0 && nLowerUpperCount != 0)
	{
		nScore += 2;
	}
	// -- Letters, numbers, and characters
	if (nNumberCount != 0 && nLowerUpperCount != 0 && nCharacterCount != 0)
	{
		nScore += 3;
	}
	// -- Mixed case letters, numbers, and characters
	if (nNumberCount != 0 && nUpperCount != 0 && nLowerCount != 0 && nCharacterCount != 0)
	{
		nScore += 5;
	}
	
	
	return nScore;
}
 
// Runs password through check and then updates GUI 
function runPassword(strPassword, strFieldID) 
{

	// Check password
	var nScore = checkPassword(strPassword);
	
	 // Get controls
    	var ctlBar = document.getElementById(strFieldID + "_bar"); 
    	var ctlText = document.getElementById(strFieldID + "_text");
    	if (!ctlBar || !ctlText)
    		return;
    	
    	// Set new width
    	ctlBar.style.width = nScore + "%";

 	// Color and text
	// -- Very Secure
 	if (nScore >= 90)
 	{
 		var strText = "Very Secure";
 		var strColor = "#020EBF";
 	}
 	// -- Secure
 	else if (nScore >= 80)
 	{
 		var strText = "Secure";
 		var strColor = "#01536C";
	}
	// -- Very Strong
 	else if (nScore >= 70)
 	{
 		var strText = "Very Strong";
 		var strColor = "#018533";
	}
	// -- Strong
 	else if (nScore >= 60)
 	{
 		var strText = "Strong";
 		var strColor = "#449101";
	}
	// -- Average
 	else if (nScore >= 50)
 	{
 		var strText = "Average";
 		var strColor = "#8A9600";
	}
	// -- Weak
 	else if (nScore >= 25)
 	{
 		var strText = "Weak";
 		var strColor = "#B57001";
	}
	// -- Very Weak
 	else
 	{
 		var strText = "Very Weak";
 		var strColor = "#EE0B00";
	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}
 
// Checks a string for a list of characters
function countContain(strPassword, strCheck)
{ 
	// Declare variables
	var nCount = 0;
	
	for (i = 0; i < strPassword.length; i++) 
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) 
		{ 
	        	nCount++;
		} 
	} 
 
	return nCount; 
} 
 
 
 
/*******/

	function confirm_submit() {
		var message = "Are you sure you want to submit your application?";
		if ( confirm( message ) ) {
			return true;
		} else {
			return false;
		}	
	}

	function validate_studio_details() {
	
		var errors = 0;
		var error_container = document.getElementById('studio_error_container');
		error_container.innerHTML = '';
		var error_message = '<div class="message_negative"><p>Some of the information appears to be missing:</p><ul>';
		var section_completed = document.getElementById('studio_details_complete');
		var studio_title = document.getElementById('studio_title');
		var address_1 = document.getElementById('studio_address_1');
		var town = document.getElementById('studio_town');
		var post_code = document.getElementById('studio_post_code');
		var email = document.getElementById('studio_email');
		var days_open = 0;
		var open_fri = document.getElementById('open_fri');
		var open_sat = document.getElementById('open_sat');
		var open_sun = document.getElementById('open_sun');
		var open_mon = document.getElementById('open_mon');
		if ( open_fri.checked ) days_open++;
		if ( open_sat.checked ) days_open++;
		if ( open_sun.checked ) days_open++;
		if ( open_mon.checked ) days_open++;
		
		
		if ( studio_title.value == '' ) {
			error_message += '<li><a href="#studio_title">You have not entered a studio title</a></li>';
			errors = 1;
		}
		
		if ( address_1.value == '' ) {
			error_message += '<li><a href="#studio_address_1">You have not entered the first line of your studio address</a></li>';
			errors = 1;
		}
		
		if ( town.value == '' ) {
			error_message += '<li><a href="#studio_town">You have not entered the town of your studio</a></li>';
			errors = 1;
		}
		
		if ( post_code.value == '' ) {
			error_message += '<li><a href="#studio_post_code">You have not entered the post code of your studio</a></li>';
			errors = 1;
		}
		
		if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
			error_message += '<li><a href="#studio_email">The email address you entered does not appear to be correct</a></li>';
			errors = 1;
		}
		
		if ( days_open < 2 ) {
			error_message += '<li><a href="#days_open">You have to be open for at least 2 days</a></li>';
			errors = 1;
		}
		
		
		
		if ( errors == 1 ) {
			error_message += '</ul></div>';
			error_container.innerHTML = error_message;
			return false;
		} else {
			section_completed.value = 'yes';
			return true;
		}
	
	}
	
	function validate_work_details() {
	
		var errors = 0;
		var error_container = document.getElementById('work_error_container');
		error_container.innerHTML = '';
		var error_message = '<div class="message_negative"><p>Some of the information appears to be missing:</p><ul>';
		var section_completed = document.getElementById('work_details_complete');
		var work_description = document.getElementById('work_description');
		var work_categories = document.getElementsByName('work_categories[]');
		
		if ( work_description.value == '' ) {
			error_message += '<li><a href="#work_description">You have not entered a description of your work</a></li>';
			errors = 1;
		}
		
		
		var num_categories = 0;
		for ( var i = 0; i < work_categories.length; i++ ) {
			if ( work_categories[i].checked ) num_categories++;
		}
		
		if ( work_categories > 2 ) {
			error_message += '<li><a href="#categories_chosen">You can only choose 2 categories</a></li>';
			errors = 1;
		}

		if ( work_categories == 0 ) {
			error_message += '<li><a href="#categories_chosen">You have not chosen any categories</a></li>';
			errors = 1;
		}

		if ( errors == 1 ) {
			error_message += '</ul></div>';
			error_container.innerHTML = error_message;
			return false;
		} else {
			section_completed.value = 'yes';
			return true;
		}
		
	
	}
	
	function validate_listing_details() {
		
		var errors = 0;
		var error_container = document.getElementById('listing_error_container');
		error_container.innerHTML = '';
		var error_message = '<div class="message_negative"><p>Some of the information appears to be missing:</p><ul>';
		var section_completed = document.getElementById('listing_details_complete');
		var advertising = document.getElementById('advertising');
		var advertising_size_full = document.getElementById('advertising_size_full');
		var advertising_size_half = document.getElementById('advertising_size_half');
		var advertising_size_quarter = document.getElementById('advertising_size_quarter');
		var sizes_checked = 0;
		
		if ( advertising.checked == true ) {
			if ( advertising_size_full.checked ) sizes_checked++;
			if ( advertising_size_half.checked ) sizes_checked++;
			if ( advertising_size_quarter.checked ) sizes_checked++;
			
			if ( sizes_checked == 0 ) {
				error_message += '<li><a href="advertising">You have not chosen advert sizes that you are interested in</a></li>';
				errors = 1;
			}
		}
		
		if ( errors == 1 ) {
			error_message += '</ul></div>';
			error_container.innerHTML = error_message;
			return false;
		} else {
			section_completed.value = 'yes';
			return true;
		}
	
	}
	
	function validate_billing_details() {

		var errors = 0;
		var error_container = document.getElementById('billing_error_container');
		error_container.innerHTML = '';
		var error_message = '<div class="message_negative"><p>Some of the information appears to be missing:</p><ul>';
		var section_completed = document.getElementById('billing_details_complete');
	
		var firstname = document.getElementById('firstname');
		var lastname = document.getElementById('lastname');
		var address_1 = document.getElementById('address_1');
		var post_code = document.getElementById('post_code');
		var country = document.getElementById('country');
		var tel = document.getElementById('tel');
		var email = document.getElementById('email');
		var password = document.getElementById('password');
		var password_confirm = document.getElementById('password_confirm');
		
		if ( firstname.value == '' ) {
			error_message += '<li><a href="#firstname">You have not entered your first name</a></li>';
			errors = 1;
		}
		
		if ( lastname.value == '' ) {
			error_message += '<li><a href="#lastname">You have not entered your last name</a></li>';
			errors = 1;
		}
		
		if ( address_1.value == '' ) {
			error_message += '<li><a href="#address_1">You have not entered the first line of your address</a></li>';
			errors = 1;
		}
		
		if ( post_code.value == '' ) {
			error_message += '<li><a href="#post_code">You have not entered your post code</a></li>';
			errors = 1;
		}
		
		if ( country.value == '' ) {
			error_message += '<li><a href="#country">You have not selected your country</a></li>';
			errors = 1;
		}
		
		if ( tel.value == '' ) {
			error_message += '<li><a href="#tel">You have not entered your telephone number</a></li>';
			errors = 1;
		}
		
		if ( email.value == '' ) {
			error_message += '<li><a href="#email">You have not entered your email address</a></li>';
			errors = 1;
		}
		
		if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
			error_message += '<li><a href="#email">The email address you entered doesn\'t appear to be correct</a></li>';
			errors = 1;
		}
		
		if ( password.value != password_confirm.value ) {
			error_message += '<li><a href="#password">The 2 passwords don\'t match</a></li>';
			errors = 1;
		}
	
		if ( errors == 1 ) {
			error_message += '</ul></div>';
			error_container.innerHTML = error_message;
			return false;
		} else {
			section_completed.value = 'yes';
			return true;
		}
	
	}

	function validate_preview() {

		var errors = 0;
		var error_container = document.getElementById('preview_error_container');
		error_container.innerHTML = '';
		var error_message = '<div class="message_negative"><ul>';
		
		var agree = document.getElementById('agree');
		if ( agree.checked == true ) {
			return true;		
		} else {
			error_message += '<li>You have not agreed to our Terms and Conditions</li>';
			errors = 1;
		}
		
		if ( errors == 1 ) {
			error_message += '</ul></div>';
			error_container.innerHTML = error_message;
			return false;
		} else {
			return true;
		}
	
	}

	function show_short_navigation() {
		var menu_item = document.getElementById('listing_type_short');
		var long_menu = document.getElementById('full_nav');
		var short_menu = document.getElementById('short_nav');
		
		if ( menu_item.checked ) {
			short_menu.style.display = '';
			long_menu.style.display = 'none';
		} else {
			short_menu.style.display = 'none';
			long_menu.style.display = '';
		}
	
	}

/*******/

//	if a page has been changed
	function changed_page() {
		document.getElementById('changed').value = 'unsaved';
	}
	
	function check_changed(dest) {
		if ( document.getElementById('changed').value == 'unsaved' ) {
			var message = "You have made changes that you haven't saved.  If you continue you will loose them.\n\nDo you really want to go to the  " + dest + " page now?";
			if ( confirm( message ) ) {
				return true;
			} else {
				return false;	
			}
		} else {
			return true;
		}
	}


//	show a section
	function show_section(section) {
		document.getElementById('intro').style.display = 'none';
		document.getElementById('studio_details').style.display = 'none';
		document.getElementById('work_details').style.display = 'none';
		document.getElementById('listing_details').style.display = 'none';
		document.getElementById('billing_details').style.display = 'none';
		document.getElementById(section + '_details').style.display = '';
	}

//	calculate list cost
	function calculate_cost() {

		var cost_display = document.getElementById('display_cost');
		cost_display.innerHTML = '';
		var cost_field = document.getElementById('cost');
		
		var cost_short = document.getElementById('cost_field_short').value;
		var cost_artist = document.getElementById('cost_field_artist').value;
		var cost_sharing = document.getElementById('cost_field_sharing').value;
		var cost_shop = document.getElementById('cost_field_shop').value;
		var cost_gallery = document.getElementById('cost_field_gallery').value;
		var cost_advert_full = document.getElementById('cost_field_advert_full').value;
		var cost_advert_half = document.getElementById('cost_field_advert_half').value;
		var cost_advert_quarter = document.getElementById('cost_field_advert_quarter').value;

		var advertising = document.getElementById('advertising');
		
		var option_short = document.getElementById('listing_type_short');
		var option_artist = document.getElementById('listing_type_artist');
		var option_sharing = document.getElementById('listing_type_sharing');
		var option_shop = document.getElementById('listing_type_shop');
		var option_gallery = document.getElementById('listing_type_gallery');
		var option_advert_full = document.getElementById('advertising_size_full');
		var option_advert_half = document.getElementById('advertising_size_half');
		var option_advert_quarter = document.getElementById('advertising_size_quarter');
		
		var current_cost = 0;
		if ( option_short.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_short);
		if ( option_artist.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_artist);
		if ( option_sharing.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_sharing);
		if ( option_shop.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_shop);
		if ( option_gallery.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_gallery);
		if ( advertising.checked ) {
			if ( option_advert_full.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_advert_full);
			if ( option_advert_half.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_advert_half);
			if ( option_advert_quarter.checked ) current_cost = parseFloat(current_cost) + parseFloat(cost_advert_quarter);
		}

		if ( current_cost > 0 ) {
			var cost_message = 'The cost for this listing will be &pound;' + current_cost;
			cost_display.innerHTML = cost_message;
			cost_field.value = current_cost;
		}
	
	}

//	count the days open
		function count_days_open() {
			
			var days_open = document.getElementById('days_open');
			var open_fri = document.getElementById('open_fri');
			var open_sat = document.getElementById('open_sat');
			var open_sun = document.getElementById('open_sun');
			var open_mon = document.getElementById('open_mon');
			var num_days = 0;
			
			days_open.innerHTML = '';
			
			if ( open_fri.checked ) num_days++;
			if ( open_sat.checked ) num_days++;
			if ( open_sun.checked ) num_days++;
			if ( open_mon.checked ) num_days++;
			
			if ( num_days < 2 ) {
				days_open.innerHTML = '<div class="message_negative">You must select<br />at least 2 days</div>';
			}
		
		}

		function count_categories() {
		
			var categories = document.getElementsByName("work_categories[]");
			var categories_chosen = document.getElementById('categories_chosen');
			var category_num = 0;
			categories_chosen.innerHTML = '';
			for ( var x = 0; x < categories.length; x++ ) {
				if ( categories[x].checked ) category_num++;
			}
			if ( category_num > 2 ) {
				categories_chosen.innerHTML = '<div class="message_negative">You can only select<br />2 categories</div>';
			}
			
		}

		function show_help(help_el,display_below) {
			var help_box = document.getElementById(help_el);
			if ( help_box.style.display == "" ) {
				help_box.style.display = "none";
				return false;
			}
			close_all_help();
			var x = display_below.offsetLeft;
			var y = display_below.offsetTop;
			var parent = display_below;
			while ( parent.offsetParent ) {
				parent = parent.offsetParent;
				x += parent.offsetLeft;
				y += parent.offsetTop;
			}
			
			x = x + 30;
			y = y - 30;
			
			help_box.style.display = "";
			help_box.style.position = "absolute";
			help_box.style.left = x + "px";
			help_box.style.top = y + "px";
			help_box.style.visibility = "visible";
			help_box.style.zIndex = 10000;
			
		}
		
		function close_help(help_el) {
			var help_box = document.getElementById(help_el);
			help_box.style.display = "none";
		}
		
		function close_all_help() {
			var x = document.getElementsByName("help");
			for ( var i = 0; i < x.length; i++ ) {
				x[i].style.display = "none";	
			}
		}
		
function validate_login() {
	
	var email = document.getElementById('e');
	var password = document.getElementById('p');
	var error_container = document.getElementById('login_error_container');
	error_container.innerHTML = '';
	var errors = 0;
	var error_message = '<div class="message_negative"><ul>';
	
	if ( email.value == '' ) {
		error_message += '<li>You have not entered your email address</li>';
		errors = 1;
	}
	
	if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
		error_message += '<li>The email address you entered does not appear to be correct</li>';
		errors = 1;
	}
	
	if ( password.value == '' ) {
		error_message += '<li>You have not entered your password</li>';
		errors = 1;
	}
	
	if ( errors == 1 ) {
		error_message += '</ul></div>';
		error_container.innerHTML = error_message;
		return false;
	} else {
		return true;	
	}

}

function validate_signup() {

	var error_container = document.getElementById('signup_error_container');
	error_container.innerHTML = '';
	var errors = 0;
	var error_message = '<div class="message_negative"><ul>';
	
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var email = document.getElementById('email');
	var password = document.getElementById('password');
	var password_confirm = document.getElementById('password_confirm');
	
	if ( firstname.value == '' ) {
		error_message += '<li>You have not entered your first name</li>';
		errors = 1;
	}
	
	if ( lastname.value == '' ) {
		error_message += '<li>You have not entered your last name</li>';
		errors = 1;		
	}
	
	if ( email.value == '' ) {
		error_message += '<li>You have not entered your email address</li>';
		errors = 1;
	}
	
	if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
		error_message += '<li>The email address you entered does not appear to be correct</li>';
		errors = 1;
	}
	
	if ( password.value == '' ) {
		error_message += '<li>You have not entered a password</li>';
		errors = 1;
	}
	
	if ( password.value != '' && password_confirm.value == '' ) {
		error_message += '<li>You have not re-entered the password</li>';
		errors = 1;
	}
	
	if ( ( password.value != '' && password_confirm.value != '' ) && ( password.value != password_confirm.value ) ) {
		error_message += '<li>The two passwords you entered don\'t match</li>';
		errors = 1;
	}
	
	if ( errors == 1 ) {
		error_message += '</ul></div>';
		error_container.innerHTML = error_message;
		return false;
	} else {
		return true;
	}

}

function validate_reminder() {

	var error_container = document.getElementById('reminder_error_container');
	error_container.innerHTML = '';
	var errors = 0;
	var error_message = '<div class="message_negative"><ul>';

	var email = document.getElementById('reminder_email');

	if ( email.value == '' ) {
		error_message += '<li>You have not entered your email address</li>';
		errors = 1;
	}
	
	if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
		error_message += '<li>The email address you entered does not appear to be correct</li>';
		errors = 1;
	}

	if ( errors == 1 ) {
		error_message += '</ul></div>';
		error_container.innerHTML = error_message;
		return false;
	} else {
		return true;
	}

}

function validate_short_details() {

	var error_container = document.getElementById('short_error_container');
	error_container.innerHTML = '';
	var errors = 0;
	var error_message = '<div class="message_negative"><ul>';
	var section_completed = document.getElementById('short_details_complete');

	var short_title = document.getElementById('short_title');
	
	if ( short_title.value == '' ) {
		error_message += '<li><a href="#short_title">You have not entered a title</a></li>';
		errors = 1;
	}
	
	if ( errors == 1 ) {
		error_message += '</ul></div>';
		error_container.innerHTML = error_message;
		return false;
	} else {
		section_completed.value = 'yes';
		return true;
	}

}
		
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//	bookmark a page
	function bookmark_page( url, title ) {
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

//	limit number of characters in a textarea field
	function limit_characters( limit_field, limit_count_field, limit_num ) {
		var limit_field = document.getElementById(limit_field);
		var limit_count_field = document.getElementById(limit_count_field);
		if ( limit_field != undefined ) {
			if ( limit_field.value.length > limit_num ) {
				limit_field.value = limit_field.value.substring( 0, limit_num );
			} else {
				limit_count_field.innerHTML	= (limit_num - limit_field.value.length) + ' characters remaining';
			}		
		}	
	}


//	show events on the home page
		function show_events(type) {
			var type;
			var tab_summary = document.getElementById('tab_summary');
			var tab_water = document.getElementById('tab_water');
			var tab_ashore = document.getElementById('tab_ashore');
			var container_summary = document.getElementById('event_summary');
			var container_water = document.getElementById('event_water');
			var container_ashore = document.getElementById('event_ashore');
			switch ( type ) {
				case 'water' :
					tab_summary.className = 'off';
					tab_water.className = 'on';
					tab_ashore.className = 'off';
					container_summary.style.display = 'none';
					container_water.style.display = '';
					container_ashore.style.display = 'none';
					break;
				case 'ashore' :
					tab_summary.className = 'off';
					tab_water.className = 'off';
					tab_ashore.className = 'on';
					container_summary.style.display = 'none';
					container_water.style.display = 'none';
					container_ashore.style.display = '';
					break;
				default :
					tab_summary.className = 'on';
					tab_water.className = 'off';
					tab_ashore.className = 'off';
					container_summary.style.display = '';
					container_water.style.display = 'none';
					container_ashore.style.display = 'none';
					break;
					
			}
		}


//	when the search field gets the focus
	function search_focus() {
		var search = document.getElementById('q');
		if ( search.value == 'Search...' ) {
			search.value = '';
		} else {
			search.select();
		}
		search.className = 'field_search_focus';
	}
	
//	when the search field loses the focus
	function search_blur() {
		var search = document.getElementById('q');
		if ( search.value == '' ) {
			search.value = 'Search...';
		}
		search.className = 'field_search';
	}

// 	retrieve a url parameter
	function get_url_parameter( name ) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null ) {
			return "";
		} else {
			return results[1];
		}
	}

//	preload images
	function preload_images () {
		arImageSrc = new Array ();
		arImageList = new Array ();
		for (counter in arImageSrc) {
			arImageList[counter] = new Image();
			arImageList[counter].src = arImageSrc[counter];
		}
	}

//	convert date and time to unixtimestamp
	function date_to_unixtime(target_field, month_el, day_el, year_el, hour_el, minute_el) {
		var target = document.getElementById(target_field);
		var year = document.getElementById(year_el).value;
		var month = document.getElementById(month_el).value;
		var day = document.getElementById(day_el).value;
		var hour = document.getElementById(hour_el).value;
		var minute = document.getElementById(minute_el).value;
		var second = '01';
		
		var errors = 0;
		var error_message = 'The following elements appear to be missing or incorrect' + "\n";
		if ( year.length != 4 ) {
			error_message += ' - you must enter a 4 digit year' + "\n";
			errors = 1;
		}
		if ( month > 12 ) {
			error_message += ' - the month must be between 1 and 12' + "\n";
			errors = 1;
		}
		if ( day > 31 ) {
			error_message += ' - the day must be between 1 and 31' + "\n";
			errors = 1;
		}
		if ( hour > 24 ) {
			error_message += ' - the hour must be 24 hour (e.g. 13 = 1pm)' + "\n";
			errors = 1;
		}
		if ( minute > 59 ) {
			error_message += ' - the minutes must be between 0 and 59' + "\n";
			errors = 1;
		}
		if ( errors == 1 ) {
			alert( error_message );
		} else {
			var humDate = new Date(Date.UTC(year, (nozeros(month)-1), nozeros(day), nozeros(hour), nozeros(minute), nozeros(second))); 
			target.value = (humDate.getTime()/1000.0);
		}
	
	}

//	get the current page
	function get_current_page() {
		var fullpath = location.pathname;
		var patharray = fullpath.split('/');
		var folderposition = patharray.length - 1;
		var filename = patharray[folderposition];
		var filearray = filename.split('.');
		var nav = filearray[0];
		return nav;
	}

//	open external links in a new window - replaces target="_blank"
	function externalLinks () { 
		if ( ! document.getElementsByTagName ) return; 
		var anchors = document.getElementsByTagName("a"); 
		for ( var i=0; i < anchors.length; i++ ) { 
			var anchor = anchors[i]; 
			if ( anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" ) anchor.target = "_blank"; 
		} 
	} 

//	add given page to favourites (bookmark)
	function setBookmark ( url, str ) {
		if ( str == '' ) str = url;
		if ( document.all ) window.external.AddFavorite( url, str );
		else alert( 'Sorry, this function only works in Internet Explorer.\n\nPlease press CTRL and D to add a bookmark to \n"' + str + '".' );
	}

//	toggle a given elements visibility
	function toggle (el) {
		if ( document.getElementById(el) == undefined ) return false;
		if ( document.getElementById(el).style.display == 'none' ) {
			document.getElementById(el).style.display = '';
		} else {
			document.getElementById(el).style.display = 'none';
		}
	}

//	show a given element
	function show (el) {
		if ( document.getElementById(el) != undefined ) document.getElementById(el).style.display = '';
	}

//	hide a given element
	function hide (el) {
		if ( document.getElementById(el) != undefined ) document.getElementById(el).style.display = 'none';
	}

//	manage the show/hide buttons
	function showhide( source_el, target_el ) {
		var source = document.getElementById(source_el);
		var target = document.getElementById(target_el);
		toggle(target_el);
		if ( target.style.display == 'none' ) {
			source.innerHTML = 'Show &darr;';
			source.className = 'showhide';
		} else {
			source.innerHTML = 'Hide &uarr;';
			source.className = 'hideshow';
		}
	}


//	activate the correct navigation
	function activate_nav() {
		

		var nav = get_current_page();
		if ( nav == '' ) {
			nav = 'index';
		}
		if ( nav == 'search' || nav == 'email' ) {
			nav = 'blank';
		}
		var parent_page = nav;
		switch ( nav ) {
		

		
		}
		if ( document.getElementById('nav_' + parent_page) != undefined ) document.getElementById('nav_' + parent_page).className = 'active';

		var show_menu = '';
		switch ( nav ) {
		
			case 'officers' :
			case 'facilities' :
			case 'history' :
			case 'membership_application' :
			case 'rules' :
			case 'merchandise' :
				show_menu = 'the_club';
				break;
				
			case 'calendar_water' :
			case 'sailing_instructions' :
			case 'racing_results' :
			case 'regattas' :
			case 'cruising' :
			case 'fleet' :
				show_menu = 'on_the_water';
				break;
				
			case 'calendar_ashore' :
			case 'opening_hours' :
			case 'club_events' :
				show_menu = 'ashore';
				break;
		
		}
//		document.getElementById('nav_'+show_menu).className = 'selected';

		

	}

//	make a textarea grow
	function extend_textarea(el) {
		if ( document.getElementById(el) == undefined ) return false;
		var el = document.getElementById(el);
		if ( el.value.length > 150 ) {
			el.style.height = "100px";
		} else {
			el.style.height = "50px";
		}
	}

//	populate a business card
	function populate_business_card(card_id,card_type) {
		xmlHttp = initiate_ajax();
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				document.getElementById('business_card_content').innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET","business_card.php?card_id=" + card_id + "&card_type=" + card_type,true);
		xmlHttp.send(null);
	}



//	initialise an httpRequest object
	function initiate_ajax() {
		var xmlHttp;
		try {

			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {

			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		return xmlHttp;
	}


//	perform the following functions when the page loads
	window.onload = function(e) {
		externalLinks();
		activate_nav();
		initiate_ajax();
	}