﻿//Cette variable représente le préfixe des id «client-side» des contrôles .Net invoqués dans les fonctions javascript de création de profil
var controlPrefix = "";

//Ajoute un bouton radio à un groupe mutuellement exclusif
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]

        if (elm.type == "radio") {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

//Permet d'activer ou de désactiver un élément de formulaire à partir du clic d'un autre élément
function changeInputStatus(inputId, status) {
    if (!document.getElementById)
        return;

    var inputToDisable = document.getElementById(inputId);
    if (inputToDisable != null) {
        if (status == "enable") {
            inputToDisable.disabled = false;
        }
        else {
            inputToDisable.disabled = true;
        }
    }
}

function confirmNoSelection(nameregex, langue) {
    re = new RegExp(nameregex);
    imgSelected = false;

    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == "radio") {
            if (re.test(elm.name)) {
                if (elm.checked) {
                    imgSelected = true;
                }
            }
        }
    }

    if (imgSelected == false) {
        if (langue == "fr") {
            return confirm("Aucune image n'est selectionnée, vous garderez donc l'avatar par défaut. Voulez-vous continuer ?");
        } else {
            return confirm("Since no image is selected, you will keep the default avatar. Do you want to continue ?");            
        }
    }
}

function initJCrop(controlPrefix) {
    if ($(controlPrefix + '_imgCrop').length > 0) {

        //On prépare les dimensions et les coordonnées de la selection originale de JCrop
        var imageCrop = $(controlPrefix + '_imgCrop');

        var selectionW = Math.ceil(imageCrop.attr("width") * 0.80);
        var selectionH = Math.ceil(imageCrop.attr("height") * 0.80);

        var squareSize = 48;

        if (selectionW > selectionH) {
            squareSize = selectionH;
        }
        else {
            squareSize = selectionW;
        }

        var selectionX = Math.ceil((imageCrop.attr("width") - squareSize) / 2);
        var selectionY = Math.ceil((imageCrop.attr("height") - squareSize) / 2);

        var presetSelection = new Array();
        presetSelection["x"] = selectionX;
        presetSelection["y"] = selectionY;
        presetSelection["w"] = squareSize;
        presetSelection["h"] = squareSize;

        storeCoords(presetSelection);

        var api = $.Jcrop(controlPrefix + '_imgCrop', { onSelect: storeCoords, bgOpacity: .4, aspectRatio: 1 });
        api.setSelect([selectionX, selectionY, squareSize, squareSize]);
    }
}

//Function de sauvegarde des dimensions et des coordonnées de jCrop
function storeCoords(c) {
    $(controlPrefix + '_X').val(c.x);
    $(controlPrefix + '_Y').val(c.y);
    $(controlPrefix + '_W').val(c.w);
    $(controlPrefix + '_H').val(c.h);
};

//Pour s'assurer que l'usager a taillé l'image téléchargée avant de complèter la création de compte
function validCropCompleted_ClientValidate(source, arguments) {

    if ($(controlPrefix + '_pnlCrop').length > 0) {

        arguments.IsValid = false;

    }
    else {
        arguments.IsValid = true;
    }
} 
