window.onload = initEmailafriendForm;

function initEmailafriendForm() {

    var f = document.getElementsByTagName('form')[0];
    if (!f) return;
    
    var inputs = f.getElementsByTagName('input');
    var comment = f.getElementsByTagName('textarea')[0];
    var defaults = {
        'to' : "recipient's e-mail address",
        'from' : "your e-mail address",
        'comment' : "enter a brief message (optional)",
        'color' : '#aaaaaa'
    }
    for (i in defaults) {
        input = f[i];
        if (input) {
            input.value = defaults[i];
            input.style.color = defaults.color;
            input.onfocus = focus;
            input.onblur = blur;
        }
    }
    
    function focus(event) {
        if (this.value == defaults[this.name]) {
            this.value = '';
            this.style.color = '#000';
            this.style.backgroundColor = '#fff';
        }
    }
    function blur(event) {
        if (this.value == '') {
            this.value = defaults[this.name];
            this.style.color = defaults.color;
        }
    }
    
    f.onsubmit = function() {
        var go = true;
        for (var i=0;i<2;i++) {
            var input = inputs[i];
            if (!isValidEmail(input.value)) {
                alert('Please enter a valid e-mail address in the "'+input.name+'" field.');
                input.style.backgroundColor = '#ffffb3';
                go = false;
            }
        }
        if (!go) return false;
        if (comment.value == defaults.comment) comment.value = '';
    }
    
}

function isValidEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

