$().ready(function() {

	var averageDiscount = 0.2; // average 20% discount
	var powerBuyDiscount = 0.66; // the discounts we offer will cover 2/3rd of a typical small business needs

	if($("#Saving").val() > 0){
		$("#noValSavingText").css('display','none');
		$("#savingText").css('display','');
		$("#noResponse").css('display','none');
	}else{
		$("#noValSavingText").css('display','');
		$("#savingText").css('display','none');
		$("#noResponse").css('display','none');
		$("#moreAccurate").css('display','none');
	}

	$("#machineQTY").change(function(){
		$("#patchesQTY").val($("#machineQTY").val());
		$("#productivitySupportQTY").val($("#machineQTY").val());
		$("#otherSupportQTY").val($("#machineQTY").val());
		$("#hideMachineQTY").val($("#machineQTY").val());
		$("#txtMachineQTY").html('Total ' + $("#machineQTY").val() );
	});

	$("#industry").change(function(){
		$("#hideIndustry").val($("#industry").val());
	});
	
	$(".monthly").change(function(){

		var tmp1 = $("#tonerQTY").val() * $("#tonerPPU").val();
		var tmp2 = $("#networkSupportHRS").val() * $("#networkSupportRPH").val();
		var tmp3 = $("#outsourceSupportHRS").val() * $("#outsourceSupportRPH").val();

		var tmpResult = tmp1+tmp2+tmp3;
		var finalResult = tmpResult.toFixed(2);

		$("#subTotalMonthlyCosts").val(finalResult);
		$("#txtSubTotalMonthlyCosts").html("$" + finalResult);

	});

	$(".annual").change(function(){

		var tmp1 = $("#patchesQTY").val() * $("#patchesPPL").val();
		var tmp2 = $("#accountingSupportQTY").val() * $("#accountingSupportPPL").val();
		var tmp3 = $("#productivitySupportQTY").val() * $("#productivitySupportPPL").val();
		var tmp4 = $("#otherSupportPPL").val() * 1;

		var tmpResult = tmp1+tmp2+tmp3+tmp4;
		var finalResult = tmpResult.toFixed(2);

		$("#subTotalAnnualCosts").val(finalResult);
		$("#txtSubTotalAnnualCosts").html("$" + finalResult);


	});
	$(".threeyears").change(function(){

		var tmp1 = $("#replacementMachineQTY").val() * $("#replacementMachinePPU").val();
		var tmp2 = $("#machineUpgradeQTY").val() * $("#machineUpgradePPU").val();
		var tmp3 = $("#machineGrowthQTY").val() * $("#machineGrowthPPU").val();
		var tmp4 = $("#printerGrowthQTY").val() * $("#printerGrowthPPU").val();

		var tmpResult = tmp1+tmp2+tmp3+tmp4;
		var finalResult = tmpResult.toFixed(2);

		$("#subTotalThreeYearCosts").val(finalResult);
		$("#txtSubTotalThreeYearCosts").html("$" + finalResult);

	});

	$(".additional").change(function(){

		var tmp1 = $("#additionalSpend").val() * 1;
		var tmp2 = $("#additionalSpendExtra").val() * 1;
		var tmp3 = $("#additionalExpenses").val() * 1;

		var tmpResult = tmp1+tmp2+tmp3;
		var finalResult = tmpResult.toFixed(2);


		$("#subTotalAdditionalCosts").val(finalResult);
		$("#txtSubTotalAdditionalCosts").html("$" + finalResult);

	});


	$('.calculator').change(function(){
		var tmp1 = $("#subTotalMonthlyCosts").val() * 36;
		var tmp2 = $("#subTotalAnnualCosts").val() * 3;
		var tmp3 = $("#subTotalThreeYearCosts").val()*1 + tmp1 + tmp2;
		var tmp4 = tmp3 + $("#additionalSpend").val()*1 + $("#additionalSpendExtra").val()*1 + $("#additionalExpenses").val()*1;
		$("#TotalSpend").val(tmp4.toFixed(2));

		var tmpTotalSpendPerSeat = $("#TotalSpend").val() / $("#machineQTY").val();
		//tmpTotalSpendPerSeat = Math.round(tmpTotalSpendPerSeat/10.0) * 10.0;
		$("#TotalSpendPerSeat").val( tmpTotalSpendPerSeat.toFixed(2) );

		var tmpSaving = ( $("#TotalSpend").val() - $("#additionalExpenses").val() ) * averageDiscount * powerBuyDiscount;
		tmpSaving = Math.round(tmpSaving/10.0) * 10.0;
		$("#Saving").val( tmpSaving.toFixed(2) );

		var tmp5 = $("#Saving").val() - $("#additionalSpendExtra").val();
		var tmp6 = $("#networkSupportHRS").val() * $("#networkSupportRPH").val();
		var tmp7 = $("#outsourceSupportHRS").val() * $("#outsourceSupportRPH").val();

		var tmpSavingExcServices = tmp5-(tmp6+tmp7);
		$("#SavingExcServices").val( tmpSavingExcServices.toFixed(2) );

		if (tmpSaving > 0){
			$("#txtSaving").html(tmpSaving.toFixed(2));
			$("#noValSavingText").css('display','none');
			$("#savingText").css('display','');
		}else{
			$("#noValSavingText").css('display','');
			$("#savingText").css('display','none');
		}


	});
	
	$("#calculatorForm").submit(function(){
		if (activeSlide != 5) {
			alert('Please complete the calculator before continuing');
			return false;
		}
	});
	
	
	if($.validator) {

		$("#calculatorForm").validate();
		$("#smCalculatorForm").validate();

	}

	$(".reset").click(function(){
		if(confirm("Are you sure you want to reset your calculator?")){
			$('.calculator').val('0');
			$('.subtotal').val('0.00');
			$('.totals').val('');
			$("#txtSubTotalMonthlyCosts").html("$0.00");
			$("#txtSubTotalAnnualCosts").html("$0.00");
			$("#txtSubTotalThreeYearCosts").html("$0.00");
			$("#txtSubTotalAdditionalCosts").html("$0.00");
			$("#txtMachineQTY").html("0");
			$("#noValSavingText").css('display','');
			$("#savingText").css('display','none');
			wizard.activate(0);
		}
	});

	//accordion
	var activeSlide = 0;
	var wizard = $("#wizard").accordion({
	    header: '.title',
	    event: false,
		autoHeight: false
	});

	$("div.title", wizard).each(function(index) {
	    $(this)
	    .next()
	    .children("img")
	    .filter(".next, .previous")
	    .click(function() {
			//alert('');
			if($(this).attr('id') == 'machineNext'){

				if ($("#machineQTY").val() > 0 && $("#industry option:selected").val() != 0) {
					activeSlide = index + ($(this).is(".next") ? 1 : -1);
					wizard.activate(activeSlide);
				}else if ($("#industry").val() <= 0) {
					alert('Please enter the industry you operate in');
				}else if ($("#machineQTY").val() <= 0) {
					alert('Please enter the number of computers you have');
				};
				
			}else{
				activeSlide = index + ($(this).is(".next") ? 1 : -1);
				wizard.activate(activeSlide);
			}
			
	    });
	});
	
	$(".smcalculator").change(function(){

		//pass in industryID and employee numbers to cf
		var averageSpend = 0;

		if($('#machineQTY').val() > 0 && $('#industry').val() > 0){

			$.AjaxCFC({
				url: "/org/red5/ajax/calculator.cfc",
				method: "getWeighting",
				data: {"numberEmployees":$('#machineQTY').val(),"IndustryID":$('#industry').val()},
				blockMessage:'<h3>Please wait while we fetch data about your industry. This wont take long.</h3>',
				error: function(r){
					//no action required
					$("#noValSavingText").css('display','none');
					$("#savingText").css('display','none');
					$("#noResponse").css('display','');
					$("#moreAccurate").css('display','none');
				},
				success: function(r) {
						averageSpend = r * 1;
						//sDumper(r);

						var totalSaving = averageSpend * averageDiscount * powerBuyDiscount *3;// ave industry spend * discounts
						//var totalSaving = (averageSpend - spendViaPowerBuy) * 3;// time 3 years

						if (totalSaving > 0){
							totalSaving = Math.round(totalSaving/10.0) * 10.0;
							$("#txtSaving").html(totalSaving);
							$("#Saving").val(totalSaving);
							$("#noValSavingText").css('display','none');
							$("#savingText").css('display','');
							$("#moreAccurate").css('display','');
						}else{
							$("#noValSavingText").css('display','');
							$("#savingText").css('display','none');
						}
				}
			});

		}

	});

});

