function chequear_form(){
	ok=1;
	errores = new Array();
	if(!unCheckbox()){ ok=0; errores.push("Solicite al menos un producto."); }
	if(!validacion_simple("Nombres","2")){ ok=0; errores.push("La casilla 'Nombres' no puede quedar vacía."); }
	if(!validacion_simple("Apaterno","2")){ ok=0; errores.push("La casilla 'Apellido Paterno' no puede quedar vacía."); }
	if(!validacion_numerica("Telefono","2")){ ok=0; errores.push("La casilla 'Número de red fija' no puede quedar vacía."); }
	if(!validacion_numerica("Movil","2")){ ok=0; errores.push("La casilla 'Número Móvil' no puede quedar vacía."); }
	if(!valida_mail("Email","")){ ok=0; errores.push("Por favor ingrese una dirección de Email válida."); }
	
	if(!validacion_simple("Nombres_contacto","2")){ ok=0; errores.push("La casilla 'Nombres' del formulario de contacto no puede quedar vacía."); }
	if(!validacion_simple("Apaterno_contacto","2")){ ok=0; errores.push("La casilla 'Apellido Paterno' de los datos de contacto no puede quedar vacía."); }
	if(!validacion_numerica("Telefono_contacto","2")){ ok=0; errores.push("La casilla 'Número de red fija' de los datos de contacto no puede quedar vacía."); }
	if(!validacion_numerica("Movil_contacto","2")){ ok=0; errores.push("La casilla 'Número Móvil' de los datos de contacto no puede quedar vacía."); }
	if(!valida_mail("Email_contacto","")){ ok=0; errores.push("Por favor ingrese una dirección de Email válida en los datos de contacto."); }
	
	if(!valida_rut('rut_empresa')) { ok=0; errores.push("El RUT ingresado no es correcto."); }
	
	if(ok==0){
		mostrar_errores(errores);
	}
	else {
		document.formulario.submit();
		//jsEnviar();
	}
}

function valida_rut(rut){
	var ok=0;
	var rutCompleto = document.getElementById(rut).value.split('-');
	var rut=rutCompleto[0];
	var dv=rutCompleto[1];

	var largo=rut.length;
	var suma=0;
	var mult=2;
	largo--;
	
	while(largo>=0) {
		suma=suma+(rut.charAt(largo)*mult);
		if(mult>6) { mult=2; }
		else { mult++; }
		largo--;
	}

	var resto=suma%11;
	var digito=11-resto;
	
	if(digito==10) { digito="k"; }
	if(digito==11) { digito=0; }
	
	if(!rut || !dv) { ok=0; }
	else if(digito!=dv) { ok=0; }
	else { ok=1; }
	
	return ok;
}

function validacion_simple(id,min_digitos){
	var ok=1;
	casilla=document.getElementById(id);
	
	if(min_digitos!=""){
		if(casilla.value.length<min_digitos) { ok=0; }
	}
	else{
		if(casilla.value.length<1) { ok=0; }
	}
	
	return ok;
}

function validacion_numerica(id,min_digitos){
	var ok=1;
	var patron=/\D/;
	casilla=document.getElementById(id);
	
	if(min_digitos!=""){
		if(casilla.value.length<min_digitos) { ok=0; }
	}
	if(casilla.value.length<1) { ok=0; }
	if(patron.test(casilla.value)) { ok=0; }
	
	return ok;
}

function validacion_alfabetica(id,min_digitos){
	var ok=1;
	var patron=/[^a-zA-Z \-áéíóúÁÉÍÓÚñÑ]/;
	casilla=document.getElementById(id);
	txt=casilla.value;
	
	if(min_digitos!=""){
		if(casilla.value.length<min_digitos) { ok=0; }
	}
	if(casilla.value.length<1) { ok=0; }
	if(patron.test(txt)) { ok=0; }
	return ok;
}

function valida_mail(id){
	casilla=document.getElementById(id);
	var ok=1;
	var es_email=/^(.+\@.+\..+)$/;
	if(!es_email.test(casilla.value)) { ok=0; }
	
	return ok;
}

function mostrar_errores(error){
	txt="Se han encontrado los siguientes errores:\n\n";
	for(i=0;i<error.length;i++){
		txt=txt+"- "+error[i]+"\n";
	}
	alert(txt);
}


function copiarValores(cb) {
	if (cb.checked) {
		document.getElementById('Nombres_contacto').value = document.getElementById('Nombres').value;
		document.getElementById('Apaterno_contacto').value = document.getElementById('Apaterno').value;
		document.getElementById('Telefono_contacto').value = document.getElementById('Telefono').value;
		document.getElementById('Movil_contacto').value = document.getElementById('Movil').value;
		document.getElementById('Email_contacto').value = document.getElementById('Email').value;
	}
}

function unCheckbox() {
	var els = document.getElementById('lineas-telefonicas').getElementsByTagName('INPUT');
	var uno = false;
	var i;
	for (i=0 ; i<els.length ; i++) {
		if (els[i].checked) { uno = true; }
	}
	return uno;
}







