function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    //if (!element.defaultValue)continue;		
    element.onfocus = function() {			
			if (!(this.type == "text" || this.type == "textarea")) return;			
			this.style.backgroundColor = '#FFFFCC';
			if (this.value == this.defaultValue) {				
				this.value = "";
			 }
    }
    element.onblur = function() {
			if (!(this.type == "text" || this.type == "textarea")) return;
			this.style.backgroundColor = '#FFFFFF';
      if (this.value == "") {
        this.value = this.defaultValue;				
      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The "+element.name+" field must be a valid email address.");
        return false;
      }
    }
	if (element.className.indexOf("fund") != -1) {
      if (!validFund()) {
        alert("The "+element.name+" field must be a valid fund.");
        return false;
      }
    }
/*	if (element.className.indexOf("cost") != -1) {
      if (!validCost()) {
        alert("The "+element.name+" field must be a valid cost center.");
        return false;
      }
    }
	if (element.className.indexOf("internalOrder") != -1) {
      if (!validOrder()) {
        alert("The "+element.name+" field must be a valid internal order.");
        return false;
      }
    }	
	if (element.className.indexOf("GLaccount") != -1) {
      if (!validGL()) {
        alert("The "+element.name+" field must be a valid G/L Account.");
        return false;
      }
    }*/	
	if (element.className.indexOf("Agreement") != -1) {
      if (!signature()) {
        alert("Your signature is required.");
        return false;
	  }
    }
  }
  return true;
}

function validFund(){
	 var test = document.getElementById("fund").value;
	 var v = test.match(/^\d{8}$/g);
	 if(v){
		 return true;
	 }else{
		 return false;
	 }
}
/*function validCost(){
	 var test = document.getElementById("cost").value;
	 var v = test.match(/^\d{10}$/g);
	 	 if(v){
		 return true;
	 }else{
		 return false;
	 }
}
function validOrder(){
	 var test = document.getElementById("internalOrder").value;
	 var v = test.match(/^\d{10}$/g);
	 	 if(v){
		 return true;
	 }else{
		 return false;
	 }
}
function validGL(){
	 var test = document.getElementById("GLaccount").value;
	 var v = test.match(/^\d{6}$/g);
	 	 if(v){
		 return true;
	 }else{
		 return false;
	 }
}*/

function signature(){
  var test = document.getElementById("Agreement").checked;
  if(test){
	return true;
  } else {
	return false;
  }
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}

$(document).ready(function(){
	 prepareForms();
});
