function trim (cadena)
{
	var i;
	var cadena2 = cadena;
	var cadena3;
	
	if (cadena.length == 0) return cadena;
	
	for (i = 0; i < cadena.length; i++)
	{
		if (cadena.substr (i, 1) == ' ')
			cadena2 = cadena.substr (i + 1);
		else
			break;
	}

	if (cadena2.length == 0) return cadena2;
	
	cadena3 = cadena2;
	
	for (i = cadena2.length - 1; i >= 0; i--)
	{
		if (cadena2.substr (i, 1) == ' ')
			cadena3 = cadena2.substr (0, i);
		else
			break;
	}

	return cadena3;
}

// Validamos que el string recibido como parámetro no contenga ninguna barra invertida.
function fn_ValidarBarraInvertida (strTexto)
{
	var i;
	var caracter;

	for (i = 0; i < strTexto.length; i++)
	{
		caracter = strTexto.substr (i, 1);
		
		if (caracter == "\\")
			return false;
	}
	
	return true;
}

function fn_Escape (cadena)
{
	var resultado = escape (cadena);
	
	while (resultado.indexOf ("+") >= 0)
		resultado = resultado.replace ("+", "%2B");

	return resultado;
}

function fn_txt_Busqueda_KeyPress (evento)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode == 13)
		fn_Buscar ();
}

function fn_BloquearBotones ()
{
	document.getElementById ("tablaGeneral").style.cursor = "wait";
	
	if (document.getElementById ("btn_Busqueda"))
	{
		document.getElementById ("btn_Busqueda").style.cursor = "wait";
		document.getElementById ("btn_Busqueda").disabled = true;
	}

	if (document.getElementById ("btn_Login_Ingresar"))
	{
		document.getElementById ("btn_Login_Ingresar").style.cursor = "wait";
		document.getElementById ("btn_Login_Ingresar").disabled = true;
	}
	
	if (document.getElementById ("btn_Contrasena_Confirmar"))
	{
		document.getElementById ("btn_Contrasena_Confirmar").style.cursor = "wait";
		document.getElementById ("btn_Contrasena_Confirmar").disabled = true;
	}
	
	if (document.getElementById ("btn_Contrasena_Volver"))
	{
		document.getElementById ("btn_Contrasena_Volver").style.cursor = "wait";
		document.getElementById ("btn_Contrasena_Volver").disabled = true;
	}
	
	if (document.getElementById ("btn_Contrasena_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Contrasena_MensajeOk_Volver").style.cursor = "wait";
		document.getElementById ("btn_Contrasena_MensajeOk_Volver").disabled = true;
	}

	if (document.getElementById ("btn_Recuperar_Confirmar"))
	{
		document.getElementById ("btn_Recuperar_Confirmar").style.cursor = "wait";
		document.getElementById ("btn_Recuperar_Confirmar").disabled = true;
	}
	
	if (document.getElementById ("btn_Recuperar_Volver"))
	{
		document.getElementById ("btn_Recuperar_Volver").style.cursor = "wait";
		document.getElementById ("btn_Recuperar_Volver").disabled = true;
	}
	
	if (document.getElementById ("btn_Recuperar_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Recuperar_MensajeOk_Volver").style.cursor = "wait";
		document.getElementById ("btn_Recuperar_MensajeOk_Volver").disabled = true;
	}	

	if (document.getElementById ("btn_Registro_Confirmar"))
	{
		document.getElementById ("btn_Registro_Confirmar").style.cursor = "wait";
		document.getElementById ("btn_Registro_Confirmar").disabled = true;
	}

	if (document.getElementById ("btn_Registro_Volver"))
	{
		document.getElementById ("btn_Registro_Volver").style.cursor = "wait";
		document.getElementById ("btn_Registro_Volver").disabled = true;
	}

	if (document.getElementById ("btn_Registro_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Registro_MensajeOk_Volver").style.cursor = "wait";
		document.getElementById ("btn_Registro_MensajeOk_Volver").disabled = true;
	}

	if (document.getElementById ("btn_MiCuenta_Confirmar"))
	{
		document.getElementById ("btn_MiCuenta_Confirmar").style.cursor = "wait";
		document.getElementById ("btn_MiCuenta_Confirmar").disabled = true;
	}

	if (document.getElementById ("btn_MiCuenta_Volver"))
	{
		document.getElementById ("btn_MiCuenta_Volver").style.cursor = "wait";
		document.getElementById ("btn_MiCuenta_Volver").disabled = true;
	}

	if (document.getElementById ("btn_MiCuenta_MensajeOk_Volver"))
	{
		document.getElementById ("btn_MiCuenta_MensajeOk_Volver").style.cursor = "wait";
		document.getElementById ("btn_MiCuenta_MensajeOk_Volver").disabled = true;
	}

	if (document.getElementById ("btn_Contacto_Enviar"))
	{
		document.getElementById ("btn_Contacto_Enviar").style.cursor = "wait";
		document.getElementById ("btn_Contacto_Enviar").disabled = true;
	}	

	if (document.getElementById ("btn_Contacto_Volver"))
	{
		document.getElementById ("btn_Contacto_Volver").style.cursor = "wait";
		document.getElementById ("btn_Contacto_Volver").disabled = true;
	}

	if (document.getElementById ("btn_Contacto_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Contacto_MensajeOk_Volver").style.cursor = "wait";
		document.getElementById ("btn_Contacto_MensajeOk_Volver").disabled = true;
	}

	if (document.getElementById ("btn_PedirLibro"))
	{
		document.getElementById ("btn_PedirLibro").style.cursor = "wait";
		document.getElementById ("btn_PedirLibro").disabled = true;
	}

	if (document.getElementById ("btn_Carrito_CompletarPedido"))
	{
		document.getElementById ("btn_Carrito_CompletarPedido").style.cursor = "wait";
		document.getElementById ("btn_Carrito_CompletarPedido").disabled = true;
	}

	if (document.getElementById ("btn_Carrito_SeguirComprando"))
	{
		document.getElementById ("btn_Carrito_SeguirComprando").style.cursor = "wait";
		document.getElementById ("btn_Carrito_SeguirComprando").disabled = true;
	}

	if (document.getElementById ("btn_Carrito_AnularPedido"))
	{
		document.getElementById ("btn_Carrito_AnularPedido").style.cursor = "wait";
		document.getElementById ("btn_Carrito_AnularPedido").disabled = true;
	}

	if (document.getElementById ("btn_Datos_PasoSiguiente"))
	{
		document.getElementById ("btn_Datos_PasoSiguiente").style.cursor = "wait";
		document.getElementById ("btn_Datos_PasoSiguiente").disabled = true;
	}
	
	if (document.getElementById ("btn_Envio_PasoSiguiente"))
	{
		document.getElementById ("btn_Envio_PasoSiguiente").style.cursor = "wait";
		document.getElementById ("btn_Envio_PasoSiguiente").disabled = true;
	}

	if (document.getElementById ("btn_MedioPago_PasoSiguiente"))
	{
		document.getElementById ("btn_MedioPago_PasoSiguiente").style.cursor = "wait";
		document.getElementById ("btn_MedioPago_PasoSiguiente").disabled = true;
	}

	if (document.getElementById ("btn_Resumen_EnviarOrden"))
	{
		document.getElementById ("btn_Resumen_EnviarOrden").style.cursor = "wait";
		document.getElementById ("btn_Resumen_EnviarOrden").disabled = true;
	}

	if (document.getElementById ("btn_GraciasPorSuCompra_Aceptar"))
	{
		document.getElementById ("btn_GraciasPorSuCompra_Aceptar").style.cursor = "wait";
		document.getElementById ("btn_GraciasPorSuCompra_Aceptar").disabled = true;
	}

	if (document.getElementById ("btn_GraciasPorSuCompra_Volver"))
	{
		document.getElementById ("btn_GraciasPorSuCompra_Volver").style.cursor = "wait";
		document.getElementById ("btn_GraciasPorSuCompra_Volver").disabled = true;
	}

	if (document.getElementById ("btn_SeleccionarSucursalOca"))
	{
		document.getElementById ("btn_SeleccionarSucursalOca").style.cursor = "wait";
		document.getElementById ("btn_SeleccionarSucursalOca").disabled = true;
	}
}

function fn_HabilitarBotones ()
{
	document.getElementById ("tablaGeneral").style.cursor = "default";
	
	if (document.getElementById ("btn_Busqueda"))
	{
		document.getElementById ("btn_Busqueda").style.cursor = "pointer";
		document.getElementById ("btn_Busqueda").disabled = false;
	}

	if (document.getElementById ("btn_Login_Ingresar"))
	{
		document.getElementById ("btn_Login_Ingresar").style.cursor = "pointer";
		document.getElementById ("btn_Login_Ingresar").disabled = false;
	}

	if (document.getElementById ("btn_Contrasena_Confirmar"))
	{
		document.getElementById ("btn_Contrasena_Confirmar").style.cursor = "pointer";
		document.getElementById ("btn_Contrasena_Confirmar").disabled = false;
	}
	
	if (document.getElementById ("btn_Contrasena_Volver"))
	{
		document.getElementById ("btn_Contrasena_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Contrasena_Volver").disabled = false;
	}
	
	if (document.getElementById ("btn_Contrasena_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Contrasena_MensajeOk_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Contrasena_MensajeOk_Volver").disabled = false;
	}

	if (document.getElementById ("btn_Recuperar_Confirmar"))
	{
		document.getElementById ("btn_Recuperar_Confirmar").style.cursor = "pointer";
		document.getElementById ("btn_Recuperar_Confirmar").disabled = false;
	}
	
	if (document.getElementById ("btn_Recuperar_Volver"))
	{
		document.getElementById ("btn_Recuperar_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Recuperar_Volver").disabled = false;
	}
	
	if (document.getElementById ("btn_Recuperar_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Recuperar_MensajeOk_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Recuperar_MensajeOk_Volver").disabled = false;
	}	

	if (document.getElementById ("btn_Registro_Confirmar"))
	{
		document.getElementById ("btn_Registro_Confirmar").style.cursor = "pointer";
		document.getElementById ("btn_Registro_Confirmar").disabled = false;
	}

	if (document.getElementById ("btn_Registro_Volver"))
	{
		document.getElementById ("btn_Registro_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Registro_Volver").disabled = false;
	}

	if (document.getElementById ("btn_Registro_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Registro_MensajeOk_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Registro_MensajeOk_Volver").disabled = false;
	}

	if (document.getElementById ("btn_MiCuenta_Confirmar"))
	{
		document.getElementById ("btn_MiCuenta_Confirmar").style.cursor = "pointer";
		document.getElementById ("btn_MiCuenta_Confirmar").disabled = false;
	}

	if (document.getElementById ("btn_MiCuenta_Volver"))
	{
		document.getElementById ("btn_MiCuenta_Volver").style.cursor = "pointer";
		document.getElementById ("btn_MiCuenta_Volver").disabled = false;
	}

	if (document.getElementById ("btn_MiCuenta_MensajeOk_Volver"))
	{
		document.getElementById ("btn_MiCuenta_MensajeOk_Volver").style.cursor = "pointer";
		document.getElementById ("btn_MiCuenta_MensajeOk_Volver").disabled = false;
	}

	if (document.getElementById ("btn_Contacto_Enviar"))
	{
		document.getElementById ("btn_Contacto_Enviar").style.cursor = "pointer";
		document.getElementById ("btn_Contacto_Enviar").disabled = false;
	}	

	if (document.getElementById ("btn_Contacto_Volver"))
	{
		document.getElementById ("btn_Contacto_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Contacto_Volver").disabled = false;
	}

	if (document.getElementById ("btn_Contacto_MensajeOk_Volver"))
	{
		document.getElementById ("btn_Contacto_MensajeOk_Volver").style.cursor = "pointer";
		document.getElementById ("btn_Contacto_MensajeOk_Volver").disabled = false;
	}

	if (document.getElementById ("btn_PedirLibro"))
	{
		document.getElementById ("btn_PedirLibro").style.cursor = "pointer";
		document.getElementById ("btn_PedirLibro").disabled = false;
	}
	
	if (document.getElementById ("btn_Carrito_CompletarPedido"))
	{
		document.getElementById ("btn_Carrito_CompletarPedido").style.cursor = "pointer";
		document.getElementById ("btn_Carrito_CompletarPedido").disabled = false;
	}

	if (document.getElementById ("btn_Carrito_SeguirComprando"))
	{
		document.getElementById ("btn_Carrito_SeguirComprando").style.cursor = "pointer";
		document.getElementById ("btn_Carrito_SeguirComprando").disabled = false;
	}

	if (document.getElementById ("btn_Carrito_AnularPedido"))
	{
		document.getElementById ("btn_Carrito_AnularPedido").style.cursor = "pointer";
		document.getElementById ("btn_Carrito_AnularPedido").disabled = false;
	}

	if (document.getElementById ("btn_Datos_PasoSiguiente"))
	{
		document.getElementById ("btn_Datos_PasoSiguiente").style.cursor = "pointer";
		document.getElementById ("btn_Datos_PasoSiguiente").disabled = false;
	}

	if (document.getElementById ("btn_Envio_PasoSiguiente"))
	{
		document.getElementById ("btn_Envio_PasoSiguiente").style.cursor = "pointer";
		document.getElementById ("btn_Envio_PasoSiguiente").disabled = false;
	}

	if (document.getElementById ("btn_MedioPago_PasoSiguiente"))
	{
		document.getElementById ("btn_MedioPago_PasoSiguiente").style.cursor = "pointer";
		document.getElementById ("btn_MedioPago_PasoSiguiente").disabled = false;
	}

	if (document.getElementById ("btn_Resumen_EnviarOrden"))
	{
		document.getElementById ("btn_Resumen_EnviarOrden").style.cursor = "pointer";
		document.getElementById ("btn_Resumen_EnviarOrden").disabled = false;
	}

	if (document.getElementById ("btn_GraciasPorSuCompra_Aceptar"))
	{
		document.getElementById ("btn_GraciasPorSuCompra_Aceptar").style.cursor = "pointer";
		document.getElementById ("btn_GraciasPorSuCompra_Aceptar").disabled = false;
	}

	if (document.getElementById ("btn_GraciasPorSuCompra_Volver"))
	{
		document.getElementById ("btn_GraciasPorSuCompra_Volver").style.cursor = "pointer";
		document.getElementById ("btn_GraciasPorSuCompra_Volver").disabled = false;
	}

	if (document.getElementById ("btn_SeleccionarSucursalOca"))
	{
		document.getElementById ("btn_SeleccionarSucursalOca").style.cursor = "pointer";
		document.getElementById ("btn_SeleccionarSucursalOca").disabled = false;
	}
}

function fn_Buscar ()
{
	var textoBusqueda = trim (document.getElementById ("txt_Busqueda").value);
	
	if (textoBusqueda != "")
	{
		if (fn_ValidarBarraInvertida (textoBusqueda) == false)
		{
			alert ("No puede utilizarse la barra invertida en la búsqueda.");
			document.getElementById ("txt_Busqueda").focus ();
			return false;
		}
	}

	if (textoBusqueda == "")
	{
		alert ("Debe ingresar una palabra para la búsqueda.");
		document.getElementById ("txt_Busqueda").focus ();
		return false;
	}
	
	fn_BloquearBotones ()
	window.location.href = "./busqueda.php?keyword=" + fn_Escape (textoBusqueda);
}

function fn_Login_KeyPress (evento)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode == 13)
		fn_Login_Ingresar ();
}

function fn_Login_Ingresar ()
{
	if (fn_Login_ValidarIngreso ())
	{
		fn_BloquearBotones ();
		
		var parametrosAjax = "";
	
		parametrosAjax = parametrosAjax + "txt_Login_Email=" + fn_Escape (trim (document.getElementById ("txt_Login_Email").value));
		parametrosAjax = parametrosAjax + "&txt_Login_Contrasena=" + fn_Escape (trim (document.getElementById ("txt_Login_Contrasena").value));
		parametrosAjax = parametrosAjax + "&txt_DesdeRegistroUsuario=0";

		fn_InvocarAjax_Post ("./login_ajax.php", parametrosAjax);
	}
}

// Realiza un login directo cuando un usuario nuevo se registra, recibiendo e-mail y contraseña como parámetros.
function fn_Login_DesdeRegistroUsuario (email, contrasena)
{
	var parametrosAjax = "";

	parametrosAjax = parametrosAjax + "txt_Login_Email=" + fn_Escape (email);
	parametrosAjax = parametrosAjax + "&txt_Login_Contrasena=" + fn_Escape (contrasena);
	parametrosAjax = parametrosAjax + "&txt_DesdeRegistroUsuario=-1";

	fn_InvocarAjax_Post ("./login_ajax.php", parametrosAjax);
}

function fn_Login_ValidarIngreso ()
{
	var controlUsuario = document.getElementById ("txt_Login_Email");
	var controlContrasena = document.getElementById ("txt_Login_Contrasena");
	
	if (trim (controlUsuario.value) == "")
	{
		alert ("Por favor, ingresá tu dirección de e-mail.");
		controlUsuario.focus ();
		return false;
	} 
	if (trim (controlContrasena.value) == "")
	{
		alert ("Por favor, ingresá tu contraseña.");
		controlContrasena.focus ();
		return false;
	}
	
	if (fn_ValidarUnEmailValido (trim (controlUsuario.value)) == false)
	{
		alert ("La dirección de e-mail ingresada no es válida.");
		controlUsuario.focus ();
		return false;
	}	

	if (fn_ValidarSoloLetrasNumeros (trim (controlContrasena.value)) == false)
	{
		alert ("La contraseña sólo puede contener letras y números. Por favor, volvé a ingresarla.");
		controlContrasena.value = "";
		controlContrasena.focus ();
		return false;
	}		
	
	return true;
}

function fn_LogOut ()
{
	fn_BloquearBotones ();
	fn_InvocarAjax_Post ("./logout_ajax.php", "");
}

function fn_LogOutOk ()
{
	if (document.getElementById ("txt_RequiereUsuarioLogueado").value == "-1")
		window.location.href = "./index.php";
	else
	{
		document.getElementById ("txt_Login_Email").value = "";
		document.getElementById ("txt_Login_Contrasena").value = "";
	
		document.getElementById ("div_UsuarioLogueado").style.display = "none";
		document.getElementById ("div_UsuarioSinLoguear").style.display = "block";

		fn_HabilitarBotones ();
	}
}

function fn_LoginOk (nombre, desdeRegistroUsuario)
{
	document.getElementById ("span_Nickname").innerHTML = nombre;

	document.getElementById ("div_UsuarioSinLoguear").style.display = "none";
	document.getElementById ("div_UsuarioLogueado").style.display = "block";
	
	if (desdeRegistroUsuario)
		fn_Registro_AltaOk ();
	else
		fn_HabilitarBotones ();
}

function fn_LoginIncorrecto ()
{
	alert ("El e-mail y/o la contraseña son incorrectos.");

	document.getElementById ("txt_Login_Contrasena").value = "";
	document.getElementById ("txt_Login_Contrasena").focus ();
}

// Validamos que el string recibido como parámetro sólo contenga letras y números.
function fn_ValidarSoloLetrasNumeros (strTexto)
{
	var i;
	var caracter;

	for (i = 0; i < strTexto.length; i++)
	{
		codigo = strTexto.charCodeAt (i);
		
		// Rangos válidos: 48-57, 65-90 y 97-122.
		if ((codigo < 48 || codigo > 57) && (codigo < 65 || codigo > 90) && (codigo < 97 || codigo > 122))
			return false;
	}
	
	return true;
}

function fn_ValidarUnEmailValido (pvStr_Cadena)
{
	pvStr_Cadena = trim (pvStr_Cadena);
	
	var arroba = pvStr_Cadena.indexOf ("@");
	var i;
	
	if (arroba == -1)
		return false;
		
	var arroba2 = pvStr_Cadena.indexOf ("@", arroba + 1);
	
	if (arroba2 != -1)
		return false;
	
	if (arroba == 0)
		return false;
	
	if (arroba == pvStr_Cadena.length - 1)
		return false;
	
	for (i = 0; i < pvStr_Cadena.length; i++)
	{
		if (pvStr_Cadena.substr (i, 1) == " " || pvStr_Cadena.substr (i, 1) == ";" || pvStr_Cadena.substr (i, 1) == "," || pvStr_Cadena.substr (i, 1) == String.fromCharCode (34) || pvStr_Cadena.substr (i, 1) == String.fromCharCode (39))
			return false;
	}

	return true;
}

function fn_InvocarAjax_Post (archivo, parametros)
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest ();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert ("Su navegador no soporta AJAX.");
				return false;
			}
		}
	}
	
	xmlHttp.open ("POST", archivo, true);	
	xmlHttp.onreadystatechange = fn_ProcesarRespuestaAjax;
	xmlHttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader ("Content-length", parametros.length);
	xmlHttp.setRequestHeader ("Connection", "close");
	xmlHttp.send (parametros);
}
	
function fn_ProcesarRespuestaAjax ()
{
	var resultado;
	
	if (xmlHttp.readyState == 4)
	{
		resultado = fn_QuitarBasuraExtremos (xmlHttp.responseText);
		eval (resultado);
	}
}

function fn_QuitarBasuraExtremos (pvStr_Texto)
{
	var lvStr_Cadena = pvStr_Texto;
	var lvStr_Limpia = pvStr_Texto;
		
	if (lvStr_Cadena.length > 0)
	{
		for (i = 0; i < lvStr_Cadena.length; i++)
		{
			if (lvStr_Cadena.substr (i, 1) == " " || lvStr_Cadena.charCodeAt (i) == 10 || lvStr_Cadena.charCodeAt (i) == 13)
				lvStr_Limpia = lvStr_Cadena.substr (i + 1);
			else
				break;
		}
	
		lvStr_Cadena = lvStr_Limpia;
	
		for (i = lvStr_Cadena.length - 1; i >= 0; i--)
		{
			if (lvStr_Cadena.substr (i, 1) == " " || lvStr_Cadena.charCodeAt (i) == 10 || lvStr_Cadena.charCodeAt (i) == 13)
				lvStr_Limpia = lvStr_Cadena.substr (0, i);
			else
				break;
		}
	}
	
	return lvStr_Limpia;
}

function fn_ScrollTop ()
{
	if (window.pageYOffset)
		return window.pageYOffset;
	else
		return Math.max (document.body.scrollTop, document.documentElement.scrollTop);
}

function fn_ClientHeight ()
{
	if (window.innerHeight)
		return window.innerHeight;
	else
		return document.body.clientHeight;
}
