/**
* Onload event written by Simon Willison (http://simon.incutio.com)
* Sets a function to be executed when the page loads
* @param    string    func    The function name to execute
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//From Sitepoint.com (http://www.sitepoint.com/article/standards-compliant-world)
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
addLoadEvent(externalLinks);

//From http://www.htmldog.com/articles/suckerfish/dropdowns/
//Suckerfish dropdown
function sfHover() {
	var n = document.getElementById("nav");
	if (n) {
		var sfEls = n.getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
addLoadEvent(sfHover);


/**
* Selects the checkboxes in a form
* @param    string   selectAllBodId  The id of the checkbox that is used to select the other ones
* @param    string   formId          The id of the form that the checkboxes are in
* @param    string   boxName         The name of the checkboxes to select
*/
function selectAllCheckboxes(selectAllBoxId, formId, boxName, callBack) {
	var selectAll = document.getElementById(selectAllBoxId);
	var form = document.getElementById(formId);
	for (var i = 0; i < form.elements.length; i ++) {
		if ((form.elements[i].type == "checkbox") && (form.elements[i].name == boxName)) {
			form.elements[i].checked = selectAll.checked
		}
	}
	if (callBack) callBack.apply(this);
}

/**
* Submits the specified form and sets a hidden form field with the action type for the form
* @param    string   type     The action type
* @param    string   formId   The ID of the form to submit
*/
function submitForm(type, formId) {
	var form = document.getElementById(formId);
	if (form) {
		if (type == 'delete') {
			if (!confirm('Are you sure that you want to delete the selected items?')) return false;
		}
		//Setup the hidden form field
		var hidden = document.createElement('input');
		hidden.setAttribute('type','hidden');
		hidden.setAttribute('name','ncFormAction');
		hidden.setAttribute('value',type);
		//Add the hidden form field
		form.appendChild(hidden);
		//submit the form
		form.submit();
	}
	return false;
}

/**
* Changes the number of results to show per page
* @param   object    select    The form select object
* @param   string    url       The url to add the new number of results to
*/
function changeDisplayNumber(select, url) {
	url = url + '/' + select.options[select.selectedIndex].value;
	window.location.href = url;
}

//Sets the interval between each fade
//var fadeInterval = 600;
////Set the colors for the fade
////var ylwFadeColors = new Array('ff','ee','dd','cc','bb','aa','99');
//var ylwFadeColors = new Array('bb','aa','99','66','33');
//function yellowFade(colorKey, nodeId) {
//	if (colorKey >= 0) {
//		document.getElementById(nodeId).style.backgroundColor = '#ffff' + ylwFadeColors[colorKey];
//		//If on the last color, set the background color to transparent
//		if (colorKey == 0) {
//			//document.getElementById(nodeId).style.backgroundColor = 'transparent';
//		}
//		colorKey --;
//		setTimeout("yellowFade(" + colorKey + ", '" + nodeId + "')", fadeInterval);
//	}
//}

function msgFade() {
	if (document.getElementById('htmlMessage')) {
		fade('htmlMessage', 30, 3000, '#ffff66', '#ffffbb');
		//yellowFade(ylwFadeColors.length - 1, 'htmlMessage');
	}
}
//Adapted from the Fade Anything Technique http://www.axentric.com/posts/default/7
function makeHex(r,g,b) {
	r = r.toString(16); if (r.length == 1) r = '0' + r;
	g = g.toString(16); if (g.length == 1) g = '0' + g;
	b = b.toString(16); if (b.length == 1) b = '0' + b;
	return "#" + r + g + b;
}
fade = function(e, fps, duration, from, to) {
	//Set the default frames per second
	if (!fps) fps = 30;
	//Set the default duration in milliseconds
	if (!duration) duration = 3000;
	//Set the default from hex
	if (!from) from = '#FFFF66';
	//Set the default to (final) color
	if (!to) to = '#FFFFFF';

	//Calculate the number of frames
	var frames = Math.round(fps * (duration / 1000));

	var interval = duration / frames;
	var delay = interval;
	var frame = 0;

	if (from.length < 7) from += from.substr(1,3);
	if (to.length < 7) to += to.substr(1,3);

	var rf = parseInt(from.substr(1,2),16);
	var gf = parseInt(from.substr(3,2),16);
	var bf = parseInt(from.substr(5,2),16);
	var rt = parseInt(to.substr(1,2),16);
	var gt = parseInt(to.substr(3,2),16);
	var bt = parseInt(to.substr(5,2),16);

	var r,g,b,h;

	var node;
	if (typeof(e) == 'string') {
		this.node = document.getElementById(e);
	}
	else {
		this.node = e;
	}
	while (frame < frames)
	{
		r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
		g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
		b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
		h = makeHex(r,g,b);

		setTimeout("this.setBgColor('" +  h + "')", delay);

		frame++;
		delay = interval * frame;
	}
	setTimeout("setBgColor('" +  h + "')", delay);

	this.setBgColor = function(color) {
		this.node.style.backgroundColor = color;
	}

}

addLoadEvent(msgFade);

//Highlighting form fields
function setupHighlightTextInputs() {
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i ++) {
		var type = inputs[i].type
		if (type == 'text' || type == 'file' || type == 'radio' || type == 'checkbox' || type == 'password') {
			inputs[i].onfocus = function() {
				highlightFormField(this);
			}
			inputs[i].onblur = function() {
				removeFormFieldHighlight(this);
			}
		}
	}

	var textarea = document.getElementsByTagName('textarea');
	for (var i = 0; i < textarea.length; i ++) {
		textarea[i].onfocus = function() {
			highlightFormField(this);
		}
		textarea[i].onblur = function() {
			removeFormFieldHighlight(this);
		}
	}
}

function highlightFormField(input) {
	input.style.backgroundColor = '#ffffbb';
}
function removeFormFieldHighlight(input) {
	input.style.backgroundColor = '';
}
addLoadEvent(setupHighlightTextInputs);

//Setting the focus on a form field and highlighting it
function formFocus(id) {
	var node = document.getElementById(id);
	if (node) {
		node.focus();
		highlightFormField(node);
	}
}

//Showing and hiding elements
function setElementDisplay(id, display) {
	var e = document.getElementById(id);
	if (e) {
		e.style.display = display;
	}
}
