
// This global holds the current action of
// event_form so it can be used later
//var g_FormAction;

// Stores the current event_form action in
// g_FormAction
function store_form_action (form_action)
{
  var g_FormAction = form_action;
}

// Used anywhere a user form (i.e. request account)
// requires a username entry (except login form!)
function is_valid_username (target)
{
  target.value = trim (target.value);
  var filter = RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,11}$");
	if (filter.test(target.value))
  {
    return true;
  }
}


// Checks to make sure a form <SELECT> box has
// a value other than 0 selected
function is_selected (target)
{
  if (target.value == '' || target.value == '0')
  {
    return false;
  }
  
  return true;
}

function is_valid_birthday (target)
{
  target.value = trim (target.value);
  var filter = RegExp("^[0-9]{1,2}$");
	if (filter.test(target.value) && target.value > 0 && target.value <= 31)
  {
    return true;
  }
  
  return false;
}

function is_valid_birthyear (target)
{
  target.value = trim (target.value);
  var filter = RegExp("^[0-9]{4}$");
	if (filter.test(target.value) && target.value > 1910 && target.value < 2100)
  {
    return true;
  }
  
  return false;
}

function is_valid_email (target)
{
  target.value = trim (target.value);
	//var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)
  var filter = RegExp("(^[a-z]([a-z0-9\-_\.]*)@([a-z0-9_\.]*)([.][a-z0-9_\]{3})$)|(^[a-z]([a-z0-9\-_\.]*)@([a-z0-9_\.]*)(\.[a-z0-9_\]{3})(\.[a-z]{2})*$)","i");
  
	if (filter.test(target.value))
  {
    return true;
  }
	else
  {
    return false;
  }
}

// You'd think JavaScript would have a trim
// function built-in... but apparently not.
// So, here's mine.
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

// Used for validating First and Last names, which
// database allows to be twenty chars long.
function is_valid_name (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[a-zA-Z\. \'-]{2,20}$/);
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Used for validating Teaching Group names
function is_valid_group_name (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[0-9a-zA-Z\. \'-]{2,20}$/);
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Middle names are never required,
// otherwise same as is_valid_name()
function is_valid_middlename (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[a-zA-Z\. \']{0,20}$/);
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// School student id can have virtually any typed character
function is_valid_school_student_id (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[A-Za-z0-9]{2,20}$/);
  
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Similar to school student id, but not required
function is_valid_custom_id (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[A-Za-z0-9_]{0,20}$/);
  
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// State names can be 20 characters long
// ... not sure this should be required?
function is_valid_state (target)
{
  target.value = trim (target.value);
  var filter = RegExp(/^[a-zA-Z\. \']{2,20}$/);
  
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Generic place names, i.e. long names of
// schools, countries, etc
function is_valid_place (target)
{
  target.value = trim (target.value);
  
  // Edited by Justin Lacy, 08/09/2005
  var filter = RegExp(/^[-&\.\', a-zA-Z#0-9]{2,40}$/);
  
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Validates street addresses. Probably
// not fool proof!
function is_valid_address (target)
{
  var filter = RegExp(/^[-&.,\/ \w#\r\n\']{4,40}$/);
  
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}

// Accurately validates virtually ANY CONCEIVABLE
// PHONE NUMBER IN THE WORLD in varying formats.
// (Brian's work - I'm quite proud of it!)
function is_valid_phone (target) {
  var usafilter = new RegExp(/^(\s*[\+]?\s*1\s*[\.-]?)?\s*(\d{3}|\(\s*\d{3}\s*\))\s*[\.-]?\s*\d{3}\s*[\.-]?\s*\d{4}((\s|[\.-]){1}\s*[\.-]?\s*([extnsioEXTENSIO\.\:]{1,9}?\s*[\.-]?)?\d{1,7})?$/);       // ((CONNECT)|| )?
  var worldfilter = RegExp("^(\s*[\+])?(\s*[\.-]?\d+\s*)*$");
	if (usafilter.test(target.value))
  {
    return true;
  }
  return false;
}

// USA Zip code, 5 digit or 5-4 format.
function is_valid_zip (target)
{
  target.value = trim (target.value);
  var filter = RegExp("^[0-9]{5}(-[0-9]{4})?$");
  var canada_filter = RegExp("^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$","i");

	if (filter.test(target.value) || canada_filter.test(target.value))
  {
    return true;
  }
  return false;
}
function is_valid_pass (target)
{
        target.value = trim (target.value);
	var rexp = RegExp("^[a-z,A-Z0-9 ]{6,10}$");

	if ( rexp.test(target.value) )
	{
		return true;
	}

	return false;

}
// FOR EMAIL FORM
function valid_subject (target)
{
  if (target.value.length < 2)
  {
    return false;
  }
  return true;
}

function valid_message (target)
{
  if (target.value.length < 2)
  {
    return false;
  }
  return true;
}

function valid_user_list (target)
{
  if (target.value.length < 6)
  {
    return false;
  }
  return true;
}

/*
function is_valid_user_list (target)
{
  alert (target);
  // target.value = trim (target.value);
  var filter = /^(([a-zA-Z0-9_-]+[,; &\<\>]+)|([,; &\<\>]+[a-zA-Z0-9_-]+))+$/;
	if (filter.test(target.value))
  {
    return true;
  }
  return false;
}
*/
//END EMAIL FORM

// MOST IMPORTANT JAVASCRIPT FUNCTION
// Virtually All Navigation takes place
// through the "Post" function.
function post (action, event, str_params, window_type)
{
var theform = document.event_form;
  // action
  //     Simply, a URL. Will be assigned as the action
  //     for event_form. This is where we're about to
  //     jump to. Example:
  //     'http://www.lpsoftware.com/dtorf?e=some_event'
  // event
  //     rarely used; don't even know if it works
  //     anymore! These days events are assigned
  //     as part of the action. See above.
  // str_params
  //     Used frequently to pass data to the next page
  //     that couldn't be determined beforehand, or
  //     needed to be sent to the next page from the
  //     previous page. Must be in the form:
  //     'var1:value;var2:value' etc.
  // window_type
  //     if none specified, assumed standard navigation.
  //     Specify 'popup' to open in a new window.
  //     Could perhaps be extended, but this works.
  
  // This is now globalized, and thus not needed.
  // var theform = get_form ("event_form");
  
  // Debugging alert.
  // alert ("JS Post:\n\naction = " + action + "\nevent = " + event + "\nstr_params = " + str_params + "\nwindow_type = " + window_type);
  
  // If an action was passed in, use it,
  // otherwise use the current action.
  // Required because event_form doesn't
  // always have an action (but I don't
  // remember why. Gimme a break, its
  // been months since I wrote this.)
  if (action != null && action != '')
  {
    theform.action = action;
  }
  else
  {
    theform.action = g_FormAction;
  }
  
  // An event was specified. This ALMOST
  // NEVER HAPPENS - events are typically
  // assigned as part of the Action.
  if (event != null && event != '')
  {
    theform.event.value = event;
  }
  
  if (str_params != null && str_params != '')
  {
    theform.str_params.value = str_params;
  }
  
  // Sets hidden field "window_type" to passed
  // value (currently supports '' or 'popup')
  theform.window_type.value = window_type;
  
  // If 'popup', the next page will be opened
  // in a new window. Uses event_form's TARGET
  // property.
  if (window_type == 'popup')
  {
    // New window.
    theform.target = 'window_popup';
  }
  else
  {
    // Apparently this is deprecated. If I remember
    // correctly, this broke windows that were
    // already popups.
    // theform.window_type.value = '';
    
    // Make sure target is blank, since we're staying in the same window.
    theform.target = '';
  }
  // Debugging purposes only
  // alert ("Form Post:\n\naction = " + theform.action + "\nevent = " + theform.event.value + "\nstr_params = " + theform.str_params.value + "\nwindow_type = " + theform.window_type.value);
  
  // Okay, we're all set - Here We Go!
  theform.submit();
}


