var plaque_count = 1; 

function resetPlaquesPage() {
  $('plaquesForm').reset();
  $('plaquesStepTwo').setStyle({ display: 'none' });
  $('plaquesStepThree').setStyle({ display: 'none' });
  $('radioYearly').setStyle({ display: 'none' });
  $('radioYearly_sm').setStyle({ display: 'none' });
  $('radioTitle').setStyle({ display: 'none' });
  $('radioLoser').setStyle({ display: 'none' });
  $('radioQBOption').setStyle({ display: 'none' });
  $('remove_button').setStyle({ display: 'none' });
  $('maxplaques_msg').setStyle({ display: 'none' });
  $('additionalplaques').innerHTML = '';
}

function selectPlaque() {
  resetPlaquesPage();

  if($('trophyfield').getValue()!="select") 
  {
    $('plaquesStepThree').setStyle({ display: 'none' });
    $('plaquesStepTwo').setStyle({ display: 'inline' });

    // Display the Yearly radio for all large trophies:
    if($('trophyfield').getValue().endsWith("_lg")) 
    {
      $('radioYearly').setStyle({ display: 'inline' });
    }

    // Display the Loser radio for all loser trophies:
    if($('trophyfield').getValue().startsWith("loser_")) {
      $('radioLoser').setStyle({ display: 'inline' });
    }
    else if($('trophyfield').getValue().startsWith("qbmini_")) {
      $('radioYearly_sm').setStyle({ display: 'inline' });
    }
    // Display the Title radio for all trophies except Loser and QB Option:
    else {
      $('radioTitle').setStyle({ display: 'inline' });
    }
  }
}

function showThree() {
    $('plaquesStepThree').setStyle({ display: 'inline' });
}

function selectYearly() {
  $('product1').setValue("Yearly Winner Plaque: ");
  $('price1').setValue("10");
}

function selectLoser() {
  $('product1').setValue("Title (ULE) Plaque: ");
  $('price1').setValue("15");
}

function selectTitle() {
  $('product1').setValue("Trophy Title Plaque: ");
  $('price1').setValue("20");
}


function plaqueHTML() {

  var productfield = 'product' + this.plaque_count;
  var qtyfield = 'qty' + this.plaque_count;
  var pricefield = 'price' + this.plaque_count;
  var comment1field = 'comment1_' + this.plaque_count;
  var comment2field = 'comment2_' + this.plaque_count;
  var comment3field = 'comment3_' + this.plaque_count;
  var maxlength = '35';
  if ($('rYearly').checked)
    maxlength = '20';
    
  var plaque = Builder.node ('div', { id: plaque_count }, [
    Builder.node ('input', { type: 'hidden', name: productfield, id: productfield, size: '36', value: $('product1').value }),
    Builder.node ('br'),
    Builder.node ('input', { type: 'text', id: comment1field, size: '36', value: '', maxlength: maxlength }),
    Builder.node ('br'),
    Builder.node ('input', { type: 'text', id: comment2field, size: '36', value: '', maxlength: maxlength  }),
    Builder.node ('br'),      
    Builder.node ('input', { type: 'text', id: comment3field, size: '36', value: '', maxlength: maxlength }),
    Builder.node ('input', { type: 'hidden', name: pricefield, id: pricefield, size: '36', value: $('price1').value }),
    Builder.node ('input', { type: 'hidden', name: qtyfield, id: qtyfield, size: '36', value: '1' })
  ]);
  return plaque;
}

  
function addPlaque() {
    ++this.plaque_count;
    if(this.plaque_count <= 19){
      $('additionalplaques').insert(this.plaqueHTML());
      $('remove_button').show();
    } else if (this.plaque_count == 20){
      $('additionalplaques').insert(this.plaqueHTML());
      $('add_button').hide();
      $('maxplaques_msg').show();
    }      

    if ($('rLoser').checked || $('rQBOption').checked){
	    $('comment3_' + this.plaque_count).disabled=true;
    } else {
	    $('comment3_' + this.plaque_count).disabled=false;      
    }
}

function removePlaque() {
    var plaque_element = 'plaque' + this.plaque_count;
    
    if($(plaque_element)){
      $(plaque_element).remove();
      $('add_button').show();
      --this.plaque_count;
    }      
    if(this.plaque_count == 1){
      $('remove_button').hide();      
    }
    if(this.plaque_count != 20){
      $('maxplaques_msg').hide();      
    }
}
 
function concatenateComments() {
    var i=0;
    var c1;
    var c2;
    var c3;
    var productDesc;
    
    for (i=1;i<=this.plaque_count;i++)
    {
	    c1 = $('comment1_' + i).value;
	    c2 = $('comment2_' + i).value;
	    c3 = $('comment3_' + i).value;
	    if(c3 != "")
		    c3 = " / " + c3;
	    productDesc = $('product' + i).value;
	    $('product' + i).value = productDesc + (c1 + " / " + c2 + c3);
    }
}
  
function resetMaxLength() {
    for (i=1;i<=this.plaque_count;i++)
    {
	    $('comment1_' + i).maxLength = "35";
	    $('comment2_' + i).maxLength = "35";
	    $('comment3_' + i).maxLength = "35";	
    }    
}

function setMaxLengthYearly(size) {
    $('plaquesForm').reset();
    for (i=1;i<=this.plaque_count;i++)
    {
	    $('comment1_' + i).maxLength = "20";
	    $('comment2_' + i).maxLength = "20";
	    $('comment3_' + i).maxLength = "20";
    }
    if(size==="sm")
        $('radioYearly_sm').checked=true;
    else
        $('radioYearly').checked=true;
}  

function enableComment() {
    for (i=1;i<=this.plaque_count;i++) {
	    $('comment3_' + i).disabled=false;
    }
}

function disableComment() {
    for (i=1;i<=this.plaque_count;i++) {
	    $('comment3_' + i).disabled=true;	
    }
}

