//
//	Takes a string and returns the same value back with the whitespace trimmed off
//
function trimString(inString) {
  inString = inString.replace( /^\s+/g, '');
  return inString.replace( /\s+$/g, '');
}

//
//	Checks the form used to contact us and returns true if they have filled out the
//	form correctly. If not, we show them a message and return a value of false back
//	to them.
//
function checkContactForm() {
	//	Keep track of our errors
	var errorElements = new Array();
	
	// Make sure we got something for an email address
	var param = trimString(document.getElementsByName('email')[0].value);
	if ( param.length < 3 ) {
		errorElements.push('   - Please enter an email address');
	}
	
	// Make sure they put something in for a subject
	param = trimString(document.getElementsByName('subject')[0].value);
	if ( param.length < 1 ) {
		errorElements.push('   - Please enter a subject');
	}
	
	// Make sure they put something in for a message
	param = trimString(document.getElementsByName('message')[0].value);
	if ( param.length < 1 ) {
		errorElements.push('   - Please enter a message');
	}
	
	// If we have no errors, we are done.
	if ( errorElements.length == 0 ) {
		return true;
	}
	
	// Build up a message that we can show them 
	var msg = "There are problems with your submission:\n\n" + errorElements.join("\n") + "\n\nPlease correct these errors and try again.";
	alert(msg);
	
	return false;
}

//
//	Adds onFocus and onBlur events to the form elements so that we can highlight the
//	current form (since IE is too lame to support the easy way of doing this with CSS)
//
function addFormElementFocus() {
	//	Set the background colors for unselected and selected form elements
	var selectedElColor = '#FFF';
	var notSelectedElColor = '#F0F0F4';
	
	//
	//	Build up the list of elements that we will be messing with
	//
	var els = new Array();
	
	// 	Get a handle to all of the select elements
	var selectEls = window.document.getElementsByTagName('select');
	if ( selectEls ) {
		//	For some reason, els.concat does not work with nodes, need to get each element
		var selectElsLength = selectEls.length;
		for ( var i = 0; i < selectElsLength; i++ ) {
			els.push(selectEls[i]);
		}
	}
	
	//	Get handles to the input elements of type text and password
	var inputEls = window.document.getElementsByTagName('input');
	if ( inputEls ) {
		var inputElsLength = inputEls.length;
		for ( var i = 0; i < inputElsLength; i++ ) {
			if( inputEls[i].type == 'text' || inputEls[i].type == 'password' ) {
				els.push(inputEls[i]);
			}
		}
	}
	
	//	Lastly, add any textareas that are present
	var textareaEls = window.document.getElementsByTagName('textarea');
	if ( textareaEls ) {
		//	For some reason, els.concat does not work with nodes, need to get each element
		var textareaElsLength = textareaEls.length;
		for ( var i = 0; i < textareaElsLength; i++ ) {
			els.push(textareaEls[i]);
		}
	}
	
	//	Now we can loop through all of the form elements and update them with events
	var elsLength = els.length;
	for ( var i = 0; i < elsLength; i++ ) {
		els[i].onfocus = function () {
			if ( this.focus ) {
				this.style.background = selectedElColor;
			}
		}
		els[i].onblur = function() {
			if ( this.blur ) {
				this.style.background = notSelectedElColor;
			}
		}
	}
	
	return;
}

//
//	This gets called on page load to give the first form element of type input in the page
//	the focus so that they can happily start typing without having to use the mouse to get
//	into the first form element.
//
function giveFormFocus() {
	//	Get a handle to the elements... if there are no we are done.
	inputEls = document.getElementsByTagName('input');
	if ( !inputEls ) {
		return;
	}
	
	//	Loop through each one and give it focus if it's of the "correct" type
	var inputElsLength = inputEls.length;
	for ( var i = 0; i < inputElsLength; i++ ) {
		if ( inputEls[i].type == "text" ) {
			//	Give this element the focus and bail out
			inputEls[i].focus();
			return;
		}
	}
}

//
//	Fades an element and hides it once done
//
function fadeEl(elId, delay, step, duration, numSteps) {
	//	If we are given a non-zero delay, we'll want to delay that long and then
	//	call this proc back with no delay set
	if ( delay > 0 ) {
		setTimeout("fadeEl('" + elId + "', 0, " + step + ", " + duration + ", " + numSteps + ")", delay);
		return;
	}
	
	//	Get a handle to the element
	var el = document.getElementById(elId);
	
	//	If there is no such element, we are done
	if ( !el ) {
		return;
	}
	
	//	Set the minimum opacity. We don't set this to '0' since fading all the 
	//	way to zero looks kind of weird.
	var minOpacity = 0.5;
	
	//	Set the opacity for this step
	el.style.opacity = 1.0 - ( 1.0 - minOpacity ) * step / numSteps;
	
	//	If we are still fading, recurse
	if ( step < numSteps ) {
		//	We are recursing
		step ++;
		setTimeout("fadeEl('" + elId + "', 0, " + step + ", " + duration + ", " + numSteps + ")", duration / numSteps);
	} else {
		//	We are completely faded now so hide the element
		hideEl(el);
	}
}

//
//	Given an element or the ID of an element, hides it
//
function hideEl(elOrId) {
	//	If we've been given an ID, get a handle on the element itself
	if ( typeof elOrId == 'string' ) {
		elOrId = document.getElementById(elOrId);	
	}
	
	//	If we have no element at this point, there is nothing we can do
	if ( !elOrId ) {
		return;
	}
	
	//	Hide the element
	elOrId.style.display = 'none';
	elOrId.style.visibility = 'hidden';
}