var slideshow_timer;
var countdown_seconds = 0;

function show_image(product_number)
{
  var str=new String();
  var obj=document.getElementById("darken");
  obj.style.display='';
  str ="<table style='width: 100%; height: 100%; cursor: pointer;'>";
  str+="  <tr>";
  str+="    <td align=center valign=center nowrap>";
  str+="      <table style='background: black; border: 1px solid #808080;' cellspacing=16>";
  str+="        <tr>";
  str+="          <td>";
  str+="            <img style='border: 1px solid #404040;' src='/photos/products/"+product_number+"/0001.jpg'>";
  str+="          </td>";
  str+="        </tr>";
  str+="        <tr>";
  str+="          <td align=right>";
  str+="            <img src=/Themes/Gallery/close.gif>";
  str+="          </td>";
  str+="        </tr>";
  str+="      </table>";
  str+="    </td>";
  str+="  </tr>";
  str+="</table>";
  obj.innerHTML=str;
  //window.open('/cgi-bin/gallery?src=/photos/products/'+product_number+'/0001.jpg','_blank','scrollbars=no,resizable=no,status=no');
}

function update_countdown()
{
  var obj = document.getElementById('countdown');
  if (obj) {
    obj.innerHTML=countdown_seconds+'s';
  }
}

function countdown()
{
  // update label
  countdown_seconds--;
  if (countdown_seconds) {
    window.clearTimeout(slideshow_timer);
    slideshow_timer=window.setTimeout('countdown()',1000);
    update_countdown();
  } else {
    document.sciform.value.value=document.sciform.Fnext.value;
    submit_sciform();
  }
}

function check_slideshow()
{
  if (document.sciform.Fslideshow && parseInt(document.sciform.Fslideshow.value)) {
    countdown_seconds=parseInt(document.sciform.Fslideshow.value);
    slideshow_timer=window.setTimeout('countdown()',1000);
    update_countdown();
  } else {
    // disable it
    if (slideshow_timer) {
      window.clearTimeout(slideshow_timer);
    }
  }
}

add_onload("check_slideshow();");

function is_panoramic()
{
  var aspect=real(document.sciform.Dproducts__form_factor_width.value)/real(document.sciform.Dproducts__form_factor_height.value);
  if (aspect>0.5 && aspect<2.0) {
    return false;
  }
  return true;
}

function invalid_size(width,height)
{
  var w=real(width);
  var h=real(height);
  // check minimum
  if (is_panoramic()) {
    if (Math.max(w,h)<30.0) {
      alert('The long dimension must be at least 30 inches.');
      return true;
    }
  } else {
    if (Math.max(w,h)<20.0) {
      alert('The long dimension must be at least 20 inches.');
      return true;
    }
  }
  // check maximum
  if (Math.max(w,h)>real(document.sciform.Dproducts__maximum_size.value)) {
    alert('The long dimension must be no more than '+parseInt(document.sciform.Dproducts__maximum_size.value)+' inches.');
    return true;
  }
  return false;
}

function purchase_price()
{
  var w = parseInt(document.sciform.Dpurchase__width.value)
  var h = parseInt(document.sciform.Dpurchase__height.value)
  var qty = parseInt(document.sciform.Dpurchase__quantity.value);
  var units = real(0.0);
  if (!qty) {
    qty=1;
  }
  units+=real(w);
  units+=real(h);
  if (is_panoramic()) {
    units+=Math.min(w,h);
  }
  document.sciform.Dpurchase__rate.value=format_number(units*5.0,2);
  document.sciform.Dpurchase__price.value=format_number(qty*units*5.0,2);
  document.sciform.Dpurchase__price_string.value='$'+format_number(qty*units*5.0,2);
}

function calc_purchase()
{
  var w = parseInt(document.sciform.Dpurchase__width.value)
  var h = parseInt(document.sciform.Dpurchase__height.value)
  if (w && h) {
    purchase_price();
  }
  else if (w) {
    document.sciform.Dpurchase__height.value=format_number(document.sciform.Dpurchase__width.value*document.sciform.Dproducts__form_factor_height.value/document.sciform.Dproducts__form_factor_width.value,0);
    h = parseInt(document.sciform.Dpurchase__height.value)
    purchase_price();
  }
  else if (h) {
    document.sciform.Dpurchase__width.value=format_number(document.sciform.Dpurchase__height.value*document.sciform.Dproducts__form_factor_width.value/document.sciform.Dproducts__form_factor_height.value,0);
    w = parseInt(document.sciform.Dpurchase__width.value)
    purchase_price();
  }
  if (!w || !h || invalid_size(w,h)) {
    document.sciform.Dpurchase__width.value='';
    document.sciform.Dpurchase__height.value='';
    document.sciform.Dpurchase__rate.value='';
    document.sciform.Dpurchase__price.value='';
    document.sciform.Dpurchase__price_string.value='';
  }
  document.sciform.Dpurchase__price_string.onchange();
}

function calc_cart(record_number)
{
  var rate = format_number(eval('document.sciform.Dcart_'+record_number+'_rate.value'),2);
  var quantity = format_number(eval('document.sciform.Dcart_'+record_number+'_quantity.value'),0);
  var price = eval('document.sciform.Dcart_'+record_number+'_price');
  price.value=format_number(rate*quantity,2);
  price.onchange(); // draw it
}
