function startup() {
  if($("vuForms")) {
	$("vuForms").reset();
  }
}

String.prototype.trim = function() {
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}


function validateForm(thisForm) {

  var selected;
  var field;
  var pattern;
  
  // Title Validation
  selected = false;
  for(i = 0; i < thisForm.title.length; i++) {
    if (thisForm.title[i].checked)
      selected = true;
    }
    if(!selected) {
      alert("Please select one of the title options provided.");
    return false;
  }
  
  // First Name Validation
  field = thisForm.firstName.value.trim();
  if(field == "") {
    alert("Please enter a value for the First Name field.");
    thisForm.firstName.focus();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only letters, dashes, spaces, apostrophes, or periods in the First Name field.");
    thisForm.firstName.focus();
    return false;
  }
  
  // Last Name Validation
  field = thisForm.lastName.value.trim();
  if(field == "") {
    alert("Please enter a value for the Last Name field.");
    thisForm.lastName.focus();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only letters, dashes, spaces, apostrophes, or periods in the Last Name field.");
    thisForm.lastName.focus();
    return false;
  }

  // Email Address Validation
  field = thisForm.emailAddress.value.trim();
  if(field == "") {
    alert("Please enter 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;
  }
  return true;
}

function subscribe() {
  var title = encodeURI($("title").innerHTML.trim());
  var firstName = encodeURI($("firstName").innerHTML.trim());
  var lastName = encodeURI($("lastName").innerHTML.trim());
  var emailAddress = encodeURI($("emailAddress").innerHTML.trim());
  var howHeard = encodeURI($("howHeard").innerHTML.trim());	var success = function(transport){subscribeSuccess(transport);}
  var failure = function(){subscribeError();}
  var missing = function(){subscribeMissing();}
  var complete = function(){subscribeComplete();}
  $("newsletter").disabled = true;
  var pars = "subcommand=AJAX";
  pars += "&title=" + title + "&firstName=" + firstName + "&lastName=" + lastName + "&emailAddress=" + emailAddress + "&howHeard=" + howHeard;
  pars += "&processor=/newsletterSubscription/subscribe/GCOM";
  new Ajax.Request("/artsci/communication/graduate/guestbook/form_confirmation_proxy.htm?template=no",
    {
	  method: "post",
	  postBody: pars,
	  onSuccess: success,
	  onFailure: failure,
	  on404: missing,
	  onComplete: complete
	}
  );
}

function subscribeSuccess(transport) {
	var output = (transport.responseText).trim();
  if(output == "Subscribed") {
  	popupWindow("/artsci/communication/graduate/guestbook/form_confirmation_confirm.htm?template=no", "Confirm", 600, 300);
  }
  else if(output == "Unchanged") {
  	popupWindow("/artsci/communication/graduate/guestbook/form_confirmation_unchanged.htm?template=no", "Unchanged", 600, 300);
  }
  else {
  	alert("Subscription request failed.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
  }
}

function subscribeError() {
  alert("Subscription request failed.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
}

function subscribeMissing() {
  alert("Subscription application not found.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
}

function subscribeComplete() {
  $("newsletter").disabled = false;
}

function popupWindow(url, popupName, width, height) {

  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));
  var windowFeatures = "width=" + width + ",height=" + height + 
        ",status=no,resizable,left=" + left + ",top=" + top + 
        "screenX=" + left + ",screenY=" + top;
  mypopup = window.open(url, popupName, windowFeatures);
}

function register() {
  var title = encodeURI($("title").innerHTML.trim());
  var firstName = encodeURI($("firstName").innerHTML.trim());
  var lastName = encodeURI($("lastName").innerHTML.trim());
  var emailAddress = encodeURI($("emailAddress").innerHTML.trim());
  var howHeard = encodeURI($("howHeard").innerHTML.trim());	
  var deptname = encodeURI("Graduate Studies in Communication");
  var success = function(transport){registerSuccess(transport);}
  var failure = function(){registerError();}
  var missing = function(){registerMissing();}
  var complete = function(){registerComplete();}
  $("apptscheduler").disabled = true;
  var pars = "subcommand=AJAX";
  
  pars += "&title=" + title + "&firstName=" + firstName + "&lastName=" + lastName + "&emailAddress=" + emailAddress + "&howHeard=" + howHeard;
  pars += "&deptname="+deptname;
  pars += "&processor=/onlineappointment/register";
  new Ajax.Request("/artsci/communication/graduate/guestbook/form_confirmation_proxy_register.htm?template=no",
    {
	  method: "post",
	  postBody: pars,
	  onSuccess: success,
	  onFailure: failure,
	  on404: missing,
	  onComplete: complete
	}
  );
}

function registerSuccess(transport) {
  var output = (transport.responseText).trim();
  if(output == "Registered") {
   	popupWindow("/artsci/communication/graduate/guestbook/form_confirmation_registered.htm?template=no", "Confirm", 600, 300);
  } else {
  	alert("Registration request failed.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
  }
}

function registerError() {
  alert("Registration request failed.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
}

function registerMissing() {
  alert("Appointment Scheduler Register application not found.\n" + "Please call the UNIT Help Desk at x97777 for assistance.");
}

function registerComplete() {
  $("apptscheduler").disabled = false;
}


