$(document).ready(function(){

	$('a.toggleError').css({ "display":"none" });

	//onclick so ? links display help-boxes
	$('a.toggle').toggle(
		function() {
			$(this).next('div').children('div.help-box').css({ "display":"inline" });
			return false;
		}
		,function() {
			$(this).next('div').children('div.help-box').css({ "display":"none" });
			return false;				
		}
	);
	
	//onfocus so ? links display help-boxes
	$('a.toggle').focus(
		function() {
			$(this).next('div').children('div.help-box').css({ "display":"inline" });
			return false;
		}
	)
	$('a.toggle').blur(
		function() {
			$(this).next('div').children('div.help-box').css({ "display":"none" });
			return false;				
		}
	)	
	
	//making the help-boxes visible when form elements are in focus
	$(':input').focus(
		function() {
		
		var errorStyle = "";
		
		if($(this).parent('div').parent('fieldset').children('div').children('div.error-box').length > 0){
		
			errorObj = $(this).parent('div').parent('fieldset').children('div').children('div.error-box');

			errorStyle = errorObj.css("display");
			
		}

		if( (errorStyle == "") || (errorStyle == "none") ){
		
			//if it's in a fieldset
			if($(this).parent('div').parent('fieldset').length > 0) {
				//if the help-box is in the same fieldset
				if($(this).parent('div').parent('fieldset').children('div').children('div.help-box').length > 0) {
					$(this).parent('div').parent('fieldset').children('div').children('div.help-box').css({ "display":"inline" });
				}
				else {
					$(this).parent('div').parent('fieldset').children('div').children('div.help-box').css({ "display":"inline" });
				}
			}
			else {
				$(this).parent('div').parent().children('div').children('div.help-box').css({ "display":"inline" });
			}
			
		}
		//clear input elements when you tab in
		if(this.value == this.title) {this.value = ""};	
		}
	);
	
	//making them hidden when not in focus
	$(':input').blur(
		function() {
			//if it's in a fieldset
			if($(this).parent('div').parent('fieldset').length > 0) {
				//if the help-box is in the same fieldset
				if($(this).parent('div').parent('fieldset').children('div').children('div.help-box').length > 0) {
					$(this).parent('div').parent('fieldset').children('div').children('div.help-box').css({ "display":"none" });
				}
				else {
					$(this).parent('div').parent('fieldset').children('div').children('div.help-box').css({ "display":"none" });
				}
			}
			else {
				$(this).parent('div').parent().children('div').children('div.help-box').css({ "display":"none" });
			}
			//retain new content to inputs if added, otherwise return to original
			if (this.nodeName == "INPUT") {
				if (this.value != this.title) {
					if (this.value == "") {this.value = this.title;} //this.value = this.title;}
				}
			}
		//validate(); currently no client side validation			
		}
	);
	//making the help-boxes visible when form elements are in focus
	$(':input').keyup(
		function() {
			if( (this.value == this.title) || (this.value == "") ){	
			}
			else{
				$(this).parent('div').parent('fieldset').children('div').children('div.error-box').css({ "display":"none" });
				$(this).parent('div').parent('fieldset').children('a.toggle').css({ "display":"inline" });
				$(this).parent('div').parent('fieldset').children('div').children('div.help-box').css({ "display":"inline" });
			}
		}
	);
	//validate(); currently no client side validation
});
 
function validate() {
	var valid = true;
	var inputs = $(':input');
	var submit = $(':input').parent(".form-footer").children("input")[0];
	for (i=0;i<inputs.length;i++) {
		//ignore submit button & disabled fields
		if(inputs[i] != submit && inputs[i].disabled != true) {
			//if it's not a select
			if(inputs[i].options == null) {
				if(inputs[i].value == inputs[i].title || inputs[i].value == "") {valid = false;}
			}
			else {	
				if(inputs[i].selectedIndex < 1) {valid = false;}
				//alert(inputs[i].nodeName+" title : "+inputs[i].title+", value : "+", index?: "+inputs[i].selectedIndex+", index"+inputs[i].value+"\n\nvalid : "+valid);
			}
		}
	}
	if (valid == true) {
		$(submit).attr({ disabled : false });
	}
	else { 
		if($(submit).attr("disabled") == false) {
		$(submit).attr({ disabled : true })
		}
	}
}

// function to allow javascript to programmatically load a CSS stylesheet
function loadStylesheet( filename, media ){
	// ensure that the browser has all the DOM methods/properties we need
	if( !(document.getElementById) ||
		!(document.childNodes) ||
		!(document.createElement) ||
		!(document.getElementsByTagName) ){
		return;
	}

	// make a new link node to the stylesheet
	var link = document.createElement( "link" );
	link.href = filename;
	link.rel = "stylesheet";
	link.type = "text/css";
	link.media = media;

	// insert the stylesheet link into the document head
	document.getElementsByTagName('head')[0].appendChild(link);
}

// load listener to load multiple fns onload
function addLoadEvent( func ){
	// if there isn't currently an onload function...
	if( typeof window.onload != "function" ){
		// ...then just set it to this one
		window.onload = func;
	} else {
		// otherwise, remember the old onload function
		var oldOnloadFn = window.onload;
		// and make a new onload function that will call the old one, and then func
		window.onload = function() {
			if( oldOnloadFn ) {
				oldOnloadFn();
			}
			func();
		};
	}
}