/**
 * @file
 * Custom javascript utilities.
 * 
 * @author Shannon M. Rause <shannon.rause@creativeflavor.com>
 * @version $Revision: 1.18 $
 * @version $Name: STOR-N-LOCK_2011-10-11_12 $
 * @version $Id: main.js,v 1.18 2010/08/25 17:53:11 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.
 */
g_location_tab_directions_initialized = false;
g_location_tab_photos_initialized = false;
g_location_tab_video_initialized = false;

function calculate(good, empty, too_big)
{
   var form = document.forms['calculatorForm'];
   var total = parseFloat(form.elements['other'].value);

   if (isNaN(total))
   {
      total = 0.0;
   } // if

   for (var i = 0; i < form.elements.length; i++)
   {
      if ((form.elements[i].type == 'text') &&
            (form.elements[i].name != 'other'))
      {
         var qty = parseInt(form.elements[i].value);

         if ((!isNaN(qty)) &&
               (qty > 0))
         {
            var temp = form.elements[i].name.split('_', 2);
            var f = parseFloat(temp[1].replace(/_/g, '.'));

            if (!isNaN(f))
            {
               total += (f * qty);
            } // if
         } // if
      } // if
   } // for

   var s = good;
   var rec = '10x30 or 15x20';

   if (total > 2400.0)
   {
      s = too_big;
   } // if
   else if (total > 1800.0)
   {
      rec = '10x30 or 15x20';
   } // else if
   else if (total > 1500.0)
   {
      rec = '10x30 or 15x20';
   } // else if
   else if (total > 1200.0)
   {
      rec = '10x25';
   } // else if
   else if (total > 900.0)
   {
      rec = '10x20';
   } // else if
   else if (total > 600.0)
   {
      rec = '10x15';
   } // else if
   else if (total > 450.0)
   {
      rec = '10x10';
   } // else if
   else if (total > 300.0)
   {
      rec = '5x15';
   } // else if
   else if (total > 150.0)
   {
      rec = '5x10';
   } // else if
   else if (total > 0.0)
   {
      rec = '5x5';
   } // else if
   else
   {
      s = empty;
   } // else

   s = s.replace(/\[\[cubicFeet\]\]/g, total);
   s = s.replace(/\[\[recommend\]\]/g, rec);
   document.getElementById('results').innerHTML = s;
   Popup.show('resultsPopup');
   return (false);
} // calculate


function change_location(select)
{
   if (select.value.charAt(0) == '_')
   {
      document.location = '/locations/' + select.value.substring(1);
   } // if
   else
   {
      document.location = '/' + select.value;
   } // else
} // change_location


function change_location_tab(id)
{
   var all_tabs = new Array(
      'locationTabDetails',
      'locationTabPhotos',
      'locationTabDirections',
      'locationTabVideo');
   var existing_tabs = new Array();

   for (var i=0; i<all_tabs.length; i++)
   {
      if (document.getElementById(all_tabs[i]))
      {
         existing_tabs[existing_tabs.length] = all_tabs[i];
      } // if
   } // for

   if (initialize_location_tab(existing_tabs, id))
   {
      Utils.changeTab(existing_tabs,
                      id);
   } // if
} // change_location_tab


function change_photo(src,
                      width,
                      height,
                      caption)
{
   var img = document.getElementById('locationTabLgImageImg');
   img.src = src.replace('#', '%23');
   img.width = width;
   img.height = height;
   var div = document.getElementById('locationTabLgImageCaption');
   Utils.removeChildren(div);
   div.appendChild(document.createTextNode(caption));
} // change_photo


function initialize_location_tab(ids,
                                 id)
{
   switch (id)
   {
      case 'locationTabDirections':
//         if (!g_location_tab_directions_initialized)
//         {
            document.getElementById('locationMapWrapper').innerHTML = g_location_map_code;
            g_location_tab_directions_initialized = true;
//         } // if
         break;

      case 'locationTabPhotos':
         if (!g_location_tab_photos_initialized)
         {
            g_location_tab_photos_initialized = true;
         } // if
         break;

      case 'locationTabVideo':
         if (!g_location_tab_video_initialized)
         {
            g_location_tab_video_initialized = true;
         } // if
         break;

      default:
         break;
   } // switch

   return (true);
} // initialize_location_tab


function onload_location(active)
{
//   if (!g_location_tab_directions_initialized)
//   {
      document.getElementById('locationMapWrapper').innerHTML = g_location_map_code;
      g_location_tab_directions_initialized = true;
//   } // if
} // onload_location


function submit_search()
{
   var select = document.getElementById('search_location');

   if (!select.options[select.selectedIndex].value)
   {
      return (true);
   } // if

   return (select.options[select.selectedIndex].value == 'Select Location');
} // submit_search


function update_photo_caption(i)
{
   var elem = document.getElementById('photoCaptions' + i);

   if ((elem) &&
       (!elem.value))
   {
      elem.value = 'N/A';
   } // if
} // update_photo_caption

