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

function autoTab(event) {
    if ($(this).val().length > ($(this).attr("maxlength")-1)) {
        var nextElement = $(this).next(".autotab");
        if (nextElement != null) {
            $(nextElement).select();
            $(nextElement).focus();
        }
    }
}

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

/**
 * Retourne le numéro de carte concaténé à partir d'un bloc de carte.
 */
function getCardNumber(cardBlock) {
    var cardNumber = "";
    $(cardBlock).find(":input.cardNumber").each(function (index, element) {
        cardNumber += $(element).val();
    });
    return cardNumber;
}

/**
 * Retourne le type de carte à partir d'un bloc de carte.
 */
function getCardType(cardBlock) {
    var cardType = $(cardBlock).find(":input.cardType").val();
    return cardType;
}

/**
 * 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(true);
    } else {
        $(cardBlock).find(".errorMessage").hide();
        $(cardBlock).find(".solde").show(true);
        $(cardBlock).find(".soldePlaces").text("" + cardInfoBean.totalSolde);

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

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