/*
function:	manageLinks()
author:		Miroslav Wiesner
date:		March 30, 2006

Does a bunch of stuff in handling anchor tags
1. gets ride of that nasty dotted line surrounding links in focus on windows machines.
2. allows for targeting new windows for external links.

*/

function blurLinks() {
	if((navigator.platform == "Win32") && document.getElementsByTagName) {
		var lnks = document.getElementsByTagName('a');
	    for (var i = 0; i < lnks.length; i++) {
			var lnk = lnks[i];
			lnk.onfocus = new Function("if(this.blur)this.blur()");
    	}
	}
}

function targetLinks() {
	var lnks = document.getElementsByTagName('a');
    for (var i = 0; i < lnks.length; i++) {
		var lnk = lnks[i];
		if (lnk.getAttribute("href") && lnk.getAttribute("rel") == "external") lnk.target = "_blank"; 
   	}
}

function resetForms() {
	var forms	= document.getElementsByTagName('form');
	for (var i = 0; i < forms.length; i++) {
		var form	= forms[i];
		if (form.id != "form-controls") form.reset();
	}
}

// function to handle pre DOM and DOM2 document call

function doc() {
	return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

// function to obtain the height of the client window.

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number')
		windowHeight = window.innerHeight;
	else
		windowHeight = doc().clientHeight;
	return windowHeight;
}

// function to obtain the width of the client window.

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number')
		windowWidth = window.innerWidth;
	else
		windowWidth = doc().clientWidth;
	return windowWidth;
}

// appends the category id to a redirecting URL for Zone dropdown 

function setZone(obj, drop) {
	var url;
	var form	= obj.parentNode;
	var drop	= $(drop);
	if(drop.value == "") url = form.action;
	else url = form.action + "C" + drop.value;
	window.location.href	= url;
}

// Poll stuff - document later

function pollChoices(form, className) {
	var form 	= $(form);
	var ul		= form.getElementsByTagName("ul")[0];
	Element.cleanWhitespace(ul);
	var as		= form.getElementsByTagName("a");
	var inputs	= form.getElementsByTagName("input");
	for (i = 0; i < as.length; i++) {
		var a = as[i];
		Event.observe(a, 'click', 
			function(e) {
				pollSelect(Event.element(e), ul, className)
			}, false);
	}
}

function pollSelect(a, ul, className) {
	for(i=0; i < ul.childNodes.length; i++) {
		var child = ul.childNodes[i];
		if(child.nodeName.toLowerCase() == "li") {
			var lnk = child.childNodes[0];
			lnk.className = "";
			lnk.childNodes[0].checked = false;
		}
	}
	a.className = className;
	a.childNodes[0].checked = true;
}

function pollValidate(form, div) {
	var div		= $(div);
	form		= $(form);
	var inputs	= form.getElementsByTagName("input");
	inputs	= $A(inputs);
	var input = inputs.find ( function(x) { return ( x.checked == true ); });
	if(input) { return true }
	else { Element.show($('poll-error')); Element.hide($('poll-form')); return false }
}
 
function pollReturn(form, str) {
	$(form).innerHTML = unescape(str);
	alert(unescape(str));
}

function getResults(form, div) {
	if (pollValidate(form, div) == true) {
		var form	= $(form);
		var div		= $(div);
		var url		= form.action;
		var pars	= Form.serialize(form);
		var myAjax	= new Ajax.Updater(
			div,
			url,
			{
				method: 'post',
				postBody: pars,
				onLoading: pollLoad(div),
				onFailure: pollError
			});
	}
}

function pollLoad(div) {
	div.innerHTML = window["pollLoading"];
}

function pollError(request) {
$('poll').innerHTML = "Ajax Error";
}

/*
function:	getWeather(selectBox)
author:		Miroslav Wiesner
date:		May 25, 2006

Appends the selected option VALUE to the select-box's parent form ACTION
to create a URL and opens a new window with the appropriate 10-day forecast
from weather.com. The default option value must be "-1".

*/

function getWeather(sbox) {
	var form	= sbox.parentNode;
	var val		= sbox.options[sbox.selectedIndex].value;
	var url		= form.action + val;
	if(val != "-1") window.open(url,'','', 'accuweather');
	sbox.options[0].selected = true;
}

/*
function:	swapDefault(input, string)
author:		Miroslav Wiesner
date:		May 25, 2006

Determines whether the value of the input is the provided string. If yes onFocus,
the value is blank. If the value is blank onBlur, the value becomes the string.

*/

function swapDefault(obj, str) {
	if (obj.value == str) obj.value = "";
	Event.observe(obj, 'blur', 
			function(e) {
				if (Event.element(e).value == "") obj.value = str;
			}, false);
}

// stuff for the dropdowns in the list view
function dynaSelect(selectBox, div) {
	var form	= selectBox.parentNode;
	var div		= $(div);
	Element.cleanWhitespace(div);
	var subBox	= div.childNodes[0];
	if(selectBox.options) {
		var boxVal = selectBox.options[selectBox.selectedIndex].value;
	} else 	if($('input-zone')) {
		var boxVal = $('input-zone').value;
	}
	var strName = "zone" + boxVal;
	div.innerHTML = splitZones(strName, subBox);
}

function splitZones(v, subBox) {
	var i;
	var str = "";
	x	= window[v];
	if (v == "") {
		var thestr = "<select name=\"select-subzone\" id=\"select-subzone\" class=\"selectbox select-dead\" disabled=\"disabled\" style=\"width: auto\"><option value=\"\">No Area Selected</option></select>";
	} else if (!x || x == "") {
		var thestr = "<select name=\"select-subzone\" id=\"select-subzone\" class=\"selectbox select-dead\" disabled=\"disabled\" style=\"width: auto\"><option value=\"\">No Localities</option></select>";
	} else {
		x	= x.substring(0,(x.length-1));
		var zoneArray	= x.split(",");
		for (i=0; i < zoneArray.length; i++) {
			var zone = zoneArray[i];
			var vals = zone.split("|");
			vals[3] ? selected = vals[3] : selected = '';
			str += "<option value=\"" + vals[0] + "\" " + selected + ">&bull; " + vals[1] + " (" + vals[2] + ")</option>" + "\n";
		}
		var thestr = "<select name=\"select-subzone\" id=\"select-subzone\" class=\"selectbox\" style=\"width: auto\"><option value=\"\">All Localities</option>" + str + "</select>";
	}
	return thestr;
}

// so pagination carries your dynamic stuff with it

function dynaLink(url, form) {
	if(!arguments[1])	var form = $('form-controls');
	form.value		= url;
	form.action		= url;
	form.submit();
}

function cacheMap(cat,zone,subz) {
	var form	= $('form-controls');
	var a, b, c;
	a = b = c = '';
	if(cat)		a	= cat.value;
	if(zone) 	b	= zone.value;
	if(subz) 	c	= subz.value;
	form.action += "Z" + a + b + c;
}

///

function getEvents(obj, div) {
	var form	= $(obj);
	var div		= $(div);
	var url		= form.action;
	var pars	= Form.serialize(form);
	var myAjax	= new Ajax.Updater(
		div,
		url,
		{
			method: 'get',
			parameters: pars,
			onLoading: eventLoad(div)
		});
}

function eventLoad(div) {
	div.innerHTML = window["eventsLoading"];
}

////

function editEntry(id, zone) {
	var form = $('editform');
	$('edit-entry').value = id;
	$('edit-zone').value = zone;
	form.submit();
}

////

function postSection(val) {
var form = $('form-controls');
var subcat = $('select-subcat');
$('select-zone').value = '';
$('select-subzone').value = '';
subcat.value = val;
form.action += "Z" + val;
form.submit();
}

// To dynamically set the location of the footer

function posFooter() {
	var major	= $('major-column') ? Element.getHeight('major-column') : 0;
	var minor	= Element.getHeight('minor-column');
	var content = Element.getHeight('content');
	var header	= Element.getHeight('header');
	var nav		= Element.getHeight('nav');
	if (major != 0 && content > major && content > minor) {
		$('footer').style.position = "absolute";
		$('footer').style.top = (header + nav + content + 30) + "px";
	}
}

// New Paging Model for Postacards

function paging(nextURL) {
	var newURL;
	var URL = document.location.href;
	if ( URL.match("\\?") ) {
		newURL = URL.substring( URL.indexOf( "?" ) );
		nextURL += newURL;
	}
	window.location.href = nextURL;
}