var themakes = new Array('Select Make','ANY MAKE','MAZDA');

var themodels = new Array();
themodels[0] = new Array('Select Model','ANY MODEL');
themodels[1] = new Array('Select Model','ANY MODEL');
themodels[2] = new Array('ANY MODEL','2','3','323','5','6','626','CX-7','MPV','MX-5','PREMACY','RX-7','RX-8','B-SERIES', 'BT-50');

function getMakes(rmake) {
    var dropDown = document.getElementById("make");
    selectedIndex = 0;

    if (dropDown) {
        dropDown.length = 0;
        for (i = 0; i < themakes.length; i++) {
            dropDown[i] = new Option(themakes[i], themakes[i]);

            if (themakes[i] == "ANY MAKE") {
                dropDown[i] = new Option(themakes[i], "");
            }

            if (themakes[i].toUpperCase() == rmake){
                selectedIndex = i;
            }
        }
        dropDown.options[selectedIndex].selected = true;
    }
    selectMake(rmake);
}

function getModels(makeindex, reqmodel) {
    var dropDown = document.getElementById("model");
    selectedIndex = 0;

    if (dropDown) {
        var tmods = themodels[makeindex];
        //alert(tmods);
        dropDown.length = 0;
        for (i = 0; i < tmods.length; i++) {
            dropDown[i] = new Option(tmods[i], tmods[i]);

            if (tmods[i] == "ANY MODEL") {
                dropDown[i] = new Option(tmods[i], "");
            }

            if (tmods[i].toUpperCase() == reqmodel) {
                selectedIndex = i;
            } else if (tmods[i].toUpperCase() == "ANY MODEL" && reqmodel == "") {
                selectedIndex = i;
            }
        }
        dropDown.options[selectedIndex].selected = true;
    }
}

function selectMake(rmake)  {
    var dropDown = document.getElementById("make");
    if (dropDown) {
        for (x = 0; x < dropDown.length; x++) {
            var mk = dropDown.options[x].value;
            if (mk.toUpperCase() == rmake) {
                if (dropDown.options[x].selected == false) {
                    dropDown.options[x].selected = true;
                }
            }
        }
    }
}

function validateSearch(f) {
    if (f.make.value == "Select Make") {
        alert('Please select a make');
        f.make.focus();
        return false;
    } else if (f.model.value == "Select Model") {
        alert('Please select a model');
        f.model.focus();
        return false;
    }
    f.submit.disabled=true;
    return true;
}

function swapImage(target, imgsrc) {
    document.getElementById(target).src = imgsrc;
}

function showSummary(id) {
    if (document.getElementById("summary-" + id)) {
        var ele = document.getElementById("summary-" + id);
        var plus = document.getElementById("plus-" + id);
        var minus = document.getElementById("minus-" + id);
        if (ele.style.display == "none") {
            ele.style.display = "";
            plus.style.display = "none";
            minus.style.display = "";
        } else {
            ele.style.display = "none";
            plus.style.display = "";
            minus.style.display = "none";
        }
    }
}

function selectValue(id, value)  {
    var dropDown = document.getElementById(id);
    if (dropDown) {
        for (x = 0; x < dropDown.length; x++) {
            var val = dropDown.options[x].value;
            if (val == value) {
                dropDown.options[x].selected = true;
            }
        }
    }
}


function calcLoan() {
        var totalPrice = document.getElementById("totalPrice").innerHTML;
        totalPrice = totalPrice.replace('£', '');
        
        var deposit = document.getElementById("selDeposit").value;
        alert(deposit);
        
        var loanPeriod = document.getElementById("selLoanPeriod").value;
        
        var interestRate = document.getElementById("selInterestRate").value / 1200;
        
        var loanAmount = totalPrice - deposit;
        
        var monthlyPayment = loanAmount * interestRate / (1 - (Math.pow(1/(1 + interestRate), loanPeriod * 12)));
        
        var totalPayable = monthlyPayment * (loanPeriod * 12);
        
        var interestPayable = totalPayable - totalPrice;
        
        document.getElementById("monthlyPayment").innerHTML = "&pound;" + monthlyPayment.toFixed(2);
        
        document.getElementById("loanAmount").innerHTML = "&pound;" + loanAmount;
        
        document.getElementById("totalPayable").innerHTML = "&pound;" + totalPayable.toFixed(2);
        
        document.getElementById("interestPayable").innerHTML = "&pound;" + interestPayable.toFixed(2);
        
        //document.getElementById("totalPrice").innerHTML;

}