var win = new Array();

function OpenWindow(url, x, y) {

  var windowAttr;
  var winName;
  
  if (x && y) {
    windowAttr = "width=" + x + ",height=" + y + ",";
  }
  var winName = "Popup" + x + y;
  windowAttr += "menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no";
  if (win[winName] && !win[winName].closed) {
    win[winName].focus();
  } else {
    win[winName] = window.open("Popup/" + url, winName, windowAttr);
  }
}
function ImageOn(imgName) {  

  if(document.images[imgName]) {
    document[imgName].src = eval(imgName + "_on.src")
  }
  
}

function ImageOff(imgName) {  

  if(document.images[imgName]) {
      document[imgName].src = eval(imgName + ".src");
  }
  
}

// *** Initialize Error Variables ***
var errMessage = "";
var errRequired = "";
var errInvalid = "";
var errCustom = "";
var firstError = "";
var focusMethod = "";

// *** Initialize Date Variables ***
var monthArr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var today = new Date();


// *** Check Functions ***
function CheckEmpty(field, fieldName) {

  //Checks if a field is empty. Adds field name to error string and assigns a field focus method
  //Given: form field (field) and field name (fieldName)
  //Returns: Nothing

  if ((field.type.indexOf("select") == -1 && field.value == "") || (field.type.indexOf("select") != -1 && field.options[field.selectedIndex].value == "")) {
    errRequired += ("\n" + fieldName)
    
    //Set the focus field and method
    if (firstError == "") {
      firstError = field;
      focusMethod = "focus";
    }
    return false;
  }
  return true;
}

function CheckValidity(validator, field, fieldName) {

  //Checks if a field value is valid. Adds the field name to an error string and 
  //assigns a field focus method
  //Given: validation type (validator), form field (field), field name (field name)
  //Returns: true if valid; false if invalid

  if (!eval(validator)(field)) {
    errInvalid += ("\n" + fieldName)
    
    //Set the focus field and method
    if (firstError == "") {
      firstError = field;
      focusMethod = "select";
    }
    return false;
  }
  return true;
}


// *** Error Reporting Functions ***
function DisplayErrors() {

  //Displays errors if any
  //Given: nothing
  //Returns: true if no errors; false if errors

  if (errRequired != "") {
    errMessage = "The following fields are required:" + errRequired;
  }
  if (errInvalid != "") {
    if (errRequired != "") {
      errMessage += "\n\n";
    }
    errMessage += "The following fields are invalid:" + errInvalid;
  }
  
  if (errCustom != "") {
    if (errInvalid != "" || errRequired != "") {
      errMessage += "\n";
    }
    errMessage += errCustom;
  }
  
  //Print Error Message and Focus to Field
  if (errMessage != "") {
    alert(errMessage);
    firstError.focus();
    if (focusMethod == "select") {
      firstError.select();
    }
    return false;
  }
  return true;
}

// *** Support Functions ****
function ValidChars(string, validChars, required) {
  //Given: string(string), validChars(string), required(bool)
  //Returns: -1 (empty), 0 (not valid), 1 (valid)
  //Checks if a string (string) contains valid characters (validChars). If required
  
  if (required && (string == "" || !string)) {
    return -1;
  }

  for (var i = 0; i < string.length; i++) {
    if (validChars.indexOf(string.charAt(i)) == -1) {
      return 0;
    }
  }
  return 1;
}


function TrimString(string) {

  //Given: string (string)
  //Returns: newString (string)
  //Trims white space from the beginning and end of a string

  var startIndex = 0;
  var endIndex = string.length;
  var isBegSpace = true;
  var isEndSpace = true;
  var newString;

  //If all white space, return empty string
  for (var i = 0; i < string.length; i++) {
    if (string.charAt(i) != " ") {
      break;
    }
  }
  if (i == string.length) {
    return "";
  }

  //Get the beginning of the string index
  while (isBegSpace) {
    if (string.charAt(startIndex) == " ") {
      startIndex++;
    }
    else {
      isBegSpace = false;
    }
  }

  //Get the end of the string index
  while (isEndSpace) {
    if (string.charAt(endIndex - 1) == " ") {
      endIndex--;
    }
    else {
      isEndSpace = false;
    }
  }

  //Create trimmed string using the indexes
  newString = string.substring(startIndex, endIndex);

  return newString;

}


function TrimFields(form) {

  //Given: form (form object)
  //Returns: nothing
  //Requires: TrimString
  //Trims white space from all text fields

  for (var i = 0; i < form.elements.length; i++) {
    if (form.elements[i].type == "text" || form.elements[i].type == "textarea") {
      form.elements[i].value = TrimString(form.elements[i].value);
    }
  }

}


function IsValidEmail(objEmail) {

  //Checks if an e-mail is in the form xxx@xxx.xxx
  //Given: e-mail field
  //Returns: true if valid; false if invalid

  var email = objEmail.value;
  var ampIndex = email.indexOf("@");
  var dotIndex = email.lastIndexOf(".");

  if (email == "") return true;
  if (HasChar(email, " ")) return false;
  
  if ((ampIndex < 1) || (dotIndex < 1) || (dotIndex > (email.length - 3)) || (ampIndex > dotIndex) || (dotIndex == (ampIndex + 1))) {
    return false;
  }
  return true;
   
}

function HasChar(string, chr) {

  //Checks if a string contains a specified character
  //Given: string (string) chr (character)
  //Returns: true if valid; false if invalid

  for (var i = 0; i < string.length; i++) {
    if (string.charAt(i) == chr) {
      return true;
    }
  }
  return false;
}