
var countries = Array();
countries['North America'] = Array ('Canada', 'USA', 'Mexico');
countries['Europe'] = Array ('Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark',
			'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland',
			'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands',
			'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain',
			'Sweden', 'United Kingdom');
countries['Central America'] = Array ('Belize', 'Costa Rica', 'El Salvador', 'Guatemala', 'Honduras', 'Nicaragua',
			'Panama');
countries['South America'] = Array ('Argentina', 'Bolivia', 'Brazil', 'Chile', 'Columbia', 'Ecuador', 'Falkland Is.',
			'French Guiana', 'Guyana', 'Paraguay', 'Peru', 'South Georgia & South Sandwich Is.',
			'Suriname', 'Uruguay', 'Venezuela')
countries['Caribbean'] = Array ('Anguila', 'Antigua & Barbuda', 'Aruba', 'Bahamas', 'Barbadoes', 
				'British Virgin Is.', 'Cayman Is.', 'Cuba', 'Dominica', 'Grenada', 'Guadeloupe',
				'Dominican Repub.', 'Haiti', 'Jamaica', 'Martinique', 'Montserrat', 'Antilles',
				'Puerto Rico',	'Saint Barthelemy', 'Saint Kitts and Nevis', 'St Lucia', 'St Martin',
				'St Vincent & Grenadines', 'Trinidad/Tobago', 'Turks & Caicos Is.', 
				'US Virgin Is.');
countries['Oceania'] = Array ('Australia', 'New Zealand', 'Melenesia', 'Micronesia', 'Polynesia');
countries['Asia'] = Array ('Kazakhstan', 'Kyrgyzstan', 'Tajikistan', 'Turkmenistan', 'Uzbekistan',
			'China', 'Hong Kong', 'Macau', 'Mongolia', 'North Korea', 'South Korea', 'Taiwan',
			'Egypt', 'Russia',
			'Brunei', 'Cambodia', 'Indonesia', 'Laos', 'Malaysia', 'Myanmar', 'Philipines', 'Singapore', 'Thailand', 'Timor-Leste', 'Vietnam',
			'Afghanistan', 'Bangladesh', 'Bhutan', 'India', 'Iran', 'Maldives', 'Nepal', 'Pakistan', 'Sri Lanka',
			'Armenia', 'Azerbaijan', 'Bahrain', 'Cyprus', 'Gaza', 'Georgia', 'Iraq', 'Israel', 'Jordan', 'Kuwait',
			'Lebanon', 'Oman', 'Qatar', 'Saudia Arabia', 'Syria', 'Turkey', 'UAE', 'West Bank', 'Yemen');

countries['Africa'] = Array ('Algeria','Angola', 'Benin', 'Botswana', 'Burkina Faso', 'Burundi', 
			'Cameroon', 'Cape Verde', 'Central African Republic', 'Chad', 'Comoros',
			'Democratic Republic of the Congo', 'Republic of the Congo',
			'C�te d\'Ivoire', 'Djibouti', 'Egypt', 'Equatorial Guinea',
			'Eritrea', 'Ethiopia', 'Gabon', 'Gambia', 'Ghana', 'Guinea',
			'Guinea-Bissau', 'Kenya', 'Lesotho', 'Liberia', 'Libya', 'Madagascar',
			'Malawi', 'Mali', 'Mauritania', 'Mauritius', 'Morocco', 'Mozambique',
			'Namibia', 'Niger', 'Nigeria', 'R�union', 'Rwanda', 'Saint Helena',
			'S�o Tom� and Pr�ncipe', 'Senegal', 'Seychelles', 'Sierra Leone',
			'Somalia', 'South Africa', 'Sudan', 'Swaziland', 'Tanzania', 'Togo',
			'Tunisia', 'Uganda', 'Sahrawi Arab Democratic Republic', 'Zambia',
			'Zimbabwe');

function createElement (name, attribs, text) {
	var node = document.createElement(name);
	if (node) {
		for ( i = 0; i < attribs.length; i+=2) {
//alert (i + ' ' + attribs[i] + ' ' + attribs[i+1]);
			node.setAttribute (attribs[i], attribs[i+1]);
		}
		if (text.length > 0) {
			var txt = document.createTextNode (text);
			node.appendChild(txt);
		}
	}
//alert (node.value);
	return node;
}

function country_populate () {

	var region = document.getElementById('region'); //document.register.region;
	var country = document.getElementById('country'); //document.register.country;
	country.options.length = 0;
	if (region == 'Choose a region') 
	{
		alert ("not a valid region");
		return;
	}
//	alert ('country_populate');
	var name = region.options[region.selectedIndex].value;
//alert (name + ' ' + countries[name].length);
	var option;
	var txt;
	var attribs = Array();
	//attribs['value'] = 'Choose a country';
	for (var i = 0; i < countries[name].length; i++) {
		option = createElement ('option', Array ('value', countries[name][i]), countries[name][i]);
		if (option) {
			country.appendChild (option);
		}
	}
}

function update_form_action () {
	//  It is done here to hide the cgi script used to process the form from spam bots.
	document.forms[0].action = document.location.protocol + '//' +
							document.location.hostname + '/cgi-bin/website-formmail.cgi';
	//alert (document.forms[0].action);
}

function checkEmail (myForm) {
	var error= document.getElementById ("email_error");
	//dump(error.innerHTML);
	strng = myForm.email_address.value;
	if (strng.length == 0) {
		error.innerHTML = "You didn't enter an email address.";
		return false;
	}
	
	var emailFilter= /^(.+)\@([A-Za-z0-9]+)([A-Za-z0-9\.]+)$/;
	if (!(emailFilter.test(strng))) { 
		error.innerHTML = "Please enter a valid email address.";
		return false;
	}
	//test email for illegal characters
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	if (strng.match(illegalChars)) {
		error.innerHTML = "The email address contains illegal characters.";
		return false;
	}

	update_form_action();

	return true;
}


// Obfuscate the email address url
function m_sfcon (u, rest) {
	pre = "mail";
	url = pre + "to:" + u + "@bluewatersys.com";
	if (rest.length > 0) {
		url = url + '?' + rest;
	}
	document.location.href = url;
		
}

function getNavName(id, key) {
	var pos = id.indexOf(key);
	if (pos >= 0) {
		pos += key.length;
		id = id.substr(pos);
		pos = id.indexOf(' ');
		if (pos >= 0) {
			id = id.substr(0, pos);
		}
	}
	return id;
}

function preloadImages() {
	$('body.frontpage .nav-outer,body.frontpage .nav-box a').each(function() {
		var file = $(this).css('background-image');
		if (file.indexOf('url(') == 0) {
			file = file.substr(4, file.length - 5);
		}
		var img = new Image();
		img.src = file;
	});
}

$(document).ready(function () {
	/* This code augments the css that does this to show/hide the nav on the front page */
	$('body.frontpage #main-nav li').hover(function () {
		/* called when on hover */
		var id = $('.nav-outer', this).attr('id');
		id = getNavName(id, 'nav-');
		$('.nav-blocks').hide();
		$('#blocks-' + id).show();
		$('#nav-' + id).show();
		if (!$('.imgtext', this).hasClass('hasellipsis')) {
			$('.imgtext', this).ellipsis();
			$('.imgtext', this).addClass('hasellipsis');
		}
	}, function () {
		/* called when on unhover */
		var id = $('.nav-outer', this).attr('id');
		id = getNavName(id, 'nav-');
		$('.nav-outer').hide();
		$('#blocks-' + id).hide();
		$('#blocks-home').show();
	});
	$('body.frontpage .nav-blocks').hover(function () {
		var id = $(this).attr('id');
		id = getNavName(id, 'blocks-');
		$('#nav-' + id).show();
		$('#nav-' + id + ' .text').show();
		$('.nav-blocks').hide();
		$(this).show();
	}, function () {
		var id = $(this).attr('id');
		id = getNavName(id, 'blocks-');
		$('.nav-outer').hide();
		$('.nav-blocks').hide();
		$('#blocks-home').show();
	});
	$('body:not(.frontpage) .imgtext').ellipsis();
	$('body.frontpage').each(function() {
		setTimeout("preloadImages()", 200);
	});
});

