function OrderEst(id, form)
{
  if (id)
  {
    this.id = id;
    
    this.form = form;
    this.form.onsubmit = this.noSubmit;
    
    this.ps = null;
    OrderEst.idx[this.id] = this;
    OrderEst.arr[OrderEst.arr.length] = this;

    this.total = 0;
    this.qty = 0;
    this.label = '';
    
    this.qty_id = 0;
    this.style_id = 0;

    this.iter = 0;
    this.sched = null;
    this.tid = null;
  }
}
new OrderEst(null);

OrderEst.arr = new Array();
OrderEst.idx = new Array();
OrderEst.defaultZip = '400';
OrderEst.defaultZone = '4';

OrderEst.zipsZones = new Array();
OrderEst.zones = new Array();


function OrderEst_prototype_notifyPSchedChg(ps)
{
  var form = this.form;

  this.total = ps.total;
  this.qty = ps.qty;
  this.label = ps.label;

  this.qty_id = ps.colSelected;
  this.style_id = ps.rowSelected;
  
  this.onchange();
}
OrderEst.prototype.notifyPSchedChg = OrderEst_prototype_notifyPSchedChg;

function OrderEst_prototype_onchange()
{
  var i, opt;
  var form = this.form;
  
  form.psched_qty_id = this.qty_id;
  form.psched_style_id = this.style_id;

  form.poster_label.value = '('+this.label+')';

  form.poster_total.value = '$'+OrderEst.dollarFmt(this.total);
  
  for (i = 0; i < form.poster_qty.options.length; i++)
  {
    opt = form.poster_qty.options[i];
    if (opt.value == this.qty_id)
    {
      opt.selected = true;
    }
  }
  //form.poster_qty.value = this.qty;
  
  form.per_poster_total.value = '$' +
   (this.qty != 0 ? OrderEst.dollarFmt4(this.total / this.qty) : '0.00');
  
  this.ship_total = OrderEst.getShippingTot(form.ship_zip.value, this.qty);
  form.ship_total.value = '$'+OrderEst.dollarFmt(this.ship_total);
   
  form.grand_poster_total.value = '$'+OrderEst.dollarFmt(parseFloat(this.total) + parseFloat(this.ship_total));  
}
OrderEst.prototype.onchange = OrderEst_prototype_onchange;



function OrderEst_prototype_zipFocus()
{
  this.zipUpdater();
}
OrderEst.prototype.zipFocus = OrderEst_prototype_zipFocus;

function OrderEst_prototype_zipBlur()
{
  clearTimeout(this.tid);
}
OrderEst.prototype.zipBlur = OrderEst_prototype_zipBlur;

function OrderEst_prototype_zipUpdater()
{
  if (this.form.ship_zip.value.match(/^[0-9]{5}/))
  { 
    this.onchange();
  }
  this.tid = setTimeout("OrderEst.idx['"+this.id+"'].zipUpdater();", 200);
}
OrderEst.prototype.zipUpdater = OrderEst_prototype_zipUpdater;



function OrderEst_prototype_onSelChange()
{  
  var i, opt, qty_id, cell;
  var form = this.form;

  for (qty_id = 0,  i = 0; i < form.poster_qty.options.length; i++)
  {
    opt = form.poster_qty.options[i];
    if (opt.selected)
    {
      qty_id = opt.value;
    }
  }

  if (this.sched != null)
  {
    if (cell = this.sched.getCell(this.sched.rowSelected, qty_id))
    {
      cell.onclick();
    }
  }
}
OrderEst.prototype.onSelChange = OrderEst_prototype_onSelChange;


function OrderEst_dollarFmt(n)
{
  var str = ''+Math.round(n * 100);
  if (str.replace)
  {
    str = str.replace(/^$/, '0');
    str = str.replace(/^(.)$/, '0$1');
    str = str.replace(/^(..)$/, '0$1');
    str = str.replace(/(..)$/, '.$1');
  }
  return str;
}
OrderEst.dollarFmt = OrderEst_dollarFmt;

function OrderEst_dollarFmt4(n)
{
  var str = ''+Math.round(n * 10000);
  if (str.replace)
  {
    str = str.replace(/^$/, '0');
    str = str.replace(/^(.)$/, '0$1');
    str = str.replace(/^(..)$/, '0$1');
    str = str.replace(/^(...)$/, '0$1');
    str = str.replace(/^(....)$/, '0$1');
    str = str.replace(/(....)$/, '.$1');
  }
  return str;
}
OrderEst.dollarFmt4 = OrderEst_dollarFmt4;

function OrderEst_getShippingTot(zip, qty)
{
  var i, zz, zone_id, zone, total;
  
  this.iter = (this.iter > 0) ? this.iter : 1;
  
  if (zip.replace)
  {
    zip = zip.replace(/[^0-9]/g, '');
    if (!zip)
    {
      zip = OrderEst.defaultZip;
    }
  }
  
  zipPref = parseInt(zip.substring(0, 3));
  for (i = 0, zone_id = OrderEst.defaultZone; i < OrderEst.zipsZones.length; i++)
  {
    zz = OrderEst.zipsZones[i];
    if (zz == null)
    {
      alert(i);
    }
    if ((parseInt(zz[0]) <= zipPref) &&
        (parseInt(zz[1]) >= zipPref))
    {
      zone_id = zz[2];
      i = OrderEst.zipsZones.length;
    }
  }

  if (!(zone = OrderEst.zones[zone_id]))
  {
    alert("'"+zone_id+"' invalid zone");
    if ((this.iter < 2) && (OrderEst.zones.length > 1))
    {
      this.iter++;
      return OrderEst.getShippingTot(OrderEst.defaultZip+"000");
    }
    else
    {
      return '0';
    }
  }

  return zone[qty] + OrderEst.zones.flat_fee; 
}
OrderEst.getShippingTot = OrderEst_getShippingTot;


function OrderEst_prototype_noSubmit()
{
  return false;
}
OrderEst.prototype.noSubmit = OrderEst_prototype_noSubmit;

function OrderEst_prototype_onsubmit()
{
  if (this.form.ship_zip.value == null)
  {
    alert("Please enter a valid zip code.");
    return false;
  }
  else if (this.form.ship_zip.value.length < 5)
  {
    alert("Please enter a valid zip code.");
    return false;
  }
  else if (this.form.ship_zip.value.match)
  {
    if (!this.form.ship_zip.value.match(/^[0-9]{5}/))
    {
      alert("Please enter a valid zip code.");
      return false;
    }
  }
  this.form.submit();
  return true;
}
OrderEst.prototype.onsubmit = OrderEst_prototype_onsubmit;


