// JavaScript Document
//para que el check funcione debe estar incluido en la pagina que es incluido, tambien la libreria prototype.
//Ivan Calderon para Artesistematico 2008.

//color del error
var error='#ff9999';
//color de correcto
var correcto='#009999';

function check(valor,div,tipo,valor2,div2){
	//check de anio
	if (tipo=='anio'){
		if (valor>=1900 && valor<=2090){
			document.getElementById(div).style.backgroundColor=correcto;
			i=i+1;
			ii=ii+1;
				if (ii==3){
				document.getElementById(valor2).style.backgroundColor=correcto;
				document.getElementById(valor2).innerHTML="OK";
				}
				else
				{
				document.getElementById(valor2).style.backgroundColor=error;
			    document.getElementById(valor2).innerHTML="Hay errores";	
				}
				
		}else{
			document.getElementById(div).style.backgroundColor=error;	
			
		}
	}
	//check que sea mes
	if (tipo=='mes'){
		if (valor>=1 && valor<=12 && valor.length==2){
			document.getElementById(div).style.backgroundColor=correcto;
			i=i+1;
			ii=ii+1;
			
			
		}else{
			document.getElementById(div).style.backgroundColor=error;
			document.getElementById(valor2).style.backgroundColor=error;
		}
	}
	//check que sea dia del mes
	if (tipo=='dia'){
		if (valor>=1 && valor<=31 && valor.length==2){
			document.getElementById(div).style.backgroundColor=correcto;
			i=i+1;
			ii=ii+1;
			
			
		}else{
			document.getElementById(div).style.backgroundColor=error;
			document.getElementById(valor2).style.backgroundColor=error;
		}
	}
	
	  
	
	
	//check que no venga vacio
	if (tipo=='vacio'){
		if (valor!="" && valor.length>2){
			document.getElementById(div).style.borderColor=correcto;
			//document.getElementById(div).innerHTML="OK";
			i=i+1;
		}else{
			document.getElementById(div).style.borderColor=error;
			//document.getElementById(div).innerHTML="Requerido";
		}
	}
	//check numero entero
	if (tipo=='entero'){
		if (!isNaN(valor) && valor>0){
			document.getElementById(div).style.borderColor=correcto;
			i=i+1;
		}else{
			document.getElementById(div).style.borderColor=error;
		}
	}
	// check direccion de mail valida
	if (tipo=='mail'){
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
			document.getElementById(div).style.borderColor=correcto;
			//document.getElementById(div).innerHTML="OK";
			i=i+1;
		}else{
			document.getElementById(div).style.borderColor=error;
			//document.getElementById(div).innerHTML="Mail no valido";
		}
	}
	//check numero entero
	if (tipo=='dni'){
		if (!isNaN(valor) && valor>1000000 && valor<100000000){
			document.getElementById(div).style.backgroundColor=correcto;
			//document.getElementById(div).innerHTML="OK";
			i=i+1;
		}else{
			document.getElementById(div).style.backgroundColor=error;
			//document.getElementById(div).innerHTML="DNI no valido";
		}
	}
	
	
	
	//check usuario existente
	if (tipo=='user'){
		
		var url = "newsletter/_AX_check_user.php";
	    var peticion = new Ajax.Request(url,
										{
										method: 'post',
										parameters: "valor="+valor,
										asynchronous: 'true',
										//onCreate : showLoader(div),
										onComplete: CheckUser
										}
										);	
			
	}
		//----------------------------------------------------------------------------
	//check password y re-password, que no este vacio , que tenga minimo de caracteres y que sean iguales
		if (tipo=='pass'){
			if (valor.length<6 || valor==""){
						document.getElementById(div).style.backgroundColor=error;
						document.getElementById(div2).style.backgroundColor=error;
					
						//alert("Password Min. 6 caracteres")
						
						}
		//primero checkeo que sean iguales
		if (valor==valor2)
			{
				
			if (valor.length>5 && valor!="")
				{
				document.getElementById(div).style.backgroundColor=correcto;
				document.getElementById(div2).style.backgroundColor=correcto;
				//document.getElementById(div).innerHTML="OK";
				//document.getElementById(div2).innerHTML="OK";
				i=i+1;
				}
				else
				{
				document.getElementById(div).style.backgroundColor=error;
				document.getElementById(div2).style.backgroundColor=error;
				}			
			}
			else
			{
			
			document.getElementById(div2).style.backgroundColor=error;
			//alert("No coinciden los passwords")
			}
		
	}
	
	
	
	
	
}
