﻿/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2011-09-05 */

/* requires: dgTools.js */

FormJoinMgr = {

    visible: false,
    requestUrl: '/ajax_FormJoin.aspx',
    Objs: [],
    EmailRegex: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z0-9]{1,}$/i,
    Validators: [],

    show: function(prodId) {
        if (this.visible) return;

        if (this._screenMask == null) {
            this._screenMask = document.createElement('div');
            this._screenMask.style.position = window.ie6 ? 'absolute' : 'fixed';
            this._screenMask.style.left = '0px';
            this._screenMask.style.top = '0px';
            this._screenMask.style.width = '100%';
            this._screenMask.style.height = (window.ie6 ? window.getClientHeight() + 'px' : '100%');
            this._screenMask.style.zIndex = 500;
            this._screenMask.style.background = '#000';
            dgTools.Elm.setOpacity(this._screenMask, 0.5);
            dgTools.observe(this._screenMask, 'click', function() { FormJoinMgr.hide(); }, false);
            document.body.appendChild(this._screenMask);
        }

        if (!this.frmObj) {

            this.BuildForm();
            dgTools.observe(this.Objs['Frame'], 'click', function() { return false; }, false);
            var thisFrmObj = this.frmObj;
            dgTools.observe(this.frmObj, 'click', function(event) { if (event.srcElement == thisFrmObj) FormJoinMgr.hide(); }, false);

            this.frmObj.style.border = 'none';
            this.frmObj.style.position = window.ie6 ? 'absolute' : 'fixed';
            this.frmObj.style.zIndex = 600;
            document.body.appendChild(this.frmObj);
        }

        if (!this.analyticsFrame) {
            this.analyticsFrame = dgTools.Elm.build('iframe', ['src', '/ajax_FormJoinAnalytics.aspx', 'style', 'position:absolute;top:-10000px;z-index:-1;display:none;visibility:hidden;margin:0;padding:0;width:0px;height:0px;border:none;']);
            document.body.appendChild(this.analyticsFrame);
        }

        dgTools.Elm.hideSelectsIe6(true);

        this.frmObj.style.display = '';
        if (window.ie6) this._screenMask.style.height = window.getClientHeight();
        this._screenMask.style.display = '';
        this.visible = true;
    },
    BuildForm: function() {
        if (this.frmObj) return;

        var ce = dgTools.Elm.build;
        var thisObj = this;
        var cf = function(type, id, text, cls, req, cmp, rgx) {
            var e = ce('label', ['for', 'FormJoin_' + id, 'className', cls || '', 'innerHTML', text]);
            e.appendChild(thisObj.Objs['val$' + id] = ce('span'));
            e.appendChild(thisObj.Objs[id] = ce('input', ['type', type, 'id', 'FormJoin_' + id, 'onblur', 'FormJoinMgr.validate()']));
            if (req || (cmp = cmp == '' ? null : cmp) || rgx) {
                thisObj.Validators.push({ id: id, req: req, cmp: cmp, rgx: rgx });
            }
            return e;
        };

        this.frmObj = ce('div', ['className', 'JoinFormFC']);
        var d, f;
        this.frmObj.appendChild(d = this.Objs['Frame'] = ce('div', ['className', 'Frame']));
        d.appendChild(ce('div', ['className', 'FrameT']));
        d.appendChild(d = ce('div', ['className', 'FrameBody']));
        d.appendChild(ce('div', ['className', 'Title', 'innerHTML', 'טופס פתיחת עסק']));
        d.appendChild(this.Objs['Error'] = ce('div', ['className', 'Error', 'style', 'display:none;']));
        d.appendChild(this.Objs['Sending'] = ce('div', ['className', 'Sending', 'style', 'display:none;',
            'innerHTML', '<img src="images/main/formJoin_progress.gif" width="16" height="16"><span>שולח טופס...</span>']));
        d.appendChild(this.Objs['Success'] = ce('div', ['className', 'Success', 'style', 'display:none;']));
        d.appendChild(d = ce('form', ['onsubmit', 'return FormJoinMgr.send();']));
        d.appendChild(f = ce('div', ['className', 'TextFields']));

        f.appendChild(cf('text', 'BusinessName', 'שם העסק / חברה / חנות:', '', true, null, null));
        f.appendChild(cf('text', 'Phone', 'טלפון:', '', true, null, null));
        f.appendChild(cf('text', 'ContactEmail', 'אימייל איש קשר:', '', true, null, this.EmailRegex));
        f.appendChild(cf('text', 'Address', 'כתובת, רחוב ומספר:', '', true, null, null));
        f.appendChild(cf('text', 'Fax', 'פקס:', '', true, null, null));
        f.appendChild(cf('text', 'UserName', 'שם משתמש:', '', true, null, null));
        f.appendChild(cf('text', 'City', 'עיר:', '', true, null, null));
        f.appendChild(cf('text', 'ContactName', 'שם איש קשר:', '', true, null, null));
        f.appendChild(cf('text', 'Email', 'אימייל:', '', true, null, this.EmailRegex));
        f.appendChild(cf('text', 'PostalCode', 'מיקוד:', '', true, null, null));
        f.appendChild(cf('text', 'ContactPhone', 'טלפון איש קשר:', '', true, null, null));
        f.appendChild(cf('password', 'Password', 'סיסמא:', '', true, null, null));
        f.appendChild(cf('password', 'Password2', 'סיסמא (חזור שנית):', 'MostLeft', true, 'Password', null));
        f.appendChild(cf('text', 'ShortDesc', 'תיאור העסק (קצר):', 'Long', true, null, null));

        f.appendChild(ce('div', ['className', 'CLEAR']));

        d.appendChild(ce('a', ['className', 'Submit', 'href', 'javascript:', 'onclick', 'return FormJoinMgr.send();', 'innerHTML', 'שלח את הטופס!']));

        this.Objs['Frame'].appendChild(ce('div', ['className', 'FrameB']));

        return false;
    },
    hide: function() {
        if (!this.visible) return;
        this.frmObj.style.display = 'none';
        this._screenMask.style.display = 'none';
        this.visible = false;
        dgTools.Elm.hideSelectsIe6(false);
        return false;
    },
    setSending: function() { this.Objs['Sending'].style.display = ''; this.Objs['Error'].style.display = 'none'; this.Objs['Success'].style.display = 'none'; },
    setSuccess: function(txt) { this.Objs['Sending'].style.display = 'none'; this.Objs['Error'].style.display = 'none'; this.Objs['Success'].style.display = ''; dgTools.Elm.setText(this.Objs['Success'], txt); },
    setError: function(txt) { this.Objs['Sending'].style.display = 'none'; this.Objs['Error'].style.display = ''; this.Objs['Success'].style.display = 'none'; dgTools.Elm.setText(this.Objs['Error'], txt); },
    validate: function() {
        var errors = false, error, val;
        for (var j = 0; j < this.Validators.length; j++) {
            error = false;
            val = this.Validators[j];
            if (this.Objs[val.id].type != 'password') this.Objs[val.id].value = this.Objs[val.id].value.trim();
            if (val.req && this.Objs[val.id].value.length == 0) { error = true; }
            if (val.cmp && this.Objs[val.id].value != this.Objs[val.cmp].value) { error = true; }
            if (val.rgx && !this.Objs[val.id].value.match(val.rgx)) { error = true; }
            if (error) this.Objs['val$' + val.id].innerHTML = '*';
            else this.Objs['val$' + val.id].innerHTML = '';
            errors |= error;
        }
        return !errors;
    },
    send: function() {
        if (this.sending) return false;
        if (!this.validate()) return false;

        this.sending = true;
        // Actual send
        this.setSending();

        var requestUrl = this.requestUrl;
        var idx = requestUrl.indexOf('?');
        if (idx > -1) requestUrl += '&sending=true';
        else requestUrl += '?sending=true';
        requestUrl += '&BusinessName=' + encodeURIComponent(this.Objs['BusinessName'].value);
        requestUrl += '&Phone=' + encodeURIComponent(this.Objs['Phone'].value);
        requestUrl += '&ContactEmail=' + encodeURIComponent(this.Objs['ContactEmail'].value);
        requestUrl += '&Address=' + encodeURIComponent(this.Objs['Address'].value);
        requestUrl += '&Fax=' + encodeURIComponent(this.Objs['Fax'].value);
        requestUrl += '&UserName=' + encodeURIComponent(this.Objs['UserName'].value);
        requestUrl += '&City=' + encodeURIComponent(this.Objs['City'].value);
        requestUrl += '&ContactName=' + encodeURIComponent(this.Objs['ContactName'].value);
        requestUrl += '&Email=' + encodeURIComponent(this.Objs['Email'].value);
        requestUrl += '&PostalCode=' + encodeURIComponent(this.Objs['PostalCode'].value);
        requestUrl += '&ContactPhone=' + encodeURIComponent(this.Objs['ContactPhone'].value);
        requestUrl += '&ShortDesc=' + encodeURIComponent(this.Objs['ShortDesc'].value);
        requestUrl += '&Password=' + encodeURIComponent(this.Objs['Password'].value);
        var thisObj = this;
        dgTools.ajax.loadUrlText(requestUrl, function(res) {
            thisObj.sending = false;
            if (res.substr(0, 2) == 'OK') {
                thisObj.setSuccess(res.substr(3));
            }
            else {
                if (res.substr(0, 5) == 'ERROR') thisObj.setError(res.substr(6));
                else thisObj.setError('אירעה שגיאה - הטופס לא נשלח.');
            }
        });

        return false;
    },
    resetForm: function() {
        this.Objs['Sending'].style.display = 'none';
        this.Objs['Success'].style.display = 'none';
        this.Objs['Error'].style.display = 'none';
        return false;
    }
}
