
var incrementChilds;
var primeraPos;
var impres = false;
function calculaTam()
{
	var altWindow;
	if(document.all)
	{
		//alert( document.getElementById("Bodydiv").innerHTML);
		altWindow = document.getElementById("Bodydiv").style.height;
		if (altWindow<10)
		{
			//altWindow = 500;
			altWindow = document.body.clientHeight;
			//alert("1 "+altWindow);
		}
		if (altWindow<10)
		{
			altWindow =document.body.offsetHeight;
			//alert("2 "+altWindow);
		}
		if (altWindow<10 || altWindow=="undefined")
		{
			altWindow = 800;
			//alert(3);
		}
		//alert(altWindow);
	}
	else
	{
		altWindow = innerHeight;
	}
	
	altWindow = altWindow - 105 + 8;	

	var altPre = document.getElementById("PreTaula").offsetHeight;
	var altCap = document.getElementById("Capcalera").offsetHeight;
	var altPeu = document.getElementById("PeuTaula").offsetHeight;

	var firefox = -20;
	if(document.all)
	{
		firefox = 0;
	}
	//alert(altWindow -(altPre+altCap+altPeu)+"px");
	//alert(altWindow -(altPre+altCap+altPeu)+firefox);
	if(altWindow -(altPre+altCap+altPeu)+firefox > 100)
	{
		document.getElementById("Taula").style.height = altWindow -(altPre+altCap+altPeu)+firefox+"px";
	}
	//document.getElementById("itec").style.display = "none";
	
}
function nombreDeChilds(pare)
{
	var quants = 0;
	var totals =  pare.childNodes.length;
	var hedp;
	
	
	
	
	//alert(totals);
	if(document.all)
	{
//		quants = totals;
		for(i=0;i<totals;i++){
			quants = quants + pare.childNodes[i].colSpan;	
		}
	}
	else
	{
		for(ka=primeraPos; ka < totals; ka = ka + incrementChilds)
		{
			if( !is_ignorable(pare.childNodes.item(ka)) )
			{
				quants++;
			}
		}

	}
	return quants;
}

function montaTaula(Color){

	// Cālcul de l'alįada disponible de la finestra
	var altWindow;
	var enIFrame = true;

	if(!document.getElementById("segonaPart")){
		enIFrame = 	false;
	}

	if(document.all)
	{
		if(enIFrame){
			altWindow = document.body.offsetHeight;
			
		}else{
			altWindow = (document.body.offsetHeight);
			altWindow = altWindow + 50;
		}
		incrementChilds = 1;
		primeraPos = 0;
	}
	else
	{
		altWindow = innerHeight;
		incrementChilds = 1;
		primeraPos = 0;
	}
	
	
	
	var TaulaOculta = document.getElementById("TaulaOculta");
	var Capcalera = document.getElementById("Capcalera");
	var Contingut = document.getElementById("Contingut");
	var PeuTaula = document.getElementById("PeuTaula");
	var Taula = document.getElementById("Taula");
	var PreTaula = document.getElementById("PreTaula");
	
	var HTMLTaula;
	var trs;
	
	if(enIFrame){
		if(((altWindow - 105) + 8)>0)altWindow = ((altWindow - 105) + 8);
	}
	
	/*	
	if(document.title == "LlistaEmpreses" && parent.document.location.href.indexOf("resultats")>0){
		altWindow = altWindow - 50;	
	}
	*/
	
	
	TaulaOculta.style.width = TaulaOculta.parentNode.style.width;
	Taula.style.width = TaulaOculta.clientWidth + 20;
	
	
	
		
	// Creem les taules paralˇleles
	var tableObj = document.createElement("TABLE");
	var tbodyObj = document.createElement("TBODY");
	var trObj = document.createElement("TR");
	

	
	
	var tableCObj = document.createElement("TABLE");
	var tbodyCObj = document.createElement("TBODY");
	if(document.all)
	{
		tableObj.style.width = TaulaOculta.offsetWidth + "px";
	}
	else
	{
		tableObj.style.width = TaulaOculta.offsetWidth-30 + "px";
	}
	tableObj.setAttribute("id", "TaulaCapcalera");
	//tableObj.setAttribute("width", "100%");
	
	tableObj.setAttribute("cellpadding", "0");
	tableObj.setAttribute("cellspacing", "0");
	tableObj.setAttribute("border", "0");
	if(document.all)
	{
		tableObj.style.width = TaulaOculta.offsetWidth;
		tableCObj.style.width = TaulaOculta.offsetWidth;
	}
	else
	{
		tableObj.style.width = TaulaOculta.offsetWidth-30 + "px";
		tableCObj.style.width = TaulaOculta.offsetWidth-30 + "px";
	}
	

	
	
	tableCObj.setAttribute("id", "TaulaContingut");
	/*tableCObj.setAttribute("width", "100%");*/
	tableCObj.setAttribute("cellpadding", 0);
	tableCObj.cellPadding = 0;
	tableCObj.cellSpacing = 0;
	
	tableCObj.setAttribute("cellspacing", "0");
	tableCObj.setAttribute("border", "0");
	
	tbodyCObj.setAttribute("cellpadding", "0");
	tbodyCObj.setAttribute("cellspacing", "0");
	tableObj.cellPadding = 0;
	tableObj.cellSpacing = 0;

	tbodyCObj.setAttribute("border", "0");
	
	tbodyObj.setAttribute("cellpadding", "0");
	tbodyObj.setAttribute("cellspacing", "0");
	tbodyObj.setAttribute("border", "0");
	tbodyObj.style.borderCollapse = "collapse";
	
	var restaAmplada;		
	if(document.all)
	{
		HTMLTaula = TaulaOculta.childNodes.item(0);
		trs = HTMLTaula //.childNodes.item(0);
		restaAmplada = 0;
	}
	else
	{
		for(var j=0;j<TaulaOculta.childNodes.length;j++)
		{	
			if(!is_ignorable(TaulaOculta.childNodes.item(j)))
			{
				HTMLTaula = TaulaOculta.childNodes.item(j);
				trs = HTMLTaula //.childNodes.item(0);
				break;
			}
		}
		restaAmplada = 20;
	}
	// I les omplim
	//var nombredefills = nombreDeChilds(trs);

	
	
	for(p=primeraPos;p<trs.childNodes.length;p = p + incrementChilds)
	{			
			if(p==primeraPos)
				{					
					var tr = trs.childNodes.item(p);
					//var nombredefills1 = nombreDeChilds(tr);
					for(i=primeraPos;i<tr.childNodes.length;i = i + incrementChilds)
						{	
							if(!is_ignorable(tr.childNodes.item(i)))
							{
								var tdObj = document.createElement("TD");
								if(tr.childNodes.item(i).innerHTML != null)
								{
									tdObj.style.width = tr.childNodes.item(i).offsetWidth + "px";
									//tdObj.style.color="#000000";
									tdObj.className = "capcalera" + Color;
									tdObj.innerHTML = tr.childNodes.item(i).innerHTML;
									trObj.appendChild(tdObj);
								}
							}
						}
				}
			else
			{
				var tr = trs.childNodes.item(p);
				var trCObj = document.createElement("TR");
				var algunPosat = false;
				//var nombredefills2 = nombreDeChilds(tr);
				for(i=primeraPos;i<tr.childNodes.length;i = i + incrementChilds)
				{	
					if(!is_ignorable(tr.childNodes.item(i)))
					{
						algunPosat = true;
						var tdCObj = document.createElement("TD");
						if(tr.childNodes.item(i).innerHTML != null)
						{
							tdCObj.style.width = tr.childNodes.item(i).offsetWidth + "px";
							tdCObj.innerHTML = tr.childNodes.item(i).innerHTML;
							tdCObj.colSpan = tr.childNodes.item(i).colSpan;
							tdCObj.rowSpan = tr.childNodes.item(i).rowSpan;
							tdCObj.className = tr.childNodes.item(i).className;
							tdCObj.noWrap = tr.childNodes.item(i).noWrap;
							//tdCObj.vAlign = tr.childNodes.item(i).vAlign;
							//tdCObj.style.border = "1px solid white";
							//tdCObj.setAttribute("rowspan", tr.childNodes.item(i).attributes.getNamedItem("rowspan"));
							//tdCObj.setAttribute("class", tr.childNodes.item(i).attributes.getNamedItem("class"));
						}
						trCObj.appendChild(tdCObj);
					}
				}
				if(algunPosat)
				{
					tbodyCObj.appendChild(trCObj);
				}
				
				if(tr.id != "Manteniment" && algunPosat)
				{	
					if(!is_ignorable(tr))
					{
						var tdSeparador = document.createElement("TD");
						var trSeparador = document.createElement("TR");				
						if(nombreDeChilds(tr) > 0)
						{
								tdSeparador.colSpan = nombreDeChilds(tr);
				}
				tdSeparador.colSpan = 5;
						tdSeparador.style.height = "1px";
						tdSeparador.style.fontSize = "1px";
						tdSeparador.style.padding = "0px";
						tdSeparador.innerHTML = "<hr class='hrSeparador" + Color + "'/>";
						trSeparador.appendChild(tdSeparador);
					//	tbodyCObj.appendChild(trSeparador);
					}
				}
					
			}
	
	}	
	
	tbodyObj.appendChild(trObj);
	tableObj.appendChild(tbodyObj);
	tableCObj.appendChild(tbodyCObj);
	
	Capcalera.appendChild(tableObj);
	Taula.appendChild(tableCObj);
	
	
	
	//Contingut.style.width = Contingut.parentNode.style.width;
	Contingut.style.position = "absolute";
	
	if(enIFrame){
		Contingut.style.top= "113px";
		Contingut.style.left = "20px";
			
		
	}else{
		Contingut.style.top= "-5px";
		Contingut.style.left = "10px";
		
	}
	
	
	
	Contingut.className = "";
	

	//Contingut.style.backgroundColor = "red";
	
	
	Contingut.style.visibility = "visible";
	//Contingut.style.display = "block";
	Taula.style.overflow = "auto";
	Taula.className = "scroll" + Color;

	var espaiLliure = (altWindow - PeuTaula.offsetHeight - PreTaula.offsetHeight) - 70;

	if(espaiLliure > 0 && espaiLliure < Taula.offsetHeight)
	{
		
		
		Taula.style.height = altWindow - PeuTaula.offsetHeight - PreTaula.offsetHeight - 70 + "px";
		
		str = "altWindow: " + altWindow + "\n";
		str += "peuTaula: " + PeuTaula.offsetHeight + "\n";
		str += "PreTaula: " + PreTaula.offsetHeight + "\n";
		str += "Taula:    " + Taula.style.height + "\n";
		str += "enIFrame: " + !enIFrame;
		
		Taula.style.overflow = "auto";
	}
	else{
	
		Taula.style.height = altWindow - PeuTaula.offsetHeight - PreTaula.offsetHeight - 70 + "px";
		//alert(Taula.offsetHeight);
		//alert(TaulaOculta.style.height);
		//Taula.style.height = "auto";
		
	}
	
	
	if(!enIFrame){
		//Taula.style.height = "auto";	
	}
	
	
	Taula.style.overflow = "auto";

	//alert(document.getElementById("ResultatNul").innerHTML);
	if (document.getElementById("ResultatNul")) {
	    //alert("asdasdas");
	    document.getElementById("ResultatNul").style.height = altWindow - PeuTaula.offsetHeight - PreTaula.offsetHeight - 40 + "px";
	    document.getElementById("Taula").style.display = "none";
	    document.getElementById("Capcalera").style.display = "none";
	}
	//posarImatgesDinsDiv( document.getElementById("TaulaOculta").getElementsByTagName("img"));   
}





function tamanyTaula(Color){
//	alert(document.getElementById("Dades").offsetHeight)
//	alert(document.getElementById("capcalera").offsetHeight)

//	setTimeout("calculs()",0);

document.getElementById("Contingut").style.position = "absolute";
document.getElementById("Contingut").style.top = "105px";
document.getElementById("Contingut").style.left = "20px";
document.getElementById("Contingut").style.width = "961px";
document.getElementById("Contingut").style.width = "961px";
document.getElementById("TableContainer").style.width = "100%";
document.getElementById("Contingut").style.backgroundColor = "red";
document.getElementById("scrollContent").style.zIndex = "1000";
document.getElementById("fixedHeader").style.width = "940px";


//document.getElementById("CapTaula").style.position = "absolute";
//document.getElementById("CapTaula").style.top = "-110px";
//document.getElementById("Contingut").style.top = "500px";

}

function calculs(){
	var dadesTaula;	
	dadesTaula = document.getElementById("TaulaDades");
	var CapcaleraTaula = document.getElementById("capcalera");

	var altWindow;
	if(document.all){
		altWindow = document.body.clientHeight;
	}
	else{
		altWindow = innerHeight;
	}
	altWindow = altWindow - 105 - 40;
	altTaula = dadesTaula.offsetHeight;	
	alert("Taula: " + altTaula + "\nWindow: " + altWindow);
	
	if(altTaula > altWindow){ 
		document.getElementById("TaulaDades").style.height = altWindow + "px";
		document.getElementById("TaulaDades").style.overflow = "auto";
	}
	alert(dadesTaula.childNodes.item(primeraPos).innerHTML)
	for(i=primeraPos;i < nombreDeChilds(dadesTaula.childNodes.item(0).childNodes.item(0));i = i + incrementChilds){
		ampleCos = dadesTaula.childNodes.item(0).childNodes.item(0).childNodes.item(i).offsetWidth;
		ampleCap = CapcaleraTaula.childNodes.item(0).childNodes.item(0).childNodes.item(i).offsetWidth;
		alert("Cos (" + i + "):" + ampleCos +"\n" + "Capcalera (" + i + "):" + ampleCap)
		if(ampleCos > ampleCap){
			
			CapcaleraTaula.childNodes.item(0).childNodes.item(0).childNodes.item(i).style.width= ampleCos - 1 + "px";
			alert("Capcalera (" + i + "):" + ampleCos + "px");
		}
		else{
			dadesTaula.childNodes.item(0).childNodes.item(0).childNodes.item(i).style.width=ampleCap + "px";
			alert("Cos (" + i + "):" + ampleCap + "px");
		}
	}


}



/**
 * Throughout, whitespace is defined as one of the characters
 *  "\t" TAB \u0009
 *  "\n" LF  \u000A
 *  "\r" CR  \u000D
 *  " "  SPC \u0020
 *
 * This does not use Javascript's "\s" because that includes non-breaking
 * spaces (and also some other characters).
 */


/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}


/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

/**
 * Version of |previousSibling| that skips nodes that are entirely
 * whitespace or comments.  (Normally |previousSibling| is a property
 * of all DOM nodes that gives the sibling node, the node that is
 * a child of the same parent, that occurs immediately before the
 * reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest previous sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space.  (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of( txt )
{
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
}


function versioImprimir(idioma,mail)
{
	var taulaNode = document.getElementById("TaulaOculta");
	var headerNode = getElementsByClass(taulaNode,"capcalera","TR");
	if (headerNode == null)
	{
		//var headerNode = taulaNode.getElementsByTagName('TR').item(0);
		headerNode = null;
	}
	else
	{
		hedp = headerNode;
		headerNode.parentNode.removeChild(headerNode);
	}

	var apartatNode = getElementsByClass(document.getElementById("navlist"),"active2","LI")
	
	var peu = document.getElementById("PeuSenseLinks").innerHTML;

	
	
	
		 

	var taula = taulaNode.innerHTML;

	var header = ""; 	
	var apartat;
	if(headerNode != null){
		header = headerNode.innerHTML;
	}

	apartat = apartatNode.innerHTML;
	apartat = apartat.replace("'","´");
	var novawindow = window.open("/recursos/preprint.html", "webprint", "top=10;left=10;toolbar=0;location=0;directories=0;status=0;menubar=0;scrollbars=0;resizable=1;width=100;height=100;");
	novawindow.blur();
	//Creem el form
	var myform = document.createElement("form");
	myform.action = "http://seat/recursos/print.asp";	
	myform.method = "POST";
	myform.name = "formImprimir";
	myform.id = "formImprimir";
	myform.target = "webprint";

	var input1 = document.createElement("input");
	input1.id = "header";
	input1.name="header";
	input1.type = "hidden";
	input1.value = header;

	var input2 = document.createElement("input");
	input2.id = "apartat";
	input2.name = "apartat";
	input2.type = "hidden";
	input2.value = apartat;

	var input3 = document.createElement("input");
	input3.id = "taula";
	input3.name = "taula";
	input3.type = "hidden";
	input3.value = taula;
	
	var input4 = document.createElement("input");
	input4.id = "url";
	input4.name = "url";
	input4.type = "hidden";
	input4.value = document.location.href;

	var input5 = document.createElement("input");
	input5.id = "peu";
	input5.name = "peu";
	input5.type = "hidden";
	input5.value = peu;

	var input6 = document.createElement("input");
	input6.id = "Idioma";
	input6.name = "Idioma";
	input6.type = "hidden";
	input6.value = idioma;

	var input7 = document.createElement("input");
	input7.id = "mail";
	input7.name = "mail";
	input7.type = "hidden";

	if(mail.length > 3)
	{
		input7.value = mail;
	}
	else
	{
		input7.value = "info@itec.cat";
	}

	myform.appendChild(input1);
	myform.appendChild(input2);
	myform.appendChild(input3);
	myform.appendChild(input4);
	myform.appendChild(input5);
	myform.appendChild(input6);
	myform.appendChild(input7);

	impres = true;	
	//novawindow.document.body.appendChild(myform);
	document.getElementById("bodyDiv").appendChild(myform);
	myform.submit();
	document.getElementById("bodyDiv").removeChild(myform);
	//novawindow.document.enviar();

}

function posarImatgesDinsDiv(imatges)
{
	var i = 0;
	while(i<imatges.length)
	{
		var borrar = imatges.item(i).parentNode;
		var div = borrar.nextSibling;
		while(is_ignorable(div) && div !="div")
		{
			div = div.nextSibling;
		}
		div.insertBefore(imatges.item(i),div.firstChild);
		//borrar.nodeType = "div";
		i++;
	}
}

function getElementsByClass(node,searchClass,tag) 
{
	var temp = node.getElementsByTagName("*");
	var i = 0;
	while(i < temp.length)
	{
		if(temp.item(i).className == searchClass)
		{
			//alert(i);
			return temp.item(i);
		}
		i++;
	}
}


var posX = 0;
var posY = 0;

function mostraAvis(onelposem, quintext) {
    posX = 0;
    posY = 0;
    getElementPosition(onelposem);
    //alert(quintext);
    document.getElementById("Avis").innerHTML = "<span>" + quintext + "</span>";
    document.getElementById("Avis").style.display = "block";
    document.getElementById("Avis").style.left = posX + 15;
    document.getElementById("Avis").style.top = posY;
    
}

function amaga() {
    document.getElementById("Avis").style.display = "none";
}

function getElementPosition(obj) {

    posX = mouseX - obj.offsetLeft;
    posY = mouseY - obj.offsetTop; 
}

var isIE = document.all;
var mouseX = 0;
var mouseY = 0;

function getMouseXY(e) {
    if (!e) e = window.event;
    if (e) {
        mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
        mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
    }
}
document.onmousemove = getMouseXY;
