var ajax=null;
var nombreNivel =  new Array("","Baja","Media","Alta");
var ultimoNombreCuenta = "";


function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}



function nombreDisponible() {
	
	var nombreCuenta= document.getElementById('NombreCuenta');
	var caja = document.getElementById('errNombreCuenta');
	
	
	if (ultimoNombreCuenta != nombreCuenta.value) {
		ultimoNombreCuenta = nombreCuenta.value;
		caja.className = "noerr";
		
		if (isNombreCuentaValido(nombreCuenta.value)==false) {
			document.getElementById('txtNombreCuenta').innerHTML = "El nombre ingresado no es válido.";
		    caja.className = "err";
			return false;
		}
	
	
		var imgCargando = document.getElementById('imgCargando');
		var botonComprobar = document.getElementById('comprobar')
	
		botonComprobar.value = "Comprobando...";
		botonComprobar.disabled="disabled";
	
	
		imgCargando.className = "comprobando";
	
		ajax=nuevoAjax();	
	
		ajax.open("POST", "cuentas/nombredisponible.php");


		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		
		ajax.send("NombreCuenta="+nombreCuenta.value+ "&p=" + (Math.round((Math.random()*1000000)+1)));


		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				var resultado = ajax.responseText;
				
				imgCargando.className = "";
				botonComprobar.value = "Comprobar disponibilidad";
				botonComprobar.disabled="";
				if (resultado==1) {
					caja.className = "noerrOK";
					document.getElementById('txtNombreCuenta').innerHTML = "Nombre de cuenta disponible.";
					return true;
				} else {
					document.getElementById('txtNombreCuenta').innerHTML = "Ya existe el nombre de cuenta que deseas utilizar.<a href=\"g\">&iquest;Olvidaste tu contrase&ntilde;a?</a> ";
					caja.className = "err";
					return false;
				}
			}
		}
	}
}

function mostrarPoderClave(nombre,barra) {

	var nombre_barra='txt_'+barra;
	var barra = document.getElementById(barra);
	var txtBarra = document.getElementById(nombre_barra);
	var poder =  poderClave(nombre.value);

	txtBarra.innerHTML =  nombreNivel[poder];
	barra.style.width = 77 * poder + "px";
		
	return false;
}


function verificarFormulario() {
	
	botonEnviar = document.getElementById('Submit');
	botonEnviar.value = "Procesando...";
	
	var hayError = false;
	var primerFocus = null;
	
	campo = document.getElementById('NombreCuenta');
	resultado = document.getElementById('errNombreCuenta');
	
	if (isNombreCuentaValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	/*if (nombreDisponible() == false) {
		campo.className = "err";
	} else {
		campo.className = "input";
	}*/
	
	campo = document.getElementById('Password');
	resultado = document.getElementById('errPassword');
	
	if (isPassValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	pass = document.getElementById('Password').value;
	campo = document.getElementById('Password2');
	resultado = document.getElementById('errPassword2');
	
	if (campo.value == "") {
		document.getElementById('errorRePass').innerHTML = "Por favor, re-ingresa la contraseña."; 
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else if (pass != campo.value) {
		document.getElementById('errorRePass').innerHTML = "Las contraseñas no coinciden."; 
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
		
	campo = document.getElementById('Mail');
	resultado = document.getElementById('errMail');
	
	if (isMailValido(campo.value)==false)  {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	campo = document.getElementById('PIN');
	resultado = document.getElementById('errPIN');
	nivelSeguridadPIN = poderClave(campo.value);
	
	if (isPINValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		document.getElementById('errorPIN').innerHTML = "Por favor, ingres&aacute; una PIN v&aacute;lida."
		if (primerFocus == null) primerFocus = campo;
	} else if (nivelSeguridadPIN == 1) {
		resultado.className = "err";
		hayError = true;
		document.getElementById('errorPIN').innerHTML = "Demasiado f&aacute;cil de adivinar. Por favor, eleg&iacute; otra alternando n&uacute;meros y letras."; 
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	
	campo = document.getElementById('Nombre');
	resultado = document.getElementById('errNombre');
	
	if (isNombreApellidoValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}

	var dia = document.getElementById('dia').value;
	var ano = document.getElementById('ano').value;
	var mes = document.getElementById('mes').value;

	resultado = document.getElementById('errNacimiento');

	if (dia == 0 || mes == 0 || ano == 0) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	campo = document.getElementById('Provincia');
	resultado = document.getElementById('errProvincia');
	
	
	if (isGenericoValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	campo = document.getElementById('Localidad');
	resultado = document.getElementById('errLocalidad');
	
	if (isGenericoValido(campo.value)==false) {
		resultado.className = "err";
		hayError = true;
		if (primerFocus == null) primerFocus = campo;
	} else {
		resultado.className = "noerr";
	}
	
	
	campo = document.getElementById('acepto');
	
	if (campo.value == 0) {
		alert("Para crear una Cuenta Premium debe leer y aceptar los terminos y condiciones del servicio.");
		hayError = true;
	}
	
	botonEnviar.value = "Crear Cuenta";
	
	if (!hayError) {
		return true;
	} else {
		if (primerFocus != null) primerFocus.focus();
		return false;
	}
}

function mostrarAyuda(num) {
	document.getElementById(num).style.display="block";	
}

function ocultarAyuda(num) {
	document.getElementById(num).style.display="none";
}


//**************************************************
// check/uncheck

function check(chk)
{
	if 	(chk.className == "checkbox") {
		chk.className = "checkbox On"
		document.getElementById("acepto").value="1";
	} else {
		chk.className = "checkbox";
		document.getElementById("acepto").value="0";
	}
	
}

function chequearReIngreso() {
	var pass = document.getElementById('Password').value;
	
	if (pass != '') {
		var campo = document.getElementById('Password2');
		var resultado = document.getElementById('errPassword2');
	
		if (campo.value == "") {
			document.getElementById('errorRePass').innerHTML = "Por favor, re-ingresa la contraseña."; 
			resultado.className = "err";
		} else if (pass != campo.value) {
			document.getElementById('errorRePass').innerHTML = "Las contraseñas no coinciden."; 
			resultado.className = "err";
		} else {
			resultado.className = "noerr";
		}
	}
}
