/* ----------------------------------------------------------------
 * $Id: utils.js,v 1.3 2009/03/06 19:32:07 dgrasse Exp $
 * $Author: dgrasse $
 *
 * ThotWave Toolbox Component.  support@thotwave.com
 * Copyright 2002 ThotWave Technologies, LLC. All rights reserved.
 * ---------------------------------------------------------------- */

          /** PUBLIC METHODS **/

//  THIS SECTION CONTAINS GENERAL FUNCTIONS //
  /*
   *  This method changes the source of the specified image.
   */
function ModImg (imageName, imageSrc)
{
  var theImage = document.images[imageName];
  
  theImage.src = imageSrc;
}

function ModImgStatus (imageName, imageSrc, theStatus)
{
  ModImg (imageName, imageSrc);
  
  window.top.status = theStatus;
}

/**
 *  Return true if at least one of the checkbox (single widget or array) is checked.
 */
function isCheckboxChecked (theCheckbox)
{
    if (theCheckbox.length)
    {
        for (var i = 0; i < theCheckbox.length; i++)
        {
            if (isCheckboxChecked(theCheckbox[i]))
            {
                return true;
            }
        }
        return false;
    }
    else
    {
        return theCheckbox.checked;
    }
}

  /*
   *  This method changes the color of the specified item
   *    to the specified color.
   */
function ChangeColor (item, color)
{
  element = document.all (item);
  element.style.color = color;
}

  /*
   *  This method changes to location of the current window
   *    to the specified url.
   */
function GoURL (theURL)
{
  window.location = theURL;
}

//  THIS SECTION CONTAINS FORM-RELATED FUNCTIONS  //
  /*
   *  This method correctly removes all Option's from theSelect.
   */
function ClearSelect (theSelect)
{
  for (var clearSelectIndex = theSelect.options.length-1; clearSelectIndex >= 0; clearSelectIndex--)
  {
    theSelect.options[clearSelectIndex] = null;
  }
}

  /*
   *  This method performs the following:
   *    - calls #clearSelect (theSelect)
   *    - creates an Option from each OptData in theOptDataArray
   *    - adds these Option's to theSelect
   */
function PopulateSelect (theSelect, theOptDataArray)
{
  clearSelect (theSelect);
  for (var populateSelectIndex = 0; populateSelectIndex < theOptDataArray.length; populateSelectIndex++)
  {
    theOptData = theOptDataArray[populateSelectIndex];
    theOption = new Option (theOptData.text, theOptData.value);
    
    theSelect.options[populateSelectIndex] = theOption;
  }
  theSelect.options[0].selected = true;
}

  /*
   *  Object that holds Option data.
   *  This object is used when the same Option
   *    is needed in more than one Select.
   */
function OptData (theText, theValue)
{
  this.text = theText;
  this.value = theValue;
}

//  THIS SECTION CONTAINS NUMBER/DATE-RELATED FUNCTIONS  //

  /*
   *  This function returns if 'theChar' is a numeric character
   */
function IsDigit (theChar)
{
  return ((theChar >= '0') && (theChar <= '9'));
}

  /*
   *  This function returns if the value is a numeric (integer or float)
   */
function IsNumeric (theValue)
{
  if (theValue == "")
  {
    return false;
  }
  else
  {
    return (theValue == (parseFloat (theValue)));
  }
}

    /*
     *  This function verifies to format
     *      99999 or 99999-9999
     */
function VerifyZipFormat (theZipValue)
{
    var zipLen = theZipValue.length;
    
    if ((zipLen != 5) && (zipLen != 10))
    {
        return false;
    }
    
    for (var i = 0; i < 5; i++)
    {
        if (!IsDigit (theZipValue.charAt (i)))
        {
            return false;
        }
    }
    if (zipLen == 10)
    {
        if (theZipValue.charAt (5) != '-')
        {
            return false;
        }
        for (var i = 6; i < 10; i++)
        {
            if (!IsDigit (theZipValue.charAt (i)))
            {
                return false;
            }
        }
    }
    
    return true;
}

    /*
     *  This function verifies to format 'X@Y.Z',
     *      where X, Y, and Z are at least one character,
     *      and no whitespace.
     */
function VerifyEmailFormat (theEmail)
{
    var theEmailLen = theEmail.length;
    if (theEmailLen < 5)
    {
        // minimal length error
        return false;
    }
    if (theEmail.indexOf (" ") != -1)
    {
        // whitespace error
        return false;
    }
    
    var firstAt = theEmail.indexOf ("@");
    var lastAt = theEmail.lastIndexOf ("@");
    if ((firstAt <= 0) ||
        (firstAt != lastAt)
       )
    {
        // badly placed @
        return false;
    }
    
    var lastDot = theEmail.lastIndexOf (".");
    if ((lastDot <= firstAt+1) ||
        (lastDot == theEmailLen-1)
       )
    {
        // badly placed .
        return false;
    }
    
    return true;
}

  /*
   *  This function verifies to any formats below.
   */
function VerifyPhoneFormat (thePhoneValue)
{
    return ((VerifyDashPhoneFormat (thePhoneValue)) ||
            (VerifyParenPhoneFormat (thePhoneValue)) ||
            (VerifyParenSpacePhoneFormat (thePhoneValue))
           );
}

  /*
   *  This function verifies to format '999-999-9999'.
   *    All trailing characters are ignored.
   */
function VerifyDashPhoneFormat (thePhoneValue)
{
  if (thePhoneValue.length < 12)
  {
    return false;
  }
  
  if ((!IsDigit (thePhoneValue.charAt (0))) ||
      (!IsDigit (thePhoneValue.charAt (1))) ||
      (!IsDigit (thePhoneValue.charAt (2))) ||
      (thePhoneValue.charAt (3) != '-') ||
      (!IsDigit (thePhoneValue.charAt (4))) ||
      (!IsDigit (thePhoneValue.charAt (5))) ||
      (!IsDigit (thePhoneValue.charAt (6))) ||
      (thePhoneValue.charAt (7) != '-') ||
      (!IsDigit (thePhoneValue.charAt (8))) ||
      (!IsDigit (thePhoneValue.charAt (9))) ||
      (!IsDigit (thePhoneValue.charAt (10))) ||
      (!IsDigit (thePhoneValue.charAt (11)))
     )
  {
    return false;
  }
  
  return true;
}

  /*
   *  This function verifies to format '(999)999-9999'.
   *    All trailing characters are ignored.
   */
function VerifyParenPhoneFormat (thePhoneValue)
{
  if (thePhoneValue.length < 13)
  {
    return false;
  }
  
  if ((thePhoneValue.charAt (0) != '(') ||
      (!IsDigit (thePhoneValue.charAt (1))) ||
      (!IsDigit (thePhoneValue.charAt (2))) ||
      (!IsDigit (thePhoneValue.charAt (3))) ||
      (thePhoneValue.charAt (4) != ')') ||
      (!IsDigit (thePhoneValue.charAt (5))) ||
      (!IsDigit (thePhoneValue.charAt (6))) ||
      (!IsDigit (thePhoneValue.charAt (7))) ||
      (thePhoneValue.charAt (8) != '-') ||
      (!IsDigit (thePhoneValue.charAt (9))) ||
      (!IsDigit (thePhoneValue.charAt (10))) ||
      (!IsDigit (thePhoneValue.charAt (11))) ||
      (!IsDigit (thePhoneValue.charAt (12)))
     )
  {
    return false;
  }
  
  return true;
}

  /*
   *  This function verifies to format '(999) 999-9999'
   *    (notice the ' ' after the closing paren).
   *    All trailing characters are ignored.
   */
function VerifyParenSpacePhoneFormat (thePhoneValue)
{
  if (thePhoneValue.length < 14)
  {
    return false;
  }
  
  if ((thePhoneValue.charAt (0) != '(') ||
      (!IsDigit (thePhoneValue.charAt (1))) ||
      (!IsDigit (thePhoneValue.charAt (2))) ||
      (!IsDigit (thePhoneValue.charAt (3))) ||
      (thePhoneValue.charAt (4) != ')') ||
      (thePhoneValue.charAt (5) != ' ') ||
      (!IsDigit (thePhoneValue.charAt (6))) ||
      (!IsDigit (thePhoneValue.charAt (7))) ||
      (!IsDigit (thePhoneValue.charAt (8))) ||
      (thePhoneValue.charAt (9) != '-') ||
      (!IsDigit (thePhoneValue.charAt (10))) ||
      (!IsDigit (thePhoneValue.charAt (11))) ||
      (!IsDigit (thePhoneValue.charAt (12))) ||
      (!IsDigit (thePhoneValue.charAt (13)))
     )
  {
    return false;
  }
  
  return true;
}
 
  /*
   *  This function verifies to format 'mm/dd/yyyy'
   */
function VerifyDateFormat (theDateValue)
{
  if (theDateValue.length != 10)
  {
    return false;
  }
  
  if ((!IsDigit (theDateValue.charAt (0))) ||
      (!IsDigit (theDateValue.charAt (1))) ||
      (theDateValue.charAt (2) != '/') ||
      (!IsDigit (theDateValue.charAt (3))) ||
      (!IsDigit (theDateValue.charAt (4))) ||
      (theDateValue.charAt (5) != '/') ||
      (!IsDigit (theDateValue.charAt (6))) ||
      (!IsDigit (theDateValue.charAt (7))) ||
      (!IsDigit (theDateValue.charAt (8))) ||
      (!IsDigit (theDateValue.charAt (9)))
     )
  {
    return false;
  }
  
  return true;
}

  /*
   *  This function verifies to format 'mm/01/yyyy'
   */
function VerifyMonthFormat (theDateValue)
{
  if (theDateValue.length != 10)
  {
    return false;
  }
  
  if ((!IsDigit (theDateValue.charAt (0))) ||
      (!IsDigit (theDateValue.charAt (1))) ||
      (theDateValue.charAt (2) != '/') ||
      (theDateValue.charAt (3) != '0') ||
      (theDateValue.charAt (4) != '1') ||
      (theDateValue.charAt (5) != '/') ||
      (!IsDigit (theDateValue.charAt (6))) ||
      (!IsDigit (theDateValue.charAt (7))) ||
      (!IsDigit (theDateValue.charAt (8))) ||
      (!IsDigit (theDateValue.charAt (9)))
     )
  {
    return false;
  }
  
  return true;
}

  /*
   *  This function expects two 'mm/dd/yyyy' formats
   *    and returns 'preDateValue <= postDateValue'
   */
function CompareDates (preDateValue, postDateValue)
{
  var preDateMonth = preDateValue.substr (0, 2);
  var preDateDate = preDateValue.substr (3, 2);
  var preDateYear = preDateValue.substr (6, 4);
  
  var postDateMonth = postDateValue.substr (0, 2);
  var postDateDate = postDateValue.substr (3, 2);
  var postDateYear = postDateValue.substr (6, 4);
  
  if ((preDateYear > postDateYear)
      ||
      ((preDateYear == postDateYear) &&
       (preDateMonth > postDateMonth)
      )
      ||
      ((preDateYear == postDateYear) &&
       (preDateMonth == postDateMonth) &&
       (preDateDate > postDateDate)
      )
     )
  {
    return false;
  }
  
  return true;
}

//  THIS SECTION CONTAINS STRING-RELATED FUNCTIONS  //

    /**
     *  This function trims the argument and returns a new value.
     */
function TrimString (theString)
{
    if (theString.length == 0)
    {
        return theString;
    }
    
    var firstSpace = theString.indexOf (" ");
    while (firstSpace == 0)
    {
        theString = theString.substr (1);
        firstSpace = theString.indexOf (" ");
        
        if (theString.length == 0)
        {
            return theString;
        }
    }
    
    var lastSpace = theString.lastIndexOf (" ");
    var lastCharIndex = theString.length-1;
    while (lastSpace == lastCharIndex)
    {
        theString = theString.substring (0, lastCharIndex);
        lastSpace = theString.lastIndexOf (" ");
        lastCharIndex = theString.length-1;
        
        if (theString.length == 0)
        {
            return theString;
        }
    }
    
    return theString;
}

var MAX_ERRORS=10;
var _ERRORS=new Array();
function TrackError(theMsg)
{
    _ERRORS.push(theMsg);
}

/**
 *  If any ERRORs, shows alert with MAX_ERRORS and returns false,
 *      otherwise returns true (for form submission assistance).
 */
function ShowErrors()
{
    if (_ERRORS.length > 0)
    {
        var alertMsg = 'There are '+_ERRORS.length+' errors.\n\n';
        
        var maxERRORs = (_ERRORS.length < MAX_ERRORS)? _ERRORS.length : MAX_ERRORS;
        for (var i = 0; i < maxERRORs; i++)
        {
            alertMsg += (_ERRORS[i] + '\n');
        }
        alert (alertMsg);
        _ERRORS=[];
        
        return false;
    }
    else
    {
        return true;
    }
}

