Number.prototype.formatMoney = function(c, d, t)
{
  var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

var colors = new Array();

colors[121] = '';

colors[124] = '';

var prices = new Array();

prices[58] = new Array();

prices[58][121] = new Array();

prices[58][121][152] = new Array();

prices[58][121][154] = new Array();

prices[58][121][155] = new Array();

prices[58][121][156] = new Array();

prices[58][121][157] = new Array();

prices[58][121][158] = new Array();

prices[58][121][159] = new Array();

prices[58][121][160] = new Array();

prices[58][121][161] = new Array();

prices[58][121][162] = new Array();

prices[58][124] = new Array();

prices[58][124][152] = new Array();

prices[58][124][154] = new Array();

prices[58][124][155] = new Array();

prices[58][124][156] = new Array();

prices[58][124][157] = new Array();

prices[58][124][158] = new Array();

prices[58][124][159] = new Array();

prices[58][124][160] = new Array();

prices[58][124][161] = new Array();

prices[58][124][162] = new Array();
prices[58][121][152] = {
  price: 0.00,
  page_count_min_price: 175.00,
  page_count_max_price: 300.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][152] = {
  price: 0.00,
  page_count_min_price: 175.00,
  page_count_max_price: 300.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][154] = {
  price: 0.00,
  page_count_min_price: 210.00,
  page_count_max_price: 450.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][154] = {
  price: 0.00,
  page_count_min_price: 205.00,
  page_count_max_price: 450.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][155] = {
  price: 0.00,
  page_count_min_price: 235.00,
  page_count_max_price: 600.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][155] = {
  price: 0.00,
  page_count_min_price: 225.00,
  page_count_max_price: 600.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][156] = {
  price: 0.00,
  page_count_min_price: 260.00,
  page_count_max_price: 750.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][156] = {
  price: 0.00,
  page_count_min_price: 250.00,
  page_count_max_price: 750.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][157] = {
  price: 0.00,
  page_count_min_price: 285.00,
  page_count_max_price: 900.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][157] = {
  price: 0.00,
  page_count_min_price: 275.00,
  page_count_max_price: 900.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][158] = {
  price: 0.00,
  page_count_min_price: 310.00,
  page_count_max_price: 1050.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][158] = {
  price: 0.00,
  page_count_min_price: 300.00,
  page_count_max_price: 1050.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][159] = {
  price: 0.00,
  page_count_min_price: 335.00,
  page_count_max_price: 1200.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][159] = {
  price: 0.00,
  page_count_min_price: 325.00,
  page_count_max_price: 1200.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][160] = {
  price: 0.00,
  page_count_min_price: 360.00,
  page_count_max_price: 1350.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][160] = {
  price: 0.00,
  page_count_min_price: 350.00,
  page_count_max_price: 1350.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][161] = {
  price: 0.00,
  page_count_min_price: 385.00,
  page_count_max_price: 1500.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][161] = {
  price: 0.00,
  page_count_min_price: 375.00,
  page_count_max_price: 1500.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[58][121][162] = {
  price: 0.00,
  page_count_min_price: 410.00,
  page_count_max_price: 1650.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[58][124][162] = {
  price: 0.00,
  page_count_min_price: 400.00,
  page_count_max_price: 1650.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};


prices[71] = new Array();

prices[71][121] = new Array();

prices[71][121][152] = new Array();

prices[71][121][154] = new Array();

prices[71][121][155] = new Array();

prices[71][121][156] = new Array();

prices[71][121][157] = new Array();

prices[71][121][158] = new Array();

prices[71][121][159] = new Array();

prices[71][121][160] = new Array();

prices[71][121][161] = new Array();

prices[71][121][162] = new Array();

prices[71][124] = new Array();

prices[71][124][152] = new Array();

prices[71][124][154] = new Array();

prices[71][124][155] = new Array();

prices[71][124][156] = new Array();

prices[71][124][157] = new Array();

prices[71][124][158] = new Array();

prices[71][124][159] = new Array();

prices[71][124][160] = new Array();

prices[71][124][161] = new Array();

prices[71][124][162] = new Array();
prices[71][121][152] = {
  price: 0.00,
  page_count_min_price: 200.00,
  page_count_max_price: 460.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][152] = {
  price: 0.00,
  page_count_min_price: 190.00,
  page_count_max_price: 450.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][154] = {
  price: 0.00,
  page_count_min_price: 240.00,
  page_count_max_price: 760.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][154] = {
  price: 0.00,
  page_count_min_price: 230.00,
  page_count_max_price: 750.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][155] = {
  price: 0.00,
  page_count_min_price: 280.00,
  page_count_max_price: 960.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][155] = {
  price: 0.00,
  page_count_min_price: 270.00,
  page_count_max_price: 950.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][156] = {
  price: 0.00,
  page_count_min_price: 320.00,
  page_count_max_price: 1160.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][156] = {
  price: 0.00,
  page_count_min_price: 310.00,
  page_count_max_price: 1150.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][157] = {
  price: 0.00,
  page_count_min_price: 330.00,
  page_count_max_price: 1360.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][157] = {
  price: 0.00,
  page_count_min_price: 350.00,
  page_count_max_price: 1350.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][158] = {
  price: 0.00,
  page_count_min_price: 340.00,
  page_count_max_price: 1560.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][158] = {
  price: 0.00,
  page_count_min_price: 390.00,
  page_count_max_price: 1550.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][159] = {
  price: 0.00,
  page_count_min_price: 440.00,
  page_count_max_price: 1760.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][159] = {
  price: 0.00,
  page_count_min_price: 430.00,
  page_count_max_price: 1750.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][160] = {
  price: 0.00,
  page_count_min_price: 480.00,
  page_count_max_price: 1960.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][160] = {
  price: 0.00,
  page_count_min_price: 470.00,
  page_count_max_price: 1950.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][161] = {
  price: 0.00,
  page_count_min_price: 520.00,
  page_count_max_price: 2160.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][161] = {
  price: 0.00,
  page_count_min_price: 510.00,
  page_count_max_price: 2150.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};
prices[71][121][162] = {
  price: 0.00,
  page_count_min_price: 560.00,
  page_count_max_price: 2360.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 1
};
prices[71][124][162] = {
  price: 0.00,
  page_count_min_price: 550.00,
  page_count_max_price: 2350.00,
  page_count_enabled: true,
  page_count_min: 32,
  page_count_max: 250,
  page_count_step: 2
};


var quantities = new Array();

quantities[152] = 1;

quantities[154] = 2;

quantities[155] = 3;

quantities[156] = 4;

quantities[157] = 5;

quantities[158] = 6;

quantities[159] = 7;

quantities[160] = 8;

quantities[161] = 9;

quantities[162] = 10;

var discount_factor = 1.0;
var tax_rate        = 0.0;
var shipping_cost   = 0.0;
var tax             = 0.0;
var subtotal        = 0;

function displayPrice( target, value )
{
  $(target).innerHTML = '$' + value.formatMoney(2, ".", ",");
}

function updateGrandTotal()
{ displayPrice( 'grand_total', tax + subtotal + shipping_cost ); }

function updateTax()
{
  if ( tax_rate == 0.0 )
    {
	  tax = 0.0;
      $('tax_group').hide();
	}
  else
    {
	  tax = subtotal * tax_rate;
	  displayPrice( 'sales_tax', tax );
	  $('tax_group').show();
	}
}

function updateSubtotal()
{
  price = prices[$('project_product_size_id').getValue()][$('project_product_color_id').getValue()][$('project_product_quantity_id').getValue()];

  if (price.page_count_enabled)
  {
    $('page_count_view').style.display = "block";
    page_count = parseInt($('project_page_count').value);
    if (page_count > price.page_count_max || isNaN(page_count))
    {
      page_count = price.page_count_max;
      subtotal = price.page_count_max_price;
    }
    else if (page_count < price.page_count_min)
    {
      page_count = price.page_count_min;
      subtotal = price.page_count_min_price;
    }
    else
    {
      for (var i = price.page_count_min; i <= price.page_count_max; i += price.page_count_step)
      {
        if ( page_count <= i )
        {
          page_count = i;
          break;
        }
      }
      price_per_step = (price.page_count_max_price - price.page_count_min_price) / ((price.page_count_max - price.page_count_min) / price.page_count_step);
      subtotal = price.page_count_min_price + (price_per_step * (page_count - price.page_count_min) / price.page_count_step);
    }

    if ( !isNaN(page_count) )
      $('project_page_count').value = page_count;
  }
  else
  {
    $('page_count_view').style.display = "none";
    subtotal = price.price;
  }

  subtotal  = ProductCustoms.addProductCustoms(subtotal);
  subtotal *= discount_factor;
/*
  $('subtotal').innerHTML = subtotal.formatMoney(2, ".", ",");
*/
  displayPrice( 'subtotal', subtotal );
  updateTax();
  updateGrandTotal();
}

var ProductCustoms = new Object();
ProductCustoms["addProductCustoms"] = function(price)
{
    return price;
}

function updateColorImage()
{
  element = $('project_product_color_id');
  if (colors[element.getValue()] != "")
    $('product_color_image').innerHTML = '<div class="form-box"><div class="form-img"><img src="' + colors[element.getValue()] + '"></div></div>';
  else
    $('product_color_image').innerHTML = '';
}

function updateCustomItemImage(custom_id)
{
  element = $('product_custom_' + custom_id);
  if (customs[custom_id][element.getValue()].image != "")
    $('product_custom_' + custom_id + '_image').innerHTML = '<div class="form-box"><div class="form-img"><img src="' + customs[custom_id][element.getValue()].image + '"></div></div>';
  else
    $('product_custom_' + custom_id + '_image').innerHTML = '';
}

calculationDisable = function() {
  $$('a.calculate').invoke('hide');
  $$('img.calculate').invoke('show');
}

calculationEnable = function() {
  $$('a.calculate').invoke('show');
  $$('img.calculate').invoke('hide');
}

shippingSelect = function(url) {
  calculationDisable();
  new Ajax.Request(url, {
    method:'put',
    asynchronous:true,
    evalScripts:true,
    parameters:Form.serialize($('calculate_form'))}
  );
}

onShippingSelect = function(cost) {
  calculationEnable();
  $('cost').innerHTML = "$0.00";
  if (isNaN(cost)) return;
  $('cost').innerHTML = "$" + cost.toFixed(2);

  shipping_cost = cost;

  updateTax();
  updateGrandTotal();
}


uploadDisable = function() {
  $$('input[type=submit]').invoke('hide');
  $$('img.upload').invoke('show');
}

uploadEnable = function() {
  $$('input[type=submit]').invoke('show');
  $$('img.upload').invoke('hide');
}


document.observe('dom:loaded', function() {
  $('project_product_color_id').observe('change', function() {
    updateColorImage();
    updateSubtotal();
  });
  $('project_product_quantity_id').observe('change', updateSubtotal );

  updateSubtotal();
  updateColorImage();

  
    new Ajax.Request('/product_sizes/update_customs_dropdowns?id=58', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('yAcj9YvJ8Q4WfzqY4vqI27XtjEH8G5aur6JGUgInq9E=')})
  

  /* $('tab1').setStyle('display:block'); */
  $$('a.calculate').invoke('toggle');
});
