﻿/// <reference path="jquery-1.3.1-vsdoc.js" />
function Page_Window_Load(e)
{
	var cartTable = $("table[id$='gvCartItems']");
	$("input",cartTable).blur(function(e){UpdateQuantity(this)});
}
function UpdateQuantity(tb)
{
	/*Note: the shopping cart control should render out with rel=id,qty for every input
	That allows us compare the new value to the data value and therefore not ping the server for meaningless updates.
	It also gives us an easy way to get the id from the textbox that is blurred for our ajax call*/
	var relValues = (tb.attributes["rel"].value).split(',');
	var cartItemId = relValues[0];
	var dataQty = relValues[1];
	var newQty = parseInt(tb.value);
	if(newQty<=0)
	{
		tb.value = dataQty;
	}
	else if(newQty != dataQty)
	{
		//if our quantity is a new value then we call the update call
		//NOTE: this uses a different method that allows an ajax call to actually run in context of a given user and cart
		//static functions such as those that $.ajax can call lose that awareness and I would rather not put guids in the markup.
		//this allows us to make sure that the user is logged in etc and is more secure than a call to a static function like $.ajax does.
		//This is using the older ajax methodology that was already implemented.  UpdateCustomerCart was already worked out in the callbackmanager class as well
		 CallServer("UpdateCustomerCart:CartItemID=" + cartItemId + "&Qty=" + newQty);
		
	}
}

function ReceiveServerData(args)
{
	var splits = args.split(',');
	
	if(splits[0]=="true")
	{
		//update the rel value so that we have the latest value in data in our rel tag.
		var cartTable = $("table[id$='gvCartItems']");
		$("input[id$='_"+splits[1]+"']",cartTable).attr("rel",splits[1]+","+splits[2]);

    }
    var sumOfPrices = 0;

    $(".row").each(function(i) {
    var qty = parseInt(trim($(this).find(".QuantityCell input").val()));
        var numberStr = trim($(this).find(".price").text());
        var cost = parseFloat(numberStr.substring(1, numberStr.length));
        sumOfPrices += (cost * qty);
    });
    //Figure out 12% Damage Waver
    var damageWaver = sumOfPrices * .12;
    //Update the fields
    $("#dmgWaver").text("$" + damageWaver.toFixed(2));
    $("#totalEstimate").text("$" + (damageWaver+sumOfPrices).toFixed(2))
   }
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
