﻿// Fichier JScript

function ShowModal() {
    Form.disable('aspnetForm');
    //var frmmod= $('divModal');
    var heightVP = document.viewport.getHeight();
    var widthVP = document.viewport.getWidth();
    var sOff = document.viewport.getScrollOffsets();

    //var DivAllH= heightVP + sOff.top;
    var DivAllH = 1;
    var DivAllV = widthVP + sOff.left;


    if (window.innerHeight && window.scrollMaxY) { // Firefox 
        DivAllH = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
        DivAllH = document.body.scrollHeight;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
        DivAllH = document.body.offsetHeight + document.body.offsetTop;
    }


    var widthBody = Element.getWidth($(document.body));
    if (widthVP < widthBody) DivAllV = widthBody;


    //  Element.setStyle(frmmod,{display:'block',visibility:'visible',height:DivAllH +'px',width:DivAllV +'px',top:0,left:0});
    //  Element.setOpacity(frmmod,0.5);
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'hidden' });
    });
    modTop = document.viewport.getScrollOffsets().top + 350 + 'px';
    modLeft = 425 + 'px';
    modWidth = 362 + 'px';
    //$('ModalBox').innerHTML= '<div onmousedown=\'HideModal()\' ><br /><br />' + '<p class="txtcontenu">'+'Un exmple de contenu</p>'+'</div>';
    $('ModalBox').setStyle({ top: modTop, left: modLeft, width: modWidth, visibility: 'visible', border: '2px solid #4e4d4d', cursor: 'hand' });
    $('ModalBox').appear();
}

function ShowModalAlerte() {
    Form.disable('aspnetForm');
    Form.enable($('ModalAlerteMoi'));
    //var frmmod= $('divModal');
    var heightVP = document.viewport.getHeight();
    var widthVP = document.viewport.getWidth();
    var sOff = document.viewport.getScrollOffsets();

    //var DivAllH= heightVP + sOff.top;
    var DivAllH = 1;
    var DivAllV = widthVP + sOff.left;


    if (window.innerHeight && window.scrollMaxY) { // Firefox 
        DivAllH = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
        DivAllH = document.body.scrollHeight;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
        DivAllH = document.body.offsetHeight + document.body.offsetTop;
    }

    var widthBody = Element.getWidth($(document.body));
    if (widthVP < widthBody) DivAllV = widthBody;

    //  Element.setStyle(frmmod,{display:'block',visibility:'visible',height:DivAllH +'px',width:DivAllV +'px',top:0,left:0});
    //  Element.setOpacity(frmmod,0.5);
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'hidden' });
    });
    modTop = document.viewport.getScrollOffsets().top + 350 + 'px';
    modLeft = 425 + 'px';
    modWidth = 362 + 'px';
    //$('ModalBox').innerHTML= '<div onmousedown=\'HideModal()\' ><br /><br />' + '<p class="txtcontenu">'+'Un exmple de contenu</p>'+'</div>';
    $('ModalAlerteMoi').setStyle({ top: modTop, left: modLeft, width: modWidth, visibility: 'visible', border: '2px solid #4e4d4d', cursor: 'hand' });
    $('ModalAlerteMoi').appear();

    new Ajax.Request('/Services/WebService.asmx/IsLog', {
        method: 'post',
        contentType: 'application/json; charset=utf-8',
        onSuccess: AfficheAlerte,
        onFailure: FailureAlerte
    });
}

function AfficheAlerte(isLog) {
    if (isLog.responseJSON.d) {

        new Ajax.Request('/Services/WebService.asmx/GetClientEmail', {
            method: 'post',
            contentType: 'application/json; charset=utf-8',
            onSuccess: AfficheEmailAlerte,
            onFailure: FailureAlerte
        });

        document.getElementById('emailAlerte').disabled = 'true';
        document.getElementById('nomAlerte').style.display = 'none';
        document.getElementById('prenomAlerte').style.display = 'none';
        document.getElementById('libelleNom').style.display = 'none';
        document.getElementById('libellePrenom').style.display = 'none';
    }
}

function AfficheEmailAlerte(transport) {
    var email = transport.responseJSON.d;
    if (email != "")
        document.getElementById('emailAlerte').value = email;
}

function AnnulerAlerte() {
    HideModalAlerte();
}

function FailureAlerte(isLog) {
}

function HideModalAlerte() {
    if (currentModalAutoCloser != null) {
        window.clearTimeout(currentModalAutoCloser);
        currentModalAutoCloser = null;
    }
    var frmmod = $('divModal');
    Element.setStyle(frmmod, { display: 'none', visibility: 'hidden' });
    $('ModalAlerteMoi').fade();
    Form.enable('aspnetForm');
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'visible' });
    });
}


function ShowModalErreur() {
    Form.disable('aspnetForm');
    //    var frmmod = $('divModalErreur');
    var heightVP = document.viewport.getHeight();
    var widthVP = document.viewport.getWidth();
    var sOff = document.viewport.getScrollOffsets();

    //var DivAllH= heightVP + sOff.top;
    var DivAllH = 1;
    var DivAllV = widthVP + sOff.left;


    if (window.innerHeight && window.scrollMaxY) { // Firefox 
        DivAllH = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
        DivAllH = document.body.scrollHeight;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
        DivAllH = document.body.offsetHeight + document.body.offsetTop;
    }


    var widthBody = Element.getWidth($(document.body));
    if (widthVP < widthBody) DivAllV = widthBody;


    //  Element.setStyle(frmmod,{display:'block',visibility:'visible',height:DivAllH +'px',width:DivAllV +'px',top:0,left:0});
    //  Element.setOpacity(frmmod,0.5);
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'hidden' });
    });
    modTop = document.viewport.getScrollOffsets().top + 350 + 'px';
    modLeft = 425 + 'px';
    modWidth = 362 + 'px';
    //$('ModalBox').innerHTML= '<div onmousedown=\'HideModal()\' ><br /><br />' + '<p class="txtcontenu">'+'Un exmple de contenu</p>'+'</div>';
    $('ModalBoxErreur').setStyle({ top: modTop, left: modLeft, width: modWidth, visibility: 'visible', border: '2px solid #4e4d4d', cursor: 'hand' });
    $('ModalBoxErreur').appear();
    //frmmod.focus();

}

function EnvoyerAmi(Url) {
    document.getElementById('modalEnvoyerAmi').visibility = 'visible';
    ShowModalEnvoiAmi();
}

function ShowModalEnvoiAmi() {
    Form.disable('aspnetForm');
    Form.enable($('modalEnvoyerAmi'));
    //    var frmmod = $('modalEnvoyerAmi');
    var heightVP = document.viewport.getHeight();
    var widthVP = document.viewport.getWidth();
    var sOff = document.viewport.getScrollOffsets();

    //var DivAllH= heightVP + sOff.top;
    var DivAllH = 1;
    var DivAllV = widthVP + sOff.left;


    if (window.innerHeight && window.scrollMaxY) { // Firefox 
        DivAllH = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
    {
        DivAllH = document.body.scrollHeight;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
        DivAllH = document.body.offsetHeight + document.body.offsetTop;
    }


    var widthBody = Element.getWidth($(document.body));
    if (widthVP < widthBody) DivAllV = widthBody;

    //    Element.setStyle(frmmod,{display:'block',visibility:'visible',height:DivAllH +'px',width:DivAllV +'px',top:0,left:0});
    //    Element.setOpacity(frmmod,0.5);
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'hidden' });
    });
    modTop = document.viewport.getScrollOffsets().top + 350 + 'px';
    modLeft = 425 + 'px';
    modWidth = 500 + 'px';
    $('modalEnvoyerAmi').setStyle({ top: modTop, left: modLeft, width: modWidth, display: 'block', visibility: 'visible', border: '1px solid #750724', cursor: 'hand' });
    $('modalEnvoyerAmi').appear();
    //frmmod.focus();

}

function HideModal() {
    if (currentModalAutoCloser != null) {
        window.clearTimeout(currentModalAutoCloser);
        currentModalAutoCloser = null;
    }
    var frmmod = $('divModal');
    Element.setStyle(frmmod, { display: 'none', visibility: 'hidden' });
    //    $('ModalBox').setStyle({display:'none',visibility:'hidden'});
    $('ModalBox').fade();
    Form.enable('aspnetForm');
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'visible' });
    });
}

function HideModalErreur() {
    if (currentModalAutoCloser != null) {
        window.clearTimeout(currentModalAutoCloser);
        currentModalAutoCloser = null;
    }
    var frmmod = $('divModal');
    Element.setStyle(frmmod, { display: 'none', visibility: 'hidden' });

    // $('ModalBox').setStyle({display:'none',visibility:'hidden'});
    $('ModalBoxErreur').fade();
    Form.enable('aspnetForm');
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'visible' });
    });
}

function HideModalEnvoiAmi() {
    if (currentModalAutoCloser != null) {
        window.clearTimeout(currentModalAutoCloser);
        currentModalAutoCloser = null;
    }
    var frmmod = $('divModal');
    Element.setStyle(frmmod, { display: 'none', visibility: 'hidden' });
    $('modalEnvoyerAmi').setStyle({ display: 'none', visibility: 'hidden' });
    Form.enable('aspnetForm');
    $A($('aspnetForm').getElementsByTagName('select')).collect(function(aSelect) {
        aSelect.setStyle({ visibility: 'visible' });
    });
}

function AskRemote(url, onSuccessMethod, onFailureMethod) {
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            var response = transport.responseText || "no response text";
            onSuccessMethod(response);
        },
        onFailure: function(errInfo) {
            onFailureMethod(errInfo);
        }
    });
}

function AddBasket4Comp(pConditionnementID, pQuantity, ProductId, FamiId, bCross) {
    document.location.href = "/pc_basketshow.aspx";
}

function AddBasket4Comp(pConditionnementID, pQuantity, ProductId, FamiId) {
    AskRemote("/basketAdd.aspx?pConditionnementID=" + pConditionnementID + "&pQtt=" + pQuantity + "&ProductId=" + ProductId + "&FamiId=" + FamiId, OkAdd, FailedAdd);
}

function AddBasket(pConditionnementID, pQuantity) {
    AskRemote("/basketAdd.aspx?pConditionnementID=" + pConditionnementID + "&pQtt=" + pQuantity, OkAdd, FailedAdd);
}

function AlerteMoi(refpick, payscom) {
    document.getElementById('payscom').value = payscom;
    document.getElementById('refpick').value = refpick;
    ShowModalAlerte();
}

function AddDolistAlerte() {
    new Ajax.Request('/Services/WebService.asmx/IsLog', {
        method: 'post',
        contentType: 'application/json; charset=utf-8',
        onSuccess: SendDoList,
        onFailure: FailureAlerte
    });
}

function SendDoList(isLog) {
    var payscom = '';
    var refpick = '';
    var email = '';
    var nom = '';
    var prenom = '';
    var sms = '';
    if (!isLog.responseJSON.d) {
        payscom = document.getElementById('payscom').value;
        refpick = document.getElementById('refpick').value;
        email = document.getElementById('emailAlerte').value;
        nom = document.getElementById('nomAlerte').value;
        prenom = document.getElementById('prenomAlerte').value;
        sms = document.getElementById('smsAlerte').value;
        if (SuccessTest()) {
            var MonTableau = [email, nom, prenom, refpick, payscom, sms];
            new Ajax.Request('/Services/WebService.asmx/AddAlerteDoList', {
                method: 'post',
                contentType: 'application/json; charset=utf-8',
                postBody: '{DoListInfos: ' + Object.toJSON(MonTableau) + '}',
                onSuccess: HideModalAlerte,
                onFailure: FailureTestInfos
            });
        }
        else {
            new Ajax.Request('/Services/WebService.asmx/GetTraductionErreurCompteAdresse', {
                method: 'post',
                contentType: 'application/json; charset=utf-8',
                onSuccess: ProcessErreurAlerte,
                onFailure: FailureTestInfos
            });
        }
    }
    else {
        sms = document.getElementById('smsAlerte').value;
        payscom = document.getElementById('payscom').value;
        refpick = document.getElementById('refpick').value;
        var MonTableau = [email, nom, prenom, refpick, payscom, sms];
        new Ajax.Request('/Services/WebService.asmx/AddAlerteDoList', {
            method: 'post',
            contentType: 'application/json; charset=utf-8',
            postBody: '{DoListInfos: ' + Object.toJSON(MonTableau) + '}',
            onSuccess: HideModalAlerte,
            onFailure: FailureTestInfos
        });
    }
}


function OkAdd(returnvalue) {
    if (returnvalue.indexOf("#") != -1) {
        var tableau = returnvalue.split("#");
        if (tableau[1] == "error") {
            ProcessAuthenticated();
            //CollapsiblePanel1.show();
            var message = tableau[2];
            //var message = returnvalue;
            //$('ModalBox').innerHTML= '<div onmousedown=\'HideModal()\' ><br /><br />' + '<p class="txtcontenu">'+ message + fermer + '</p>' +'</div>';
            $('ModalBoxErreurMsg').innerHTML = message;
            ShowModalErreur();
            currentModalAutoCloser = setTimeout(f2, 12000);
        }
        else {
            ProcessAuthenticated();
            //CollapsiblePanel1.show();
            var message = returnvalue.substring(0, returnvalue.indexOf("#"));
            //var message = returnvalue;
            //$('ModalBox').innerHTML= '<div onmousedown=\'HideModal()\' ><br /><br />' + '<p class="txtcontenu">'+ message + fermer + '</p>' +'</div>';
            $('ModalBoxMsg').innerHTML = message;
            ShowModal();
            currentModalAutoCloser = setTimeout(f1, 12000);
        }
    }
}


var currentModalAutoCloser;
function FailedAdd(errInfo) {
    alert("etat:" + errInfo.status);
}

function f1() {
    HideModal();

}
function f2() {
    HideModalErreur();

}

function ProcessErreurAlerte(transport) {
    var result = transport.responseJSON.d;

    select = document.getElementById("emailAlerte");
    if (select.value == "") {
        setClass(select, 'modalsaisieemailErreur');
        setClass(document.getElementById("libelleEmail"), 'modaltitresaisieemailErreur');
        test = false;
    }
    else {
        setClass(select, 'modalsaisieemail');
        setClass(document.getElementById("libelleEmail"), 'modaltitresaisieemail');
    }

    if (!test) {
        document.getElementById('InfosNonValide').innerText = result.SaisieVide;
        document.getElementById('InfosNonValide').textContent = result.SaisieVide;
        //document.getElementById('InfosNonValide').style.display = 'block';
        document.getElementById('InfosNonValide').style.color = '#b80c37';

    }
}

function SuccessTest() {
    var testFormDoList = true;

    select = document.getElementById("emailAlerte");
    if (select.value == "") {
        testFormDoList = false;
    }
    return testFormDoList;
}


function FailureTestInfos() {
    alert('erreur');
}

function AddDolistNewletterInsc() {
    if (VerifDoListInscr) {
        email = document.getElementById('email').value;
        payscom = document.getElementById('payscom').value;
        var MonTableau = [email, payscom];
        new Ajax.Request('/Services/WebService.asmx/InscriptionNewsLetter', {
            method: 'post',
            contentType: 'application/json; charset=utf-8',
            postBody: '{DoListInfos: ' + Object.toJSON(MonTableau) + '}',
            onSuccess: ValidationIncriptionNewsletter,
            onFailure: FailureAlerte
        });
    }
    else {
        alert('erreur');
    }
}

function ValidationIncriptionNewsletter() {
    document.location.href = "http://www.millesima.fr/Statics/validNLinscription.aspx";
}

function VerifDoListInscr() {
    if (!ControlChamp(document.getElementById('email'), 'email')) {
        return false;
    }
    else {
        return true;
    }
}

function ControlChamp(obj, typ) {
    switch (typ) {
        case 'email':
            var Regex = new RegExp('^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$');
            break;
        case 'date':
            var Regex = new RegExp('^(([1-9]|0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([0-9][0-9]|[0-9][0-9][0-9][0-9]))?$');
            break;
    }
    var Result = Regex.test(obj.value);
    return Result;
}