function addLoadEvent ( func ) {
	var oldonload = window.onload;
	if ( typeof window.onload != 'function' ) {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
	window.onunload = function() {
		window.onload = "";
	}
}

var preloads = {
	fireOnLoad : function ( ) {
		if ( document.body.id == "adhd_page" ) {
			adhd_page.wireContent();
		}
		if ( document.body.id == "had_page" ) {
			had_page.wireContent();
		}
		if ( document.body.id == "contact" ) {
			contact_page.wireContent();
		}
		if ( document.body.id == "hadresults" || document.body.id == "adhdresults" ) {
			results_page.wireContent();
		}
		if ( document.body.id == "more-info" ) {
			if ( window.name == "moreinfo" ) {
				var el = document.getElementById("close-button");
				el.onclick = new Function( "self.close();return false;" );
			}
		}
	}
}

var univ = {
	preload : function ( ) {
		var el = document.getElementById("e");
		if ( el ) {
			var ref = document.location.href;
			el.href = ((ref.indexOf("?")>0)?(ref.substring(0,ref.indexOf("?")) + "?e=1"):(ref + "?e=1"));
		}
	}
}

var adhd_page = {
	wireContent : function ( ) {
		var fm = document.getElementById("adhdassessment");
		if ( fm ) {
			var uls = fm.getElementsByTagName("ul");
			var lis = "";
			var j = 0;
			for ( var i = 0; i < uls.length; i++ ) {
				lis = uls[i].getElementsByTagName("li");
				for ( j = 1; j < lis.length; j++ ) {
					lis[j].className = "rate_" + j;
				}
			}
			var el = document.getElementById("tgif");
			if ( el ) {
				el.value = document.body.id;
			}
		}
	}
}

var had_page = {
	wireContent : function ( ) {
		var el = document.getElementById("tgif");
		if ( el ) {
			el.value = document.body.id;
		}
	}
}

var results_page = {
	wireContent : function ( ) {
		var el = document.getElementById("tgif");
		if ( el ) {
			el.value = document.body.id;
		}
		el = document.getElementById("contact-me");
		if ( el ) {
			el.onsubmit = new Function ( "return results_page.sendContact();" );
		}
	},
	sendContact : function ( ) {
		var el = document.getElementById("contact-submit");
		el.style.display = "none";
		el = document.getElementById("contact-me");
		edithash = $("contact-me").serialize(true);
		new Ajax.Request( "contact-me.php",
		{
			method: "post",
			parameters: edithash,
			onSuccess: function( transport ) {
				el.style.display = "none";
				urchinTracker("self-assess-contact-form.html");
				alert(transport.responseText);
			}
		}
		);
		return false;
	}
}

var contact_page = {
	wireContent : function ( ) {
		var el = document.getElementById("tgif");
		if ( el ) {
			el.value = document.body.id;
		}
		el = document.getElementById("contact-us");
		if ( el ) {
			el.onsubmit = new Function ( "return contact_page.sendContact();" );
		}
	},
	sendContact : function ( ) {
		var el = document.getElementById("contact-submit");
		el.style.display = "none";
		el = document.getElementById("contact-us");
		edithash = $("contact-us").serialize(true);
		new Ajax.Request( "contact-us.php",
		{
			method: "post",
			parameters: edithash,
			onSuccess: function( transport ) {
				el.style.display = "none";
				urchinTracker("main-contact-form.html");
				alert(transport.responseText);
			}
		}
		);
		return false;
	}
}

if ( document.getElementById && document.getElementsByTagName ) { 
	addLoadEvent(univ.preload);
	addLoadEvent(preloads.fireOnLoad);
}
