// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var stripeLineItems = function() {
	$('#line_items tr:odd').removeClass('light');
	$('#line_items tr:odd').addClass('dark');
	
	$('#line_items tr:odd').removeClass('dark');
	$('#line_items tr:even').addClass('light');
}

var resetLineItemForm = function() {
	$('#line_item_quantity').val(0);
	$('#line_item_vendor_id').focus();
	calculateFaceValue();
}

var calculateFaceValue = function () {
	var l_qty = $('#line_item_quantity').val();
	var l_denom = $('#line_item_denomination').val();
	
	var l_faceValue = l_qty * (l_denom / 100);
	
	$('#face_value_container').text("$" + l_faceValue.toFixed(2));
}

var bindCalculateFaceValueFunctions = function() {
	$('#line_item_quantity').change(calculateFaceValue);
	$('#line_item_denomination').change(calculateFaceValue);
}

function coordinatorOrderRecalc() {
	calculateFaceValueTotal();
	calculateDiscountTotal();
}

function toggleIncludeClass() {
	$(this).parents('tr').children('.individual_order_face_value_total').toggleClass('include');
	$(this).parents('tr').children('.individual_order_discount_total').toggleClass('include');
	coordinatorOrderRecalc();
}

function calculateFaceValueTotal() {
	var sum = $(".li_face_value_total").sum() + $(".individual_order_face_value_total.include").sum();
	
	$("#face_value_total").text("$" + sum.toFixed(2));
}

function calculateDiscountTotal() {
	var sum = $(".li_discount_total").sum() + $(".individual_order_discount_total.include").sum();
	
	$("#discount_total").text("$" + sum.toFixed(2));
}

function calculateLineItemFaceValueTotals() {
	// run the calc() method on each of the "total" fields 
   $("[@id^=face_value_line_item]").calc( 
      // the equation to use for the calculation 
      "qty * (price/100)", 
      // we now define the values for the variables defined in the equation above 
      { 
         // instead of using a static value, we use a jQuery object which grabs all the quantities 
         qty: $("input[@name^=order][@id$=quantity]"), 
         // now we define the jQuery object which reads in the "price" from the table cell 
         price: $("input[@name^=order][@id$=denomination]") 
      }, 
      // this function is execute after the calculation is completed, which allows us to 
      // add formatting to our value 
      function (s){ 
         // return the number as a dollar amount 
         return "$" + s.toFixed(2); 
      }, 
      // once all calculations are completed, we execute the code below 
      function ($this){ 
         // now we get the sum() of all the values we just calculated 
         var sum = $this.sum() + $(".individual_order_face_value_total.include").sum(); 

         // now that we have the grand total, we must update the screen 
         $("#face_value_total").text( 
            // round the results to 2 digits 
            "$" + sum.toFixed(2) 
         ); 
      } 
   );
}

function calculateLineItemDiscountTotals() {
	// run the calc() method on each of the "total" fields 
   $("[@id^=discount_total_line_item]").calc( 
      // the equation to use for the calculation 
      "qty * (price/100) * ((100-discount)/100)", 
      // we now define the values for the variables defined in the equation above 
      { 
         qty: $("input[@name^=order][@id$=quantity]"), 
         price: $("input[@name^=order][@id$=denomination]"),
         discount: $("[@id^=discount_line_item]")
      }, 
      // this function is execute after the calculation is completed, which allows us to 
      // add formatting to our value 
      function (s){ 
         // return the number as a dollar amount 
         return "$" + s.toFixed(2); 
      }, 
      // once all calculations are completed, we execute the code below 
      function ($this){ 
         // now we get the sum() of all the values we just calculated 
         var sum = $this.sum() + $(".individual_order_discount_total.include").sum(); 

         // now that we have the grand total, we must update the screen 
         $("#discount_total").text( 
            // round the results to 2 digits 
            "$" + sum.toFixed(2) 
         ); 
      } 
   );
}

function calculateIndividualLineItemFaceValueTotals() {
	// run the calc() method on each of the "total" fields 
   $("[@id^=face_value_line_item]").calc( 
      // the equation to use for the calculation 
      "qty * (price/100)", 
      // we now define the values for the variables defined in the equation above 
      { 
         // instead of using a static value, we use a jQuery object which grabs all the quantities 
         qty: $("input[@name^=individual_order][@id$=quantity]"), 
         // now we define the jQuery object which reads in the "price" from the table cell 
         price: $("select[@name^=individual_order][@id$=denomination]")
      }, 
      // this function is execute after the calculation is completed, which allows us to 
      // add formatting to our value 
      function (s){ 
         // return the number as a dollar amount 
         return "$" + s.toFixed(2); 
      }, 
      // once all calculations are completed, we execute the code below 
      function ($this){ 
         // now we get the sum() of all the values we just calculated 
         var sum = $this.sum(); 

         if(sum >= 1000)
         {
           enableIndividualShipping();
         }
         else
         {
           disableIndividualShipping();
         }

         // now that we have the grand total, we must update the screen 
         $("#face_value_total").text( 
            // round the results to 2 digits 
            "$" + sum.toFixed(2) 
         ); 
      } 
   );
}

function calculateIndividualLineItemDiscountTotals() {
	// run the calc() method on each of the "total" fields 
   $("[@id^=discount_total_line_item]").calc( 
      // the equation to use for the calculation 
      "qty * (price/100) * ((100-discount)/100)", 
      // we now define the values for the variables defined in the equation above 
      { 
         // instead of using a static value, we use a jQuery object which grabs all the quantities 
         qty: $("input[@name^=individual_order][@id$=quantity]"), 
         // now we define the jQuery object which reads in the "price" from the table cell 
         price: $("select[@name^=individual_order][@id$=denomination]"),
         discount: $("[@id^=discount_line_item]")
      }, 
      // this function is execute after the calculation is completed, which allows us to 
      // add formatting to our value 
      function (s){ 
         // return the number as a dollar amount 
         return "$" + s.toFixed(2); 
      }, 
      // once all calculations are completed, we execute the code below 
      function ($this){ 
         // now we get the sum() of all the values we just calculated 
         var sum = $this.sum(); 

         // now that we have the grand total, we must update the screen 
         $("#discount_total").text( 
            // round the results to 2 digits 
            "$" + sum.toFixed(2) 
         ); 
      } 
   );
}

function calculateIndividualLineItemEarnings() {
   var result = ($("#face_value_total").parseNumber() - $("#discount_total").parseNumber());
   $("#earnings_total").text("$" + result.toFixed(2));
}

function bindLineItemCalculationFunctions() {
	$("input[@name^=order][@id$=quantity]").bind("keyup", calculateLineItemFaceValueTotals);
	$("input[@name^=order][@id$=quantity]").bind("keyup", calculateLineItemDiscountTotals);
}

function calculateIndividualLineItemTotals() {
	calculateIndividualLineItemFaceValueTotals();
	//calculateIndividualLineItemDiscountTotals();
	//calculateIndividualLineItemEarnings();
}

function bindIndividualLineItemCalculationFunctions() {
	$("input[@name^=individual_order][@id$=quantity]").bind("keyup", calculateIndividualLineItemTotals);
}

function enableIndividualShipping() {
	$("#ship-to-individual").show();
}

function disableIndividualShipping() {
	$("#ship-to-individual").hide();
}

function sortLineItemsTable() {
	$("#line_items_table").trigger("update");
	var sorting = [[0,0],[2,0]];
	$("#line_items_table").trigger("sorton", [sorting]);
}

function scrollToAddLineItem () {
	var target = $('#add_line_item');
	if (target.length) {
		var top = target.offset().top;
		$('html,body').animate({scrollTop: top}, 1000);
		return false;
	}
}