
function stringValido(szValor)
{
	szNuevo = "";
	for (i = 0; i < szValor.length; i++)
	{
		ch = szValor.substring(i, i+1);
		if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") || (ch >= "0" && ch <= "9") || (ch=="-") || (ch=="_") || (ch=="@") || (ch=="."))
		{
			szNuevo+=ch;
		}
	}
	if (szValor != szNuevo)
	{
		return(false);
	}
	return(true);
}


// Funcion que comprueba los datos pasados
function comprueba(valor,identificador) {
	if(identificador=='') { return false; }
	if(valor=='') { return true; }
	var longitud = valor.length;
	if (valor.length!=0) {
		switch(identificador)
		{
			case 'tel':
				var objRegExp  = /^[0-9]{9}$/;
				return objRegExp.test(valor);
			case 'num':
				var objRegExp  = /^[0-9]{1,}$/;
				return objRegExp.test(valor);

			case 'fec':
				var objRegExp = /^\d{2}(\-|\/|\.)\d{2}\1\d{4}$/;
				
				if(!objRegExp.test(valor))
				{
					return false;
				}
				else
				{
					if (valor.length != 10) return false ;
					
					var strSeparator = valor.substring(2,3);
					var arrayDate = valor.split(strSeparator);

					if ((isNaN (arrayDate [0])) || (isNaN (arrayDate [1])) || (isNaN (arrayDate [2]))) return false ;

					var mesOK ;
					var diaOK ;
					
					var arrayDays = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
					var intDay = parseInt(arrayDate[0],10);
					var intMonth = parseInt(arrayDate[1],10);
					var intYear = parseInt(arrayDate[2],10);
					var maxDays = arrayDays[intMonth];

					if ((intMonth <= 0) || (intMonth > 12))
					{
						mesOK = false;
					}
					else
					{
						mesOK = true;
						
						if (intDay > maxDays)
						{
							diaOK = false ;
						}
						else
						{
							if (intMonth != 2)
							{
								diaOK = true ;
							}
							else
							{
								if (((intDay == 29) && !((intYear % 4 == 0) && ((intYear % 100 != 0) || (intYear % 400 == 0)))))
								{
									diaOK = false ;
								}
								else
								{
									diaOK = true ;
								}
							}
						}
					}

					if (diaOK && mesOK) return true;
				}
				return false;
				
			case 'hor':
				var objRegExp = /^([0-2][0-3]:[0-5][0-9])|([0-1][0-9]:[0-5][0-9])$/;
				return objRegExp.test(valor);
			case 'cod':
				var objRegExp  = /^[0-9]{5}$/;
				return objRegExp.test(valor);
			case 'ema':
				/*
				var objRegExp2 = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi; 
				return objRegExp2.test(valor);
				*/
				return ((valor.indexOf ("@") != -1) && (valor.indexOf (".") != -1)) ;
			case 'eda':
				var objRegExp  = /^[0-9]{1,2}$/;
				return objRegExp.test(valor);

			case 'dni':
					objRegExpDNI = /^[0-9]{8}[a-zA-Z]{1}$/;
					objRegExpNIE = /^[xX][0-9]{8}[a-zA-Z]{1}$/;
					objRegExpCIF = /^[A-Za-z0-9]{9}$/;
					
					if ((!objRegExpDNI.test (valor)) 
						&& (!objRegExpNIE.test (valor))
						&& (!objRegExpCIF.test (valor))) return false ;
					
					//Miramos si es CIF o NIF/NIE
					if (/^[ABCDEFGHKLMNPQS]/.test(valor.charAt (0).toUpperCase ())){
						if (!/^[A-Za-z0-9]{9}$/.test(valor)) return false;
					    else if (!/^[ABCDEFGHKLMNPQS]/.test(valor.charAt (0).toUpperCase ())) return false;
						else return true;
					}else{
						bNIE = (valor.charAt (0).toUpperCase () == 'X' ? true : false) ;
						
						letras = "TRWAGMYFPDXBNJZSQVHLCKE" ;
						
						numero = valor.substring ((bNIE ? 1 : 0),(bNIE ? 9 : 8)) ;
						letra = valor.substring (valor.length - 1,valor.length).toUpperCase () ;
						
						return letra == letras.charAt (parseInt (numero,10) % 23) ;
					}
			case 'piva':
				if( valor.length != 11 )
					return false;
				var validi = "0123456789";
				for( i = 0; i < 11; i++ ){
					if( validi.indexOf( valor.charAt(i) ) == -1 )
						return false;
				}
				s = 0;
				for( i = 0; i <= 9; i += 2 )
					s += valor.charCodeAt(i) - '0'.charCodeAt(0);
				for( i = 1; i <= 9; i += 2 ){
					c = 2*( valor.charCodeAt(i) - '0'.charCodeAt(0) );
					if( c > 9 )  c = c - 9;
					s += c;
				}
				if( ( 10 - s%10 )%10 != valor.charCodeAt(10) - '0'.charCodeAt(0) )
					return false;
				return true;						
					
			case 'neg':
				var objRegExp  = /^-[0-9]{1,}$|^[0-9]{1,}$/;
				return objRegExp.test(valor);
			case 'dec':
				var objRegExp = /^[0-9]{1,}\.[0-9]{1,}$|^[0-9]{1,}$/;
				return objRegExp.test(valor);
			case 'ned':
				var objRegExp = /^-[0-9]{1,}\.[0-9]{1,}$|^[0-9]{1,}\.[0-9]{1,}$|^[0-9]{1,}$|^-[0-9]{1,}$/;
				return objRegExp.test(valor);
			case 'tar':
				return (checkcreditcard(valor));				
			default:
				return true;
		}

	}else{
		return false;
	}
}

function OpenWin(page)
{
  var w = 450;
  var h = 250;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  window.open(page,'ficha','scrollbars=yes,width=500,height=600,resizable=no');
}



/* Control de la tarjeta de credito */ 
function checkcreditcard(valor)
{
	var white_space = " -";
	var creditcard_string="";
	var check_char;

    if (valor.length == 0)
        return true;

	// squish out the white space
	for (var i = 0; i < valor.length; i++)
	{
		check_char = white_space.indexOf(valor.charAt(i))
		if (check_char < 0)
			creditcard_string += valor.substring(i, (i + 1));
	}	

	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	 
	 	
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!checkinteger(creditcard_string))
		return false;

    // now check mod10

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i))

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;

	return(true);
}

function checkinteger(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
		return checknumber(object_value);
    else
		return false;
}

function checknumber(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
}


function CCCValido(bancoIn,sucursalIn,controlIn,cuentaIn)
{

        peso= new Array (6,3,7,9,10,5,8,4,2,1);
        banco = bancoIn;
        sucursal = sucursalIn;
        control = controlIn;
        cuenta =cuentaIn;

        // verifica que ningun campo sea nulo:
        if ((banco.length==0 ) || (sucursal.length==0) || (control.length == 0) || (cuenta.length == 0)) return false;

        // verifica que todos los campos sean nuericos:
        if (!EsNumerico(banco) || !EsNumerico(sucursal) || !EsNumerico(control) || !EsNumerico(cuenta)) return false;

        // filtra la CCC 0 0 0 0:
        if (parseInt(bancoIn,10) == 0  &&  parseInt(sucursalIn,10) == 0 && parseInt(controlIn,10) == 0 && parseInt(cuentaIn,10) == 0 )
                return false;

        //--- Se calcula el digito de control para el par banco-sucursal

        suma=0;
        banco_sucursal=banco+sucursal;
        longitud_banco_sucursal = banco_sucursal.length;

        for(i=0; i<longitud_banco_sucursal; i++) {
                numero = banco_sucursal.charAt(longitud_banco_sucursal-i-1);
                suma = suma + parseInt(numero,10)*peso[i];
        }

        if ((suma % 11)!=0) {
                primer_digito = 11-(suma % 11);
        } else {
                primer_digito = 0;
        }

        //--- Se calcula el dígito de control para el número de cuenta
        suma=0;
        longitud_numero_cuenta = cuenta.length;

        for(i=0; i<longitud_numero_cuenta; i++) {
                numero = cuenta.charAt(longitud_numero_cuenta-i-1);
                suma = suma + parseInt(numero,10)*peso[i];
        }

        if ((suma % 11)!=0) {
                segundo_digito = 11-(suma % 11);
        } else {
                segundo_digito = 0;
        }

        //--- Se construyen los dígitos de control

				digitos_control=String(primer_digito).charAt(0)+String(segundo_digito).charAt(0);

        //--- Se comprueba si la cuenta es correcta
        return (digitos_control==control);

}

function EsNumerico (valor)
{
        return !(isNaN(valor));
}
