$(document).ready(function() {
   $(":text.autotab").keyup(autoTab);

   $(":text.chiffrement").focus(dechiffrement);
   $(":text.chiffrement").blur(chiffrement);
});

/**
 * Fichier contenant les fonctions d'accès aux informations des cartes UGC 5 et 7.
 */

/**
 * Appelle via DWR la méthode getCardInfo.
 */
function getCardInfo(cardBlockId) {
    var cardBlock = $("#" + cardBlockId);
    hideAllCardsBlock(cardBlock);
    var cardNumber = getCardNumber(cardBlock);
    if (cardNumber.length > 0) {
        var cardType = getCardType(cardBlock);
        dwr_cardManager.getCardInfo(cardNumber, cardType, cardBlockId, getCardInfo_callBack);
    }
}

/**
 * Masque les blocs des cartes.
 */
function hideAllCardsBlock(cardBlock) {
    $(cardBlock).find(".errorMessage").hide();
    $(cardBlock).find(".solde").hide();
    $(cardBlock).find(".cardExpiration").hide();
    $(cardBlock).find(".cardHistory").hide();
}

/**
 * Réinitialise les champs d'un bloc de cartes.
 */
function initCardBlock(cardBlock) {
    $(cardBlock).find(":input.cardNumber").val("");
    $(cardBlock).find(".soldePlaces").text("");
    $(cardBlock).find(".expirationDate").text("");
    $(cardBlock).find(".cardExpiration").find(".expirationRow").remove();
    $(cardBlock).find(".cardHistory").find(".soldeRow").remove();
    hideAllCardsBlock(cardBlock);
}

/**
 * Fonction de rappel de l'appel DWR "getCardInfo".
 * Par défaut, les blocs sont masqués.
 */
var getCardInfo_callBack = function(cardInfoBean) {
    var cardBlock = $("#" + cardInfoBean.callbackParameters);

    if (cardInfoBean.error) {
        $(cardBlock).find(".errorMessage").show();
    } else {
        $(cardBlock).find(".errorMessage").hide();
        $(cardBlock).find(".soldePlaces").text("" + cardInfoBean.totalSolde);
        $(cardBlock).find(".solde").show("fast");

        // si la date d'expiration est renseignée, on affiche le bloc correspondant
        if (cardInfoBean.expirationDate != null) {
            $(cardBlock).find(".expirationDate").text("" + cardInfoBean.expirationDate);
            $(cardBlock).find(".cardExpiration").show();
        }

        // soldes en rapport avec la date d'expiration
        var expirationDateList = cardInfoBean.soldes;
        var expirationDateTable = $(cardBlock).find(".cardExpiration");

        // si un tableau de dates expirées renseignées, on affiche le bloc correspondant
        if (expirationDateList.length > 0) {
            // on vide le tableau
            $(expirationDateTable).find(".expirationRow").remove();
            // on ajoute les lignes d'historique au tableau
            jQuery.each(expirationDateList, function(index, element) {
                createTableRowExpiration(expirationDateTable, element);
            });
            $(expirationDateTable).show();
        }

        // historique des e-tickets
        var historyList = cardInfoBean.historique;
        var historyTable = $(cardBlock).find(".cardHistory");

        // si un historique est renseigné, on affiche le bloc correspondant
        if (historyList.length > 0) {
            // on vide le tableau
            $(historyTable).find(".soldeRow").remove();
            // on ajoute les lignes d'historique au tableau
            jQuery.each(historyList, function(index, element) {
                createTableRowHistory(historyTable, element);
            });
            $(historyTable).show();
        }
    }
}

/**
 * Créé une ligne d'un tableau de classe ".cardHistory"
 */
function createTableRowHistory(table, element) {
    var templateRow = $(table).find(".soldeTemplateRow");
    var parent = $(templateRow).parent().get(0);
    var row = $(templateRow).clone();
    $(row).removeClass("soldeTemplateRow");
    $(row).addClass("soldeRow");
    $(row).find(".soldeSeanceRow").text("" + element.showing);
    $(row).find(".soldeMovieRow").text("" + element.movie);
    $(row).find(".soldeComplexRow").text("" + element.complex);
    $(row).find(".soldeNumberOfSeatsRow").text("" + element.ticketCount);
    $(row).show();
    $(row).appendTo(parent);
}

/**
 * Créé une ligne d'un tableau de classe ".cardExpiration"
 */
function createTableRowExpiration(table, element) {
    var templateRow = $(table).find(".expirationTemplateRow");
    var parent = $(templateRow).parent().get(0);
    var row = $(templateRow).clone();
    $(row).removeClass("expirationTemplateRow");
    $(row).addClass("expirationRow");
    $(row).find(".expirationDesignationRow").text("" + element.validityZone);
    $(row).find(".expirationDateFinRow").text("" + element.endOfValidity);
    $(row).find(".expirationSoldeRow").text("" + element.solde);
    $(row).show();
    $(row).appendTo(parent);
}

