/********************SIICEX***********************/
function MakeXMLReq(){
  var xmlHttp = null;
  try
  {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
                                                         {
      //For IE 5.5 Users
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
  return xmlHttp;
}
/*--------------------------------------*/
function ViewAjax(vdir,vobjeto){
	var tagValue="<input type=hidden name=_page_ value=\"\">";
	var obj=document.getElementById(vobjeto);
	$.get( vdir, function(vdata){
			if (vdata.indexOf(tagValue,1)>0){  o
				bj.innerHTML=vdata.split(tagValue)[1].replace("</form>","");
			}
		}
    );
}
/*--------------------------------------*/
function VerAjax(vdir,vobjeto)
{  
   var tagValue="<input type=hidden name=_page_ value=\"\">";
   var obj=document.getElementById(vobjeto);
   $.get( vdir, function(vdata)
                {  if (vdata.indexOf(tagValue,1)>0)
                   {  obj.innerHTML=vdata.split(tagValue)[1].replace("</form>","");
                   }
                }
        );
}
/*--------------------------------------*/
function EsVacioB (){
  if (document.getElementById("textfield").value !="") {
    return true;
  }else{
    return false;
  }
}
/*--------------------------------------*/
function Abrir() {
	URL="http://www.siicex.gob.pe:8118/search/?index=siicex&query="+document.getElementById("textfield2").value;
	day = new Date();                        
	id = day.getTime();                        
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=600,height=600');");
	document.getElementById("textfield2").value='';
}
/*--------------------------------------*/
function validarAbrir() {
	if (EsVacioB ()) {
		document.onkeypress=function(e){
			var esIE=(document.all);
			var esNS=(document.layers);
			tecla=(esIE) ? event.keyCode : e.which;
			if(tecla==13){	 
					Abrir(); return false;  
			}
		} 
	}
	else{
		document.onkeypress=function(e){
		var esIE=(document.all);
		var esNS=(document.layers);
		tecla=(esIE) ? event.keyCode : e.which;
		if(tecla==13){	 
			return false;
		  }
		} 
	}
}
/*--------------------------------------*/
function EsVacioE (){
  if (document.getElementById('pdesProducto').value !="") {
    return true;
  }else{
    return false;
  }
}
/*--------------------------------------*/
function validarAbrirEM() {

if (EsVacioE ()) {document.onkeypress=function(e){
var esIE=(document.all);
var esNS=(document.layers);
tecla=(esIE) ? event.keyCode : e.which;
if(tecla==13){	 
		AbrirESTUDIO(); return false;  
  }
} }else{
document.onkeypress=function(e){
var esIE=(document.all);
var esNS=(document.layers);
tecla=(esIE) ? event.keyCode : e.which;
if(tecla==13){	 
	return false;
  }
}}
}
/*----------------------------------------------------------*/
function fichaProductoCalidad(ppartida, ppais, ptiporequisito, prequisito){
    var vresultado = "";
    var vUrl="portlet5open.asp?_portletid_=SFichaProductoRequisitoCalidad&scriptdo=cc_fp_requisitoAjax";
    if (ppartida != null) vUrl = vUrl+"&ppartida="+ppartida;
    if (ppais != null) vUrl = vUrl+"&ppais="+ppais;
    if (ptiporequisito != null) vUrl = vUrl+"&ptiporequisito="+ptiporequisito;
    if (prequisito != null) vUrl = vUrl+"&prequisito="+prequisito;

    var objTodo="";
    var objtdPais=document.getElementById("tdPais");
    var objtdTipoRequisito=document.getElementById("tdTipoRequisito");
    var objtdRequisito=document.getElementById("tdRequisito");
    var objtdFichaRequisito=document.getElementById("tdFichaRequisito");
	
	$.get(vUrl, function(vresultado) {
	  $('.result').html(vresultado);
		if (vresultado.indexOf("<input type=hidden name=_page_ value=\"\">",1)>0)
		   {
			  objTodo = vresultado.split("<input type=hidden name=_page_ value=\"\">")[1].replace("</form>","");
			  objtdPais.innerHTML=objTodo.split("<!--LISTAPAIS-->")[1]; 
			  objtdTipoRequisito.innerHTML=objTodo.split("<!--LISTATIPO-->")[1];
			  objtdRequisito.innerHTML=objTodo.split("<!--LISTAREQUISITO-->")[1];
			  objtdFichaRequisito.innerHTML = objTodo.split("<!--DETALLEREQUISITO-->")[1];
		   }
	});
  }
/********************SIICEX***********************/


/********************BASE DE DATOS GLOBAL***********************/


// Arrays for nodes and icons
var nodes			= new Array();
var openNodes	= new Array();
var icons			= new Array(6);


////////////////////////////////////////////////////////////////////////////////////

//Optine solo la partida de toda la cadena

function ExtraePartida(cadena)
{	
	var Finpos=cadena.indexOf (" ");
	var cadenafinal=cadena.substr (0, (Finpos));
	return cadenafinal;
}

//Asigna el valor de la partida hijo elejida
function ListarDestinos(text)
{
	  SeleccionaHijo(text);	
	  var valpartida=document.getElementById('t1').value;
	 if (document.getElementById('tevento').value=="destino"){
		valpartida=document.getElementById('t2').value=text;
	  }else{
	  	valpartida=document.getElementById('t1').value=text;
	  }	  
        document.getElementById('tevento').value="destino";
        portletformbdg_correlacion.method="GET";
        portletformbdg_correlacion.action="portal5ES.asp#ancla_tab";
        portletformbdg_correlacion.submit();
}
//Opteniendo el estado del radiobutton

function Radiocheked(text)
{
       var valpartida;
        if (document.getElementById('tevento').value=="destino"){
		valpartida=document.getElementById('t2').value;
	  }else{
	  	valpartida=document.getElementById('t1').value;
	  }	  
        var cadena=ExtraePartida(text);
	  if (valpartida==cadena){
		return " checked style=background:#C0C0C0";
	  }else{
     		return " ";
	  }
}

function BackGround(text)
{
       var valpartida;
        if (document.getElementById('tevento').value=="destino"){
		valpartida=document.getElementById('t2').value;
	  }else{
	  	valpartida=document.getElementById('t1').value;
	  }	  
        var cadena=ExtraePartida(text);
	  if (valpartida==cadena){
		return " style=background:#C0C0C0";
	  }else{
     		return " ";
	  }
}
function SeleccionaHijo(radio){
	document.getElementById(radio).checked=true;
}

//Corta la cadena en 40 digitos
function ExtraeDescripcionHijos(cadena)
{		
	if (cadena.length>40){
		cadena=cadena.substr(0,70);
		cadena=cadena+"...";
	}
	return cadena;
}


// Loads all icons that are used in the tree
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";
}
// Create the tree
function createTree(arrName, startNode, openNode) {
	nodes = arrName;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			if (nodes.length>1){
				document.write("<a style=color:#666633  href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + ExtraeDescripcionHijos(nodeValues[2]) + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"np4/SIICEX1/images/BDG/img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
			}else{
				//onclick=ListarDestinos('"+ExtraePartida(nodeValues[2])+"')
				document.write("<input "+ Radiocheked(nodeValues[2]) +" type=radio name=arbollst id="+ExtraePartida(nodeValues[2])+" value="+ExtraePartida(nodeValues[2])+"  \>");
			}
		} else document.write("<img src=\"np4/SIICEX1/images/BDG/img/base.gif\" align=\"absbottom\" alt=\"\" />Partida<br />");
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
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]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
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;
}
// Adds a new node to the tree
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: oc(" + 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: oc(" + 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 style=color:#666633 " + BackGround(nodeValues[2]) + " title='"+nodeValues[2]+"' href=\"" + nodeValues[3] + "\"  onclick=ListarDestinos('"+ExtraePartida(nodeValues[2])+"') onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			// 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\" />");
				
				//<img id=\"icon" + nodeValues[0] + "\" src=\"np4/SIICEX1/images/BDG/img/page.gif\" align=\"absbottom\" alt=\"Page\" />
				//icono de la pagina se cambio con radio button
				//+nodeValues[2]	
				//onclick=ListarDestinos('"+ExtraePartida(nodeValues[2])+"')
			} else document.write("<input "+ Radiocheked(nodeValues[2]) +" type=radio name=arbollst id="+ExtraePartida(nodeValues[2])+" value="+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 (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("div" + 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';
	}
}
// 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;
}
/********************BASE DE DATOS GLOBAL***********************/
