﻿function SetWidth(obj)
{
    var width  = document.getElementById(obj).selectedIndex + 1;
    var room = document.getElementById("divFloorPlan");
    
    room.style.width = width * 20 + "px";
    
    Calculate();
}

function SetHeight(obj)
{
    var height  = document.getElementById(obj).selectedIndex + 1;
    var room = document.getElementById("divFloorPlan");
    
    room.style.height = height * 20 + "px";
    
    Calculate();
}

function SetType(obj)
{
    var mylist = document.getElementById(obj);
    
    Calculate();
}

function AddVelux()
{
    document.getElementById("spanAddons").innerHTML += "<img src='./icons/velux.png' />";
    var numVelux = document.getElementById("spanNumVelux").innerHTML;
    numVelux ++;
    document.getElementById("spanNumVelux").innerHTML = numVelux;
    
    Calculate();
}

function AddItem(item)
{
    switch(item)
    {
        case "Velux":
        {
            var num = document.getElementById("spanNumVelux").innerHTML;
            
            if(num < 9)
            {
                num ++;
                document.getElementById("spanNumVelux").innerHTML = num;
                document.getElementById("spanAddons").innerHTML += "<img src='./icons/velux.png' />";
            }
            
            break;
        }
        
        case "Dormer":
        {
            var num = document.getElementById("spanNumDormer").innerHTML;
            
            if(num < 5)
            {
                num ++;
                document.getElementById("spanNumDormer").innerHTML = num;
                document.getElementById("spanAddons").innerHTML += "<img src='./icons/dormer.png' />";
            }
        
            break;
        }
        
        case "Bathroom":
        {
            var num = document.getElementById("spanNumBathrooms").innerHTML;
            
            if(num < 5)
            {
                num ++;
                document.getElementById("spanAddons").innerHTML += "<img src='./icons/bathroom.png' />";
                document.getElementById("spanNumBathrooms").innerHTML = num;
            }
            
            break;
        }
        
        case "Radiator":
        {
            var num = document.getElementById("spanNumRadiators").innerHTML;
            
            if(num < 9)
            {
                num ++;
                document.getElementById("spanAddons").innerHTML += "<img src='./icons/radiator.png' />";
                document.getElementById("spanNumRadiators").innerHTML = num;
            }
            
            break;
        }
        
        case "Roof":
        {
            var num = BoolToInt(document.getElementById("spanBoolRoof").innerHTML);
            
            if(num < 1)
            {
                num ++;
                document.getElementById("spanAddons").innerHTML += "<img src='./icons/roof.png' />";
                document.getElementById("spanBoolRoof").innerHTML = IntToBool(num);
            }
            
            break;
        }
        default:
        {
            break;
        }
    }    
    
    Calculate();
}

function Restart()
{
    document.getElementById("spanAddons").innerHTML = "&nbsp;";
    document.getElementById("spanNumVelux").innerHTML = "0";
    document.getElementById("spanNumDormer").innerHTML = "0";
    document.getElementById("spanNumBathrooms").innerHTML = "0";
    document.getElementById("spanNumRadiators").innerHTML = "0";
    document.getElementById("spanBoolRoof").innerHTML = IntToBool(0);
    
    Calculate();
}

function ToolTip(msg)
{
    document.getElementById("spanToolTip").innerHTML = msg;
}

function Calculate()
{
    /*
     *Get values
    */
    var listType = document.getElementById("listType");
    var type = listType.options[listType.selectedIndex].text;
    
    listType = null;
    
    var typePrice = 0;

    switch(type)
    {
        case "Loft Conversion":
        {
            typePrice = 380;
            break;
        }
        
        case "Extension (Ground Floor)":
        {
            typePrice = 1250;
            break;
        }
        
        case "Extension (Double Storey)":
        {
            typePrice = 1900;
            break;
        }
        
        case "Extension (Over Garage)":
        {
            typePrice = 1200;
            break;
        }
        
        case "New Build":
        {
            //need to take into account no of stories
            typePrice = 800;
            break;
        }
        
        default:
        {
            break;
        }
    }
    
    var listWidth = document.getElementById("listWidth");
    var listHeight = document.getElementById("listHeight");
    
    var width = listWidth.options[listWidth.selectedIndex].text;
    var height = listHeight.options[listHeight.selectedIndex].text;
    
    listWidth = null;
    listHeight = null;
    
    var numVelux = document.getElementById("spanNumVelux").innerHTML;
    var numDormer = document.getElementById("spanNumDormer").innerHTML;
    var numBathrooms = document.getElementById("spanNumBathrooms").innerHTML;
    var numRadiators = document.getElementById("spanNumRadiators").innerHTML;
    var numRoofs = BoolToInt(document.getElementById("spanBoolRoof").innerHTML);
    
    /*
     *Do calculations
    */
    var pricePerVelux = 400;
    var pricePerDormer = 2600;
    var pricePerWindow = 500;
    var pricePerBathroom = 2800;
    var pricePerRadiator = 400;
    var pricePerKitchen = 9000;
    var pricePerHeatingSystem = 2800;
    var pricePerRoof = 90;
    
    var veluxPrice = pricePerVelux * numVelux;
    var dormerPrice = pricePerDormer * numDormer;
    var bathroomPrice = pricePerDormer * numBathrooms;
    var radiatorPrice = pricePerRadiator * numRadiators;
    var roofPrice = pricePerRoof * numRoofs;
    
    var finalPrice = 0;
    
    var area = width * height;
    var basicPrice = area * typePrice;
    
    finalPrice = basicPrice + veluxPrice + dormerPrice + bathroomPrice + radiatorPrice + roofPrice;
    //alert(finalPrice);
    
    var lower = finalPrice - ((finalPrice/100)*10);
    var upper = finalPrice + ((finalPrice/100)*10);
    
    /*
     *Update html
    */

    document.getElementById("spanEstimateLower").innerHTML = CommaFormatted(String(lower));
    document.getElementById("spanEstimateUpper").innerHTML = CommaFormatted(String(upper));
}

function CommaFormatted(nStr) 
{
    var rgx = /(\d+)(\d{3})/;
    
    while (rgx.test(nStr)) 
    {
      nStr = nStr.replace(rgx, '$1,$2');
    }
    
    return nStr;
}

function BoolToInt(val)
{
    if(val == "Yes")
    {
        return 1;
    }
    else
    {
        return 0; 
    }
}

function IntToBool(val)
{
    if(val == 1)
    {
        return "Yes";
    }
    else
    {
        return "No"; 
    }
}
