/**
 * @author Falko Zander, www.falko-zander.de
 */

/**
 * @param DOM_id String
 * @return Object
 */
function cMail(DOM_id) {
    this.DOM_id = DOM_id;
    
    var THIS = this;
    this.dlg = new cDialog("frm_kontakt", "globalframe", 1, function() { THIS.clear(); });
}

/**
 * @param recipient String
 */
cMail.prototype.open = function(recipient) {
    this.dlg.show();
    this.recipient = recipient;
}

cMail.prototype.send = function(recipient) {
    if (typeof recipient != 'undefined') {
        this.recipient = recipient;
    }
    
    var frm = $(this.DOM_id);
    if (frm != null) {
        // name, email, tel, subject, text
        
        frm.name.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.removeAttribute('style');
        frm.email.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.removeAttribute('style');
        frm.subject.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.removeAttribute('style');
        frm.text.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.removeAttribute('style');
        $('frm_kontakt_hinweis').style.display = 'none';
                    
        var wrong = false;
        if (frm.name.value == '') {
            wrong = true;
            frm.name.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.style.color = '#CC3333'
        }
        if (frm.email.value == '') {
            wrong = true;
            frm.email.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.style.color = '#CC3333'
        }
        if (frm.subject.value == '') {
            wrong = true;
            frm.subject.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.style.color = '#CC3333'
        }
        if (frm.text.value == '') {
            wrong = true;
            frm.text.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.style.color = '#CC3333'
        }
        if (wrong == true) {
            $('frm_kontakt_hinweis').style.display = 'inline';
            return;
        }
        
        var content = JSON.stringify({
            name: frm.name.value,
            email: frm.email.value,
            tel: (frm.tel.value != '') ? frm.tel.value : ' ',
            subject: frm.subject.value,
            text: frm.text.value,
            recipient: this.recipient
        });
        
        var myxhr = new cXHR();
        var ok_cb = function() { alert('Ihre E-Mail wurde gesendet.'); };
        var oe_cb = function() { alert('Achtung: Ihre E-Mail wurde nicht gesendet. Bitte versuchen Sie es noch einmal.'); };
        myxhr.put('/cgi-bin/mail.cgi', ok_cb, content, 1, oe_cb);
        
        this.clear();
        this.dlg.close();
    }
}

cMail.prototype.clear = function() {
    var frm = $(this.DOM_id);
    if (frm != null) {
        frm.name.value = '';
        frm.email.value = '';
        frm.tel.value = '';
        frm.subject.value = '';
        frm.text.value = '';
    }
}
