function meterOpcion(dato,lista)
{		
    var aux;
    aux = new Option(dato,dato,'','selected');
    //alert(dato);
		//alert(lista.options[0].value);
		lista.options[0]=aux ;
}

function campoSecundario(dato,lista,boton)
{
    var aux;
    var n;
    aux = new Option(dato,dato,'','selected');
		n = lista.options.length ;
    // Se comprueba el nombre del boton y segun sea
		// '[ add ]' o '[ replace ]'
		// 
		if (boton.value == '[ add ]')
    {
        // Aqui se inserta un elemento en el primer sitio
        // del select y despues se le cambia el nombre al boton						
        lista.options[n]=aux ;
        boton.value = "[ replace ]"
		}
    else
    {
        lista.options[n-1]=aux ;
		}
}

function meter(dato,lista)
{
    var res;
    res = lista+dato+"\n";
    return(res) ;
}

function sacarUltimo(lista)
{
    var arr;
    var res;
    arr = lista.split("\n") ;
    res = new String();
    for(i=0;i<(arr.length-2);i++)
    {
        res = res + arr[i] ;
    }
    return(res) ;
}

function isExtXml(cadena)
{
    var cont;
    var extension;
    var res;
    cont = cadena.lastIndexOf('.');
    extension = cadena.substring(cont+1,cadena.length);
    if (extension == "xml")
    {
        esxml=true;
    }
    else
    {
        esxml=false;
    }
    if (esxml != true )
    {
        alert("WARNING! The file does not have extension xml") ;
        res=no;          		
    }
    else
    {
        res=si;
    }
		return (res);
}

function verificarVaciosUpload(cadena)
{
    var pnt;
    var control;	
    var esxml;
    var cont;
    var extension;
    pnt = window.document.forms[0].elements ;                	      					
    control = true ;
		esxml = true;
    if (window.document.forms[0].nombreobject.value=="")
    {
        control=false;
        window.document.forms[0].nombreobject.focus();
		}
    else
    {
        if ((window.document.forms[0].elFichero.value=="") && (window.document.forms[0].descripcion.value==""))
        {
            control=false;
            pnt[1].focus();	
			  }
        else
        {
            if (window.document.forms[0].elFichero.value!="")
            {
                cont = cadena.lastIndexOf('.');
                extension = cadena.substring(cont+1,cadena.length);
                if (extension == "xml")
                {
                    esxml=true;
                }
                else
                {
                    esxml=false;
                }
                if (esxml != true )
                {
                    alert("WARNING! The file does not have extension xml") ;
                }
            }
        }
    }
		if (control != true )
    {
        alert("WARNING! \nyou must fill all the fields of this formulary.") ;                		
    }
    else if (esxml == true)
    {
        window.document.forms[0].submit() ;
    }
			
}

function verificarVacios(texto)
{
    var i;
    var n;
    var pnt;
    var control;
    i = 0 ;    // inicio desde el que se empieza a comprobar.
    n = window.document.forms[0].elements.length ;
    pnt = window.document.forms[0].elements ;                	      					
    control = true ;
    while ((i != (n))&&(control!=false))
    {
        if (((pnt[i].name.indexOf('secondary'))!=0)&&(pnt[i].value == ""))
        { 				
				    control=false;
				    pnt[i].focus();
				}
				i += 1 ;
		}
    if (control != true )
    {
        alert(texto);
                     		
    }
    else
    {
        window.document.forms[0].submit();
    }
}

function verificarVaciosWorkflow(texto)
{
    var i;
    var n;
    var pnt;
    var control;
    i = 0 ;    // inicio desde el que se empieza a comprobar.
    n = 2;
    pnt = window.document.forms[0].elements ;                	      					
    control = true ;
    while ((i != (n))&&(control!=false))
    {
        if (((pnt[i].name.indexOf('secondary'))!=0)&&(pnt[i].value == ""))
        { 				
				    control=false;
				    pnt[i].focus();
				}
				i += 1 ;
		}
    if (control != true )
    {
        alert(texto);
                     		
    }
    else
    {
        window.document.forms[0].submit();
    }
}

function limCaracteres(cadena,tope)
{			
    if (cadena.length>tope)
    {
		    alert("WARNING!!! \n This field has a max of "+tope+" characters.");					
		}		
		return cadena.substring(0,tope);
}
	
//el primer parametro es la cadena para cortar y el segundo el corte:
function cortarCadena(cadena,cortador)
{
    var res;
    res = new Array();
    res = cadena.split(cortador) ;
    return res ;
}

function resetearFormulario()
{
    window.document.forms[0].reset();
}

function popup(archivo,titulo)
{
    ventana=window.open(archivo,titulo,'width=730,height=620 top=30,left=100,scrollbars=yes,location=no,resizable=no');
}

function sumarOpcion(lista,dato,id)
{
    var tam;
    var aux;
    tam = lista.length ;
    aux = new Option(dato,id,'','selected');
    lista.options[tam]=aux ;
}
        
function closewin()
{
    window.close();
}

function isExtXml(cadena)
{
    var cont;
    var extension;
    var esxml;
    cont = cadena.lastIndexOf('.');
    extension = cadena.substring(cont+1,cadena.length);
    if (extension == "xml")
    {
        esxml=true;
    }
    else
    {
        esxml=false;
    }
    if (esxml != true )
    {
        alert("WARNING! The file does not have extension xml") ;                		
    }
}

function VerificarVaciosYXml(cadena)
{
    var cont;
    var extension;
    var esxml;
    cont = cadena.lastIndexOf('.');
    extension = cadena.substring(cont+1,cadena.length);
    if (extension == "xml")
    {
        esxml=true;
    }
    else
    {
        esxml=false;
    }
    if (esxml != true )
    {
        alert("WARNING! The file does not have extension xml") ;                		
    }
    else
    {
        verificarVacios('WARNING! \nyou must fill all the fields of this formulary.');
    }
}

function VerificarVaciosYXmlWorkflow(cadena)
{
    var cont;
    var extension;
    var esxml;
    cont = cadena.lastIndexOf('.');
    extension = cadena.substring(cont+1,cadena.length);
    if (extension == "xml")
    {
        esxml=true;
    }
    else
    {
        esxml=false;
    }
    if (esxml != true )
    {
        alert("WARNING! The file does not have extension xml") ;                		
    }
    else
    {
        verificarVaciosWorkflow('WARNING! \nyou must fill all the fields of this formulary.');
    }
}

//funcion que abre una ventana emergente.
function displayHTML(form,name,tipo,origen,forbidden)
{
    var pag;
    pag=form+'?Objeto='+name+'&Tipo='+tipo+'&origen='+origen+'&forbiddenOut='+forbidden;
    win = window.open(pag, "", 'toolbar = no, status = yes,resizable=yes,scrollbars=yes');
}

//funcion que carga una pagina en la misma ventana.
function loadPage(pag,name,tipo,origen,forbidden)
{
    self.location.href=pag+'?Objeto='+name+'&Tipo='+tipo+'&origen='+origen+'&forbiddenOut='+forbidden
}

function loadName(path)
{
     var count;
     var indwindows;
     var indlinux;
     var nameObject;
     
     indwindows = path.lastIndexOf('\\');
     count=path.lastIndexOf('.')
     indlinux=path.lastIndexOf('/');
     if (indwindows!="")
     {
         nameObject= path.substring(indwindows+1,count);
         
     }
     else
     {
         nameObject=path.substring(indlinux+1,count);
     }

      window.document.forms[0].nameObject.value=nameObject;

}

function loadNameWorkflow(path)
{
     var count;
     var indwindows;
     var indlinux;
     var nameObject;
     
     indwindows = path.lastIndexOf('\\');
     count=path.lastIndexOf('.')
     indlinux=path.lastIndexOf('/');
     if (indwindows!="")
     {
         nameObject= path.substring(indwindows+1,count);
         
     }
     else
     {
         nameObject=path.substring(indlinux+1,count);
     }

     if (window.document.forms[0].name.value == "")
     {
      	window.document.forms[0].name.value+=nameObject;
     }

}

function Help()
{
    var cont;
    var descrip;

    cont = window.document.forms[0].services.value.lastIndexOf(':');
    descrip = window.document.forms[0].services.value.substring(cont+1,window.document.forms[0].services.value.length);
    window.document.getElementById('descrip').innerHTML=descrip;
}

function loadWorkflow(pag,id)
{
    self.location.href=pag+'?user='+id;
}

function enviar(form,url)
{
	    form.action = url;
		form.submit()
}

function enviar2(form,url)
{
	var id;
	id = form.executionSelect.options[form.executionSelect.selectedIndex].value;
	    form.action = url+'?id='+id;
		form.submit()
}

function enviarOpen(form,url)
{
	    form.action = url;
/*	    form.target = "_BLANK";*/
		form.submit()
}

function enviarOpenExecution(form,url)
{
/*	window.open ("executing.gif","W","menubar=0,titlebar=NO,resizable=1,width=220,height=130");*/
	    form.action = url;
/*	    form.target = "W";*/
		form.submit();
}

function verifGenericObjetc()
{
	var i;
    	var n;
    	var pnt;
    	var control;
    	i = 0 ;    // inicio desde el que se empieza a comprobar.
    	n = window.document.forms[0].elements.length ;
    	pnt = window.document.forms[0].elements ;                	      					
    	control = true ;
    	while ((i != (n))&&(control!=false))
    	{
        	if (pnt[i].value == "")
        	{ 	
        		if((i!=2) && (i!=4) && (i!=5))
        		{			
		    		control=false;
				pnt[i].focus();
				break;
			}
		}
		i += 1 ;
	}
    	if (control != true )
    	{
        	//alert(texto);
        	alert("WARNING! \nYou must fill all filelds (id and namespace are optional fields)");                		
    	}
    	else
    	{
        	window.document.forms[0].submit();
    	}
}

function testOnBlur(typeName,idElement)
{
	var element = window.document.getElementById(idElement);
	var val = element.value;
	if(typeName == 'Integer')
	{
		for(i=0;i<val.length;i++)
		{
			if(val.charAt(i)<'0' || val.charAt(i)>'9')
			{
				alert("ERROR\n the value must be an INTEGER");
				break;
			}
		}
	}
	if(typeName == 'Float')
	{
		for(i=0;i<val.length;i++)
		{
			if(val.charAt(i)!='.')
			{
				if(val.charAt(i)<'0' || val.charAt(i)>'9')
				{
					alert("ERROR\n the value must be an FLOAT");
					break;
				}
			}
		}
	}
}

function popupHas(pag,name,tipo,title,hasLevel)
{
	var temp = window.document.forms.elForm.has.value;
	var destination = pag+'?Objeto='+hasLevel+'&Tipo='+tipo+'&has='+temp;
	title=window.open(destination,title,'width=730,height=620 top=30,left=100,scrollbars=yes,location=no,resizable=no');
}

function addHas(elem,textt)
{
	var temp = elem.value;
	var temp2 = temp+textt;
	elem.value=temp2;
}
function actionNodeParent(menuId, actuatorId,action)
{
    var menu = window.parent.frames["MarcoIzq"].document.getElementById(menuId);
    var actuator =  window.parent.frames["MarcoIzq"].document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;
    var display = menu.style.display;
    if (action==1)
    {
        actuator.parentNode.style.backgroundImage ="url(../images/minus.gif)";
         menu.style.display ="block";
    }
    else
    {
        actuator.parentNode.style.backgroundImage ="url(../images/plus.gif)";
         menu.style.display ="none";
    }

    return false;


}
function openTreeLevel(parent,child,level)
{
    var idNodo;
    var num;
    
    if (parent=='Datatypes')
    {
       idNodo=parent.parent.frames[2].document.getElementById('firstParent').value;
       param1='Id'+idNodo+'Menu';
       param2='Id'+idNodo+'Actuator';
       if (level==1)
       {
            actionNodeParent(param1, param2,1);
       }
       else (level==2)
       {
            n=parseInt(idNodo)+1;
            param1_1='Id'+n+'Menu';
            param2_1='Id'+n+'Actuator';
            actionNodeParent(param1, param2,1);actionNodeParent(param1_1,param2_1,1);
            
       }
        
    }
    if (parent=='Services')
    {
        idNodo=window.parent.parent.frames[2].document.getElementById('secondParent').value;
        param1='Id'+idNodo+'Menu';
        param2='Id'+idNodo+'Actuator';
        if (level==1)
        {
            actionNodeParent(param1, param2,1);
        }
        else (level==2)
        {
            n=parseInt(idNodo)+1;
            param1_1='Id'+n+'Menu';
            param2_1='Id'+n+'Actuator';
            actionNodeParent(param1, param2,1);actionNodeParent(param1_1,param2_1,1);
        }
    }
    return false;
}
function checkRadio(typeRadio){
    var i
    var check;
    for (i=0;i<typeRadio.length;i++){
       if (typeRadio[i].checked)
       {
          check=i;
       }
    }
    return check;
}
function printPage(iframe_name)
{
  
  if (eval('window.frames.'+iframe_name+'.print'))
  {
      eval('window.frames.'+iframe_name+'.print();');
  }  
  else
  {
      alert("Sorry, impossible print. Update your navigator.");
  }
    
    
}
var xmlhttp
function createXML()
{
    if (window.ActiveXObject)
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

    }
    else if (window.XMLHttpRequest)
       {
           xmlhttp=new XMLHttpRequest();

        }

}
function loadServicesProv()
{
     var varServ=window.document.forms[0].services.value;
     window.parent.frames["listServices"].location='servicesProvider.php?services='+varServ;
     window.document.forms[0].activate.disabled=false;
     window.document.forms[0].deleted.disabled=false;
}
function loadXMLDocGet(url)
{
    if (window.ActiveXObject)// code for IE
    {
        if (xmlhttp)
        {
            xmlhttp.onreadystatechange=xmlhttpChangeGet;
            xmlhttp.open("GET",url,true);
            xmlhttp.send();
        }
    }
    else // code for Mozilla, etc.
    {
        if (window.XMLHttpRequest)
        {
            if (xmlhttp)
            {
                xmlhttp.onreadystatechange=xmlhttpChangeGet;
                xmlhttp.open("GET",url,true);
                xmlhttp.send(null);
            }
        }
   }   
}
function xmlhttpChangeGet()
{
// if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4)
    {
     // if "OK"
        if (xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
   
        }
        else
        {
            alert("Problem retrieving XML data")
        }
    }
}
function actionOnService(url,where)
{
   i=checkRadio(window.parent.frames["listServices"].document.forms[0].selectServ);
   varOrdered=window.parent.frames["listServices"].document.forms[0].selectServ[i].value;
   if (where=='register')
   {
       window.open(url+'?idServ='+varOrdered+'&where=register'); 
   }
   else
   {
       
       parameters=url+'?&idServ='+varOrdered+'&where=delete';
       loadXMLDocGet(parameters);
       //alert("despues de load"); 
   }
   
   
}
function disableButton()
{
    window.document.forms[0].activate.disabled=true;
    window.document.forms[0].deleted.disabled=true;

}

function checkRadioB(typeRadio){
    var i
    var check;
    check='none';
    //alert(typeRadio.length);
    for (i=0;i<typeRadio.length;i++){
       if (typeRadio[i].checked)
       {
          check=i;
       }
    }
    return check;
}
function sendValues(where)
{   
   var item;
   //window.document.forms[0].nombreobject.value 
  
   if (where=='Inputs')
   {
       var i=checkRadioB(window.document.forms[0].inputs);
       item=window.document.forms[0].inputs[i].value;
  //     alert(item);
        window.parent.parent.frames["services"].location='resultServices.php?input='+item+'&output=&type=';
        window.parent.parent.frames["services"].frames["resultServ"].location='services.php?input='+item+'&output=&type=';
   }
   else if (where=='Outputs')
   {
       var i=checkRadioB(window.document.forms[0].outputs);
       item=window.document.forms[0].outputs[i].value;
       window.parent.parent.frames["services"].location='resultServices.php?input=&output='+item+'&type=';
       window.parent.parent.frames["services"].frames["resultServ"].location='services.php?input=&output='+item+'&type=';
   }
   else if (where=='Types')
   {
       var i=checkRadioB(window.document.forms[0].typesServ);
       item=window.document.forms[0].typesServ[i].value;
       window.parent.parent.frames["services"].location='resultServices.php?input=&output=&typesServ='+item;
       window.parent.parent.frames["services"].frames["resultServ"].location='services.php?input=&output=&typesServ='+item;
   }
}
function reloadAllTree(treeIn, treeOut, treeTyp)
{
    window.parent.frames["search"].frames[0].location.href=treeIn;
    window.parent.frames["search"].frames[1].location.href=treeTyp;
    window.parent.frames["search"].frames[2].location.href=treeOut;
    
}

function resetTree(nameSession, nameRadio)
{
    //pongo todos los radio a off.
    //numRadio=window.document.forms[0].inputs.length;
    eval('numRadio=window.document.forms[0].'+nameRadio+'.length;');
    //ind=window.document.forms[0].inputs.checked;
    //alert(ind);
    for (i=0; i < (numRadio) ; i++ )
    {
        window.document.forms[0].elements[i].checked=false;
    }
    //window.parent.frames["services"].location='resultServices.php?delete='+nameSession;
    window.parent.parent.frames["services"].location='resultServices.php?delete='+nameSession;
    window.parent.parent.frames["services"].frames["resultServ"].location='services.php?delete='+nameSession;
  
    //hago en segundo plano una llamada que borra la variable de session que le mande.
   // parameters='name='+nameSession;
   // alert(parameters);
   // url='deleteSession.php?';
   // loadXMLDoc(url,parameters);
}
function loadPageSearch(origen,varServ)
{
    if (origen=='Help')
    {
        window.open('../help/Services/showService.php?id='+varServ);
    }
    else if (origen=='Services')
    {
        window.parent.parent.location='../../index.php?search=&file=generateForm.php?services='+varServ;
    }


}

function onloadTree()
{
    window.parent.parent.location='../../index.php';
}
function onloadPageForSearch(numServ,origen)
{
    var res=false;
    if (numServ>1) 
    {
        for (i=0;i<window.frames[0].document.forms[0].elements.length;i++)
        {
            if ((window.frames[0].document.forms[0].elements[i].type=="radio") && (window.frames[0].document.forms[0].elements[i].checked))
            {
                varServ=window.frames[0].document.forms[0].resul[i].value;
                loadPageSearch(origen,varServ);
                res=true;
            }
        }
    } 
    else if (numServ==1)
    {
        if (window.frames[0].document.forms[0].resul.checked)
        {
           varServ=window.frames[0].document.forms[0].resul.value;
           loadPageSearch(origen,varServ);
            res=true;
        }
        
        
    }
    if ((numServ>0) && (res==false))
    {
      alert("Should choose a service.");
    }
}
function checkValueBoolean(value)
{
    var valueUpper=value.toUpperCase();
    var res;
    res=false;
    if ((valueUpper=='TRUE') || (valueUpper=='FALSE') || (valueUpper==""))
    {
        res=true;
    }
    return res;
}
function checkSecondaryBoolean()
{
    var i;
    var n;
    var elements;
    var control;
    var text;
    var nameOfElements;
    i = 0 ;    // inicio desde el que se empieza a comprobar.
    n = window.document.forms[0].elements.length ;
    elements = window.document.forms[0].elements ;                	      					
    control = true;
    while ((i != (n))&&(control!=false))
    {
        nameOfElements=elements[i].name.toUpperCase(); 
        if ((nameOfElements.indexOf('BOOLEAN')!=-1)&& (!checkValueBoolean(elements[i].value)))
        { 				
				    control=false;
				    elements[i].focus();
				}
				i += 1 ;
		}
    if (control != true )
    {
        alert("Error, the value type is not correct you must type/introduce a boolean.");
                     		
    }
    else
    {
       
        text='WARNING! \nyou must fill all the fields of this formulary.';
        verificarVacios(text);
    }
}

