	var msgAvisoImagenes='Se van a subir imagenes. El tiempo del proceso de alta dependerá de la cantidad y tamaño de dichas imagenes. ¿Quieres continuar con el envío de las fotos?';
	
	var MAX_HEIGHT = 57;
	var MAX_WIDTH = 57;
	
	// para eliminar la foto,
	// deja el input a vacio y la imagen contenedor
	function eliminarFoto(idElemento,accion,tipo){
		reemplazarFoto('',idElemento,accion,tipo);
	}
	
	// Iñaki - Jul 06.
	function InsertarFoto(id_input_que_lanza_el_evento,URIfoto,id_destino) // necesita haber añadido la libreria 'nodos.js' y la libreria 'cambiarevento.js' al documento.
	{
		//Borrando lo que hubiese.
		document.getElementById(id_destino).innerHTML='';
		// Metiendo la imagen en el div
		C=crear(id_destino+'_imagen','img',id_destino,'AB');
		if (!document.all) { document.getElementById(id_destino).innerHTML='La protección de seguridad de su navegador no ha permitido al sistema mostrarle la previsualización de imagen (opción sólo valida en Internet Explorer). Su imagen será salvada de la forma normal pero usted no puede previsualizarla.<br/>'; 
		// Metiendo el link para borrar.
				C=crear(id_destino+'_link','a',id_destino,'AB');
				C.innerHTML="Eliminar";
				C.href='#';
				Nuevo_evento="document.getElementById('" + id_destino + "').innerHTML=''; borrar_input_file('" + id_input_que_lanza_el_evento +"','"+id_destino+"');"
				Nuevo_evento+="return false;";
				cambiar_evento(id_destino+'_link','onclick',Nuevo_evento);
		}
		else{
				I=new Image;
				I.src=URIfoto;
				C.src=URIfoto;
				W=parseFloat(I.width);
				//H=parseFloat(I.height);        // No es necesario cambiar la altura ya que si no se fuerza se adapta proporcionalmente.
				if (W>80){
					//A=W/H;
					C.style.width='80px';
					//C.heigth=(80*A)+'px';
				}
				// Metiendo el link para borrar.
				C=crear(id_destino+'_link','a',id_destino,'AB');
				C.innerHTML="Eliminar";
				C.href='#';
				Nuevo_evento="document.getElementById('" + id_destino + "').innerHTML=''; borrar_input_file('" + id_input_que_lanza_el_evento +"','"+id_destino+"');"
				Nuevo_evento+="return false;";
				cambiar_evento(id_destino+'_link','onclick',Nuevo_evento)
				//Metiendo el link para restaurar.
				C=crear(id_destino+'_link2','a',id_destino,'AB');
				C.innerHTML="Restaurar";
				C.href='#';
				cambiar_evento(id_destino+'_link2','onclick',"document.getElementById('"+id_destino+"').innerHTML=''; InsertarFoto('"+id_input_que_lanza_el_evento+"','"+URIfoto+"','"+id_destino+"'); return false;")
		} // este es el end del else (document.all)
	}
	
	function borrar_input_file(id,id_destino)
	{
											IF=document.getElementById(id);
		NAME=IF.name;
																					padreIF = IF.parentNode;
											IF.id='temp';
		C=document.createElement('input');
		C.type ='file';
		C.accept='image/jpeg, image/jpg, image/pjpeg, image/gif, image/png';
																					padreIF.insertBefore(C, IF);
																					padreIF.removeChild(IF);
											IF.id=''; IF.name='';
		

		C.id=id; 
		C.name=NAME;
		Nuevo_evento="InsertarFoto(this.id,'file:///'+document.getElementById(this.id).value,'"+id_destino+"');";
		cambiar_evento(id,'onchange',Nuevo_evento);
			
	}

	
	
	// para reemplazar la foto
	// recibimos la URI, el id, la accion ('BORRAR', 'NUEVA', 'RESTAURAR')
	function reemplazarFoto(URIfoto,idElemento,accion,tipo){


		// los elementos de la imagen
		var imgFoto = document.getElementById('imgFoto_'+tipo+'_'+idElemento);
		var padreImgFoto = imgFoto.parentNode;	
				
		// los elementos del input
		var inputFoto = document.getElementById('inputFoto_'+tipo+'_'+idElemento);
		var tdInputFoto = inputFoto.parentNode;

		if(accion=='BORRAR'){
			
			// nos cargamos la imagen	
			
			imgFoto.parentNode.removeChild(imgFoto);
			//var backupImgFoto=document.getElementById('backupImgFoto_'+idElemento);
			if(document.getElementById('anchorImgFoto_'+tipo+'_'+idElemento)){
				var anchorImgFoto = document.getElementById('anchorImgFoto_'+tipo+'_'+idElemento);
				var padreAnchorImgFoto = anchorImgFoto.parentNode;	
				anchorImgFoto.parentNode.removeChild(anchorImgFoto);
				
				
			}
			
			if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
				document.getElementById('inputBorrarFoto_'+tipo+'_'+idElemento).value='S';
				
			}
		}
		else if(accion=='RESTAURAR'){
			// nos cargamos la imagen					
			imgFoto.parentNode.removeChild(imgFoto);
			document.getElementById('inputBorrarFoto_'+tipo+'_'+idElemento).value='N';
			//var backupImgFoto=document.getElementById('backupImgFoto_'+idElemento);	
		}
		else if(accion=='NUEVA'){
			if(URIfoto!='file:///'){
				// nos cargamos la imagen					
				imgFoto.parentNode.removeChild(imgFoto);
				//if(document.getElementById('backupImgFoto_'+idElemento)){
				//var backupImgFoto=document.getElementById('backupImgFoto_'+idElemento);
				if(document.getElementById('anchorImgFoto_'+tipo+'_'+idElemento)){
					var anchorImgFoto = document.getElementById('anchorImgFoto_'+tipo+'_'+idElemento);
					var padreAnchorImgFoto = anchorImgFoto.parentNode;	
					anchorImgFoto.parentNode.removeChild(anchorImgFoto);
					padreImgFoto = document.getElementById('tdImgFoto_'+tipo+'_'+idElemento)
				}
				//}
				if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
					document.getElementById('inputBorrarFoto_'+tipo+'_'+idElemento).value='S';
				}
				else{
					document.getElementById('inputBorrarFoto_'+tipo+'_'+idElemento).value='N';
				}
			}
		}


		//creamos la imagen							
		imgFoto = document.createElement("img");
		imgFoto.border = 1;
		imgFoto.id = 'imgFoto_'+tipo+'_'+idElemento;
		imgFoto.name='IMG_'+tipo+'_FOTO_'+idElemento;
							
		var altImagen='Foto del usuario / anuncio';
		imgFoto.alt=altImagen;	
		
		if(accion=='BORRAR'){
			if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
				padreAnchorImgFoto.appendChild(imgFoto);
			}
			else{
				padreImgFoto.appendChild(imgFoto);
				
			}
		}
		else if(accion=='RESTAURAR'){
			padreImgFoto.appendChild(imgFoto);
		}
		else if(accion=='NUEVA'){
			if(URIfoto!='file:///'){
				if(document.getElementById('anchorImgFoto_'+tipo+'_'+idElemento)){
					padreAnchorImgFoto.appendChild(imgFoto);
				}
				else{
					padreImgFoto.appendChild(imgFoto);
				}
			}
		}
			
		// borrar o reemplazar
		if(URIfoto == ''){														
			imgFoto.src = 'http://'+getHostname()+'/Images/sinfoto.gif';
					
			// nos cargamos el input y lo reconstruimos
			tdInputFoto.removeChild(inputFoto);
			var nuevoFile = document.createElement("input");
			nuevoFile.type = 'file';
			nuevoFile.name = tipo+'_FOTO_'+idElemento;
			nuevoFile.accept = 'image/jpeg,image/gif';
			nuevoFile.id = 'inputFoto_'+tipo+'_'+idElemento;
			nuevoFile.size = 25;
			nuevoFile.className = 'texto';
					
			//si es IExplorer
			if(navigator.appName.match('Microsoft')){
				nuevoFile.setAttribute("onchange",inputFoto.onchange);
			}
			//si es Mozilla
			else{
				nuevoFile.setAttribute("onchange","reemplazarFoto('file:///'+document.getElementById(this.id).value,'"+idElemento+"','NUEVA','"+tipo+"');");
			}											
			tdInputFoto.appendChild(nuevoFile);

					
			// el enlace de borrar
			if(accion=='BORRAR'){
				if(document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento)){
					anchorBorrarFoto=document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento);
					var padreAnchorBorrarFoto=document.getElementById('tdInputBorrarFoto_'+tipo+'_'+idElemento);
				
					padreAnchorBorrarFoto.removeChild(anchorBorrarFoto);
				}
				if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
					var nuevoAnchor = document.createElement('a');
					nuevoAnchor.href = "javascript:reemplazarFoto(document.getElementById('backupImgFoto_"+tipo+"_"+idElemento+"').value,'"+idElemento+"','RESTAURAR','"+tipo+"');";
					nuevoAnchor.id = 'anchorBorrarFoto_'+tipo+'_'+idElemento;
					nuevoAnchor.name='ANCHOR_'+tipo+'_FOTO_'+idElemento;
					var texto = document.createTextNode('Restaurar');
   				nuevoAnchor.appendChild(texto);							
					padreAnchorBorrarFoto.appendChild(nuevoAnchor);	
				}
			}
		}
		// la URI de imagen no esta vacia
		else{
			// si tenemos algo, lo mostramos
			if (URIfoto != 'file:///'){
			
				if(accion=='RESTAURAR'){
				
					// borramos el input y lo reconstruimos
					
					tdInputFoto.removeChild(inputFoto);
					var nuevoFile = document.createElement("input");
					nuevoFile.type = 'file';
					nuevoFile.name = tipo+'_FOTO_'+idElemento;
					nuevoFile.accept = 'image/jpeg,image/gif';
					nuevoFile.id = 'inputFoto_'+tipo+'_'+idElemento;
					nuevoFile.size = 25;
					nuevoFile.className = 'texto';
			
					//si es IExplorer
					if(navigator.appName.match('Microsoft')){
						nuevoFile.setAttribute("onchange",inputFoto.onchange);
					}
					//si es Mozilla
					else{
						nuevoFile.setAttribute("onchange","reemplazarFoto('file:///'+document.getElementById(this.id).value,'"+idElemento+"','NUEVA','"+tipo+"');");
					}											
					tdInputFoto.appendChild(nuevoFile);
					
	
	
					// el anchor de borrar / restaurar
					anchorBorrarFoto=document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento);
					var padreAnchorBorrarFoto=document.getElementById('tdInputBorrarFoto_'+tipo+'_'+idElemento);
			
					padreAnchorBorrarFoto.removeChild(anchorBorrarFoto);
					var nuevoAnchor = document.createElement('a');
					nuevoAnchor.href = "javascript:eliminarFoto('"+idElemento+"','BORRAR','"+tipo+"');";
					nuevoAnchor.id = 'anchorBorrarFoto_'+tipo+'_'+idElemento;
					nuevoAnchor.name='ANCHOR_'+tipo+'_FOTO_'+idElemento;
					var texto = document.createTextNode('Eliminar');
   					nuevoAnchor.appendChild(texto);							
					padreAnchorBorrarFoto.appendChild(nuevoAnchor);	
					
					
					// borramos la foto y creamos el anchor de la foto y la imagen
					
					////
					
					// los elementos de la imagen

					var imgFoto = document.getElementById('imgFoto_'+tipo+'_'+idElemento);
					var padreImgFoto = imgFoto.parentNode;	
		

								
					// nos cargamos la imagen y la reconstruimos						
					imgFoto.parentNode.removeChild(imgFoto);
		
					// creamos el anchor
					var idFoto=document.getElementById('idImgFoto_'+tipo+'_'+idElemento).value;
					var nuevoAnchorFoto = document.createElement('a');
					nuevoAnchorFoto.href = "javascript:MostrarPagPersonalizada('http://"+getHostname()+"/Fotos-Anuncios.xsql?IDFOTO="+idFoto+"','Foto','675px','675px','0','0');";
					nuevoAnchorFoto.id = 'anchorImgFoto_'+tipo+'_'+idElemento;
   				nuevoAnchorFoto.border='0';					
					padreImgFoto.appendChild(nuevoAnchorFoto);	
					
					// creamos la imagen
					
					
					imgFoto = document.createElement("img");
					imgFoto.border = 1;
					imgFoto.id = 'imgFoto_'+tipo+'_'+idElemento;
					imgFoto.src=URIfoto;
					imgFoto.name='IMG_'+tipo+'_FOTO_'+idElemento;
					
					
					var altImagen='Foto del usuario / anuncio';
					imgFoto.alt=altImagen;

					nuevoAnchorFoto.appendChild(imgFoto);

					////

				}
				else if(accion=='NUEVA'){
				
		
				
					if(URIfoto!='file:///'){
				
						if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
				
					
							anchorBorrarFoto=document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento);
							var padreAnchorBorrarFoto=document.getElementById('tdInputBorrarFoto_'+tipo+'_'+idElemento);
			
							padreAnchorBorrarFoto.removeChild(anchorBorrarFoto);
							var nuevoAnchor = document.createElement('a');
							nuevoAnchor.href = "javascript:reemplazarFoto(document.getElementById('backupImgFoto_"+tipo+"_"+idElemento+"').value,'"+idElemento+"','RESTAURAR','"+tipo+"');";
							nuevoAnchor.id = 'anchorBorrarFoto_'+tipo+'_'+idElemento;
							nuevoAnchor.name='ANCHOR_'+tipo+'_FOTO_'+idElemento;
							var texto = document.createTextNode('Restaurar');
   						nuevoAnchor.appendChild(texto);							
							padreAnchorBorrarFoto.appendChild(nuevoAnchor);	
						}
						else{
					
							var padreAnchorBorrarFoto=document.getElementById('tdInputBorrarFoto_'+tipo+'_'+idElemento);
					
							if(document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento)){
								anchorBorrarFoto=document.getElementById('anchorBorrarFoto_'+tipo+'_'+idElemento);
								padreAnchorBorrarFoto.removeChild(anchorBorrarFoto);
							}
							
							var nuevoAnchor = document.createElement('a');
							nuevoAnchor.href = "javascript:eliminarFoto('"+idElemento+"','BORRAR','"+tipo+"');";
							nuevoAnchor.id = 'anchorBorrarFoto_'+tipo+'_'+idElemento;
							nuevoAnchor.name='ANCHOR_'+tipo+'_FOTO_'+idElemento;
							var texto = document.createTextNode('Eliminar');
   						nuevoAnchor.appendChild(texto);							
							padreAnchorBorrarFoto.appendChild(nuevoAnchor);	
						}
				  	
						imgFoto.src = URIfoto;
										
						imgFoto.height = MAX_HEIGHT;
						imgFoto.width = MAX_WIDTH;
					}
				}
			}
			if (URIfoto == 'file:///'){
				if(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento)){
					reemplazarFoto(document.getElementById('backupImgFoto_'+tipo+'_'+idElemento).value,idElemento,'RESTAURAR',tipo);
				}
				else{
					reemplazarFoto('',idElemento,'BORRAR',tipo);
				}
			}
		}
	}
	
	
	function SubmitFormImagenes(form){
		
		var destino;
		var mostrarConfirm='N';
		var mostrarMensajeDiv='S';
		
		if(mostrarConfirm=='S'){
			if(!confirm(msgAvisoImagenes)){
				return;	
			}
		}
		
		// COMPROBAMOS QUE EL ACTION DEL FORMULARIO SEA RELATIVO O ABSOLUTO  MR
		if (form.action.indexOf("http")==-1){
			
			destino='http://'+getHostname()+'/'+form.action;
		}
		else{
			destino=form.action;
		}
		
			
		form.elements['FORM_FINAL'].value=destino;
		form.action='http://'+getHostname()+'/imagenes/upload.jsp';
	
		form.encoding='multipart/form-data';
			
		if(mostrarMensajeDiv=='S'){	
				
			var ventanaEnvio = document.createElement("div");
			
			ventanaEnvio.style.position = "absolute";
			ventanaEnvio.style.width = "400px";
			ventanaEnvio.style.height="100px";
				
			
				
			//si es IExplorer
			if(navigator.appName.match('Microsoft')){
				var desfase=document.body.scrollTop-120;
			}
			//si es Mozilla
			else{
				var desfase=window.scrollY-120;
			}											
    	
			
			
			
			ventanaEnvio.style.left = parseInt(((top.screen.availWidth-400) / 2))+'px';
			ventanaEnvio.style.top = parseInt(((top.screen.availHeight-100) / 2)+desfase)+'px';
			ventanaEnvio.className="enviandoImagenes";
			ventanaEnvio.zIndex='99';
			ventanaEnvio.id = 'divVentanaEnvio';
			
			
			var saltoDeLinea = document.createElement("br");
			ventanaEnvio.appendChild(saltoDeLinea);
			var textoVentana = document.createTextNode('Por favor, espera unos instantes');
			ventanaEnvio.appendChild(textoVentana);
			saltoDeLinea = document.createElement("br");
			ventanaEnvio.appendChild(saltoDeLinea);
			saltoDeLinea = document.createElement("br");
			ventanaEnvio.appendChild(saltoDeLinea);
			textoVentana = document.createTextNode('Las fotos se están enviando...');
			ventanaEnvio.appendChild(textoVentana);
			saltoDeLinea = document.createElement("br");
			ventanaEnvio.appendChild(saltoDeLinea);
			saltoDeLinea = document.createElement("br");
			ventanaEnvio.appendChild(saltoDeLinea);
			textoVentana = document.createTextNode('Gracias.');
   			ventanaEnvio.appendChild(textoVentana);
    	
			if (document.getElementById('idForm'))
				var elForm = document.getElementById('idForm');
			else
				var elForm = document.getElementById('form'); 
			elForm.appendChild(ventanaEnvio);
    	    	
	

		}
		
		SubmitForm(form);		
	}
	
	function tieneImagenes(form){
		
		
		// miramos si hay imagenes, si es asi redireccionamos el submit al modulo de upload
		// si no, continuamos normalmente
		var hayImagenes=0;
		for (var  n=0;n<form.length&&!hayImagenes;n++){
			if(form.elements[n].type=='file' && form.elements[n].value!=''){
				
				hayImagenes=1;
			}
		}
		return hayImagenes;
	}
	
	function prepararImagenesBorradas(form){
		// buscamos las imagenes borradas
		var cadenaImaganesBorradas='';
				
		for(var n=0;n<form.length;n++){
			if(form.elements[n].name && form.elements[n].name.indexOf("ID_")==0 && form.elements[n].name.indexOf("_FOTO")!=-1){
				var Id=form.elements[n].value;
				var borrar=form.elements[form.elements[n].name.replace('ID_','BORRAR_')].value;
				cadenaImaganesBorradas+=Id+'|'+borrar+'#';
			}
		}
		
		form.elements['IMAGENES_BORRADAS'].value=cadenaImaganesBorradas;

	}
	
	// funcion necesaria para moverse por las altas
	function historia_upload(){
		if(document.forms[0].elements['HISTORIA_UPLOAD']){
			if(document.forms[0].elements['HISTORIA_UPLOAD'].value!=''){
				return parseInt(document.forms[0].elements['HISTORIA_UPLOAD'].value);
			}
		}
		return 0;
	}
