﻿function showLargeImage()
{
    var imgLarge = $get('imgLarge');
    imgLarge.src = String.format("{0}new/cars/images/full/{1}.aspx", applicationPath, carRef); 
    imgLarge.alt = carDet;
    
    var divImgLarge = $get('divImgLarge');
    show(divImgLarge);
}

var cashPrice = 0;
var carRef = '';
var sessionId = '';
var carFound = true;
var carDet = '';
        
function getNewCarQuoteLookups()
{
    Carselect.Services.CarService.GetNewCarQuoteLookups(onCompletedGetNewCarQuoteLookups);
}

function onCompletedGetNewCarQuoteLookups(results)
{
    var selDeposit = $get('selDeposit');
    var selTerm = $get('selTerm');
    var selMileage = $get('selMileage');

    for (i = 0; i<results.Mileages.length; i++)
    {
        addOptionSelected(selMileage, results.Mileages[i].Name, results.Mileages[i].Value, results.Mileages[i].IsDefault);
    }
    for (i = 0; i<results.Terms.length; i++)
    {
        addOptionSelected(selTerm, results.Terms[i].Name + " months", results.Terms[i].Value, results.Terms[i].IsDefault);
    }
    for (i = 0; i<results.DepositRates.length; i++)
    {
        addOptionSelected(selDeposit, results.DepositRates[i].Name + "%", results.DepositRates[i].Value, results.DepositRates[i].IsDefault);
    }
    
    calculateQuote();
}

function recalculateQuote()
{
    var divWait = $get('divWaitQuote');
    showWaiting(divWait);
    
    calculateQuote();
}


function onCompletedCalculateQuote(quote) {

    var interestRate = 0.0;
    if (quote.APR == 8.4) interestRate = 7.9;
    if (quote.APR == 7.9) interestRate = 7.4;
    
    var tbl = $get('tblQuote');
    
    var cell = tbl.rows[0].cells[1].innerHTML = "£" + String.format("{0:n}", cashPrice); 
    cell = tbl.rows[1].cells[1].innerHTML = "£" + String.format("{0:n}", quote.Deposit);
    cell = tbl.rows[2].cells[1].innerHTML = "£" + String.format("{0:n}", quote.Credit); //Total amount of credit
    cell = tbl.rows[3].cells[1].innerHTML = "£" + String.format("{0:n}", quote.PurchaseFee); //Purchase Fee
    cell = tbl.rows[4].cells[1].innerHTML = "£" + String.format("{0:n}", quote.AcceptanceFee); //Credit facility fee
    cell = tbl.rows[5].cells[1].innerHTML = "£" + String.format("{0:n}", quote.Total); //Total amount payable
    cell = tbl.rows[6].cells[1].innerHTML = "£" + String.format("{0:n}", 0); //Initial payment
    cell = tbl.rows[7].cells[0].innerHTML = String.format("{0} Monthly Payments", quote.Term); //Number of monthly payments
    cell = tbl.rows[7].cells[1].innerHTML = "£" + String.format("{0:n}", quote.Rental); //Value of monthly payments
    cell = tbl.rows[8].cells[1].innerHTML = "£" + String.format("{0:n}", quote.RV); //Final payment
    cell = tbl.rows[9].cells[1].innerHTML = String.format("{0} Months", quote.Term + 1); //Duration of agreement
    cell = tbl.rows[10].cells[1].innerHTML = String.format("{0}%", addCommas(quote.APR)); //Representative APR
    cell = tbl.rows[11].cells[1].innerHTML = String.format("{0}%", interestRate); //Interest rate fixed 
    
    var spProduct = $get('spProduct');
    spProduct.innerHTML = quote.Product; 
    
    var divWait = $get('divWaitQuote');
    hide(divWait);  
}

function calculateQuote()
{ 
    var selDeposit = $get('selDeposit');
    var selTerm = $get('selTerm');
    var selMileage = $get('selMileage');

    Carselect.Services.CarService.GetNewCarQuote(carRef, cashPrice, parseInt(selTerm.value), parseInt(selMileage.value), parseFloat(selDeposit.value), onCompletedCalculateQuote);
}

function saveNoQuote()
{
    Carselect.Services.CarService.SaveNoQuote(carRef, cashPrice, false); 
}

function getWhatCarReport()
{
    if (!reportLoaded)
    {
        var divWait = $get('divWait');
        showWaiting(divWait);
    
        Carselect.Services.CarService.GetFullWhatCarReview(carRef, onCompletedGetFullWhatCarReview);
    }
}

//function getWhatCarVideoURL()
//{
//    Carselect.Services.CarService.GetWhatCarVideoURL(carRef, onCompletedGetWhatCarVideoURL);
//}

//function onCompletedGetWhatCarVideoURL(url)
//{       
//        var mediaPlayer = $get('MediaPlayer');
//        mediaPlayer.url = url;
////        mediaPlayer.url = "mms://a1211.v38076a.c38076.g.vm.akamaistream.net/7/1211/38076/v0001/haymarket.akamai1.streamuk.com/wmv/WhatCar/Alfa_Romeo/Alfa_Romeo_Mito_512kws.wmv";
//}

var canQuote = 0;

function setUpLinks()
{
    var imgMain = $get('imgMain');
    imgMain.src = String.format("{0}new/cars/images/large/{1}.aspx", applicationPath, carRef);
    imgMain.alt = carDet;
}

function setUpInitialPage()
{
    var lnkGallery = $get('lnkGallery');
    var lnkReviews = $get('lnkReviews');
    var divNoCar = $get('divNoCar');
    var middleDetailContent = $get('middleDetailContent');
    var leftDetailContent = $get('leftDetailContent');
    
    if (carFound)
    {
        if (isWhatCarAvailable)
        {
            show(lnkReviews);
            show(lnkGallery);
        }
        else
        {
            hide(lnkReviews);
            hide(lnkGallery);
        }
        
        setUpLinks();

        if (canQuote > 0)
            getNewCarQuoteLookups();
        else
            saveNoQuote();
    }
    else
    {
        show(divNoCar);
        hide(middleDetailContent);
        hide(leftDetailContent);
    }
}
