

/**********************************/
/*            REGISTRO            */
/**********************************/

function ltrim(s) {     return s.replace(/^\s+/, ""); }  
function rtrim(s) {     return s.replace(/\s+$/, ""); } 

var registro = {
	verificarRUT: function(el) {
		var x = el;
		var el = $(el);
		
		if(el.val().length+1 > 8) {
			$('.loading').css('display', 'block').show();
			
			$.ajax({
				url: "/cgi-bin/ncommerce3/ExecMacro/Bazuca/Ajax/ValidaRut.d2w/report?rut="+el.val(),	//"comprobar.php?rut="+el.val()
				cache: false,
				success: function(data){
					//Si el RUT es incorrecto, despliega LB
					if(!Valida_Rut(x) && el.val().length > 8) {
						var mensajeError_X = "El RUT ingresado no es v&aacute;lido.";
						$('body').append('<div id="lb-loginError-X" class="lb oculto"><h2 class="centrado">'+mensajeError_X+'</h2></div>');
						$('#lb-loginError-X h2').css({ paddingBottom: 0 });
						$.vModal({ id: 'lb-loginError-X' });
						el.val('');
						
					} else {
						//Si el RUT ya existe en la DB, despliega LB
						if(el.val() == rtrim(ltrim(data))) {
							$.vModal({ id: 'lb-verificador' });
							el.blur();
							$(el).val('');
						}
						
						//De lo contrario hacer nada.
					}
					
					$('.loading').hide();
				}
			});	
		}
		
		registro.verificacion();
	},
	
	verificarFecha: function(el,vMin,vMax) {
		
		$(el).attr({ maxlength: vMin });

		$(el).blur(function() {
			$(el).attr({ maxlength: vMax });
		});
		
		borrarTexto(el);
	},
	
	verificarFormulario: function() {
		var error = null;
		error = Array();
		


		var rut = ($('#forma-registro input[name=shlogid]').val().length > 0)?true:error.push('No has puesto tu RUT');
		if (error.length==0) {
			var rut = ($('#forma-registro input[name=shlogid]').val().length < 12)?true:error.push('El RUT ingresado no es v&aacute;lido.');
		}
		if (error.length==0) {
			var tempRUT = Valida_Rut($('#forma-registro input[name=shlogid]')[0]);
			if(!tempRUT)
				var mensajeError_1 = error.push('El RUT ingresado no es v&aacute;lido.');
		}


		var dia = (($('#forma-registro select[name=fecha-de-nacimiento-dia]').val().length+1 > 2) && ($('#forma-registro select[name=fecha-de-nacimiento-dia]').val() <= 31))?true:error.push('D&iacute;a Incorrecto');
		var mes = (($('#forma-registro select[name=fecha-de-nacimiento-mes]').val().length+1 > 2) && ($('#forma-registro select[name=fecha-de-nacimiento-mes]').val() <= 12 ))?true:error.push('Mes Incorrecto');
		var anio = ($('#forma-registro select[name=fecha-de-nacimiento-anio]').val().length+1 > 4)?true:error.push('A&ntilde;o Incorrecto');
		if (dia && mes && anio)
			var fechavalida = (checkdate($('#forma-registro select[name=fecha-de-nacimiento-dia]').val(), $('#forma-registro select[name=fecha-de-nacimiento-mes]').val(), $('#forma-registro select[name=fecha-de-nacimiento-anio]').val()))?true:error.push('Fecha no valida');

		$('#forma-registro input[name=sdcomp]').val($('#forma-registro select[name=fecha-de-nacimiento-anio]').val()+$('#forma-registro select[name=fecha-de-nacimiento-mes]').val()+$('#forma-registro select[name=fecha-de-nacimiento-dia]').val());

		var sexo = ($('#sdgendr').val() != 'N')?true:error.push('No has seleccionado tu Sexo');


		var nombre = ($('#forma-registro input[name=safname]').val().length+1 > 1)?true:error.push('No has puesto tu Nombre');
		var paterno = ($('#forma-registro input[name=salname]').val().length+1 > 1)?true:error.push('No has puesto tu Apellido Paterno');
		var materno = ($('#forma-registro input[name=samname]').val().length+1 > 1)?true:error.push('No has puesto tu Apellido Materno');

		var email = ($('#forma-registro input[name=saemail1]').val().length+1 > 1)?true:error.push('No has puesto tu Email');	
		if (email)	{
			var email = (registro.verificarEmail($('#forma-registro input[name=saemail1]').val()))?true:error.push('Email Inv&aacute;lido');
		}

		var telefono = ($('#forma-registro input[name=saphone1]').val().length+1 > 1)?true:error.push('No has puesto tu Tel&eacute;fono');

		var clave = ($('#forma-registro input[name=shlpswd]').val().length+1 > 1)?true:error.push('No has puesto tu Clave');
		var reclave = ($('#forma-registro input[name=shlpswdver]').val().length+1 > 1)?true:error.push('No has confirmado tu Clave');
		if (clave && reclave)	{
			var validacion = ( $('#forma-registro input[name=shlpswd]').val() == $('#forma-registro input[name=shlpswdver]').val() )?true:error.push('Claves deben ser iguales');
		}

		if($('#sapkgsup').attr('checked'))
			$('#forma-registro input[name=sdfield6]').val('1001');
		else
			$('#forma-registro input[name=sdfield6]').val('0');


		var info = 'shlogid='+$('#forma-registro input[name=shlogid]').val()+'&shlpswd='+$('#forma-registro input[name=shlpswd]').val()
		var rep = $('#forma-registro input[name=t_url]').val().replace(/<info>/g, info);
		$('#forma-registro input[name=url]').val(rep);


//$('#forma-registro input[name=url]').val($('#forma-registro input[name=url]').val().replace("<id>", a));

		
		//if(rut && dia && mes && anio && nombre && paterno && materno && email && clave && reclave && preg && resp) {
		if(error.length==0) {
			$('.botonContinuar').unbind().click(function() {
				$('#forma-registro').submit();
				//window.location.href = $(this).attr('href');
			});
		} else {
			$('.botonContinuar').unbind().click(function() {
				var tempError = '';
				for(var i=0;i<error.length; i++) {
					tempError += '<li>'+error[i]+'</li>';	
				}
				
				$('#lightbox ul').html(tempError);
				
				$.vModal({ id: 'lightbox' });
			});
		}
	},
	
	verificarEmail: function(str) {
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	},
	
	verificacion: function() {
		registro.verificarFormulario();
		setTimeout("registro.verificacion()", 1000);
	},
	
	rutValido: function(evt) {
		var key = evt.keyCode ? evt.keyCode : evt.which ;
		return (key <= 31 || (key >= 48 && key <= 57) || key == 75 || key == 107); 
    }
}

//Funciones de /login
/*
function continuarLogin() {
	if(($('#rut').val().length >= 8) && ($('#pass').val().length > 2)) {
		createCookie('user', 'Juan Carlos', 10);
		return true;
		
	} else {
		var mensajeError = "El RUT y/o la contrase&ntilde;a ingresada son incorrectos. Favor verifica la informaci&oacute;n y vuelve a intentarlo.";
		$('body').append('<div id="lb-loginError" class="lb oculto"><h2 class="centrado">'+mensajeError+'</h2></div>');
		$('#lb-loginError h2').css({ paddingBottom: 0 });
		$.vModal({ id: 'lb-loginError' });
		
		return false;
	}	
}
*/
function continuarPasoA() {
	return ($('#rutA').val().length >= 8);
}

function continuarPasoB() {
	return ($('#recordar').val().length > 2);
}
