function enviarDatos() {
	var formulario = document.formContactenos;
    if( validaDatos() == false)
    	return;
	formulario.submit();
}

function validaDatos() {

	var formulario = document.formContactenos;
	
	if (Trim(formulario.nombre.value) == ""){
		alert('Debes ingresar el campo Nombre');
		formulario.nombre.focus();
		return false;
	}
	
	if (Trim(formulario.aPaterno.value) == ""){
		alert('Debes ingresar campo Apellido Paterno');
		formulario.aPaterno.focus();
		return false;
	}

	if (!formulario.rut.value){
		alert('Debes ingresar el campo RUT');
		formulario.rut.focus();
		return false;
	}
	
	if (!formulario.rutDv.value){
		alert('Debes ingresar el dígito que está después del guión en tu Rut');
		formulario.rutDv.focus();
		return false;
	}

    if(!checkRutField( formulario.rut.value+"-"+formulario.rutDv.value, formulario.rut ) ) {
		formulario.rut.focus();
		return false;
	}
	
	if (!formulario.email1.value){
		alert('Debes ingresar el campo e-mail');
		formulario.email1.focus();
		return false;
	}

	if (!formulario.email2.value){
		alert('Por favor ingresar tu dirección de email en el campo Confirmar E-mail');
		formulario.email2.focus();
		return false;
	}
	
	if (formulario.email1.value != formulario.email2.value) {
		alert("El e-mail y su confirmación deben ser iguales");
		return false;
	}
	
	// valido formato del email
	var formato = /^([\w-\.])+@([\w-]+\.)+([A-Z]){2,4}$/;
	var comparacion = formato.test(formulario.email1.value);
	if(comparacion == false){
		alert("El e-mail ingresado no es válido");
		return false;
	}
	
	if (!formulario.fonoDomicilio.value){
		alert('Debes ingresar el campo Teléfono(contacto)');
		formulario.fonoDomicilio.focus();
		return false;
	}
	
}

function validarEmail(valor){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = valor.value;
	
	if (strEmail.search(validRegExp) == -1) {
	    alert("La dirección de email es incorrecta o está mal escrita. Ingrésala nuevamente");
	    valor.focus();
	    return (false);
	}else
		return (true);
}

function validarEmailDominio(valor,dominio){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = valor.value + '@' + dominio.value;
	
	if (strEmail.search(validRegExp) == -1) {
	    alert("La dirección de email es incorrecta o está mal escrita. Ingrésala nuevamente");
	    valor.focus();
	    return (false);
	}else
		return (true);
} 


////////////////////////////////////////////////
function KeyIsNumber(evt){
var isNav = (navigator.appName.indexOf("Netscape") != -1)
var isIE = (navigator.appName.indexOf("Microsoft") != -1)

	if (isNav) {
		if ( ( evt.which >= 0 && evt.which <= 9 ) // Tab delete etc
            || (evt.which >= 48 &&  evt.which <=57) ) // [0-9]
		return true;
	return false;
	}
	else if (isIE)
		{evt = window.event;
		if ( (evt.keyCode >= 48 && evt.keyCode <= 57) )
			return true;
		return false;
		}
	else {
		alert("Su browser no es soportado por esta aplicación")
	}
	return false
}

function Trim( str ) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

function TrimRight( str ) {
	var resultStr = "";
	var i = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
			i--;
	
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
	}

	return resultStr; 
}

function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
		// Make sure the argument is a string
		str += "";
	if (str.length == 0) 
		resultStr = "";
	else { 
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;

		while ((i <= len) && (str.charAt(i) == " "))
		i++;
		// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
	}
	return resultStr;
}
