﻿
function populateNewCarVariantsTable(results, tbl)
{
    var html = "<table class='resultList' rules='cols' border='1' style='border-collapse:collapse;' cellspacing='0' id='tblVariants'>";
    html += "<tr class='heading'>"+
         "<td class='addShowroom'>Add to showroom</td>"+
         "<td><a title='click to sort by Description' href=\"javascript:changeCurrentSort('');\">Description</a></td>"+
         "<td><a title='click to sort by Retail Price' href=\"javascript:changeCurrentSort('ListPrice');\">Retail Price</a></td>"+
         "<td><a title='click to sort by Our Price' href=\"javascript:changeCurrentSort('Price');\">Our Price</a></td>"+
         "<td><a title='click to sort by amount of Saving' href=\"javascript:changeCurrentSort('Saving');\">Saving</a></td>"+
         "</tr>";
    html += "</table>";
    tbl.innerHTML = html;
    
    var t = $get('tblVariants');
    addNewCarToTable(results, t);
}

function addNewCarToTable(results, tbl)
{
    var currentTableSize = tbl.rows.length;
    for (i = 0; i < results.length; i++)
    {
        var newRow = tbl.insertRow(currentTableSize + i);
        var evenRow = (currentTableSize + i + 1) % 2 == 0;
        Sys.UI.DomElement.addCssClass(newRow, (evenRow ? "row" : "altrow"));

        var oCell = newRow.insertCell(0);
        oCell.innerHTML = String.format("<span><a title='add to showroom' id='btnAddShowroom' class='unselected' href=\"javascript:addToShowroom('{0}');\">add</a></span></td>", results[i].ReferenceNo);
        oCell.style.width = "8%";
        oCell.className = "addShowroom";
        
        oCell = newRow.insertCell(1);
        oCell.innerHTML = String.format("<a title='{4}' href='{0}new/cars/{1}/{2}/{3}.aspx'>{4}</a>", applicationPath, encodeURI(results[i].Make), encodeURI(results[i].Model), results[i].ReferenceNo, results[i].Description);
        oCell.style.width = "45%";
        
        oCell = newRow.insertCell(2);
        if (results[i].Saving > 0 && results[i].HasCashOption)
            oCell.innerHTML = "£" + String.format("{0:n}", results[i].ListPrice);
        else
            oCell.innerHTML = "";
        oCell.style.width = "9%";
        oCell.style.textAlign = 'right';
        
        oCell = newRow.insertCell(3);
        if (results[i].Price > 0)
            oCell.innerHTML = String.format("£{0:n}", results[i].Price);
        else if (!results[i].HasCashOption)
            oCell.innerHTML = "";
        else
            oCell.innerHTML = "<span title='Price on application - please call for a quote'>PoA</a>";
        oCell.style.width = "9%";
        oCell.style.textAlign = 'right';
        
        oCell = newRow.insertCell(4);
        if (results[i].Saving > 0 && results[i].HasCashOption)
            oCell.innerHTML = String.format("<span class='saving'>£{0:n}</span>", results[i].Saving);
        else
            oCell.innerHTML = "";
        oCell.style.width = "9%";
        oCell.style.textAlign = 'right';
    } 
}

function populateNewCarsTable(results, divVariants, tblVariants, divMoreCars, lnkMoreCars)
{
    if (results.CurrentSet == 1)
    {
        if (results.Items.length == 0)
        {
            $get('divVariants').innerHTML = "No results returned";
        }
        else
            populateNewCarVariantsTable(results.Items, divVariants);
    }
    else
        addNewCarToTable(results.Items, tblVariants);
        
    showMore(results, divMoreCars, lnkMoreCars);    
}

function showMore(results, divMoreCars, lnkMoreCars)
{
    var carIndex = results.NumberInSet * (results.CurrentSet - 1) + results.Items.length;
    
    divMoreCars.style.display = (results.Total > carIndex) ? "block" : "none";
    lnkMoreCars.href = String.format("javascript:getMoreCars({0})", results.CurrentSet + 1);
    var nextPageContains = ((results.Total - results.Items.length * results.CurrentSet) > results.Items.length) ? results.NumberInSet : (results.Total - results.Items.length * results.CurrentSet);
    lnkMoreCars.innerHTML = String.format("{0} of {1}. Expand", carIndex, results.Total);
}

function populateUsedCarVariantsTable(results, tbl)
{
    var html = "<table class='resultList' rules='cols' border='1' style='border-collapse:collapse;' cellspacing='0' id='tblVariants'>";
    html += "<tr class='heading'>"+
         "<td class='addShowroom'>Add to showroom</td>"+
         "<td><a title='click to sort by Description' href=\"javascript:changeCurrentSort('');\">Description</a></td>"+
         "<td><a title='click to sort by Colour' href=\"javascript:changeCurrentSort('Colour');\">Colour</a></td>"+
         "<td><a title='click to sort by Year of Registration' href=\"javascript:changeCurrentSort('RegYear');\">Year</a></td>"+
         "<td><a title='click to sort by Fuel Type' href=\"javascript:changeCurrentSort('Fuel');\">Fuel</a></td>"+
         "<td><a title='click to sort by Transmission Type' href=\"javascript:changeCurrentSort('TransmissionType');\">Trans</a></td>"+
         "<td><a title='click to sort by Mileage' href=\"javascript:changeCurrentSort('CurrentMileage');\">Miles</a></td>"+
         "<td><a title='click to sort by Price' href=\"javascript:changeCurrentSort('Price');\">Price</a></td>"+
         "<td><a title='click to sort by Saving' href=\"javascript:changeCurrentSort('Saving');\">Saving</a></td>"+
         "</tr>";
        
    html += "</table>";
    tbl.innerHTML = html;
    
    var t = $get('tblVariants');
    addUsedCarToTable(results, t);
}

function addUsedCarToTable(results, tbl)
{
    var currentTableSize = tbl.rows.length;
    
    for (i = 0; i < results.length; i++)
    {   
        var newRow = tbl.insertRow(currentTableSize + i);
        var evenRow = (currentTableSize + i + 1) % 2 == 0;
        Sys.UI.DomElement.addCssClass(newRow, (evenRow ? "row" : "altrow"));

        var oCell = newRow.insertCell(0);
        oCell.innerHTML = String.format("<a title='add to showroom' id='btnAddShowroom' class='unselected' href=\"javascript:addToShowroom('{0}');\">add</a></td>", results[i].ReferenceNo);
        oCell.style.width = "8%";
        oCell.className = "addShowroom";
        
        String.format("<a title='{4}' href='{0}new/cars/{1}/{2}/{3}.aspx'>{4}</a>", applicationPath, encodeURI(results[i].Make), encodeURI(results[i].Model), results[i].ReferenceNo, results[i].Description);
        oCell = newRow.insertCell(1);
        oCell.innerHTML = String.format(
        "<img class=\"imgLink floatLeft\" src='{0}used/cars/images/thumbs/{1}.aspx{2}' alt='{3} {4} {5}'/>" + 
        "<div class=\"description\"><a title='{3} {4} {5}' href='{0}used/cars/{3}/{4}/{1}.aspx'>{3} {4} {5}</a><br />" + 
        "</div>", 
            applicationPath, 
            results[i].ReferenceNo, 
            (results[i].ImageVersion == 0 ? "" : "?" + results[i].ImageVersion), 
            results[i].Make, 
            results[i].Model, 
            results[i].Description);
        oCell.style.width = "42%";
        
        oCell = newRow.insertCell(2);
        oCell.innerHTML = results[i].Colour;
        oCell.style.width = "9%";
        
        oCell = newRow.insertCell(3);
        oCell.innerHTML = results[i].RegYear;
        oCell.style.width = "7%";
        
        oCell = newRow.insertCell(4);
        oCell.innerHTML = results[i].FuelType;
        oCell.style.width = "8%";
        
        oCell = newRow.insertCell(5);
        oCell.innerHTML = results[i].TransmissionType;
        oCell.style.width = "8%";
        
        oCell = newRow.insertCell(6);
        oCell.innerHTML = String.format("{0}K", results[i].CurrentMileage);
        oCell.style.width = "8%";
        oCell.style.textAlign = 'right';
        
        oCell = newRow.insertCell(7);
        oCell.innerHTML = String.format("<span class='price'>£{0:n}</span><br /><span class='sold' style='display:{1};'>Sold</span> <span class='justArrived' style='display:{2};'>Just arrived!</span>", 
            results[i].Price, 
            (results[i].IsSold ? "inline" : "none"),
            (results[i].IsNew ? "inline" : "none"));
        oCell.style.width = "10%";
        
        oCell = newRow.insertCell(8);
        oCell.innerHTML = String.format("<span class='price'>£{0:n}</span>", results[i].Saving);
        oCell.style.width = "10%";
    } 
}

function populateUsedCarsTable(results, divVariants, tblVariants, divMoreCars, lnkMoreCars)
{
    if (results.CurrentSet == 1)
    {
        if (results.Items.length == 0)
        {
            $get('divVariants').innerHTML = "No results returned";
        }
        else
            populateUsedCarVariantsTable(results.Items, divVariants);
    }
    else
        addUsedCarToTable(results.Items, tblVariants);
        
    showMore(results, divMoreCars, lnkMoreCars);    
}

