var projectType = 'none';
var minimum;
$(document).ready(function(){
    $("#form").show();
    $('#projectType').change(function(){
        switch ($('#projectType').val()) {
            case 'schools':
                genSchools();
                break;
            case 'hospitals':
                genHospitals();
                break;
            case 'universities':
                genUniversities();
                break;
            case 'other':
                genOther();
                break;
            default:
                genNone();
                break;
        }
    });
    $("#firstname").change(function(){
        calc(projectType);
    });
    $("#lastname").change(function(){
        calc(projectType);
    });
    $("#email").change(function(){
        calc(projectType);
    });
    $("#phone").change(function(){
        calc(projectType);
    });
    $("#state").change(function(){
        calc(projectType);
    });

    $('#projectType').change();
});

function checkRequired() {
    var retVal = true;
    if ($("#firstname").val() == '') retVal = false;
    if ($("#lastname").val() == '') retVal = false;
    if ($("#email").val() == '') retVal = false;
    if ($("#phone").val() == '') retVal = false;
    if ($("#state").val() == '') retVal = false;
    return retVal;
}

function calc(type) {
    var total = 0;
    switch(type) {
        case 'none':
            total = 0;
            break;
            
        case 'schools':
            var eVal = $('#elementarySchool').val();
            var mVal = $('#middleHighSchool').val();
            var oVal = $('#otherSchool').val();
            $("#data").val("Project Type: Schools\nElementary: "+ eVal + "\nMiddle/High: "+ mVal +"\nOther: "+ oVal);
            var elementaryTotal = eVal * 1021;
            var middleTotal = mVal * 2042;
            var otherTotal = oVal * 340;
            total = elementaryTotal + middleTotal + otherTotal;
            // special cases
            if (eVal == 4 && mVal == 0 && oVal == 0) total = minimum;
            if (eVal == 2 && mVal == 1 && oVal == 0) total = minimum;
            if (eVal == 0 && mVal == 2 && oVal == 0) total = minimum;
            break;

        case 'hospitals':
            var u5Val = $("#hosMainUnder5").val();
            var o5Val = $("#hosMainOver5").val();
            var oVal = $("#hosOther").val();
            $("#data").val("Project Type: Hospitals\nMain building < 5 floors: "+ u5Val + "\nMain building > 5 floors: "+ o5Val +"\nOther: "+ oVal);
            var U5Total = u5Val * 1362;
            var O5Total = o5Val * 2042;
            var oTotal = oVal * 1362;
            total = U5Total + O5Total + oTotal; 
            // special cases
            if (u5Val == 3 && o5Val == 0 && oVal == 0) total = minimum;
            if (u5Val == 2 && o5Val == 0 && oVal == 1) total = minimum;
            if (u5Val == 1 && o5Val == 0 && oVal == 2) total = minimum;
            if (u5Val == 0 && o5Val == 0 && oVal == 3) total = minimum;
            if (u5Val == 0 && o5Val == 0 && oVal == 3) total = minimum;
            break;
        
        case 'universities' :
            var u5Val = $("#uniMainUnder5").val();
            var o5Val = $("#uniMainOver5").val();            
            var oVal = $("#uniOther").val();
            $("#data").val("Project Type: Universities\nMain building < 5 floors: "+ u5Val + "\nMain building > 5 floors: "+ o5Val +"\nOther: "+ oVal);
            var U5Total = u5Val * 1362;
            var O5Total = o5Val * 2042;
            var oTotal = oVal * 1362;
            total = U5Total + O5Total + oTotal;
            // special cases
            if (u5Val == 3 && o5Val == 0 && oVal == 0) total = minimum;
            break;
            
        case 'other':
            var u5Val = $("#otherUnder5").val();
            var o5Val = $("#otherUnder610").val();
            var o10Val = $("#otherOver10").val();            
            var oVal = $("#otherOther").val();
            $("#data").val("Project Type: Office/Other\nMain building < 5 floors: "+ u5Val + "\nMain building 6-10 floors: "+ o5Val + "\nMain building > 10 floors: "+ o10Val +"\nOther: "+ oVal);            
            var U5Total = u5Val * 1362;
            var u5Total = u5Val * 1362;
            var o5Total = o5Val * 2042;
            var o10Total = o10Val * 2723;
            var oTotal = oVal * 1362;
            total = u5Total + o5Total + o10Total + oTotal;
            // special cases
            if (u5Val == 3 && o5Val == 0 && o10Val == 0 && oVal == 0) total = minimum;
            if (u5Val == 2 && o5Val == 0 && o10Val == 0 && oVal == 1) total = minimum;
            if (u5Val == 1 && o5Val == 0 && o10Val == 0 && oVal == 2) total = minimum;
            if (u5Val == 0 && o5Val == 0 && o10Val == 0 && oVal == 3) total = minimum;
            break;
    }
    if (total < minimum && total != 0) total = minimum;
    if (total > 0 && checkRequired()) { 
        $("#submit").removeAttr("disabled");
    } else {
        $("#submit").attr("disabled","disabled");
    }
    var cleanTotal = formatNumber(total, 2, ',', '.', '$', '');
    $('#total').text(cleanTotal);
    var agg = $('#data').val();
    $('#data').val(agg +'\nTotal: '+ $('#total').text());
}

function genSchools() {
    projectType = 'schools';
    minimum = 3995;
    $('#buildings').text("");
    //$('.example').hide();
    //$('#schoolExample').show();
    $("#totals").text("").show();
    $("#data").val(" ");

    $('#buildings').append("Number of Elementary Schools:<br /><input type='text' class='number' value=0 name='elementarySchool' id='elementarySchool' /><br />");
    $('#buildings').append("Number of Middle &amp; High Schools:<br /><input type='text' class='number' value=0 name='middleHighSchool' id='middleHighSchool' /><br />");
    $('#buildings').append("Number of Other Buildings:<br /><input type='text' class='number' value=0 name='otherSchool' id='otherSchool' /><br />");
    
    $("#totals").append("<strong>Estimate: <span id ='total'>$0.00</span></strong>");
    
    $("#elementarySchool").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#middleHighSchool").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#otherSchool").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });
}

function genHospitals() {
    projectType = 'hospitals';
    minimum = 4085;
    $('#buildings').text("");
    //$('.example').hide();
    //$('#hospitalExample').show();
    $("#totals").text("").show();
    $("#data").val(" ");

    $('#buildings').append("Number of Main building up to 5 floors:<br /><input type='text' class='number' value=0 name='hosMainUnder5' id='hosMainUnder5' /><br />");
    $('#buildings').append("Number of Main building over 5 floors:<br /><input type='text' class='number' value=0 name='hosMainOver5' id='hosMainOver5' /><br />");
    $('#buildings').append("Number of Additional Attached buildings:<br /><input type='text' class='number' value=0 name='hosOther' id='hosOther' /><br />");

    $("#totals").append("<strong>Estimate: <span id ='total'>$0.00</span></strong>");

    $("#hosMainUnder5").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#hosMainOver5").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#hosOther").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });
}

function genUniversities() {
    projectType = 'universities';
    minimum = 4085;
    $('#buildings').text("");
    //$('.example').hide();
    //$('#UniversityExample').show();
    $("#totals").text("").show();
    $("#data").val(" ");
    
    $('#buildings').append("Number of Single Structure up to 5 floors:<br /><input type='text' class='number' value=0 name='uniMainUnder5' id='uniMainUnder5' /><br />");
    $('#buildings').append("Number of Single Structure over 5 floors:<br /><input type='text' class='number' value=0 name='uniMainOver5' id='uniMainOver5' /><br />");
    $('#buildings').append("Number of Additional Attached Structures:<br /><input type='text' class='number' value=0 name='uniOther' id='uniOther' /><br />");

    $("#totals").append("<strong>Estimate: <span id ='total'>$0.00</span></strong>");

    $("#uniMainUnder5").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#uniMainOver5").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#uniOther").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

}

function genOther() {
    projectType = 'other';
    minimum = 4085;
    $('#buildings').text("");
    //$('.example').hide();
    //$('#otherExample').show();
    $("#totals").text("").show();
    $("#data").val(" ");
    
    $('#buildings').append("Number of Single Structure up to 5 floors:<br /><input type='text' class='number' value=0 name='otherUnder5' id='otherUnder5' /><br />");
    $('#buildings').append("Number of Single Structure 6 - 10 floors:<br /><input type='text' class='number' value=0 name='other610' id='otherUnder610' /><br />");
    $('#buildings').append("Number of Single Structure &gt; 10 floors:<br /><input type='text' class='number' value=0 name='otherOver10' id='otherOver10' /><br />");
    $('#buildings').append("Number of Additional Attached Structures:<br /><input type='text' class='number' value=0 name='otherOther' id='otherOther' /><br />");

    $("#totals").append("<strong>Estimate: <span id ='total'>$0.00</span></strong>");

    $("#otherUnder5").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#otherUnder610").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#otherOver10").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });

    $("#otherOther").change(function() {
        if (isNaN($(this).val())) $(this).val(0);
        calc(projectType);
    });
}

function genNone() {
    projectType = 'none';
    $('#buildings').text("");
    //$('.example').hide();
    $("#totals").hide();
    $("#data").val("");
    $("#submit").attr("disabled", "disabled");
}

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
    var x = Math.round(num * Math.pow(10,dec));
    if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');
    var z = y.length - dec; if (z<0) z--; 
    for(var i = z; i < 0; i++) y.unshift('0'); 
    if (z<0) z = 1; y.splice(z, 0, pnt); 
    if(y[0] == pnt) y.unshift('0'); 
    while (z > 3) {
        z-=3; y.splice(z,0,thou);
    }var r = curr1+n1+y.join('')+n2+curr2;
    return r;
}
