/*
  *  Title
  *
  *     common.js
  *
  *  Description
  *
  *     This file provides generic JavaScript functions to be used
  *     in any validation script.
  *
  *  Notes
  *
  *     None
  *
  *  --------------------------------------------------------------
  *
  *  Copyright (c) 1999 AGENCY.COM All Rights Reserved.
  *  This software is the confidential and proprietary information
  *  of AGENCY.COM. ("Confidential Information").  You shall not
  *  disclose such Confidential Information and shall use it only
  *  in accordance with the terms of the license agreement you
  *  entered into with AGENCY.COM
  *
  *  AGENCY.COM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE 
  *  SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
  *  INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-
  *  INFRINGEMENT.  AGENCY.COM SHALL NOT BE LIABLE FOR ANY DAMAGES
  *  SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  *  --------------------------------------------------------------
  */
var required = false;
var notRequired = true;

// whitespace characters
var whitespace = " \t\n\r";
var decimalPointDelimiter = ".";

var daysInMonth = new Array(12);
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;


var iDayPrefix = "The second field in ";
var iDaySuffix = " must be a day number between 1 and 31.";
var iMonthPrefix = "The first field in ";
var iMonthSuffix = " must be a month number between 1 and 12.";
var iYearPrefix = "The third field in ";
var iYearSuffix = " must be a 4 digit year number.";


// Check whether 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 i;

  // Is s empty?
  if (isEmpty(s))
  {
    return true;
  }

  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++)
  {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);

    if (whitespace.indexOf(c) == -1)
    {
      return false;
    }
  }

  // All characters are whitespace.
  return true;
}

function isInteger (s)
{   
  var i;

  if (isEmpty(s))
  {
    if (isInteger.arguments.length == 1)
    {
      return required;
    }
    else
    {
      return notRequired;
    }
  }

  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i);

    if (!isDigit(c))
    {
      return false;
    }
  }

  // All characters are numbers.
  return true;
}

// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s)
{   
  var i;
  var seenDecimalPoint = false;

  if (isEmpty(s))
  { 
    if (isFloat.arguments.length == 1)
    {
      return required;
    }
    else
    {
      return (isFloat.arguments[1] == true);
    }
  }

  if (s == decimalPointDelimiter)
  {
    return false;
  }

  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i);

    if ((c == decimalPointDelimiter) && !seenDecimalPoint)
    {
      seenDecimalPoint = true;
    }
    else if (!isDigit(c))
    {
      return false;
    }
  }

  // All characters are numbers.
  return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"));
}

// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)
{   
  return stripCharsInBag (s, whitespace);
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)
{   
  var i;
  var returnString = "";

  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1)
    {
      returnString+= c;
    }
  }

  return returnString;
}

// isYear (STRING s)
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 4 digits only.
//
//

function isYear (s)
{   
  if (isEmpty(s))
  {
    return false;
  } 
  
  if (!isInteger(s))
  {
    return false;
  }
  return (s.length == 4);
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
//

function isIntegerInRange (s, a, b)
{   
  if (isEmpty(s))
  {
    return false;
  }

  // Catch non-integer strings to avoid creating a NaN below,
  // which isn't available on JavaScript 1.0 for Windows.
  if (!isInteger(s))
  {
    return false;
  }

  // Now, explicitly change the type to integer via parseInt
  // so that the comparison code below will work both on 
  // JavaScript 1.2 (which typechecks in equality comparisons)
  // and JavaScript 1.1 and before (which doesn't).
  // Commented out next five lines and replaced by fixing parseInt call
  //var c = s.charAt(0);
  //if (c == "0")
  //{
  //  s = s.substring(1);
  //}
  var num = parseInt (s, 10);
  return ((num >= a) && (num <= b));
}

// isMonth (STRING s)
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//

function isMonth (s)
{  
  if (isEmpty(s))
  {
    return false;
  }
  else
  {
    return isIntegerInRange (s, 1, 12);
  }
}

// isDay (STRING s)
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 

function isDay (s)
{   
  if (isEmpty(s))
  {
    return false;
  }
  else 
  {   
    return isIntegerInRange (s, 1, 31);
  }
}

// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (month, day, year)
{   
  // catch invalid years (not 4-digit) and invalid months and days.
  if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false)))
  {
    return false;
  }

  // Explicitly change type to integer to make code work in both
  // JavaScript 1.1 and JavaScript 1.2.
  var intYear = parseInt(year, 10);
  var intMonth = parseInt(month, 10);
  var intDay = parseInt(day, 10);

  // catch invalid days, except for February
  if (intDay > daysInMonth[intMonth])
  {
    return false; 
  }

  if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
  {
    return false;
  }

  return true;
}

function selectField (theField)
{
  theField.select();
}

// checkRadioButtonChecked
// 
// Checks if a set of radio buttons has a button checked.

function checkRadioButtonChecked (radio)
{
  for (var i=0; i < radio.length; i++)
  {
    if (radio[i].checked == true)
    {
      return true;
    }
  }
  return false;
}

function getRadioButtonValue (radio)
{
  var found = false;

  for (var i=0; i < radio.length; i++)
  {
    if (radio[i].checked == true)
    {
      found = true;
      break;
    }
  }

  if (found)
  {
    return radio[i].value;
  }
  else
  {
    return "";
  }
}

function isRadioButtonOption (radio, value)
{
  for (var i=0; i < radio.length; i++)
  {
    if (radio[i].value == value)
    {
      return true;
    }
  }
  return false;
}

function setRadioButtonChecked (radio, value)
{
  for (var i=0; i < radio.length; i++)
  {
    if (radio[i].value == value)
    {
      radio[i].checked = true;
      return true;
    }
  }
  return false;
}

function clearRadioButton (radio)
{
  for (var i=0; i < radio.length; i++)
  {
    radio[i].checked = false;
  }
  return;
}

function setRadioButtonState (radio, value, enabled)
{
  for (var i=0; i < radio.length; i++)
  {
    if (radio[i].value == value)
    {
      radio[i].disabled = !enabled;
      return true;
    }
  }
  return false;
}

function getSelectValue (select)
{
  return select.options[select.selectedIndex].value;
}

function stripBegEndSpaces(field)
{
  var pos = 0;
  var interString = "";
  var finalString = "";

  while (pos < field.value.length && field.value.charAt(pos) == " ")
  {
      pos++;
  }
  while (pos < field.value.length)
  {
    interString += field.value.charAt(pos);
    pos++;
  }

  var cnt = interString.length - 1;

  while (cnt > 0 && interString.charAt(cnt) == " ")
  {
    cnt--;
  }

  var index = 0;

  while (index <= cnt)
  {
    finalString += interString.charAt(index);
    index++;
  }
  field.value = finalString;
}

function focusField(field,radio,values)
{
  var value=getRadioButtonValue (radio);
  var a = values.split (';');
  for (i = 0; i < a.length; i++)
  {
    if (a[i] == value)
    {
      return false;
    }
  }

  field.blur ();
  return true;
}


// Log an error if there are any = or | chars in the field

function checkBadCharsField(field, fieldName)
{
  for(var i = 0; i < field.value.length; i++)
  {
    if(field.value.charAt(i) == "=")
    {
      addError(fieldName + " cannot have a '=' character.",fieldName + 'Img');
      break;
    }
    if(field.value.charAt(i) == "|")
    {
      addError(fieldName + " cannot have a '|' character.",fieldName + 'Img');
      break;
    }
  }
  return;
}

// Check for = and | characters in all text values on the form passed

function checkBadCharsForm(form)
{
  for (var i = 0; i < form.elements.length; i++)
  {
    if (form.elements[i].type == "text")
    {
      checkBadCharsField(form.elements[i], form.elements[i].name);
    }
  }
}

// control the display and hiding of arrows for required fields

function toggleArrows(state) {
  if (state == 'off') {
  	if (!imageNames=='') {
      arrayOfImages = imageNames.split(':')
      for (var i=0; i < arrayOfImages.length; i++) {
	    if (document[arrayOfImages[i]]) 
          document[arrayOfImages[i]].src= "/images/clearpix.gif"
      }
    }
    imageNames = '';
  } else {
    if (!imageNames=='') {
      arrayOfImages = imageNames.split(':')
      for (var i=0; i < arrayOfImages.length; i++) {
        if (document[arrayOfImages[i]]) 
          document[arrayOfImages[i]].src= "/images/arrow.gif";
      }
    }
  }
}
