/**
 * Funcoes do OPME que dependem do JQuery.
 * $Id: opme.jquery.js,v 1.4 2009/12/10 18:08:07 aagustinho Exp $
 */

//see more info at http://docs.jquery.com/Using_jQuery_with_Other_Libraries
var $j = jQuery.noConflict();

//see more info at http://docs.jquery.com/Ajax/jQuery.ajax
function _ajax(type, url, data, callback, dataType) {

	var xhr = $j.ajax({
		type: type,
		url: url,
		data: data,
		success: function(result) {

			if (xhr.getResponseHeader('X-Ajax-Error')) {
				_handleAndShowError(result);
			} else {

				try {
					callback(result);
				} catch(err) {
					//need to log the exception here
				}

			}

		},
		error: function(xhr, textStatus, errorThrown) {
			//alert('error xhr: ' + xhr + ', textStatus: ' + textStatus + ', errorThrown: ' + errorThrown);
			_handleAndShowError(xhr.responseText);
		},
		dataType: dataType
	});

}

function _handleAndShowError(result) {
	$j('#miolo').html(result);
}

function ajaxPost(url, data, callback, dataType) {
	_ajax('POST', url, data, callback, dataType);
}

function ajaxGet(url, data, callback, dataType) {
	_ajax('GET', url, data, callback, dataType);
}

function ajaxGetNoCache(url, data, callback, dataType) {
	_ajax('GET', antiCacheRand(url), data, callback, dataType);
}

function antiCacheRand(aurl){
    var dt = new Date();
    if(aurl.indexOf("?")>=0){ 
		aurl = aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
        return aurl;
    }
    else { 
		aurl = aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
        return aurl;

}


