var ventanasAbiertas = 0;
var WinW;
var WinH;
var activoNormativa = 0;
var activoFitxaBuit = 0;


function openMF(idEmp, PropVal)
{
	var strOpen;
	var top;
	var left;
	var idEntrada;
	if(PropVal==0){
		idEntrada = 2
	} else{
		idEntrada = PropVal
	}
	strOpen = "/noumetafitxa.e/metafitxa/metafitxa.asp?IdEmp=" + idEmp + "&IdEntrada=" + idEntrada + "&Tipus=E";
		
	top = centerTopW(500)-15
	left = centerLeftW(750)
	strComObrir = "width=800,height=565,top=" + top + ",left=" + left + ",scrollbars=no,location=no,resizable=no,titlebar=no,toolbar=no,status=no" //status=no
	window.open(strOpen,"",strComObrir,"");
}

function MostrarCertificadoEmpresa(nom)
{
	openCenteredWindow(nom, 650, 900);
}
function MostrarNovaFitxa(nom)
{
	openCenteredWindow2(nom,680,800);
}
function openCenteredWindow2(url, h, w){
	url = url
	// str = "'toolbar=auto,location=no,directories=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + w + ",height=" + h + ",top=" + centerTopW(h) + ",left=" + centerLeftW(w) + "'"
	str = "'copyhistory=no,toolbar=no,location=no, menubar=no,directories=no,status=no,scrollbars=yes,resizable=no,width=" + w + ",height=" + h + ",top=" + centerTopW(h) + ",left=" + centerLeftW(w) + "'"
	//w = window.open("OmpleEstadistiques.asp?peticio=r&url=" + url, '', str);
	w = window.open(url, '', str)
}

function InitCE(nom)
{
	calculaPeu();
	rellenarEstadistica(nom,'');
	if(document.getElementById("ordre").value == 1)
	{
		if(document.getElementById("jsnotrobat").value == 1)
		{
			if(ob_cuantosHijo(document.getElementById("MiTree")) < 2)
			{
				setTimeout('LlamarHijos(document.getElementById("MiTree"))',100);
			}
		}
	}
	else
	{
		montaTaula('Groc');
		recalculaTaula();
	}
	window.onresize = register;
	Reposicionar();
}

function recalculaTaula(){
	var altWindow;
	if(document.all)
	{
		altWindow = document.body.offsetHeight;
	}
	else
	{
		altWindow = innerHeight;
	}
	document.getElementById("Taula").style.height = altWindow - document.getElementById("PeuTaula").offsetHeight - document.getElementById("PreTaula").offsetHeight - 70 + "px";
	document.getElementById("Taula").style.overflow = "auto";			
	document.getElementById("Taula").style.height = document.getElementById("Taula").offsetHeight + 50;
}


function CarregaLLetra(Quina, idioma, tipo){
	var lletra = (Quina.id != 'totes') ? Quina.id.toString() : '';
	document.getElementById("Taula").innerHTML = "";
	document.getElementById("Capcalera").innerHTML = "";
	document.getElementById("DivTaulaOculta").innerHTML="";
	document.getElementById("DivAbecedari").innerHTML = nouMarcatgeCE.Clases.CAjax.PintaAbecedari(idioma, tipo, lletra).value
	document.getElementById("DivTaulaOculta").innerHTML = nouMarcatgeCE.Clases.CAjax.GetEmpresesByLletra(idioma,tipo,lletra).value;
	document.getElementById("DivTaulaOculta").scrollTop=0;
	rellenarEstadistica("MarcatgeCe-FiltrarLletres","");
	montaTaula('Groc');
	recalculaTaula();
}


function register(e)
{
	if (!e) e = window.event;
	Reposicionar();
	return true;
}

function Reposicionar()
{
	if(document.all)
	{
		// Gecko 1.0 (Netscape 7) and Internet Explorer 5+
		WinW = window.document.body.clientWidth;  
		WinH = window.document.body.clientHeight;
	}
	else
	{
		// Navigator 4.x, Netscape 6.x, CompuServe 7 and Opera
		WinW = window.innerWidth;
		WinH = window.innerHeight;
	}
	if(activoNormativa > 0) ReposicionarNormativa();
	if(activoFitxaBuit > 0) ReposicionarFitxaBuit();
}

function ReposicionarNormativa()
{
	if((WinW - 50 - (20 * ventanasAbiertas))> 0)
	{
		document.getElementById("ventanaNormativa").style.width = WinW - 50 - (20 * ventanasAbiertas) + "px";
		document.getElementById("ventanaNormativa").style.left = 25 + (10 * ventanasAbiertas) + "px";
		document.getElementById("ventanaNormativa").style.top = 5 + (10 * ventanasAbiertas) + "px";
		document.getElementById("ventanaNormativa").style.zIndex = 20010 + (100 * ventanasAbiertas);
		document.getElementById("cortinaNormativa").style.zIndex = 20000 + (100 * ventanasAbiertas);
	}
	if((WinH - 30 - (20 * ventanasAbiertas))> 0) document.getElementById("ventanaNormativa").style.height = WinH - 30 - (20 * ventanasAbiertas) + "px";
	if(document.getElementById("ventanaContenidoNormativa"))
		document.getElementById("ventanaContenidoNormativa").style.height = WinH - document.getElementById("ventanaCabeceraNormativa").offsetHeight - document.getElementById("ventanaPieNormativa").offsetHeight - 10 - 20 - (20 * ventanasAbiertas) + "px";
}

function ReposicionarFitxaBuit()
{
	if((WinW - 50 - (20 * ventanasAbiertas))> 0)
	{
		document.getElementById("ventanaFitxaBuit").style.width = WinW - 50 - (20 * ventanasAbiertas) + "px";
		document.getElementById("ventanaFitxaBuit").style.left = 25 + (10 * ventanasAbiertas) + "px";
		document.getElementById("ventanaFitxaBuit").style.top = 5 + (10 * ventanasAbiertas) + "px";
		document.getElementById("ventanaFitxaBuit").style.zIndex = 20010 + (100 * ventanasAbiertas);
		document.getElementById("cortinaFitxaBuit").style.zIndex = 20000 + (100 * ventanasAbiertas);
	}
	if((WinH - 30 - (20 * ventanasAbiertas))> 0) document.getElementById("ventanaFitxaBuit").style.height = WinH - 30 - (20 * ventanasAbiertas) + "px";
	if(document.getElementById("ventanaContenidoFitxaBuit"))
		document.getElementById("ventanaContenidoFitxaBuit").style.height = WinH - document.getElementById("ventanaCabeceraFitxaBuit").offsetHeight - document.getElementById("ventanaPieFitxaBuit").offsetHeight - 10 - 20 - (20 * ventanasAbiertas) + "px";
}

function AbrirNormativa(IdMaterial, idioma, DeDonde)
{
	nouMarcatgeCE.Clases.CAjax.GetListaNormativas(idioma, IdMaterial, DeDonde, GetListaNormativas_callback);
	/*
	var strOpen;
	var top;
	var left;
	top = centerTopW(565);
	left = centerLeftW(800);
	var str = "Normativa.aspx?id=" + IdMaterial;
	openCenteredEndWindow(str,1000,1000);
	*/
}

function GetListaNormativas_callback(res)
{

	document.getElementById("ventanaNormativa").innerHTML = res.value;
	if(!NiftyCheck())
		return;
	var DeDonde = document.getElementById("jsdedonde").value;
	if(DeDonde == "0")
	{
		RoundedTop("div#ventanaNormativa","#D5D5D5","#FD7216");
		RoundedBottom("div#ventanaNormativa","#D5D5D5","#FD7216");
	}
	else
	{
		RoundedTop("div#ventanaNormativa","#D5D5D5","#BAB9B9");
		RoundedBottom("div#ventanaNormativa","#D5D5D5","#BAB9B9");
	}
	document.getElementById("cortinaNormativa").style.display = "block";
	document.getElementById("ventanaNormativa").style.display = "block";
	activoNormativa = 1;
	ReposicionarNormativa();

	rellenarEstadistica("FitxaNorma","");
//	var top = centerTopW(500)-15;
//	var left = centerLeftW(750);
//	var strComObrir = "width=800,height=565,top=" + top + ",left=" + left + ",scrollbars=no,location=no,resizable=no,titlebar=no,toolbar=no,status=no";
//	var w = window.open(res.value, "", strComObrir);
//	w.document.write(res.value);
}
function ocultarNormativa()
{
	document.getElementById("cortinaNormativa").style.display = "none";
	document.getElementById("ventanaNormativa").style.display = "none";
	document.getElementById("ventanaNormativa").innerHTML = "";
	activoNormativa = 0;
}

function AbrirFitxaBuit(idioma, IdMaterial, IdFont, IdUs, Ordre, DeDonde)
{
	nouMarcatgeCE.Clases.CAjax.GetFitxaRmBuit(idioma, IdMaterial, IdFont, IdUs, Ordre, DeDonde,GetFitxaBuit_callback);
}

function GetFitxaBuit_callback(res)
{
/*
	document.getElementById("ventanaFitxaBuit").innerHTML = res.value;
	if(!NiftyCheck())
		return;
	var DeDonde = document.getElementById("jsdedonde").value;
	if(DeDonde == "0")
	{
		RoundedTop("div#ventanaFitxaBuit","#D5D5D5","#FD7216");
		RoundedBottom("div#ventanaFitxaBuit","#D5D5D5","#FD7216");
	}
	else
	{
		RoundedTop("div#ventanaFitxaBuit","#D5D5D5","#BAB9B9");
		RoundedBottom("div#ventanaFitxaBuit","#D5D5D5","#BAB9B9");
	}
	document.getElementById("cortinaFitxaBuit").style.display = "block";
	document.getElementById("ventanaFitxaBuit").style.display = "block";
	activoFitxaBuit = 1;
	ReposicionarFitxaBuit();
*/
	var top = centerTopW(500)-15;
	var left = centerLeftW(750);
	var strComObrir = "width=800,height=565,top=" + top + ",left=" + left + ",scrollbars=no,location=no,resizable=yes,titlebar=no,toolbar=no,status=no";
	rellenarEstadistica("FitxaMaterial", "");
	var w = window.open(res.value, "", strComObrir);
}

function ocultarFitxaBuit()		
{
	document.getElementById("cortinaFitxaBuit").style.display = "none";
	document.getElementById("ventanaFitxaBuit").style.display = "none";
	document.getElementById("ventanaFitxaBuit").innerHTML = "";
	activoFitxaBuit = 0;
}

function calculaPeu()
{	
	if(document.getElementById("jsmostrar").value == 1)
	{
		if(document.all)
		{
			var alt = document.getElementById("RequadreDret").offsetHeight;
			//document.getElementById("metaNews").style.height = alt - 10;
		}
		else
		{
			var alt = document.getElementById("RequadreDret").offsetHeight;
			//document.getElementById("metaNews").style.height = alt - 20;
		}
	}
}
function rellenarEstadistica(nom, Extra)
{
	var idioma = document.getElementById("jsidioma").value;
	var stridioma;
	var strNom;
	var strExtra = "";
	if(idioma == 1) stridioma = "cat"
	else stridioma = "cas"
	if(Extra != "")	strExtra = Extra;
	strNom = "MarcatgeCE-" + nom;
	estadistiques(strNom,stridioma,strExtra,document.location.href);
	estadistiquesGoogle(strNom);
	
}


function MostrarCarregant(toogle)
{
	if(toogle == 0)
	{
		//Reposicionar();
		document.getElementById("cortina").style['visibility'] = "visible";
		document.getElementById("loading").style['visibility'] = "visible";
	}
	else
	{
		document.getElementById("cortina").style['visibility'] = "hidden";
		document.getElementById("loading").style['visibility'] = "hidden";
	}
}

function EnviarConsulta(t, d)
{
	openCenteredWindow("NoTrobat.aspx?D=" + d + "&T=" + t, 250, 500);
}

function openCenteredWindow(url, h, w){
	url = url
	// str = "'toolbar=auto,location=no,directories=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + w + ",height=" + h + ",top=" + centerTopW(h) + ",left=" + centerLeftW(w) + "'"
	str = "'copyhistory=no,toolbar=no,location=no, menubar=no,directories=no,status=no,scrollbars=no,resizable=no,width=" + w + ",height=" + h + ",top=" + centerTopW(h) + ",left=" + centerLeftW(w) + "'"
	//w = window.open("OmpleEstadistiques.asp?peticio=r&url=" + url, '', str);
	w = window.open(url, '', str)
}


function centerTopW(w_alt)
{
		// Calcula la posició de la finestra centrada a la pantalla pel Top
	winH = window.screen.height
	winH = (winH - w_alt)/2
	return(winH)
}
	
function centerLeftW(w_ample)
{
		// calcula la posició de la finestra centrada a la pantalla per l'esquerra
			winW = window.screen.width
			winW = (winW - w_ample)/2
		return(winW)
}

function reSearch(tipo, texto){
	var mostrar = 0;
	var dedonde = "";
	if(document.getElementById("jsmostrar"))	mostrar = document.getElementById("jsmostrar").value;
	if(document.getElementById("jsdedonde"))	dedonde = document.getElementById("jsdedonde").value;
	if(tipo==1)
	{
			str = "NouResultatsCE.aspx?D=" + dedonde + "&Mostrar=" + mostrar + "&paraula=" + toHexa(texto);
			if(parent.document.getElementById("paraula"))	parent.document.getElementById("paraula").value = texto;
			document.location.href = str;
	}
	else
	{
		if(document.getElementById("AfinaParaula").value != ""){
			str = "NouResultatsCE.aspx?D=" + dedonde + "&Mostrar=" + mostrar + "&paraula=" + toHexa(document.getElementById("AfinaParaula").value);
			if(parent.document.getElementById("paraula"))	parent.document.getElementById("paraula").value = document.getElementById("AfinaParaula").value;
			document.location.href = str;
		}
	}
}	 

function toHexa(valor){
	var text1 = valor;
	var text2 = "";
	for (i=0;i<text1.length;i++){
		text2 += text1.charCodeAt(i).toString(16);
	}
	return text2;
}

