4; // Cambiar entre 1 y 9 - Version del archivo (utilizar números enteros)

var gVersion = 'v1.0.4';
var gbMostrarVersion = false;
if (gbMostrarVersion) alert ('buscar.js: ' + gVersion);

var opcionesBuscar = {
    target:        '#ajaxcontent',				// target element(s) to be updated with server response
    beforeSubmit:  validar,   							// pre-submit callback
    success:       resultadoConsultarOK,		// post-submit callback
    error:	   resultadoConsultarError,	// post-submit callback error

    // other available options:
    url:       'ajax_buscar.php',		// override for form's 'action' attribute
    type:      'POST',        		// 'get' or 'post', override for form's 'method' attribute
    dataType:  'html',        		// 'xml', 'script', or 'json' (expected server response type)
    clearForm: false,        		// clear all form fields after successful submit
    resetForm: true        			// reset the form after successful submit
};

var opciones = {
    target:        '#ajaxcontent',				// target element(s) to be updated with server response
    beforeSubmit:  validar,   							// pre-submit callback
    success:       resultadoConsultarOK,		// post-submit callback
    error:		   resultadoConsultarError,	// post-submit callback error

    // other available options:
    url:       'ajax_accion.php',		// override for form's 'action' attribute
    type:      'POST',        		// 'get' or 'post', override for form's 'method' attribute
    dataType:  'html',        		// 'xml', 'script', or 'json' (expected server response type)
   	clearForm: false,        		// clear all form fields after successful submit
    resetForm: true        			// reset the form after successful submit
};

$(document).ready(function()
{
//		$('#formulario').ajaxForm(opcionesBuscar);
//		$('#formconsserv').ajaxForm(opciones);
});

function HistoryBackContentLoader(parametros)
{
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	$('body').css('cursor','wait');
	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_accion.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
};

function CambiarBarrio(Idioma, Destacada, cboBarrio)
{
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	$('body').css('cursor','wait');
	var parametros = {"l":Idioma, "d":Destacada, "b":cboBarrio };
	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_buscar.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
};

function Busqueda(Idioma, Destacada, sKey, cboBarrio, iCategoria, iTipoProp, sCheckIn, sCheckOut, page) {
	$('body').css('cursor','wait');
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	var parametros = {"l":Idioma,
										"d":Destacada,
										"key":sKey,
										"b":cboBarrio,
										"cboCategoria":iCategoria,
										"cboTipo":iTipoProp,
										"checkin":sCheckIn,
										"checkout":sCheckOut,
										"page":page
									};

	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_buscar.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
};

function MostrarLista(Idioma)
{
	$('body').css('cursor','wait');
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	var parametros = {"l":Idioma, "d":-2, "key":getCookieList()}; //Requiere jscript listadoUsuario.js

	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_buscar.php",
      type:     		"post",
      dataType: 		"json"
	});

	return false;
};

function MostrarPropiedad(Idioma, cboProp)
{
	$('body').css('cursor','wait');
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	var parametros = {"l":Idioma, "p":"prop", "id":cboProp};

	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_accion.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
};

function MostrarSeccionPropiedad(Idioma, Seccion, cboProp)
{
$('body').css('cursor','wait');
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	var parametros = {"l":Idioma, "p":Seccion, "id":cboProp};

	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_accion.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
} ;

function MostrarSeccionFija(Idioma, Seccion)
{
	$('body').css('cursor','wait');
	$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
	var parametros = {"l":Idioma, "p":Seccion};

	$.ajax({
      async:    		true,
      success:			resultadoDetalleOK,
      data:     		parametros,
      url:		  		"ajax_accion.php",
      type:     		"post",
      dataType: 		"json"
	});
	return false;
};

function resultadoDetalleOK(responseText) {

  try
  {
		$('body').css('cursor','default');
		var oMensaje = $('#ajaxcontent');

  	if (responseText.Ok)
  	{
  		if(responseText.goto)	window.scrollTo(0,$(responseText.goto).offset().top);
			mostrarMensaje(oMensaje, responseText.Datos);
			AnchorLink('#ajaxcontent a');
			AnchorLink('.historial');
		}
  	else
  	{
  		if (responseText.Mensaje == '')
  			responseText.Mensaje = 'Lo sentimos. NO se han podido obtener los datos.';

  		mostrarMensaje(oMensaje, responseText.Mensaje, true);
    }
  }
	catch(e)
	{
    mostrarMensaje(oMensaje, e.message + '\n' + responseText, true);
	}
}

/*********************************************************************
Funcion: validar
Motivo:
	Valida los datos enviados
Parametros:
	formData ->
	jqForm	 ->
	options	 ->
Devuelve:
	No devuelve datos ni resultados.
**********************************************************************/
function validar(formData, jqForm, options)
{
	try
	{
		var oMensaje = $('#MensajesABM');
		var options = {
				        showInlineMessages:		true,
				        showSummary:			false,
				        inlineMessageCssClass:	'validation-advice'
					  };

		if(jqForm.attr('id') == 'formmail')
			if(jqForm.valid())
			{
				$('#ajaxcontent').attr("innerHTML", "<div style='margin: 50px 300px 800px; padding-left: 25px; background-image: url(images/template/loadinfo.net.gif); background-repeat: no-repeat; color: rgb(242, 97, 34); padding-top: 5px; padding-bottom: 5px;'>Cargando</div>");
//				$.historyLoad($.param(formData));
				return true;
			}
			else
			{
				mostrarMensaje(oMensaje, 'Verifique los campos con error.', true, true);
				return false;
			}
//		else
//			$.historyLoad($.param(formData));


		mostrarMensaje(oMensaje, 'Espere por favor...', false, true);
		return true;
	}
	catch(oError)
	{
		alert('Error (validar): ' + oError.message);
		return false;
	}
}

/*********************************************************************
Funcion: resultadoConsultarOK
Motivo:
	Maneja la carga de los resultados.
Parametros:
	responseText -> Es el texto devuelto por el server.
	statusText	 -> Es el mensaje de status.
Devuelve:
	No devuelve datos ni resultados.
**********************************************************************/
function resultadoConsultarOK(responseText, statusText)
{
    try
    {
		var oMensaje = $('#ajaxcontent');
		if (responseText.substring(0,1) == '[' || responseText.substring(0,1) == '{' )
		{
			// Genero el resultado objeto
	    	var oResultado = eval('(' + responseText + ')');

	    	if (oResultado.Ok)
	    	{
    			//location.href = location.href+"#topdetalle";
   				window.scrollTo(0,$("#ajaxcontent").offset().top);
    			mostrarMensaje(oMensaje, oResultado.Datos);
					AnchorLink('#ajaxcontent a');
					AnchorLink('.historial');
    		}
	    	else
	    	{
	    		if (oResultado.Mensaje == '')
	    			oResultado.Mensaje = 'NO se han podido obtener los datos.';

	    		mostrarMensaje(oMensaje, oResultado.Mensaje, true);
	    	}
		}
		else
		{
	    	alert('Respuesta del servidor inesperada.');
	    	mostrarMensaje(oMensaje, responseText, true);
		}
  }
	catch(e)
	{
		alert('Error (resultadoConsultarOK): ' + e.message);
    	mostrarMensaje(oMensaje, e.message + '\n' + responseText, true, false);
	}

}

/*********************************************************************
Funcion: resultadoConsultarError
Motivo:
	Maneja los errores en la respuesta del servidor.
Parametros:
	oHTML		-> Es el objeto XMLHttpRequest.
	sMensaje	-> Es el mensaje de error.
	oError		-> Es el objeto con el error si ocurrió alguno.
Devuelve:
	No devuelve datos ni resultados.
**********************************************************************/
function resultadoConsultarError(oHTML, statusText, oError)
{
	try
	{
		var oMensaje = $('#listadoAvisosResultado');
		var sError = '';

		mostrarOcultarBotones();

		if (oHTML.status == 0)
		{
			mostrarMensaje(oMensaje, 'Acción cancelada por el usuario', true, false);
			return false;
		}

		if (oHTML.status == 401)
		{
			location = 'logout.php';
			return false;
		}

		if (oHTML.responseText.substring(0,1) == '[' || oHTML.responseText.substring(0,1) == '{' )
		{

			var oRegistro = eval('(' + oHTML.responseText + ')');
			if (oRegistro.Ok)
			{
				alert('ATENCION OK con error:\n\n' + oRegistro.Mensaje);
				mostrarMensaje(oMensaje, oRegistro.Mensaje, true, false);
			}
			else
			{
				alert('ATENCION Error:\n\n' + oRegistro.Mensaje);
				mostrarMensaje(oMensaje, oRegistro.Mensaje, true, false);
			}
		}
		else
		{
			if (oError)
				sError = '\n' + oError.message + '\n';

			sError += oHTML.responseText;

			alert('ATENCIÓN:\nOcurrió un error.' + sError);
			mostrarMensaje(oMensaje, sError, true, false);
		}

	}
	catch(oError)
	{
		alert('Error(resultadoConsultarError):' + oError.message);
		mostrarMensaje(oMensaje, oError.message + ':<br/><br/>' + oHTML.responseText, true, false);
	}
}
