


var shop = {

    shopname: 'bj-coffee',
    language: 'fr',
    defaultDisapearTimeout: 2000,
    isOnForgot: false,

    init: function() {
        if ($('.pcontentmsg').length > 0) {
            setInterval("shop.fadeoutMessage('slow')", shop.defaultDisapearTimeout);
            $(".pcontentmsg").bind("mouseover", function() { shop.fadeoutMessage('slow') });
        }
        $('#shopLink').click(shop.openProductList);
    },

    openProductList: function() {
        $('#shopLink').toggleClass('on');
        $("#productList").slideToggle("fast");

    },

    forgotInfo: function() {
        if (!(shop.isOnForgot)) {
            $('#login-box').hide();
            $('#forgot-box').show();
            $('#btnLogin').attr('src', '/images/shop/btn_submit_' + language + '.gif');
            $('#action').val('forgot-check');
        } else {
            $('#login-box').show();
            $('#forgot-box').hide();
            $('#btnLogin').attr('src', '/images/shop/btn_login_' + language + '.gif');
            $('#action').val('login-check');
        }
        shop.isOnForgot = !(shop.isOnForgot);
    },


    checkLoginBox: function() {

        var isOk = true;
        $('#btnLogin').attr('className', 'btnOff');
        $('#btnLogin').enabled(false);
        if ($('#action').val() === 'login-check') {
            var curPassword = $('#password').val();
            if (curPassword.length < 5) { isOk = false; $('#password').focus(); $('#password').attr('className', 'loginError'); } else { $('#password').attr('className', 'login'); }
            var curEmail = $('#email').val(); if (!(checkEMail(curEmail))) { isOk = false; $('#email').focus(); $('#email').attr('className', 'loginError'); } else { $('#email').attr('className', 'login'); }
        } else if ($('#action').val() === 'forgot-check') {
            var curEmail = $('#email_forgot').val(); if (!(checkEMail(curEmail))) { isOk = false; $('#email_forgot').focus(); $('#email_forgot').attr('className', 'loginError'); } else { $('#email_forgot').attr('className', 'login'); }
        } else {
            isOk = false;
        }

        if (!(isOk)) {
            $('#btnLogin').attr('className', 'btnOn');
            $('#btnLogin').enabled(true);
        }
        return isOk;
    },




    displayInvoicing: function() {
        if ($('#isinvoice').cbchecked()) {
            $('#newAccount3').show();
        } else {
            $('#newAccount3').hide();
        }
    },

    addCartSimple: function(pNo, pRef) {
        var spid = $('#pid-' + pNo).val();
        var sqty = $('#qty-' + pNo).val();
        if (pRef) {
            document.location.href = '?pid=' + spid + '&qty=' + sqty + '&action=addCart&ref=' + pRef;
        } else {
            document.location.href = '?pid=' + spid + '&qty=' + sqty + '&action=addCart';
        }
    },


    deleteItem: function(pNo) {
        var spid = $('#pid-' + pNo).val();
        document.location.href = '?pid=' + spid + '&action=deleteProduct';

    },

    editQty: function() {
        $('#action').val('updateQtyCart');
        $('#formCart').submit();
    },


    deleteCart: function() {
        document.location.href = '?action=deleteCart';
    },

    orderCart: function() {
        document.location.href = 'order-identification.asp';
    },

    fadeoutMessage: function(speed) {
        $(".pcontentmsg").fadeOut(speed);
    },

    checkCreateAccount: function() {
        var isOk = true;

        $('#btnSave').attr('className', 'btnOff');
        $('#btnSave').enabled(false);

        /* CHECKING FORM FROM BOTTOM TO TOP */
        /* 1. If invoicing address then check the City, place and street */
        if ($('#isinvoice').cbchecked()) {
            if ($('#icity').val().length < 2) { isOk = false; showError('icity'); $('#icity').focus() } else { hideError('icity'); }
            if ($('#izipcode').val().length < 4) { isOk = false; showError('izipcode'); $('#izipcode').focus() } else { hideError('izipcode'); }
            if ($('#istreet1').val().length < 5) { isOk = false; showError('istreet1'); $('#istreet1').focus() } else { hideError('istreet1'); }
            if ($('#icompany').val().length < 2) { isOk = false; showError('icompany'); $('#icompany').focus() } else { hideError('icompany'); }
        }
        /* 2. Delivery address then check the City, place and street */
        if ($('#dcity').val().length < 2) { isOk = false; showError('dcity'); $('#dcity').focus() } else { hideError('dcity'); }
        if ($('#dzipcode').val().length < 4) { isOk = false; showError('dzipcode'); $('#dzipcode').focus() } else { hideError('dzipcode'); }
        if ($('#dstreet1').val().length < 5) { isOk = false; showError('dstreet1'); $('#dstreet1').focus() } else { hideError('dstreet1'); }
        if ($('#dcompany').val().length < 2) { isOk = false; showError('dcompany'); $('#dcompany').focus() } else { hideError('dcompany'); }

        /* 3. User information */
        if ($('#phone').val().length < 10) { isOk = false; showError('phone'); $('#phone').focus() } else { hideError('phone') }
        if (checkPassword($('#password').val()) == false) {
            isOk = false;
            showError('password');
            $('#password').focus();
        } else {
            hideError('password');
            if ($('#passwordconfirm').val() !== $('#password').val()) {
                isOk = false;
                showError('passwordconfirm');
                $('#passwordconfirm').focus();
            } else {
                hideError('passwordconfirm');
            }
        }
        if ($('#username').val().length < 7 || !(checkEMail($('#username').val()))) { isOk = false; showError('username'); $('#username').focus() } else { hideError('username'); }
        if ($('#customernumber').val().length > 0 && $('#customernumber').val().length !== 6) {
            isOk = false;
            showError('customernumber');
            $('#customernumber').focus();

        } else {
            //hideError('customernumber');
        }

        if ($('#firstname').val().length < 2) { isOk = false; showError('firstname'); $('#firstname').focus() } else { hideError('firstname'); }
        if ($('#lastname').val().length < 2) { isOk = false; showError('lastname'); $('#lastname').focus() } else { hideError('lastname'); }

        if (!(isOk)) {
            $('#btnSave').attr('className', 'btnOn');
            $('#btnSave').enabled(true);

        }
        return isOk;

    }


};


$(document).ready(function() {
    shop.init();
    $(document).pngFix();
});





var returningurl = '';

function editDeliveryAddress(aid){
    document.location.href = '/' + language + '/clients/address-edit.asp?action=edit-address-delivery&addressid=' + aid + '&returningurl=' + returningurl  ;
}

function addDeliveryAddress(){
    document.location.href = '/' + language + '/clients/address-edit.asp?action=edit-address-delivery&addressid=0&returningurl=' + returningurl  ;
}

function editInvoicingAddress(aid){
    document.location.href = '/' + language + '/clients/address-edit.asp?action=edit-address-invoicing&addressid=' + aid + '&returningurl=' + returningurl  ;
}

function processOrder(){
    var isOk = false;
    if ($('#chkAccept').cbchecked()) {
        $('#btnProcess').attr('className', 'btnOff');
        $('#btnProcess').enabled(false);
        isOk = true;
    } else {
        alert('Vous devez accepter les conditions générales de ventes.')
    }
    return isOk;
}



