var archivoAjax = "./registro_procesar.php";

function fn_LimpiarCamposControl ()
{
	document.frm_Datos.txt_HacerConfirmar.value = "0";
	document.frm_Datos.txt_HacerSubirImagen.value = "0";		
}

function fn_BloquearBotones ()
{
	document.getElementById ("tablaExterior").style.cursor = 'wait';
	
	document.frm_Datos.btn_Confirmar.style.cursor = 'wait';
	document.frm_Datos.btn_Confirmar.disabled = true;

	document.frm_Datos.btn_Volver.style.cursor = 'wait';
	document.frm_Datos.btn_Volver.disabled = true;
}

function fn_HabilitarBotones ()
{
	document.frm_Datos.btn_Confirmar.style.cursor = 'pointer';
	document.frm_Datos.btn_Confirmar.disabled = false;

	document.frm_Datos.btn_Volver.style.cursor = 'pointer';
	document.frm_Datos.btn_Volver.disabled = false;

	document.getElementById ("tablaExterior").style.cursor = 'default';		
}

function fn_ValidarIngreso ()
{
	var formulario = document.frm_Datos;		

	if (trim (formulario.txt_Nombre.value) == "")
	{
		alert ("Debe ingresar su nombre y apellido.");
		formulario.txt_Nombre.focus ();
		return false;
	} 
	
	if (trim (formulario.txt_Email.value) == "")
	{
		alert ("Debe ingresar su e-mail.");
		formulario.txt_Email.focus ();
		return false;
	}		
	
	if (fn_ValidarEmailValido (formulario.txt_Email.value, false) == false)
	{
		alert ("El e-mail ingresado es inv" + String.fromCharCode (225) + "lido.");
		formulario.txt_Email.focus ();
		return false;
	}		
	
	if (trim (formulario.txt_Usuario.value) == "")
	{
		alert ("Debe ingresar un nickname.");
		formulario.txt_Usuario.focus ();
		return false;
	} 
	
	if (formulario.txt_BusquedaRapida.value != "" && formulario.txt_IdUbicacion.value == "")
	{
		alert ("La localidad ingresada no es válida. Por favor, escriba todo o parte del nombre de la localidad y selecciónela de la lista desplegable.");
		formulario.txt_BusquedaRapida.focus ();
		return false;
	}

	if (formulario.txt_IdUbicacion.value == "")
	{
		alert ("Debe ingresar su localidad.");
		formulario.txt_BusquedaRapida.focus ();
		return false;
	}

	if (trim (formulario.txt_Password1.value) == "")
	{
		alert ("Debe ingresar una contraseña.");
		formulario.txt_Password1.focus ();
		return false;
	} 

	if (trim (formulario.txt_Password2.value) == "")
	{
		alert ("Debe repetir la contraseña.");
		formulario.txt_Password2.focus ();
		return false;
	}

	if (trim (formulario.txt_Password1.value) != trim (formulario.txt_Password2.value))
	{
		alert ("La contraseña no coincide. Por favor vuelva a ingresarla.");
		formulario.txt_Password1.value = "";
		formulario.txt_Password2.value = "";
		formulario.txt_Password1.focus ();
		return false;
	}

	var pwdIngresada = trim (formulario.txt_Password1.value);

	if (pwdIngresada.length < 6)
	{
		alert ("La contraseña no puede tener menos de seis caracteres. Por favor vuelva a ingresarla.");
		formulario.txt_Password1.value = "";
		formulario.txt_Password2.value = "";
		formulario.txt_Password1.focus ();
		return false;
	}

	if (fn_ValidarSoloLetrasNumeros (pwdIngresada) == false)
	{
		alert ("La contraseña s" + String.fromCharCode (243) + "lo puede contener letras y n" + String.fromCharCode (250) + "meros. Por favor vuelva a ingresarla.");
		formulario.txt_Password1.value = "";
		formulario.txt_Password2.value = "";
		formulario.txt_Password1.focus ();
		return false;
	}		
	
	return true;
}

function fn_ListaParametrosAjax ()
{
	var formulario = document.frm_Datos;
	var lista = "";

	lista = lista + "txt_HacerConfirmar=" + formulario.txt_HacerConfirmar.value;

	lista = lista + "&txt_Nombre=" + fn_Escape (trim (formulario.txt_Nombre.value));
	lista = lista + "&txt_Email=" + fn_Escape (trim (formulario.txt_Email.value));
	lista = lista + "&txt_Usuario=" + fn_Escape (trim (formulario.txt_Usuario.value));
	lista = lista + "&txt_IdUbicacion=" + formulario.txt_IdUbicacion.value;
	lista = lista + "&txt_NombreArchivoFoto=" + fn_Escape (formulario.txt_NombreArchivoFoto.value);
	lista = lista + "&txt_Password1=" + fn_Escape (trim (formulario.txt_Password1.value));
	
	return lista;
}
	
function fn_Confirmar ()
{
	var formulario = document.frm_Datos;		

	if (fn_ValidarIngreso ())
	{
		fn_LimpiarCamposControl ();
		fn_BloquearBotones ();

		formulario.txt_HacerConfirmar.value = "-1";
		
		var parametrosAjax = fn_ListaParametrosAjax ();
		fn_InvocarAjax_Post (archivoAjax, parametrosAjax);
	}
}

function fn_KeyPress (evento)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode == 13)
		fn_Confirmar ();
}

function fn_AltaOk ()
{
	fn_BloquearBotones ();
	
	if (document.frm_Datos.txt_DesdeLoginPopUp.value == "-1")
		window.location.href = document.frm_Datos.txt_DestinoAvanceDesdeLoginPopUp.value;
	else
		window.location.href = "./registro_ok.php?" + document.frm_Datos.txt_QueryString.value;
}

function fn_UsuarioRepetido (itemRepetido)
{
	if (itemRepetido == "nickname")
	{
		alert ("El nickname ingresado corresponde al de otro usuario. Por favor, ingrese uno diferente.");
		document.frm_Datos.txt_Usuario.focus ();
	}
	else if (itemRepetido == "e-mail")
	{
		alert ("El e-mail ingresado corresponde al de otro usuario. Por favor, ingrese uno diferente.");
		document.frm_Datos.txt_Email.focus ();
	}		
	else
		alert ("Se produjo un error al intentar dar de alta el usuario.");
}

function fn_Volver ()
{
	fn_BloquearBotones ();
	window.location.href = document.frm_Datos.txt_DestinoRetorno.value;
}

function fn_Bloqueo_Input_File (evento)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode != 9 && evento.keyCode != 35 && evento.keyCode != 36 && evento.keyCode != 37 && evento.keyCode != 39)
	{
		alert ("Utilice el bot" + String.fromCharCode (243) + "n 'Examinar...' para seleccionar el archivo.");
		document.frm_Datos.txt_UploadImagen.blur ();
	}
}

function fn_ImagenSeleccionada ()
{
	var formulario = document.frm_Datos;

	fn_LimpiarCamposControl ();
	fn_BloquearBotones ();

	formulario.txt_HacerSubirImagen.value = "-1";

	formulario.action = "./registro_subirfoto.php";
	formulario.target = "iframeSubmit";
	formulario.submit ();
}

function fn_RefrescarImagen (imagen)
{
	var formulario = document.frm_Datos;
	
	// Validamos que el nombre de la imagen no contenga un '+'.	
	if (imagen != "")
	{
		for (i = 0; i < imagen.length; i++)
		{
			if (imagen.substr (i, 1) == "+")
			{
				alert ("El nombre de la Imagen no puede contener el caracter '+'.");
				imagen = "";
				break;
			}
		}
	}		
	
	formulario.txt_NombreArchivoFoto.value = imagen;
	
	if (imagen != "")
	{
		try
		{
			document.getElementById ("td_MuestraImagen").style.display = "table-cell";
		}
		catch (err)
		{
			document.getElementById ("td_MuestraImagen").style.display = "block";
		}

		document.getElementById ("td_UploadImagen").style.display = "none";
		
		formulario.txt_NombreArchivoFoto.value = imagen;
		formulario.img_Foto.src = "./fotosusuarios/" + imagen;
	}
	else
	{
		try
		{
			document.getElementById ("td_UploadImagen").style.display = "table-cell";
		}
		catch (err)
		{
			document.getElementById ("td_UploadImagen").style.display = "block";
		}

		document.getElementById ("td_MuestraImagen").style.display = "none";			
		
		formulario.txt_NombreArchivoFoto.value = "";
		formulario.img_Foto.src = "./imagenes/nada.gif";
	}
}

function fn_QuitarImagen ()
{
	fn_RefrescarImagen ("");
}
