function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...
    var j=0, i = s.length;
    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }
  return s;
}


function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...
    var i = s.length - 1;       // Get length of string
    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;
    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }
  return s;
}

function trim(str)
{
  return RTrim(LTrim(str));
}

function is_valid_email (email){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}


//get previous sibling node w/o returning null if there's whitespace or newlines seperating them
//does not work with IE, nor does it seemingly need it
Object.prototype.previousObject = function() {
	var p = this;
	do p = p.previousSibling;
	while (p && p.nodeType != 1);
	return p;
}

//check if any of the fields have attribute 'required="1"' are blank
function rvalidate(){
	var browser = navigator.appName;
	var form = document.forms[0];
	var err = "";
	for(x=0; x<form.elements.length; x++){
		element = form.elements[x];
		element.value = trim(element.value);
		//check for elements that are currently showing and required
		if(element.getAttribute("required")==1 && element.style.display!="none"){
			//if(element.name=="contact_method" && element.checked && element.value=="email"){
			if(element.name=="email"){
				email = element.value;
				if(email==""){
					err += "email\n";
				}
				else if(!is_valid_email(email)){
					alert("Please enter a valid email.\n");
					return false;
				}
			}
			else if(element.value==""){
				//get the previous nodes text which should be the label for the variable being checked
				//IE can get this imformation w/o returning null but will error out with the latter method
				if(browser=="Microsoft Internet Explorer"){
					text_name = element.parentNode.previousSibling.firstChild.innerHTML;
				}
				else{
					text_name = element.parentNode.previousObject().firstChild.innerHTML;
				}
				//remove colon and add newline
				err += text_name.substring(0, text_name.length - 1) + "\n";
			}			
		}
	}
	if(err!=""){
		alert("The follow is required:\n\n" + err);
		return false;
	}
	else{
		return true;
	}
}

onload=function(){
if (document.getElementsByClassName == undefined) {
	document.getElementsByClassName = function(className)
	{
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = document.getElementsByTagName("*");
		var results = [];

		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}

		return results;
	}
}
}

function expand_selection (selection){
	hidden_classes = document.getElementsByClassName("hidden");
	comment_classes = document.getElementsByClassName("comment");
	unhide_classes = document.getElementsByClassName(selection);

	for(x=0; x<hidden_classes.length; x++){
		if(hidden_classes[x]!=comment_classes[1]){
			hidden_classes[x].style.display = "none";
		}
	}
	for(x=0; x<unhide_classes.length; x++){
		unhide_classes[x].style.display = "inline";
	}
	for(x=0; x<comment_classes.length; x++){
		comment_classes[x].style.display = "inline";
	}
}

