
/**
 * Display an when the text is not null. If the second argument is present
 * it is analyzing it to look for an login situation.
 *  
 * This relies  on a div with id error and some other Dom structure.
 * 
 * @param text the message to display or null
 * @param me second argument to the error handler  
 * @return
 */
showError = function(text,status,location) {
	if ( Public && Public.progressIndicator ) {
		Public.progressIndicator.hideAll();
	}
    if( typeof(status) != "undefined" && status && status.xhr ) {
        checkForLogin(status);
        // if the above function returned look for http errors
        if ( status.xhr.status != 200 ) {
            text = "An error occured, please try again later";
        }
    }
    // Check to see if we have been called directly with the data from the server
    // and there is an actionErrors member setup through the usual struts reporting mechanism.
    if( typeof(text) != "undefined" && text && text.actionErrors && text.actionErrors.length > 0 ) {
            text = text.actionErrors[0];
    }
    if ( text.message ) {
    	text = text.message;
    }
    if ( location ) {
    	text = text + "  -  " + location;
    }

   
    var errorDiv = document.getElementById("error");
    if( errorDiv == null ) {
    	if ( console && console.log ) {
    		console.log(text);    		
    	}
    }
    if ( text != null ) {
        errorDiv.style.display = 'block';
        var errorSpan = document.getElementById("errorText");
        errorSpan.innerHTML = text;
        setTimeout(function(){errorDiv.style.display='none';}, 5000);
    } else {
        errorDiv.style.display = 'none';
    }
};



/**
 * Analyze the second argument to the error handler and if necessary redirect to the 
 * login page.
 */
checkForLogin = function(me) {
    if (me.xhr && me.xhr.responseText&& me.xhr.responseText.indexOf("JSON_LOGIN") > 0) {
        alert("Your session expired, you need to re-login. Sorry for the inconvenience.");
        // IMPORTANT: if we redirect directly to the login page then after login
        // the json action will be re-executed but there is no easy way to
        // display the result as a html page that allows the user 
        // to continue their flow.
        // SO: send to the main page.
        window.location = "http://" + window.location.host + "/web/main.do";
    }

};