/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : -
// Note   : -
// Date   : -
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Inclusion dynamique des fichiers externes javascripts
/*----------------------------------------------------------------------------*/
function importJavascript(src)
{
	document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>");
}
function importCss(href, media)
{
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />');
}

/*----------------------------------------------------------------------------*/
// IMPORTATIONS DE FICHIERS
/*----------------------------------------------------------------------------*/
importJavascript("/includes/js/plugins/jquery.defaultbutton.js");
importJavascript("/includes/js/json2.js");
importJavascript("/includes/js/plugins/jquery.colorbox.js");
importCss("/includes/js/plugins/colorbox/colorbox.css", "screen");
importCss("/includes/js/plugins/datePicker.css", "screen");

/*----------------------------------------------------------------------------*/
// DÉCLARATION JQUERY
/*----------------------------------------------------------------------------*/
$(document).ready(function() {
	$("hr").replaceWith("<div class=\"hr\">&nbsp;</div>");
	$("tr:odd", "table").children("td").addClass("Impair");
	$("tr:even", "table").children("td").addClass("Pair");

	// ------------------------------------------------------------------------
	// CALENDRIER
	// ------------------------------------------------------------------------
	if (jQuery("body").attr("id") == "lang-FR") {
	    var nomMois = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];
	    var nomJournee = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
	    var nomJourneeMin = ['D', 'L', 'M', 'M', 'J', 'V', 'S'];
	}
	else {
	    var nomMois = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	    var nomJournee = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	    var nomJourneeMin = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
	}

	if (jQuery(".Calendrier").length > 0) {
	    jQuery(".Calendrier").datepicker({
	        monthNames: nomMois,
	        dayNames: nomJournee,
	        dayNamesMin: nomJourneeMin,
	        showOtherMonths: true,
	        dateFormat: 'yy-mm-dd',
	        beforeShow: function() {
	            // au besoin
	        }
	    });
	}
		
	$(".BtAjoutVendeur").click(function(){
		ajouterVendeur();
		return false;
	});
	
	//<![CDATA[
    $("#ChampNomVendeur").keypress(function(e) {
        if (e.which == 13) {
            return ajouterVendeur();
        }
    });
	
	$(".BtReinitialisation").click(function(){
		resetList();
		return false;
	});

});

function showChangeEmail() {
    $.colorbox({
        inline: true,
        href: '#ContainerModCourriel',
        transition: 'fade',
        opacity: 0.7,
        width:"325px"
    })
    $("#colorbox").appendTo('form');
}

function showChangeEmailAfterPostback() {
    $.colorbox({
        inline: true,
        href: '#ContainerModCourriel',
        transition: 'fade',
        opacity: 0.7,
        width: "325px"
    })
    $("#colorbox").appendTo('form');
    
}



/*----------------------------------------------------------------------------*/
// AFFICHAGE D'UN MESSAGE DANS UN POPUP
/*----------------------------------------------------------------------------*/
function display() {
	$.colorbox({html:'<p class="CBMessage">'+arguments[0]+'</p>', transition:'none', opacity:0.7});
}

function ajouterVendeur() {
	$nomVendeur = $("#ChampNomVendeur").val();	
	$("#ListeVendeurs").children("li.PremierItem").remove();
	$nomVendeur = $.trim($nomVendeur);
    $("#ListeVendeurs").append('<li><a href="#" onclick="retirerVendeur(this); return false;" class="BtRetrait">'+ $nomVendeur +'<span>[x]</span></a></li>');
	$("#ChampNomVendeur").val("");
	return false;
}

function retirerVendeur(v) {
	$(v).parent("li").remove();
	if($("#ListeVendeurs").children("li").length < 1) $("#ListeVendeurs").append('<li class="PremierItem">Il n\'y a aucun vendeur dans la liste</li>');
}

function resetList() {
	$("#ListeVendeurs").children("li").remove();
	$("#ListeVendeurs").append('<li class="PremierItem">Il n\'y a aucun vendeur dans la liste</li>');
}



/*----------------------------------------------------------------------------*/
// VALIDATION DES FORMULAIRES
/*----------------------------------------------------------------------------*/
//function CheckForm(groupName) {
//    if (typeof (Page_ClientValidate) == 'function') {
//        if (!Page_ClientValidate(groupName)) {
//            var arrayError = new Array();
//            //Ne pas oublier d'appliquer "Page_ClientValidate()" avant d'appeler cette procédure
//            for (var i = 0; i < Page_Validators.length; i++) {
//                if (Page_Validators[i].validationGroup == groupName && !Page_Validators[i].isvalid) {
//                    if (Page_Validators[i].controltovalidate != "")
//                        arrayError.push(Page_Validators[i].controltovalidate);
//                }
//            }

//            // Show Style on the control
//            ShowFormError(arrayError.join(","));
//            return false;
//        }
//    }
//    return true;
//}

// strErrorCS : String with id comma separated
function ShowFormError(strErrorCS) {
    var classError = "Erreur";
    var arrayError = strErrorCS.split(",");
    if (arrayError.length > 0) {
        jQuery('.' + classError).removeClass(classError); //Cleanup
        for (var i = 0; i < arrayError.length; i++) {
            jQuery('#' + arrayError[i]).addClass(classError);
        }
    }
}

function CheckForm(formName) {
    if (typeof (Page_ClientValidate) == 'function') {
        if (!Page_ClientValidate(formName)) {
            ValidateForm(formName, true, "Erreur");
            return false;
        }
    }
    return true;
}

function ValidateForm(groupName, OnlyOneErrorByField, classError) {
    var msg = new Array();
    var ctl = new Array();
    if (classError != '') jQuery('.' + classError).removeClass(classError);
    for (var i = 0; i < Page_Validators.length; i++) {
        if (Page_Validators[i].validationGroup == groupName && !Page_Validators[i].isvalid) {
            if (OnlyOneErrorByField) {
                var exist = false;
                for (var y = 0; y < ctl.length; y++) {
                    if (ctl[y] == Page_Validators[i].controltovalidate)
                        exist = true;
                }
                if (!exist) {
                    msg[msg.length] = Page_Validators[i].errormessage;
                    if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
                }
            }
            else {
                msg[msg.length] = Page_Validators[i].errormessage;
                if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
            }
            ctl[i] = Page_Validators[i].controltovalidate;
        }
    }
    return msg;
}


