/*Code by Simon Willison, published at http://www.sitepoint.com/article/simple-tricks-usable-forms
*/

addEvent(window, 'load', function() {
	if(document.getElementById(firstid)) {
		initial = document.getElementById(firstid);
		initial.focus();
		initial.style.borderTop='3px solid ' + focuscolor;
		initial.style.borderLeft='3px solid ' + focuscolor;
		initial.style.borderBottom='2px solid white';
		initial.style.borderRight='2px solid white';
	}
});

addEvent(window, 'load', function() {
	var input, textarea;
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; (input = inputs[i]); i++) {
		mytype = input.type;
		if(mytype !== "submit") {
			addEvent(input, 'focus', oninputfocus);
			addEvent(input, 'blur', oninputblur);	
		}
	}
	var textareas = document.getElementsByTagName('textarea');
	for (var i = 0; (textarea = textareas[i]); i++) {
		addEvent(textarea, 'focus', oninputfocus);
		addEvent(textarea, 'blur', oninputblur);
	}
});

function oninputfocus(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* Style up that focused field */
 source.style.borderTop='3px solid ' + focuscolor;
 source.style.borderLeft='3px solid ' + focuscolor;
 source.style.borderBottom='2px solid white';
 source.style.borderRight='2px solid white';
}

function oninputblur(e) {
/* Cookie-cutter code to find the source of the event */
	if (typeof e == 'undefined') {
		var e = window.event;
	}
	var source;
	if (typeof e.target != 'undefined') {
		source = e.target;
	} else if (typeof e.srcElement !== 'undefined') {
		source = e.srcElement;
	} else {
	return;
}
 /* Style down that unfocused field */
 source.style.borderTop='2px solid #848484';
 source.style.borderLeft='2px solid #848484';
 source.style.borderBottom='2px solid white';
 source.style.borderRight='2px solid white';
} 

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
	return r;
	} else {
		return false;
	}
}
