﻿var e = []; function ce(id) { if (!e[id]) { e[id] = $(id); } return e[id]; }

e['document'] = $(document);
e['document'].ready(function() { app.init(); });


var app = {

    module: 'form-brochure',
    oForm: null,
    btnSubmit: null,
    isChecked: true,
    isBack: false,
    bgColorError: '#FFF0F0',
    txtColorError: '#CE0000',
    cookieUser: null,
    cookieDomain: 'acanthe.ch',
    cookieName: 'acantheUserV3',

    init: function() {

        if ($('#myForm').length > 0) {
            app.oForm = $('#myForm').get();
            $('#btnSend').bind('click', app.send).enabled(true);
            if (typeof Cookie !== 'undefined') {
                app.cookieUser = new Cookie(app.cookieName, app.cookieDomain);
                if (!app.isBack) { app.initWithCookie(); }
            }
            if (app.isBack) { app.isChecked = app.check(); }
        }
    },

    closeAndCancel: function() {
        app.saveCookie();
        window.parent.hs.close();
    },

    close: function() {
        window.parent.hs.close();
    },

    initWithCookie: function() {
        var cu = app.cookieUser;
        if (cu.bValidCookie) {
            var isEx = false;
            if (cu.getValue("co") != '') { isEx = true; $('#Company').val(cu.getValue("co")); }
            if (cu.getValue("ge") != '') { isEx = true; $('#Gender').val(cu.getValue("ge")); }
            if (cu.getValue("ln") != '') { isEx = true; $('#Lastname').val(cu.getValue("ln")); }
            if (cu.getValue("fn") != '') { isEx = true; $('#Firstname').val(cu.getValue("fn")); }
            if (cu.getValue("ma") != '') { isEx = true; $('#Email').val(cu.getValue("ma")); }
            if (cu.getValue("s1") != '') { isEx = true; $('#Street1').val(cu.getValue("s1")); }
            if (cu.getValue("s2") != '') { isEx = true; $('#Street2').val(cu.getValue("s2")); }
            if (cu.getValue("zi") != '') { isEx = true; $('#Zipcode').val(cu.getValue("zi")); }
            if (cu.getValue("ci") != '') { isEx = true; $('#City').val(cu.getValue("ci")); }
            if (cu.getValue("cn") != '') { isEx = true; $('#Country').val(cu.getValue("cn")); }
            if (isEx) { $('#emptyForm').show(); }
        }

    },

    clearForm: function() {
        $('#Company').val('');
        $('#Gender').val('');
        $('#Lastname').val('');
        $('#Firstname').val('');
        $('#Email').val('');
        $('#Street1').val('');
        $('#Street2').val('');
        $('#Zipcode').val('');
        $('#City').val('');
        $('#Country').val('CH');
    },

    saveCookie: function() {
        var cu = app.cookieUser;
        if ((cu.bValidCookie) && $('#rememberMe').cbchecked()) {
            cu.setValue("co", $('#Company').val());
            cu.setValue("ge", $('#Gender').val());
            cu.setValue("ln", $('#Lastname').val());
            cu.setValue("fn", $('#Firstname').val());
            cu.setValue("ma", $('#Email').val());
            cu.setValue("s1", $('#Street1').val());
            cu.setValue("s2", $('#Street2').val());
            cu.setValue("zi", $('#Zipcode').val());
            cu.setValue("ci", $('#City').val());
            cu.setValue("cn", $('#Country').val());
        } else {
            cu.setValue("co", "");
            cu.setValue("ge", "");
            cu.setValue("ln", "");
            cu.setValue("fn", "");
            cu.setValue("ma", "");
            cu.setValue("s1", "");
            cu.setValue("s2", "");
            cu.setValue("zi", "");
            cu.setValue("ci", "");
            cu.setValue("cn", "");
        }
    },

    send: function() {
        var isValidated = app.check();
        if (isValidated === true) {
            app.saveCookie();
            $('#btnSend').enabled(false);
            $('#myForm').submit();
        } else {
            return false;
        }
    },

    check: function() {
        app.isChecked = true; app.checkRequired('Country'); app.checkRequired('City'); app.checkRequired('Zipcode'); app.checkRequired('Street1'); if (app.checkEMail($('#Email').val())) { app.showError(true, 'Email', 'Email'); } else { app.showError(false, 'Email', 'Email'); }; app.checkRequired('Firstname'); app.checkRequired('Lastname'); app.checkRequired('Company');
        return app.isChecked;
    },

    checkRequired: function(name, captionName) {
        if (typeof captionName === 'undefined') { captionName = name };
        if ($('#' + name).val().length < 1) { app.showError(false, name, captionName); } else { app.showError(true, name, captionName); };
    },

    showError: function(bool, name, captionName) {
        if (bool) {
            $('#' + captionName).css('background', '#ffffff'); $('#' + captionName + '_caption').css('color', '').css('font-weight', 'normal');
        } else { $('#' + captionName).css('background', app.bgColorError); $('#' + captionName + '_caption').css('color', '#ce0000').css('font-weight', 'bold'); app.isChecked = false; };
    },

    checkEMail: function(strEmail) { var RegExpr = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/; return RegExpr.test(strEmail); }


};


