//funciones de EldarBerserker XD o sea Erwin San Martin Lassalle XD

function objetoAjax()
{
	//alert('crea');
	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 cerrarSesion(pagina)
{
	sesion = objetoAjax();	
	sesion.open("GET", pagina);
	sesion.onreadystatechange = revisar;
	sesion.send('');
	
	function revisar()
	{
		if(sesion.readyState == 4)
		{
			document.location = 'index.php';
		}
	}
}

function iniciarSesion(div_id, formulario, pagina, abrir)
{
	divContenido = document.getElementById(div_id);
	rut = formulario.rut.value;
	passw = formulario.password.value;
	
	ajax=objetoAjax();
	ajax.open("POST", pagina, true);
	ajax.onreadystatechange = revisar_content;

	function revisar_content()
	{
		if(ajax.readyState == 1)
		{
			divContenido.innerHTML = '<div align="center"><div><img src="img/spinner.gif"><br><span class="resumen_noticia">Iniciando Sesi&oacute;n</span></div></div>';
		}
		
		if(ajax.readyState == 4)
		{
			respuesta = ajax.responseText;			
			
			//divContenido.innerHTML = ajax.responseText;
			if(respuesta.search(/correcto/) != -1)
			{
				document.location = abrir + '?panel';
				//divContenido.innerHTML = "sesión iniciada";
			}
			else if(respuesta.search(/especial/) != -1)
			{
				document.location = '/concursantes/index.php?panel';
				//divContenido.innerHTML = "sesión iniciada";
			}
			else
			{				
				document.location = abrir + '?' + respuesta;
			}		
		}
	} 	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send('rut=' + rut + '&password=' + passw)
	//ajax.send(null);
}


function vacio(q) 
{  
	for ( i = 0; i < q.length; i++ ) 
	{  
		if ( q.charAt(i) != " " )
		{  
			return true;
		}  
	}  
	return false  
}  
   
function validaNumeros(e)
{
	tecla = (window.event) ? window.event.keyCode : e.which;
	//alert (tecla);
	if(tecla == 8 || tecla == 0)
	{
		return true;
	}
	patron = /[1234567890]/;
	te = String.fromCharCode(tecla);
	return(patron.test(te));
}

function validaNumerosEspecial(e, texto)
{
	tecla = (window.event) ? window.event.keyCode : e.which;
	//alert (tecla);
	if(tecla == 8 || tecla == 0)
	{
		return true;
	}
	patron = /[1234567890,]/;
	te = String.fromCharCode(tecla);	
	//document.getElementById('sup_construida').value = posicion;
	if(te == ',')
	{		
		if(texto.indexOf(',') == -1)
		{			
			return(patron.test(te));
		}
		else
		{
			return false;
		}
	}
	else
	{
		return(patron.test(te));
	}
	
}

	function letrasRut(e)
	{
		tecla = (window.event) ? window.event.keyCode : e.which;
		//alert (tecla);
		if(tecla == 8 || tecla == 0)
		{
			return true;
		}
		patron = /[1234567890Kk]/;
		te = String.fromCharCode(tecla);
		
		return(patron.test(te));
	}

function validaMail(txt)
{         
	//expresion regular  
 	if(vacio(txt) == false) 
	{		
		return false;
	}
	else
	{
		var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
	 
	 if(!b.test(txt))
	 {
	 //comentar la siguiente linea si no se desea que aparezca el alert()  
		 //alert("E-mail " + (b.test(txt)?"":"no ") + "válido.")  
	 }
	 //devuelve verdadero si validacion OK, y falso en caso contrario  
		return b.test(txt);
	}
}

function validaFecha(txt)
{         
	//expresion regular  
 	if(vacio(txt) == false) 
	{		
		return false;
	}
	else
	{

		var b=/((\d{1,2})\/(\d{1,2})\/(\d{4,4}))/;
	 
		if(b.test(txt))
		{			
			ftxt = txt.split('/');
			
			if(parseInt(ftxt[0], 10) <= 0)
			{
				
				return false;
			}
			
			if(parseInt(ftxt[1], 10) <= 0)
			{				
				return false;
			}
			
			if(parseInt(ftxt[2], 10) <= 0)
			{
				return false;
			}
			
			return true;
		}
		else
		{
			return false;
		}
	}
}

function Disponibilidad(nombre, div_id, pagina, boton_id)
{
	divID = document.getElementById(div_id);
	boton = document.getElementById(boton_id);
	
	ajax = objetoAjax();	
	ajax.open("POST", pagina, true);
	ajax.onreadystatechange = revisar;
		
	function revisar()
	{
		if(ajax.readyState == 4)
		{
			respuesta = ajax.responseText;			
			if(respuesta.search(/disponible/) != -1)
			{
				divID.innerHTML = 'Disponible';
				divID.style.color = '#000000';
				divID.style.background = "#00ff02";
				boton.disabled = false;
			}
			else
			{
				divID.innerHTML = 'Utilizado(a)';
				divID.style.color = '#FFFFFF';
				divID.style.background = "#ff0909";
				boton.disabled = true;
			}		
		}
	}
	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send('rut=' + nombre)
	//ajax.send(null);
}

function valida_rut(rut, dv)
{		
	
	if(rut.length > 0 && parseInt(rut) != 0)
	{
		base = rut.split('.');
		if(base.length > 1)
		{
			base = base[0] + base[1] + base[2];
		}
		else
		{
			base = base[0];
		}
		mult = 2
		nume = 0;
		
		for(i = (base.length - 1) ; i >= 0; i--)
		{
			nume += base.charAt(i) * mult;
			
			if(mult == 7)
			{
				mult = 2;
			}
			else
			{
				mult++;
			}
		}
		
		resto = nume % 11;
		
		if(resto==1)
		{
			dive='K';
		}
		else if(resto == 0)
		{
			dive = 0;
		}
		else
		{
			dive = 11 - resto;
		}
		
		if(dv == 'k')
		{
			dv = 'K';
		}
		
		if(dv != dive)		
		{
			return false;
		}
		else
		{
			return  true;
		}
	}
	else
	{
		return false;
	}
}