var currentThumb, hideTimeout, currentID;
function showThumb(id) {
  // If a hide timeout is in effect, clear it because we re-entered a rollover item
  if (hideTimeout) { clearTimeout(hideTimeout); hideTimeout = false; }
  var page_image = document.getElementById('image-0');
  var image = document.getElementById('thumb-' + id);
  if (image) {
    image.style.left = page_image.offsetLeft + 15 + 'px';  
    image.style.top  = page_image.offsetTop + 13 + 'px';
    image.className  = 'rollover';
    disable(page_image);
    if (currentThumb && currentThumb.id != image.id)
      // Delay the changing back to hidden for 1/100s - this makes it appear smooth, otherwise you get
      // a flicker between changing images.
      delayedHide(currentThumb, 'rollover_hidden');
    currentThumb = image;
  }
}

function hideThumbAfter (timeout) {
  hideTimeout = setTimeout(hiddenThumb, timeout);
}

function hiddenThumb() {
  if (currentThumb) {
    enable(document.getElementById('image-0'));
    currentThumb.className = "rollover_hidden";
  }
  hideTimeout = false;
}

function delayedHide(object, class_name) {
  if (!object) return;
  setTimeout(function () { object.className = class_name }, 10);
}

function showImage(id) {
  if (hideTimeout) { clearTimeout(hideTimeout); hideTimeout = false; }
  var page_image = document.getElementById('image-0');
  var image = document.getElementById('image-' + id); 
  if (!image) return;

  page_image.className = 'hidden';
  image.className      = 'image_display';
  if (currentID && currentID != id) delayedHide(document.getElementById('image-' + currentID), 'hidden');

// Display content panel
  if (document.getElementById('content-' + id)) {
    //document.getElementById('content-0').className     = 'hidden';
    document.getElementById('content-' + id).className = 'stylecontent';
  }
  if (currentID && currentID != id) delayedHide(document.getElementById('content-' + currentID), 'stylecontenthidden');

// Disable thumbnail image and frame
  document.getElementById('thumbnail_frame-' + id).style.borderColor = '#C00000';
  disable(document.getElementById('thumbnail-' + id));
  
  if (currentID != id && document.getElementById('thumbnail-' + currentID)) {
    enable(document.getElementById('thumbnail-' + currentID));
    document.getElementById('thumbnail_frame-' + currentID).style.borderColor = '#575555';
  }

  currentID = id;
  if (document.getElementById('current_image')) document.getElementById('current_image').value = id;
}

function hideImageAfter(timeout) {
  hideTimeout = setTimeout(hiddenImage, timeout);
}

function hiddenImage() {
  if (currentID) {
    document.getElementById('image-' + currentID).className = "image_hidden";
    document.getElementById('image-0').className = 'display image';

// Enable thumbnail image and frame
    if (document.getElementById('content-' + currentID)) {
      document.getElementById('content-' + currentID).className = 'stylecontenthidden';
      document.getElementById('content-0').className            = 'defaultcontent';
    }
    enable(document.getElementById('thumbnail-' + currentID));
    document.getElementById('thumbnail_frame-' + currentID).style.borderColor = '#000000';
  }
  hideTimeout = false;
}

function disable(image) { // Disables a span
    image.style.borderColor = "#C00000";
    if (document.frames) {
        image.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=25)";
    }
    else {
        var agent = navigator.userAgent;
        var ver = /Mozilla\/5\.0\s*\(X11; [^)]*; rv:(\d\.\d+)([ab]?)\)/.exec(agent);
        // GTK2 Mozilla/Firebird/etc. builds do not properly support the -moz-opacity style.  This
        // is mozilla bug 201209, which was fixed in the 1.6 trunk on 2003-11-01.  Therefore, if
        // on X11, we require >= 1.6b in order to enable the mozopacity style.
        if (!ver || (ver.index >= 0 && (ver[1] > 1.6 || (ver[1] == 1.6 && ver[2] >= 'b'))))
            image.style.MozOpacity = 0.25;
    }
}
function enable(image) { // Enables a span
    image.style.borderColor = "#000000";
    if (document.frames) image.style.filter = null;
    else image.style.MozOpacity = 1;
}

/* This function allows to create a scroll bar with parameters:
- name: object name
- data: a data array */
function scrollBar(name, data) {
  this.name   = name;
  this.option = data;

  this.config = new Object(); this.object = new Object();
  this.start = 0; this.end = 0;      
}

scrollBar.prototype.init = function() { //This method should be called after create the object
  this.object = {
    next: document.getElementById(this.config['next']),
    prev: document.getElementById(this.config['prev'])
  };
  disable(this.object['prev']);
  if (this.config['hits'] <= this.config['mh']) disable(this.object['next']);
  this.end = this.config['mh'];
}

scrollBar.prototype.next = function() { // next method
  if (this.option.length == 0 || this.end == this.config['hits'] || this.config['hits'] <= this.config['mh']) return;
  var first = document.getElementById(this.config['item'] + this.option[this.start].id);
  var last  = document.getElementById(this.config['item'] + this.option[this.end].id);
  first.className = 'hidden';
  if (this.config['gallery']) { // this is a specific option which is for the configuration gallery only
    var css = this.config['css'] ? this.config['css'] : 'frame_small';
    last.className = css + ' last display';
    if (this.config['mh'] == 3) {
      document.getElementById(this.config['item'] + this.option[this.end - 1].id).className = css + ' center';
      document.getElementById(this.config['item'] + this.option[this.end - 2].id).className = css + ' center';
    }
    else {
      document.getElementById(this.config['item'] + this.option[this.end - 1].id).className = css;
      document.getElementById(this.config['item'] + this.option[this.end - 2].id).className = css + ' center';
      document.getElementById(this.config['item'] + this.option[this.end - 3].id).className = css;
    }
  }
  else {
    last.className = 'display';
  }

  this.start++;
  enable(this.object['prev']);
  if (this.end < this.config['hits']) this.end++;      
  if (this.end == this.config['hits']) disable(this.object['next']);
}

scrollBar.prototype.previous = function() { // prvious method
  if (this.option.length == 0 || this.start == 0) return;

  if (this.start > 0) this.start--;
  this.end--;
  var first = document.getElementById(this.config['item'] + this.option[this.start].id);
  var last  = document.getElementById(this.config['item'] + this.option[this.end].id);

  if (this.config['gallery']) {
    var css = this.config['css'] ? this.config['css'] : 'frame_small';
    if (this.config['mh'] == 3) {
      first.className = css + ' display center';
      document.getElementById(this.config['item'] + this.option[this.end - 1].id).className = css + ' last';
      document.getElementById(this.config['item'] + this.option[this.end - 2].id).className = css + ' center';
    }
    else {
      first.className = css + ' display';
      document.getElementById(this.config['item'] + this.option[this.end - 1].id).className = 'frame_small last';
      document.getElementById(this.config['item'] + this.option[this.end - 2].id).className = 'frame_small';
      document.getElementById(this.config['item'] + this.option[this.end - 3].id).className = 'frame_small center';
    }
  }
  else {
    first.className = 'display';
  }
  last.className  = 'hidden';
  enable(this.object['next']);
  if (this.start == 0) disable(this.object['prev']);
}

/* This function allows to create paging feature with parameters:
- name: object name
- config: configuration which is a hash of { mh: n; hits: n; name: item name; css: css name }
- data: an array */
function paging(name, config, data) {
  this.name   = name;
  this.option = data;
  this.config = config;

  this.nh = 1;      
}
paging.prototype.go = function(nh) {
  var beg = nh == 1 ? 0 : (nh - 1) * this.config['mh'];
  if (beg >= this.config['hits']) return;
  var name  = building_type == 'plan' ? 'planopt_' : this.config['name'];
  var count = 0;
  if (this.nh > 1) {
    var start = (this.nh - 1) * this.config['mh'];
    for (var i=start; i<this.option.length; i++) {
      var item = document.getElementById(name + this.option[i].id);
      if (!item) continue;
      item.className = 'hidden';
      count++;
      if (count == this.config['mh']) break;
    }
  }
  count = 0;      
  for (var i=0; i<this.option.length; i++) {
    var item = document.getElementById(name + this.option[i].id);
    if (!item) continue;
    if (i < beg) {
      if (item.className != 'hidden') item.className = 'hidden';
      continue;
    }
    count++;
    item.className = 'display column';
    if (count == this.config['mh']) break;
  }
  if (document.getElementById('link_' + nh)) document.getElementById('link_' + nh).innerHTML = nh;
  if (nh != this.nh && this.nh) document.getElementById('link_' + this.nh).innerHTML = '<a href="javascript:' + this.name + '.go(' + this.nh + ')">' + this.nh + '</a>';
  this.nh = nh;
}

/* functions that use for the quote page */
function previewImage(width, height) {
  var id = document.getElementById('current_image');
  if (!id) return;
  winOpen('/images/gallery/' + id.value,'preview',width,height);
}

function calc_fees(object, id,len) {
  if ( object.value.length >= len ) {

  var postal = object.value;
  if ( isPostCode(postal) ) {
  var floor;
  if (document.getElementById('floor_yes') && document.getElementById('floor_yes').checked) floor = document.getElementById('floor_yes').value;
  else floor = 0;
  document.frmconfig.action = root_url + '/products/fees';
  document.frmconfig.target = 'hidden_frame';
  document.frmconfig.submit();
 // location.reload();
  }
  else {alert("Please enter your postal code without space to determine freight charges.");
  }
  }
}

function isPostCode(postal){ // CANADIAN CODES ONLY

  entry=postal;
  strlen=entry.length; if(strlen!=6) {return false;}
  entry=entry.toUpperCase();  // in case of lowercase characters
  // Check for legal characters in string - note index starts at zero
  if('ABCEHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {return false;}
  if('0123456789'.indexOf(entry.charAt(1))<0) {return false;}
  if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {return false;}
  if('0123456789'.indexOf(entry.charAt(3))<0) {return false;}
  if('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {return false;}
  if('0123456789'.indexOf(entry.charAt(5))<0) {return false;}
  return true;

}


function display(type, floor) {
  var product     = document.getElementById('product_detail');
  var plan        = document.getElementById('plan_detail');
  var product_opt = document.getElementById('product_option');
  var plan_opt    = document.getElementById('plan_option');
  var product_desc= document.getElementById('product_desc');
  var plan_desc   = document.getElementById('plan_desc');
  var add_tagline = document.getElementById('add_tagline');
  var warranty    = document.getElementById('warranty');
  var shipping_amount = document.getElementById('shipping_amount');
  if (type == 'plan') {
    plan.style.display        = 'block';
    plan_opt.style.display    = 'block';
    plan_desc.style.display   = 'block';
    if (add_tagline) add_tagline.style.visibility = 'hidden';
    product.style.display     = 'none';
    product_opt.style.display = 'none';
    product_desc.style.display= 'none';
    document.getElementById('shipping').style.visibility = 'hidden';
    document.getElementById('subt').style.visibility = 'hidden';
    if (document.getElementById('postal')) document.getElementById('postal').style.visibility   = 'hidden';
    if (warranty) warranty.style.display = 'none';
  }
  else if (type == 'installed') {
    plan.style.display        = 'none';
    plan_opt.style.display    = 'none';
    plan_desc.style.display   = 'none';
    if (add_tagline) add_tagline.style.visibility = 'visible';
    product.style.display     = 'block';
    product_opt.style.display = 'block';
    product_desc.style.display= 'block';
    document.getElementById('shipping').style.visibility = 'hidden';
    document.getElementById('subt').style.visibility = 'visible';
    if (document.getElementById('postal')) document.getElementById('postal').style.visibility   = 'hidden';
    if (warranty) warranty.style.display = 'inline';
  }
  else {
    plan.style.display        = 'none';
    plan_opt.style.display    = 'none';
    plan_desc.style.display   = 'none';
    if (add_tagline) add_tagline.style.visibility = 'visible';
    product.style.display     = 'block';
    product_opt.style.display = 'block';
    product_desc.style.display= 'block';
    document.getElementById('shipping').style.visibility = 'visible';
    document.getElementById('subt').style.visibility = 'visible';
    if (document.getElementById('postal')) document.getElementById('postal').style.visibility   = 'visible';
    if (warranty) warranty.style.display = 'inline';
  }
  var summary_data = floor > 0 ? summary_nofloor : summary;
  if (!summary_data[type]) {
       return;
  }
  var data = summary_data[type];
  for (var k in data) {
    if (k == 'shipping' ) document.getElementById(k).className = type == 'plan' ? 'hidden' : 'block';
    if (k == 'gross_total' || k == 'sub_total' || k == 'base_price' || k == 'net_total' ) {
        document.getElementById(k).innerHTML = data[k];
//    } else if (k == 'net_total') {
// document.getElementById(k).innerHTML = data[k] + shipping_amount;
    } else if (k == 'discount') {
        if (data['discount'].kit != '') {
            document.getElementById('discount_kit').className = 'block';
            document.getElementById('discount_kit_amount').innerHTML = data['discount'].kit;
        } else {
            document.getElementById('discount_kit').className = 'hidden';
        }
        if (data['discount'].option ) {
            if ( type != 'plan' ) {
               document.getElementById('discount_opt').className = 'block';
               document.getElementById('discount_opt_amount').innerHTML = data['discount'].option;
            } else {
               document.getElementById('discount_opt').className = 'hidden';
            }
        }else {
//            alert("here" + data['discount'].option);
            document.getElementById('discount_opt').className = 'hidden';
        }
        if (data['discount'].kit != '') {
            document.getElementById('kit_type').innerHTML = type == 'plan' ? 'Plan' : 'Kit';
        }
    }
    else if (document.getElementById(k)) {
      if (data[k] == '') document.getElementById(k).className = 'hidden'; 
      else {
        document.getElementById(k).className = 'block';
        document.getElementById(k + '_amount').innerHTML = data[k];
      }
    }
  }
  building_type = type;

  if (type == 'plan') { //selected type is plan
    var form = document.frmconfig;
    var tab = document.getElementById('planopt');
    while (tab.rows.length > 0) {
      tab.deleteRow(0);
    }
    for (var i=0; i<form.elements.length; i++) {
      var e = form.elements[i];
      if (e.type == 'checkbox' && e.name == 'option' && e.checked) {
        var item;
        for (var j=0; j<option.length; j++) {
          if (option[j].id == e.value) { item = option[j]; break; }
        }
        addItem(item);
      }
    }
    plan_recalc();
    return;
  }

  //Load option prices
  for (var i=0; i<option.length; i++) {
    document.getElementById('optprice_' + option[i].id).innerHTML = (option[i].cat == 'Floors' && floor == '') ? 'Incl.' : option[i][type][0];
  }
}

function selectedType(type) {
  if (document.getElementById('floor_no') && document.getElementById('floor_no').checked)  display(type, 0);
  else if (document.getElementById('floor_yes')) display(type, document.getElementById('floor_yes').value);
  else display(type, 0);
}

function selectedFloor(floor) {
  var type;
  if (document.getElementById('type1') && document.getElementById('type1').checked)
    display(document.getElementById('type1').value, floor);
  else if (document.getElementById('type2') && document.getElementById('type2').checked)
    display(document.getElementById('type2').value, floor);
  else if (document.getElementById('type3') && document.getElementById('type3').checked)
    display(document.getElementById('type3').value, floor);
  else if (document.getElementById('type4') && document.getElementById('type4').checked)
    display(document.getElementById('type4').value, floor);
  else
    display(document.getElementById('type').options[document.getElementById('type').selectedIndex].value, floor);
}

function selectItem(id, checked) {
  var item = getElement(id);
  if (checked) addItem(item);
  else deleteItem(item);
  plan_recalc();
}

function getElement(id) {
  var item;
  for (i=0; i<option.length; i++) {
    if (option[i].id == id) { item = option[i]; break; }
  }
  return item;
}

function addItem(item) {
  var tabOpt = document.getElementById('planopt');
  var oRow   = tabOpt.insertRow(tabOpt.rows.length);
  oRow.id = 'listopt_' + item.id;
  tabOpt.style.padding = '0px';
  tabOpt.style.margin  = '0px';
  tabOpt.cellPadding = 0;
  tabOpt.cellSpacing = 0;
  for (var i=0; i<2; i++) {
    var oCell = oRow.insertCell(i);
    if (i == 1) {
      oCell.style.paddingTop = '3px';
      oCell.className = 'numberic';
      oCell.innerHTML = item.plan[0];
    }
    else {
      oCell.style.paddingTop = '3px';
      oCell.innerHTML = item.name;
    }
  }
  if (tabOpt.rows.length > 0) document.getElementById('optpanel').className = 'solidline optionlist' + (ca_site ? '' : 'us');

// Set scroll bar configration
  selectedOpt.push(item);
  planopt_sbar.option = selectedOpt;
  planopt_sbar.config['hits'] += 1;
  if (planopt_sbar.config['hits'] > planopt_sbar.config['mh']) {
    tabOpt.rows[tabOpt.rows.length - 1].className = 'hidden';
    enable(planopt_sbar.object['next']);
  }
}

function deleteItem(item) {
   var tabOpt = document.getElementById('planopt');
   for (var i=0; i<tabOpt.rows.length; i++) {
     if (tabOpt.rows[i].id == 'listopt_' + item.id) {
       tabOpt.deleteRow(i); break;
     }
   }
   //if (tabOpt.rows.length == 0) document.getElementById('optpanel').className = 'solidline hidden optionlist' + (ca_site ? '' : 'us');

   var newArray = new Array();
   for (i=0; i<selectedOpt.length; i++) {
     if (selectedOpt[i].id == item.id) continue;
     newArray.push(selectedOpt[i]);
   }
   selectedOpt = newArray;
   planopt_sbar.option = selectedOpt;

   planopt_sbar.config['hits'] = selectedOpt.length;
   planopt_sbar.start = 0;
   planopt_sbar.end   = planopt_sbar.config['mh'];

   disable(planopt_sbar.object['prev']);
   if (planopt_sbar.config['hits'] <= planopt_sbar.config['mh']) disable(planopt_sbar.object['next']);
   else enable(planopt_sbar.object['next']);

   for (var i=0; i<tabOpt.rows.length; i++) {
     document.getElementById(tabOpt.rows[i].id).className = (i <= (planopt_sbar.config['mh'] - 1)) ? 'display' : 'hidden';
   }
   
}

function plan_recalc() {
  var form      = document.frmconfig;
  var sub_total = plan_baseprice;
  var discount  = plan_discount;
  var opt_price = 0;
  for (var i=0; i < form.elements.length; i++) {
    var e = form.elements[i];
    if (e.type == 'checkbox' && e.name=="option" && e.checked) {
      var item = getElement(e.value);
      if (!item) continue;
      opt_price += item.plan[1];
    }
  }

  if (per_plandisc > 0) discount += (opt_price * per_plandisc / 100);
//  alert(discount)
  document.getElementById('discount_kit_amount').innerHTML = '$' + outputComma(discount.toFixed(2), true);
  document.getElementById('discount_kit').className = discount > 0 ? 'block' : 'hidden';
  sub_total += opt_price;
  var net_total   = sub_total - discount;
  var gross_total = sub_total - discount;
  for (var key in taxcode) {
    var tax = net_total * taxcode[key] / 100;
    gross_total += tax;
    document.getElementById(key + '_amount').innerHTML = '$' + outputComma(tax.toFixed(2), true);
  }
  document.getElementById('sub_total').innerHTML   = '$' + outputComma(sub_total.toFixed(2), true);
  //if (plan_discount > 0) {
  //  document.getElementById('net_total_amount').innerHTML = '$' + outputComma(net_total.toFixed(2), true);
  //  document.getElementById('net_total').className = 'display';
  //}
  document.getElementById('gross_total').innerHTML = '$' + outputComma(gross_total.toFixed(2), true);
}

function dimension_desc(name, flag, index) {
  var desc = document.getElementById('desc');
  if (flag) {
    desc.innerHTML = '';
    var img  = document.createElement('img');
    img.src  = '/images/' + name + '_' + index + '.jpg';
    desc.appendChild(img);
  }
  else desc.innerHTML = 'Move your mouse over the numbers to see the part description &amp; material.';
}

function buythis(ot) {
  if ( ot && ot != 'pl' && ot != 'pi' ) { 
    if ( document.getElementById('safe_check') ) {
     var safe_check = document.getElementById('safe_check').value;
     if ( !safe_check ) {
        alert("Please choose the format.");
        return;
     }
    }
    if ( document.frmconfig.postal ) {
    var postal = document.frmconfig.postal.value;
    if ( !isPostCode(postal) ) {
       alert("Please enter your postal code without space to determine freight charges.");
       return;
     }
    }
  }
  document.frmconfig.action = root_url + "/cart/add.html";
  document.frmconfig.target = '';
  document.frmconfig.submit();
}

function checkout_update(object,len) {
  if ( object.value.length >= len ) {
     var postal = document.myform.postal.value;
     if ( !isPostCode(postal) ) {
        alert("Please enter your postal code without space to determine freight charges.");
        return;
      }
     document.myform.action = root_url + "/cart/update.html";
     document.myform.target = '';
     document.myform.submit();
  }
}

function checkout(site,query) {
  var str='';
  if ( query ) str = query;
  var postal = document.myform.postal.value;

  if ( site == 'ca' ) {
  var pickup = document.myform.pickup.checked;
    if ( !isPostCode(postal) && !pickup ) {
     alert("Please enter your postal code without space to determine freight charges.");
     return;
    }
    document.myform.action = "https://www.summerwood.com/cdn/cart/checkout.html" + str;
//    document.myform.action = "http://208.97.123.18:8002/cart/checkout.html"+str;
  } else {
    if ( !postal ) { 
     alert("Please choose your state to determine freight charges.");
     return;
    }
  document.myform.action = "https://www.summerwood.com/cart/checkout.html"+str;
//    document.myform.action = "http://208.97.123.18:8002/cart/checkout.html"+str;
  }  
  document.myform.submit();
}

function rollover(id,mouseout,ext) {
//    alert(document.getElementById(id).name);
   var img = document.getElementById(id).name + (mouseout ? '_b1' : '_b2') + '.' + ext;
   document.getElementById(id).src = '/images/' + img;
   }

function img_rollover(id,mouseout,img1, img2) {
   var img = mouseout ? img1 : img2;
   document.getElementById(id).src = '/images/' + img;
   }

function icon_rollover(id,mouseout,img1, img2) {
   var img = mouseout ? img1 : img2;
   document.getElementById(id).src = '/images/icon/' + img;
   }

function updatequotes (url) {
  document.frmconfig.action = url;
  document.frmconfig.method = "post";
//  document.frmconfig.target = 'hidden_frame';
  document.frmconfig.submit();
}

function checkSource(value, index) {
  if (value == '') return;
  if (index == 1) {
    document.getElementById('source_magazine').selectedIndex = 0;
    document.getElementById('source_referral').selectedIndex = 0;
  } else if (index == 2) {
    document.getElementById('source_internet').selectedIndex = 0;
    document.getElementById('source_referral').selectedIndex = 0;
  } else {
    document.getElementById('source_magazine').selectedIndex = 0;
    document.getElementById('source_internet').selectedIndex = 0;
  }
}

function checkSubmit() {
   if ( document.getElementById('first_name').value == '' ) {
       alert("Please fill your first name");
       return false;
   }
   if ( document.getElementById('last_name').value == '' ) {
       alert("Please fill your last name");
       return false;
   }
   if ( document.getElementById('email').value == '' ) {
       alert("Please fill your email address");
       return false;
   }
   if ( document.getElementById('mailing').checked == true && document.getElementById('country').selectedIndex == 0 ) {
       alert("Please select your country");
       return false;
   }
   if (document.getElementById('source_internet').selectedIndex == 0 && document.getElementById('source_magazine').selectedIndex == 0 && document.getElementById('source_referral').selectedIndex == 0) {
       alert("Please let us know how you heard about us!");
       return false;
   } else {
       if (document.getElementById('source_internet').selectedIndex != 0) {
            document.getElementById('source_final').value = document.getElementById('source_internet').value;
       } else if (document.getElementById('source_magazine').selectedIndex != 0) {
           document.getElementById('source_final').value = document.getElementById('source_magazine').value;
       } else if (document.getElementById('source_referral').selectedIndex != 0) {
           document.getElementById('source_final').value = document.getElementById('source_referral').value;
       }
    }
    return true;
}


function portfolio_checkSubmit() {
   if ( document.getElementById('mailing').checked == true && document.getElementById('country').selectedIndex == 0 ) {
       alert("Please select your country");
       return false;
   } else return true;
}
