/*
**
**  Copyright (c) 1997-2003 by Sniglets, Inc., all rights reserved.
**
**  Usage, duplication, and/or modification of this program, its
**  representation, or its results without written permission from
**  Sniglets, Inc. is expressly prohibited.
**
**  $Id: sniglets-blinds.js,v 1.32 2004/08/26 18:26:21 john Exp john $
**
*/

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 sniglets_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 sniglets_blinds_go()
{
  var f, e, v, si, i_option, i_product, i_color, price, description, s, v,
    product_width, product_length, qty, pieces, p;

  f = document.forms['order_form'];
  i_product = parseInt(f.elements['form_type'].value);
  if (i_product < 0)
  {
    alert('Please select price 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 = sniglets_blinds_calculate();
  
  if (price <= 0)
  {
	alert('You did not selected any item to be added to the cart');
	return;
  }
  description = '';
  description += sb_product['name'];
  description += ', Color: ' + f.elements['form_color'].value;

  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;
    }
    s = '';
    eval(sb_option['script_check']);
    if (s)
    {
      alert(s);
      return;
    }
    eval(sb_option['script_string']);
    if (s == 'error')
      return;
	p = -1;
    eval(sb_option['script_price']);

	qty = f.elements['form_' + sb_option['label'] + '_quantity'];
	if (qty)
		pieces = qty.value;
    if (p > 0)
		description += s + ' (' + pieces + ' pcs.)';
	else
	    description += s;
  }

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

document.forms['store_form'].elements['eidp'].value =
    sniglets_blinds_to_hex("new\t" + description + "\t" + (price / 100.0));

document.forms['store_form'].submit();


  return true;
}

function sniglets_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 sniglets_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'];
	  qty = f.elements['form_' + sb_option['label'] + '_quantity'];
      if (ec)
        ec.value = 'N/A';
	  if (qty)
		qty.value = '1';
    }
  }
  return 0.0;
}

function sniglets_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 sniglets_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, qty, pieces;

  f = document.forms['order_form'];
  i_product = parseInt(f.elements['form_type'].value);
  if (i_product < 0)
    return sniglets_blinds_zero();
  sb_product = sb_products[i_product];
	
  if (f.elements['form_discount']) {
	discount = f.elements['form_discount'].value;
	designer_discount = 0.9;
	document.getElementById("discountNote").style.display = "inline";
  }
  else {
  	discount = 0;
	designer_discount = 0.8;
	document.getElementById("discountNote").style.display = "none";
  }
  discount = 1 - (discount/100);
	
  if (sb_product['prices'])
  	price = Math.floor(sb_product['prices'] * discount * 100.0);
  else
    price = 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'];

	qty = f.elements['form_' + sb_option['label'] + '_quantity'];
	pieces = parseInt(qty.value);
	if (isNaN(pieces) || (pieces < 1))
	{
		alert('The quantity must be a valid whole number.');
		pieces.value = 1;
		return sniglets_blinds_zero();
	}
	
    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 (pieces >= 1)
    {
		p = p * pieces;
		if (ec)
			ec.value = sniglets_blinds_dollar_format(p);
                upcharge += Math.floor(p);
    }
  }
  
  designer_code_pattern = "DH00";
  designer_code = f.elements['form_designer_code'].value;
  designer_code_check = designer_code.substring(0,4).toUpperCase();
  
  if ((f.elements['form_quantity_discount']) && quantity>=20)
  	quantity_discount = f.elements['form_quantity_discount'].value;
  else
  	quantity_discount = 0;
  quantity_discount = 1 - quantity_discount/100;
  
  if ((designer_code_check == designer_code_pattern) && (designer_code.length == 8))
  {
  	price = price * designer_discount * quantity_discount;
	document.getElementById("designerNote").style.display = "inline";
	document.forms['store_form'].designer_code.value = designer_code.toUpperCase();
  }
  else {
  	price = price * 1;
	document.getElementById("designerNote").style.display = "none";
	document.forms['store_form'].designer_code.value = "";
  }
  
  price += upcharge;
  price = Math.round(price);
  f.elements['form_price'].value = sniglets_blinds_dollar_format(price);
  return price;

}

function sniglets_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('-- select --', -1);
    a_colors = sb_products[i_product]['colors'];
	for (i_color = 0; i_color < a_colors.length; i_color++)
    {
      f.elements['form_color'].options[i_select] =
        new Option(a_colors[i_color], a_colors[i_color]);
      i_select++;
    }
  }
  else
    f.elements['form_color'].options[i_select++] =
      new Option('-- select --', -1);
  sniglets_blinds_calculate();
  return true;
}

function sniglets_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('-- 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;
  sniglets_blinds_change_type();
}
