addEvent(window, 'load', initForm);

var highlight_array = new Array();

function initForm(){
	initializeFocus();
	ifInstructs();
	browserDetect();
}

// for radio and checkboxes, they have to be cleared manually, so they are added to the
// global array highlight_array so we dont have to loop through the dom every time.
function initializeFocus(){
	fields = getElementsByClassName("field");
	for(i = 0; i < fields.length; i++) {
		if(fields[i].type == 'radio' || fields[i].type == 'checkbox' || fields[i].type == 'file') {
			fields[i].onclick = function(){clearSafariRadios(); addClassName(this.parentNode.parentNode, "focused", true)};
			fields[i].onfocus = function(){clearSafariRadios(); addClassName(this.parentNode.parentNode, "focused", true)};
			highlight_array.splice(highlight_array.length,0,fields[i]);
		}
		else {
			fields[i].onfocus = function(){clearSafariRadios();addClassName(this.parentNode.parentNode, "focused", true)};
			fields[i].onblur = function(){removeClassName(this.parentNode.parentNode, "focused")};
		}
	}
}

function clearSafariRadios() {
	for(var i = 0; i < highlight_array.length; i++) {
		removeClassName(highlight_array[i].parentNode.parentNode, 'focused');
	}
}

function browserDetect(){
	var detect = navigator.userAgent.toLowerCase();
	var container = document.getElementsByTagName('body');
	if(detect.indexOf('safari') + 1){
		addClassName(container[0], 'safari', true);
	}
	if(detect.indexOf('firefox') + 1){
		addClassName(container[0], 'firefox', true);
	}
}

function ifInstructs(){
	var container = document.getElementById('public');
	if(container){
		removeClassName(container,'noI');
		var instructs = getElementsByClassName("instruct");
		if((container.offsetWidth <= 450) || (instructs == '')){
			addClassName(container,'noI',true);
		}
	}
}