function TextButtForm(name)
{
  if (name != null)
  {
    TextButtForm.init(this, name);
  }
}
new TextButtForm(null);
TextButtForm.prototype = new OIRDesignForm(null);
TextButtForm.prototype.superOnchange = OIRDesignForm.prototype.onchange;
TextButtForm.priceGroup = 'ez_design';

TextButtForm.init = function(thisObj, name)
{
  OIRDesignForm.init(thisObj, name);
  thisObj.previewFrame = null;
  thisObj.type = "txt_butt_sing";
  thisObj.fgSelector = null;
  thisObj.bgSelector = null;
  this.supressErrors = false;
  /* grpEl keys
  button_size_id
  button_qty_id
  color_id_fg
  color_id_bg
  
  color_num_display_fg
  color_num_display_bg

  text_message_1
  text_message_2
  text_message_3

  button_unit
  extra_price
  button_subtotal

  reorder_row_id
  */
}


TextButtForm.prototype.preview = function()
{
  this.supressErrors = true;
  this.previewButt();
  this.supressErrors = false;
}

TextButtForm.prototype.previewButt = function()
{
  var bcolor, fcolor, obj, loc;
  this.formOK = true;
  if (this.previewFrame && this.onsubmit())
  {
    loc = 
     "preview-txt_butt.php"+
     "?string1="+URLCoder.encode(this.getElVal(this.grpEls['text_message_1'].el))+
     "&fcolor="+URLCoder.encode(this.getElVal(this.grpEls['color_id_fg'].el))+
     "&bcolor="+URLCoder.encode(this.getElVal(this.grpEls['color_id_bg'].el))+
     "&font="+URLCoder.encode(this.getElVal(this.grpEls['font_code'].el));
    
    if (this.grpEls['text_message_2'])
    {
      loc += 
       "&string2="+URLCoder.encode(this.getElVal(this.grpEls['text_message_2'].el));
    }
    
    if (this.grpEls['text_message_3'])
    {
      loc += 
       "&string3="+URLCoder.encode(this.getElVal(this.grpEls['text_message_3'].el));
    }
    this.previewFrame.location = loc;
  }
}

TextButtForm.prototype.onchange = function()
{
  this.preview();
}

TextButtForm.prototype.chooseColor = function(elColor, elNum)
{
  if (elColor.name.indexOf("_fg") > -1)
  {
    obj = this.grpEls['color_num_display_fg']
  }
  else
  {
    obj = this.grpEls['color_num_display_bg']
  }
  
  if (obj != null)
  {
    obj.cacheVal = this.getElVal(elNum);
  }
}

TextButtForm.prototype.initQty = function()
{
  var qty, size_id;
  qty = OIRDesignForms.getQty(this.getElVal(this.grpEls['button_qty_id'].el));
  size_id = this.getElVal(this.grpEls['button_size_id'].el);
  this.pricer.addCount(TextButtForm.priceGroup, size_id, parseInt(qty));
}

TextButtForm.prototype.updateQty = function()
{
  var old_qty, qty, old_size_id, size_id;
  
  old_qty = OIRDesignForms.getQty(this.grpEls['button_qty_id'].cacheVal);
  old_size_id = this.grpEls['button_size_id'].cacheVal;
  this.pricer.addCount(TextButtForm.priceGroup, old_size_id, parseInt(old_qty) * -1);
  
  qty = OIRDesignForms.getQty(this.getElVal(this.grpEls['button_qty_id'].el));
  size_id = this.getElVal(this.grpEls['button_size_id'].el);
  this.pricer.addCount(TextButtForm.priceGroup, size_id, parseInt(qty));
}


TextButtForm.prototype.updateSubtotal = function()
{
  var button_size_id, unit_price, ext_price, extra_price, reorder;

  qty = OIRDesignForms.getQty(this.getElVal(this.grpEls['button_qty_id'].el));
  button_size_id = this.getElVal(this.grpEls['button_size_id'].el);
  
  sched = TextButtForm.sched[button_size_id];
  
  unit_price = this.pricer.getUnitPrice(TextButtForm.priceGroup, button_size_id, sched);
  ext_price = unit_price * qty;
  
  reorder = false;
  if (this.grpEls['reorder_row_id'])
  {
    reorder = 
     this.getElVal(this.grpEls['reorder_row_id'].el) ? 
     true :
     false;
  }
  
  if ((qty >= TextButtForm.setupWaive) ||
      reorder)
  {
    extra_price = 0
  }
  else
  {
    extra_price = TextButtForm.setup;
  }
   
  this.setElVal(this.grpEls['button_unit'].el, "$"+this.fmtMoney(unit_price));
  this.setElVal(this.grpEls['extra_price'].el, "$"+this.fmtMoney(extra_price));
  this.setElVal(this.grpEls['button_subtotal'].el, "$"+this.fmtMoney(ext_price + extra_price));
  
  if ((obj = this.grpEls['color_num_display_fg']) != null)
  {
    this.setElVal(obj.el, obj.cacheVal);
  }

  if ((obj = this.grpEls['color_num_display_bg']) != null)
  {
    this.setElVal(obj.el, obj.cacheVal);
  }

  return ext_price + extra_price;
}

TextButtForm.prototype.onsubmit = function()
{
  var str, obj, fcolor, bcolor;
  /* Check design name*/
  if (this.grpEls['text_message_1'])
  {
    str = this.getElVal(this.grpEls['text_message_1'].el);
    if (this.formOK && (!str.match(/[^ \r\t\n]/)))
    {
      if (!this.supressErrors)
      {
        alert("You must enter a text message in each text field");
      }
      this.formOK = false;
    }
  }

  if (this.grpEls['text_message_2'])
  {
    str = this.getElVal(this.grpEls['text_message_2'].el);
    if (this.formOK && (!str.match(/[^ \r\t\n]/)))
    {
      if (!this.supressErrors)
      {
        alert("You must enter a text message in each text field");
      }
      this.formOK = false;
    }
  }

  if (this.grpEls['text_message_3'])
  {
    str = this.getElVal(this.grpEls['text_message_3'].el);
    if (this.formOK && (!str.match(/[^ \r\t\n]/)))
    {
      if (!this.supressErrors)
      {
        alert("You must enter a text message in each text field");
      }
      this.formOK = false;
    }
  }

  if (this.grpEls['color_id_fg'] && this.grpEls['color_id_bg'])
  {
    fcolor = this.getElVal(this.grpEls['color_id_fg'].el);
    bcolor = this.getElVal(this.grpEls['color_id_bg'].el);
    if (this.formOK && (fcolor == bcolor))
    {
      alert("You must choose different font and background colors");
      this.formOK = false;
    } 
  }

  return this.formOK;
}
