//
// Copyright 2002 Sensible Internet Retail, Inc.
// Author: Matthew V. Maloney except findPos() from quirksmode
//

/* fp.js for wasatch layered floorplans
***/


// use this global var to recognize which checkbox was actually 
// click to make it immune from being affected by any hide, require, 
// or chain attributes.
var firestarter = '';


function findPos(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

function option_selected_by_id(option_id)
{
  var option_elem = document.getElementById(option_id);
  return (option_elem.style.visibility && ("hidden" != option_elem.style.visibility));
}

function update_level_stats()
{
  if (document.getElementById('fp_stats'))
  {
    var es = document.getElementById('fp_stats').innerHTML;
    es = es.replace(/dimensions/, 'fly');
    es = es.replace(/Total Sq\. Ft\./, 'Total');
    es = es.replace(/Finished Sq\. Ft\./, 'Fin.');
    es = es.replace(/Unfinished Sq\. Ft\./, 'Unf.');
    es = es.replace(/Width/, 'Width');
    es = es.replace(/Depth/, 'Depth');
    es = es.replace(/id=\".*?\"/g, '');
    var level_stat_elems = document.getElementsByName('fp_level_stats');
    for (var i=0; i < level_stat_elems.length; i++)
    {
      var e = level_stat_elems[i];
      e.innerHTML = es;
    }
  }
}

function fp_show_option(base_id, option_id)
{
  //alert('showing '+option_id);

  // get references to the base image element and the option's checkbox element.
  var b = document.getElementById(base_id);
  var o = document.getElementById(option_id);

  // get a reference to the input element that controls this option.
  //var chk_option_id = option_id.replace(/fp_/, 'fp_chk_');
  //var chko = document.getElementById(chk_option_id);
  var chko = document.getElementById(option_id.replace(/fp_/, 'fp_chk_'));

  // make sure the checkbox is checked.
  chko.checked = true;

  // set the position and show the option's image element.
  var pos = findPos(b);
  o.position = 'absolute';
  o.style.left = pos[0];
  o.style.top = pos[1];
  o.style.visibility = 'visible';
  //o.className = 'fp_option_show';

  // see if this option has any effect on finished/unfinished sqft or the width/depth totals.
  // update the finished/unfinished sqft and/or the width/depth totals.

  // look for difference to floorplan finshed sqft
  //var finsqft_diff = 1*(chko.attributes['finsqft_diff']?chko.attributes['finsqft_diff'].value:0);
  var finsqft_diff = 1*(chko.getAttribute('finsqft_diff')?chko.getAttribute('finsqft_diff'):0);
  if (finsqft_diff)
  {
    //alert('finsqft_diff='+finsqft_diff);
    var e = document.getElementById('fp_finsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue + 1*finsqft_diff;
  }

  // look for difference to floorplan unfinshed sqft
  var unfinsqft_diff = 1*(chko.getAttribute('unfinsqft_diff')?chko.getAttribute('unfinsqft_diff'):0);
  if (unfinsqft_diff)
  {
    //alert('unfinsqft_diff='+unfinsqft_diff);
    var e = document.getElementById('fp_unfinsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue + 1*unfinsqft_diff;
  }

  // if either the finished or unfinished sqft were just changed, then change the total square feet too.
  if (finsqft_diff || unfinsqft_diff)
  {
    //alert('finsqft_diff='+finsqft_diff+"\n"+'unfinsqft_diff='+unfinsqft_diff);
    var e = document.getElementById('fp_totsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue + 1*finsqft_diff + 1*unfinsqft_diff;
  }

  // look for difference to floorplan width
  var width_diff = 1*(chko.getAttribute('width_diff')?chko.getAttribute('width_diff'):0);
  if (width_diff)
  {
    //alert('width_diff='+width_diff);
    var e = document.getElementById('fp_width_feet');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue + 1*width_diff;
  }

  // look for difference to floorplan depth
  var depth_diff = 1*(chko.getAttribute('depth_diff')?chko.getAttribute('depth_diff'):0);
  if (depth_diff)
  {
    //alert('depth_diff='+depth_diff);
    var e = document.getElementById('fp_depth_feet');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue + 1*depth_diff;
  }

  // update any stats tables that exist for each level.
  update_level_stats();

  // look at this option's conflicting options ('hide_options')
  var turn_off_options_list = (chko.getAttribute('hide_options')?chko.getAttribute('hide_options'):'');
  if (turn_off_options_list)
  {
    //alert('turn_off_options_list='+turn_off_options_list);
    var turn_off_options = turn_off_options_list.split(",");
    if (turn_off_options)
    {
      for (var i=0; i < turn_off_options.length; i++)
      {
        var turn_off_option_id = turn_off_options[i];
        var turn_off_option_elem = document.getElementById(turn_off_options[i]);
        if (turn_off_option_elem.style.visibility && ("hidden" != turn_off_option_elem.style.visibility))
        {
          fp_hide_option(turn_off_options[i]);
        }
      }
    }
  }

  // if the checkbox element for option_id contains a 'require_options' attribute,
  // then turn on those options, so long as they are not already turned on.
  var required_options_list = (chko.getAttribute('require_options')?chko.getAttribute('require_options'):'');
  if (required_options_list)
  {
    //alert('required_options_list='+required_options_list);
    var required_options = required_options_list.split(",");
    if (required_options)
    {
      var required_option_id;
      var img_required_option;
      var chk_required_option;
      for (var i=0; i < required_options.length; i++)
      {
        // get the id of this required option from the required_options array.
        required_option_id = required_options[i];

        // see if the required_option_id has multiple id's (as in delimitted with pipe).
        if (required_option_id.match(/\|/))
        {
          // this means that only one of the included option names are required.
          piped_option_ids = required_option_id.split(/\|/);
          var piped_option_id;
          var found_one_selected_option = false;
          for (var j=0; j < piped_option_ids.length; j++)
          {
            piped_option_id = piped_option_ids[j];
            if (option_selected_by_id(piped_option_id))
            {
              found_one_selected_option = true;
            }
          }
          if (found_one_selected_option)
          {
            // we found one of the required options to already be selected.
            // we don't need to auto-select any other options for this required_option_id
            break;
          }

          // if we got this far, then we need to show the first option listed in the piped-list.
          required_option_id = piped_option_ids[0];
        }

        // get references to the option's image and the option's checkbox.
        img_required_option = document.getElementById(required_option_id);
        chk_required_option = document.getElementById(required_option_id.replace(/fp_/, 'fp_chk_'));

        // if the option is not already selected, then
        // 1. select it, and
        // 2. make note of it in the attributes for the original option_id's checkbox element.
        if (!chk_required_option.checked && (required_option_id != firestarter))
        {
          // get a reference to the base_id of the required_option.
          var required_option_base_id = (chk_required_option.getAttribute('base_id')?chk_required_option.getAttribute('base_id'):'');
          // if no base_id for the required option was specified, assume this option is on the same level as it's partner option.
          if (!required_option_base_id) { required_option_base_id = base_id; }

          // check and disable the required option's checkbox.
          chk_required_option.checked = true;
          //chk_required_option.disabled = true;

          // show the required option.
          fp_show_option(required_option_base_id, required_option_id)

          // add this required_option's id to the attributes
          // of the original option_id's checkbox element.
          // name of the attribute is 'turned_on_options'.
          var turned_on_options = (o.getAttribute('turned_on_options')?o.getAttribute('turned_on_options'):'');
          o.setAttribute("turned_on_options", (turned_on_options?turned_on_options+',':'') + required_option_id);
        }

      }
    }
  }
}

function fp_hide_option(option_id)
{
  //alert('hiding '+option_id);

  // get reference to the option's checkbox element.
  var o = document.getElementById(option_id);

  // hide the requested option.
  o.style.visibility = 'hidden';
  //o.className = 'fp_option_hide';

  // get a reference to the input element that controls this option.
  var chko = document.getElementById(option_id.replace(/fp_/, 'fp_chk_'));

  // make sure the option checkbox is unchecked.
  chko.checked = false;

  // see if this option has any effect on finished/unfinished sqft or the width/depth totals.
  // update the finished/unfinished sqft and/or the width/depth totals.

  // look for difference to floorplan finshed sqft
  var finsqft_diff = 1*(chko.getAttribute('finsqft_diff')?chko.getAttribute('finsqft_diff'):0);
  if (finsqft_diff)
  {
    //alert('finsqft_diff='+finsqft_diff);
    var e = document.getElementById('fp_finsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue - 1*finsqft_diff;
  }

  // look for difference to floorplan unfinshed sqft
  var unfinsqft_diff = 1*(chko.getAttribute('unfinsqft_diff')?chko.getAttribute('unfinsqft_diff'):0);
  if (unfinsqft_diff)
  {
    //alert('unfinsqft_diff='+unfinsqft_diff);
    var e = document.getElementById('fp_unfinsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue - 1*unfinsqft_diff;
  }

  // if either the finished or unfinished sqft were just changed,
  // then change the total square feet too.
  if (finsqft_diff || unfinsqft_diff)
  {
    //alert('finsqft_diff='+finsqft_diff+"\n"+'unfinsqft_diff='+unfinsqft_diff);
    var e = document.getElementById('fp_totsqft');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue - 1*finsqft_diff - 1*unfinsqft_diff;
  }

  // look for difference to floorplan width
  var width_diff = 1*(chko.getAttribute('width_diff')?chko.getAttribute('width_diff'):0);
  if (width_diff)
  {
    //alert('width_diff='+width_diff);
    var e = document.getElementById('fp_width_feet');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue - 1*width_diff;
  }

  // look for difference to floorplan depth
  var depth_diff = 1*(chko.getAttribute('depth_diff')?chko.getAttribute('depth_diff'):0);
  if (depth_diff)
  {
    //alert('depth_diff='+depth_diff);
    var e = document.getElementById('fp_depth_feet');
    e.firstChild.nodeValue = 1*e.firstChild.nodeValue - 1*depth_diff;
  }

  // update any stats tables that exist for each level.
  update_level_stats();

  // also hide any options that this option originally turned on.
  var turned_on_options_list = (o.getAttribute('turned_on_options')?o.getAttribute('turned_on_options'):'');
  //alert('turned_on_options_list='+turned_on_options_list);

  // add the permanently specified me_hiding_chain list to the turned_on_options list
  var me_hiding_chain = (chko.getAttribute('me_hiding_chain')?chko.getAttribute('me_hiding_chain'):'');
  //alert('me_hiding_chain='+me_hiding_chain);

  // combine the lists. it doesn't matter if some elements are specified twice.
  var turn_off_options_list = turned_on_options_list + ((turned_on_options_list && me_hiding_chain)?',':'') + me_hiding_chain;

  // if there is a list of options to turn off, do it.
  if (turn_off_options_list)
  {
    var turn_off_options = turn_off_options_list.split(",");
    if (turn_off_options)
    {
      for (var i=0; i < turn_off_options.length; i++)
      {
        var turn_off_option_id = turn_off_options[i];
        var turn_off_option_chk_id = turn_off_option_id.replace(/fp_/, 'fp_chk_');
        var turn_off_option_chk = document.getElementById(turn_off_option_chk_id);

        // reenable this checkbox.
        //turn_off_option_chk.disabled = false;

        // if the option is currently selected, deselect it.
        if (turn_off_option_chk.checked && (turn_off_option_id != firestarter))
        {
          //turn_off_option_chk.checked = false;
          //alert('hiding ' + turn_off_option_id);
          fp_hide_option(turn_off_option_id);
        }
      }
    }
  }

  // clear the turned_on_options attribute because they have all been turned off now.
  o.removeAttribute("turned_on_options");
}

function fp_reset_options()
{
  var e = document.getElementsByTagName('input');
  for (var i=0; i < e.length; i++)
  {
    if (e[i].checked) e[i].checked = false;
    /*
    if (e[i].checked)
    {
      var gif = e[i].id.replace(/chk_/, '');
      fp_show_option(gif);
    }
    */
  }
}

function toggle_option(base_id, input_ref)
{
  //var option_id = input_ref.attributes['option_id'].value;
  var option_id = input_ref.getAttribute('option_id');

  // remember this option_id to make it immune from any 
  // other actions until we are turning it on or off.
  firestarter = option_id;

  if (input_ref.checked)
  {
    //var require_options = input_ref.attributes['require_options'];
    //var hide_options = input_ref.attributes['hide_options'];
    //fp_show_option(base_id, option_id, (hide_options?hide_options.value:''), (require_options?require_options.value:''));
    fp_show_option(base_id, option_id);
  }
  else
  {
    //var turned_on_options = input_ref.attributes['turned_on_options'];
    //fp_hide_option(option_id, (turned_on_options?turned_on_options.value:''));
    fp_hide_option(option_id);
  }

  firestarter = '';
}

function fp_print()
{

  // create a form variable that will be the form used to post to fp_print.php.
  /*
  var f = document.createElement("form");
  f.setAttribute("name", "fp_form_1");
  f.setAttribute("id", "fp_form_1");
  f.setAttribute("method", "post");
  f.setAttribute("action", "fp_print.v2.php");
  */
  var f = document.getElementById("fp_form_1");

  // create a variable to hold the hidden input elements that we will add to the form.

  // set some general attributes for the form data such as title, finished/unfinished sq ft.
  var hi = document.createElement("input");
  hi.setAttribute("type", "hidden");
  hi.setAttribute("name", "title");
  hi.setAttribute("id", "title");
  //hi.setAttribute("value", document.title);
  hi.setAttribute("value", document.getElementById('fp_title').firstChild.nodeValue);
  f.appendChild(hi);
  hi = null;

  /*
  var hi = document.createElement("input");
  hi.setAttribute("type", "hidden");
  hi.setAttribute("name", "finsqft");
  hi.setAttribute("id", "finsqft");
  hi.setAttribute("value", document.getElementById('fp_finsqft').firstChild.nodeValue);
  f.appendChild(hi);
  hi = null;
  */

  /*
  var hi = document.createElement("input");
  hi.setAttribute("type", "hidden");
  hi.setAttribute("name", "unfinsqft");
  hi.setAttribute("id", "unfinsqft");
  hi.setAttribute("value", document.getElementById('fp_unfinsqft').firstChild.nodeValue);
  f.appendChild(hi);
  hi = null;
  */

  if (document.getElementById('fp_stats'))
  {
    var hi = document.createElement("input");
    hi.setAttribute("type", "hidden");
    hi.setAttribute("name", "stats_html");
    hi.setAttribute("id", "stats_html");
    hi.setAttribute("value", document.getElementById('fp_stats').innerHTML);
    f.appendChild(hi);
    hi = null;
  }

  // see how many levels of a floorplan are on the page,
  // -NO- and get a reference to each floorplan's container element.
  //var fp_container;
  var floorplans = new Array();
  var fp_i = 0;
  while (fp_i < 5)
  {
    if (!document.getElementById('fp_'+ ++fp_i))
    {
      continue;
    }

    // var fp_i now represents the base floorplan number that we will look for in the page.

    /*
    fps[] -- array of floor plans
      fp_name -- title for this section/level of the floorplan. i.e. subtitle of printed page.
      fp_gifs[] -- array relative filenames of gif images that need to be merged for this floorplan
      fp_optnames[] -- array of option names that were selected for this level in the home.
    */

    var hi = document.createElement("input");
    hi.setAttribute("type", "hidden");
    hi.setAttribute("name", "fps["+fp_i+"][fp_name]");
    hi.setAttribute("id", "fps_"+fp_i+"_fp_name");
    hi.setAttribute("value", document.getElementById('fp_name'+fp_i).firstChild.nodeValue);
    f.appendChild(hi);
    hi = null;

    var hi = document.createElement("input");
    hi.setAttribute("type", "hidden");
    hi.setAttribute("name", "fps["+fp_i+"][fp_gifs][0]");
    hi.setAttribute("id", "fps_"+fp_i+"_fp_gifs_0");
    //hi.setAttribute("value", document.getElementById('fp_base'+fp_i).attributes['src'].value);
    hi.setAttribute("value", document.getElementById('fp_base'+fp_i).getAttribute('src'));
    f.appendChild(hi);
    hi = null;

    var o;
    var o_i = 0;
    //alert('fp_chk_base'+fp_i+'_option'+ ++o_i);
    while (o = document.getElementById('fp_chk_base'+fp_i+'_option'+ ++o_i))
    {
      if (o.checked)
      {
        var hi = document.createElement("input");
        hi.setAttribute("type", "hidden");
        hi.setAttribute("name", "fps["+fp_i+"][fp_gifs]["+o_i+"]");
        hi.setAttribute("id", "fps_"+fp_i+"_fp_gifs_"+o_i);
        //hi.setAttribute("value", document.getElementById(document.getElementById('fp_chk_base'+fp_i+'_option'+o_i).attributes['option_id'].value).attributes['src'].value);
        hi.setAttribute("value", document.getElementById(document.getElementById('fp_chk_base'+fp_i+'_option'+o_i).getAttribute('option_id')).getAttribute('src'));
        f.appendChild(hi);
        hi = null;

        var hi = document.createElement("input");
        hi.setAttribute("type", "hidden");
        hi.setAttribute("name", "fps["+fp_i+"][fp_optnames]["+o_i+"]");
        hi.setAttribute("id", "fps_"+fp_i+"_fp_optnames_"+o_i);
        hi.setAttribute("value", o.nextSibling.nodeValue);
        f.appendChild(hi);
        hi = null;
      }
    }

  }

  f.target = "_blank";

  f.submit();
  return;

}

//window.onfocus = recalc_stats;

function recalc_stats()
{
  var floorplans = new Array();
  var fp_i = 0;
  while (fp_i < 5)
  {
    if (!document.getElementById('fp_'+ ++fp_i))
    {
      continue;
    }

    // var fp_i now represents the base floorplan number that we will look for in the page.

    var o;
    var o_i = 0;
    //alert('fp_chk_base'+fp_i+'_option'+ ++o_i);
    while (o = document.getElementById('fp_chk_base'+fp_i+'_option'+ ++o_i))
    {
      if (o.checked)
      {
        var base_id = 'fp_base'+fp_i;
        //alert(base_id);
        var option_id = 'fp_base'+fp_i+'_option'+o_i;
        //alert(option_id);
        fp_show_option(base_id, option_id);
      }
    }
  }
}
