function isEmpty(s){
	return ((s == null) || (s.length == 0) || (Trim(s) == ""));
}

var whitespace = " \t\n\r";
function isWhitespace(s){
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}
	return true;
}

function LTrim(str){	
	while(str.length > 0 && str.charAt(0) == " "){
		str = str.substr(1,str.length)
	}

	return str
}

function RTrim(str){
	while(str.length > 0 && str.charAt(str.length-1) == " "){
		str = str.substr(0,str.length-1);
	}
	
	return str
}

function Trim(theStr){
	theStr = RTrim(theStr);
	theStr = LTrim(theStr);
	
	return theStr 
}

function isEmail(s){
	if (isEmpty(s)) 
		if (isEmail.arguments.length == 1) return true;
		else return (isEmail.arguments[1] == true);
	if (isWhitespace(s)) return false;
	
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")){
		i++
	}
	
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	
	while ((i < sLength) && (s.charAt(i) != ".")){
		i++
	}
	
	if ((i >= sLength - 1) || (s.charAt(i) != "."))	return false;
	else return true;
}


function checkContacto(theForm){
	var nombre,email,fono,nombre_empresa,mensaje
	
	if (typeof(theForm.nombre) == "undefined" || typeof(theForm.email) == "undefined" || typeof(theForm.nombre_empresa) == "undefined" || typeof(theForm.fono) == "undefined"){
		alert("Por favor, ingrese los campos correspondientes");
		return false;
	}
	
	nombre = theForm.nombre.value = Trim(theForm.nombre.value);
	email = theForm.email.value = Trim(theForm.email.value);
	fono = theForm.fono.value = Trim(theForm.fono.value);
	nombre_empresa = theForm.nombre_empresa.value = Trim(theForm.nombre_empresa.value);
	mensaje = theForm.mensaje.value = Trim(theForm.mensaje.value);
	
	if (nombre == null || nombre == ''){
		alert("Por Favor, ingrese su Nombre");
		theForm.nombre.focus();
		return false;
	}

	if (email == null || email == ''){
		alert("Por Favor, ingrese su e-mail");
		theForm.email.focus();
		return false;
	}

	if (!isEmail(email)){
		alert("El email ingresado no es v&aacute;lido, por favor ingr&eacute;selo en el formato casilla@dominio.cl");
		theForm.email.focus();
		return false;
	}
	

	if (fono == null || fono == ''){
		alert("Por Favor, ingrese su tel&eacute;fono de contacto");
		theForm.fono.focus();
		return false;
	}

	
	if (nombre_empresa == null || nombre_empresa == ''){
		alert("Por Favor, ingrese el nombre de la Empresa a la que pertenece");
		theForm.nombre_empresa.focus();
		return false;
	}
	
	if (mensaje == null || mensaje == ''){
		alert("Por Favor, ingrese el Mensaje que desea enviar");
		theForm.mensaje.focus();
		return false;
	}
	

	return true;
}
