/**
 * @file
 * Custom javascript utilities.
 * 
 * @author Shannon M. Rause <shannon.rause@creativeflavor.com>
 * @version $Revision: 1.13 $
 * @version $Name: CSS21_2010-04-71_2 $
 * @version $Id: main.js,v 1.13 2010/04/11 18:15:16 smr Exp $
 *
 * These files are copyrighted to Creative Flavor Inc. and are
 * subject to the terms of the applicable Service Agreement.
 * If no service agreement is available you must contact us at
 * legal@creativeflavor.com or 303-379-9450.
 * 
 * These files may be watermarked to ensure traceability.
 */
function create_rent_me_discounts_select(defaultDiscountId)
{
   var discountIdSelect = document.getElementById('discountId');
   
   if ((defaultDiscountId == undefined) ||
       (!defaultDiscountId))
   {
      var selected = discountIdSelect.value;
   } // if
   else
   {
      var selected = defaultDiscountId;
   } // else

   Utils.removeChildren(discountIdSelect);
   var elem = document.createElement('option');
   elem.value = '';
   elem.appendChild(document.createTextNode(''));
   discountIdSelect.appendChild(elem);

   var unitId = document.getElementById('unitId').value;
   var i = 0;
   elem = document.getElementById('discountUnitId' + i);

   while (elem)
   {
      if ((elem.value == 'all') ||
          (elem.value == unitId))
      {
         var discountId = document.getElementById('discountId' + i).value;
         var discountName = document.getElementById('discountName' + discountId).value;
         elem = document.createElement('option');
         elem.value = discountId;
         elem.appendChild(document.createTextNode(discountName));

         if (selected == discountId)
         {
            elem.setAttribute('selected',
                              'selected');
         } // if

         discountIdSelect.appendChild(elem);
      } // if

      i = i + 1;
      elem = document.getElementById('discountUnitId' + i);
   } // while
} // create_rent_me_discounts_select


function show_location_units(className)
{
   var trs = document.getElementById('locationTable').getElementsByTagName('tr');
   var count = 0;

   for (var i = 0; i < trs.length; i++)
   {
      if (trs[i].className == className)
      {
         count++;

         // first two are for the more link.
         if (count == 3)
         {
            if (trs[i].style.display == 'none')
            {
               // currently hidden - show.
               var display = '';
            } // if
            else
            {
               // currently shown - hide.
               var display = 'none';
            } // else
         } // if

         if (count > 2)
         {
            trs[i].style.display = display;
         } // if
      } // if
   } // for
} // show_location_units


function update_rent_me_prices()
{
   var unitId = document.getElementById('unitId').value;

   if (unitId)
   {
      var price = parseFloat(document.getElementById('unitPrice' + unitId).value);
      var discountId = document.getElementById('discountId').value;

      if (parseInt(discountId))
      {
         var discountFormula = document.getElementById('discountFormula' + discountId).value;
         var re = /\{total\}/g;
         discountFormula = discountFormula.replace(re, price);
         price = eval(discountFormula);
      } // if

      var administrationFee = 0.0;
      var lockCost = 0.0;

      if (parseFloat(document.getElementById('administrationFee').value))
      {
         var administrationFeeLabel = Utils.formatPrice(document.getElementById('administrationFee').value);
         var administrationFee = parseFloat(document.getElementById('administrationFee').value);
      } // if
      else
      {
         var administrationFeeLabel = 'FREE';
      } // else

      if (parseFloat(document.getElementById('lockCost').value))
      {
         if (parseInt(document.getElementById('lockIsFree').value))
         {
            var lockCostLabel = 'FREE (' + Utils.formatPrice(document.getElementById('lockCost').value) + ' value)';
            var lockCost = 0.0;
         } // if
         else
         {
            var lockCostLabel = Utils.formatPrice(document.getElementById('lockCost').value);
            var lockCost = 0.0;//parseFloat(document.getElementById('lockCost').value);
         } // else
      } // if
      else
      {
         var lockCostLabel = 'FREE';
      } // else

      var unitSalesTax = parseFloat(document.getElementById('unitSalesTaxPercent').value) * price / 100.0;
      var lockSalesTax = 0.0; // parseFloat(document.getElementById('lockSalesTaxPercent').value) * lockCost / 100.0;
      var salesTaxLabel = Utils.formatPrice(Math.round((unitSalesTax + lockSalesTax) * 100.0) / 100.0);
      var total = Math.round((price + unitSalesTax + lockSalesTax + administrationFee + lockCost) * 100.0) / 100.0;
      var totalLabel = Utils.formatPrice(total);
   } // if
   else
   {
      var salesTaxLabel = 'Please select a unit.';
      var administrationFeeLabel = 'Please select a unit.';
      var lockCostLabel = 'Please select a unit.';
      var totalLabel = 'Please select a unit.';
   } // else

   Utils.removeChildren(document.getElementById('salesTaxTd'));
   document.getElementById('salesTaxTd').appendChild(document.createTextNode(salesTaxLabel));

   Utils.removeChildren(document.getElementById('adminstrationFeeTd'));
   document.getElementById('adminstrationFeeTd').appendChild(document.createTextNode(administrationFeeLabel));

   /* leaving in case this needs to be re-added.
   Utils.removeChildren(document.getElementById('lockCostTd'));
   document.getElementById('lockCostTd').appendChild(document.createTextNode(lockCostLabel));
   */

   Utils.removeChildren(document.getElementById('totalTd'));
   document.getElementById('totalTd').appendChild(document.createTextNode(totalLabel));
} // update_rent_me_prices

