// JavaScript Document

function validar_email1()
{
	
	$('#mensaje-enviar').html("");
	
// E-mail Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var value = document.getElementById('email').value;
	apos=  value.indexOf("@"); 
	dotpos=value.lastIndexOf(".");
	lastpos=value.length-1;
	document.getElementById('img-email').style.display = 'block';
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
	{
		//alert("Email no valido");
		//document.getElementById('email').select();
		//document.getElementById('email').focus();
		//document.getElementById('email').select();
		//document.getElementById('rolando').html("Email no valido.");
		document.getElementById('img-email').src= "../img/error.gif";		

		return false;
	}
	else
	{
		document.getElementById('img-email').src= "../img/ok.gif";
		return true;
	}	
}


function validar_nombre()
{
		$('#mensaje-enviar').html("");
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var valor = document.getElementById('nombre').value;
	document.getElementById('img-nombre').style.display = 'block';
	if (valor == "" || valor.length == 0 || /^\s+$/.test(valor) )
	{
		document.getElementById('img-nombre').src= "../img/error.gif";		
		return false;
	}
	else
	{
		document.getElementById('img-nombre').src= "../img/ok.gif";
		return true;
	}
} 

function validar_email()
{
	$('#error-email').html("");
	
	
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var valor = document.getElementById('email').value;
	document.getElementById('img-email').style.display = 'block';
	if (!validar_email1(valor))
	{
		
		return false;
	}
	else
	{
		var aleatorio = Math.round(Math.random()*1000);
		
			   $.ajax({
        type: "GET",
        url: "../ajaxs/validarusuario/"+valor+"/"+aleatorio,
		beforeSend: function(objeto){
//		
			document.getElementById('img-email').src= "../img/cargando1.gif";
        },
        success: function(datos){
			$('#img-cargando1-email').css("display","none");
			$('#img-email').css("display","block");
			if(datos==1){
				$('#error-email').css("display","none");
						document.getElementById('img-email').src= "../img/ok.gif";
						validado=true;
					
				}else
				{
						document.getElementById('img-email').src= "../img/error.gif";
						$('#error-email').css("display","block");
						$('#error-email').html("El email ya existe.");
						validado=false;
					}
			


      }
});
	
	}
	
	
} 

function validar_clave()
{
		$('#mensaje-enviar').html("");
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var valor = document.getElementById('clave').value;
	document.getElementById('img-clave').style.display = 'block';
	if (valor == "" || valor.length == 0 || /^\s+$/.test(valor) )
	{
		document.getElementById('img-clave').src= "../img/error.gif";		
		return false;
	}
	else
	{
		document.getElementById('img-clave').src= "../img/ok.gif";
		return true;
	}
}

function validar_rclave()
{
		$('#mensaje-enviar').html("");
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var valor = document.getElementById('rclave').value;
	var valor2 = document.getElementById('clave').value;
	document.getElementById('img-rclave').style.display = 'block';
	if(validar_clave())
	{
		if (valor == valor2 )	{
			document.getElementById('img-rclave').src= "../img/ok.gif";		
			return true;
		}
		else
		{
			document.getElementById('img-rclave').src= "../img/error.gif";
			return false;
		}
	}
	else
	{
		document.getElementById('img-rclave').src= "../img/error.gif";
		return false;
	}
	
	
	
	
} 

function validar_telefono()
{
		$('#mensaje-enviar').html("");
// Emptyfield Validation by Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove this line and the two lines above.
	var valor = document.getElementById('telefono').value;
	document.getElementById('img-telefono').style.display = 'block';
	if( !(valor.length>0) ) {
		document.getElementById('img-telefono').src= "../img/error.gif";	
		return false;
	}else{
		document.getElementById('img-telefono').src= "../img/ok.gif";	
		return true;
	}
} 


function mostrar_provincias(valor)
{
		$('#mensaje-enviar').html("");
	   $.ajax({
        type: "GET",
        url: "../ajaxs/ubi/0/"+valor,
		beforeSend: function(objeto){
			//$('#formulario').css("display","none");
           // $('#cargando').css("display","block");
		   $('#img-cargando1-prov').css("display","block");
		   	$('#provincias').html("");
			$('#distritos').html("");
			
        },
        success: function(datos){
				
				$('#img-cargando1-prov').css("display","none");
				
				$('#provincias').html(datos);
				$('#distritos').html("");
				mostrar_distritos(document.getElementById('provincias').childNodes[1].value);

      }
});
	   
} 

function mostrar_distritos(valor)
{
		$('#mensaje-enviar').html("");
	   $.ajax({
        type: "GET",
        url: "../ajaxs/ubi/1/"+valor,
		beforeSend: function(objeto){

            $('#img-cargando1-dist').css("display","block");
			$('#distritos').html("");
        },
        success: function(datos){
			$('#img-cargando1-dist').css("display","none");
			$('#distritos').html(datos);

      }
});
	   
} 

var validado=false;

function validar_usuario1(){

		return validado;
		
	}
function validar(){

var sw=true;

sw1=validar_nombre();
sw4=validar_clave();
sw5=validar_rclave();
sw6=validar_telefono();


sw=sw1 && sw4 && sw5 && sw6;

sw2=false;
if(sw){
	var valor = document.getElementById('email').value;
		if (!validar_email1(valor))
	{
		
		sw2=false;
	}
	else
	{
		var aleatorio = Math.round(Math.random()*1000);
		
			   $.ajax({
        type: "GET",
        url: "../ajaxs/validarusuario/"+valor+"/"+aleatorio,
		beforeSend: function(objeto){
//		
			document.getElementById('img-email').src= "../img/cargando1.gif";
        },
        success: function(datos){
			$('#img-cargando1-email').css("display","none");
			$('#img-email').css("display","block");
			if(datos==1){
				$('#error-email').css("display","none");
						document.getElementById('img-email').src= "../img/ok.gif";
						validado=true;
						sw2=true;
						document.forms[0].submit();	
					
				}else
				{
						document.getElementById('img-email').src= "../img/error.gif";
						$('#error-email').css("display","block");
						$('#error-email').html("El email ya existe.");
						validado=false;
					}
			


      }
});
	
	}

	
}else
{
	$('#mensaje-enviar').html("Verifique sus datos por favor.");	
		
}

	
	}