//////////////////////////////////////////////////////////////
// AÑADIR USUARIOS
//////////////////////////////////////////////////////////////

var contadoru = 0;
var numerousuarios = 0;

function anadirusuarios()
{
	if (document.getElementById('usuarios').value!='')
	{	
		contadoru = contadoru + 1; 
		numerousuarios = numerousuarios+1;
		document.getElementById('usuariosinsertar').value = document.getElementById('usuariosinsertar').value+document.getElementById('usuarios').value+'<br>';		
		
		document.getElementById('usuariosmostrar').innerHTML = document.getElementById('usuariosmostrar').innerHTML+'<font id="'+contadoru+'u" style=\'color:red; cursor:pointer\' onclick=\'eliminarusuarios("'+contadoru+'u",numerousuarios)\'>'+document.getElementById('usuarios').value+'<br></font>';

	
	}

}




//////////////////////////////////////////////////////////////
// ELIMINAR USUARIOS
//////////////////////////////////////////////////////////////

function eliminarusuarios(nueva)
{	
	
	//alert(nueva);
	//alert(numerousuarios);
	//alert(document.getElementById(i+'u').innerHTML);
	var insertaru = '';
	for(i=1;i<=numerousuarios;i++)
	{	
		
		//alert (document.getElementById(i+'u').innerHTML.replace('BR','br'))
		if (i+'u'!=nueva)
			insertaru = insertaru + document.getElementById(i+'u').innerHTML.replace('BR','br');	
	
			
			//alert(document.getElementById(i+'u').innerHTML);
	}
	document.getElementById(nueva).innerHTML='';
	document.getElementById('usuariosinsertar').value = insertaru;
	
}




//////////////////////////////////////////////////////////////
// EDITAR USUARIOS
//////////////////////////////////////////////////////////////

function editarusuarios(numerousuarios)
{
	
	if (contadoru=='0' || contadoru=='')
		contadoru = numerousuarios+1;
	if (document.getElementById('usuarios').value!='')
	{	
		numerousuarios = numerousuarios+1;
		document.getElementById('usuariosinsertar').value = document.getElementById('usuariosinsertar').value+document.getElementById('usuarios').value+'<br>';
		document.getElementById('usuariosmostrar').innerHTML = document.getElementById('usuariosmostrar').innerHTML+'<font id="'+contadoru+'u" style=\'color:red; cursor:pointer\' onclick=\'eliminarusuarioseditando("'+contadoru+'u")\'>'+document.getElementById('usuarios').value+'<br></font>';
		//document.getElementById('pais').value='';
		contadoru = contadoru+1;
	}
	
}




//////////////////////////////////////////////////////////////
// ElIMINAR USUARIOS EDITANDO USUARIOS
//////////////////////////////////////////////////////////////

function eliminarusuarioseditando(nueva, numerousuarios)
{	
	
	var insertaru = '';
	var mostraru = '';
	ideliminaru = '0';
	for(j=1;j<=1000;j++)
	{	
		if (document.getElementById(j+'u')) 
		{
		
			//alert (document.getElementById(j+'u').innerHTML.replace('BR','br'))
		
			
			if (j+'u' != nueva)
			{
			 	//alert(j+'p');
				ideliminaru++;
				insertaru = insertaru + document.getElementById(j+'u').innerHTML.replace('BR','br');
				mostraru = mostraru + '<font id="'+ideliminaru+'u" style=\'color:red; cursor:pointer\' onclick=\'eliminarusuarioseditando("'+ideliminaru+'u")\'>'+document.getElementById(j+'u').innerHTML+'</font>';		
			
			}
		
		}
		
	}	
	document.getElementById('usuariosinsertar').value = insertaru;
	document.getElementById('usuariosmostrar').innerHTML = mostraru;
	
}




//////////////////////////////////////////////////////////////
// AÑADIR CLIENTES
//////////////////////////////////////////////////////////////

var contadorc = 0;
var numeroclientes = 0;

function anadirclientes()
{
	if (document.getElementById('clientes').value!='')
	{	
		contadorc = contadorc + 1; 
		numeroclientes = numeroclientes+1;
		document.getElementById('clientesinsertar').value = document.getElementById('clientesinsertar').value+document.getElementById('clientes').value+'<br>';		
		
		document.getElementById('clientesmostrar').innerHTML = document.getElementById('clientesmostrar').innerHTML+'<font id="'+contadorc+'c" style=\'color:red; cursor:pointer\' onclick=\'eliminarclientes("'+contadorc+'c",numeroclientes)\'>'+document.getElementById('clientes').value+'<br></font>';

	
	}

}




//////////////////////////////////////////////////////////////
// ELIMINAR CLIENTES
//////////////////////////////////////////////////////////////

function eliminarclientes(nueva)
{	
	var insertarc = '';
	for(i=1;i<=numeroclientes;i++)
	{	
		if (i+'c'!=nueva)
			insertarc = insertarc + document.getElementById(i+'c').innerHTML.replace('BR','br');	
	}
	document.getElementById(nueva).innerHTML='';
	document.getElementById('clientesinsertar').value = insertarc;
	
}


//////////////////////////////////////////////////////////////
// EDITAR CLIENTES
//////////////////////////////////////////////////////////////

function editarclientes(numeroclientes)
{
	
	if (contadorc=='0' || contadorc=='')
		contadorc = numeroclientes+1;
	if (document.getElementById('clientes').value!='')
	{	
		numeroclientes = numeroclientes+1;
		document.getElementById('clientesinsertar').value = document.getElementById('clientesinsertar').value+document.getElementById('clientes').value+'<br>';
		
		document.getElementById('clientesmostrar').innerHTML = document.getElementById('clientesmostrar').innerHTML+'<font id="'+contadorc+'c" style=\'color:red; cursor:pointer\' onclick=\'eliminarclienteseditando("'+contadorc+'c")\'>'+document.getElementById('clientes').value+'<br></font>';
		//document.getElementById('pais').value='';
		contadorc = contadorc+1;
	}
	
}




//////////////////////////////////////////////////////////////
// ELIMINAR CLIENTES EDITANDO
//////////////////////////////////////////////////////////////

function eliminarclienteseditando(nueva, numeroclientes)
{	
	
	var insertarc = '';
	var mostrarc = '';
	ideliminarc = '0';
	for(j=1;j<=1000;j++)
	{	
		if (document.getElementById(j+'c')) 
		{
		
			if (j+'c' != nueva)
			{
			 	//alert(j+'p');
				ideliminarc++;
				
				//alert (document.getElementById(j+'c').innerHTML);
				insertarc = insertarc + document.getElementById(j+'c').innerHTML.replace('BR','br');
				mostrarc = mostrarc + '<font id="'+ideliminarc+'c" style=\'color:red; cursor:pointer\' onclick=\'eliminarclienteseditando("'+ideliminarc+'c")\'>'+document.getElementById(j+'c').innerHTML+'</font>';		
			
			}
		
		}
		
	}	
	document.getElementById('clientesinsertar').value = insertarc;
	document.getElementById('clientesmostrar').innerHTML = mostrarc;
	
}




//////////////////////////////////////////////////////////////
// AÑADIR OPCIONES
//////////////////////////////////////////////////////////////

var contadoro = 0;
var numeroopciones = 0;
//var longitud = new Array();
		
function anadiropciones()
{
	if (document.getElementById('opciones').value!='')
	{	
		contadoro = contadoro + 1; 
		numeroopciones = numeroopciones+1;
		
		document.getElementById('opcionesinsertar').value = document.getElementById('opcionesinsertar').value+document.getElementById('opciones').value+'<br>';		
		
		document.getElementById('opcionesmostrar').innerHTML = document.getElementById('opcionesmostrar').innerHTML+'<input type="checkbox" onclick=\'activopciones()\'  name="'+contadoro+'check" id="'+contadoro+'check"><font id="'+contadoro+'o" style=\'color:red; cursor:pointer\' onclick=\'eliminaropciones("'+contadoro+'o",numeroopciones)\'>'+document.getElementById('opciones').value+'<br></font>';

		document.getElementById('opciones').value = "";
		

		
		
		
		document.getElementById('opcionesactivas').value = document.getElementById('opcionesactivas').value+"0<br>";
	
		
// AQUI TENGO QUE HACER QUE SE QUEDEN ACTIVOS LOS DIV CUANDO AÑADO OTRA OPCION	
		
		var activar = document.getElementById('opcionesactivas').value.split("<br>");
		
		//activas = document.getElementById('opcionesactivas').value.replace("<br>", "< >");
		
		numeroopcionesactivas = substr_count(document.getElementById('opcionesactivas').value, '<br>');
		
		
		//var activar;
		var contador2=0;
	
	for(j=0;j<=1000;j++)
	{	
		
		
			
			 	//alert(j+'p');
				
				//alert (document.getElementById(j+'c').innerHTML);
				//insertaro = insertaro + document.getElementById(j+'o').innerHTML.replace('BR','br');
				
				
				if(document.getElementById(j+'check')){
					
					//alert("j: "+ j );
				
					//alert("activar: j:"+ activar[j] );
					
					if(activar[contador2]=="1"){
			
						//alert("Entra para activarlo");
						document.getElementById(j+'check').checked = true;
						
					
					}else{
				
						
						//alert("Entra para desactivarlo");
						document.getElementById(j+'check').checked = false;

					}
					
					contador2++;
				}
				
						
			
		
	}
		
		
		
		
		
		//alert(longitud[1]);
		
/////////////////////////////////////////////////////////////////////////////////
	





}

}

function substr_count(string,substring,start,length)
{
 var c = 0;
 if(start) { string = string.substr(start); }
 if(length) { string = string.substr(0,length); }
 for (var i=0;i<string.length;i++)
 {
  if(substring == string.substr(i,substring.length))
  c++;
 }
 return c;
}


//////////////////////////////////////////////////////////////
// ELIMINAR OPCIONES
//////////////////////////////////////////////////////////////

function eliminaropciones(nueva)
{	
	
	//alert(nueva);
	//alert(numerousuarios);
	//alert(document.getElementById(i+'u').innerHTML);
	var insertaro = '';
	var oactivas = '';
	
	for(i=1;i<=numeroopciones;i++)
	{	
		
		//alert (document.getElementById(i+'u').innerHTML.replace('BR','br'))
		if (i+'o'!=nueva){
			
			insertaro = insertaro + document.getElementById(i+'o').innerHTML.replace('BR','br');
			
			if(document.getElementById(i+'check')){
			if(document.getElementById(i+'check').checked){
			
			oactivas = oactivas + '1<br>';
			
			}else{
				
			oactivas = oactivas + '0<br>';	
			
			}
			}
			
		}else{
		
		var elem = document.getElementById(i+'check');
		elem.parentNode.removeChild(elem);
		//oactivas = oactivas + '2<br>';
		//alert(i+'check');
		
		}
		
		
		
		
		
	}
	document.getElementById(nueva).innerHTML='';
	//elem.parentNode.removeChild(nueva);
	document.getElementById('opcionesinsertar').value = insertaro;
	document.getElementById('opcionesactivas').value = oactivas;
	
	
}




//////////////////////////////////////////////////////////////
// ACTIVAR O DESACTIVAR OPCIONES
//////////////////////////////////////////////////////////////

function activopciones(modificando)
{
	
	
	//alert("Modificando: " + modificando);
	
	if(numeroopciones=="" || numeroopciones=="0"){
		
	
	if(modificando!=""){

		numeroopciones = modificando;
	}
	
	
	}
	
	
	
	document.getElementById('opcionesactivas').value ="";
	
	for(i=0;i<=10000;i++)
	{	
	
	if(document.getElementById(i+'check')){
		
		
		if(document.getElementById(i+'check').checked){
			
			//alert("Entra en checkeado");
			document.getElementById('opcionesactivas').value = document.getElementById('opcionesactivas').value+ "1<br>";
	
		}else{
	
			//alert("Entra en no checkeado");
			document.getElementById('opcionesactivas').value = document.getElementById('opcionesactivas').value+ "0<br>";
	
		}
		
		//alert("Entra dentro del if");
	}
	// Cierro el id donde compruebo que los check existen
	//alert("entra en el for");
	}
	
	
	//numeroopciones++;
	// Cierro el for
	
	//alert("entra en la funcion");
}





//////////////////////////////////////////////////////////////
// ELIMINAR OPCIONES EDITANDO
//////////////////////////////////////////////////////////////

function eliminaropcioneseditando(nueva, numeroopciones)
{	
	
	var insertaro = '';
	var mostraro = '';
	var oactivas = '';
	ideliminaro = '0';
	var activar = "";
	for(j=1;j<=1000;j++)
	{	
		
		if (document.getElementById(j+'o')) 
		{
			
			if (j+'o' != nueva)
			{
			 	//alert(j+'p');
				ideliminaro++;
				
				//alert (document.getElementById(j+'c').innerHTML);
				insertaro = insertaro + document.getElementById(j+'o').innerHTML.replace('BR','br');
				
				if(document.getElementById(j+'check')){
				if(document.getElementById(j+'check').checked){
			
				oactivas = oactivas + '1<br>';
				activar = 'checked="checked"';
				}else{
				
				oactivas = oactivas + '0<br>';	
				activar = "";
				}
				}
				
				
				
				mostraro = mostraro + '<input type="checkbox" onclick=\'activopciones('+ideliminaro+')\'  name="'+ideliminaro+'check" id="'+ideliminaro+'check" '+activar+'><font id="'+ideliminaro+'o" style=\'color:red; cursor:pointer\' onclick=\'eliminaropcioneseditando("'+ideliminaro+'o")\'>'+document.getElementById(j+'o').innerHTML+'</font>';		
			
			}else{
		
		var elem = document.getElementById(j+'check');
		elem.parentNode.removeChild(elem);
		//oactivas = oactivas + '2<br>';
		//alert(i+'check');
		
		
		}
		
	}	
}	
	document.getElementById('opcionesinsertar').value = insertaro;
	document.getElementById('opcionesmostrar').innerHTML = mostraro;
	document.getElementById('opcionesactivas').value = oactivas;

	//numeroopciones--;

}




//////////////////////////////////////////////////////////////
// EDITAR OPCIONES
//////////////////////////////////////////////////////////////

var activar;
	

function editaropciones(numeroop)
{
	
	if (contadoro==''){
		contadoro = numeroop+1;
		numeroopciones = numeroop+1;
		//alert(contadoro);
	}
		
	if (document.getElementById('opciones').value!='')
	{	
		numeroopciones =  numeroopciones+1;
		//alert("Numero opciones" + numeroopciones);
		document.getElementById('opcionesinsertar').value = document.getElementById('opcionesinsertar').value+document.getElementById('opciones').value+'<br>';
		
		
		//alert(document.getElementById('opcionesmostrar').innerHTML);
		contadoro = contadoro+1;
		
		document.getElementById('opcionesmostrar').innerHTML = document.getElementById('opcionesmostrar').innerHTML+'<input type="checkbox" onclick=\'activopciones()\' name="'+contadoro+'check" id="'+contadoro+'check"><font id="'+contadoro+'o" style=\'color:red; cursor:pointer\' onclick=\'eliminaropcioneseditando("'+contadoro+'o")\'>'+document.getElementById('opciones').value+'<br></font>';
		//document.getElementById('pais').value='';
		contadoro = contadoro+1;
	}
	
	
	document.getElementById('opcionesactivas').value = document.getElementById('opcionesactivas').value + '0<br>';
	
	
	
	if(document.getElementById('opcionesactivas').value!=""){
		
		//alert(document.getElementById('opcionesactivas').value);
		activar = document.getElementById('opcionesactivas').value.split("<br>");
		//alert ("Activar 0: " + activar[0]);
		//alert ("Activar 1: " + activar[1]);
		
	
	}			
	
	var contador2=0;
	
	for(j=0;j<=1000;j++)
	{	
		
		
			
			 	//alert(j+'p');
				
				//alert (document.getElementById(j+'c').innerHTML);
				//insertaro = insertaro + document.getElementById(j+'o').innerHTML.replace('BR','br');
				
				
				if(document.getElementById(j+'check')){
					
					//alert("j: "+ j );
				
					//alert("activar: j:"+ activar[j] );
					
					if(activar[contador2]=="1"){
			
						//alert("Entra para activarlo");
						document.getElementById(j+'check').checked = true;
						
					
					}else{
				
						
						//alert("Entra para desactivarlo");
						document.getElementById(j+'check').checked = false;

					}
					
					contador2++;
				}
				
						
			
		
	}	
	
	
	

	
	//document.getElementById('opcionesactivas').value = oactivas;
	//numeroopciones++;
	document.getElementById('opciones').value = "";
}





//////////////////////////////////////////////////////////////
// SELECT DEPENDIENTES
//////////////////////////////////////////////////////////////

	var peticion = false;
	var  testPasado = false;
	try
	{
  		peticion = new XMLHttpRequest();
  	}
  	catch (trymicrosoft) 
  	{
  		try 
  		{
  			peticion = new ActiveXObject("Msxml2.XMLHTTP");
  		}
  		catch (othermicrosoft) 
  		{
  			try
  			{
  				peticion = new ActiveXObject("Microsoft.XMLHTTP");
  			}
  			catch (failed)
  			{
  				peticion = false;
  			} 
  		}
	}

	if (!peticion)
		alert("ERROR AL INICIALIZAR!");
 

	function cargarCombo (url, comboAnterior, element_id) 
	{ 
	    //Obtenemos el contenido del div
	    //donde se cargaran los resultados
	    var element =  document.getElementById(element_id);
	    //Obtenemos el valor seleccionado del combo anterior
	    var valordepende = document.getElementById(comboAnterior);
	    var x = valordepende.value
	    //construimos la url definitiva
		//alert(document.getElementById('idproyecto').value);
	    if(document.getElementById('idproyecto').value == ""){
	    
	    //pasando como parametro el valor seleccionado
	    var fragment_url = url+'?ide='+x;
	    
	    }else{
	    	
	    var fragment_url = url+'?ide='+x+'&idpro='+document.getElementById('idproyecto').value;
	    	
	    }
	    
	    
	    
	    //abrimos la url 
		var Urll = location.href;
    	var cad;
		cad = Urll.substr(Urll.length-1,1);
		
		
		
			if(cad=="/"){

				peticion.open("GET", './'+fragment_url); 
				
			}else{
				
				peticion.open("GET", fragment_url); 
				
			}
	    
	    
	    
	      
	    peticion.onreadystatechange = function() { 
	        if (peticion.readyState == 4) {
		//escribimos la respuesta
		element.innerHTML = peticion.responseText;
	        } 
	    } 
	   peticion.send(null); 
	} 
	

	
	
//////////////////////////////////////////////////////////////
// AVISO DE ELIMINAR UN PROYECTO, DOCUMENTO...
//////////////////////////////////////////////////////////////		
	
function aviso(ideliminar, campoeliminar, propiedadeliminar){
	
	if (!confirm("ALERTA!! se va a eliminar "+propiedadeliminar)) 
	{
		return false;
	}
	else 
	{
		
		document.location = './'+campoeliminar+'&ideli='+ideliminar;
		//window.open('./'+campoeliminar+'&ideli='+ideliminar,'_parent');
		return true;
	}

}


//////////////////////////////////////////////////////////////
// AVISO DE ELIMINAR UNA EMPRESA...
//////////////////////////////////////////////////////////////		

function avisoempresa(ideliminar, campoeliminar, propiedadeliminar){
	
	if (!confirm("ALERTA!! se va a eliminar "+propiedadeliminar)) 
	{
		return false;
	}
	else 
	{
		
		document.location = './'+campoeliminar+'&ideliempresa='+ideliminar;
		//window.open('./'+campoeliminar+'&ideli='+ideliminar,'_parent');
		return true;
	}

}	
	
	
