﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
//set default value for labels and error messages if client did not define yet
//--------------------------------------------------------------
if (typeof (DefaultMakeValue) == 'undefined')
    DefaultMakeValue = 'Make';

if (typeof (DefaultDTCValue) == 'undefined')
    DefaultDTCValue = '';

if (typeof (DefaultLoadingValue) == 'undefined')
    DefaultLoadingValue = 'Loading...';

if (typeof (NoMakeSelectedErrorMsg) == 'undefined')
    NoMakeSelectedErrorMsg = 'Please select a make';

if (typeof (NoDTCEnteredErrorMsg) == 'undefined')
    NoDTCEnteredErrorMsg = 'Please enter a DTC code';

if (typeof (InnovaLang) == 'undefined')
    InnovaLang = '';

if (typeof (NoCodeFoundMsg) == 'undefined')
    NoCodeFoundMsg = 'Sorry, we were unable to find that code';

////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
//Get parameters from query string (on address bar)
//--------------------------------------------------------------
var param_Make = urlDecode(QUERY_STRING('make'));
var param_Dtc = urlDecode(QUERY_STRING('dtc'));

////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
//Declare variables for all innova controls, they will be
//initialized if having component embeded in a webpage.
//--------------------------------------------------------------

//DTC
var dtcMakesCombobox;
var dtcCodeInput;
var dtcSearchForm;
var dtcErrorDiv;
var dtcDetailsDiv;
var dtcLoadingDiv;


////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
//initialize
//--------------------------------------------------------------

$(document).ready(function() {
    DTC_Init();
});

////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
// BEGIN METHODS for DTC
//--------------------------------------------------------------

function DTC_Init() {
    //--------------------------------------------------------------
    //find controls inside page
    //--------------------------------------------------------------
    dtcMakesCombobox = $("form#innova-dtc-search-box select[name=make]");
    dtcCodeInput = $("form#innova-dtc-search-box input[name=dtc]");
    dtcSearchForm = $("form#innova-dtc-search-box");
    dtcErrorDiv = $("#innova-dtc-search-box .error");
    dtcLoadingDiv = $("#innova-dtc-loading");
    dtcDetailsDiv = $("#innova-dtc-details");

    //If have enough parameter (year/make/model) transfer from another page, then search to show the details result.
    if (typeof (param_Make) != 'undefined' && typeof (param_Dtc) != 'undefined') {
        dtcMakesCombobox.html('<option value="">' + param_Make + '</option>');
        dtcCodeInput.val(param_Dtc);

        DTC_Search(param_Make, param_Dtc);
    }
    else {
        dtcMakesCombobox.html('<option value="">' + DefaultMakeValue + '</option>');
        dtcCodeInput.val(DefaultDTCValue);
    }

    //--------------------------------------------------------------
    //attached events for controls
    //--------------------------------------------------------------
    dtcSearchForm.submit(DTC_SearchSubmit);

    //set method=get for search dtc to force transfering parameter make to the next page
    dtcSearchForm.attr("method", "get");

    //init values for combobox
    DTC_InitMakes();
}

function DTC_InitMakes() {

    dtcMakesCombobox.html('<option value="">' + DefaultLoadingValue + '</option>');
    dtcMakesCombobox.attr("disabled", true);

    $.getJSON(INNOVA_HOST + "/Dtc/Makes?callback=?&lang=" + InnovaLang,
    function(result) {
        dtcMakesCombobox.removeAttr("disabled");
        var makes = result.Data.Data;
        var options = '<option value="">' + DefaultMakeValue + '</option>';
        for (var i = 0; i < makes.length; i++) {
            var make = makes[i];
            options += '<option value="' + make.Value + '">' + make.Value + '</option>';
        }

        dtcMakesCombobox.html(options);

        if (param_Make != null && typeof (param_Make) != 'undefined')
            dtcMakesCombobox.val(param_Make);
    });
}

function DTC_SearchSubmit() {
    var make = dtcMakesCombobox.val();
    var dtc = dtcCodeInput.val();

    return DTC_Search(make, dtc);
}

function validateDTCCode(dtc) {
    var regex1 = /^\d{1,3}$/;
    var regex2 = /^[bcpuBCPU]{1}[\da-zA-Z]{4}$|^[bcpBCP]{1}[\d]{4}[\da-zA-Z]{2}$|^[\d]{1,3}-[a-zA-Z]{1,2}$|^\d{1,2}$/;
    if (regex1.test(dtc) || regex2.test(dtc)) {
        return false;
    }
    return true;

}
function DTC_Search(make, dtc) {
    debugger
    dtc = EscapeSpecialChar($.trim(dtc));

    if (make == '') {
        dtcMakesCombobox.addClass('error');
        dtcErrorDiv.html(NoMakeSelectedErrorMsg);
        return false;
    } else {
        dtcMakesCombobox.removeClass('error');
    }

    if (dtc == '' || dtc == EscapeSpecialChar($.trim(DefaultDTCValue))) {
        dtcCodeInput.addClass('error');
        dtcErrorDiv.html(NoDTCEnteredErrorMsg);
        return false;
    }
    else if (validateDTCCode(dtc)) {
        $("#mesgError").html("The following must be corrected before your search can be performed: <br />* The error code must be a 1 to 3 digit number or it must be a 4 digit number prefixed with B, C, P or U");
        dtcCodeInput.addClass('error');
        return false;
    }
    else {
        $("#mesgError").html("");
        dtcCodeInput.removeClass('error');
    }


    dtcErrorDiv.html("");
    //no div to display dtc details existing in the current page, return true to submit jumping to another page.
    if (dtcDetailsDiv.attr("id") != 'innova-dtc-details')
        return true;

    dtcLoadingDiv.css("display", "block");
    $.getJSON(INNOVA_HOST + "/Dtc/" + make + "/" + dtc + "?callback=?&lang=" + InnovaLang,
        function(result) {
            dtcLoadingDiv.css("display", "none");
            var description = result.Data;
            dtcDetailsDiv.find("#code").html(dtc);
            dtcDetailsDiv.find("#make").html(make);

            if (description != null)
                dtcDetailsDiv.find("#description").html(description);
            else
                dtcDetailsDiv.find("#description").html(NoCodeFoundMsg);
        });

    return false;
}

//--------------------------------------------------------------
// END METHODS for DTC
//--------------------------------------------------------------

