﻿// Default JavaScript

/*
#########################################################################################
## Global Section - All variables and/or constants                                     ##
#########################################################################################
*/

//Global Constants

//Global Variables

// Global Functions

// validateUsedAutoFields
// validates the used auto fields on the home page
// 
// @author	kgrove - 03/10/06
// @edit	-
// @param	p_oSourceOption - Source option to copy
//          p_sImageRootPath - root path to the images directory
// @return	none
function validateUsedAutoFields(p_sControlIndicator, p_sImageRootPath) {
    var oMake = document.getElementById(p_sControlIndicator + "ddl_inv_Make");
    var oZipCode = document.getElementById(p_sControlIndicator + "txt_inv_Zip");
    var oButton = document.getElementById(p_sControlIndicator + "btn_inv_go");
    var oModel = document.getElementById(p_sControlIndicator + "ddl_inv_Model");

    if (parseInt(oMake.value) > 0) {
        oModel.disabled = false;
    }
    else {
        oModel.disabled = true;
    }
    if (parseInt(oMake.value) > 0 && oZipCode.value.length >= 5) {
        oButton.disabled = false;
        oButton.src = p_sImageRootPath + "gobutton_large.gif";
        oButton.style.cursor = "pointer";
    }
    else {
        oButton.disabled = true;
        oButton.src = p_sImageRootPath + "gobutton_large_off.gif";
        oButton.style.cursor = "default";
    }
}


// validateDealerFields
// validates the dealer fields on the home page
// 
// @author	kgrove - 03/10/06
// @edit	-
// @param	p_iPosition - Where in the chain the combobox resides
//          p_oSourceOption - Source option to copy
//          p_sImageRootPath - root path to the images directory
// @return	none
function validateDealerFields(p_iPosition, p_sControlIndicator, p_sImageRootPath) {
    var oMake = document.getElementById(p_sControlIndicator + "ddl_loc_Make");
    var oZipCode = document.getElementById(p_sControlIndicator + "txt_loc_Zip");
    var oDistance = document.getElementById(p_sControlIndicator + "ddl_loc_Distance");
    var oButton = document.getElementById(p_sControlIndicator + "btn_loc_go");
    var oCategory = document.getElementById(p_sControlIndicator + "ddl_loc_Category");

    var validateOk = true;
    switch (p_iPosition) {
        case 1: // Category
            validateOk = false;
            break;
        case 2:
            validateOk = true;
            break;
    }

    if (validateOk && parseInt(oMake.value) > 0 && oZipCode.value.length >= 5 && parseInt(oDistance.value) > 0) {
        oButton.disabled = false;
        oButton.src = p_sImageRootPath + "gobutton_large.gif";
        oButton.style.cursor = "pointer";
    }
    else {
        oButton.disabled = true;
        oButton.src = p_sImageRootPath + "gobutton_large_off.gif";
        oButton.style.cursor = "default";
    }
}


// resetHomeNCBComboBoxes
// resets the comboboxes for the home page NCB
// 
// @author	kgrove - 03/10/06
// @edit	-
// @param	p_iPosition - Where in the chain the combobox resides
//          p_sControlIndicator - View State ID for the page
//          p_sImageRootPath - root path to the images directory
// @return	the outer HTML of the element
function resetHomeNCBComboBoxes(p_iPosition, p_sControlIndicator, p_sImageRootPath) {
    var oYear = document.getElementById(p_sControlIndicator + "ddl_ncb_Year");
    var oMake = document.getElementById(p_sControlIndicator + "ddl_ncb_Make");
    var oModel = document.getElementById(p_sControlIndicator + "ddl_ncb_Model");
    var oButton = document.getElementById(p_sControlIndicator + "btn_ncb_go");

    switch (p_iPosition) {
        case 1: //Year
            _addOption(oModel, "Select a Model", "0");
            oModel.value = "0";
            oModel.disabled = true;
            oButton.disabled = true;
            oButton.src = p_sImageRootPath + "gobutton_large_off.gif";
            oButton.style.cursor = "default";
            break;
        case 2: //Make            
            oButton.disabled = true;
            oButton.src = p_sImageRootPath + "gobutton_large_off.gif";
            oButton.style.cursor = "default";
            break;
        case 3: //Model
            oButton.disabled = false;
            oButton.src = p_sImageRootPath + "gobutton_large.gif";
            oButton.style.cursor = "pointer";
            break;
    }

}


// enableNCBGoButton
// enables the go button on the Home page and removes the "Select" item from the NCB make drop-down
// 
// @author	kgrove - 03/10/06
// @edit	-
// @param	p_oElement - NCB Model Drop-Down
//          p_sGoButton - Id of the go button
// @return	the outer HTML of the element
function enableNCBGoButton(p_oElement, p_sGoButton) {
    _removeOption(p_oElement, "0");
    document.getElementById(p_sGoButton).disabled = false;
}

// @author	achow - 12/29/2008
// @edit	-
// @param	p_oElement - Drop-Down
//          p_sGoButton - Id of the go button
// @return	the outer HTML of the element
function enableGoButton(p_oElement, p_sGoButton) {
    _removeOption(p_oElement, "0");
    document.getElementById(p_sGoButton).disabled = false;
}


// Checks for an Enter key
// triggers an onclick event to the form's button when an enter key is pressed.
// 
// @author	Greg Mercado - 04/07/06
// @edit	-
// @param	e - event
//@param	buttonId - button to be click
//@param	sourceId - source to be evaluated.

function checkEnterKeyZip(e, buttonId, sourceId) {

    var bt = document.getElementById(buttonId);
    var source = document.getElementById(sourceId);
    var key;

    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13) {
        if (source.selectedIndex > 0);
        {
            bt.click();
            return false;
        }
    }
    else {
        if (key == 0 || key == 8) {  //Allow Delete, Arrows, BackSpace, Enter
            return true;
        }
        return isInteger(String.fromCharCode(key)); // make sure its numeric
    }
}



/*
Client Browser detection 
*/
// Detecting silverlight version
var isSLInstalled = Silverlight.isInstalled(null);

getSilverlightVersion = function() {
    var SLVersion;

    try {

        try {
            var control = new ActiveXObject('AgControl.AgControl');

            if (control.IsVersionSupported("3.0"))

                SLVersion = 3; else

                if (control.IsVersionSupported("2.0"))

                SLVersion = 2; else

                SLVersion = 1;

            control = null;
        }

        catch (e) {

            var plugin = navigator.plugins["Silverlight Plug-In"];
            if (plugin) {

                if (plugin.description === "1.0.30226.2")

                    SLVersion = 2; else

                    SLVersion = parseInt(plugin.description[0]);
            }

            else

                SLVersion = 0;
        }

    }

    catch (e) {

        SLVersion = 0;
    }

    return SLVersion;
}

var Version = getSilverlightVersion();
var VersionName = "No silverlight installed";
if (Version > 0)
    VersionName = "Silverlight " + Version

// Use these to debug
function CallSuccess(resuslt) {
    //window.alert(resuslt);
}

function CallFailed(resuslt) {
    //alert(resuslt);
}

// Silverlight setting type ID is 1
var slParameters = "{'userSettingTypeID':'1','info':'" + VersionName + "'}"

// More setting can be check, just add the similar code like the one that is commented out below
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "default.aspx/CaptureClientSettings",
        contentType: "application/json; charset=utf-8",
        data: slParameters,
        dataType: "json"
    });
    //    $.ajax({
    //        type: "POST",
    //        url: "default.aspx/CaptureClientSettings",
    //        contentType: "application/json; charset=utf-8",
    //        data: parameters2,
    //        dataType: "json",
    //        success: CallSuccess,
    //        error: CallFailed
    //    });
});


function MLTClick(MLTType) {
    switch (MLTType) {
        case "Motorcycle":
            ShowLink("Specialty/SearchMotorcycleInv.aspx?showform=true", false);
            break;
        case "Powersport":
            ShowLink("Specialty/SearchPowersportInv.aspx?showform=true", false);
            break;
        case "RV & Trailers":
            ShowLink("Specialty/SearchRVInv.aspx?showform=true", false);
            break;
        case "Boats":
            ShowLink("Specialty/SearchBoatInv.aspx?showform=true", false);
            break;
        case "Personal Watercraft":
            ShowLink("Specialty/SearchPWCInv.aspx?showform=true", false);
            break;
        case "Commercial Trucks":
            ShowLink("Specialty/SearchTruckInv.aspx?showform=true", false);
            break;
        default:
            ShowLink("Specialty/Default.aspx", false);
            break;
    }

}

function ShowLink(url, openLinkInNewWindow) {
    if (openLinkInNewWindow) {
        var windowOptions = "titlebar=yes,width=800px,height=600px,location=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes";
        var windowName = "_blank";
        window.open(url, windowName, windowOptions);
    }
    else {
        window.location = url;
    }

    return false;
}

/* Banner Starts here */
var gCurrentImage = 0; /* the default image in the array of images */
var bannerArray = new Array();

function NextRandomBanner() {
    gCurrentImage = Math.floor(Math.random() * bannerArray.length);
    LoadBanner(bannerArray[gCurrentImage]);
}

function NextBanner() {
    if (gCurrentImage == bannerArray.length - 1) {
        gCurrentImage = 0;
    }
    else {
        gCurrentImage = gCurrentImage + 1;
    }

    LoadBanner(bannerArray[gCurrentImage]);
}

function PreviousBanner() {
    if (gCurrentImage == 0) {
        gCurrentImage = bannerArray.length - 1;
    }
    else {
        gCurrentImage = gCurrentImage - 1;
    }

    LoadBanner(bannerArray[gCurrentImage]);
}

function onPageLoad() {
    $.ajax({
        type: "POST",
        url: "home.aspx/LoadAds",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: CallSuccess,
        error: CallFailed
    });
}

function CallSuccess(res) {
    var bannerCount = res.d.length;
    var i;
    var hasDefaultAd = false;
    var y;
    for (i = 0; i < bannerCount; i++) {
        if (!hasDefaultAd && res.d[i].Priority > 0) {
            hasDefaultAd = true;
            y = i;
        }
        var bannerObj = new BannerObject(res.d[i].ImageURL, res.d[i].DetailURL, res.d[i].OpenLinkInNewWindow, res.d[i].LogDirectClickItemID);
        bannerArray[i] = bannerObj
    }

    if (!hasDefaultAd)
        NextRandomBanner();
    else {
        gCurrentImage = y;
        LoadBanner(bannerArray[gCurrentImage]);
    }
}

function BannerObject(imageURL, detailURL, openLinkInNewWindow, logDirectClickItemID) {
    this.ImageURL = imageURL;
    this.DetailURL = detailURL;
    this.OpenLinkInNewWindow = openLinkInNewWindow;
    this.LogDirectClickItemID = logDirectClickItemID;
}

function CallFailed(res) {
    //alert("fail.." + res);
}

/* Banner Ends here */


