/* Call an AJAX call and replace the target element with the response data */
function ajaxReplaceContent(containerName, pageURL, params){       
    //force the atitude to be JSON
    if(isString(params)){
        if(params != ""){
            params = params + "&";
        }
        params = params + "attitude=json";
    }else{
        params.attitude="json";
    }    
    //make the request
    new Ajax.Request(pageURL, {
        method:'post',
        parameters : params,
        onSuccess: function(transport){
            var response = transport.responseJSON || "";
            if(response == ""){      
                ajaxReplaceContentFailed(containerName, "No Data");
            }else if(response.error == 1){
                ajaxReplaceContentFailed(containerName, "Error");
            }else{
                //Success
                ajaxReplaceContentWithResponse(containerName, response);
            }
        },
        onFailure : function(){
            ajaxReplaceContentFailed(containerName, "Page Failure");
        }
    }
    );
}

/* Replace other content on load */
function ajaxReplaceContentReload(containerName, pageURL, params, callbackURL, callbackParams){

}

function ajaxReplaceContentWithResponse(containerName, response){
    Element.replace($(containerName), response.result);
}

function ajaxReplaceContentFailed(containerName, error){
    alert("Fail:Unable to complete request" + " " + error);
}


/* Utilities */
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
function isObject(obj) {
   if (obj.constructor.toString().indexOf("Object") == -1 || obj == null)
      return false;
   else
      return true;
}
function isString(obj) {
   if (obj.constructor.toString().indexOf("String") == -1 || obj == null)
      return false;
   else
      return true;
}