
/**
 * A class to keep track of the price the user has selected for his/her photos.
 *
 */
function priceClass(page_size_in, num_pages)
{
  this.page_size = page_size_in || "12x12";
  this.num_pages = num_pages || 10;
  this.num_custom_pages = 0;
  this.self_stick_album_assembly_flag = 0; 
  this.self_stick_album_assembly_last = "no";
  this.ppi_printing_flag = 0;
  this.ppi_printing_last = "no";

  //define the pricing structure
  this.page_size_to_num_lookup = new Array();


  this.page_size_to_num_lookup["12x12"] = new Array();
  this.page_size_to_num_lookup["12x12"]["10/20"] = 125;
  this.page_size_to_num_lookup["12x12"]["15/30"] = 175;
  this.page_size_to_num_lookup["12x12"]["20/40"] = 225;
  this.page_size_to_num_lookup["12x12"]["custom"] = 10;
  this.page_size_to_num_lookup["12x12"]["ppi"] = 7.50;
  this.page_size_to_num_lookup["12x12"]["self_stick"] = "N/A";

  this.page_size_to_num_lookup["12x8"] = new Array();
  this.page_size_to_num_lookup["12x8"]["10/20"] = 125;
  this.page_size_to_num_lookup["12x8"]["15/30"] = 175;
  this.page_size_to_num_lookup["12x8"]["20/40"] = 225;
  this.page_size_to_num_lookup["12x8"]["custom"] = 10;
  this.page_size_to_num_lookup["12x8"]["ppi"] = "N/A";
  this.page_size_to_num_lookup["12x8"]["self_stick"] = "N/A";

  this.page_size_to_num_lookup["11x14"] = new Array();
  this.page_size_to_num_lookup["11x14"]["10/20"] = 150;
  this.page_size_to_num_lookup["11x14"]["15/30"] = 200;
  this.page_size_to_num_lookup["11x14"]["20/40"] = 250;
  this.page_size_to_num_lookup["11x14"]["custom"] = 15;
  this.page_size_to_num_lookup["11x14"]["ppi"] = 9.50;
  this.page_size_to_num_lookup["11x14"]["self_stick"] = new Array();
  this.page_size_to_num_lookup["11x14"]["self_stick"]["10/20"] = 175;
  this.page_size_to_num_lookup["11x14"]["self_stick"]["15/30"] = 200;
  this.page_size_to_num_lookup["11x14"]["self_stick"]["20/40"] = 225;


  this.page_size_to_num_lookup["10x15"] = new Array();
  this.page_size_to_num_lookup["10x15"]["10/20"] = 150;
  this.page_size_to_num_lookup["10x15"]["15/30"] = 200;
  this.page_size_to_num_lookup["10x15"]["20/40"] = 250;
  this.page_size_to_num_lookup["10x15"]["custom"] = 15;
  this.page_size_to_num_lookup["10x15"]["ppi"] = 9.50;
  this.page_size_to_num_lookup["10x15"]["self_stick"] = "N/A";

  this.page_size_to_num_lookup["10x10"] = new Array();
  this.page_size_to_num_lookup["10x10"]["10/20"] = 125;
  this.page_size_to_num_lookup["10x10"]["15/30"] = 175;
  this.page_size_to_num_lookup["10x10"]["20/40"] = 225;
  this.page_size_to_num_lookup["10x10"]["custom"] = 10;
  this.page_size_to_num_lookup["10x10"]["ppi"] = 3.50;
  this.page_size_to_num_lookup["10x10"]["self_stick"] = new Array();
  this.page_size_to_num_lookup["10x10"]["self_stick"]["10/20"] = 145;
  this.page_size_to_num_lookup["10x10"]["self_stick"]["15/30"] = 170;
  this.page_size_to_num_lookup["10x10"]["self_stick"]["20/40"] = 195;

  this.page_size_to_num_lookup["8x8"] = new Array();
  this.page_size_to_num_lookup["8x8"]["10/20"] = 125;
  this.page_size_to_num_lookup["8x8"]["15/30"] = 175;
  this.page_size_to_num_lookup["8x8"]["20/40"] = 225;
  this.page_size_to_num_lookup["8x8"]["custom"] = 10;
  this.page_size_to_num_lookup["8x8"]["ppi"] = 3.15;
  this.page_size_to_num_lookup["8x8"]["self_stick"] = new Array();
  this.page_size_to_num_lookup["8x8"]["self_stick"]["10/20"] = 110;
  this.page_size_to_num_lookup["8x8"]["self_stick"]["15/30"] = 130;
  this.page_size_to_num_lookup["8x8"]["self_stick"]["20/40"] = 150;

  this.page_size_to_num_lookup["6x6"] = new Array();
  this.page_size_to_num_lookup["6x6"]["10/20"] = 100;
  this.page_size_to_num_lookup["6x6"]["15/30"] = 125;
  this.page_size_to_num_lookup["6x6"]["20/40"] = 150;
  this.page_size_to_num_lookup["6x6"]["custom"] = 5;
  this.page_size_to_num_lookup["6x6"]["ppi"] = 2.10;
  this.page_size_to_num_lookup["6x6"]["self_stick"] = new Array();
  this.page_size_to_num_lookup["6x6"]["self_stick"]["10/20"] = 100;
  this.page_size_to_num_lookup["6x6"]["self_stick"]["15/30"] = 113;
  this.page_size_to_num_lookup["6x6"]["self_stick"]["20/40"] = 125;

  this.page_size_to_num_lookup["5x5"] = new Array();
  this.page_size_to_num_lookup["5x5"]["10/20"] = 100;
  this.page_size_to_num_lookup["5x5"]["15/30"] = 125;
  this.page_size_to_num_lookup["5x5"]["20/40"] = 150;
  this.page_size_to_num_lookup["5x5"]["custom"] = 5;
  this.page_size_to_num_lookup["5x5"]["ppi"] = 1.15;
  this.page_size_to_num_lookup["5x5"]["self_stick"] = new Array();
  this.page_size_to_num_lookup["5x5"]["self_stick"]["10/20"] = 100;
  this.page_size_to_num_lookup["5x5"]["self_stick"]["15/30"] = 113;
  this.page_size_to_num_lookup["5x5"]["self_stick"]["20/40"] = 125;

  /**
   * Sync the priceClass' internval value to those reflected on the page
   *
  */
  priceClass.prototype.syncValuesToPage = function()
  {
    this.page_size = document.getElementById('page_size').value;
    this.num_pages = document.getElementById('num_pages').value;
    this.num_custom_pages = document.getElementById('num_custom_pages').value;
    this.num_pages = document.getElementById('num_pages').value;
    
    this.self_stick_album_assembly_flag =  (this.page_size_to_num_lookup[this.page_size]["self_stick"] != "N/A")?1:0;
    if(document.getElementById('self_stick_album_assembly').value != "N/A")
    this.self_stick_album_assembly_last = document.getElementById('self_stick_album_assembly').value;
  
    this.ppi_printing_flag =  (this.page_size_to_num_lookup[this.page_size]["ppi"] != "N/A")?1:0;
    if(document.getElementById('ppi_printing').value != "N/A")
    this.ppi_printing_last = document.getElementById('ppi_printing').value;
  }


  /**
   * Get the last set value of the self_stick album assembly selection
   * @return string the value of the last know selection
   */
   priceClass.prototype.getSelfStickLast = function()
   {
     return this.self_stick_album_assembly_last;
   }

  
  /**
   * Get the last set value of the ppi printing selection
   * @return string the value of the last know selection
   */
   priceClass.prototype.getPpiLast = function()
   {
     return this.ppi_printing_last;
   }


  /**
   * Get the current page size
   * @return string page size
  */
  priceClass.prototype.getPageSize = function()
  {
    return this.page_size;
  }
  
  /**
   * Set a new page size
   * @param string new_page_size: new page size string
   */
  priceClass.prototype.setPageSize = function(new_page_size)
  {
    this.page_size = new_page_size;
  }
  
  
  /**
   * Get the price for ppi printing of the current selection
   * @return float ppi printing cost
   */
  priceClass.prototype.getPpiPrice = function()
  {
    var return_val = 0;
    
    if(this.getPpiCapableFlag() && document.getElementById('ppi_printing').value == "yes")
    {
      return_val = this.page_size_to_num_lookup[this.page_size]["ppi"];
    }
    
    return return_val;
  }

  
  /**
   * Get the price for self stick assembly of the current selection
   * @return int self stick assembly cost
   */
  priceClass.prototype.getSelfStickPrice = function()
  {
    var return_val = 0;
    
    if(this.getSelfStickCapableFlag() && document.getElementById('self_stick_album_assembly').value == "yes")
    {
      return_val = this.page_size_to_num_lookup[this.page_size]["self_stick"][this.num_pages];
    }
    
    return return_val;
  }
  
  
  /**
   * Tell whether or not the current selection is capable of having ppi printing
   * @return bool the ppi printing flag
  */
  priceClass.prototype.getPpiCapableFlag = function()
  {    
    return this.ppi_printing_flag;
  }
  
  
  /**
   * Tell whether or not the current selection is capable of having self_stick assembly
   * @return bool the self stick album assembly flag
  */
  priceClass.prototype.getSelfStickCapableFlag = function()
  {    
    return this.self_stick_album_assembly_flag;
  }


  /**
   * Get the price for page size and number of pages select
   * @param int price for page size / num of pages selection
   */
  priceClass.prototype.getPageSizeAndNumPrice = function()
  {
    return  this.page_size_to_num_lookup[this.page_size][this.num_pages];
  }
  
  
  /**
   * Get the number of custom pages selected
   * @return int the number of custom pages selected
   */
   priceClass.prototype.getNumCustomPages = function()
   {
     return this.num_custom_pages;
   }
  
  /**
   * Get the price for custom pages for the current selection
   * @param int price for the number of custom pages selected
   */
  priceClass.prototype.getCustomPagesPrice = function()
  {
     var return_val = 0;
     
    return_val = custom_pages_cost = this.page_size_to_num_lookup[this.page_size]["custom"];
    
     return return_val;
  }


  /**
   * Get the number of pages that will be printed.  Must to a little string transformation to pull out this value
   * @return int the number of pages to print
  */
  priceClass.prototype.getNumPages = function()
  {    
     var num_pages = document.getElementById('num_pages');
     num_pages = /\d{2}$/.exec(num_pages.value);
     //cast to an int
     num_pages -= 0;
     
     return num_pages;
  }

  
  
  /**
   * Get the total value of the page size + number of pages + num of custom pages + self stick assembly
   * @return int the total price
  */
  priceClass.prototype.getTotal = function()
  {    
   return this.getPageSizeAndNumPrice() + (this.getCustomPagesPrice() * this.getNumCustomPages()) + (this.getPpiPrice() * this.getNumPages()) + this.getSelfStickPrice();
  }

}  //end of priceClass


/**
 * Sets an selector to either Yes or No or "N/A" if the option is not available
 * @param the html element object of the selector to check
 * @param the flag that determines if the options should be avaible now for the selector
 * @param the last valid value of the selector
 */
function setSelectorAvailability(selector_element, selection_flag, selection_last_value)
{
  //set the availability 
  if( (selector_element.length == 1 && selection_flag == 1) || (selector_element.length > 1 && selection_flag == 0) )
  {
    while(selector_element.length > 0)
    {
      selector_element.remove(selector_element.length - 1);
    }
    //check to see if the selector options should be available to the user, as not
    //all page sizes have that option available
    if(selection_flag == 0)
    {
      var temp_option_ref = document.createElement('option');
      temp_option_ref.value = "N/A";
      temp_option_ref.text = "Not Available";
      try
      {
        selector_element.add(temp_option_ref,null);
      }
      catch(ex)
      {
        selector_element.add(temp_option_ref); //IE only
      }

    }
    else //options are available
    {
      var temp_option_ref1 = document.createElement('option');
      temp_option_ref1.value = "no";
      temp_option_ref1.text = "No";
      var temp_option_ref2 = document.createElement('option');
      temp_option_ref2.value = "yes";
      temp_option_ref2.text = "Yes";
      
      try
      {
        selector_element.add(temp_option_ref1, null);
        selector_element.add(temp_option_ref2, null);
      }
      catch(ex)
      {
        selector_element.add(temp_option_ref1); //IE only
        selector_element.add(temp_option_ref2); //IE only
      }
      if(selection_last_value == "yes" && selector_element.selectedIndex != 1)
      {
        selector_element.selectedIndex = 1;
        updateFields();
      }
    }
  }
  
}


/**
 * Updates various html fields on the pages
 * Updates the number of Custom Page possibilities to be correct
 * Gets the price objects Total and put that as the price_total span's value
 *
 */
function updateFields()
{
  //update the price object's internal values
  price.syncValuesToPage();
  var price_total = document.getElementById('price_total');
  num_custom_pages_allowed = price.getNumPages();
  var num_custom_pages = document.getElementById('num_custom_pages').length;
  var self_stick_flag = price.getSelfStickCapableFlag();
  var ppi_flag = price.getPpiCapableFlag();
  
  //correct how many custom pages are allowed in the "Number of custom pages" drop down.
  //and then sync the page values with the price object values
  var custom_pages_selector = document.getElementById('num_custom_pages');
  
  if(num_custom_pages_allowed + 1 > num_custom_pages)
  {
    //append the needed number of new pages
    for(var i = num_custom_pages; i <= num_custom_pages_allowed; i++)
    {
      var temp_option_ref = document.createElement('option');
      temp_option_ref.value = i;
      temp_option_ref.text = i;
      try
      {
        custom_pages_selector.add(temp_option_ref,null);
      }
      catch(ex)
      {
        custom_pages_selector.add(temp_option_ref);
      }
    }
  }
  else  
  //there are values that need to be trimmed and make sure the number of pages selected is not higher than the new
  //number of pages avaible for selection
  {
    if(num_custom_pages > num_custom_pages_allowed)
    {
      var i = num_custom_pages;
      while(i-- > num_custom_pages_allowed + 1)
      {
        document.getElementById('num_custom_pages').remove(i);
      }
    }
    price.syncValuesToPage();
  }

   //update the Total price column
   price_total_num = new String(price.getTotal());
   if(price_total_num.indexOf('.') == -1)
     price_total_num += '.';
   while(price_total_num.slice(price_total_num.indexOf('.') +1).length != 2)
     price_total_num += "0";
   price_total.innerHTML = "$" + price_total_num;

  //Update the subtotal areas
  
  //ppi printing
  document.getElementById('ppi_printing_subtotal').innerHTML = "";
  if(price.getPpiCapableFlag() && document.getElementById('ppi_printing').value == "yes")
  {
    ppi_price = new String(price.getPpiPrice());
    while(ppi_price.slice(ppi_price.indexOf('.') + 1).length != 2)
      ppi_price += "0";
    document.getElementById('ppi_printing_subtotal').innerHTML = price.getNumPages() + " sides @ $" + ppi_price + " ea.";
  }
  //self stick album assembly
  document.getElementById('self_stick_album_assembly_subtotal').innerHTML = "";
  if(price.getSelfStickCapableFlag() && document.getElementById('self_stick_album_assembly').value == "yes")
  {
    document.getElementById('self_stick_album_assembly_subtotal').innerHTML = "$" + price.getSelfStickPrice() + ".00";
  }
  //the pages/page size costs
  document.getElementById('num_pages_subtotal').innerHTML = "$" + price.getPageSizeAndNumPrice() + ".00";
  //the custom pages cost
  document.getElementById('num_custom_pages_subtotal').innerHTML =  price.getNumCustomPages() + " sides @ $"+ price.getCustomPagesPrice() + ".00 ea.";
  
  
  setSelectorAvailability(document.getElementById('self_stick_album_assembly'), self_stick_flag, price.getSelfStickLast());
  setSelectorAvailability(document.getElementById('ppi_printing'), ppi_flag, price.getPpiLast());  
}

function init()
{
 document.getElementById('price_form').style.visibility = "visible";
 updateFields();
}

//on load stuff
var price = new priceClass();
window.onload=init;

