//Esta variable indica si está bien dejar las casillas en blanco como regla general
var defaultEmptyOK = false
// listas de caracteres
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ"
var whitespace = " \t\n\r";
// caracteres admitidos en los de telefono
var phoneChars = "()-+ ";

// Verfifica si la variable esta vacia.
function isEmpty(s){
  return ((s == null) || (s.length == 0))
}

// Quita todos los caracteres que  estan en "bag" del string "s" s.
// Busca los caracteres definidos en bag en la cadena s y los saca.
function stripCharsInBag (s, bag){
   var i;
   var returnString = "";
// Buscar por el string, si el caracter no esta en "bag", agregarlo a returnString
    for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);		// Obtiene el caracter i de la cadena s. (Caracter en posicion i)
    if (bag.indexOf(c) == -1) returnString += c;//busca en la cadena bac el caracter c, si es -1 es porque no esta
    }
    return returnString;
}
//si esta vacio
function Vacio(pString)
{
  return ((pString == null) || (pString.length == 0));
}
//si es letra
// c es una letra del alfabeto espanol
function isLetter (c)
{
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) );
}
//si  c es un digito
// c es un digito
function isDigit (c){
   return ((c >= "0") && (c <= "9"))
}

// c es letra o digito
function isLetterOrDigit (c){
   return (isLetter(c) || isDigit(c))
}

// s es vacio o solo caracteres de espacio
function isWhitespace (s){
   var i;
   if (isEmpty(s)) return true;
   for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
// si el caracter en que estoy no aparece en whitespace, entonces retornar falso
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}
//si es un mail
// s es una direccion de correo valida
function isEmail (s){
    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       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;
}

// s es un numero entero (con o sin signo)
function isInteger (s){
   var i;
    if (isEmpty(s)) 
	  // Comprueba si la cantidad de parametros esta bien
      if (isInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isInteger.arguments[1] == true);
	  // --------------------------------------------------
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c)) return false;
        } else { 
            if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}


//si es un numero
// s es un numero (entero o flotante, con o sin signo)
function isNumber (s)
{   
    var i;
    var dotAppeared;
    dotAppeared = false;
    if (isEmpty(s)) 
       if (isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isNumber.arguments[1] == true);

       for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if( i != 0 ) {
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
              return false;
            }
			else     
                if (!isDigit(c)) return false;
        }
		else { 
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else     
                if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}
// s es numero de telefono valido
function isPhoneNumber (s){
   var modString;
    if (isEmpty(s)) 
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    modString = stripCharsInBag( s, phoneChars );
    return (isInteger(modString))
}
// s tiene solo letras
function isAlphabetic (s){
   var i;
   if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);
    for (i = 0; i < s.length; i++){
// Chequea si los caracteres de  la cadena son letras.
        var c = s.charAt(i);
        if (!isLetter(c))
          return false;
    }
    return true;
}

// notificar que el campo theField esta vacio
function avisoVacio (pControl, pMessage){ 
    pControl.focus();
    alert(pMessage);
    //statBar(mMessage)
    return false;
}

// notificar que el campo theField es invalido
function avisoInvalido (theField, s){
    theField.focus();
    theField.select();
    alert(s);
    //tatBar(pPrompt + s);
    return false;
}
// s tiene solo letras y numeros
function isAlphanumeric (s){
   var i;
   if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
           return false;
    }
    return true;
}
// s tiene solo letras, numeros o espacios en blanco
function isName (s){
	
    	if (isEmpty(s)) 			
	       if (isName.arguments.length == 1) return defaultEmptyOK;
       	   else return (isAlphanumeric.arguments[1] == true);
    	   return( isAlphanumeric( stripCharsInBag( s, whitespace ) ) );

}
/// s es un numero (entero o flotante, con o sin signo)
function isNumber (s)
{   var i;
    var dotAppeared;
    dotAppeared = false;
    if (isEmpty(s)) 
       if (isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isNumber.arguments[1] == true);
       for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if( i != 0 ) {
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
              return false;
            }else     
                if (!isDigit(c)) return false;
        }else { 
            if ( c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else     
                if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}

function checkField (theField, theFunction, emptyOK, s){
    var msg;

    if (checkField.arguments.length < 3) emptyOK = defaultEmptyOK;//me lleguen dos argumentos
    if (checkField.arguments.length == 4) {// si son 4 es s
        msg = s;//le llega un mensaje
    } 
	else 
	{
        if( theFunction == isAlphabetic ) msg = "Ingrese solo letras.";
        if( theFunction == isAlphanumeric ) msg = "Ingrese solo letras y numeros.";
        if( theFunction == isInteger ) msg = "Ingrese solo numeros.";
        if( theFunction == isNumber ) msg = "Ingrese solo numeros.";
        if( theFunction == isEmail ) msg = "Ingrese un email valido.";
        if( theFunction == isPhoneNumber ) msg = "Ingrese un numero de telefono valido.";
        if( theFunction == isName ) msg = "Ingrese un nombre valido.";
    }
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
//    if ((emptyOK == false) && (isEmpty(theField.value))) 
//        return avisoVacio(theField, "No se ha completado correctamente el Formulario.");
    if (theFunction(theField.value) == true) 
        return true;
    else
        return avisoInvalido(theField,msg);
}
/*
function validar() 
{
	var bolOK = false;

    if( checkField( document.curriculum.nombre, isName, false, "Apellido") &&
		checkField( document.curriculum.apellido, isName, false, "Nombre" ) &&
        checkField( document.curriculum.documento, isNumber, false, "Nro. de Documento") &&
        checkField( document.curriculum.telefono, isPhoneNumber, false, "Telefono" ) &&
        checkField( document.curriculum.email, isEmail, false, "Email" ))
		{
			alert( "Todo verificado con exito" );
			bolOK = true;
		}
	if (bolOK)
		document.curriculum.submit();
}*/
