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[64] = '';

colors[65] = '';

var prices = new Array();

prices[38] = new Array();

prices[38][64] = new Array();

prices[38][64][70] = new Array();

prices[38][64][71] = new Array();

prices[38][64][72] = new Array();

prices[38][64][73] = new Array();

prices[38][64][74] = new Array();

prices[38][64][75] = new Array();

prices[38][64][76] = new Array();

prices[38][64][77] = new Array();

prices[38][64][78] = new Array();

prices[38][64][79] = new Array();

prices[38][64][80] = new Array();

prices[38][64][81] = new Array();

prices[38][65] = new Array();

prices[38][65][70] = new Array();

prices[38][65][71] = new Array();

prices[38][65][72] = new Array();

prices[38][65][73] = new Array();

prices[38][65][74] = new Array();

prices[38][65][75] = new Array();

prices[38][65][76] = new Array();

prices[38][65][77] = new Array();

prices[38][65][78] = new Array();

prices[38][65][79] = new Array();

prices[38][65][80] = new Array();

prices[38][65][81] = new Array();
prices[38][64][70] = {
  price: 38.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][70] = {
  price: 22.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][71] = {
  price: 51.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][71] = {
  price: 34.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][72] = {
  price: 76.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][72] = {
  price: 55.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][73] = {
  price: 128.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][73] = {
  price: 96.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][74] = {
  price: 180.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][74] = {
  price: 137.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][75] = {
  price: 232.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][75] = {
  price: 178.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][76] = {
  price: 284.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][76] = {
  price: 219.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][77] = {
  price: 336.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][77] = {
  price: 260.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][78] = {
  price: 388.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][78] = {
  price: 301.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][79] = {
  price: 440.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][79] = {
  price: 321.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][80] = {
  price: 492.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][80] = {
  price: 356.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][64][81] = {
  price: 544.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[38][65][81] = {
  price: 397.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};


prices[40] = new Array();

prices[40][64] = new Array();

prices[40][64][70] = new Array();

prices[40][64][71] = new Array();

prices[40][64][72] = new Array();

prices[40][64][73] = new Array();

prices[40][64][74] = new Array();

prices[40][64][75] = new Array();

prices[40][64][76] = new Array();

prices[40][64][77] = new Array();

prices[40][64][78] = new Array();

prices[40][64][79] = new Array();

prices[40][64][80] = new Array();

prices[40][64][81] = new Array();

prices[40][65] = new Array();

prices[40][65][70] = new Array();

prices[40][65][71] = new Array();

prices[40][65][72] = new Array();

prices[40][65][73] = new Array();

prices[40][65][74] = new Array();

prices[40][65][75] = new Array();

prices[40][65][76] = new Array();

prices[40][65][77] = new Array();

prices[40][65][78] = new Array();

prices[40][65][79] = new Array();

prices[40][65][80] = new Array();

prices[40][65][81] = new Array();
prices[40][64][70] = {
  price: 76.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][70] = {
  price: 44.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][71] = {
  price: 102.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][71] = {
  price: 65.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][72] = {
  price: 154.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][72] = {
  price: 106.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][73] = {
  price: 258.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][73] = {
  price: 188.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][74] = {
  price: 362.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][74] = {
  price: 270.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][75] = {
  price: 466.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][75] = {
  price: 352.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][76] = {
  price: 570.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][76] = {
  price: 434.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][77] = {
  price: 672.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][77] = {
  price: 516.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][78] = {
  price: 776.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][78] = {
  price: 598.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][79] = {
  price: 880.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][79] = {
  price: 682.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][80] = {
  price: 984.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][80] = {
  price: 766.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][64][81] = {
  price: 1088.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};
prices[40][65][81] = {
  price: 849.00,
  page_count_min_price: 0.00,
  page_count_max_price: 0.00,
  page_count_enabled: false,
  page_count_min: 0,
  page_count_max: 0,
  page_count_step: 0
};


var quantities = new Array();

quantities[70] = 25;

quantities[71] = 50;

quantities[72] = 100;

quantities[73] = 200;

quantities[74] = 300;

quantities[75] = 400;

quantities[76] = 500;

quantities[77] = 600;

quantities[78] = 700;

quantities[79] = 800;

quantities[80] = 900;

quantities[81] = 1000;

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=38', {asynchronous:true, evalScripts:true, method:'get', parameters:'authenticity_token=' + encodeURIComponent('5PHaZfgAuvix0W0wL62SnU7xtcrLU2ddDOrRSE95rd8=')})
  

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