///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PRINCIPAL DEL AJAX
function EA_AJAXCallRequest( parametros, func_ok ) {
	var params = 'ajax=1&' + parametros;
	var url = './AppAjax.php';
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: params, 
				onFailure:  showError,
				onLoading:  showLoading,
				onLoaded:	showLoaded,
				onComplete: eval(func_ok)
			});

	return true;
}
//--------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PRINCIPAL PARA RECARGAR CONTENIDO EN UN ELEMENTO
///////////// params: elemento -> etiqueta html a recargar usando ajax
/////////////	parametros -> los parametros a enviar, string
function EA_AJAXRefreshContenido( elemento, parametros ) {
	if(! $(elemento))	return false;
	
	var params = "ajax=1&" + parametros;
	var myAjax = new Ajax.Updater( elemento , './AppAjax.php', 
					{
  						method: 'get',	parameters: params
					}
				);
	return true;
}
//--------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA OCULTAR LAS FILAS
////// recibe el id de la fila
function EA_AJAXDegradeRow( id ){
	if( $(id) ){
		new Effect.Pulsate( id, { pulses: 2 });
	}
}	
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA MOSTRAR LAS FILAS
////// recibe el id de la fila
function EA_AJAXNoDegradeRow( id ){
	if( $(id) ){
		new Effect.Appear( id );
	}
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA ELIMINAR MEDIANTE AJAX
function EA_AJAXDeleteRowElement( id, parametros ) {
	if(isBlanco(parametros)){
		alert(':: ERROR :: NOT AJAX valid!!');
		return false;
	}
	
	EA_AJAXDegradeRow( 'fila_list_' + id );
	EA_AJAXCallRequest( parametros, 'EA_AJAXResponseDeleteRowElement' );
}
//--------------------

//////////////////////////////////////////////////////////////////////////////////
//// funcion que muestra el resultado del ajax en una eliminacion
//// 
function EA_AJAXResponseDeleteRowElement(originalRequest) {
	var aux = new String(originalRequest.responseText);
	var resultado = aux.split('#@@@#');
	var exito  = parseInt( resultado[0] );
	var msg = new String( resultado[1] );
	var id_e = new String( resultado[2] );
	
	if(exito == 1){
		showOK( msg );
		if( !isBlanco(id_e) && $('fila_list_' + id_e) ){
			new Effect.Fade( 'fila_list_' + id_e );
		}
	}
	else{
		EA_AJAXNoDegradeRow( 'fila_list_' + id_e );
		showError( msg );
	}
	
	if(exito == 1){
		return true;
	}
	else{
		return false;
	}
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA ACTUALIZAR UN ESTADO MEDIANTE AJAX
function EA_AJAXChangeStateID( id, parametros ) {
	if(isBlanco(parametros)){
		alert(':: ERROR :: NOT AJAX valid!!');
		return false;
	}
	
	EA_AJAXDegradeRow( 'fila_list_' + id );
 	EA_AJAXCallRequest( parametros, 'EA_AJAXResponseChangeStateID' );
}
//--------------------


//////////////////////////////////////////////////////////////////////////////////
//// funcion que muestra el resultado del ajax cuando cambio UN ESTADO MEDIANTE AJAX
//// 
function EA_AJAXResponseChangeStateID(originalRequest) {
	var aux = new String(originalRequest.responseText);
	var resultado = aux.split('#@@@#');
	var exito  = parseInt( resultado[0] );
	var msg = new String( resultado[1] );
			
	if(exito == 1){
		window.top.showOK( msg );
		var aux2 = new String( resultado[2] );
		var data = aux2.split('*@@@*');
		var id_e = new String( data[0] );
		var src = new String( data[1] );
		var title = new String( data[2] );
		
		if( !isBlanco(id_e) && $('img_list_' + id_e) ){
			$('img_list_' + id_e).setAttribute( 'src', src );
			$('img_list_' + id_e).setAttribute( 'title' , title );
		}
	}
	else{
		window.top.showError( msg );
	}
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA ELIMINAR MEDIANTE AJAX
function EA_AJAXDeleteItemListaOrdenada( id, parametros ) {
	if(isBlanco(parametros)){
		alert(':: ERROR :: NOT AJAX valid!!');
		return false;
	}
	
	EA_AJAXDegradeRow( 'itemListaOrdenada_' + id );
	EA_AJAXCallRequest( parametros, 'EA_AJAXResponseDeleteItemListaOrdenada' );
}
//--------------------

//////////////////////////////////////////////////////////////////////////////////
//// funcion que muestra el resultado del ajax en una eliminacion
//// 
function EA_AJAXResponseDeleteItemListaOrdenada(originalRequest) {
	var aux = new String(originalRequest.responseText);
	var resultado = aux.split('#@@@#');
	var exito  = parseInt( resultado[0] );
	var msg = new String( resultado[1] );
	var id_e = new String( resultado[2] );
	
	if(exito == 1){
		showOK( msg );
		if( !isBlanco(id_e) && $('itemListaOrdenada_' + id_e) ){
			new Effect.Fade( 'itemListaOrdenada_' + id_e );
			if( $('listaOrdenada') && $('itemListaOrdenada_' + id_e)){
				$('itemListaOrdenada_' + id_e).remove();
			}			
		}
	}
	else{
		EA_AJAXNoDegradeRow( 'itemListaOrdenada_' + id_e );
		showError( msg );
	}
	
	if(exito == 1){
		return true;
	}
	else{
		return false;
	}
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PARA MENSAJES INFORMATIVOS DE AJAX
function AJAXshowLoading( obj ) {
	if( $('ApplicationMessageBox') ){
		$('ApplicationMessageBox').style.display = 'none';
	}
	if(obj){
		obj.className = 'AJAX-mensajeBox-INFO';
		obj.style.display = 'block';
		obj.innerHTML = "Sending information.....";
	}
}
//----------

function AJAXshowLoaded( obj ) {
	if( $('ApplicationMessageBox') ){
		$('ApplicationMessageBox').style.display = 'none';
	}
	if(obj){
		obj.className = 'AJAX-mensajeBox-INFO';
		obj.style.display = 'block';
		obj.innerHTML = "Process Result.....";
	}
}
//----------

function AJAXshowError( obj, msg ) {
	if( $('ApplicationMessageBox') ){
		$('ApplicationMessageBox').style.display = 'none';
	}
	if(obj){
		obj.className = 'AJAX-mensajeBox-ERROR';
		obj.style.display = 'block';
		obj.innerHTML = msg;
	}
}

function AJAXshowOK( obj, msg ) {
	if( $('ApplicationMessageBox') ){
		$('ApplicationMessageBox').style.display = 'none';
	}
	if(obj){
		obj.className = 'AJAX-mensajeBox-OK';
		obj.style.display = 'block';
		obj.innerHTML = msg;
	}
}

function AJAXhideElement( obj ) {
	if(obj){
		obj.style.display = 'none';
	}
}
//----------

//----------
function showLoading() {	AJAXshowLoading( $('AJAXSPANMSG') );	}
function showLoaded() {	AJAXshowLoaded( $('AJAXSPANMSG') );	}
function showError( msg ) {	AJAXshowError( $('AJAXSPANMSG'), msg );	}
function showOK( msg ) {	AJAXshowOK( $('AJAXSPANMSG'), msg );	}
//----------
///////////////////////////////////////////////////////////////////////////////////////////
