var defaultEmptyOK = false
var Espacio = "\n";
var checkNiceness = true;
var whitespace = " \n\t\r";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑÜ.,:;";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü.,:;";
//var caracter = " _.,:/\\'ºª()@€+-*%='\n'";
var caracter = " .,:/\\\'ºª()@€+-='\n'";
var caracteresPermitidos = "abcdefghijklmnñopqrstuvwxyzáéíóúü ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÜ1234567890,.-+/\'ºª()@€:=<>";
var caracteresPermitidosDir = "abcdefghijklmnñopqrstuvwxyzáéíóúü ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÜ1234567890,.-+/\\'ºª()@€:=<>";
var phoneChars = "()-+ ";

// Mensajes
var pPrompt = "Error: ";
var mMessage = "No puede dejar este espacio vacío. Es un Campo obligatorio";
var pAlphabetic   = "Inserte un texto que contenga solo letras";
var pAlphanumeric = "Inserte un texto que contenga solo letras y/o numeros";
var pInteger = "Inserte un numero entero";
var pNumber = "Inserte un numero, para los decimales utilice la coma. (ej.:14444,99)";
var pEmail = "Inserte una dirección de correo electrónico válida ej.:contacto@metodopilates.com";
var pPhoneNumber = "Inserte un número de teléfono ej.:(+34) 902 15 21 33";
var pName = "Inserte un texto que contenga solo letras, numeros o espacios";
var pCaracter= "Inserte un texto que contenga solo letras, numeros, espacios o los siguientes caracteres:\n / \ . , ' º ª ( ) @ € + - =";
var pEntero = "Inserte un número entero";
var pCif = "Inserte un texto que contenga solo letras y/o numeros. Ej: B12345678";
var pEspacio = "No puede poner saltos de lineas";
var pCP = "Debe introducir un código postal válido.";
var pSelect = "Debe seleccionar una opción";

function checkField (theField, theFunction, emptyOK, s){
  var msg;
  if (checkField.arguments.length < 3)
    emptyOK = defaultEmptyOK;
  if (checkField.arguments.length == 4) {
    msg = s;
  }
  else {
    if(theFunction == isAlphabetic)
      msg = pAlphabetic;
    if(theFunction == isAlphanumeric)
      msg = pAlphanumeric;
    if(theFunction == isInteger)
      msg = pInteger;
    if(theFunction == isNumber)
      msg = pNumber;
    if(theFunction == isEmail)
      msg = pEmail;
    if(theFunction == isPhoneNumber)
      msg = pPhoneNumber;
    if(theFunction == isName)
      msg = pName;
    if(theFunction == isCaracter)
      msg = pCaracter;
    if(theFunction == isEntero)
      msg = pEntero;
    if(theFunction == isCif)
      msg = pCif;
    if(theFunction == isEspacio)
      msg = pEspacio;
    if(theFunction == isCP)
      msg = pCP;
		if(theFunction == isSelect)
			msg = pSelect;
  }
  if(theFunction == isCaracter){
    if((emptyOK == true) && (isEmpty(theField.value)))
      return true;
    if((emptyOK == false) && (isWhitespace(theField.value)))
      return warnInvalid(theField, mMessage);
    return validaCadena(theField.value, theField);
  }
	if(theFunction == isDireccion){
    if((emptyOK == true) && (isEmpty(theField.value)))
      return true;
    if((emptyOK == false) && (isWhitespace(theField.value)))
      return warnInvalid(theField, mMessage);
    return validaCadenaDir(theField.value, theField);
  }
  else{
    if ((emptyOK == true) && (isEmpty(theField.value)))
      return true;

    if ((emptyOK == false) && (isEmpty(theField.value)))
      return warnEmpty(theField,mMessage);

    if (checkNiceness && theFunction!=isCaracter && !isNice(theField.value))
      return warnInvalid(theField, pNice);

    if (theFunction(theField.value) == true)
      return true;
    else
      return warnInvalid(theField,msg);
  }
}

// Funciones que devuelven error

function warnEmpty (theField){
  theField.focus()
  alert(mMessage)
  statBar(mMessage)
  return false
}

function warnInvalid (theField, s){
  theField.focus()
	try{
  	theField.select()
	}
	catch(Exception){;}
  alert(s)
  statBar(pPrompt + s)
  return false
}


