var prices = new Array();
prices["dd"] = new Array();

String.prototype.trim = function() {
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

function startup() {
	if($("vuForms")) {
	 $("vuForms").reset();
	 populatePrices();
	}
}

function populatePrices() {
  prices["dd"]["person"] = $("ddPricePerson") ? Number($("ddPricePerson").innerHTML) : 0;
  prices["dd"]["couple"] = $("ddPricePerson") ? Number($("ddPriceCouple").innerHTML) : 0;
  prices["db"] = $("dbPrice") ? Number($("dbPrice").innerHTML) : 0;
  prices["mc"] = $("mcPrice") ? Number($("mcPrice").innerHTML) : 0;
  prices["sp"] = $("spPrice") ? Number($("spPrice").innerHTML) : 0;
  prices["sf"] = $("sfPrice") ? Number($("sfPrice").innerHTML) : 0;
  prices["sn"] = $("snPrice") ? Number($("snPrice").innerHTML) : 0;
  prices["go"] = $("goPrice") ? Number($("goPrice").innerHTML) : 0;
}

function calculate(thisForm) {
  var price = 0;
  if(thisForm.dd2 && thisForm.dd2.checked) {
  	price += prices["dd"]["person"];
  }
  else if(thisForm.dd3 && thisForm.dd3.checked) {
  	price += prices["dd"]["couple"];
  }
  if(thisForm.db2 && thisForm.db2.checked) {
  	price += prices["db"];
  }
  if(thisForm.mc2 && thisForm.mc2.checked) {
  	price += prices["mc"];
  }
  else if(thisForm.mc3 && thisForm.mc3.checked) {
  	price += 2 * prices["mc"];
  }
  if(thisForm.sp2 && thisForm.sp2.checked) {
  	price += prices["sp"];
  }
  if(thisForm.sf2 && thisForm.sf2.checked) {
  	price += prices["sf"];
  }
  if(thisForm.sn2 && thisForm.sn2.checked) {
    price += prices["sn"];
  }
  if(thisForm.go2 && thisForm.go2.checked) {
  	price += 2 * prices["go"];
  }
  else if(thisForm.go3 && thisForm.go3.checked) {
  	price += 3 * prices["go"];
  }
  else if(thisForm.go4 && thisForm.go4.checked) {
  	price += 4 * prices["go"];
  }
  $("price").innerHTML = "<span>Cost: $" + price + "</span><input type='hidden' name='cost' id='cost' value='" + price + "'>";      
}

function validateForm(thisForm) {
  var selected;
  var element
  var field;
  var pattern;

  // First Name Validation
  field = thisForm.firstName.value.trim();
  if(field.length == 0) {
    alert("Please provide your first name.");
    thisForm.firstName.focus();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, letters, dashes, apostrophes, or periods in your first name.");
    thisForm.firstName.focus();
    return false;
  }
  
  // Last Name Validation
  field = thisForm.lastName.value.trim();
  if(field.length == 0) {
    alert("Please provide your last name.");
    thisForm.lastName.focus();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, letters, dashes, apostrophes, or periods in your last name.");
    thisForm.lastName.focus();
    return false;
  }
  
  // Email Address Validation
  field = thisForm.emailAddress.value.trim();
  if(field == "") {
    alert("Please provide your email address.");
    thisForm.emailAddress.focus();
    return false;
  }
  pattern = /^.*@\w[\w\-\.]*.\w[\w\-]*$/;
  if(!field.match(pattern)) {
    alert("Please enter a valid email address.");
    thisForm.emailAddress.focus();
    return false;
  }
    
  // Banner ID Validation
  if(thisForm.bannerId.value.trim().length == 0) {
    alert("Please provde your banner ID.");
    thisForm.bannerId.focus();
    return false;
  }
  
  // Cost Validation
  if(thisForm.cost.value.trim().length == 0 || thisForm.cost.value == 0) {
    alert("There was a problem calculating the cost. " +
      "If you attempted to register for an event, " +
      "please notify the Student Development Office " +
      "of the problem you encountered so the form can be repaired");
      return false;
  }
  
  //alert("Would normally submit");
  //return false;
  return true;
}

