// ------------------------------------------ \\

function delfile(idfile,idchk)
{
	alert(idfile);
	alert(idchk);
	var chk = document.getElementById(idchk);
	if(chk.checked){
		//rese();
		document.getElementById('idchk').style.display='none';
	}
	else
	{
		document.getElementById('idfile').style.display='block';
	}
}


// ------------------------------------------ \\




// ------------------------------------------ \\


//<![CDATA[

	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;
			} 
		}
	}
	
	function busca(texto)
	{
		for(i=0;i<texto.length;i++)
		{
			if(texto.charAt(i)=="?") return true;
		}
		return false
	}
	
	function cargarcombo (url, comboAnterior, element_id) 
	{
		var element =  document.getElementById(element_id);
		var valordepende = document.getElementById(comboAnterior)
		var x = valordepende.value;
		if(busca(url) == false)
		{
			var fragment_url = url+'?id='+x;
		}
		else
		{
			var fragment_url = url+'&id='+x;
		}
		element.innerHTML = ' '; 
		
		//var contenidoprov;
		
		peticion.open("GET", fragment_url); 
		peticion.onreadystatechange = function() { 
			if (peticion.readyState == 4)
				element.innerHTML = peticion.responseText;
		} 
		peticion.send(null); 
	}
//]]>

function func_mostrarnif ()
{
	if (document.getElementById("pais").value == "73" && document.getElementById("tipus1").checked)
	{
		document.getElementById("mostrarnif").style.display = "block";  
	}
}
function func_mostrarnif2 ()
{
	document.getElementById("mostrarnif").style.display = "block";  
}
function func_ocultarnif ()
{
	document.getElementById("mostrarnif").style.display = "none";  
}
function func_mostrarstd ()
{
		document.getElementById("mostrarstd").style.display = "block";  
}
function func_ocultarstd ()
{
	document.getElementById("mostrarstd").style.display = "none";  
}
function func_mostrarcif ()
{
		document.getElementById("mostrarcif").style.display = "block";  
}
function func_ocultarcif ()
{
	document.getElementById("mostrarcif").style.display = "none";  
}
function func_mostrarbussines ()
{
	if(document.getElementById('tipus2').checked)
	{
		document.getElementById("mostrarbussines").style.display = "block";  
		if(document.getElementById('pais').value!=75)
		{
			func_mostrarcif ();
		}
		else
		{
			func_ocultarcif ();
		}
	}
}
function func_ocultarbussines ()
{
	document.getElementById("mostrarbussines").style.display = "none";
	func_ocultarcif ();	
}
// ------------------------------------------ \\

// valida los nombres de las etiquetas
function validarvaloratributo (numeroatributo)
{
	var valorvalidado = document.getElementById("valoratributo"+numeroatributo).value;
	valorvalidado = valorvalidado.replace ("  ", " ");
	valorvalidado = valorvalidado.replace (";", "");
	document.getElementById("valoratributo"+numeroatributo).value = valorvalidado;
}

// elimina valores del atributo
function eliminarvaloratributo(valor, numeroatributo)
{	
	// convierto la cadena en un array
	var cadena = document.getElementById("valoresatrib"+numeroatributo).value;
	cadena = cadena.split(";");
	// inicializo todo
	document.getElementById("valoresatributo"+numeroatributo).innerHTML = "";
	document.getElementById("valoresatrib"+numeroatributo).value = "";	
	var k = 0;
	// recorro el array
	for (var i=0;i<cadena.length-1;i++)
	{
		// si el campo es distinto al que he pulsado para eliminar, lo añado
		j=i+1;		
		if (j != valor && j<=25)
		{
			k = k+1;						
			document.getElementById("valoresatrib"+numeroatributo).value = document.getElementById("valoresatrib"+numeroatributo).value+cadena[i]+";";
			valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+k+"','"+numeroatributo+"')\">(x)</a></p>";
			document.getElementById("valoresatributo"+numeroatributo).innerHTML = document.getElementById("valoresatributo"+numeroatributo).innerHTML+valoratributo+"\n";			
			// muestro el enlade de añadir para que pueda introducir más valores
			//document.getElementById("anadir"+numeroatributo).style.visibility = "visible";				
		}
	}	
	// situo el cursor en el input para introducir un nuevo valor para este atributo
	document.getElementById("valoratributo"+numeroatributo).focus();
}

var cantidadatributosuno = "";
var cantidadatributosdos = "";
var cantidadatributostres = "";
var cantidadatributoscuatro = "";
// añado un valor al atributo
function anadirvaloresatributo(numeroatributo)
{			
	// si estoy editando la oferta
	if (numeroatributo == "editaruno")
	{
		cadena = document.getElementById("valoresatribuno").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			if (j<=25)
			{				
				valoratributo = "<p><strong>"+cadena[i]+"</strong><a href=\"javascript:eliminarvaloratributo('"+j+"','uno')\">(x)</a></p>";
				document.getElementById("valoresatributouno").innerHTML = document.getElementById("valoresatributouno").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadiruno").style.visibility  = "hidden";
				}
			}				
		}				
	}
	if (numeroatributo == "editardos")
	{
		cadena = document.getElementById("valoresatribdos").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			if (j<=25)
			{				
				valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+j+"','dos')\">(x)</a></p>";
				document.getElementById("valoresatributodos").innerHTML = document.getElementById("valoresatributodos").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}				
		}				
	}
	if (numeroatributo == "editartres")
	{
		cadena = document.getElementById("valoresatribtres").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			if (j<=25)
			{				
				valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+j+"','tres')\">(x)</a></p>";
				document.getElementById("valoresatributotres").innerHTML = document.getElementById("valoresatributotres").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadirtres").style.visibility  = "hidden";
				}
			}				
		}				
	}
	
	if (numeroatributo == "editarcuatro")
	{
		cadena = document.getElementById("valoresatribcuatro").value;	
		cadena = cadena.split(";");				
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			if (j<=25)
			{				
				valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+j+"','cuatro')\">(x)</a></p>";
				document.getElementById("valoresatributocuatro").innerHTML = document.getElementById("valoresatributocuatro").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadircuatro").style.visibility  = "hidden";
				}
			}				
		}				
	}
	
	if (document.getElementById("valoratributo"+numeroatributo) && document.getElementById("valoratributo"+numeroatributo).value != "")	
	{				
		// inicializo las variables
		var j = "";
		var repetido = "no";		
		document.getElementById("valoresatributo"+numeroatributo).innerHTML = "";	
		// asigno el valor introducido a una variable
		var valoratributo = document.getElementById("valoratributo"+numeroatributo).value;	
		// introduzco el valor en el input para luego guardarlos
		document.getElementById("valoresatrib"+numeroatributo).value = document.getElementById("valoresatrib"+numeroatributo).value+valoratributo+";";
		// convierto la cadena en un array
		var cadena = document.getElementById("valoresatrib"+numeroatributo).value;
		cadena = cadena.split(";");			
		
		// miro si el nuevo valor ya está en el array		
		for(ind=0; ind<cadena.length-2; ind++)
    	{    		    		    		
    		if (document.getElementById("valoratributo"+numeroatributo).value == cadena[ind])
    		{    			    		 
    			repetido = "si";       					    			    		
    		}   		    	
    	}
    	// si ya estaba en el array lo borro del input para que luego no lo pinte
    	if (repetido == "si")
    	{
    		cadena.splice(ind,1); 	   			    		
			document.getElementById("valoresatrib"+numeroatributo).value = cadena;  			
			// reemplazamos la coma por el puntoycoma en el input
			out = ","; // letra a reemplazar
			add = ";"; // letra que reemplazará
			cadena = document.getElementById("valoresatrib"+numeroatributo).value;			
			// ponemos un bucle porque hay que reemplazar muchas veces
			while (cadena.indexOf(out)>-1)
			{	
				cadena = cadena.replace(out,add)			
			}
			cadena = cadena.replace(";;",";");
			document.getElementById("valoresatrib"+numeroatributo).value = cadena;			
			cadena = cadena.split(";");	
    	}

		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			
			// introduzco el valor en el div para mostrarlos			
			j=i+1;						
			if (j<=25)
			{		
				valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+j+"','"+numeroatributo+"')\">(x)</a></p>";				
				document.getElementById("valoresatributo"+numeroatributo).innerHTML = document.getElementById("valoresatributo"+numeroatributo).innerHTML+valoratributo+"\n";
				
				// situo el cursor en el input para introducir un nuevo valor para este atributo
				document.getElementById("valoratributo"+numeroatributo).focus();
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadir"+numeroatributo).style.visibility  = "hidden";
				}
			}				
		}
		// vacío el input en el que se insertan los valores
		document.getElementById("valoratributo"+numeroatributo).value = "";
	}
	if (numeroatributo == "tres" && document.getElementById(valoratributotres).value != '')
	{
		//document.getElementById('divanadirtres').style.visibility='visible';
		// inicializo las variables
		var j = "";
		var repetido = "no";		
		document.getElementById("valoresatributo"+numeroatributo).innerHTML = "";	
		// asigno el valor introducido a una variable
		var valoratributo = document.getElementById("valoratributo"+numeroatributo).value;

		// introduzco el valor en el input para luego guardarlos
		document.getElementById("valoresatrib"+numeroatributo).value = document.getElementById("valoresatrib"+numeroatributo).value+valoratributo+";";
		document.getElementById("valoresatrib"+numeroatributo).value = document.getElementById("valoresatrib"+numeroatributo).value.replace(";;", ";");
		
		// convierto la cadena en un array
		var cadena = document.getElementById("valoresatrib"+numeroatributo).value;								
		cadena = cadena.split(";");					
		// miro si el nuevo valor ya está en el array		
		for(ind=0; ind<cadena.length-2; ind++)
    	{
    		if (document.getElementById("valoratributo"+numeroatributo).value == cadena[ind])
    		{    			    		 
    			repetido = "si";       					    			    		
    		}   		    	
    	}    	
    	// si ya estaba en el array lo borro del input para que luego no lo pinte
    	if (repetido == "si" )
    	{
    		cadena.splice(ind,1); 	   			    		
			document.getElementById("valoresatrib"+numeroatributo).value = cadena;  			
			// reemplazamos la coma por el puntoycoma en el input
			out = ","; // letra a reemplazar
			add = ";"; // letra que reemplazará
			cadena = document.getElementById("valoresatrib"+numeroatributo).value;			
			// ponemos un bucle porque hay que reemplazar muchas veces
			while (cadena.indexOf(out)>-1)
			{	
				cadena = cadena.replace(out,add)			
			}
			cadena = cadena.replace(";;",";");
			document.getElementById("valoresatrib"+numeroatributo).value = cadena;			
			cadena = cadena.split(";");	
    	}
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;						
			if (j<=25)
			{			
				valoratributo = "<p><strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+j+"','"+numeroatributo+"')\">(x)</a></p>";
				document.getElementById("valoresatributo"+numeroatributo).innerHTML = document.getElementById("valoresatributo"+numeroatributo).innerHTML+valoratributo+"\n";
				
				// situo el cursor en el input para introducir un nuevo valor para este atributo
				document.getElementById("valoratributo"+numeroatributo).focus();
				// oculto el enlade de añadir para que no pueda introducir más valores
				if (j==25)
				{
					document.getElementById("anadir"+numeroatributo).style.visibility  = "hidden";
				}
			}				
		}
		// vacío el input en el que se insertan los valores
		document.getElementById("valoratributo"+numeroatributo).value = "";
	}
}


// ------------------------------------------ \\

		function initialize()
		{
			var j=1;
			while(document.getElementById('load'+j))
			{
				if(document.getElementById('archivo'+j).value)
					document.getElementById('load'+j).style.visibility='visible';
				j=j+1;
			}
		}

// ------------------------------------------ \\

	function strrev (string) 
	{
    	var ret = '', i = 0;
 	    string += '';
    	for ( i = string.length-1; i >= 0; i-- )
    	{
    		ret += string.charAt(i);
    	}
 	    return ret;
	}
	
// ------------------------------------------ \\

	function nombre(fic,elemento)
	{
	  var fic = fic.split('\\');
	  var aux=strrev (fic);
	  var ext3=strrev (aux[0]+aux[1]+aux[2]);
	  var ext4=strrev (aux[0]+aux[1]+aux[2]+aux[3]);
	  if(ext3!='doc' && ext3!='txt' && ext3!='rtf' && ext4!='html')
	  {
	  	alert('Los documentos deben estar en formato doc, txt, rtf o html.');
	  	document.getElementById("elemento").value=='';
	  }
	}
// ------------------------------------------ \\
	
	function pass_alert()
	{
		alert("Gato");
		dni = document.getElementById('nif').value;
		alert(dni);
		return 0;
		
	}
// ------------------------------------------ \\

	function nif_alert(situacion, error, error2, error3)
	{
		var pais = document.getElementById('pais').value;
		var nif = document.getElementById('nif').value;
		var id = document.getElementById('id').value;
		if(id)
		{
			if(validarpassolvido2(error2, error3)==0)
				return 0;
		}
		else
		{
			if(validarpassolvido(error2, error3)==0)
				return 0;
		}
		if(situacion==1)
		{
			if(document.getElementById('tipus1').checked)
				var tipo = 1;
			else
				var tipo = 2;
		}
		else
		{
			var tipo = document.getElementById('tipous').value;
		}
		if(pais==73 && tipo==1 && document.getElementById('nif').value)
		{
			var dni = document.getElementById('nif').value;
			numero = dni.substr(0,dni.length-1);
			let = dni.substr(dni.length-1,1);
			numero = numero % 23;
			letra='TRWAGMYFPDXBNJZSQVHLCKET';
			letra=letra.substring(numero,numero+1);
			if (letra!=let)
			{
				return 0;
			}
			else
			{
				return validarEmail(document.getElementById('mail1').value, error);
			}
		}
		else
		{
			if(tipo==1)
			{
				return validarEmail(document.getElementById('mail1').value, error);
			}
			else
			{
				return validarEmail(document.getElementById('mail1').value, error);
			}
		}
	}
	function nif_alert2(situacion, error1, error2)
	{
		var pais = document.getElementById('pais').value;
		var nif = document.getElementById('nif').value;
		/*var id = document.getElementById('id').value;
		if(id)
		{
			if(validarpassolvido(error3, error4)==0)
				return 0;
		}
		else
		{
			if(validarpassolvido2(error3, error4)==0)
				return 0;
		}*/
		if(situacion==1)
		{
			if(document.getElementById('tipus1').checked)
				var tipo = 1;
			else
				var tipo = 2;
		}
		else
		{
			var tipo = document.getElementById('tipous').value;
		}
		if(pais==73 && tipo==1 && document.getElementById('nif').value)
		{
			var dni = document.getElementById('nif').value;
			numero = dni.substr(0,dni.length-1);
			let = dni.substr(dni.length-1,1);
			numero = numero % 23;
			letra='TRWAGMYFPDXBNJZSQVHLCKET';
			letra=letra.substring(numero,numero+1);
			if (letra!=let)
			{
				alert(error2);
				return 0;
			}
			else
			{
				return validarEmailDos(document.getElementById('mail1').value, error1);
			}
		}
		else
		{
			if(tipo==1)
			{
				return validarEmailDos(document.getElementById('mail1').value, error1);
			}
			else
			{
				return validarEmailDos(document.getElementById('mail1').value, error1);
			}
		}
	}
	function validarEmail(valor, error) {
		//var filter=/^[A-Za-z0-9_][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/; 
		var filter=/^[A-Za-z0-9_][A-Za-z0-9_]*@\w+([\.-]?\w+)+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if(document.getElementById('mail1').value==document.getElementById('mail2').value)
		{
			if (filter.test(valor))
			{
				return 1;
			}
			else
			{
				alert(error);
				return 0;
			}
		}
		else
		{
			alert(error);
			return 0;
		}
	}
	function validarEmailDos(valor, error) {
		//var filter=/^[A-Za-z0-9_][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/; 
		var filter=/^[A-Za-z0-9_][A-Za-z0-9_]*@\w+([\.-]?\w+)+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (filter.test(valor))
		{
			return 1;
		}
		else
		{
			alert(error);
			return 0;
		}
	}
	function validarpassolvido(mensajeuno, mensajedos)
	{
		var pass = document.getElementById('pass1').value;
		var passrep = document.getElementById('pass2').value;
		if(pass.length>7)
		{
			if(pass==passrep)
			{
				return 1;
			}
			else
			{
				alert(mensajeuno);
				return 0;
			}
		}
		else
		{
			alert(mensajedos);
			return 0;
		}
	}
	function validarpassolvido2(mensajeuno, mensajedos)
	{
		var pass = document.getElementById('pass1').value;
		var passrep = document.getElementById('pass2').value;
		if(pass.length>7 || pass.length==0)
		{
			if(pass==passrep)
			{
				return 1;
			}
			else
			{
				alert(mensajeuno);
				return 0;
			}
		}
		else
		{
			alert(mensajedos);
			return 0;
		}
	}

// ------------------------------------------ \\

