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

colors[41] = '';

var prices = new Array();

prices[30] = new Array();

prices[30][39] = new Array();

prices[30][39][37] = new Array();

prices[30][39][38] = new Array();

prices[30][39][39] = new Array();

prices[30][39][40] = new Array();

prices[30][39][41] = new Array();

prices[30][39][42] = new Array();

prices[30][39][43] = new Array();

prices[30][39][44] = new Array();

prices[30][41] = new Array();

prices[30][41][37] = new Array();

prices[30][41][38] = new Array();

prices[30][41][39] = new Array();

prices[30][41][40] = new Array();

prices[30][41][41] = new Array();

prices[30][41][42] = new Array();

prices[30][41][43] = new Array();

prices[30][41][44] = new Array();
prices[30][39][37] = {
  price: 385.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[30][41][37] = {
  price: 138.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[30][39][38] = {
  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[30][41][38] = {
  price: 148.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[30][39][39] = {
  price: 418.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[30][41][39] = {
  price: 165.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[30][39][40] = {
  price: 460.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[30][41][40] = {
  price: 207.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[30][39][41] = {
  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[30][41][41] = {
  price: 275.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[30][39][42] = {
  price: 628.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[30][41][42] = {
  price: 343.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[30][39][43] = {
  price: 712.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[30][41][43] = {
  price: 411.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[30][39][44] = {
  price: 796.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[30][41][44] = {
  price: 479.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[31] = new Array();

prices[31][39] = new Array();

prices[31][39][37] = new Array();

prices[31][39][38] = new Array();

prices[31][39][39] = new Array();

prices[31][39][40] = new Array();

prices[31][39][41] = new Array();

prices[31][39][42] = new Array();

prices[31][39][43] = new Array();

prices[31][39][44] = new Array();

prices[31][41] = new Array();

prices[31][41][37] = new Array();

prices[31][41][38] = new Array();

prices[31][41][39] = new Array();

prices[31][41][40] = new Array();

prices[31][41][41] = new Array();

prices[31][41][42] = new Array();

prices[31][41][43] = new Array();

prices[31][41][44] = new Array();
prices[31][39][37] = {
  price: 235.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[31][41][37] = {
  price: 183.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[31][39][38] = {
  price: 383.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[31][41][38] = {
  price: 223.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[31][39][39] = {
  price: 585.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[31][41][39] = {
  price: 287.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[31][39][40] = {
  price: 647.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[31][41][40] = {
  price: 391.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[31][39][41] = {
  price: 768.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[31][41][41] = {
  price: 495.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[31][39][42] = {
  price: 889.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[31][41][42] = {
  price: 599.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[31][39][43] = {
  price: 1010.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[31][41][43] = {
  price: 703.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[31][39][44] = {
  price: 1131.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[31][41][44] = {
  price: 807.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[37] = new Array();

prices[37][39] = new Array();

prices[37][39][37] = new Array();

prices[37][39][38] = new Array();

prices[37][39][39] = new Array();

prices[37][39][40] = new Array();

prices[37][39][41] = new Array();

prices[37][39][42] = new Array();

prices[37][39][43] = new Array();

prices[37][39][44] = new Array();

prices[37][41] = new Array();

prices[37][41][37] = new Array();

prices[37][41][38] = new Array();

prices[37][41][39] = new Array();

prices[37][41][40] = new Array();

prices[37][41][41] = new Array();

prices[37][41][42] = new Array();

prices[37][41][43] = new Array();

prices[37][41][44] = new Array();
prices[37][39][37] = {
  price: 229.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[37][41][37] = {
  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[37][39][38] = {
  price: 373.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[37][41][38] = {
  price: 212.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[37][39][39] = {
  price: 565.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[37][41][39] = {
  price: 265.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[37][39][40] = {
  price: 622.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[37][41][40] = {
  price: 314.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[37][39][41] = {
  price: 736.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[37][41][41] = {
  price: 412.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[37][39][42] = {
  price: 850.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[37][41][42] = {
  price: 510.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[37][39][43] = {
  price: 964.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[37][41][43] = {
  price: 608.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[37][39][44] = {
  price: 1078.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[37][41][44] = {
  price: 706.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[82] = new Array();

prices[82][39] = new Array();

prices[82][39][37] = new Array();

prices[82][39][38] = new Array();

prices[82][39][39] = new Array();

prices[82][39][40] = new Array();

prices[82][39][41] = new Array();

prices[82][39][42] = new Array();

prices[82][39][43] = new Array();

prices[82][39][44] = new Array();

prices[82][41] = new Array();

prices[82][41][37] = new Array();

prices[82][41][38] = new Array();

prices[82][41][39] = new Array();

prices[82][41][40] = new Array();

prices[82][41][41] = new Array();

prices[82][41][42] = new Array();

prices[82][41][43] = new Array();

prices[82][41][44] = new Array();
prices[82][39][37] = {
  price: 860.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[82][41][37] = {
  price: 574.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[82][39][38] = {
  price: 897.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[82][41][38] = {
  price: 596.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[82][39][39] = {
  price: 1003.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[82][41][39] = {
  price: 895.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[82][39][40] = {
  price: 1145.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[82][41][40] = {
  price: 790.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[82][39][41] = {
  price: 1430.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[82][41][41] = {
  price: 1006.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[82][39][42] = {
  price: 1715.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[82][41][42] = {
  price: 1222.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[82][39][43] = {
  price: 1999.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[82][41][43] = {
  price: 1438.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[82][39][44] = {
  price: 2283.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[82][41][44] = {
  price: 1654.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[37] = 100;

quantities[38] = 250;

quantities[39] = 500;

quantities[40] = 1000;

quantities[41] = 2000;

quantities[42] = 3000;

quantities[43] = 4000;

quantities[44] = 5000;

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

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