/*======================================================================================
' Copyright : Streamline Technologies, LLC - Nashville, TN
' Author	: Alex Short
' Created	: 8/19/2004
' Purpose	: Javascript functions used in the Streamline Ticketing application.
======================================================================================*/

// Array that holds number of days in each month  
var daysInMonth = new Array();
  daysInMonth[1] = 31;
  daysInMonth[2] = 29;   // must programmatically check this
  daysInMonth[3] = 31;
  daysInMonth[4] = 30;
  daysInMonth[5] = 31;
  daysInMonth[6] = 30;
  daysInMonth[7] = 31;
  daysInMonth[8] = 31;
  daysInMonth[9] = 30;
  daysInMonth[10] = 31;
  daysInMonth[11] = 30;
  daysInMonth[12] = 31;

function checkAll(formName, checkboxName, isChecked) {
  var i;
  var theForm;
  var element;
    
  for (i=0; i<document.forms.length; i++) {
    theForm = document.forms[i];
    if (theForm.name == formName) break;
  }
  for (i=0; i<theForm.length; i++) {
    element = theForm.elements[i];
    if ((element.type == "checkbox") && (element.name == checkboxName) && (!element.disabled)) element.checked = isChecked;
  }
}

// Opens a new window
function openWin(url,name,toolbar,width,height,status,scrollbars,resize,menubar) {
  window.open(url,name,"toolbar=" + toolbar + ",width=" + width + ",height=" + height + ",status=" + status + ",scrollbars=" + scrollbars + ",resizable=" + resize + ",menubar=" + menubar);  
}

// Submits form f
function submitForm(f) {
  for (var i=0; i<document.forms.length; i++) 
    if (document.forms[i].name == f) document.forms[i].submit();   
}




// Returns the value of a radio button
function getRadioButtonValue (radio) {   
  for (var i=0; i<radio.length; i++) 
    if (radio[i].checked) break;
  return radio[i].value
}

// Returns true if string s is empty
function isEmpty(s) {   
  return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or whitespace characters only.
function isWhitespace (s) { 
  var whitespace = " \t\n\r";  
  if (isEmpty(s)) return true;
  for (var i=0; i<s.length; i++) {   
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  return true;
}

// Returns true if character c is a digit (0 .. 9)
function isDigit (c) {   
  return ((c >= "0") && (c <= "9"))
}

// Returns true if all characters in string s are digits
function isInteger (s) {   
  for (var i=0; i<s.length; i++) {
    var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  return true;  
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {
  var returnString = "";
  for (var i=0; i<s.length; i++) {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

// Returns true if all characters are numbers; first character can be + or -
function isSignedInteger (s) {   
  var startPos = 0;
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    startPos = 1;    
  return (isInteger(s.substring(startPos, s.length)))
}

// Returns true if string s is an integer >= 0
function isNonnegativeInteger (s) {   
  return ((isSignedInteger(s)) && (parseInt (s) >= 0));
}

// Returns true if string s is an integer > 0
function isPositiveInteger (s) {   
  return ((isSignedInteger(s)) && (parseInt (s) > 0));
}

// Returns true if string s is an integer within the range of integer arguments a and b
function isIntegerInRange (s, a, b) {   
  if (!isInteger(s)) return false;
  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}

// Returns true if string s has a length in the range of integer arguments a and b
function isStringInRange (s, a, b) {   
  var len = s.length;
  return ((len >= a) && (len <= b));
}

// Returns true if all characters in s are digits; allows 1 decimal point
function isFloat (s) {   
  var seenDecimalPoint = false;
  if (s == ".") return false;
  for (var i=0; i<s.length; i++) {   
    var c = s.charAt(i);
    if ((c == ".") && !seenDecimalPoint) seenDecimalPoint = true;
    else if (!isDigit(c)) return false;
  }
  return true;
}

// Returns true if all characters in s are digits; allows 1 decimal point; first character can be + or -
function isSignedFloat (s) {   
  var startPos = 0;
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    startPos = 1; 
  return (isFloat(s.substring(startPos, s.length)));
}

// Returns true if at least 1 item in the checkbox field is checked
function isCheckboxChecked (checkbox) {
  if (checkbox.length == undefined) {
    if (checkbox.checked) return true;
    else return false;
  }
  else {
  for (var i=0; i<checkbox.length; i++) 
    if (checkbox[i].checked) return true;
  return false;
  }
}

// Returns true if string s is in valid email format
function isEmail (s) {   
  if (isWhitespace(s)) return false;
  var i = 1;
  var sLength = s.length;

  while ((i < sLength) && (s.charAt(i) != "@")) i++; 

  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;

  while ((i < sLength) && (s.charAt(i) != ".")) i++; 
  
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}

// Returns true if string s is an integer between 1 and 12
function isMonth (s) {   
  return isIntegerInRange (s, 1, 12);
}

// Returns true if string s is an integer between 1 and 31
function isDay (s) {
  return isIntegerInRange (s, 1, 31);
}

// Returns true if string s is a valid year number (2 or 4 digits) 
function isYear (s) {   
  if (!isNonnegativeInteger(s)) return false;
  return ((s.length == 2) || (s.length == 4));
}

// Returns number of days in February of year
function daysInFebruary (year) {
  return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0) )) ? 29 : 28);
}

// Returns true if string s is an integer between 1 and 12
function isHour (s) {   
  return isIntegerInRange (s, 1, 12);
}

// Returns true if string s is an integer between 1 and 59
function isMinuteOrSecond (s) {   
  if(!isIntegerInRange (s, 0, 59)) return false;
  return (s.length == 2);
}

// Returns true if string s is a valid time (hh:mm:ss or hh:mm)
  function isTime (s) { 
    var elems = s.split(":");
    if (!((elems.length == 2) || (elems.length == 3))) return false; //should be 2 or 3 components to the time
    if (!(isHour(elems[0], false) && isMinuteOrSecond(elems[1], false))) return false;
    if (elems.length == 3) { //validate seconds
	  if (!(isMinuteOrSecond(elems[2], false))) return false    
    }
    return true;
  }

// Returns true if string s is a valid date (mm/dd/yyyy or mm/dd/yy)
  function isDate (s) { 
    var elems = s.split("/");
    if (!(elems.length == 3)) return false; //should be 3 components to the date
    if (!(isYear(elems[2], false) && isMonth(elems[0], false) && isDay(elems[1], false))) return false;
    var month = parseInt(elems[0],10);
    var day = parseInt(elems[1],10);
    var year = parseInt(elems[2],10); 
    if (day > daysInMonth[month]) return false; 
    if ((month == 2) && (day > daysInFebruary(year))) return false;
    return true;
  }

// Returns true if strings s1 & s2 match
function compareStrings (s1, s2) {   
  var doesMatch = false;
  if (s1 == s2) doesMatch = true;
  return doesMatch;
}

// Returns true if string s is a valid state code 
function isStateCode (s) {   
  var USStateCodeDelimiter = "|";
  var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";
  return ((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1));
}

// Returns true if string s is a valid zip code
function isZIPCode (s) {  
  return (isInteger(s) && ((s.length == 5) || (s.length == 9)));
}

// Returns true if string s is a valid US phone number
function isUSPhoneNumber (s) {   
  return (isInteger(s) && s.length == 10)
}

// Functions used to interactively check various form fields
function checkDate (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a value in date format (mm/dd/yyyy or mm/dd/yy).');
      return false;
    }
  }
  if (!isDate(theField.value)) { 
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be in date format (mm/dd/yyyy or mm/dd/yy).');
    return false;
  }
  else return true;
}
  
function checkTime (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a value in time format (hh:mm:ss or hh:mm).');
      return false;
    }
  }
  if (!isTime(theField.value)) { 
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be in time format (hh:mm:ss or hh:mm).');
    return false;
  }    
  else return true;
}

function checkStringInRange (theField, fieldLabel, minValue, maxValue, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a value from ' + minValue + ' to ' + maxValue + ' characters in length.');
      return false;
    }
  }
  if (!isStringInRange(theField.value, minValue, maxValue)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must range from ' + minValue + ' to ' + maxValue + ' characters in length.');
    return false;
  }
  else return true;
}

function checkStringMatch (field1, field2, fieldLabel) {
  if (!compareStrings(field1.value, field2.value)) {
    field1.focus();
	alert(fieldLabel);
    return false; 
  }
  else return true;
}

function checkInteger (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter an integer value.');
      return false;
    }
  }
  if (!isInteger(theField.value)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be an integer.');
    return false;
  }
  else return true;
}
  
function checkNonnegativeInteger (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a non-negative integer value.');
      return false;
    }
  }
  if (!isNonnegativeInteger(theField.value)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be a non-negative integer.');
    return false;
  }  
  else return true;
}
  
function checkPositiveInteger (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a positive integer value.');
      return false;
    }
  }
  if (!isPositiveInteger(theField.value)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be a postive integer.');
    return false;
  }
  else return true;
}
  
function checkIntegerInRange (theField, fieldLabel, minValue, maxValue, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter an integer value between ' + minValue + ' and ' + maxValue + '.');
      return false;
    }
  }
  if (!isIntegerInRange(theField.value, minValue, maxValue)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nThe value entered must be an integer between ' + minValue + ' and ' + maxValue + '.');
    return false;
  }
  else return true;
}

function checkFloat (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a value of type float.');
      return false;
    }
  }
  if (!isFloat(theField.value)) {    
    theField.focus();
    alert(fieldLabel + ' is a required field.\nPlease enter a value of type float.')
    return false;
  }
  else return true;
}

function checkSignedFloat (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a value of type float.');
      return false;
    }
  }
  if (!isSignedFloat(theField.value)) {    
    theField.focus();
    alert(fieldLabel + ' is a required field.\nPlease enter a value of type float.')
    return false;
  }
  else return true;
}

function checkCheckbox (theField, fieldLabel) {
  if (!isCheckboxChecked(theField)) {
    alert(fieldLabel + ' is a required field.\nPlease select at least 1 item.');
    return false;
  }
  else return true;
}    

function checkEmail (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a valid email address.');
      return false;
    }
  } 
  if (!isEmail(theField.value)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nPlease enter a valid email address.');
    return false;
  }    
  else return true;
}

function checkStateCode (theField, fieldLabel, emptyOK) { 
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease select a state.');
      return false;
    }
  }
  theField.value = theField.value.toUpperCase();
  if (!isStateCode(theField.value)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nPlease enter a valid 2 character US state code.');
    return false;
  }
  else return true;
}

function reformat (s) {   
  var arg;
  var sPos = 0;
  var resultString = "";

  for (var i=1; i<reformat.arguments.length; i++) {
    arg = reformat.arguments[i];
    if (i % 2 == 1) resultString += arg;
    else {
      resultString += s.substring(sPos, sPos + arg);
      sPos += arg;
    }
  }
  return resultString;
}

function reformatZIPCode (ZIPString) {   
  if (ZIPString.length == 5) return ZIPString;
  else return (reformat (ZIPString, "", 5, "-", 4));
}

function checkZIPCode (theField, fieldLabel, emptyOK) {   
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a valid zip code.');
      return false;
    }
  }
  var normalizedZIP = stripCharsInBag(theField.value, "-");
  if (!isZIPCode(normalizedZIP)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nPlease enter a valid zip code.');
    return false;
  } 
  else {  
    theField.value = reformatZIPCode(normalizedZIP)
    return true;
  }
} 
  
function reformatUSPhone (USPhone) {   
  return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}

function checkUSPhone (theField, fieldLabel, emptyOK) {
  if (isEmpty(theField.value)) {
    if (emptyOK == true) return true;
    else {
      theField.focus();
      alert(fieldLabel + ' is a required field.\nPlease enter a phone number, including area code.');
      return false;
    }
  }
  var normalizedPhone = stripCharsInBag(theField.value, "()- ");
  if (!isUSPhoneNumber(normalizedPhone)) {
    theField.focus();
    alert('Invalid Field: ' + fieldLabel + '\nPlease enter a valid phone number, including area code.');
    return false;
  }
  else {  
    theField.value = reformatUSPhone(normalizedPhone);
    return true;
  }
}

function checkRadioButton (theField, fieldLabel) {
  var result = false;
  if (theField.length == undefined) {
    if (theField.checked) result = true;
  }
  else {
	for (i=0; i<theField.length; i++) {
      if (theField[i].checked) {
        result = true;
        break;
      }
    }
  }
  if (!result) { 
    alert('Invalid Field: ' + fieldLabel);
  }
  return result;
}