var currentSuscribeTypeSelected = "html";

function CurrentSuscribeType(object)
{
    currentSuscribeTypeSelected = object.value;
}

function suscribe()
{
	var deferred = dojo.xhrPost({
        url: "newsletter/suscribe/news",
        content:
        {
            "email":dojo.byId("txtNewsletter").value,
            "type":currentSuscribeTypeSelected
		},
        handleAs: "text",
        timeout: 5000, //Time in milliseconds
        handle: handlesuscribersresponse
	});
}

function handlesuscribersresponse (response, ioArgs){
    //This function handles the response.
    //Inside this function, the "this" variable
    //will be the object used as the argument to the dojo.xhrGet() call.
    if(response instanceof Error){
            if(response.dojoType == "cancel"){
                    //The request was canceled by some other JavaScript code.
                    console.debug("Request canceled.");
            }else if(response.dojoType == "timeout"){
                    //The request took over 5 seconds to complete.
                    console.debug("Request timed out.");
            }else{
                    //Some other error happened.
                    console.error(response);
            }
            alert('Error'+ response);
    }else{
            //Success.Since original call wanted the response handled
            //as text (handleAs: "text"), response will be a text string.
            console.debug("Successful server response: " + response);

            //ioArgs is an object with some useful properties on it.
            //For instance, for XMLHttpRequest calls, ioArgs.xhr is the
            //XMLHttpRequest that was used for the call.
            console.debug("HTTP status code: ", ioArgs.xhr);
            if(response == "")
            {
                dojo.byId("cmdNewsletter").style.visibility = "hidden";
                dojo.byId("cmdNewsletter").style.display = "none";
                dojo.byId("suscribeok").style.display = "none";
                dojo.byId("suscribeko").style.visibility = "visible";

            }
            else if(response != "")
            {
                dojo.byId("cmdNewsletter").style.visibility = "hidden";
                dojo.byId("cmdNewsletter").style.display = "none";
                dojo.byId("suscribeok").style.visibility = "visible";

            }
            //alert(response);
    }
    //If you think there could be other callback handlers registered with this deferred, then
    //return response to propagate the same response to other callback handlers. Otherwise,
    //the error callbacks may be called in the success case.
    return response;
}

