var defaultEmptyOK = false
var checkNiceness = true;
var digits = '01234567899.,';
var lowercaseLetters = 'abcdefghijklmnopqrstuvwxyzáéëíóöúñüç .:,"-+\/ºª&()';
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉëÍÓÖÚÑÜÇ'";
var whitespace = " \t\n\r";
var phoneChars = "()-+ ";
var mMessage = "Error: No puede dejar este espacio vacio"
var pPrompt = "Error: ";
var pAlphanumeric = "Ingrese un texto que contenga solo letras y/o numeros";
var pAlphabetic   = "Ingrese un texto que contenga solo letras";
var pInteger = "Ingrese un numero entero, sin decimales";
var pNumber = "Ingrese un numero";
var pPhoneNumber = "Ingrese un número de teléfono";
var pEmail = "Ingrese una dirección de correo electrónico válida";
var pName = "Ingrese un texto que contenga solo letras, numeros o espacios";
var pNice = "No puede utilizar comillas aqui";
var pDate = "La fecha introducida no es correcta";
var pHora = "La hora introducida no es correcta. El formato es hh:mm";
var pDateX = "La fecha de inicio debe ser anterior a la fecha de finalización";
var pMoney = "Ingrese un número en formato de moneda p.e.: 1.234,56 €";
var pTope = "Ha excedido la longitud permitida para este campo";
var pDigit = "Introduzca sólo números";

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

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;
}


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);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}


function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}


function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

function isLetter (c)
{
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "999999"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       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;
}


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 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++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }
    return true;
}

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;
}


function isName (s)
{
    if (isEmpty(s)) 
       if (isName.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);
    
    return( isAlphanumeric( stripCharsInBag( s, whitespace ) ) );
}

function isDate (theField)
{
	if (isEmpty(theField)) 
       if (isDate.arguments.length == 1) return defaultEmptyOK;
       else return (isDate.arguments[1] == true);
	
var fech = /^(0[1-9]|[1-9]|[1-2][0-9]|3[0-1])(\/|\-)(0[1-9]|[1-9]|1[0-2])(\/|\-)(19[0-9][0-9]|20[0-1][0-9]|[0-9][0-9])$/
	
   	if (!(fech.test(theField) )) return false;
	else return true;
}



function isHora (theField)
{
	if (isEmpty(theField)) 
       if (isDate3.arguments.length == 1) return defaultEmptyOK;
       else return (isDate3.arguments[1] == true);
	
var fech = /^(0[0-9]|[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[0-9]|[0-5][0-9])$/
	
   	if (!(fech.test(theField) )) return false;
	else return true;
}


function isMoney (theField)
{
	if (isEmpty(theField)) 
       	if (isMoney.arguments.length == 1) return defaultEmptyOK;
       	else return (isMoney.arguments[1] == true);
	
	var money = /^\d{1,3}(((\.\d{3}){1,2})|\d{0,6})(\,\d{1,2})?\ *€?$/
	if (!(money.test(theField) )) return false;
	else return true;
}


function isDateX (s)
{
	if (isEmpty(s)) 
       if (isDateX.arguments.length == 1) return defaultEmptyOK;
       else return (isDateX.arguments[1] == true);

	var ini = (myform.fechaini.value);
	var fin = (myform.fechafin.value);
	uno = ini.indexOf("/");
	if ( uno > 0 ) {
	dos = ini.lastIndexOf("/");
    	dia = ini.substr(0, uno);
	mes = ini.substring(uno+1, dos);
	ano = ini.substring(dos+1, dos+5);
	uno1 = fin.indexOf("/");
	dos1 = fin.lastIndexOf("/");
    	dia1 = fin.substr(0, uno1);
	mes1 = fin.substring(uno1+1, dos1);
	ano1 = fin.substring(dos1+1, dos1+5);
}

	else {
	uno = ini.indexOf("-");
	dos = ini.lastIndexOf("-");
    	dia = ini.substr(0, uno);
	mes = ini.substring(uno+1, dos);
	ano = ini.substring(dos+1, dos+5);
	uno1 = fin.indexOf("-");
	dos1 = fin.lastIndexOf("-");
    	dia1 = fin.substr(0, uno1);
	mes1 = fin.substring(uno1+1, dos1);
	ano1 = fin.substring(dos1+1, dos1+5);
}
	
	var ini2 = new Date(ano, mes-1, dia);
	var fin2 = new Date(ano1, mes1-1, dia1);

	if ( ini2 > fin2 ) return false;
	else return true;	

}

function isTope(s)
{
	if (isEmpty(s)) 
	if (isTope.arguments.length == 1) return defaultEmptyOK;
	else return (isTope.arguments[1] == true);
	
	if ( s.length > 2000 ) return false;
	else return true;

}

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))
}

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;
}

function isNice(s)
{
        var i = 1;
        var sLength = s.length;
        var b = 1;
        while(i<sLength) {
                if( (s.charAt(i) == "\"") || (s.charAt(i) == "'" ) ) b = 0;
                i++;
        }
        return b;
}

function statBar (s)
{   window.status = s
}

function warnEmpty (theField)
{   theField.focus()
    alert(mMessage)
    statBar(mMessage)
    return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    statBar(pPrompt + s)
    return false
}


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 == isDate ) msg = pDate;
	if( theFunction == isHora ) msg = pHora;
	if( theFunction == isDateX ) msg = pDateX;
	if( theFunction == isMoney ) msg = pMoney;
	if( theFunction == isTope ) msg = pTope;
	if( theFunction == isDigit ) msg = pDigit;
    }
    
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

    if ((emptyOK == false) && (isEmpty(theField.value))) 
        return warnEmpty(theField);

    //if ( checkNiceness && !isNice(theField.value))
    //    return warnInvalid(theField, pNice);

    if (theFunction(theField.value) == true) 
        return true;
    else
        return warnInvalid(theField,msg);

}

function VerifyOne () {
    if( checkField( document.myform.cuota1, isDigit, false ) &&
	checkField( document.myform.cuota2, isDigit, true ) &&
	checkField( document.myform.cuota3, isDigit, true ) &&
	checkField( document.myform.cuota4, isDigit, true ) &&
	checkField( document.myform.cuota5, isDigit, true ) &&
	checkField( document.myform.apuesta, isDigit, false ) )
	document.myform.submit();
	
};