var nodes			= new Array();
var openNodes	    = new Array();
var icons			= new Array(6);
var varerror;
var Namefuncion;
var mercadoE;
var mercadoI;
var tamano;
var vpx;
var vpi;
var vppx;
var vppi;


function getTabs(evento){
  var comilla='"';
	  if (evento=="E"){
	  	tabelegido=document.getElementById("tdfila").innerHTML="<ul class="+comilla+"estm_pestanaa"+comilla+"><li><b><a href="+comilla+"javascript:getTabs('G')"+comilla+">Generales</b></a></li><li class="+comilla+"pestana_activadaa"+comilla+"><b>Específicas</b></li></ul>";
	  	document.getElementById('trg').style.display='none';
	  	document.getElementById('tre').style.display='block';
	  }else{
	    tabelegido=document.getElementById("tdfila").innerHTML="<ul class="+comilla+"estm_pestanaa"+comilla+"><li class="+comilla+"pestana_activadaa"+comilla+"><b>Generales</b></li><li><a href="+comilla+"javascript:getTabs('E')"+comilla+"><b>Específicas</b></a></li></ul>";
	    document.getElementById('trg').style.display='block';
	  	document.getElementById('tre').style.display='none';
	  }
  }
function MakeXMLReq1()
{
  var xmlHttp = null;
  try
  {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
      //For IE 5.5 Users
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
                    
  return xmlHttp;
}          
function ExtraeDescripcionHijos(cadena)
{	
//alert(tamano);	
  if(tamano<35){
	if (cadena.length>35){
		cadena=cadena.substr(0,tamano);
		cadena=cadena+"...";
	}
	return cadena;
  }else{
    if (cadena.length>85){
		cadena=cadena.substr(0,tamano);
		cadena=cadena+"...";
	}
  	return cadena;
  }
}

function ExtraePartida(cadena)
{	
	if ((cadena.substr(0,1))=="V"){
		return cadena;
	}else{
		var Finpos=cadena.indexOf (" ");
		var cadenafinal=cadena.substr (0, (Finpos));			
		return cadenafinal;
	}
}
function valorcheck_elegidos(pnombre)
{
    //alert(pnombre);
	var nom=document.getElementById(pnombre).value;
	var elegido=document.getElementById('t2').value;
	if (document.getElementById(pnombre).checked){
	 document.getElementById('t2').value=elegido+"|"+nom;
	}else{      
	 document.getElementById('t2').value=Desmarcar(elegido,nom)	 
	}
}

function Desmarcar(caja,valorbuscado)
{
  var cadena=caja+"|";
  var Initpos=cadena.indexOf(valorbuscado);
  var primersegmento=cadena.substr(0,Initpos);    
  var segundosegmento=cadena.substr(primersegmento.length,cadena.length);   
  Initpos=segundosegmento.indexOf("|");
  var segmentofinal=segundosegmento.substr(Initpos+1,segundosegmento.length)        
  nuevacadena=primersegmento+segmentofinal;        
  return nuevacadena.substr(0,nuevacadena.length-1)
}
function MarcaRadio(id)
{
	document.getElementById('t1').value=id;
	changetext('',descriptions);
	//portletformbdg_correlacionador.submit();
}
function Radiocheked(text)
{	   
	   var valpartida=document.getElementById('t1').value;
	   if (valpartida=="")
	   {
	   		document.getElementById('t1').value=ExtraePartida(text);
	   		return " checked=checked";
	   }else{
	       var cadena=ExtraePartida(text);
			  if (valpartida==cadena){
				return " checked=checked";
			  }else{
		     		return " ";
			  }
	  }
}
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "np4/SIICEX1/images/BDG/img/plus.gif";
	icons[1] = new Image();
	icons[1].src = "np4/SIICEX1/images/BDG/img/plusbottom.gif";
	
	icons[2] = new Image();
	icons[2].src = "np4/SIICEX1/images/BDG/img/minus.gif";
	
	icons[3] = new Image();	
	icons[3].src = "np4/SIICEX1/images/BDG/img/minusbottom.gif";
	
	icons[4] = new Image();
	icons[4].src = "np4/SIICEX1/images/BDG/img/folder.gif";
	icons[5] = new Image();
	icons[5].src = "np4/SIICEX1/images/BDG/img/folderopen.gif";
}
function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}
function createTree(nfun,arrName, startNode, openNode) {
    //var mercadoE=document.getElementById('paisx').options[document.getElementById('paisx').selectedIndex].text;
    //var mercadoI=document.getElementById('paisi').options[document.getElementById('paisi').selectedIndex].text;
    /*mercadoE=mercadoE.substr(0,mercadoE.indexOf("("));
    mercadoE=trim(mercadoE);
    
    mercadoI=mercadoI.substr(0,mercadoI.indexOf("("));
    mercadoI=trim(mercadoI);*/

    
    Namefuncion=nfun;
	nodes = arrName;
	if (nodes.length > 0) {
	   	preloadIcons();		
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	    if (Namefuncion=="oc"){
			if (startNode !=0) {
				var nodeValues = nodes[getArrayId(startNode)].split("|");
				document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
			} else document.write("<img src=\"np4/SIICEX1/images/BDG/img/base.gif\" align=\"absbottom\" alt=\"\" /><b>Mercado Origen ("+mercadoE+")</b><br />");
		}else{
			if (startNode !=0) {
				var nodeValues = nodes[getArrayId(startNode)].split("|");
				document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
			} else document.write("<img src=\"np4/SIICEX1/images/BDG/img/base.gif\" align=\"absbottom\" alt=\"\" /><b>Mercado Destino ("+mercadoI+")</b><br />");
		}
		
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"np4/SIICEX1/images/BDG/img/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"np4/SIICEX1/images/BDG/img/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}
			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);
			// Write out join icons
			
			if (hcn) {
				if (ls) {
					document.write("<a href=\"javascript:"+Namefuncion+"(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/");
					 	if (ino) document.write("plus");
						else document.write("minus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript:"+Namefuncion+"(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/");
						if (ino) document.write("plus");
						else document.write("minus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\"np4/SIICEX1/images/BDG/img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"np4/SIICEX1/images/BDG/img/join.gif\" align=\"absbottom\" alt=\"\" />");
			}


			// Start link
			//document.write("<a title='"+nodeValues[2]+"' href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			document.write("<a title='"+nodeValues[2]+"' href=\"javascript:void(0)\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			if (Namefuncion=="oc"){
				// Write out folder & page icons
				if (hcn) {
					document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/folder")
						if (ino) document.write("open");
					document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
				
				
				//} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
				//} else document.write("-------");
				} else document.write("<input "+ Radiocheked(nodeValues[2]) +" type=radio name=arbollst id="+ExtraePartida(nodeValues[2])+" value="+ExtraePartida(nodeValues[2])+" onclick=(MarcaRadio('"+ExtraePartida(nodeValues[2])+"')) \>");
				
				// Write out node name
				document.write(ExtraeDescripcionHijos(nodeValues[2]));
			}else{
				if (hcn) {
					document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/folder")
						if (ino) document.write("open");
					document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
				
				
				//} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
				//} else document.write("-------");
				} else document.write("<input type=checkbox name=check1"+ExtraePartida(nodeValues[2])+" id=check1"+ExtraePartida(nodeValues[2])+" value="+ExtraePartida(nodeValues[2])+" onclick=valorcheck_elegidos('check1"+ExtraePartida(nodeValues[2])+"') \>");
					   				  
				
				// Write out node name
				document.write(ExtraeDescripcionHijos(nodeValues[2]));
			}
			// End link
			document.write("</a><br />");			
			// If node has children write out divs and go deeper			
			if (Namefuncion=="oc"){
				if (hcn) {
					document.write("<div id=\"xdiv" + nodeValues[0] + "\"");
						if (!ino) document.write(" style=\"display: block;\"");
					document.write(">");
					addNode(nodeValues[0], recursedNodes);
					document.write("</div>");
				}					
			}else{
				if (hcn) {
					document.write("<div id=\"div" + nodeValues[0] + "\"");
						if (!ino) document.write(" style=\"display: block;\"");
					document.write(">");
					addNode(nodeValues[0], recursedNodes);
					document.write("</div>");
				}
			}	
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Opens or closes a node
function oc(node, bottom) {
	var theDiv = document.getElementById("xdiv" + node);
	var theJoin	= document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}

function od(node, bottom) {
	var theDiv2 = document.getElementById("div" + node);
	var theJoin2	= document.getElementById("join" + node);
	var theIcon2 = document.getElementById("icon" + node);
	
	if (theDiv2.style.display == 'none') {
		if (bottom==1) theJoin2.src = icons[3].src;
		else theJoin2.src = icons[2].src;
		theIcon2.src = icons[5].src;
		theDiv2.style.display = '';
	} else {
		if (bottom==1) theJoin2.src = icons[1].src;
		else theJoin2.src = icons[0].src;
		theIcon2.src = icons[4].src;
		theDiv2.style.display = 'none';
	}
}

// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}
