function apagaCapa (idcapa)

{

	if (document.getElementById(idcapa) != null)

	{

		capa = document.getElementById(idcapa);

		capa.style.display = "none";

	} 

}



function cerrarTodasCapas (num_capas, inicio)

{

	for (var i=0; i<num_capas; i++) { apagaCapa(inicio+i); } 

}



function enciendeCapa (idcapa)

{

	if (document.getElementById(idcapa) != null)

	{

		capa = document.getElementById(idcapa);

		capa.style.display = "block";

	} 

}



function estaEncendidaCapa (idcapa)

{

	if (document.getElementById(idcapa) != null)

	{

		capa = document.getElementById(idcapa);

		if (capa.style.display=="block") return (true);

		else return (false);

	} else return(false);

}



function cambiarEstadoCapa (num_capas, inicio, num_capa)

{

	var enciende = true;

	if (document.getElementById(inicio+num_capa).style.display == "block") enciende = false;

	cerrarTodasCapas (num_capas, inicio); 

	if (enciende) enciendeCapa (inicio+num_capa);

}


function compararFecha (fechaBase, fechaComporada)
{
	// -1 - MENOR
	//  0 - IGUAL
	//  1 - MAYOR 
	var i=0;
	
	// Ano
	if (fechaBase.getFullYear() > fechaComporada.getFullYear()) { return (-1); }
	else if (fechaBase.getFullYear() == fechaComporada.getFullYear())
	{
		
		// Mes
		if (fechaBase.getMonth() > fechaComporada.getMonth()) return (-1);
		else if (fechaBase.getMonth() == fechaComporada.getMonth())
		{
			// Día
			if (fechaBase.getDate() > fechaComporada.getDate()) return (-1);
			else if (fechaBase.getDate() == fechaComporada.getDate()) return (0);
			else return (1);
			// Fin día		
		}
		else return (1);
		// Fin mes
	}
	else { alert (fechaBase.getFullYear()+''+fechaComporada.getFullYear()); return (1); }
	
}


function crearPantallaCargando ()

{

	document.write ('<div bgcolor="#FFFFFF" id="capacargando" name="capacargando" style="z-index: 200; width:100%; height:100%; display: none; position: absolute; bottom:0px; left: 0px; top: 0px;">');

	document.write ('<table bgcolor="#FFFFFF" width="950" height="100%" align="center">');

	document.write ('	<td align="center" valign="middle">');

	document.write ('	Enviando ...');

	document.write ('	</td>');

	document.write ('</table>');

	document.write ('</div>');

}



function activarPantallaCargando ()

{

	enciendeCapa ('capacargando');

}



function desActivarPantallaCargando ()

{

	apagaCapa ('capacargando');

}





function elemento (nombre)

{

	return document.getElementById (nombre);

}



function comprobarArrayCheckbox (formulario)

{

	var valordevuelto = false;	
	for( i=0; ele=elemento(formulario).elements[i];i++)
		if(ele.type=='checkbox')
		{
    	  if (comprobarCampo (ele)) valordevuelto = true; 
		}
	return (valordevuelto);

}


function comprobarCampo (campo)

{

	var valordevuelto = false;

	

	if (campo != null)

	{	

		for (var i=0; i < campo.length; i++) 

		{

			if (campo[i].checked)  valordevuelto = true; 

		}

		
		if (campo != null) 
		{
			if (campo.type=='checkbox') { if (campo.checked) valordevuelto = true; }
			else if ((campo.value!='') || (campo.type=="submit") || (campo.type=="hidden") || (campo.type=="file")) valordevuelto = true;

		}	
		
	}

	return (valordevuelto);

}



function comprobarTodoFormulario(formulario)

{

	var valorDevuelto = true;

	{

		for (var i=0; i<formulario.length; i++)

		{

			if (!comprobarCampo(formulario.elements[i])) 

			{

				valorDevuelto = false;

			}

		}

	}

	if (!valorDevuelto) alert ('Debe rellenar todos los campos');

	return (valorDevuelto);

}



function cambiarFondoCss (elemento, fondo)

{

	elemento.style.backgroundImage = "url("+fondo+")";

}



function cambiarImagen  (id, imagen_nueva)

{

	elemento (id).src = imagen_nueva;

}



// ---------------------PREVIEWS

 



function crearPreview ()

{

	document.write ('<div id="capapreview" name="capapreview" style="z-index: 50; width:100%; height:100%; display: none; position: absolute; bottom:0px; left: 0px; top: 0px;">');

	document.write ('<table onClick="javascript: apagaCapa(\'capapreview\')" width="100%" height="100%" background="script/fondo_semi.gif" align="center">');

	document.write ('	<td align="center" valign="middle">');

	document.write ('	<table><td align="center" valign="middle"><a href="javascript: apagaCapa(\'capapreview\')">');

	document.write ('	<img src="" name="imagenPreview" id="imagenPreview" style="border:10px;"></a>&nbsp;&nbsp;<br/><br/><td></table>');

	document.write ('</td></table>');

	document.write ('</div>');

}



crearPreview ();



function mostrarPreview (imagen)

{

	enciendeCapa ('capapreview');

	elemento('imagenPreview').src = imagen;
	
	alto = elemento('imagenPreview').height;
	ancho = elemento('imagenPreview').width;
	if (alto > 500)
	{
		elemento('imagenPreview').height = 500;
		ancho *= (500/alto);
	}
	if (ancho > 900)
	{
		elemento('imagenPreview').width = 900;
		alto *= (900/ancho);

	}
	elemento('imagenPreview').width = ancho;
}


// -------------------------- FIN PREVIEWS


function validarEmail (nombre_campo)

{

	if (!comprobarCampo (elemento(nombre_campo))) return false;

	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

	if (elemento(nombre_campo).value.length == 0 ) return false;
	if (filter.test(elemento(nombre_campo).value)) return true;

	else elemento(nombre_campo).focus();

	return false;

} 



function validarNIF (nombre_campo)

{
	if (!comprobarCampo (elemento(nombre_campo))) return false;

	abc=elemento(nombre_campo).value;

	dni=abc.substring(0,abc.length-1);

	let=abc.charAt(abc.length-1);

	if (!isNaN(let)) return false;

	else

	{

		cadena="TRWAGMYFPDXBNJZSQVHLCKET";

		posicion = dni % 23;

		letra = cadena.substring(posicion,posicion+1);

		if (letra!=let.toUpperCase())

		{

	    	elemento(nombre_campo).focus();

	    	return false;

		}

	 }

	return true;

}


function validarNIFCIF (nombre_campo)

{
	if (!comprobarCampo (elemento(nombre_campo))) return false;

	cif=elemento(nombre_campo).value;
	par = 0
	non = 0
	letras="ABCDEFGHKLMNPQS"
	let=cif.charAt(0)
	if (!isNaN(let)) return validarNIF(nombre_campo);

	if (cif.length!=9) return false; 
	if (letras.indexOf(let.toUpperCase())==-1) return false;
	
	for (zz=2;zz<8;zz+=2) par = par+parseInt(cif.charAt(zz))
	for (zz=1;zz<9;zz+=2)
  	{
		nn = 2*parseInt(cif.charAt(zz))
		if (nn > 9) nn = 1+(nn-10)
		non = non+nn
	}

	parcial = par + non
	control = (10 - ( parcial % 10))
	if (control==10) control=0

	if (control!=cif.charAt(8)) return false;
  	else return true;
}


function validarNumeroCuenta (nombre_campo)

{

	Pesos= new Array(6,3,7,9,10,5,8,4,2,1);

	var result ='';

	var iTemp =0;

	banco = 		elemento(nombre_campo+'1').value + elemento(nombre_campo+'2').value;

	digitocontrol =	elemento(nombre_campo+'3').value;

	cuenta = 		elemento(nombre_campo+'4').value;

	

	if (!(banco != '') && (digitocontrol != '') && (cuenta != '')) return false;

	 

	for (var n=0;n<=7;n++) iTemp  = iTemp + banco.substr(7 - n, 1) * Pesos[n];

	result = 11 - iTemp % 11;

	if (result > 9) result=1-result % 10;

	iTemp=0;

	for (var n=0;n<=9;n++)  iTemp  = iTemp + cuenta.substr(9 - n, 1) * Pesos[n];

	iTemp =11 - (iTemp % 11);

	if (iTemp > 9) iTemp =1-(iTemp % 10);

    result=result*10+iTemp;

	if (result != parseInt(digitocontrol))	return(false);

	else elemento(nombre_campo).value = elemento(nombre_campo+'1').value+"-"+elemento(nombre_campo+'2').value+'-'+digitocontrol+'-'+cuenta;

	

	return(true);

}


function comprobarFechaMayorActual (ano, mes, dia)
{
	var fecha=new Date();
	if (ano > fecha.getFullYear()) return (true);
	else if (ano < fecha.getFullYear()) return (false);
	else
	{
		if (mes > fecha.getMonth()+1) return (true);
		else if (mes < fecha.getMonth()+1) return (false);
		else
		{
			if (dia >= fecha.getDate()) return (true);
			else if (dia < fecha.getDate()) return (false);
		}
	}							
}

function mostrarFlash (nombre, tamX, tamY, flashvars)
{
	nombre	= nombre.substr(0, nombre.length-4);
	if (AC_FL_RunContent == 0) 
	{
		alert('Esta página requiere el archivo AC_RunActiveContent.js.');
	} 
	else 
	{
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/swflash.cab#version=9,0,0,0',
			'width', tamX,
			'height', tamY,
			'src', nombre,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'noorder',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', nombre,
			'name', nombre,
			'menu', 'true',
			'flashvars', flashvars,
			'allowFullScreen', 'true',
			'allowScriptAccess','sameDomain',
			'movie', nombre,
			'salign', '');
	}
}



