// bigWebApps pricing calculator

function formatFloat(str) {
    str = str.toString();
	if(str.indexOf(".") == -1) { return str += ".00"; }
	str = str.substring(0, str.indexOf(".") + 3);
	if(str.indexOf(".") == str.length - 2) { str += "0"; }
	return str;
}

function calc() {
    var techs = $("input[name='p_techs']").val();
    var instances = $("select[name='p_instances']").val();
    
    if(techs >= 16) { 
        $("#p_baseprice").html("Call for a Custom Quote");
        $("#p_remoteprice").html("$0.00");
        $("#p_emailprice").html("$0.00");
        $("#p_secureprice").html("$0.00");
        $("#p_assetprice").html("$0.00");
        $("#p_totalprice").html("Custom Quote");
        $("#p_annualprice").html("&nbsp;");
        $("#p_monthlyprice").html("&nbsp;");
        return;
    }

    var base_subtotal = 0;
	if(techs <= 15 && techs > 10) {
		base_subtotal += (techs - 10) * 650;
		base_subtotal += 7625;
	}
	if(techs <= 10 && techs > 5) {
		base_subtotal += (techs - 5) * 725;
		base_subtotal += 4000;
	}
	if(techs <= 5 && techs > 0) {
		base_subtotal += techs * 800;
	}

    $('#p_baseprice').html('$'+formatFloat(base_subtotal));

	var instances_subtotal = base_subtotal * ((instances-1) / 5) ;

    var ad_subtotal = (base_subtotal + instances_subtotal) * 0.15;
    var remote_subtotal = (base_subtotal + instances_subtotal) * 0.1;
    var email_subtotal  = (base_subtotal + instances_subtotal) * 0.1;
    var secure_subtotal = (base_subtotal + instances_subtotal) * 0.05;
    var asset_subtotal  = (base_subtotal + instances_subtotal) * 0.3;

    $('#p_adprice').html('$'+formatFloat(ad_subtotal));
    $('#p_remoteprice').html('$'+formatFloat(remote_subtotal));
    $('#p_emailprice' ).html('$'+formatFloat(email_subtotal));
    $('#p_secureprice').html('$'+formatFloat(secure_subtotal));
    $('#p_assetprice' ).html('$'+formatFloat(asset_subtotal));

    var grand_total = base_subtotal + instances_subtotal;
    if( $("input:radio[name='p_ad']:checked").val() == 1 ) grand_total += ad_subtotal;
    if( $("input:radio[name='p_remote']:checked").val() == 1 ) grand_total += remote_subtotal;
    if( $("input:radio[name='p_email']:checked" ).val() == 1 ) grand_total += email_subtotal;
    if( $("input:radio[name='p_secure']:checked").val() == 1 ) grand_total += secure_subtotal;
    if( $("input:radio[name='p_asset']:checked" ).val() == 1 ) grand_total += asset_subtotal;

    $('#p_totalprice').html('$'+formatFloat(grand_total));
    $('#p_annualprice').html('$'+formatFloat(grand_total/techs));
    $('#p_monthlyprice').html('$'+formatFloat(grand_total/techs/12));
}

$(document).ready(function(){
    $('#pricing input').change(calc);
    $('#pricing input').click(calc);
    $('#pricing input').keyup(calc);
    $('#pricing select').change(calc);
    $('#pricing select').keyup(calc);
    
    calc();
});