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

function objetoAjax()
{
	//alert('crea');
	var XMLHttp=false;
	
	if(window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var versiones = ["Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP", "Microsof.XMLHTTP"];
		for (var i=0; i<versiones.length; i++)
		{
			try
			{
				XMLHttp = new ActiveXObject(versiones[i]);
				if(XMLHttp)
				{
					return XMLHttp;
					break;
				}
			}
			catch(e) {};
		}		
	}
}

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 validaCodigo(code)
{
	patron = /[^áéíóúàèìòùâêîôû ]/;
	for(i=0; i<code.length; i++)
	{
		l = code[i];
		//alert(l);
		res = patron.test(l)
		
		if(!res)
		{
			return false;
		}
	}
	return true;
}

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 validaFono(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
	{
		if(te == '(')
		{		
			if(texto.indexOf('(') == -1)
			{			
				return(patron.test(te));
			}
			else
			{
				return false;
			}
		}
		else
		{
			if(te == ')')
			{		
				if(texto.indexOf(')') == -1)
				{			
					return(patron.test(te));
				}
				else
				{
					return false;
				}
			}
			else
			{
				//alert(patron.test(te));
				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;
		}
	}
}
