// my_options.js

// User selects options
// Any add on cost is calculated
// and displayed as form text input.
//
// User submits order
// Option data is formatted and
// add on cost added to price and 
// order form input values are set.

// my_toolbox.js is used to get input values and set input values.

var debug = 0;

// blue: +1.00
var cost_index;

var sizes_choice = '';
var sizes_cost = 0;

var colors_choice = '';
var colors_cost = 0;

var engraves_style = '';
var engraves_cost = 0;
var engraves_free = 4;


function ChangeSizes()
{
  // User changed size choice
  sizes_cost = 0;
  sizes_choice = getInputValue(document.calculator.sizes);
        if(debug==1){alert('Before processing size choice is: ' + sizes_choice);}
  
  
  // Do we need to split the name, cost value? large: +1.00
  // Returns char space of the cost_split pattern or -1
  cost_index = sizes_choice.indexOf(": +");
        if(debug==1){alert('Cost index is: ' + cost_index);}
        
        if (cost_index > -1) {
          var sizes_array=sizes_choice.split(": +");
          sizes_choice = sizes_array[0];
          sizes_cost = sizes_array[1];
          
          if(debug==1){alert('After processing size choice is: ' + sizes_choice);}
        } 
        
   // Need to recalculate the total add on cost
   DisplayCost(); 
      
}

function ChangeColors()
{
  // User changed color choice
  colors_cost = 0;
  colors_choice = getInputValue(document.calculator.colors);

  // Do we need to split the name, cost value? large: +1.00
  // Returns char space of the cost_split pattern or -1
  cost_index = colors_choice.indexOf(": +");
        
        if (cost_index > -1) {
          var colors_array=colors_choice.split(": +");
          colors_choice = colors_array[0];
          colors_cost = colors_array[1];
        } 
        
   // Need to recalculate the total add on cost
   DisplayCost(); 
      
}

function ChangeEngraves()
{
  // User changed engraving text or style choice
  engraves_cost = 0;
  engraves_style = getInputValue(document.calculator.engraves_style);
        if(debug==2){alert('Before split style choice is: ' + engraves_style);}
        
   if(engraves_style == '') {
     alert('You need to select a print lettering style before entering text. Click in one of the dots.');
   }

   var engraves_count = 0;
   var items_per_set = 1;   
   var cost_per_letter = 0;
   var engraves_text = getInputValue(document.calculator.engraves_text);
   
   var engraves_array=engraves_style.split(": +");
       engraves_style = engraves_array[0];
       cost_per_letter = engraves_array[1];
        if(debug==2){alert('Style choice is: ' + engraves_style);}

  
  // Do we have any text to calculate the cost on?
  if (engraves_text != '')
  {
     // Count the chars without the spaces; subtract the free letters
     engraves_count = engraves_text.replace(/\s/g, '').length - engraves_free;
     
        if(debug==2){ alert('Text count: ' + engraves_count); }

     if (engraves_count > 0) {
         items_per_set = getInputValue(document.calculator.engraves_set);
         engraves_cost = (engraves_count * cost_per_letter) * items_per_set;   
     }
  }

   // Need to recalculate the total add on cost
   DisplayCost(); 
}


function DisplayCost()
{
  // Total all the options costs
  var display_cost = (colors_cost*1) + (sizes_cost*1) + (engraves_cost*1) ;
          if(debug==3){alert('Total of costs is number: ' + display_cost);}
  
  // Turn it into a dollar value text string
  display_cost = dollarValue(display_cost);
  
   // Set input value to display for User
   setInputValue(document.calculator.cost,display_cost);
   
  // return to browser
  return false;
}


function dollarValue(amt)
{
    // Format the fee as a dollar value
    var amt;
    var str,pos,rnd=0;
	
    if (amt < .995)
      rnd = 1;  // for old Netscape browsers
      str = escape (amt*1.0 + 0.005001 + rnd);  // float, round, escape
      pos = str.indexOf (".");

    if (pos > 0) str = str.substring (rnd, pos + 3);

    str = "\$" + str;
    
    return str;
}


function GiftCard()
{
  // requires hard returns only; no fee calculations
  var opt3 = '';    // giftcard text
  
   // Do we have an engraving option?
   var text = document.calculator.giftcard_text.value;
    
    if (text != '')
    {
       // Retain engraving text line breaks in shopping cart
       opt3 = text.replace(/(\r\n|\r|\n)/g, '<br>');
    }
    
    // Copy the selected giftcard text into the shopping cart option 3 field
	  eval("document.calculator.ITEM_OPTION3.value = '" + opt3 + "'");
    
  // Complete submitting of form
  return true;
}


function UpdateInput()
{
  // update form values for PS input
  var opt1 = '';    // color, size
  var opt2 = '';    // engraving style, alignment
  var opt3 = '';    // engraving text
  var final_price;  // item price + option costs
  
  // Do we have a size option?
  if (document.calculator.sizes) {
    // User choice or get default
    if (sizes_choice == '') {
      sizes_choice = getInputValue(document.calculator.sizes);
    }
    
    // Append the value to the option
    opt1 = sizes_choice;
  }
  
  // Do we have a color option?
  if (document.calculator.colors) {
    // User choice or get default
    if (colors_choice == '') {
      colors_choice = getInputValue(document.calculator.colors);
    }
    
    // Append the value to the option
    if (opt1 == '') {
        opt1 = colors_choice;
    }
    else {
        opt1 = opt1 + ", " + colors_choice;
    }
  }
  

   // Do we have an engraving option?
  if (document.calculator.engraves_text) { 
    var text = document.calculator.engraves_text.value;
    
    if (text != '')
    {
       // Retain engraving text line breaks in shopping cart
       opt3 = text.replace(/(\r\n|\r|\n)/g, '<br>');

       opt2 = engraves_style;
       
       // Do we have an alignment option?
       if (document.calculator.engraves_alignment) {
         opt2 = opt2 + ", " + getInputValue(document.calculator.engraves_alignment);
       }
    }
  }
  
  // Add the option costs to the item price
  final_price = parseFloat(document.calculator.ITEM_PRICE.value) + parseFloat(colors_cost) + parseFloat(sizes_cost) + parseFloat(engraves_cost);

  
	// Copy the selected color-size choices into the shopping cart option 1 field
	eval("document.calculator.ITEM_OPTION1.value = '" + opt1 + "'");

	// Copy the selected engraving style-alignment choices into the shopping cart
  // option 2 field
	eval("document.calculator.ITEM_OPTION2.value = '" + opt2 + "'");

	// Copy the selected engraving text into the shopping cart option 3 field
	eval("document.calculator.ITEM_OPTION3.value = '" + opt3 + "'");
  
  
	// Set the final price value for the shopping cart
	eval("document.calculator.ITEM_PRICE.value = '" + final_price + "'");


  // Complete submitting of form
  return true;
}

