/******************************
  Table of Contents
 ******************************
  1. Global Variables
  2. Functions
    - addToTotal()
    - changeVeh()
    - clearTextInput()
    - setInput()
    - hideElementByName()
    - hideElementById()
    - isAddrElement()
    - openWindow()
    - showArea()
    - noShowArea()
    - storeFormValues()
    - toggleUseAddress()
    - togglePayment()
    - updateLength()
    - viewLargerImage()
    - fixFFScrollBug()
    - jumpto()

**********/

/**
  Global Variables and their usages
**/

/**
 * REGEXP used with viewLargerImages to change the urls for the images from the small size to the
 * larger size.
**/
var smallSizes = /\/1.0\//;

/**
 * Used with toggleUseAddress() this one stores the default billing address.
 */
var storedAddrInfo;

/**
 * Used with toggleUseAddress() this stores the last changes the
 * user made for a different billing address.
 */
var storedUserAddr = new Array();



/**
 * this function reads the value of element (which should be a checkbox) and
 * adds price * 2 to the total price of the currently selected tire/wheel
 * package if the element is checked, or subtracts price * 2 from the total if
 * element is unchecked, returns new total.
 */
function addToTotal(price, element)
{
  var total = (document.getElementsByName("total"))[0];
  total.readOnly = false;
  var t = total.value;
  price *= 2;
  if( t.charAt(0) == '$' )
  {
    t = parseFloat(t.substring(1));
  }
  else
  {
    t = parseFloat(t);
  }

  if( element.checked )
  {
    t += price;
  }
  else
  {
    t -= price;
  }
  total.value = '$' + t.toFixed(2).toString();
  total.readOnly = true;
}


/**
 * This function adds a fragment identifier to the form's action field, so as to
 * put the vehicle select form at the top of the page.
 */
function addVehicleSelectFragment(form)
{
  form.action += "#vehicleSelect";
}

/**
 * This function is used by vehicleSelect.jsp to cause the year combo box to
 * auto submit
 */
function changeVeh(form, val)
{
  var hid = document.getElementById("appFilterHidden");
  hid.value = val;
  hid.disabled = false;
}



/**
 * This function simply clears the value of a <input type="text"> element.
 *
 * @param element - the element whose value will be cleared.
 */
function clearTextInput(element)
{
  element.value = "";
}


/**
 * Given the id of checkbox or radio button this function selects/checks that input
 * Used places where more than one input element has information to be entered, this can
 * automatically select an associated radio or checkbox.
 *
 * @param element - the element that will be selected/checked
 */
function setInput(elementid)
{
  var element = document.getElementById(elementid);
  if (element && (element.type == "checkbox" || element.type == "radio"))
  {
      element.checked = true;
  }
}


/* These functions can be used to hide elements.
They was designed to hide submit buttons on forms that use eventhandlers to submit the form.
The first takes as an input the name of the element to hide, the second takes the id. */
function hideElementByName(element)
{
  var es = document.getElementsByName(element);
  var len = es.length;
  for( var i = 0; i < len; i++ )
  {
    var e = es[i];
    e.style.display = "none";
  }
}


function hideElementById(element)
{
  var e = document.getElementById(element);
  e.style.display = "none";
}


/** Returns true if element is of type 'text', 'select-one', or
 *  select-multiple' or if it's one of the address type radio buttons
*/
function isAddrElement(element)
{
  var sel = "select-";
  var type = String(element.type)
  var TorS = type == "text" || type.substring(0,sel.length) == sel;
  var isAddType = element.className == "addrType" ? true : false;
  return TorS || isAddType;
}


function openWindow(theURL, winName, features)
{
  window.open(theURL, winName, features);
}


/**
 * Stores the (text and select) field values from the passed in form for later
 * retreival.
 * Returns an Array
 */
function storeFormValues(form)
{
  var a = Array(form.length);

  for( var i = 0; i < form.length; i++ )
  {
    if( isAddrElement(form.elements[i]) )
    {
      if( form.elements[i].value )
      {
        a[i] = form.elements[i].value;
      }
      else
      {
        a[i] = "";
      }
    }
  }
  return a;
}


/**
 * toggleUseAddress()  the value of the checkbox
 *
 * form is the form to manipulate
 * el is the element controlling the toggle
 */
function toggleUseAddress(form, el)
{
  // store form values if this is the first time this method has run
  // note storedAddrInfo is global
  if( !storedAddrInfo && el.checked )
  {
    storedAddrInfo = storeFormValues(form);
  }

  if( !el.checked )
  {
    /* if checked then use default address for address */
    /* for each input in form disable and populate from stored values (if any) */
    for( var i = 0; i < form.length; i++ )
    {
      var e = form.elements[i];
      if( isAddrElement(e) && e.name != "pwdHint" )
      {
        storedUserAddr[i] = e.value;
        if( storedAddrInfo )
        {
          e.value = storedAddrInfo[i];
        }
        else
        {
          e.value = "";
        }
        e.disabled = true;
      }
    }
  }
  else
  {
    /* for each input in form store value, clear and enable */
    for( var i = 0; i < form.length; i++ )
    {
      var e = form.elements[i];
      if( isAddrElement(e) )
      {
        if( storedUserAddr[i] )
        {
          e.value = storedUserAddr[i];
        }
        else
        {
          e.value = "";
        }
        e.disabled = false;
      }
    }
  }
}


function togglePayment()
{
  var bml = document.getElementById("PayBML");
  var cc = document.getElementById("PayCC");
  var pp = document.getElementById("PayPaypal");

  var ccnum = document.getElementById("CCnum");
  var months = document.getElementById("CCmonths");
  var years = document.getElementById("CCyears");
  var cvv = document.getElementById("CCcvv");

  if (bml)
  {
    if (bml.checked)
    {
      ccnum.disabled = true;
      months.disabled = true;
      years.disabled = true;
      cvv.disabled = true;
    }
  }

  if (cc)
  {
    if (cc.checked)
    {
      ccnum.disabled = null;
      months.disabled = null;
      years.disabled = null;
      cvv.disabled = null;
    }
  }

  if (pp)
  {
    if (pp.checked)
    {
      ccnum.disabled = true;
      months.disabled = true;
      years.disabled = true;
      cvv.disabled = true;
    }
  }
}


/**
 * Assuming there is an image element called LargerImageImg on any page from which this function is
 * used, this function replaces the source of that element with the source from the given image element.
 *
 */
function viewLargerImage(thumb)
{
  if (thumb)
  {
    var larger = document.getElementById("LargerImageImg");
    if (larger)
    {
      //var s = thumb.src;
      larger.src = thumb.src.replace(smallSizes, "/400/");
      //larger.src = s;
    }
  }
}


/**
 * Used to scroll the page to specific location after loading, so the customer doesn't have to. *
 */
function jumpto(targetAnchor)
{
  var targAnc = document.getElementsByName(targetAnchor);
  if (targAnc)
  {
    window.location.hash = targetAnchor;
  }
}