/*
**
**  Copyright (c) 1986-2008, Accent Blinds and Shades. All rights reserved.
**
**  Usage, duplication, and/or modification of this program, its
**  representation, or its results without written permission from
**  Accent Blinds and Shades is expressly prohibited.
**
*/

if (navigator.appVersion.substring(0, 1) < 4)
  alert("\n\nPlease note that you are using a version 3.0 browser." +
    "While we made every effort to make this page compatible with " +
    "version 3.0 browsers, it does make extensive use of JavaScript, " +
    "which probably won't work correctly in version 3.0 browsers.  For " +
    "this reason, we encourage you to upgrade your browser as soon as " +
    "possible.\n\nNetscape upgrades can be downloaded for free at " +
    "www.netscape.com/download.\nMicrosoft Internet Explorer upgrades " +
    "can be downloaded for free from www.microsoft.com/ie/.\n\nIf you " +
    "experience troubles with this form due to using a version 3.0 " +
    "browser, please call us toll-free at 1-XXX-XXX-XXXX and we will " +
    "help you with your order via telephone. We apologize for any " +
    "inconvenience this may cause!");

function blinds_to_hex(s)
{
  var hexes, o, i;

  hexes = '0123456789abcdef';
  o = '';
  for (i = 0; i < s.length; i++)
    o += hexes.charAt((s.charCodeAt(i) >> 4) & 0x0F) +
      hexes.charAt(s.charCodeAt(i) & 0x0F);
  return o;
}

function blinds_go()
{
  var f, e, v, si, i_option, i_product, i_color, price, description, s, v,
    product_width, product_length;

  f = document.forms['order_form'];
  i_product = parseInt(f.elements['form_type'].value);
  if (i_product < 0)
  {
    alert('Please select pattern group.');
    return;
  }
  sb_product = sb_products[i_product];

  i_color = Math.floor(f.elements['form_color'].value);
  if (i_color < 0)
  {
    alert('Please select a product color.');
    return;
  }

  price = blinds_calculate();
  if (price <= 0)
  {
    alert('Please enter your dimensions.');
    return;
  }

  product_width = parseInt(f.elements['form_width_int'].value);
  e = f.elements['form_width_frac'];
  if (e.value != 'even')
    product_width += eval(e.value);
  product_length = parseInt(f.elements['form_length_int'].value);
  e = f.elements['form_length_frac'];
  if (e.value != 'even')
    product_length += eval(e.value);

  description = '';
  description += sb_product['name'];
  description += ', ' + f.elements['form_color'].value + ' color';
  description += ', ' + product_width + ' inches wide by ' +
    product_length + ' inches in length';

  for (var i_option = 0; i_option < sb_options.length; i_option++)
  {
    sb_option = sb_options[i_option];
    e = f.elements['form_' + sb_option['label']];
    if (sb_option['type'] == 'select')
    {
      v = e.value;
      si = e.selectedIndex;
    }
    else if (sb_option['type'] == 'radio')
    {
      v = '';
      for (si = 0; si < e.length; si++)
        if (e[si].checked)
	{
	  v = e[si].value;
	  break;
	}
      if (si >= e.length)
        si = -1;
    }
    else if (sb_option['type'] == 'checkbox')
    {
      v = (e.checked) ? 'yes' : 'no';
      si = (e.checked) ? 1 : 0;
    }
    else if (sb_option['type'] == 'text')
    {
      v = e.value;
      si = (v) ? 1 : 0;
    }
    else if (sb_option['type'] == 'internal')
    {
      continue;
    }
    s = '';
    eval(sb_option['script_check']);
    if (s)
    {
      alert(s);
      return;
    }
    eval(sb_option['script_string']);
    if (s == 'error')
      return;
    description += s;
  }

  s = '';
  eval(sb_config['script_check']);
  if (s)
  {
    alert(s);
    return;
  }

  description += '.';
  v = f.elements['form_comments'].value;
  if (v.length > 0)
    description += '  Comments: ' + v;
	
  document.forms['store_form'].elements['eidp'].value =
    blinds_to_hex("new\t" + description + "\t" + (price / 100.0));
  document.forms['store_form'].elements['quantity'].value =
    f.elements['form_quantity'].value;
  document.forms['store_form'].submit();
//  alert(description);

  return true;
}

function blinds_go_product()
{
  var i_product;

  i_product = parseInt(document.forms['order_form'].elements['form_type'].value);
  if (i_product < 0)
    return;
  if (window.location != top.location)
    window.location = sb_products[i_product]['url'];
  else
    top.location = sb_products[i_product]['url'];
}

function blinds_zero()
{
  var f, ec;
  f = document.forms['order_form'];
  f.elements['form_price'].value = '$0.00';
  f.elements['form_total'].value = '$0.00';
  for (var i_option = 0; i_option < sb_options.length; i_option++)
  {
    sb_option = sb_options[i_option];
    if (sb_option['script_price'])
    {
      ec = f.elements['form_' + sb_option['label'] + '_charge'];
      if (ec)
        ec.value = 'N/A';
    }
  }
  return 0.0;
}

function blinds_dollar_format(x)
{
  var o, d, c;

  if ((x == '') || (x <= 0))
    return '$0.00';
  d = Math.floor(x / 100.0);
  c = x % 100;
  o = '$' + d + '.';
  return o + ((c < 10) ? '0' : '') + c;
}

function blinds_calculate()
{
  var e, ec, f, v, si, p, i_product, a_prices, product_length, product_width,
    quantity, i_w, i_l, i, total, price, upcharge;

  f = document.forms['order_form'];
  i_product = parseInt(f.elements['form_type'].value);
  if (i_product < 0)
    return blinds_zero();
  sb_product = sb_products[i_product];

  e = f.elements['form_width_int'];
  if (e.value == '')
    return blinds_zero();
  product_width = parseInt(e.value);
  if (isNaN(product_width) || (product_width < 1))
  {
    alert('The width must be a valid whole number.');
    e.value = 0;
    f.elements['form_width_frac'].selectedIndex = 0;
    return blinds_zero();
  }
  e = f.elements['form_width_frac'];
  if (e.value != 'even')
    product_width += eval(e.value);
  if (product_width < sb_product['width_minimum'])
  {
    f.elements['form_width_int'].value = sb_product['width_minimum'];
    e.selectedIndex = 0;
    alert('Please contact us for widths under ' +
      sb_product['width_minimum'] + ' inches!');
    return blinds_zero();
  }
  if (product_width > sb_product['width_maximum'])
  {
    f.elements['form_width_int'].value = sb_product['width_maximum'];
    e.selectedIndex = 0;
    alert('Please contact us for widths over ' +
      sb_product['width_maximum'] + ' inches!');
    return blinds_zero();
  }

  e = f.elements['form_length_int'];
  if (e.value == '')
    return blinds_zero();
  product_length = parseInt(e.value);
  if (isNaN(product_length) || (product_length < 1))
  {
    alert('The length must be a valid whole number.');
    e.value = 0;
    f.elements['form_length_frac'].selectedIndex = 0;
    return blinds_zero();
  }
  e = f.elements['form_length_frac'];
  if (e.value != 'even')
    product_length += eval(e.value);
  if (product_length < sb_product['length_minimum'])
  {
    f.elements['form_length_int'].value = sb_product['length_minimum'];
    e.selectedIndex = 0;
    alert('Please contact us for lengths under ' +
      sb_product['length_minimum'] + ' inches!');
    return blinds_zero();
  }
  if (product_length > sb_product['length_maximum'])
  {
    f.elements['form_length_int'].value = sb_product['length_maximum'];
    e.selectedIndex = 0;
    alert('Please contact us for lengths over ' +
      sb_product['length_maximum'] + ' inches!');
    return blinds_zero();
  }

  e = f.elements['form_quantity'];
  quantity = parseInt(e.value);
  if (isNaN(quantity) || (quantity < 1))
  {
    alert('The quantity must be a valid whole number.');
    e.value = 1;
    return blinds_zero();
  }

  i_w = -1;
  for (i = 0; i <= sb_product['widths'].length; i++)
  {
    if (product_width <= sb_product['widths'][i])
    {
      i_w = i;
      break;
    }
  }
  if (i_w < 0)
  {
    f.elements['form_width_int'].value = sb_product['width_minimum'];
    f.elements['form_width_frac'].selectedIndex = 0;
    return blinds_zero();
  }
  i_l = -1;
  for (i = 0; i <= sb_product['lengths'].length; i++)
  {
    if (product_length <= sb_product['lengths'][i])
    {
      i_l = i;
      break;
    }
  }
  if (i_l < 0)
  {
    f.elements['form_length_int'].value = sb_product['length_minimum'];
    f.elements['form_length_frac'].selectedIndex = 0;
    return blinds_zero();
  }
  i = i_l * sb_product['widths'].length + i_w;
  a_prices = sb_product['prices'];
  price = a_prices[i];
  if (price < 0)
  {
    alert('Please contact us for sizes over ' + sb_product['widths'][i_w] +
      ' inches wide by ' + sb_product['lengths'][i_l] + ' inches in length.');
    f.elements['form_width_int'].value = '';
    f.elements['form_length_int'].value = '';
    return blinds_zero();
  }
  price = Math.floor(price * 100.0);

  upcharge = 0;
  for (var i_option = 0; i_option < sb_options.length; i_option++)
  {
    sb_option = sb_options[i_option];
    if (!sb_option['script_price'])
      continue;
    e = f.elements['form_' + sb_option['label']];
    ec = f.elements['form_' + sb_option['label'] + '_charge'];
    if (sb_option['type'] == 'select')
    {
      v = e.value;
      si = e.selectedIndex;
    }
    else if (sb_option['type'] == 'radio')
    {
      v = '';
      for (si = 0; si < e.length; si++)
        if (e[si].checked)
	{
	  v = e[si].value;
	  break;
	}
      if (si >= e.length)
        si = -1;
    }
    else if (sb_option['type'] == 'checkbox')
    {
      v = (e.checked) ? 'yes' : 'no';
      si = (e.checked) ? 1 : 0;
    }
    else if (sb_option['type'] == 'text')
    {
      v = e.value;
      si = (v) ? 1 : 0;
    }
    else if (sb_option['type'] == 'internal')
    {
      continue;
    }
    p = -1;
    eval(sb_option['script_price']);
    if (p < 0)
    {
      if (ec)
        ec.value = 'N/A';
    }
    else
    {
      if (ec)
        ec.value = blinds_dollar_format(p);
      upcharge += p;
    }
  }

  price += upcharge;
  price = Math.floor(price * sb_product['markup_multiplier']);
  f.elements['form_price'].value = blinds_dollar_format(price);
  total = price * quantity;
  f.elements['form_total'].value = blinds_dollar_format(total);
  return price;
}

function blinds_change_type()
{
  var f, i_product, a_colors, i_color, i_select, i;

  f = document.forms['order_form'];
  i_product = parseInt(f.elements['form_type'].value);

  i_select = f.elements['form_color'].length;
  for (i = i_select - 1; i >= 0; i--)
    f.elements['form_color'].options[i] = null;

  i_select = 0;
  if (i_product >= 0)
  {
    if (!sb_config['product_color_list_box'])
      f.elements['form_color'].options[i_select++] =
        new Option('Please select.', -1);
    a_colors = sb_products[i_product]['colors'];
    for (i_color = 0; i_color < a_colors.length; i_color += 3)
    {
      f.elements['form_color'].options[i_select] =
        new Option(a_colors[i_color], a_colors[i_color]);
      f.elements['form_color'].options[i_select].style.backgroundColor =
        a_colors[i_color + 2];
      f.elements['form_color'].options[i_select].style.color =
        a_colors[i_color + 1];
      i_select++;
    }
  }
  else
    f.elements['form_color'].options[i_select++] =
      new Option('Please select.', -1);

  f.elements['form_comments'].value = '';
  f.elements['form_quantity'].value = 1;

  blinds_calculate();
  return true;
}

function blinds_startup()
{
  var i_product, i_select, i_tape;

  if (sb_products.length > 1)
  {
    i_select = 0;
    if (!sb_config['product_type_list_box'])
      document.forms['order_form'].elements['form_type'].options[i_select++] =
	new Option('Please select.', -1);
    for (i_product = 0; i_product < sb_products.length; i_product++)
      document.forms['order_form'].elements['form_type'].options[i_select++] =
	new Option(sb_products[i_product]['name'], i_product);
  }
  else
    document.forms['order_form'].elements['form_type'].value = 0;
  blinds_change_type();
}