function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name + "=" + value + expires + "; path=/;";
}

function calculate_price(block_container, formula_container, discount_container, output_container) {
  var _width  = isNaN($("#num1").val() * 1) ? 0 : Math.abs($("#num1").val() * 1) ;
  var _height = isNaN($("#num2").val() * 1) ? 0 : Math.abs($("#num2").val() * 1) ;

   $(block_container).each(function(index) {
    _discount = 0;
    formula  = $(this).find(formula_container).text();
    discount = $(this).find(discount_container).text();
    
    _price    = total_price1(_width, _height, formula);
    _discount = total_price1(_width, _height, discount);
    _res = Math.round(_price - _discount);
    
    if(_discount > 0) {
      $(this).find(".economy").show().find(".red").text(_discount);
      }
    
    $(this).find(output_container).text(_res);
  });

  $(".out_width").text(_width);
  $(".out_height").text(_height);
  
  createCookie('out_width', _width, 365);
  createCookie('out_height', _height, 365);
}
  
function total_price1(w, h, formula) {
  if (formula == '') {
    formula = '0';
    }
  total = eval (formula);
  total = Math.round(total*100) / 100;
  return total;
  }


$().ready(function(){
  calculate_price('.item','.formula','.discount','.price span');
  
  $("#num1, #num2").bind('keyup change blur propertychange',function(e) {
    e.preventDefault();
    calculate_price('.item','.formula','.discount','.price span');
    
  });  
});
