
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; }
	longitud = valor.length;
	if (valor.length!=0) {
		switch(identificador)
		{
		case 'tel_it':
			var objRegExp  = /^[03]{1}[0-9]{9}$/;
			return objRegExp.test(valor);
		case 'tel':
			var objRegExp  = /^[69]{1}[0-9]{8}$/;
			return objRegExp.test(valor);
		case 'fijo':
			var objRegExp  = /^9[0-9]{8}$/;
			return objRegExp.test(valor);
		case 'movil':
			var objRegExp  = /^6[0-9]{8}$/;
			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 objRegExp = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/i; 
			return objRegExp.test(valor);
		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}$/;
			
			if ((!objRegExpDNI.test (valor)) && (!objRegExpNIE.test (valor))) return false ;
			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 '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));
}

function validarCIF (texto)
{ 
	var pares = 0; 
	var impares = 0; 
	var suma; 
	var ultima; 
	var unumero; 
	var uletra = new Array("J","A","B","C","D","E","F","G","H","I"); 
	var xxx; 
	 
	texto = texto.toUpperCase(); 
	 
	var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g); 
	if (!regular.exec(texto)) return false; 
	      
	ultima = texto.substr(8,1); 
	
	for (var cont = 1 ; cont < 7 ; cont ++)
	{ 
		xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0"; 
		impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
		pares += parseInt(texto.substr(cont,1)); 
	} 
	
	xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0"; 
	impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
	  
	suma = (pares + impares).toString(); 
	unumero = parseInt(suma.substr(suma.length - 1,1)); 
	unumero = (10 - unumero).toString(); 
	if(unumero == 10) unumero = 0; 
	  
	return ((ultima == unumero) || (ultima == uletra[unumero])) ;
} 
