function widget_email_collector_focus(element) {
    if (element.style.color) {
        element.style.color = '';
        element.value = '';
    }
    return true;
}

var handleResponse = function(xmlhttp) {
    var response = xmlhttp != null ? xmlhttp.responseText : null;
    if (response) {
        $('email_collector').style.height = '';
        if (response.indexOf('success') > -1) {
            $('email_collector').innerHTML = '<span id=\"success\">Thanks for signing up!  To ensure delivery add <b>daily@mail.popsugar.com</b> to your contact list.</span>';
        }
        else {
            if (response.indexOf('invalid email') > -1) {
                $('email_collector').innerHTML = '<span id="error">Oops! Invalid email.</span>';
            }
            else {
                $('email_collector').innerHTML = '<span id="error">An error occurred.  Please try again later.</span>';
            }
        }
    }
};

function widget_email_submit_email(formElement) {
    var formValues = Form.serialize(formElement);
    var options = {
        method: 'post',
        contentType: 'application/x-www-form-urlencoded',
        postBody: formValues,
        onSuccess: handleResponse
    };
    new Ajax.Request(formElement.action, options);
    return false;
}

/*
function widget_email_render(name,list) {
    var content;

    if (name != null && name.length > 0) {
        content = '<form method="post" action="/manage/widgets/email/signup" onsubmit="return widget_email_submit_email(this)"><span class="content_logged_in">' + name + ', click to <input class="fancybutton" name="submit" type="submit" value="Sign Up!" /></span><input type="hidden" name="List" value="'+list+'"></form>';
    }
    else {
        content = '<form method="post" action="/manage/widgets/email/signup" onsubmit="return widget_email_submit_email(this)"><input type="hidden" name="List" value="'+list+'"><input name="Email" size="14" type="text" maxlength=100 value="Enter email" style="color:#999;" onfocus="return widget_email_collector_focus(this);" class="form-text"><input  style="width: 64px;" class="fancybutton" name="submit" type="submit" value="Sign Up!" /></form>';
    }
    $('email_collector').innerHTML = content;
}

var options = {
    method: 'get',
    onSuccess: function (xmlhttp) {
        var response = xmlhttp != null ? xmlhttp.responseText : null;
        
        if (response != null) {
            response = response.replace(/^\s+|\s+$/g, '') ;
        }
        widget_email_render(response,list);
    }
};
new Ajax.Request("/manage/widgets/email/getusername", options);

*/

