// JavaScript Document

// On first page load the folowing script checks if the cookie "cart" exists

var  objCount ;

function detailCalc() {
	if ($('.jsMainItemQuantity').val() == "1") {
	basePrice = val1Price;
	}else if ($('.jsMainItemQuantity').val() == "2") {
	basePrice = val2Price;
	}else if ($('.jsMainItemQuantity').val() == "3") {
	basePrice = val3Price;
	}else if ($('.jsMainItemQuantity').val() == "4") {
	basePrice = val4Price;
	}else if ($('.jsMainItemQuantity').val() == "5") {
	basePrice = val5Price;
	}else if ($('.jsMainItemQuantity').val() == "1.5 Mbps") {
	basePrice = val1Price;
	}else if ($('.jsMainItemQuantity').val() == "7 Mbps") {
	basePrice = val2Price;
	}else if ($('.jsMainItemQuantity').val() == "12 Mbps") {
	basePrice = val3Price;
	}else if ($('.jsMainItemQuantity').val() == "20 Mbps") {
	basePrice = val4Price;
	}else if ($('.jsMainItemQuantity').val() == "40 Mbps") {
	basePrice = val5Price;
	};
	
	if ($('.jsServiceAgreement').val() == "none") {
	agreement = noAgree;
	}else if ($('.jsServiceAgreement').val() == "1 yr") {
	agreement = oneYearAgree;
	}else if ($('.jsServiceAgreement').val() == "2 yr") {
	agreement = twoYearAgree;
	}else if ($('.jsServiceAgreement').val() == "3 yr") {
	agreement = threeYearAgree;
	} else {
	agreement = agreement;
	};
	if ($("input:radio[name='jsSubItemValue']:checked").val() == "DXLY6") {
	modemPrice = buyPremiumModem;
	modemName="Buy Premium Modem";
	}else if ($("input:radio[name='jsSubItemValue']:checked").val() == "DXLY7") {
	modemPrice = rentPremiumModem;
	modemName="Rent Premium Modem";
	}else if ($("input:radio[name='jsSubItemValue']:checked").val() == "DXLY4") {
	modemPrice = buyStandardModem;
	modemName="Buy Standard Modem";
	}else if ($("input:radio[name='jsSubItemValue']:checked").val() == "DXLY5") {
	modemPrice = rentStandardModem;
	modemName="Rent Standard Modem";
	}else if ($("input:radio[name='jsSubItemValue']:checked").val() == "No Modem") {
	modemPrice = noModem;
	modemName="No Modem";
	}else {
	modemPrice = 0;
	modemName="";
	};
	
	var price = (basePrice * agreement)
	
	price = price.toFixed(2);
	var priceArr = price.split(".");
	var dolVal=priceArr[0];
	var centVal = priceArr[1];
	// this applies all the variable text to the detail page//
	$('.jsEstPrice span').html(price);
	$('.jsBigPrice').html('<span>$</span>'+dolVal+'<span>'+centVal+'*</span>');
	$('.jsLineNum').html($('.jsMainItemQuantity').val()+" Lines");
	$('.jsSpeedNum').html("Up to "+$('.jsMainItemQuantity').val());
	$('.jsLinePrice').html("$"+basePrice.toFixed(2));
	$('.jsAgreeTerms').html($('.jsServiceAgreement').val()+'. - '+(100-(agreement*100))+'% off');
	$('.jsOneTimeCharge').html('$'+oneTimeCharge.toFixed(2));
	
	$('.jsModem').html(modemName);
	$('.jsModemPrice ').html(modemPrice); 
	//;
};

function cartCalc () {
	
	var itemVal;
	var itemCount = 1;
	var reoccurrTotal = 0;
	var oneTimeTotal = 0;
	
	$(".jsROItemID").each( function (){
		var itemID= ($(this).val());
		var myCart = $.cookieJar('myCart');
		var cartObject = myCart.toObject();
		var itemQuantity = cartObject[itemID].quantity
		var itemVal = $(this).parent().find('.jsItemVal').val();
		var itemTotal= (itemQuantity*itemVal);
		reoccurrTotal = reoccurrTotal + itemTotal;
		$(this).parent().find('.jsQuantVal').html("X "+itemQuantity);
		$(this).parent().find('.jsItemValHtml').html("$"+itemVal);
		$(this).parent().find('.jsItemQuantity').html("$"+itemVal+" X "+itemQuantity);
		$(this).parent().find('.jsItemTotal').html("$"+itemTotal.toFixed(2));
		
	});
		
	$(".jsOTItemID").each( function (){
		var itemID= ($(this).val());
		var myCart = $.cookieJar('myCart');
		var cartObject = myCart.toObject();
		var itemQuantity = cartObject[itemID].quantity
		
		var itemVal = $(this).parent().find('.jsItemVal').val();
		var itemTotal= (itemQuantity*itemVal);
		oneTimeTotal = oneTimeTotal + itemTotal;
		
		$(this).parent().find('.jsQuantInVal').val(itemQuantity);
		$(this).parent().find('.jsQuantVal').html("X "+itemQuantity);
		$(this).parent().find('.jsItemValHtml').html("$"+itemVal);
		$(this).parent().find('.jsItemQuantity').html("$"+itemVal+" X "+itemQuantity);
		$(this).parent().find('.jsItemTotal').html("$"+itemTotal.toFixed(2));
		
	});
	var subTotal = reoccurrTotal + oneTimeTotal;
	
	var jsDiscount = $(".jsDiscount").val();
	var estTotal = subTotal - jsDiscount;
	
	price = estTotal.toFixed(2);
	var priceArr = price.split(".");
	var dolVal=priceArr[0];
	var centVal = priceArr[1];
	$('.jsBigPrice').html('<span>$</span>'+dolVal+'<span>'+centVal+'*</span>');
	$('.jsReoccurringTotal').html("$"+reoccurrTotal.toFixed(2));
	$('.jsOneTimeTotal').html("$"+oneTimeTotal.toFixed(2));
	$('.jsSubTotal').html("$"+subTotal.toFixed(2));
	$('.jsDiscoutVal').html("$"+jsDiscount);
	$('.jsEstTotal').html("$"+estTotal.toFixed(2));
}

/* Detail Add to cart Function */
$(document).ready(function () {
	
	
//Builds the Local Voice cart objects
	$('.js_add_voice_config_to_cart').click(function () {
		var myCart = $.cookieJar('myCart');
		/*Primary Item*/				
		var mainItemValue = ($('.jsMainItemValue').val());/**/
		//pulls the quantity val
		var mainItemQuantity = ($('.jsMainItemQuantity').val());
		var agreementValue =   ($('.jsServiceAgreement').val());
		
		var attributeList = "";
		
		itemSize = $('.jsFeature:checked').size();
		
		$(".jsFeature:checked").each( function () {
		if (itemSize > 1) {
			attributeList = ( attributeList + $(this).val()+",");
			--itemSize;
			}
			else {
			attributeList = ( attributeList + $(this).val());
			}
		});
		
		//The cookie Values Concat
		var itemValues = {"quantity": mainItemQuantity ,"agreement": agreementValue ,"attributes": [ attributeList ]};
		
		//Sets the cookie myCart
		myCart.set(mainItemValue, itemValues);
		
	});	
	
//Builds the Internet cart objects
	$('.js_add_config_to_cart').click(function () {
		var myCart = $.cookieJar('myCart');
		/*Primary Item*/				
		var mainItemValue = ($('#jsMainItemValue').val());/**/
		//pulls the quantity val
		var mainItemQuantity =($('#jsMainItemQuantity').val());
		var agreementValue =   ($('.jsServiceAgreement').val());
		//pulls all the item attributes into a list
		itemSize = $('.jsMainItemAttributes').size();
		var attributeList = "" ;
		$('.jsMainItemAttributes').each(function(){				 
			if (itemSize > 1) {
			attributeList = ( attributeList + $(this).val()+",");
			--itemSize;
			}
			else {
			attributeList = ( attributeList + $(this).val());
			}
		});	
		//The cookie Values Concat
		var itemValues = {'quantity': mainItemQuantity ,"agreement": agreementValue, 'attributes': [attributeList]};
		//Sets the cookie myCart
		myCart.set(mainItemValue, itemValues);
		
		//pulls the subitem		
		var subItemValues = {'quantity': 1};
		var subItemValue = $("input:radio[name='jsSubItemValue']:checked").val();
		myCart.set(subItemValue, subItemValues);
		
	});
	
//Builds the Internet Transport Only cart objects
	$('.js_add_transport_to_cart').click(function () {
		var myCart = $.cookieJar('myCart');
		//pulls the MainItem		
		var mainItemValue = $("input:radio[name='jsMainItemValue']:checked").val();
		//pulls the quantity val
		var mainItemQuantity =($('#jsMainItemQuantity').val());
		//The cookie Values Concat
		var itemValues = {'quantity': mainItemQuantity };
		//Sets the cookie myCart
		myCart.set(mainItemValue, itemValues);
			
	});		
//Builds the Interstitial cart objects and callout cart objects
	$('.js_add_inter_to_cart').click(function () {
		var myCart = $.cookieJar('myCart');
		//pulls the MainItem		
		var mainItemValue = ($(this).parent().find('.jsMainItemValue').val());/**/
		//pulls the quantity val
		var mainItemQuantity =($(this).parent().find('.jsMainItemQuantity').val());
		//The cookie Values Concat
		var itemValues = {'quantity': mainItemQuantity };
		//Sets the cookie myCart
		myCart.set(mainItemValue, itemValues);
		
	});	
//Builds the Non-Config Detail cart objects and callout cart objects
	$('.js_add_nonconfig_to_cart').click(function () {
		var myCart = $.cookieJar('myCart');
		//pulls the MainItem		
		var mainItemValue = ($('.jsMainItemValue').val());/**/
		//pulls the quantity val
		var mainItemQuantity =($('.jsMainItemQuantity').val());
		//The cookie Values Concat
		var itemValues =  {'quantity': mainItemQuantity };
		//Sets the cookie myCart
		myCart.set(mainItemValue, itemValues);
	});	
	

	
	
//This sets the cart item number value
	var num = 0  ;
	var myCart = $.cookieJar('myCart');
	var cartObject = myCart.toObject();
	jQuery.each(cartObject, function() {
	num = num + 1
	});
	$('div.sbr_cart_ticker a').html("Your cart has "+num+" items");
//This sets the configuration values up.
	$('.jsMainItemQuantity').change(detailCalc); 
	$('.jsServiceAgreement').change(detailCalc); 
	$("input:radio[name='jsSubItemValue']").click(detailCalc);
	
//This controls the Cart Quantity Update ///////////////////////////////////////////////
	$('.jsQuantInVal').change(function () {
		var quantity = $(this).val();
		quantity = quantity * 1;
		
		if (isNaN (quantity) == true) {
			alert ("Try again. This is not a number")
		} else {
			//pulls the MainItem
			var itemID= ($(this).parents('div.sbr_cart_summary_information').find('.jsOTItemID').val());
			var myCart = $.cookieJar('myCart');	
			//The cookie Values Concat
			var itemValues =  {'quantity': quantity };
			//Sets the cookie myCart
			myCart.set(itemID, itemValues);
			cartCalc();
		}
	});	
//This removes items from the cart //////////////////////////////////////////////////
	$('.jsRemove').click(function () {
		var myCart = $.cookieJar('myCart');
		var itemID= ($(this).parents('div.sbr_cart_summary_information').find('input').val());
		myCart.remove(itemID);
		$(this).parents('div.sbr_cart_summary_information').hide();
		window.location=window.location;
		
	});
//This removes items from the cart //////////////////////////////////////////////////
	$('.jsRemoveAll').click(function () {
		var myCart = $.cookieJar('myCart');
		myCart.remove();
		window.location=window.location;		
	});
	
	cartCalc();
});





