﻿//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 vacio(q) 
{  
	for ( i = 0; i < q.length; i++ ) 
	{  
		if ( q.charAt(i) != " " )
		{  
			return false;
		}  
	}  
	return true;
}  
   
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)) 
	{		
		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 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)
		{
			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;
			}
		}
	}
