try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }
Ext.namespace('Yft.EmailForm');
Yft.EmailForm = function(config) {this.init()}
CURRENT_PROTO = Yft.EmailForm.prototype;

CURRENT_PROTO.init=function(parent_id) {
    this.elements   = this.get_elements(parent_id || 'form-container');
    var submit_form = Ext.get('submit-form');
    if(submit_form)
        submit_form.on('click', this.form_send, this);
}

CURRENT_PROTO.form_send=function() {
    var values = this.check_elements();
    if(!values) 
        return;
    this.show_sending();
    values.site = 'yellowfish';
    console.log(values);
    Ext.Ajax.request({
        url: '/contacts',
        params: values,
        success: function(r) {
            r=this.ajax_parse_response(r);
            this.show_submit_btn();
            if(r.success) {
                alert('Sent Successfully');
                this.clear_elements();
            }
        },
        failure:function(r) {
            this.show_submit_btn();
            alert('Failed to Send. Please contact us at 1 888 817 2969');
        },
        scope: this
    });
}

CURRENT_PROTO.clear_elements=function() {
    for(var i=0; i < this.elements.length; i++) {
        var e = this.elements[i];
        e.dom.value='';
    }
}

CURRENT_PROTO.check_elements=function() {
    var values = {};
    for(var i=0; i < this.elements.length; i++) {
        var e   = this.elements[i];
        var eid = e.id;
        var is_reqd = e.dom.getAttribute('reqd');
        var value = e.getValue() || '';
        if(!Ext.isEmpty(is_reqd)) {
            value = value.replace(/^\s+/,'').replace(/\s+$/,'');
            if(Ext.isEmpty(value)) {
                alert(is_reqd);
                return false;
            }                 
        }
        values[eid] = value;
    }
    
    return values;
}

CURRENT_PROTO.show_sending=function() {
    var img = Ext.get('submit-form');
    var src = img.getAttribute('src');
    this.orig_submit_img = img.dom.getAttribute('src');
    
    img.dom.setAttribute('src', '../images/submitting-btn.gif');
    img.un('click', this.form_send, this);
}

CURRENT_PROTO.show_submit_btn=function() {
    var img = Ext.get('submit-form');
    var src = img.getAttribute('src');
    
    img.dom.setAttribute('src', this.orig_submit_img);
    img.on('click', this.form_send, this);
}

CURRENT_PROTO.ajax_parse_response=function(resp) {
    if(resp._fw_decoded)
        return resp;
    var r = (resp.result != null) ? resp.result : Ext.decode(resp.responseText);
    r._fw_decoded = true;
    return r;
}

CURRENT_PROTO.get_elements=function(parent_id) {
    var parent = Ext.get(parent_id);
    var elements = [];
    if(parent) {
        Ext.each([parent.select('input').elements, parent.select('textarea').elements, parent.select('select').elements], function(collection) {
            Ext.each(collection, function(element) {
                elements.push(Ext.get(element));
            }, this);
        }, this);
    }
    return elements;
}
