//ACT (ACTIONFARM) 29 Sept 2008
//JAVASCRIPT VERSION 1.0
//NIGEL'S LIB
//get element by ID shortcut
function $(theBtnId) {
	return document.getElementById(theBtnId);
}
//ajax request function
var xmlhttp = null;
function createXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
		if (typeof xmlhttp.overrideMimeType != 'undefined') {
			xmlhttp.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('Perhaps your browser does not support XMLHttpRequests?');
	}
}
//ajax send email address function
function doAjaxPostEmail(theEmailAddress,beenSubmitted) {
	createXMLHttpRequest();
	var url = "ajaxmail.php?timestamp=" + new Date().getTime();
	var queryString = "email=" + theEmailAddress + "&BeenSubmitted=" + beenSubmitted;
	xmlhttp.open("POST", url, true);
	xmlhttp.onreadystatechange = handleStateChange;
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlhttp.send(queryString);
}
//END OF NIGEL'S LIB
//----
//ajax state change function for email back form
function handleStateChange() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			$('mailbackStatus').innerHTML = xmlhttp.responseText;
			$('mailbackBtn').form.email.value = '';
		} else {
			$('mailbackStatus').innerHTML = 'ERROR!<br />Please try again.';
		}
	} else {
		$('mailbackStatus').innerHTML = 'Please wait<span style="text-decoration:blink;">...</span>';
	}
	$('mailbackBtn').disabled = "";
}
//reset menu buttons width
function resetMenu() {
	$('menuWindow').innerHTML = $('menuWindow').innerHTML; // this is the bit that does the reset
	// reassign menu functions - that were wiped out by the above reset
	menuListItems = $('menuWindow').getElementsByTagName('li');
	for (var item=0; item < menuListItems.length; item++) {
		if(menuListItems[item].className == 'hasChild') {
			menuListItems[item].getElementsByTagName('a').item(0).onclick = function() {
				this.parentNode.getElementsByTagName('ul').item(0).style.display = (this.parentNode.getElementsByTagName('ul').item(0).style.display == '') ? 'block' : '';
				resetMenu();
				return false;
			}
		}
	}
}

//save menu state function
function saveMenuState() {
	menuListItems = $('menuWindow').getElementsByTagName('li');
	menustates = new Array(); 
	for (var item=0; item < menuListItems.length; item++)
	{
		if (menuListItems[item].className == 'hasChild')
		{
			//alert(menuListItems[item].getElementsByTagName('ul').item(0).style.display);
			thismenustate = (menuListItems[item].getElementsByTagName('ul').item(0).style.display == 'block') ? 'block' : '';
			//add item's state to an array
			menustates.push(thismenustate);
			
		}
	}
	// join the menustates together to make a string
	// and
	// write menu state string to a cookie
	document.cookie = "menuState="+ menustates.join() + ";;path=/";
}

// restore cookies stuff
//get the right cookie function
function cookieVal(cookieName) {
	thisCookie = document.cookie.split("; ");
	for (i=0; i < thisCookie.length; i++)
	{
		if (cookieName == thisCookie[i].split("=")[0])
		{
			return thisCookie[i].split("=")[1];
		}
	}
	return 0;
}

//restore menu state function
function restoreMenuState() {
	menuListItems = $('menuWindow').getElementsByTagName('li');
	menustates = cookieVal("menuState");
	menustatesArr = menustates.split(",");
	counter = 0;
	for (var item=0; item < menuListItems.length; item++)
	{
		if (menuListItems[item].className == 'hasChild')
		{
			menuListItems[item].getElementsByTagName('ul').item(0).style.display = menustatesArr[counter];
			counter++;
		}
	}
}


//window onload functions
window.onload = function() {
	//if browser does not support the dom then abort
	if (!document.getElementById) return false;
	//IE browser quotes fix
	if (window.ActiveXObject) { //Are we on windows Explorer
		//quote tags
		quoteTags = document.getElementsByTagName('q');
		for (i=0; i<quoteTags.length; i++) {
			quoteTags[i].innerHTML = "&#8220;" + quoteTags[i].innerHTML + "&#8221;";
		}
	}
	//email me back form
	if ($('mailbackBtn')) {
		$('mailbackBtn').form.onsubmit = function() {
			if ($('mailbackBtn').form.email.value != '') {
				$('mailbackBtn').disabled = "disabled";
				doAjaxPostEmail($('mailbackBtn').form.email.value,1);
			} else {
				$('mailbackStatus').innerHTML = 'Please enter your e-mail address if you would like<br />us to contact you';
			}
			return false;
		}
	}
	//search form
	if ($('searchBtn')) {
		$('searchBtn').form.onsubmit = function() {
			return false;
		}
	}
	//menu toggles
	if ($('menuWindow')) {
		menuListItems = $('menuWindow').getElementsByTagName('li');
		for (var item=0; item < menuListItems.length; item++) {
			if(menuListItems[item].className == 'hasChild') {
				menuListItems[item].getElementsByTagName('ul').item(0).style.display = ''; //set the display state for javascript's benifit - avoids double click problem
				menuListItems[item].getElementsByTagName('a').item(0).onclick = function() {
					this.parentNode.getElementsByTagName('ul').item(0).style.display = (this.parentNode.getElementsByTagName('ul').item(0).style.display == '') ? 'block' : '';
					resetMenu();
					return false;
				}
			}
		}
	}
	// restore the menu state
	restoreMenuState();
}
//window close functions
window.onunload = function() {
	saveMenuState();
}
//inport close menu css file - sets menu state if javascript is enabled
document.write('<link href="http://www.actionfarm.co.uk/menu.css" rel="stylesheet" title="Default" type="text/css" media="screen" />');