$(document).ready(function() {



var totalCost=          0;

var costs = new Array();

costs["lounge-living"]=             40;
costs["lounge-living-check"] =      25;
costs["flight-of-stairs"] =         28;
costs["flight-of-stairs-check"] =   15;
costs["landing"] =                  11;
costs["landing-check"] =            5;
costs["hallway"] =                  11;
costs["hallway-check"] =            5;
costs["thru-lounge"] =              55;
costs["thru-lounge-check"] =        30;
costs["dining-room"] =              25;
costs["dining-room-check"] =        15;
costs["bedroom"] =                  20;
costs["bedroom-check"] =            10;
costs["box-room"] =                 11;
costs["box-room-check"] =           5;
costs["rug-small"] =                5;
costs["rug-small-check"] =          5;
costs["rug-medium"] =               10;
costs["rug-medium-check"] =         10;
costs["rug-large"] =                15;
costs["rug-large-check"] =          10;
costs["armchair"] =                 25;
costs["armchair-check"] =           15;
costs["two-seat"] =                 50;
costs["two-seat-check"] =           25;
costs["three-seat"] =               55;
costs["three-seat-check"] =         30;
costs["four-seat"] =                60;
costs["four-seat-check"] =          35;
costs["puff"] =                     10;
costs["puff-check"] =               5;
costs["stool"] =                    5;
costs["stool-check"] =              5;



var firstRun = true;
function calculateCosts(){
        var totalCarpetCost=    0;
        var totalUpholstryCost= 0;
        $(".carpet-cost,.item-cost").each(function(i){
                 
                    $(this).css('backgroundColor','white'); // clear error highlighting
                    var num = 0; //set number of items to 0
                    var cost = 0 //set cost to 0
                  
                    
                    if (this.type=='text') { // if the input type is text get the number
                        var textId = this.id;
                        
                        if(isNaN(this.value)) { // basic error checking
                            alert("please correct your entry, it can only be a number");
                            $(this).css('backgroundColor','red');
                            cost=0;
                            return false;
                        } else {
                        num=this.value;
                        }
                     //   alert(costs[textId]);
                        cost = num * costs[textId];
                        
                    }
                    if (this.type=='checkbox') { // if the input type is checkbox get the number from correspoding text box
                        var boxId=this.id; // get id of this check box
                        var numId= boxId.slice(0,boxId.lastIndexOf("-")); //get id of corresponding text input
                        var numCheck = $("#"+numId).attr('value'); // get value of text input
                        
                        if(isNaN(numCheck)) { // basic error checking
                            alert("please correct your entry, it can only be a number");
                            $("#"+numId).css('backgroundColor','red');
                            cost=0;
                            return false;
                        } else if (this.checked==true){
                            num = numCheck;
                        }
                        cost = num * costs[boxId];
                    }
                    
                    if (this.className =='carpet-cost') {
                        totalCarpetCost += cost;
                    }
                    if (this.className =='item-cost') {
                        totalUpholstryCost += cost;
                    }
                    return true;
                    
                });
         
        totalCost = totalCarpetCost + totalUpholstryCost;
        
        //update visible quotes
        $("#total-carpet-cost").html("&pound; "+totalCarpetCost);
        $("#total-uphosltry-cost").html("&pound; "+totalUpholstryCost);
        $("#total-cost").html("&pound; "+totalCost);
        
        //update hidden text fields for submission
        $("#carpet-total").val(totalCarpetCost);
        $("#upholstery-total").val(totalUpholstryCost);
        $("#grand-total").val(totalCost);     
       if (firstRun ==true) {
       		firstRun=false;
       } else {
       		$.jGrowl('Your quote has been updated.');
       }
}

$(".carpet-cost,.item-cost").change(calculateCosts); //run calculate costs function on form changes
calculateCosts();



});