var sending = false;

function sendMail (mode)
{
    var suffix = '';
    
    var popup = EL ('envoyerAmiProductSheet');

    if (popup != null && popup.style.display == 'block')
    {
        suffix = "-fp";
    }
    var nom    = EL ('nom-prenom' + suffix);
    var email  = EL ('email' + suffix);
    
    if (!sending && check (nom.value, email.value, suffix))
    {
        var texte     = EL ('texteComp' + suffix);
        var article1  = EL ('article_1');
        var quantity1 = EL ('quantity_1');
        var article2  = EL ('article_2');
        var quantity2 = EL ('quantity_2');
        
        sending = true;
        
        if (mode == 'proposition')
        {
            var checkedIds = getCheckedIds();
            
            sendPropositionMail (nom.value, email.value, texte.value, article1.value, quantity1.value, article2.value, quantity2.value, mode, checkedIds);
        }
        else
        {
            sendBasketAndProductSheetMail (nom.value, email.value, texte.value, article1.value, quantity1.value, article2.value, quantity2.value, mode);
        }
    }
}


function check (nom, email, suffix)
{
    var errorDiv        = null;
    var errorName       = EL ('message-error-name' + suffix);
    var errorEmail      = EL ('message-error-email' + suffix);
    var errorValidEmail = EL ('message-error-valid-email' + suffix);
    
    errorName.style.display       = "none";
    errorEmail.style.display      = "none";
    errorValidEmail.style.display = "none";
    
    if (nom == '')
    {
        errorDiv = errorName;
    }
    else if (email == '')
    {
        errorDiv = errorEmail;
    }
    else if (!Valraiso.util.checkEmail (email))
    {
        errorDiv = errorValidEmail;
    }
    
    if (errorDiv != null)
    {
        errorDiv.style.display = "block";
        
        return false;
    }
    
    return true;
}


function sendBasketAndProductSheetMail (nom, email, texte, article1, quantity1, article2, quantity2, mode)
{
    var query = Bw.IO.Query.create();
    var url   = "/SendMailAjax?article1=";
        url  += article1;
        url  += "&quantity1=";
        url  += quantity1;
        url  += "&article2=";
        url  += article2;
        url  += "&quantity2=";
        url  += quantity2;
        url  += "&nom=";
        url  += Url.encode (nom);
        url  += "&email=";
        url  += email;
        url  += "&mode=";
        url  += mode;
        url  += "&texte=";
        
        if (texte != null && texte != '')
        {
            url += Url.encode (texte);
        }
    
    query.setNoCache();
    query.get (url, function () { mailSent (query, mode); });
}


function sendPropositionMail (nom, email, texte, article1, quantity1, article2, quantity2, mode, checkedIds)
{
    var query = Bw.IO.Query.create();
    var url   = "/SendMailAjax?article1=";
        url  += article1;
        url  += "&quantity1=";
        url  += quantity1;
        url  += "&article2=";
        url  += article2;
        url  += "&quantity2=";
        url  += quantity2;
        url  += "&nom=";
        url  += Url.encode (nom);
        url  += "&email=";
        url  += email;
        url  += "&mode=";
        url  += mode;
        url  += "&checkedIds=";
        url  += checkedIds;
        url  += "&texte=";
        
        if (texte != null && texte != '')
        {
            url += Url.encode (texte);
        }
    
    query.setNoCache();
    
    query.get (url, function () { mailSent (query); });
}


function showProductSheetPopUp()
{
    var popup = EL ('envoyerAmiProductSheet');
    var pos   = getDocumentScroll();

    popup.style.display = 'block';
    popup.style.top     = (pos.y + 100) + 'px';
    popup.style.left    = '-100px';

    toggleProductSheetCurtain();

    return false;
}


function masqueEnvoyerAmi()
{
    if (!sending)
    {
        var popup2 = EL ('envoyerAmiProductSheet');

        if (popup2 != null && popup2.style.display == 'block')
        {
            masquePopUp ('envoyerAmiProductSheet');
        }
        else
        {
            masquePopUp ('envoyerAmi');
        }
        
        toggleCurtain();
    }
}


function masqueEnvoyerAmiFP()
{
    if (!sending)
    {
        var popup2 = EL ('envoyerAmiProductSheet');

        if (popup2 != null && popup2.style.display == 'block')
        {
            masquePopUp ('envoyerAmiProductSheet');
        }
        else
        {
            masquePopUp ('envoyerAmi');
        }
        
        toggleProductSheetCurtain();
        showProductSheetSelect();
    }
}


function mailSent (query, mode)
{
    var popup;
    var suffix = '';
    var texte  = query.getText();
    var popup2 = EL ('envoyerAmiProductSheet');

    if (popup2 != null && popup2.style.display == 'block')
    {
        masquePopUp ('envoyerAmiProductSheet');
        
        suffix = '-fp';
    }
    else
    {
        masquePopUp ('envoyerAmi');
    }
    
    if (texte.indexOf ('error') != -1)
    {
        affichePopUp ('mailError' + suffix);
        
        popup = EL ('mailError' + suffix);
    }
    else
    {
        var nom       = EL ('nom-prenom' + suffix);
        var email     = EL ('email' + suffix);
        var texteComp = EL ('texteComp' + suffix);
        
        nom.value       = '';
        email.value     = '';
        texteComp.value = '';
        
        affichePopUp ('mailEnvoye' + suffix);
        
        popup = EL ('mailEnvoye' + suffix);
        
        var extraText = EL ('extraText');
        
        extraText.innerHTML = texte;
    }
    
    sending = false;
    
    toggleCurtain();
    
    if (mode == 'productSheet')
    {
        popup.style.left = '-100px';
    }
}


function URLEncode (clearString)
{
    var regex  = /(^[a-zA-Z0-9_.]*)/;
    var output = '';
    var x      = 0;

    clearString = clearString.toString();

    while (x < clearString.length)
    {
        var match = regex.exec (clearString.substr (x));

        if (match != null && match.length > 1 && match[1] != '')
        {
            output += match[1];
            x      += match[1].length;
        }
        else
        {
            if (clearString[x] == ' ')
            {
                output += '+';
            }
            else
            {
                var charCode = clearString.charCodeAt (x);
                var hexVal   = charCode.toString (16);

                output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
            }

            x++;
        }
    }
    
    return output;
}


var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


function protectQuotesForTranslations (translation)
{
    translation.replace ("'", "\'");
}


var productSheetCurtainVisible = false;


function showProductSheetCurtain()
{
    var curtain = document.getElementById ("masqueFicheProduit");

    if (curtain == null)
    {
        var fp       = EL ('fp');
        var fpHeight = fp.offsetHeight;
        var fpWidth  = fp.width;

        if (typeof fpWidth == 'undefined')
        {
            fpWidth = fp.offsetWidth;
        }

        curtain    = document.createElement ("div");
        curtain.id = "masqueFicheProduit";

        with (curtain.style)
        {
            width           = fpWidth + "px";
            height          = fpHeight + "px";
            display         = "none";
            backgroundColor = "black";
            position        = "absolute";
            top             = 0;
            left            = 0;
            opacity         = 0.7;
            MozOpacity      = 0.7;
            filter          = "alpha(opacity=70)";
            zIndex          = 12;
        }

        fp.appendChild (curtain);
    }

    productSheetCurtainVisible = true;
    curtain.style.display = "block";
    Valraiso.CSS.events.hideSelect();
}


function hideProductSheetCurtain()
{
    var curtain = document.getElementById ("masqueFicheProduit");
    
    if (curtain != null)
    {
        curtain.style.display = "none";
        productSheetCurtainVisible = false;
    }
}


function toggleProductSheetCurtain()
{
    if (productSheetCurtainVisible) hideProductSheetCurtain();
    else showProductSheetCurtain();
}


function showProductSheetSelect()
{
    var qte1 = EL ('_quantity1');
    var qte2 = EL ('_quantity2');
    
    qte1.style.visibility = 'visible';
    
    if (qte2 != null)
    {
        qte2.style.visibility = 'visible';
    }
}
