﻿// 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
//    });
});


