// Valida Form
 function mostrarcampos()
  {
   
	if (document.forms[0].chkbox.checked)
	//if (document.getElementById("chkbox").checked=true
    {
      document.getElementById("escondido").style.display = "block";
	  //alert("listo");
    }
    else
    {
      document.getElementById("escondido").style.display = "none";
    }
}
//-----------
function CheckForm () { 
    var dsp=6;
	var errorMsg = "";

	if (document.forms[0].rut.value == ""){
	errorMsg += "\n\t Rut\t\t- Escriba Rut";
	}

	if (document.forms[0].nombre.value == ""){
		errorMsg += "\n\t Nombre\t\t- Escriba Nombre";	
	}

	if (document.forms[0].apellido.value=="" ){
		errorMsg += "\n\t Apellido \t\t- Escriba Apellidos";
	}

	
	if (document.forms[0].email.value == ""){
		errorMsg += "\n\t Email \t\t- Escriba su Email de Contacto";
	}
	
	if (document.forms[0].phone.value == ""){
		errorMsg += "\n\t Telefono \t- Escriba su Telefono de Contacto";
	}
	
	
	

	if (document.forms[0].chkbox.checked==true){
		
		
		
		if (!document.forms[0].tp[0].checked &&!document.forms[0].tp[1].checked){
			errorMsg += "\n\t Tipo de Cliente \t- Persona Natural o Empresa";
	    	}
		
		if (document.forms[0].tp[1].checked){
	
				if (document.forms[0].rutemp.value == ""){
					errorMsg += "\n\t Rut Empresa \t- Escriba Rut Empresa";
	    		}
	  			if (document.forms[0].rsocial.value == ""){
					errorMsg += "\n\t Razon Social \t- Escriba Razon Social";
	    		}
			}
			
		if (document.forms[0].select1.value == ""){
		errorMsg += "\n\t Pais \t\t- Seleccione Pais";
	    }
		if (document.forms[0].direccion.value == ""){
		errorMsg += "\n\t Direccion \t- Escriba direccion";
	    }
	
	    if (document.forms[0].select2.value == ""){
		errorMsg += "\n\t Region \t\t- Seleccione Region";
	    }
	    if (document.forms[0].select3.value == ""){
		errorMsg += "\n\t Comuna \t\t- Seleccione Comuna";
	    }
	    if (document.forms[0].giro.value == ""){
		errorMsg += "\n\t Giro \t\t- Escriba Giro";
	    }
		
	 }
		if (document.forms[0].chkbox.checked==false)
	 {
		  document.forms[0].chkbox.value = "";
		  
		  document.forms[0].rsocial.value = "";
		  document.forms[0].select1.value = "";
		  document.forms[0].select2.value ="";
		  document.forms[0].select3.value = "";
		  document.forms[0].direccion.value = "";
		  document.forms[0].giro.value = "";
		
		 }


	if (document.forms[0].mensaje.value == ""){
		errorMsg += "\n\t Mensaje \t\t- Escriba su Mensaje o Sugerencia";
	}
   if (document.forms[0].uword.value == ""){
		errorMsg += "\n\t Codigo de Imagen \t- Ingrese el Codigo de Seguridad de la imagen ";
	}


		
	//En condoro muestra error
	if (errorMsg != ""){
		msg = "Estimado Cliente(a)\n\n";
		msg += "Su ingreso no ha sido procesado por no llenar datos obligatorios.\n";
		msg += "Porfavor corriga el problema y Registre la informacion.\n";
		msg += "______________________________________________________________\n\n";
		msg += "Los siguientes datos necesitan ser llenados: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	
	return true;
}

///
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.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
		window.document.form1.rut.focus();		
		window.document.form1.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.form1.rut.focus();		
		window.document.form1.rut.select();		
		return false	
	}

	return true
}

function Rut(texto)
{	
	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.form1.rut.focus();		
		window.document.form1.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.form1.rut.focus();			
			window.document.form1.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.forms[0].rut.value = invertido.toUpperCase()		

	if ( revisarDigito2(texto) )		
		return true;	

	return false;
}

function tipo(){
	if(document.forms[0].tp[1].checked){
			//alert(document.forms[0].tp[1].value);
			document.forms[0].persona.value="EMP";
			document.forms[0].rutemp.disabled=false;
			document.forms[0].rsocial.disabled=false;
			document.forms[0].rutemp.focus();
         }
		 
		 if(document.forms[0].tp[0].checked){
			//alert(document.forms[0].tp[0].value);
			document.forms[0].persona.value="PNA";
			document.forms[0].rutemp.disabled=true;
			document.forms[0].rutemp.value="";
			document.forms[0].rsocial.disabled=true;
			document.forms[0].rsocial.value="";
		
         }
	}
