
function abrir (pag, nombre, w, h)
{
	posleft = (screen.availWidth / 2) - (w / 2);

	postop = (screen.availHeight / 2) - (h / 2);
	window.open(pag,nombre,'width=' + w +',height=' + h + ',alwaysRaised=1,resizable=0,personalbar=0,left=' + posleft + ',top=' + postop + ',scrollbars=0');
}



function isDecimal(s_potentialDecimal)  {
	return isNumber(s_potentialDecimal, 'true');
}

/**
	Is the provided string a legal number?

	PARAMETERS
		s_potentialNumber
			The string to analyze.  Required.
		bs_decimalAllowed
			Is the potential number allowed to be a decimal?  If 'true', then yes.  If 'false', no.

	RETURNS
		true
			If s_potentialNumber is a legal integer (regardless the value of bs_decimalAllowed).  A legal integer is a string that...
				...contains one or more zeros.  Note that 0 equals 0000 equals 0000000000 equals ...
				...starts with zero or one dashes (indicating negative), followed by one or more digits (0-9), where at least one is greater than zero.
			If bs_decimalAllowed is 'true' and s_potentialNumber is a legal decimal.  A legal decimal is a string that...
				...is an integer.
				...starts with zero or one dashes (indicating negative) followed by zero or more digits, followed by a decimal point ('.'), followed by *one* or more digits.

			(Note that s_potentialNumber is checked to be an integer first.  If it is, then true is returned, regardless the value of bs_decimalAllowed.  If the string is an integer, it is not checked, specifically, to see if it's a decimal.  Hence, the description of a legal decimal number says "a decimal point" instead of "zero or one decimal points".)

		false
			If otherwise.

	LEGAL EXAMPLES (both integers and decimals)
			-1
			0
			1
			-50
			8750328754
			00008750328754
			08750328754
			-487584758475235
			-0000000487584758475235
			-0487584758475235

	ILLEGAL EXAMPLES (both integers and decimals)
			4875847-58475235
			487584758475235-
			[EMPTY_STRING]
			a
			0-
			-0
			Not a number!!!
			Some 123 numbers 456
			123 456
			-0.0
			-.0
			-0.000000
			-.000000
			1.
			.

	LEGAL EXAMPLES (decimals, but only after it is determined that the value is *not* an integer)
			1.1
			1.000830847018374
			058763408562473.01837486564417308746
			.1
			.0
			.01
			-1.0
 **/
function isNumber(s_potentialNumber, bs_decimalAllowed)  {

	if(s_potentialNumber.length < 1)  {
		return false;
	}

	if(s_potentialNumber == '-')  {
		//A negative sign only makes no sense.
		return false;
	}

	if(/^-[0]+$/.test(s_potentialNumber))  {
		//Negative zero makes no sense.
		return false;
	}

	if(/^[0]+$/.test(s_potentialNumber))  {
		//Zero is a legal number:
		//0  ==  0000000000000  ==  000
		return true;
	}
	//It is definitely not zero.

	//^         The start of the line.
	//[-]{0,1}  Zero or one dashes.
	//[0-9]+    One or more of any digit.
	//$         The end of the line.
	if(/^[-]{0,1}[0-9]+$/.test(s_potentialNumber))  {
		//No matter what, this is valid.  This is a leagal integer
		//and decimal.
		return true;
	}
	//It's not an integer...

	if(bs_decimalAllowed == 'false')  {
		//...but it must be.
		return false;
	}
	//...and that's okay.  It might be a decimal

	//Due to precision:
	//10.0000000000001 is legal (12 zeros)
	//10.00000000000001 is not  (13 zeros)

	//^         The start of the line.
	//[-]{0,1}  Zero or one dashes.
	//[0-9]*    Zero or more digits.
	//[,]  		A decimal point.
	//[0-9]+    One or more digits.
	//$         The end of the line.
	if(/^[-]{0,1}[0-9]*[,][0-9]+$/.test(s_potentialNumber))  {
		//It is a "raw" decimal.  The only thing that would make
		//this illegal is if it were -0.0 or -.0, or -0.0000 or
		//-.0000 or ...

		return !(/^-0*[,]0+$/.test(s_potentialNumber));
	}

	//It is not a legal decimal number.
	return false;
}

// Funcion que valida field segun el formato indicado por indice.

// Busca caracteres no validos con el formato indicado.

function isInString(field,indice) {

	exp= new Array();

	exp[0]= /[^0-9a-zA-Z]/				//Numeros, letras mayusculas y minusculas

	exp[1]= /[^0-9a-z]/				//Numeros y letras minusculas

	exp[2]= /[\D]/					//Numeros

 	exp[3]= /[^\w,;.:ªº@#$%&áéíóúÁÉÍÓÚ \/\-\_ñÑ]/	//Numeros, letras mayusculas y minusculas y otros caracteres

	exp[4]= /[^\w\_]/				//Numeros, letras mayusculas y minusculas y _

	exp[5]= /[^0-9a-z\_]/				//Numeros, letras minusculas y _

	exp[6]= /[^\d\_]/				//Numeros y _



	return !(exp[indice].test(field));		//false si no coincide con el formato

}



/**********

Checks if an email is correct

Input: Mail to validate

Output : True if email is correct. False otherwise.

******** */

function isMail(sMail) {

//     var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i

//     return emailReg.test(_email);

	var Pos    = sMail.indexOf('@')

	var Period   = sMail.lastIndexOf('.')

	var Space    = sMail.indexOf(' ')

	var Length   = sMail.length - 1   // Array is from 0 to length-1

	var invalid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // invalid characters

	var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid characters

	var end = /\.[a-zA-Z]{2,3}$/; // end of email



	if ((Pos < 3) ||				// Must be atleast 3 characters before @ sign

		(Period <= Pos+1) ||        // Must be atleast one valid char btwn '@' and '.'

		(Period == Length ) ||      // Must be atleast one valid char after '.'

		(Space  != -1) ||           // No empty spaces permitted

		(invalid.test(sMail)) ||	// Mustn't contain invalid characters

		(!valid.test(sMail)) ||		// Must contain valid characters

		(sMail.search(end) == -1))  // Check email's end

	{  

	      return false;

	}

 	return true;

  }





// Checks if a character is numeric.

function isDigit(caracter) {

   return isInString(caracter,2)

}



// Checks is a string is numeric.

function isNumeric(cadena) {

   return isInString(cadena,2)

}



// Checks if a string is empty

function isEmpty(lstrcadena) {

   return (lstrcadena.length != 0)?false:true

}


//----------------------------------------------------------------------------

//	Function formatNIF

//	Input: NIF Field

//	OutPut:	True if correct, False otherwise

//----------------------------------------------------------------------------



function formatNIF(field) {

var nceros;

var ceros='';



	if (field.value.length >9)

		return false;

	nceros= parseInt(9 - field.value.length);

	

	field.value=field.value.toUpperCase();

	

	for (i=0;i<nceros;i++) {

		ceros+='0'; }

		

	if (isNaN(parseInt(field.value.charAt(0))))

		field.value = field.value.charAt(0) + ceros + field.value.substring(1,field.value.length)

    else

		field.value = ceros + field.value

		

	return (field.value.length != 9)?false:true;

}



//----------------------------------------------------------------------------

// Comprueba la letra que corresponde al dni correspondiente

//----------------------------------------------------------------------------

function NIFLetter(nif1)

{

   var cadena  = "";

   var letras='TRWAGMYFPDXBNJZSQVHLCKET'

   var NumDNI=0;

   var Indice=0;

   var divis = 0

   

   nif1 = nif1.toUpperCase();

   

   if( nif1.charAt(0) != 'X')	// Españoles

      cadena += nif1

   else 			// Extranjeros

      cadena += nif1.substring(1,11);

   

   

   correcto = cadena.charAt(cadena.length-1);

   correcto = correcto.toUpperCase();

   

   NumDNI = parseInt(cadena,10);

   

   divis = isNaN(parseInt(NumDNI/23,10))?0:parseInt(NumDNI/23,10);



   Indice = NumDNI - (23 * divis);

   

   if( (Indice >= 0) && (Indice < 24) ) {

      if( letras.charAt(Indice) == correcto) 

         return true

      else

         return false

   } 

   else

      return false;

}



function isEmpty(s)

{   return ((s == null) || (s.length == 0))

}


function isCreditCard(st) {

  // Encoding only works on cards with less than 19 digits

  if (st.length > 19)

    return (false);

    

  if (st.length < 12)    

    return (false);

	



  sum = 0; mul = 1; l = st.length;

  for (i = 0; i < l; i++) {

    digit = st.substring(l-i-1,l-i);

    tproduct = parseInt(digit ,10)*mul;

    if (tproduct >= 10)

      sum += (tproduct % 10) + 1;

    else

      sum += tproduct;

    if (mul == 1)

      mul++;

    else

      mul--;

  }



  if ((sum % 10) == 0)

    return (true);

  else

    return (false);

}



function isFecha(objName) 

{

//var strDatestyle = "US"; //Estilo de Fecha de Estados Unidos

var strDatestyle = "EU";  //Estilo de fecha Europeo

var strDate;

var strDateArray;

var strDay;

var strMonth;

var strYear;

var intday;

var intMonth;

var intYear;

var booFound = false;

var datefield = objName;

var strSeparatorArray = new Array("-"," ","/",".");

var intElementNr;

var err = 0;

var strMonthArray = new Array(12);

strMonthArray[0] = "Ene";

strMonthArray[1] = "Feb";

strMonthArray[2] = "Mar";

strMonthArray[3] = "Abr";

strMonthArray[4] = "May";

strMonthArray[5] = "Jun";

strMonthArray[6] = "Jul";

strMonthArray[7] = "Ago";

strMonthArray[8] = "Sep";

strMonthArray[9] = "Oct";

strMonthArray[10] = "Nov";

strMonthArray[11] = "Dic";

strDate = datefield.value;



if (strDate.length < 1) {

	return true;

}

for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {

	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {

		strDateArray = strDate.split(strSeparatorArray[intElementNr]);

		if (strDateArray.length != 3) {

			err = 1;

			return false;

		}

		else {

			strDay = strDateArray[0];

			strMonth = strDateArray[1];

			strYear = strDateArray[2];

		}

		booFound = true;

   	}

}

if (booFound == false) {

	if (strDate.length>5) {

		strDay = strDate.substr(0, 2);

		strMonth = strDate.substr(2, 2);

		strYear = strDate.substr(4);

   	}

}

if (strYear != null && strYear.length == 2) {

	intYear = parseInt(strYear, 10);

	if (intYear < 51)

		strYear = '20' + strYear;

	else

		strYear = '19' + strYear;

}

// US style

if (strDatestyle == "US") {

	strTemp = strDay;

	strDay = strMonth;

	strMonth = strTemp;

}

intday = parseInt(strDay, 10);

if (isNaN(intday)) {

	err = 2;

	return false;

}

intMonth = parseInt(strMonth, 10);

if (isNaN(intMonth)) {

	for (i = 0;i<12;i++) {

		if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {

			intMonth = i+1;

			strMonth = strMonthArray[i];

			i = 12;

   		}

	}

	if (isNaN(intMonth)) {

		err = 3;

		return false;

   	}

}

intYear = parseInt(strYear, 10);

if (isNaN(intYear)) {

	err = 4;

	return false;

}

if (intMonth>12 || intMonth<1) {

	err = 5;

	return false;

}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {

	err = 6;

	return false;

}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {

	err = 7;

	return false;

}

if (intMonth == 2) {

	if (intday < 1) {

		err = 8;

		return false;

	}

if (SaltaAnio(intYear) == true) {

	if (intday > 29) {

		err = 9;

		return false;

	}

}

else {

	if (intday > 28) {

		err = 10;

		return false;

	}

}

}

if (strDatestyle == "US") {

	datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;

}

else {

	//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;

	datefield.value = intday + "-" + intMonth + "-" + strYear;

}

return true;

}


//*************************************************************

//* Validar una fecha con formato (DD/MM/AAAA)

//*************************************************************

function bFechaValida(Fecha)

{

   var sDiaAux;

   var sMesAux;

   var sDia;

   var sMes;

   var sAnno;

   var iIniMes;

   var iFinMes;

   var iIniAnio;

   var iFinAnio;

   var iLongitud;

   var iPos;

   var iCar;

   

   // Si la fecha no se ha completado

   if (Fecha.length == 0)

      return(true); 



  // Inicializar variables



   iIniMes = 3;       // El mes comenzará en carácter 3

   iFinMes = 5;       // El mes finaliza antes del carácter 5

   iLongitud = 10;    // La longitud de la fecha será de 10 caracteres





   // Comprobar que todos los caracteres sean numéricos o "/"

   for (iPos = 0; iPos <= Fecha.length-1; iPos++)

   {

      iCar = Fecha.charAt(iPos);

      if ((iCar < "0" || iCar > "9") && (iCar !="/")) 

         return false;	

   }



   // Obtener el día

   sDiaAux = Fecha.substring (0,2);



   // El primer carácter del día no puede ser "/"

   if (sDiaAux.charAt(0) == "/")

      return false;



   // Si el segundo carácter del día es "/"

   if (sDiaAux.charAt(1) == "/")

   {

      // El día es menor de 10 y se le añade "0"

      sDia = "0" + sDiaAux.charAt(0);

      iIniMes = iIniMes - 1;

      iFinMes = iFinMes - 1;

      iLongitud = iLongitud - 1;

   }

   else

   {

      // El día es de dos caracteres



      // Si el tercer caracter no es "/"

      if (Fecha.charAt(2) != "/")

         return (false);



      sDia = sDiaAux;

   }



   // Si el día no es correcto

   if (sDia < "01" || sDia > "31")

      return (false);





   // Inicializar las posiciones de inicio y fin del año

   iIniAnio = iIniMes + 3;

   iFinAnio = iFinMes + 5;



   // Obtener el mes

   sMesAux = Fecha.substring (iIniMes,iFinMes);



   // El primer carácter del mes no puede ser "/"

   if (sMesAux.charAt(0) == "/")

      return false;



   // Si el segundo carácter del mes es "/"

   if (sMesAux.charAt(1) == "/")

   {

      // El mes es menor de 10 y se le añade "0"

      sMes = "0" + sMesAux.charAt(0);

      iIniAnio = iIniAnio - 1;

      iFinAnio = iFinAnio - 1;

      iLongitud = iLongitud - 1;

   }

   else

   {

      // El mes es de dos caracteres

      sMes = sMesAux;

   }



   // Si el mes no es correcto

   if (sMes < "01" || sMes > "12")

      return (false);



   // Si la fecha no se ha completado

   if (Fecha.length > iLongitud)

      return(false);



   // Obtener el año

   sAnno = Fecha.substring (iIniAnio,iFinAnio);



   // Ningún carácter del año puede ser "/"

   if ((sAnno.charAt(0) == "/") || (sAnno.charAt(1) == "/") || (sAnno.charAt(2) == "/") || (sAnno.charAt(3) == "/"))

      return false;



   // Si el año no es de 4 dígitos

   if (sAnno.length < 4)

	 return (false);



   // Validar si es un mes de 30 días

   if (((sMes == "04") || (sMes == "06") || (sMes == "09") || (sMes == "11")) && (sDia > "30"))

   {

      return (false);

   }

   else

   {

      // Si es febrero

      if ((sMes == "02"))

      {

         // El día no puede ser mayor que 29

         if (sDia > "29")

         {

            return (false);

         }

         else

         {

            // Si el día es 29

            if (sDia == "29")

            {

               // Debe ser un año bisiesto

               // Son bisiestos los divibles entre 4 pero no entre 100, pero sí entre 400



               resto = sAnno % 4;

               if (resto != 0)

               {

                  return (false);

               }

               else

               {

                  resto = sAnno % 100;

                  if (resto == 0)

                  {

                     resto = sAnno % 400;

                     if (resto != 0)

                     {

                        return (false);

                     }

                  }

               }

            }

         }

      } 

   }





   if (sAnno < "1900" | sAnno > "2078")



   {

    return (false);



   } 

   // La fecha es correcta

   return (sDia+"/"+sMes+"/"+sAnno);

}



