var action = "send";

function clean() {
    $('#'+boxId+'_error').hide();
    $('#'+boxId+'_email').val('');
}

function changeView() {
    $('#'+boxId+'_message').hide();
    $('#'+boxId+'_label').hide();
    $('#'+boxId+'_email').show();
    $('#'+boxId+'_error').hide();
    if(action == "cancel") {
        action = "send";
        $('#'+boxId+'_text').html(_("USER_NEWSLETTER_TEXT_SUBSCRIBE"));
        $('#'+boxId+'_em').html(_("USER_NEWSLETTER_SUBSCRIBE"));
        $('#'+boxId+'_href').html(_("USER_NEWSLETTER_UNSUBSCRIBE"));
    }
    else {
        action = "cancel";
        $('#'+boxId+'_text').html(_("USER_NEWSLETTER_TEXT_UNSUBSCRIBE"));        
        $('#'+boxId+'_em').html(_("USER_NEWSLETTER_UNSUBSCRIBE"));
        $('#'+boxId+'_href').html(_("USER_NEWSLETTER_SUBSCRIBE"));
    }
    $('#'+boxId+'_button').show();
    $('#'+boxId+'_text').show();
    $('#'+boxId+'_em').show();
    $('#'+boxId+'_href').show();
}

function reset_box() {
    action = "cancel";
    changeView();
}

function checkForm_news(email) {
    var isValidEmail_news = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email));
    if(!isValidEmail_news || email == '') {
        $('#'+boxId+'_error').html(_("USER_NEWSLETTER_EMAIL_INVALID"));
        $('#'+boxId+'_error').show();
        return false;
    }
    return true;
}

function submit() {
    var email = $('#'+boxId+'_email').val();
    if(checkForm_news(email)) {
        if(action == "send") {
            gjcall("newsletter", {
    		    'action': 'subscribe',
		        'email': email,
		        'market': globalMarket,
                'callback' :'viewMessage_news',
                'module' : siteModule
	            }
            );
        }
        else {
            gjcall("newsletter", {
    		    'action': 'unsubscribe',
		        'email': email,
		        'market': globalMarket,
                'callback' :'viewMessage_news', 
                'module' : siteModule
	            }
            );
        }
    }
}

function viewMessage_news(error, message, label) {
    if(error == "") {
        $('#'+boxId+'_message').html(_(message));
        $('#'+boxId+'_label').html(_(label));
        $('#'+boxId+'_message').show();
        $('#'+boxId+'_label').show();
        $('#'+boxId+'_text').hide();
        $('#'+boxId+'_error').hide();
        $('#'+boxId+'_email').val(_("USER_NEWSLETTER_INSERT"));
        $('#'+boxId+'_email').hide();
        $('#'+boxId+'_button').hide();
        $('#'+boxId+'_href').hide();
        setTimeout("reset_box();", 5000);
    }
    else {
        $('#'+boxId+'_error').html(_(error));
        $('#'+boxId+'_error').show();
    }
}

