// v2.0 

function getAlfabeto(){
	return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
function getNumeros(){
	return "1234567890";
}
function getTildadas(){
	return "áàéèíìóòúùüñÁÀÉÈÍÌÓÒÚÙÜÑ";
}

function getTildadasDominio(){
	return "áéíóúüñ";
}

function lTrim(sString){
	while (sString.substring(0, 1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rTrim(sString){
	while (sString.substring(sString.length - 1, sString.length) == ' '){
		sString = sString.substring(0, sString.length - 1);
	}
	return sString;
}

function trim(sString){
	sString = lTrim(sString);
	sString = rTrim(sString);
	return sString;
}
//---------------------------------------------------------------
function filtraCaracteres( s, permitidos ){
	return buscaCharInvalido( s, permitidos );
}

function buscaCharInvalido( s, permitidos ){
	s = trim(s);
	for( var cont_string = 0; cont_string < s.length; cont_string++ ){
		if( permitidos.indexOf( s.charAt(cont_string) ) == -1 ){
			// ops... un caracter invalido.
			return true;
		}
	}
	// todo ok.
	return false;
}
//----------------------------------------------------------------


function tipoCharExistentes( texto, ch ){
	var encontrados = 0;
	for( cont = 0; cont < texto.length; cont++ ){
		if( texto.charAt(cont) == ch ){
			encontrados++;
		}
	}
	return encontrados;
}

function suprimeChar( texto, suprimir ){
	var new_string = "";
	var encontrado = false;
	texto = trim(texto);
	for( cont_texto = 0; cont_texto < texto.length; cont_texto++ ){
		for(cont_suprimir = 0; cont_suprimir < suprimir.length; cont_suprimir++ ){
			if( texto.charAt(cont_texto) == suprimir.charAt(cont_suprimir) ){
				encontrado = true;
			}
		}
		if( !encontrado ){
			new_string += texto.charAt(cont_texto);
		}else encontrado = false;
	}
	return new_string;
}
/*
function suprimeTilde( texto ){
	var texto2 = "";
	var ch = '';
	for( c = 0; c < texto.length; c++ ){
		ch = texto.charAt(c);
		alert(tipoCharExistentes("àáäâ",ch) + " : ");
		if( tipoCharExistentes("àáäâ",ch) > 0 ){
			texto2 += "a";
			alert(texto2);
		}else if( tipoCharExistentes("èéëê",ch) > 0 ){
			texto2 += "e";
		}else if( tipoCharExistentes("ìíïî",ch) > 0 ){
			texto2 += "i";
		}else if( tipoCharExistentes("òóöô",ch) > 0 ){
			texto2 += "o";
		}else if( tipoCharExistentes("ùúüû",ch) > 0 ){
			texto2 += "u";
		}else if( tipoCharExistentes("ÀÁÄÂ",ch) > 0 ){
			texto2 += "A";
		}else if( tipoCharExistentes("ÈÉËÊ",ch) > 0 ){
			texto2 += "E";
		}else if( tipoCharExistentes("ÌÍÏÎ",ch) > 0 ){
			texto2 += "I";
		}else if( tipoCharExistentes("ÒÓÖÔ",ch) > 0 ){
			texto2 += "O";
		}else if( tipoCharExistentes("ÙÚÜÛ",ch) > 0 ){
			texto2 += "U";
		}else texto2 += ch;
		//alert(texto2);
	}
	return texto2;
}
*/
//----------------------
//  VALIDADOR DE RUT ---
//----------------------
var blacklist = new Array(
"111111111"
);
function isRut( texto ){
	texto = suprimeChar( texto, ".-" );
	texto = texto.toUpperCase();
	
	for(var cont=0; cont < blacklist.length; cont++ ){
		if( texto == blacklist[cont] ){ return false }
	}
	//------------------------------
	var tmpstr = "";
	for ( i=0; i < texto.length ; i++ )		
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);	
			texto = tmpstr;	
			largo = texto.length;	

	if ( largo < 2 )	
	{		
		//alert("Debe ingresar el rut completo")		
		//window.document.Formulario.rut.focus();		
		//window.document.Formulario.rut.select();		
		return false;	
	}	

	for (i=0; i < largo ; i++ )	
	{			
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{			
			//alert("El valor ingresado no corresponde a un R.U.T valido");			
			//window.document.Formulario.rut.focus();			
			//window.document.Formulario.rut.select();			
			return false;		
		}	
	}	

	var invertido = "";	
	for ( i=(largo-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + texto.charAt(i);	
	var dtexto = "";	
	dtexto = dtexto + invertido.charAt(0);	
	dtexto = dtexto + '-';	
	cnt = 0;	

	for ( i=1,j=2; i<largo; i++,j++ )	
	{		
		//alert("i=[" + i + "] j=[" + j +"]" );		
		if ( cnt == 3 )		
		{			
			dtexto = dtexto + '.';			
			j++;			
			dtexto = dtexto + invertido.charAt(i);			
			cnt = 1;		
		}		
		else		
		{				
			dtexto = dtexto + invertido.charAt(i);			
			cnt++;		
		}	
	}	
	invertido = "";	
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + dtexto.charAt(i);	
	//window.document.Formulario.rut.value = invertido.toUpperCase()		
	if ( revisarDigito2(texto) )		
		return true;	
	return false;
}
function revisarDigito( dvr )
{	
	dv = dvr + ""	
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')	
	{		
		//alert("Debe ingresar un digito verificador valido");		
		//window.document.Formulario.rut.focus();		
		//window.document.Formulario.rut.select();		
		return false;	
	}	
	return true;
}
function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		//alert("Debe ingresar el rut completo")		
		//window.document.Formulario.rut.focus();		
		//window.document.Formulario.rut.select();		
		return false;	
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( dv );	

	if ( rut == null || dv == null )
		return 0	

	var dvr = '0'	
	suma = 0	
	mul  = 2	

	for (i= rut.length -1 ; i >= 0; i--)	
	{	
		suma = suma + rut.charAt(i) * mul		
		if (mul == 7)			
			mul = 2		
		else    			
			mul++	
	}	
	res = suma % 11	
	if (res==1)		
		dvr = 'k'	
	else if (res==0)		
		dvr = '0'	
	else	
	{		
		dvi = 11-res		
		dvr = dvi + ""	
	}
	if ( dvr != dv.toLowerCase() )	
	{		
		//alert("EL rut es incorrecto")		
		//window.document.Formulario.rut.focus();		
		//window.document.Formulario.rut.select();		
		return false	
	}

	return true
}
//--------------------------
//  FIN VALIDADOR DE RUT ---
//--------------------------

//------------------------
//  VALIDADOR DE EMAIL ---
//------------------------
function isEmail( email ){
	email = email.toLowerCase();
	email = trim(email);
	
	// Verifica caracteres invalidos 
	var caracteres = "abcdefghijklmnopqrstuvwxyz0123456789_-.@";
	if( buscaCharInvalido(email, caracteres) ){
		return false
	}
	
	// Verifica @
	if( tipoCharExistentes(email, "@") > 1 || tipoCharExistentes(email, "@") < 1 ){
		return false;
	}
	
	// Verifica .
	if( tipoCharExistentes(email, ".") < 1 ){
		return false;
	}
	// Verifica _-.@ en posiciones invalidas
	caracteres = "-._";
	var partes = email.split("@");
	var char_inicio = partes[1].charAt(0);
	var char_fin = partes[1].charAt(partes[1].length - 1);
	if( partes.length < 2 ){
		return false;
	}else if( tipoCharExistentes(partes[1], ".") > 2 ){
		return false;
	}else if( tipoCharExistentes(partes[1], "_") > 0 ){
		return false;
	}else if( email.charAt(0) == "." ){
		return false;
	}else if( tipoCharExistentes(caracteres, char_inicio) > 0 || tipoCharExistentes(caracteres, char_fin) > 0 ){
		return false;
	}
	// Verifica ".." en dominio.
	var cantidad = 0;
	for( cont = 0; cont < partes[1].length; cont++ ){
		if( partes[1].charAt(cont) == "." ){
			cantidad++;
		}else cantidad = 0;
		if( cantidad >= 2 ){ return false; }
	}
	// Verifica extencion
	var extencion = new Array("cl","com","net","info","org","pe","ja","es","ru","tk");
	var partes2 = partes[1].split(".");
	var ext = partes2[partes.length - 1];
	existe = false;
	for( cont = 0; cont < extencion.length; cont++ ){
		if( ext == extencion[cont] ){ existe = true; }
	}
	if(!existe){ return false; }
	
	// EMAIL VALIDO
	return true;
}
//----------------------------
//  FIN VALIDADOR DE EMAIL ---
//----------------------------

function valida(){
	var dominio=document.getElementById(dominio).value
	var extension=document.getElementById(extension).value
	var opcion1=document.getElementById(opcion1).value
	var opcion2=document.getElementById(opcion2).value
	var opcion3=document.getElementById(opcion3).value
	var nombre_contacto=document.getElementById(nombre_contacto).value
	var razon=document.getElementById(razon).value
	var rut=document.getElementById(rut).value
	var email=document.getElementById(email).value
	var fono=document.getElementById(fono).value
	var msn="";
	if(dominio=="")
		{
			msn= "Ingrese Dominio\n"	
		}
	if(extension=="")
		{
			msn=+"Ingrese Extension\n"	
		}
	if(opcion1=="")
		{
			msn=+"Ingrese Opcional 1\n"	
		}
	if(opcion2=="")
		{
			msn=+"Ingrese Opcional 2\n"	
		}
	if(opcion3=="")
		{
			msn=+"Ingrese Opcional 3\n"	
		}
	if(nombre_contacto=="")
		{
			msn=+"Ingrese Nombre de Contacto\n"	
		}
	if(razon=="")
		{
			msn=+"Ingrese Razon Social\n"	
		}
	if(rut=="")
		{
			msn=+"Ingrese Rut\n"	
		}
	if(email=="")
		{
			msn=+"Ingrese Email\n"	
		}
	if(fono=="")
		{
			msn=+"Ingrese Fono\n"	
		}
	if(msn=="")
		{
			document.form1.submit();
		}
	else{
			alert(msn);
		}
}
