// This validator is for the CLC Request for the
// Web Cast and MultiMedia Storage Form
// 11 August 2009

// Main function
function checkWholeForm(regform) {
 var msg = "";
 var i = 0;

  msg += checkContactName(regform.FirstName.value);
  if (msg == "")
  {
   msg += checkContactName(regform.LastName.value);
  }
  msg += checkEmail(regform.email.value);
  msg += checkOrgName(regform.OrganizationName.value);
  msg += checkRecordingName(regform.RecordingName.value);
// Disabled checkRecordingLocation 28 October 2009 at 1141 lax
// Because professors do not understand the meaning of this field.
//  msg += checkRecordingLocation(regform.RecordingLocationWebSite.value);

 if (msg != "") {
  msg_hdr = "====================================================\n"
  msg_hdr += "The form was not submitted because of the following error(s).\n";
  msg_hdr += "Please correct these error(s) and resubmit.\n";
  msg_hdr += "====================================================\n\n"

  new_msg = msg_hdr + msg;
  alert(new_msg);
  return false;
 }
 return true;
}
// ********* End of main function *********

// ********* Start of helper functions *********
function isBlank (someString)
{
 for(var i = 0; i < someString.length; i++)
 {
  var c = someString.charAt(i);
  if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
 }
 return true;
}
	
function containsSpaces (someString)
{
 for(var i = 0; i < someString.length; i++)
 {
  var c = someString.charAt(i);
  if ((c == ' ') || (c == '\n') || (c == '\t')) return true;
 }
 return false;
}
	
// Functions to check contact information
function checkContactName (someString)
{
 var error = "";

 if (isBlank(someString))
 { error = "- Please enter your first and last name.\n"; }

 return error;
}

function checkEmail (someString)
{
// Check the validity of the submitted email address
//  ^(\w|\-|\_|\.)+\@ checks that the address begins with
//  any combination of alphanumeric characters, dashes, underscores
//  or dots any number of times, followed by an @
//
//  ((\w&\-&\_)+\.) deals with subdomains, checking for a group of
//  any number of valid characters followed by a dot. The + sign after
//  that allows for any number of subdomains.
//
//  [a-zA-Z]{2,}$ checks that the address ends in at least 2 alpha
//  characters to cope with the top level domain.

 var error = "";
 var pattern = /^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/;

 if (someString.search(pattern) == "-1")
 { error = " - Please enter a valid email address.\n"; }

 return error;
}

function checkOrgName ( someString )
{
 var error = "";

 if (isBlank(someString))
 { error = "- Please enter the name of your department or organization.\n"; }

 return error;
}

function checkRecordingName ( someString )
{
 var error = "";

 if ((isBlank(someString)) || (containsSpaces(someString)))
 { error = "- Please enter a valid name for your recording.\n"; }

 return error;
}


function checkRecordingLocation ( someString )
{
 var error = "";

 if (isBlank(someString))
 { error = "- Please enter a valid web address for your recording.\n"; }

 return error;
}



