var mode = 1; //advanced
$(document).ready(
	function() 
	{
		$("input.green-text-box").change(function(){performROICalculations();});
		$("input.green-text-box").keyup(function(){performROICalculations();});
		
		$("#lnkToggle").toggle( 
			function () 
			{ 
			     setModeAdvanced();
			}, 
			function () 
			{ 
			    setModeSimple(); 
			});
		
		//$("a.showtip").mouseover(function(){showtooltip();});
		//$("a.showtip").mouseout(function(){kill();});

		//$("#lnkToggle").click(function(){toggleMode();});
		$('a.showtip').tooltip({showURL: false});
		
		setModeSimple();
		$('#txtSeats').val("50")
		performROICalculations();
   	}
);
function showtooltip()
{
	//alert("hello");
	popup('click to see all ');	
}
function setModeSimple()
{
	$("input.green-text-box").attr("disabled", true);
	$("input.green-text-box").css("position", "relative");
	$("input.green-text-box").css("z-index", "-1");
	$('#txtSeats').removeAttr("disabled");
	$('#txtSeats').css("position","static");
	$('#txtSeats').css("z-index","0");
	$('#txtCRI').removeAttr("disabled");
	$('#txtCRI').css("position","static");
	$('#txtCRI').css("z-index","0");
	mode = 1;
	$("#lnkToggle").html("Toggle Advanced Mode");
}

function setModeAdvanced()
{
	$("input.green-text-box").removeAttr("disabled");
	$("input.green-text-box").css("position", "static");
	$("input.green-text-box").css("z-index", "0");
	$('#txtSeats').attr("disabled", true);
	$('#txtSeats').css("position", "relative");
	$('#txtSeats').css("z-index", "-1");
	mode = 2;
	$("#lnkToggle").html("Toggle Simple Mode");
}

function toggleMode()
{
	if(mode == 1) // is simple
	{
		mode = 2;
	}
	else   // is avanced
	{
		mode = 1;
	}
	return false;
	
}

function performROICalculations()
{
	if (typeof roiCurrencySymbol == "undefined") {
		currencySymbol = "$";	//set currency here
	} else {
		currencySymbol = roiCurrencySymbol;
	}

	if(mode == 1)
	{
		var H41 = $("#txtSeats").val();
		$("#txtTPMonth").val( Math.round(H41 * 10000 / 12) );
		$("#txtUPMonth").val( Math.round(H41 * 5000 / 12) )
	}
	var C8 = $("#txtTPMonth").val();
	var C9 = $("#txtUPMonth").val();
	var H9 = $("#txtPColorPrint").val();
	var H11 = $("#txtLCBW").val();
	var H12 = $("#txtLCColor").val();
	var H13 = $("#txtHCBW").val();
	var H14 = $("#txtHCColor").val();
	var F18 = $("#txtMVS").val();
	var F19 = $("#txtMCVS").val();
	var F20 = $("#txtDS").val();
	var F21 = $("#txtAS").val();
	
	var H36 = $("#txtCRP").val();
	var F37 = $("#txtCRI").val();
	
	if(C8 && C9 && H9 && H11 && H12 && H13 && H14 && F18 && F19 && F20 && F21 && H36 && F37)
	{
		
		C8 = cNum(C8);
		C9 = cNum(C9);
		H9 = cNum(H9);
		H11 = cNum(H11);
		H12 = cNum(H12);
		H13 = cNum(H13);
		H14 = cNum(H14);
		F18 = cNum(F18);
		F19 = cNum(F19);
		F20 = cNum(F20);
		F21 = cNum(F21);
                
		H36 = cNum(H36);
		F37 = cNum(F37);
		
		if(!isNaN(C8) && !isNaN(C9) && !isNaN(H9) && !isNaN(H11) && !isNaN(H12) && !isNaN(H13) && !isNaN(H14) && !isNaN(F18) && !isNaN(F19) && !isNaN(F20) && !isNaN(F21) && !isNaN(H36) && !isNaN(F37))
		{
			H9 = H9 / 100;
			F18 = F18 / 100;
			F19 = F19 / 100;
			F20 = F20 / 100;
			F21 = F21 / 100;
			
			F37 = F37 / 100;
			
			var D15 = (H12*H9)+(H11*(1-H9));
			var H15 = (H14*H9)+(H13*(1-H9));
			
			var G18 = (C9*(1-F21)*F18*(H15-D15));
			var H18 = G18*12;
			var G19 = ((H9*(1-F21)*(C9-(F18*C9))*F19)*(H14-H12));
			var H19 = G19 * 12;
			var G20 = ((C8/2)*F20)*0.00613;
			var H20 = G20 * 12;
			var G21 = ((C8-C9)*F21*D15)+(C9*F21*H15);
			var H21 = G21 * 12;
			var G22 = G18 + G19 + G20 + G21;
			var H22 = H18 + H19 + H20 + H21;
			
			var G25 = (C8*F20)/2+(C8*F21);
			var H25 = G25*12;
			
			var G26 = G25*0.011;
			var H26 = H25*0.011;
			
			var G28 = G25 / 8333;
			var G29 = (G28*44.8)+(0.01838*G25);
			
			var H28 = H25/8333;
			var H29 = (H28*44.8)+(0.01838*H25);
			
			var G31 = 0.948*G29;
			var G32 = (G29/52.9);
			var H31 = 0.948*H29;
			var H32 = (H29/52.9);
			
			var G37 = (C8*F37)*H36;
			var H37 = G37*12;
			
			var G40 = G22+G37;
			var H40 = H22+H37;
			//var H41 = (C8*12)/10000;
			$("#lblLowBlend").html(currencySymbol + Math.round(D15*10000)/10000);
			$("#lblHighBlend").html(currencySymbol + Math.round(H15*10000)/10000);
			
			$("#lblMVSM").html(currencySymbol + Math.round(G18, 2));
			$("#lblMVSY").html(currencySymbol + Math.round(H18, 2));
			$("#lblMCVSM").html(currencySymbol + Math.round(G19, 2));
			$("#lblMCVSY").html(currencySymbol + Math.round(H19, 2));
			$("#lblDSM").html(currencySymbol + Math.round(G20, 2));
			$("#lblDSY").html(currencySymbol + Math.round(H20, 2));
			$("#lblASM").html(currencySymbol + Math.round(G21, 2));
			$("#lblASY").html(currencySymbol + Math.round(H21, 2));
			$("#lblTCRM").html(currencySymbol + Math.round(G22, 2));
			$("#lblTCRY").html(currencySymbol + Math.round(H22, 2));
			
			$("#Label1").html(addCommas(Math.round(G25, 2)));
			$("#Label2").html(addCommas(Math.round(H25, 2)));
			
			$("#Label3").html(addCommas(Math.round(G26, 2)));
			$("#Label4").html(addCommas(Math.round(H26, 2)));
			
			$("#Label5").html(addCommas(Math.round(G28, 2)));
			$("#Label6").html(addCommas(Math.round(H28, 2)));
			
			$("#Label7").html(addCommas(Math.round(G29, 2)));
			$("#Label8").html(addCommas(Math.round(H29, 2)));
			
			$("#Label9").html(addCommas(Math.round(G31, 2)));
			$("#Label10").html(addCommas(Math.round(H31, 2)));
			
			$("#Label11").html(addCommas(Math.round(G32, 2)));
			$("#Label12").html(addCommas(Math.round(H32, 2)));
			
			$("#lblCRM").html(addCommas(Math.round(G37, 2)));
			$("#lblCRY").html(addCommas(Math.round(H37, 2)));
			
			var ROIM = new NumberFormat(Math.round(G40, 2)).toFormatted();
			var ROIY = new NumberFormat(Math.round(H40, 2)).toFormatted();
			
			$("#lblROIM").html(currencySymbol + ROIM);
			$("#lblROIY").html(currencySymbol + ROIY);
		}
	}	
	
	//var g43 = H42/G40;
}

function cNum(str)
{
	str = str.replace("$", "").replace("£","").replace(" ","").replace(",","").replace("Pages", "").replace("percentage","").replace("%","").replace("pages","").replace("Percentage","");
	return parseFloat(str);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
