window.onload = function () {
	ClassFirstOptions();
	ClearDefaultInputVals();
	AdvancedSearch();
	//ButtonsAsAnchors();
	FacilitiesIcons();
}

/*  Applies a class of 'firstOption' to the first option of a select list, providing it has a
    value of '0' and contains the word 'select'. This is done through JavaScript as the assignment
    of this class is far from mission critical; it simply provides a hook to style the first
    option differently to indicate it is unselectable. Achieving this through JS also provides
    semantic and SEO benefits, and can be a time-saver when developing pages with a magnitude of
    select lists.  */

function ClassFirstOptions() {
	var selects = document.getElementsByTagName('select'),
		firstOption;
	for (var i = 0; i < selects.length; i++) {
		firstOption = selects[i].getElementsByTagName('option')[0];
		if (
			firstOption.value == '0' &&
			firstOption.innerHTML.search(/\bselect\b/i) != -1
		) {
			firstOption.className = 'first';
			if (selects[i].value == '0') {
				selects[i].style.color = '#999'
			}
			selects[i].onchange = function () {
				if (this.value == '0') {
					this.style.color = '#999';
				}
				else {
					this.style.color = '#555';
				}
			}
		}
	}
}


function ClearDefaultInputVals() {
	var searchPostcode = document.getElementById('searchPostcode');
	if (searchPostcode.value == 'Enter Postcode') {
		searchPostcode.style.color = '#999';
	}
	searchPostcode.onfocus = function () {
		if (this.value == 'Enter Postcode') {
			this.value = '';
			this.style.color = '#555'
		}
	}
	searchPostcode.onblur = function () {
		if (this.value.replace(/^\s+|\s+$/g,'') == '') {
			this.value = 'Enter Postcode';
			this.style.color = '#999'
		}
	}
}

function AdvancedSearch() {
	var h = document.getElementById('advancedSearch'),
		ol = document.getElementById('advancedSearchCriteria'),
		cookieName = 'HG_AdvancedSearchVisible';
	if (!h || !ol) return;
	if (readCookie(cookieName)) {
		h.className  = readCookie(cookieName);
		ol.className = readCookie(cookieName);
		// set hidden variable to status so can be checked server side.
		document.getElementById('advancedStatus').value = "" + ol.className;
	}
	h.onclick = function () {
		cls = (this.className == 'hide') ? 'show' : 'hide';
		this.className = cls;
		ol.className = cls;
		createCookie(cookieName, cls);
		// set hidden variable to status so can be checked server side.
		document.getElementById('advancedStatus').value = "" + cls;
		return false;
	}
	
}

function ButtonsAsAnchors() {
	var registerNowButton = document.getElementById('registerNowButton');
	if (!registerNowButton) return false;
	registerNowButton.onclick = function () {
		window.location = '../my-grapevine/';
		return false;
	}
}

function FacilitiesIcons() {
	var directoryList = document.getElementById('directorySearchResults');
	if (!directoryList) return;
	var ul = directoryList.getElementsByTagName('ul');
	for (var i = 0; i < ul.length; i++) {
		if (ul[i].className == 'facilitiesList') {
			img = ul[i].getElementsByTagName('img');
			for (var j = 0; j < img.length; j++) {
			}
		}
	}
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else var expires = '';
	document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, '', -1);
}

//
//	Checks whether the user has selected an option from the list, focusing the field
//	and returning false if not:
//
function CheckMandatoryList(field, msg) {
	if (field.options[field.selectedIndex].value == 0) {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}	

//
// Validates the search form within secondary content include file:
//
function ValidateSearchForm(form) {
	if (!CheckMandatoryList(form.search_town, "Please select a town.")) return false;
	if (!CheckMandatoryList(form.search_business_type, "Please select a business type.")) return false;
	return true;
}

function addLoadEvent(fn) {
	var oldOnload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	} else {
		window.onload = function() {
			oldOnload();
			fn();
		}
	}
}