﻿var totalCarsInShowroom = 0;
var selectedPage = 1;

function removeFromFullShowroom(refNo)
{
    var divWait = $get('divWait');
    showWaiting(divWait);
    
    removeFromShowroom(refNo);

    populateShowroomPage(selectedPage);
    
    Carselect.Services.ShowroomService.GetNumberOfCarsInShowroom(onCompletedGetNumberOfCarsInFullShowroom, onFailedGetNumberOfCarsInShowroom);
    showNextPrevious();
}

function onCompletedGetShowroomPage(results)
{       
    var sh = $get('divShowroomPage');
    var buf = new StringBuffer();
    
          
    //  need to display a message if the showroom is empty?
    
    
    buf.append("<table class=\"showroomTable\" cellpadding=0 cellspacing=0>");
    buf.append("<tr>");
    
    buf.append("<td class=\"showroomLabel\"></td>");
    
    for (i = 0; i < results.length; i++)
    {
        buf.append("<td class=\"showroomImage\">");
            
        var used = (results[i].__type == "UsedCar:#Carselect.Data.DataObjects");
        var desc = String.format("{0} {1} {2}", results[i].Make, (used ? results[i].Model : ""), results[i].Description);
        var href = String.format("{0}{1}/cars/{2}/{3}/{4}.aspx", applicationPath, (used ? "used" : "new"), results[i].Make, results[i].Model, results[i].ReferenceNo);
            
        buf.append(String.format("<div class='fixed'><img src='{0}{1}/cars/images/large/{2}.aspx?{3}' alt='{4} {5}'></div>", applicationPath, (used ? "used" : "new"), results[i].ReferenceNo, (used && results[i].ImageVersion > 0 ? results[i].ImageVersion : ""), (used ? "Used" : "New"), desc));
            
        buf.append(String.format("<div class='carDesc'><a href=\"{0}\">", href)); 
        buf.append(String.format("<h2 class='alt'>{0}</h2></a></div>", desc));

        buf.append(String.format("<P title='{1}'><strong>Price: {0}</strong></p>", results[i].Price == 0 ? "PoA" : String.format("£{0:n}",  results[i].Price), results[i].Price == 0 ? "Price on application" : ""));

        buf.append(String.format("<p><a href=\"javascript:removeFromFullShowroom('{0}');\" id=\"lnkRemove{1}\" title='remove from showroom'>", results[i].ReferenceNo, i));
        buf.append("remove from showroom</a></p>");
            
        buf.append("</td>");
    }
    
    buf.append("</tr>");




    buf.append("<tr>");
        buf.append("<td><h4>Specifications</h4></td>");
        buf.append("<td></td>");
        if (results.length > 1) {  buf.append("<td></td>");  }
        if (results.length > 2) {  buf.append("<td></td>");  }
    buf.append("</tr>");
    
    buf.append("<div class=\"showroomSpecifications\">");
 
    if (results.length > 0)
    {                      
            buf.append("<tr class=\"showroomDetailsAlt\">");                    
                buf.append("<td class=\"showroomLabel\">Fuel Type:</td>");
                buf.append("<td>" + results[0].FuelType + "</td>");
                
                if (results.length > 1) { buf.append("<td>"); buf.append(results[1].FuelType); buf.append("</td>"); }
                if (results.length > 2) { buf.append("<td>"); buf.append(results[2].FuelType); buf.append("</td>");}
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">Body Colour:</td>");
                
                buf.append("<td>");
                if (results[0].Colour != null) { buf.append(results[0].Colour); } else { buf.append("Your choice"); }
                buf.append("</td>");
                
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    if (results[1].Colour != null) { buf.append(results[1].Colour); } else { buf.append("Your choice"); }
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    if (results[2].Colour != null) { buf.append(results[2].Colour); } else { buf.append("Your choice"); }
                    buf.append("</td>");
                }
                
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Transmission:</td>");
                buf.append("<td>" + results[0].TransmissionType + "</td>");
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    buf.append(results[1].TransmissionType); 
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    buf.append(results[2].TransmissionType); 
                    buf.append("</td>");
                }

            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">Engine Size:</td>");
                buf.append("<td>" + results[0].EngineSize + "</td>");
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    buf.append(results[1].EngineSize); 
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    buf.append(results[2].EngineSize); 
                    buf.append("</td>");
                }
                
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Miles:</td>");
                buf.append("<td>");
                if (results[0].CurrentMileage != null) { buf.append(String.format("{0}K", results[0].CurrentMileage)); } else { buf.append("New"); }
                buf.append("</td>");
                

                if (results.length > 1) { 
                    buf.append("<td>");
                    if (results[1].CurrentMileage != null) { buf.append(String.format("{0}K", results[1].CurrentMileage)); } else { buf.append("New"); }
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    if (results[2].CurrentMileage != null) { buf.append(String.format("{0}K", results[2].CurrentMileage)); } else { buf.append("New"); }
                    buf.append("</td>");
                }

            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">Ins Group:</td>");
                buf.append("<td>" + results[0].InsuranceGroup + "</td>");
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    buf.append(results[1].InsuranceGroup); 
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    buf.append(results[2].InsuranceGroup); 
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">CO2 Emissions:</td>");
                buf.append("<td>" + results[0].CO2 + "</td>");
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    buf.append(results[1].CO2); 
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    buf.append(results[2].CO2); 
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">CO2 Band:</td>");
                buf.append("<td>" + results[0].CO2Band + "</td>");

                if (results.length > 1) { buf.append("<td>"); buf.append(results[1].CO2Band); buf.append("</td>");}
                if (results.length > 2) { buf.append("<td>"); buf.append(results[2].CO2Band); buf.append("</td>");}
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Registered:</td>");
                buf.append("<td>");
                buf.append(results[0].RegYear == null ? "New" : String.format("{0} - {1}", DateFormat.getMonthName(results[0].RegMonth), results[0].RegYear));
                buf.append("</td>");
                
                if (results.length > 1) { 
                    buf.append("<td>");
                    buf.append(results[1].RegYear == null ? "New" : String.format("{0} - {1}", DateFormat.getMonthName(results[1].RegMonth), results[1].RegYear));
                    buf.append("</td>");
                }
                
                if (results.length > 2) { 
                    buf.append("<td>");
                    buf.append(results[2].RegYear == null ? "New" : String.format("{0} - {1}", DateFormat.getMonthName(results[2].RegMonth), results[2].RegYear));
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">Status:</td>");
                
                var used = (results[0].__type == "UsedCar:#Carselect.Data.DataObjects");
                buf.append(String.format("<td><span class='{0}'>{1}</span></td>", (used && results[0].IsSold ? "sold" : "available"), (used && results[0].IsSold ? "Sold" : "Available")));
                
                if (results.length > 1) 
                {
                    used = (results[1].__type == "UsedCar:#Carselect.Data.DataObjects");
                    buf.append(String.format("<td><span class='{0}'>{1}</span></td>", (used && results[1].IsSold ? "sold" : "available"), (used && results[1].IsSold ? "Sold" : "Available")));
                }
                if (results.length > 2) 
                {
                    used = (results[2].__type == "UsedCar:#Carselect.Data.DataObjects");
                    buf.append(String.format("<td><span class='{0}'>{1}</span></td>", (used && results[2].IsSold ? "sold" : "available"), (used && results[2].IsSold ? "Sold" : "Available")));
                }
            buf.append("</tr>");
            
            
            buf.append("</div>");
            
            
            buf.append("<tr>");
                buf.append("<td><h4 class=\"extras\">Fitted extras</h4></td>");
                buf.append("<td></td>");
                if (results.length > 1) {  buf.append("<td></td>");  }
                if (results.length > 2) {  buf.append("<td></td>");  }
            buf.append("</tr>");
            
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Alloy wheels:</td>");
                
                buf.append("<td>");
                if (results[0].AlloyWheels)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                buf.append("</td>");
                
                if (results.length > 1) {
                    buf.append("<td>");
                    if (results[1].AlloyWheels)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
                
                if (results.length > 2) {
                    buf.append("<td>");
                    if (results[2].AlloyWheels)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">CD player:</td>");
                
                buf.append("<td>");
                if (results[0].CDPlayer)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");   }
                buf.append("</td>");
                
                if (results.length > 1) {
                    buf.append("<td>");
                    if (results[1].CDPlayer)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
                
                if (results.length > 2) {
                    buf.append("<td>");
                    if (results[2].CDPlayer)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Air con:</td>");
                
                buf.append("<td>");
                if (results[0].AirCon)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                buf.append("</td>");
                
                if (results.length > 1) {
                    buf.append("<td>");
                    if (results[1].AirCon)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
                
                if (results.length > 2) {
                    buf.append("<td>");
                    if (results[2].AirCon)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            
            buf.append("<tr class=\"showroomDetails\">");
                buf.append("<td class=\"showroomLabel\">Leather:</td>");
                
                buf.append("<td>");
                if (results[0].Leather)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                buf.append("</td>");
                
                if (results.length > 1) {
                    buf.append("<td>");
                    if (results[1].Leather)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
                
                if (results.length > 2) {
                    buf.append("<td>");
                    if (results[2].Leather)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
            buf.append("</tr>");
            
            
            buf.append("<tr class=\"showroomDetailsAlt\">");
                buf.append("<td class=\"showroomLabel\">Sat nav:</td>");
                
                buf.append("<td>");  
                if (results[0].SatNav)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                buf.append("</td>");
                
                if (results.length > 1) {
                    buf.append("<td>");
                    if (results[1].SatNav)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
                
                if (results.length > 2) {
                    buf.append("<td>");
                    if (results[2].SatNav)  {  buf.append("<img src='/images/tick.gif'>");  }  else {  buf.append("-");  }
                    buf.append("</td>");
                }
            buf.append("</tr>");
        
        buf.append("</table>");
    }
    
    sh.innerHTML = buf.toString();
    
    var divWait = $get('divWait');
    hide(divWait);  
}

function onCompletedGetNumberOfCarsInFullShowroom(results)
{
    totalCarsInShowroom = results;      
    showNextPrevious();
}

function showNextPrevious()
{
    var lnkPrevious = $get('lnkPrevious');
    var lnkNext = $get('lnkNext');

    if (selectedPage > 1)
        show(lnkPrevious);
    else
        hide(lnkPrevious);
        
    if (totalCarsInShowroom > selectedPage * 3)
        show(lnkNext);
    else
        hide(lnkNext);
}

function nextPage()
{
    var divWait = $get('divWait');
    showWaiting(divWait);
    
    selectedPage = selectedPage + 1;
    populateShowroomPage(selectedPage);
    
    showNextPrevious();
}

function previousPage()
{
    var divWait = $get('divWait');
    showWaiting(divWait);
    
    selectedPage = selectedPage - 1;
    populateShowroomPage(selectedPage);
    
    showNextPrevious();
}

function populateShowroomPage(selectedPage)
{        
    Carselect.Services.ShowroomService.GetFullShowroom(selectedPage, onCompletedGetShowroomPage, onFailedGetShowroom);
}

function doPageLoad()
{
    selectedPage = 1;
    Carselect.Services.ShowroomService.GetNumberOfCarsInShowroom(onCompletedGetNumberOfCarsInFullShowroom, onFailedGetNumberOfCarsInShowroom);
    
    var divWait = $get('divWait');
    showWaiting(divWait);
    
    populateShowroomPage(selectedPage);
}
