﻿
$(document).ready(function() {

    $("#btnSubmitRC").bind("click", function(event)
    { event.preventDefault(); submitRCForm(); return false; });

    $("#btnDC_Close").bind("click", function() { DriverCardsPanel.hide(); return false; });
    DriverCardsPanel.hide();
    $(document).keyup(function(e) {
        if (e.keyCode == 27) { DriverCardsPanel.hide(); }
    });

//    $("#txtvehicleRegChecker").bind("submit", function(event)
    //    { event.preventDefault(); submitRCForm(); return false; });

    document.onkeypress = stopRKey; 

});

function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}

DriverCardsPanel = {
    show: function() {
        $(document).showMask();
        var rep = $('#DriverCards');
        rep.show();
        rep.formcenter();
    },

    hide: function() {
        var rep = $('#DriverCards');
        rep.hide();
        $(document).hideMask();
    }
}

function submitRCForm() {
    var vehicleReg = $("[id*=txtvehicleRegChecker]").val();
    
    vehicleReg = vehicleReg.replace(/ /g, "");
    
    var buff = new StringBuffer();

    if (vehicleReg == "")
        buff.append("Missing Vehicle Registration\r\n");
        
    if (buff.length() > 0)
        alert(String.format('INVALID DETAILS!\nYour request contains the following error(s):\n\n{0}\n\rPlease correct your details and try again.\r\nThank You.', buff.toString()));
    else {
        SalesAndMarketingSite.Services.UserService.GetVehicleCompanyDetail(vehicleReg, onCompletedRegistrationChecker, onFailed);

        // show the please wait animation
        //var divWait = $get('divRCWait');
        var divWait = $('#divRCWait');
        showWaiting(divWait);
    }
}

function onCompletedRegistrationChecker(companyID) {
    //var divWait = $get('divRCWait');
    var divWait = $('#divRCWait');
    hide(divWait);
  //  alert(companyID);
    if (companyID == -1 || companyID == 75 || companyID == 76) {
        $(".lexHeritage").show();
        $(".autoleaseHeritage").hide();
    }
    else {
        $(".autoleaseHeritage").show();
        $(".lexHeritage").hide();        
    }
    DriverCardsPanel.show();
    
    //document.location.href = '/pages/thankyou.aspx';
}

function showWaiting(div) {
    show(div);
    showSpinning(div);
    var existingHTML = div.html();
    div.html(existingHTML + "<br />Please wait...");
}
