﻿
var navigation_path = "";
var navigation_page = "carlist.aspx";
var enable_advanced_options = false;

// Cache object used by vehicle list pages to remember selected make, mode, page etc
function Cache()
{

}

var cache = null;

function initPage()
{
    // construct the cache as at page load
    cache = new Cache();
    cache.make = document.getElementById("make").value;
    cache.model = document.getElementById("model").value;
    
    if (document.getElementById("keyword") != null)
        cache.keyword = document.getElementById("keyword").value;
    if (document.getElementById("hdnView") != null)
        cache.view = document.getElementById("hdnView").value;        
    if (document.getElementById("sort") != null)        
        cache.sort = document.getElementById("sort").value;
    if (document.getElementById("dealer") != null)
        cache.dealer = document.getElementById("dealer").value;        
    else if (document.getElementById("dealer2") != null)
        cache.dealer = document.getElementById("dealer2").value;        
    if (document.getElementById("advanced") != null)
        cache.advanced = document.getElementById("advanced").value;      
    cache.extra = "";
    if (document.getElementById("fromYear") != null)
        cache.extra += "&fromYear=" + document.getElementById("fromYear").value;      
    if (document.getElementById("toYear") != null)
        cache.extra += "&toYear=" + document.getElementById("toYear").value;      
              
}

// the user has clicked on the filter button, redislay the page based on the newly selected options
function filterClick() {    
    var dealer = null;
    if (document.getElementById("dealer") != null)
        dealer = document.getElementById("dealer").value;
    else if (document.getElementById("dealer2") != null)
        dealer = document.getElementById("dealer2").value;        
    var advanced = null;
    if (enable_advanced_options && document.getElementById("advanced"))
        advanced = document.getElementById("advanced").value;

    var extra = "";
    if (document.getElementById("fromYear"))
        extra += "&fromYear=" + document.getElementById("fromYear").value;
    if (document.getElementById("toYear"))
        extra += "&toYear=" + document.getElementById("toYear").value;
        
    navigate(
        document.getElementById("make").value,
        document.getElementById("model").value,
        document.getElementById("hdnView") != null ? document.getElementById("hdnView").value : null,
        dealer,
        null, // review to default sort settings when applying new filter,
        null,
        document.getElementById("keyword") != null ? document.getElementById("keyword").value : null,
        advanced,
        extra
   );
}

function advancedFilterClick(returnLink) {
    var params = "";
    var extra = "";
        
    if (document.getElementById("fromPrice"))
        params += "fromPrice=" + document.getElementById("fromPrice").value;
    if (document.getElementById("toPrice"))
        params += "|toPrice=" + document.getElementById("toPrice").value;
    if (document.getElementById("fromYear"))
    {
        params += "|fromYear=" + document.getElementById("fromYear").value;
        extra += "&fromYear=" + document.getElementById("fromYear").value;
    }
    if (document.getElementById("toYear"))
    {
        params += "|toYear=" + document.getElementById("toYear").value;
        extra += "&toYear=" + document.getElementById("toYear").value;
    }
    if (document.getElementById("fromMileage"))        
        params += "|fromMileage=" + document.getElementById("fromMileage").value;
    if (document.getElementById("toMileage"))
        params += "|toMileage=" + document.getElementById("toMileage").value;
    if (document.getElementById("bodyType"))
        params += "|bodyType=" + document.getElementById("bodyType").value;
    if (document.getElementById("fuel"))
        params += "|fuel=" + document.getElementById("fuel").value;       
    if (document.getElementById("location"))
        params += "|location=" + document.getElementById("location").value;       
    
	

	
	
	
	if(document.getElementById("advanced") && document.getElementById("advanced").value.indexOf('listingTypeCode')>-1){


 params += "|listingTypeCode=" + document.getElementById("advanced").value.replace('listingTypeCode=','');


}
	
	
	
	
	
	
	
	
	
	
	
    if (returnLink)
        params += "|returnLink=true";
    
    var dealer = null;
    if (document.getElementById("dealer") != null)
        dealer = document.getElementById("dealer").value;
    else if (document.getElementById("dealer2") != null)
        dealer = document.getElementById("dealer2").value;           
    
    navigate(
        document.getElementById("make").value,
        document.getElementById("model").value,
        null,
        dealer,
        null, 
        null,
        document.getElementById("keyword") != null ? document.getElementById("keyword").value : null,
        params,
        extra
   );    
}

// the user has changed the sort order, redisplay the page based on the newly selected sort order
function sortClick() {
    navigate(cache.make, cache.model, cache.view, cache.dealer, 
        document.getElementById("sort").value,
        null,
        cache.keyword,
        cache.advanced);    
}

function navigate(make, model, view, dealer, sort, page, keyword, advanced, extra) {
    var params = "?make=" + make;
    params += "&model=" + model;
    if (sort)
        params += "&sort=" + sort;
    if (dealer)
        params += "&dealer=" + dealer;
    if (page)
        params += "&page=" + page;
    if (view)
        params += "&view=" + view;
    if (keyword)
        params += "&keyword=" + keyword;
    if (advanced)
        params += "&advanced=" + advanced;
    if (extra && extra != "")
        params += extra;

    window.location.href = navigation_path + navigation_page + params;
}

function changeDealerShip() {
    if (document.getElementById("dealership").value != "")
        window.location.href = "carlist.aspx?dealer=" + document.getElementById("dealership").value;
}

function changeDealer() {
    if (document.getElementById("dealer").value != "")
        window.location.href = navigation_page + "?dealer=" + document.getElementById("dealer").value;
}

// class used for pre-loading the list of models for the available makes
function Model(id, name, make) {
    this.id = id;
    this.name = name;
    this.make = make;
}

var models = [];

function addOption(select, text, value) {
    var oOption = new Option(text, value);
    select.options[select.options.length] = oOption;
}

// the make has been changed, populate the model drop down based on the list of pre-loaded models
function changeMake() {
    var select = document.getElementById("model");
    var id = document.getElementById("make").value;
    select.options.length = 0;
    addOption(select, "Any Model", "");
    for (var i = 0; i < models.length; i++) {
        if (models[i].make == id)
        {
            addOption(select, models[i].name, models[i].id);
        }
    }
}

// this function relies on the AJAX javascript dynamically inserted into the admin/carlist.aspx page
function changeMakeAjax(make, model, all, selectModelText)
{    
    var select = document.getElementById(model);
    select.options.length = 0;
    if (selectModelText)
        addOption(select, selectModelText, "");
    else
        addOption(select, "Any Model", "");

    if (document.getElementById(make).value == "")
        return;

    var list = null;
    
    if (all)
        list = Webdesign.Carupdater.UI.Ajax.ListModels(document.getElementById(make).value).value;
    else
        list = Webdesign.Carupdater.UI.Ajax.ListDealerModels(document.getElementById(make).value).value;    
    
    
    if (list.models.model.id)
    {
        addOption(select, list.models.model.name, list.models.model.id);   
    }
    else if (list.models)
    {
        for (var i = 0; i < list.models.model.length; i++)
        {
            addOption(select, list.models.model[i].name, list.models.model[i].id);   
        }        
    }
}

// this function relies on the AJAX javascript dynamically inserted into the admin/carlist.aspx page
function changeMakeColour(make, colour)
{    
    var select = document.getElementById(colour);
    select.options.length = 0;
    addOption(select, "Choose Colour", "");

    if (document.getElementById(make).value == "")
        return;

    var list= Webdesign.Carupdater.UI.Ajax.ListColours(document.getElementById(make).value).value;    
    
    
    if (list.colours.colour.id)
    {
        addOption(select, list.colours.colour.name, list.colours.colour.name);   
    }
    else if (list.colours)
    {
        for (var i = 0; i < list.colours.colour.length; i++)
        {
            addOption(select,  list.colours.colour[i].name, list.colours.colour[i].name);   
        }        
    }
}

function go(page) {
    navigate(cache.make, cache.model, cache.view, cache.dealer, cache.sort, page, cache.keyword, cache.advanced, cache.extra);
}

// user has chosen to delete an image
function removeImage(id)
{
    if (window.confirm('Are you sure you wish to delete this image?'))
    {
        Webdesign.Carupdater.UI.Ajax.DeleteImage(id);
        window.location.reload(true);
    }
}

// user has chosen to set a specific image as the first image to display for a vehicle
function setFirstImage(id)
{
    Webdesign.Carupdater.UI.Ajax.SetFirstImage(id);
    window.location.reload(true);
}

// sets the value in a text input to be that selected in a select control
function setTextValue(selectElement, textElement)
{
    document.getElementById(textElement).value = document.getElementById(selectElement).value;
}

function previewVehicle(vehicleID)
{
    //window.location.href = "../used/carview.aspx?id=" + vehicleID;
    window.open("../used/carview.aspx?id=" + vehicleID);
    //return false;
}

function addImages(vehicleID)
{
    window.location.href = "imageupload.aspx?id=" + vehicleID;
    return false;
}

function goBack(){
    history.go(-1);    
}

function goUrl(url)
{
    if (window.parent != null)
        window.parent.location = url;
    else
        window.location.href = url;
    return false;
}

function goDealerUrl(url)
{
    window.location.href = url + "?dealer=" + document.getElementById('dealer').value;
}

function showLegal()
{
    window.open('../public/legal.htm', 'legal', 'height=300,width=540,menubar=0,toolbar=0');
}

function requestMake()
{
    window.open('request.aspx?mode=make', 'request', 'height=400,width=600,menubar=0,toolbar=0');
}

function requestModel()
{
    window.open('request.aspx?mode=model', 'request', 'height=400,width=600,menubar=0,toolbar=0');
}

var play_window = null;

var vv_player = null;
var vv_errors = 0;

function playVideo(id, popup) {

    if (popup) {
        
        play_window = new Window(
        {
            className: "alphacube",
            width: 400,
            height: 360,
            zIndex: 1000,
            resizable: false,
            draggable: false,
            title: "",
            showEffect: Element.show,
            showEffectOptions: { queue: 'front' },
            hideEffect: Element.hide,
            maximizable: false,
            minimizable: false,
            url: "video.aspx?id=" + id,
            destroyOnClose: true            
        });        
        play_window.showCenter(true);

        return;
    }

    initPlayWindow();
    videoStart(id);
}

function initPlayWindow() {
    play_window = new Window(
        {
            className: "alphacube",
            width: 380,
            height: 360,
            zIndex: 1000,
            resizable: false,
            draggable: false,
            title: "",
            showEffect: Element.show,
            showEffectOptions: { queue: 'front' },
            hideEffect: Element.hide,
            maximizable: false,
            minimizable: false,
            //url: pageurl,
            //destroyOnClose: true,
            closeCallback: canClosePlayer
        });
    play_window.setContent("virtual-video-player");
    play_window.showCenter(true);
}

function playAllVideos() {
    initPlayWindow();
    videoStartAll();
}

function videoStartAll() {
    vv_errors++;
    if (vv_errors > 10)
        return;
    
    try {
        vv_player = document.getElementById("httpTV");
        if (vv_player == null) {
            window.setTimeout("videoStartAll();", 1000);
            return;
        }
        var playList = document.getElementById("virtual-video-play-list").value;
        vv_player.Content.httpTV.PlayVideo(4, playList);
    } catch (ex) {
        window.setTimeout("videoStartAll();", 1000);
    }
}


function videoStart(id) {
    vv_errors++;
    if (vv_errors > 10)
        return;

    try
    {
        vv_player = document.getElementById("httpTV");

        if (vv_player == null) {
            window.setTimeout("videoStart(" + id + ");", 1000);
            return;
        }

        vv_player.Content.httpTV.PlayVideo(4, id);
    } catch(ex) {
        window.setTimeout("videoStart(" + id + ");", 1000);
    }        
}

function canClosePlayer() {

    if (vv_player && vv_player.Content && vv_player.Content.httpTV)
        vv_player.Content.httpTV.StopVideo();
    return true;
}

function closeVideo() {
    if (play_window)
        play_window.close();
    else
        window.parent.play_window.close();            
}


/*
below are some additional functions exposed by the VV player
function NextVideo() {

try {
plugin.Content.httpTV.NextVideo();
} catch (ex) {
       
}
}

function PauseVideo() {

try {
plugin.Content.httpTV.PauseVideo();
} catch (ex) {
       
}
}

function ReplayVideo() {

try {
plugin.Content.httpTV.ReplayVideo();
} catch (ex) {
       
}
}

function StopVideo(sender) {

try {       
document.getElementById(sender).style.top = "-5000px";
plugin.Content.httpTV.StopVideo();
} catch (ex) {

}
}
*/   