/*$Id: notifyme.js,v 1.2 2009-02-20 15:39:53 dbernabei Exp $*/
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Notify Me AJAX script
 * 
 * Copyright (c) 2006 ASPHERIO S.L.. All rights reserved.
 *
 * @package NotifyMe
 * @author [AB]
 */

dojo.require('dojo.validate.web');
dojo.require('dojo.io.*');

/**
 * NotifyMe handler singleton
 *
 * @requires NotifyMe_Data
 */
var notifyme = {
    /**
     * Field onFocus handler
     *
     * Usage:
     * <code>
     * onfocus='notifyme.handle_focus(this);'
     * </code>
     *
     * @param {object} field  the element node of the handled input field
     */
    handle_focus: function(field) {
        if (field.value == NotifyMe_Data.FieldEmail) {
            field.value = '';
        }
    },

    /**
     * Field onBlur handler
     *
     * Usage:
     * <code>
     * onblur='notifyme.handle_blur(this);'
     * </code>
     *
     * @param {object} field  the element node of the handled input field
     */
    handle_blur: function(field) {
        if (field.value != '') {
            if (!dojo.validate.isEmailAddress(field.value, false, false))
            {
                field.className = 'error';
                this._set_error(NotifyMe_Data.MessageErrorInvalidEmail);
            } else {
                if (field.className == 'error') {
                    field.className = '';
                    this._set_error(null);
                }
            }
            
            return;
        } else {
            field.value = NotifyMe_Data.FieldEmail;
        }
    },

    /**
     * onSubmit handler that intercepts the submission and sends the form over
     * AJAX
     *
     * Usage:
     * <code>
     * onsubmit='return notifyme.send(this);'
     * </code>
     *
     * @param  {object}  formnode  the element node of the notifyme form
     * @return {boolean}           an interception determiner
     */
    send: function(formnode) {
        if (!dojo.validate.isEmailAddress(formnode.email.value,
                                          false, false))
        {
            formnode.email.className = 'error';
            this._set_error(NotifyMe_Data.MessageErrorInvalidEmail);
            
            return false;
        }

        this._set_error(null);
        formnode.email.className = '';
        
        formnode.email.disabled  = true;
        formnode.submit.disabled = true;

        emailValue = formnode.email.value;

        formnode.email.value = '';
        formnode.email.className = 'spinner';

        dj_config = {
            url: NotifyMe_Data.AjaxHandler,
            mimetype: 'text/plain',
            error: function(t, err) {
                alert(err.message);
            },
            load: function(t, data, e) {
                notifyme._handle_response(formnode, data, emailValue);
            },
            content: {
                _no_test: 1, // this suppresses debug output
                SID:      formnode.SID.value,
                sku:      formnode.sku.value,
                email:    emailValue,
                submit:   1
            }
        }

        dojo.io.bind(dj_config);
        
        return false;
    },

    /**
     * AJAX response callback
     *
     * This method is called by dojo.io.bind() after a successfull
     * XmlHttpRequest request.
     *
     * @private
     */
    _handle_response: function(formnode, data, emailValue) {
        formnode.email.disabled  = false;
        formnode.submit.disabled = false;

        formnode.email.className = '';
        formnode.email.value = emailValue;

        if (data == 'noerror') {
            this._set_error('<span class="success">' +
                              NotifyMe_Data.MessageSuccess + '</span>');

            window.setTimeout(function() {notifyme._set_error(null);}, 10000);
        } else {
            this._set_error(data);
        }
    },

    /**
     * Displays a notifyme error message
     *
     * @param {String|null} errormsg  a string of the error message to
     *                                 display or null to clear the
     *                                 errormessage
     * @private
     */
    _set_error: function(errormsg) {
        if (errormsg) {
            document.getElementById('notifyme-entry-error').innerHTML =
                errormsg;
        } else {
            document.getElementById('notifyme-entry-error').innerHTML =
                '&nbsp;';
        }
    }
}
