/**
 * @author JalWeb
 */

//function to check valid email address
function esMail(mail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.getElementById(mail).value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) {
      return false;
    } 
    return true; 
}

   function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

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

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }

function esNum(num){
	return isInteger(num.value);
}

function validar() {
	
	error = 0;

	nombre = document.getElementById('nombre');
	asunto = document.getElementById('asunto');
	email = document.getElementById('email');
	telf = document.getElementById('telf');
	txt = document.getElementById('consulta');
	fallo = document.getElementById('error');
	
	if (nombre.value=='') {
		nombre.style.border='2px solid #f00'
		error = 1;
	}else {
		nombre.style.border='1px solid #000'
	}
	
	if (asunto.value=='') {
		asunto.style.border='2px solid #f00'
		error = 1;
	}else {
		asunto.style.border='1px solid #000'
	}
	
	if (email.value != '') {
		if (esMail('email')) {
			email.style.border='1px solid #000';
		}else {
			email.style.border='2px solid #f00'
			error = 1;
		}
	}else {
		email.style.border='1px solid #000'
	}
	
	if (telf.value.length != 9 || !esNum(telf)) {
		telf.style.border='2px solid #f00'
		error = 1;
	}else {
		telf.style.border='1px solid #000'
	}

	if (txt.value.length <= 5) {
		txt.style.border='2px solid #f00'
		error = 1;
	}else {
		txt.style.border='1px solid #000'
	}
	
	if (error) {
		fallo.style.display = 'block';
		return false;
	}else {
		return true;
	}
	
	
}