//functions to enhance usability of search form

function resetForm() {
	document.forms.searchForm.q.value = "Enter search terms";
	//called during onload at bottom of script
}
function emptyField() {
	document.forms.searchForm.q.value = "";
}
function formSubmit() {
	var myForm = document.forms.searchForm;
	var formValue = myForm.q.value;
	if(formValue == "Enter search terms") {
		alert("Please enter search terms before submitting.");
	}
	else if(formValue == " " || formValue == "") {
		alert("Please enter search terms before submitting.");
	}
	else {
		formValue += " site:www.eckstein.seattleschools.org";
		myForm.q.value = formValue;
		myForm.submit();
	}
}


//Style switcher functions from http://www.alistapart.com/articles/alternate/

function setActiveStyleSheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
		&& a.getAttribute("title")
		&& !a.disabled) return a.getAttribute("title");
	}
	return null;
}

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 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 getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
	}
	return null;
}

function findStyle() {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}

window.onunload = function() {
	var title = getActiveStyleSheet();
	createCookie("style", title, 365);
}

//Create links for style switcher
function createStyleSwitcher() {
	if(document.getElementById) {
	
		var styleSwitcher = document.getElementById('styleSwitcher');
		var styleList = document.createElement('ul');
		
		function styleObject(title, id, linkTitle, linkText) {
			this.title = title;
			this.id = id;
			this.linkTitle = linkTitle;
			this.linkText = linkText;
		}
		
		var styles = new Array();
		styles[0] = new styleObject('default', 'setDefaultStyle', 'Set style to default', 'Default Style');
		styles[1] = new styleObject('contrast', 'setContrastStyle', 'Set style to high contrast with larger text', 'High Contrast');
				
		var listItems = new Array();
		var styleLinks = new Array();
		var styleLinksText = new Array();
		var styleTitles = new Array();
		
		for(i = 0; i < styles.length; i++) {
			styleLinks[i] = document.createElement('a');
			styleLinks[i].setAttribute('id', styles[i].id);
			styleLinks[i].setAttribute('href', '#');
			styleLinks[i].setAttribute('title', styles[i].linkTitle);
			styleLinksText[i] = document.createTextNode(styles[i].linkText);
			styleLinks[i].appendChild(styleLinksText[i]);
			listItems[i] = document.createElement('li');
			listItems[i].appendChild(styleLinks[i]);
			styleList.appendChild(listItems[i]);
		}
		styleSwitcher.appendChild(styleList);
		
		/* for some reason this event handler won't work in the loop.
		I get an error saying that styles[i].title has no properties
		each event handler must be added explicitly =( */
		styleLinks[0].onclick = function() {
			setActiveStyleSheet(styles[0].title);
			return false;
		}
		styleLinks[1].onclick = function() {
			setActiveStyleSheet(styles[1].title);
			return false;
		}
	}
}
onload = function() {
	resetForm();
	findStyle();
	createStyleSwitcher();
}