var ultimo_paso = 1;

var befPrecioFinal = 0;
var befBeneficioUser = 0;
var befPrecioEbook = 0;
var befPrecioEbook = 0, befBeneficioEbook = 0;

var paso_5_load = false;
var is_chrome = navigator.userAgent.toLowerCase().indexOf('webkit') > -1;

function guardarycontinuar(pas){
		if( $('autor_id').value == 0 )
			{
				launchLightBox('areg');
				return false;
			}
			
			((is_chrome) ? $("paso_" + curr_paso).hide() : Effect.fade("paso_" + curr_paso));
			curr_paso = pas == 0 ? (curr_paso+1) : pas;
			
			ultimo_paso = curr_paso > ultimo_paso ? curr_paso : ultimo_paso;

			
			for(var i = 1; i <= 5; i++)
			{
				if( i == curr_paso )
				{
					$('tab_paso_' + i).className = "procesoseleccion_parts_title procesoseleccion_parts_title_on";					
					$('texto_paso_' + i).style.display = "";
				}
				else
				{
					if( i > ultimo_paso )
						$('tab_paso_' + i).className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
					
					$('texto_paso_' + i).style.display = "none";
				}
			}
			
			if( curr_paso > 1 )
			{
				var ca = (curr_paso - 1);
				$('ca_paso_' + ca).style.display = "";
			}
			
			checkPendientes(0, 4, curr_paso);
			
			// En caso que se tenga que actualizar el editor portadas
			//if (typeof pedirEstado == "function")
				//pedirEstado();
			
			
			
			if(curr_paso>1 && $('editar').value!=0){
				sendPublicar2(curr_paso-1)
			}
			//alert('valor de editar '+$('editar').value +' y el valor de pas '+pas+' curr_paso'+curr_paso)
			//guardarLibro()
			setTimeout('doPaso()', 700);
	
}

function cambiarPaso(pas)
		{
			if( $('autor_id').value == 0 )
			{
				launchLightBox('areg');
				return false;
			}
			
			((is_chrome) ? $("paso_" + curr_paso).hide() : Effect.fade("paso_" + curr_paso));
			curr_paso = pas == 0 ? (curr_paso+1) : pas;
			
			ultimo_paso = curr_paso > ultimo_paso ? curr_paso : ultimo_paso;

			
			for(var i = 1; i <= 5; i++)
			{
				if( i == curr_paso )
				{
					$('tab_paso_' + i).className = "procesoseleccion_parts_title procesoseleccion_parts_title_on";					
					$('texto_paso_' + i).style.display = "";
				}
				else
				{
					if( i > ultimo_paso )
						$('tab_paso_' + i).className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
					
					$('texto_paso_' + i).style.display = "none";
				}
			}
			
			if( curr_paso > 1 )
			{
				var ca = (curr_paso - 1);
				$('ca_paso_' + ca).style.display = "";
			}
			
			checkPendientes(0, 4, curr_paso);

			setTimeout('doPaso()', 700);
		}
		
		function doPaso()
		{
			switch(curr_paso)
			{
				case 1:
					$('procesoseleccion_parts_flecha_selected').style.left = "52px";
				break;
				case 2:
					savePaso1();
				break;
				case 3:
					savePaso2();
				break;
				case 4:
					savePaso3()
				break;
				case 5:
					savePaso4();
				break;
			}
			
			if( isPasoLoad(curr_paso) == false )
			{
				loadPaso(curr_paso);
			}

			((is_chrome) ? $("paso_" + curr_paso).show() : Effect.appear("paso_" + curr_paso));
			//Effect.appear("paso_" + curr_paso);
		}
		
		function loadPaso(cpaso)
		{
			var miAjax = new Ajax('/publicar/form_paso' + cpaso, {
				update: $("paso_" + cpaso),
				data: "PHPSESSID=" + $("PHPSESSID").value,
				onFailure: function() {
					cambiarPaso(cpaso);
				}
			}).request();
		}
		
		function isPasoLoad(cpaso)
		{
			if( $('paso_' + cpaso).innerHTML.length < 100 )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		
		function save_param(str)
		{
			var miAjax = new Ajax('/publicar/save_param', {
				method: 'post',
				data: str
			}).request();
		}
		
		function savePaso1()
		{
			ultimo_paso = ultimo_paso > 2 ? ultimo_paso : 2;
			
			if( form_type == 0 )
				save_param("ultimo_paso=" + ultimo_paso);
			
			$('procesoseleccion_parts_flecha_selected').style.left = "207px";
		}
		
		function savePaso2()
		{
			if( isPasoLoad(2) == true )
			{
				var str = "";
				var autores = "";
			
				var daut = document.getElementsByName("autores[]");
				var taut = document.getElementsByName("autores_tipos[]");
				var sautores = "";
				var sautores_tipos = "";
			
				for(var i = 0; i < daut.length; i++)
				{
					autores += daut[i].value + "<br />";
					sautores += sautores == "" ? daut[i].value : ";" + daut[i].value;
					sautores_tipos += sautores_tipos == "" ? taut[i].value : ";" + taut[i].value;
				}
			
				str += "<b>Titulo:</b> " +  ( ($('titulo').value == "") ? "<span class=\"pendiente\">pendiente</span>" : $('titulo').value ) + "<br />";
				/*str += "<b>Autores:</b> " + ( (autores == "<br />") ? "<span class=\"pendiente\">pendiente</span><br />" : autores );
				str += "<b>G&eacute;nero:</b> " + ( $('genero').value == 0 ? "<span class=\"pendiente\">pendiente</span>" : ($('genero').options[$('genero').selectedIndex]).text ) + "<br />";
				str += "<b>Tem&aacute;tica:</b> " + ( $('tematica').value == 0 ? "<span class=\"pendiente\">pendiente</span>" : ($('tematica').options[$('tematica').selectedIndex]).text ) + "<br />";
				str += "<b>Idioma:</b> "+ ( $('idioma_id').value == 0 ? "<span class=\"pendiente\">pendiente</span>" : ($('idioma_id').options[$('idioma_id').selectedIndex]).text );
				*/
				$('dat_paso_2').innerHTML = str;
				
			
				if( form_type == 0 )
					save_param("ultimo_paso=" + ultimo_paso + "&titulo=" + $('titulo').value + "&autores=" + sautores + "&autores_tipos=" + sautores_tipos + "&tags=" + $('tags').value + "&sinopsis=" + $('sinopsis').value + "&genero=" + $('genero').value);
			}
			
			ultimo_paso = ultimo_paso > 3 ? ultimo_paso : 3;
			
			$('procesoseleccion_parts_flecha_selected').style.left = "360px";
		}
		
		function savePaso3()
		{
			if( isPasoLoad(3) == true )
			{
				$('dat_paso_3').innerHTML = "<b>Tama&ntilde;o:</b> " + $('link_tipostamano_' + $('tamano').value).getAttribute("title");
				$('dat_paso_3').innerHTML += "<br /><b>Tinta:</b> " + $('link_tinta_' + $('tinta').value).getAttribute("title");
				$('dat_paso_3').innerHTML += "<br /><b>Maquetaci&oacute;n:</b> " + $('linkmaque_' + $('maquetacion').value).getAttribute("title");
				$('dat_paso_3').innerHTML += "<br /><b>Papel:</b> " + $('lpapel_' + $('papel').value).getAttribute("rev");
						
				if( form_type == 0 )
					save_param("ultimo_paso=" + ultimo_paso + "&tamano=" + $('tamano').value + "&maquetacion=" + $('maquetacion').value + "&tinta=" + $('tinta').value + "&papel=" + $('papel').value + "&ancho_lomo=" + $('ancho_lomo').value + "&idioma_id=" + $('idioma_id').value + "&servicios=" + $('servicios').value);
			}

			ultimo_paso = ultimo_paso > 4 ? ultimo_paso : 4;

			$('procesoseleccion_parts_flecha_selected').style.left = "514px";
		}
		
		function savePaso4()
		{
			if( isPasoLoad(4) == true )
			{
				/*
				var pof = portada_id == 0 ? 'portadas' : 'portadasbubok';
				var pod = portada_id == 0 ? portadas_dir : 'archivo/portadas/bubok/';
			
				var pos = $(pof).value;
				var img = [];
				img = pos.split(";");
			
				$('dat_paso_4').innerHTML = '';
				if( pos != "" )
				{
					for(var i = 0; i < img.length; i++)
					{
						var fpod = ( img[i].match(/([a-zA-Z0-9]{32})_([0-9])/) ) ? "./archivo/portadas/users/" : pod ;
						$('dat_paso_4').innerHTML += '<img src=' + fpod + img[i] + ' height=48 width=34 /> ';
					}
				}
			*/
			
				if( form_type == 0 )
				{	
					save_param("ultimo_paso=" + ultimo_paso + "&portadas=" + $('portadas').value + "&tipos_portadas=" + $('tipos_portadas').value + "&portada_id=" + $('portada_id').value);
				}
			}
			
			ultimo_paso = 5;

			$('procesoseleccion_parts_flecha_selected').style.left = "668px";
		}
		
		function addLibro(name, pags)
		{
			$('libros').value = name;
		
			$('tab_paso_1').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";

			paginas_total += parseInt(pags);
			$('paginas_total').value = paginas_total;
			
			$('precio_final').disabled = "";
			$('beneficio_user').disabled = "";
			
			checkAllowed();

			updateAnchoLomo();

			updateCanasta();
			
			checkValidSize();
			
			reCalc();
		}
		
		function justifyLibro(tamano)
		{
			var miAjax = new Ajax("/publicar/justifyLibro", {
				method: 'post',
				data: "tamano=" + tamano,
				evalScripts: true
			}).request();
			
			$('resize').value = 1;
			
			tamano_mm = tamano;
			
			var szs = [];
			szs = document.getElementsByName("tamanos[]");
			for(var i = 0; i < szs.length; i++)
				szs[i].value = tamano;
			
			reCalc();
	
			updatePortadaSizes();
		}
		
		function borrarLibroUp(id, name, pags)
		{
			var reg = new RegExp(name + '(;)?', 'gi');
			//paginas_total -= parseInt(pags);
			//$('paginas_total').value = paginas_total;
			
			//var libs = $('libros').value.replace(reg, "");
			//$('libros').value = libs;
			
			//$('fsUploadProgress1').removeChild($(id));
			$('fsUploadProgress1').empty();
			paginas_total = 0;
			$('paginas_total').value = 0;
			
			if( paginas_total < 1 )
			{
				$('precio_final').disabled = "disabled";
				$('beneficio_user').disabled = "disabled";
				paginas_total = 0;
			}
			
			if( id.match(/_swf_/) )
			{
				try {
					new Ajax('publicar/borrarpdf', {
						method: 'post',
						data: 'libro=' + $('editar').value + '&pdf=' + name,
						update: $('dvpresupuesto'),
						onComplete: function() {
						}
					}).request();
				}
				catch(ex) {
				}
				
			}
			
			updateAnchoLomo();

			updateCanasta();
			
			reCalc();
			
			/* show button */
			$('button_upload').style.display = "";
	}
	
	function addServicio(id, titulo, precio, pt)
	{
		Effect.fade('serv_' + id);
		if(id!=69){
			Effect.appear('serv_' + id + '_c');
			if( $('serv_' + id) )
				Effect.appear('serv_' + id + '_b');
			num_canasta++;
		}
		
		if(id==69) Dialog("Se ha iniciado el proceso de registro de tu obra. Revisa tu correo electr&oacute;nico, te hemos enviado un mensaje con las instrucciones");
				
		var typ = pt == true ? 1 : 0;
		id = id + "|" + precio + "|" + typ;
			
		$('servicios').value += $('servicios').value == "" ? id : ";" + id;
		
		if($('editar').value!=""){
			new Ajax('autores/addservicio/', {postBody: 'libro=' + $('editar').value + '&servicio=' + id + '&coste=' + precio}).request();
		}

		updateCanasta();
	}
		
	function removeServicio(id, titulo, precio, pt)
	{
		Effect.fade('serv_' + id + '_b');
		Effect.fade('serv_' + id + '_c');
		if( $('serv_' + id) )
			Effect.appear('serv_' + id);
			
		num_canasta--;
			
		var typ = pt == true ? 1 : 0;
			
		var servs = [];
		var sval = "";
						
		servs = $('servicios').value.split(";");
			
		for(var i = 0; i < servs.length; i++)
		{
			var prts = [];
			prts = servs[i].split("|");
				
			var ser_id = prts[0];
				
			if( !(parseInt(ser_id) > 0) || parseInt(ser_id) == parseInt(id) )
				continue;
					
			sval += (sval == "") ? servs[i] : ";" + servs[i]
		}
			
		$('servicios').value = sval;
						
		updateCanasta();
	}
	
	function inArray(arr, needle)
	{
			for(var i = 0; i < arr.length; i++)
			{
				if(arr[i] == needle)
				{
					return true; 
				}
			}
			
			return false;
		}

		function updateCanasta()
		{
			$('num_canasta').innerHTML = num_canasta;

			var servs = [];
			var precio_total = $('portada_id').value == 0 ? 0.00 : parseFloat($('portadas_coste').value);
			
			servs = document.getElementById('servicios').value.split(";");
			
			for(var i = 0; i < servs.length; i++)
			{
				var prts = [];
				prts = servs[i].split("|");
				
				var ser_id = prts[0];
				
				if( !(parseInt(ser_id) > 0) )
					continue;
				
				var ser_precio = prts[1];
				var ser_type = prts[2];
				
				var precio_mas = 0.00;
				
				if( ser_type == 1 )
				{
					precio_mas = parseFloat(ser_precio);
				}
				else
				{
					precio_mas = (parseFloat(ser_precio)*paginas_total);
				}
				
				$("ptxt_" + ser_id).innerHTML = precio_mas + "&euro;";
				$("cptxt_" + ser_id).innerHTML = precio_mas + "&euro;";
				
				precio_total += precio_mas;
				
			}
			
			precio_total = isNaN(precio_total) ? 0.00 : parseFloat(precio_total).toFixed(2);
			
			$('costo_canasta').innerHTML = parseFloat(precio_total).toFixed(2);
		}
		
		function guardarLibro()
		{
			if( checkPendientes(1, 5, 7) == false )
			{
				return false;
			}
			
			if( checkValidSize() == false )
			{
				cambiarPaso(3);
				
				return false;
			}
						
			$('resppublicar').style.display = "";
			
			//if (typeof pedirEstado == "function")
				//pedirEstado();
			
			WaitBubok();
			
			sendPublicar();
			
			return true;
		}
		
		function sendPublicar()
		{
			var formu = $('fpublicar');
			var miAjax = new Ajax(formu.getAttribute("action"), {
				method: 'post',
				data: formu,
				onComplete: function(html) {
					//$('resppublicar').style.display = "none";
					if( html.match(/mysql/i) )
					{
						Dialog('El proceso no ha podido completarse, pruebe a publicarlo de nuevo');	
					}
					else if( html.match(/error/i) || html.match(/fpdf/i) )
					{
						Dialog('Error al leer el archivo PDF');	
					}
					else
					{
						// good
					}
					
					$('wait_bubok').style.display = "none";
				},
				update: $('resppublicar')
			}).request();
		}
		
		function sendPublicar2(paso)
		{
			var formu = $('fpublicar');
			var miAjax = new Ajax('publicar/guardarycontinuar/'+paso, {
				method: 'post',
				data: formu,
				onComplete: function(html) {
					//$('resppublicar').style.display = "none";
					if( html.match(/mysql/i) )
					{
						Dialog('El proceso no ha podido completarse, pruebe a publicarlo de nuevo');	
					}
					else if( html.match(/error/i) || html.match(/fpdf/i) )
					{
						Dialog(paso+'Error al leer el archivo PDF'+html);	
					}
					else
					{
						// good
						
						/*if($('libros').value=13253 && paso==2){
							$('ya_guardo_file').value='1';
							alert('se cambio'+$('ya_guardo_file').value)  
						}
						if(paso==2){
							$('ya_guardo_file').value=1;
						}*/
						
					}
					
					$('wait_bubok').style.display = "none";
				},
				update: $('resppublicar')
			}).request();
		}
		
		function updateAnchoLomo()
		{
			var ancho_lomo = parseFloat($('lpapel_' + $('papel').value).rel);
			
			$('ancho_lomo').value = ancho_lomo;
		}
		
		function reCalc()
		{
			updatePresupuesto();

			setTimeout('updatePrecioFinal()', 2000);
		}
		
		function resetCoste()
		{
			var def = 0.00;
			$('coste_produccion').value = def;
			$('precio_final').value = def;
			$('beneficio_user').value = def;
			$('beneficio_bubok').value = def;
			
			paginas_total = 0;
		}
		
		function updatePresupuesto()
		{
			//tamano tinta maquetacion 
			try {
				new Ajax('publicar/presupuesto', {
					method: 'post',
					data: 'tamano=' + $('tamano').value + '&tinta=' + $('tinta').value + '&maquetacion=' + $('maquetacion').value + '&editar=' + $('editar').value,
					update: $('dvpresupuesto'),
					onComplete: function() {
						var dv = $('dvpresupuesto').innerHTML;
						if( dv == "error" )
						{
							alert('Calculo no disponible');
							return false;
						}
						var pre = [];
						pre = dv.split("|");
						
						if( parseInt(paginas_total) < 1 )
						{
							resetCoste();
						}
						else
						{
							var precio = parseFloat(pre[0]) + (parseInt(paginas_total) * parseFloat(pre[1]));
							// Incrementos para libros nuevos
							var inc_nuevos = parseFloat(pre[2]);

							var incremento = ((precio/100) * parseInt(porcentajeIncrementoCosto)) + fijobubokporlibro + inc_nuevos;

							precio += incremento;
							
							$('coste_produccion').value = parseFloat(precio).toFixed(2);
						}
						
						$('precio_final').value = parseFloat( parseFloat($('coste_produccion').value) + parseFloat($('beneficio_bubok').value) + parseFloat($('beneficio_user').value) ).toFixed(2);
						
						$('dv_coste_produccion').innerHTML = $('dv_precio_final').innerHTML = $('coste_produccion').value;
					}
				}).request();
			}
			catch(ex) {
			}			
		}

		function callTimeOut(ty)
		{
			var befVal = ty == 0 ? befPrecioFinal : befBeneficioUser;
			
			if( befVal == 0 || befVal != $( (ty == 0 ? 'precio_final' : 'beneficio_user') ).value )
			{
				setTimeout('callTimeOut(' + ty + ')', 500);					
			}
			else
			{
				if( ty == 0 )
					onChangePrecioFinal();
				else if( ty == 1 )
					updateBeneficio();
			}
			
			if( ty == 0 )
				befPrecioFinal = $('precio_final').value;
			else if( ty == 1 )
				befBeneficioUser = $('beneficio_user').value;
		}
		
		function onChangePrecioFinal()
		{
			 if( parseFloat($('precio_final').value) < parseFloat($('coste_produccion').value) )
			 {
			 	Dialog('El precio final no puede ser menor al precio de producci&oacute;n');
				
				$('beneficio_user').value = $('beneficio_bubok').value = 0.00;
				$('precio_final').value = parseFloat($('coste_produccion').value).toFixed(2);
				
				return false;
			 }
			 
			 if( $('precio_final').value.match(/([^0-9\.]+)/) )
			 {
				 $('precio_final').value = $('precio_final').value.replace(/([^0-9\.]+)/, "")
			 }			 
			 if( $('precio_final').value == "" )
			 {
				 $('precio_final').value = 0.00;
			 }

			
			 precio_final = parseFloat($('precio_final').value).toFixed(2);
			 coste_produccion = parseFloat($('coste_produccion').value).toFixed(2)
			 
			 var buser = precio_final-coste_produccion;

			 bperce = buser / 10;
			 
			 $('beneficio_user').value = parseFloat((bperce * 8)).toFixed(2);
			 $('beneficio_bubok').value = parseFloat((bperce * 2)).toFixed(2);
			 
			 updatePrecioFinal();
		}

		function updateBeneficio()
		{
			if( $('beneficio_user').value.match(/([^0-9\.]+)/) )
			{
				$('beneficio_user').value = $('beneficio_user').value.replace(/([^0-9\.]+)/, "")
			}			 
			if( $('beneficio_user').value == "" )
			{
				 $('beneficio_user').value = 0.00;
			}
						
			var beneficio = parseFloat($('beneficio_user').value).toFixed(2);			
			var beneficio_bubok = parseFloat( ((beneficio * 10) / 8)*0.2 ).toFixed(2);
			
			$('beneficio_bubok').value = parseFloat(beneficio_bubok).toFixed(2);
			$('precio_final').value = parseFloat( parseFloat($('coste_produccion').value) + parseFloat(beneficio) + parseFloat(beneficio_bubok)).toFixed(2);

			updatePrecioFinal();
		}
				
		function onChangePrecioEbook()
		{
			if( $('precio_ebook').value.match(/([^0-9\.]+)/) )
			{
				 $('precio_ebook').value = $('precio_ebook').value.replace(/([^0-9\.]+)/, "")
			}			 
			if( $('precio_ebook').value == "" )
			{
				 $('precio_ebook').value = 0.00;
			}
			
			var precio_ebook = $('precio_ebook').value;
			
			if( befPrecioEbook == precio_ebook )
			{
				updatePrecioFinal();
			}
			else
			{
				setTimeout('updatePrecioFinal()', 1000);
			}
		}
		
		function callTimeOut2(ty)
		{
			var befVal = ty == 0 ? befPrecioEbook : befBeneficioEbook;
			
			if( befVal == 0 || befVal != $( (ty == 0 ? 'precio_ebook' : 'beneficio_user2') ).value )
			{
				setTimeout('callTimeOut2(' + ty + ')', 500);					
			}
			else
			{
				if( ty == 0 )
					onChangePrecioEbookT();
				else if( ty == 1 )
					updateBeneficioEbook();
			}
			
			if( ty == 0 )
				befPrecioEbook = $('precio_ebook').value;
			else if( ty == 1 )
				befBeneficioEbook = $('beneficio_user2').value;
		}
		
		function onChangePrecioEbookT()
		{
			 if( $('precio_ebook').value.match(/([^0-9\.]+)/) )
			 {
			 	$('precio_ebook').value = $('precio_ebook').value.replace(/([^0-9\.]+)/, "")
			 }		
			 if( $('precio_ebook').value == "" )
			 {
			   $('precio_ebook').value = 0.00;
			 }

			 var precio_ebook = parseFloat($('precio_ebook').value).toFixed(2);
			 			 
			 var bperce = 10;//(porcentajeBeneficio/10)+10;
			 
			 bperce = precio_ebook / bperce;
			 
			 $('beneficio_user2').value = parseFloat((bperce * 8)).toFixed(2);
			 $('beneficio_bubok2').value = parseFloat((bperce * 2)).toFixed(2);
			 
			 onChangePrecioEbook();
		}

		function updateBeneficioEbook()
		{
			if( $('beneficio_user2').value.match(/([^0-9\.]+)/) )
			{
				$('beneficio_user2').value = $('beneficio_user2').value.replace(/([^0-9\.]+)/, "");
			}
			if( $('beneficio_user2').value == "" )
			{
				$('beneficio_user2').value = 0.00;
			}						
			
			var beneficio = parseFloat($('beneficio_user2').value).toFixed(2);
			var precio_ebook = parseFloat( ((beneficio * 10) / 8) ).toFixed(2);
			
			$('beneficio_user2').value = (parseFloat( (precio_ebook*0.80) ).toFixed(2));
			$('beneficio_bubok2').value = (parseFloat( (precio_ebook*0.20) ).toFixed(2));
			$('precio_ebook').value = precio_ebook;
		}
		
		function updatePrecioFinal()
		{
			var precio = $('precio_final').value;

			$('libro_coste').value = precio;
			
			$('dv_precio').innerHTML = 
							'<div style="width:70%; float:left">' + 
							'<span class="txtgreen txtmeddium"><b style="font-size:10px">Coste producci&oacute;n<br />' + 
							'Precio en papel<br />' +
							'Beneficio en papel<br />' + 
							 
							'Precio en ebook</b></span></div>' + 
							'<div style="width:29%; float:left; font-size:10px">' + 
							parseFloat($('coste_produccion').value).toFixed(2) + '<b>&euro;</b><br />' + 
							parseFloat($('precio_final').value).toFixed(2) + '<b>&euro;</b><br />' + 
							parseFloat($('beneficio_user').value).toFixed(2) + '<b>&euro;</b><br />' + 
							 
							parseFloat($('precio_ebook').value).toFixed(2) + '<b>&euro;</b></div>';
		}

		function checkValidSize()
		{
			var tams = [];
			tams = document.getElementsByName("tamanos[]");
			var va = true;
			//$('resize').value = 0;
			
			for(var i = 0; i < tams.length; i++)
			{
				if( tamano_mm != tams[i].value )
				{
					va = confirm("Algunos archivos enviados no tienen las medidas elegidas, seran redimensionados automaticamente.");
					$('resize').value = 1;
					break;
				}
			}
			
			return va;
		}
		
		function selectFiles()
		{
			if( $('autor_id').value == 0 )
			{
				cambiarPaso(1);
			}
			else
			{
				upload1.selectFiles();
			}
		}
		
		function miISBN(obj)
		{
			if( obj.value == 2 )
			{
				$('dvmi_isbn').style.display = "";
			}
			else
			{
				$('dvmi_isbn').style.display = "none";
			}
		}
		
		function checkPendientes(tipo, ult_paso, curr_paso)
		{
			var completado_1 = completado_2 = completado_3 = false;
			
			correctWys();
			
			if( ult_paso >= 1 )
			{
				if(  curr_paso == 1 )
				{	
				}
				else if( $('libros').value.length < 4 )
				{
					$('tab_paso_1').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
					
					if( $('fsUploadProgress1').innerHTML.length < 5 )
						$('fsUploadProgress1').setHTML('Archivo: <font style="color:red">pendiente</font>');
				
					if( tipo == 1 )
					{
						Dialog('Debes completa el paso de envio');
				
						cambiarPaso(1);
				
						return false;
					}
				}
				else if( ult_paso > 1 )
				{
					$('tab_paso_1').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";
					completado_1 = true;
				}
			}
			
			if( ult_paso >= 2 )
			{
				var aut = [];
				var uaut = false;
				aut = document.getElementsByName("autores[]");
				for(var i = 0; i < aut.length; i++)
				{
					if( aut[i].value != "" )
						uaut = true;
				}
				
				if( curr_paso == 2 )
				{
					if( $defined(mySlide2) )
					{
						document.getElementById('paso_col_2').style.display = "";
						mySlide2.slideIn();
					}
				}
				if( !$('titulo') && form_type == 1 && curr_paso != 7 )
				{
				}
				else if( !$('titulo') )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";

					if( tipo == 1 )
					{
						Dialog('debes completar los datos del libro');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('titulo').value.length < 2 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes agregar un titulo');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( uaut == false )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes agregar al menos un autor');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('tags').value.length < 2 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes agregar alguna palabra clave');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('sinopsis').value.length < 2 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes agregar una sinopsis');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('tematica').value == 0 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes elegir una tematica');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('genero').value == 0 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes elegir un genero');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( $('idioma_id').value == 0 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				
					if( tipo == 1 )
					{
						Dialog('Debes elegir un idioma');
				
						cambiarPaso(2);
				
						return false;
					}
				}
				else if( ult_paso > 2 )
				{
					$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";
					completado_2 = true;
				}
			}
			
			//Patch
			if( curr_paso == 2 )
			{
				$('tab_paso_2').className = "procesoseleccion_parts_title procesoseleccion_parts_title_on";					

			}

			if( ult_paso >= 3 )
			{
				if( curr_paso == 3 )
				{	
					if( completado_1 != true )
					{
						Dialog('Debes completar el paso de envio');
						
						cambiarPaso(1);
						
						return false;
					}
					else if( completado_2 != true && form_type != 1 )
					{
						Dialog('Debes completar el paso de datos');
						
						cambiarPaso(2);
						
						return false;
					}
					else
					{
						if( $defined(mySlide3) )
						{
							document.getElementById('paso_col_3').style.display = "";
							mySlide3.slideIn();
						}
					}
				}
				else if( isPasoLoad(3) == false && form_type == 0 )
				{
					$('tab_paso_3').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";
				}				
				else
				{
					$('tab_paso_3').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";
					completado_3 = true;
				}
			}
			
			if( ult_paso >= 4 )
			{
				if( curr_paso == 4 )
				{
					setTimeout('llenarDatosPortadas();', 1000);
					if( completado_1 != true )
					{
						Dialog('Debes completar el paso de envio');
						
						cambiarPaso(1);
						
						return false;
					}
					else if( completado_2 != true && form_type != 1 )
					{
						Dialog('Debes completar el paso de datos');
						
						cambiarPaso(2);
						
						return false;
					}
					else if( completado_3 != true )
					{
						Dialog('Debes completar el paso de encuadernaci&oacute;n');
						
						cambiarPaso(3);
						
						return false;
					}
					else
					{
						if( $defined(mySlide4) )
						{
							document.getElementById('paso_col_4').style.display = "";
							mySlide4.slideIn();
						}
					}
				}
				else if( !isPasoLoad(4) && $('editar').value == 0 )
				{
					$('tab_paso_4').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";					

					if( tipo == 1 )
					{					
						Dialog('Debes completar el paso de tu portada');
				
						cambiarPaso(4);
					
						return false;
					}
				}
				else if( ult_paso > 4 )
				{
					$('tab_paso_4').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";					
				}
				else
				{
					$('tab_paso_4').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";					
				}
			}
			
			if( curr_paso >= 5 )
			{
					if( completado_1 != true )
					{
						Dialog('Debes completar el paso de envio');
						
						cambiarPaso(1);
						
						return false;
					}
					else if( completado_2 != true && form_type != 1 || completado_2 != true && curr_paso == 7 )
					{
						Dialog('Debes completar el paso de datos');
						
						cambiarPaso(2);
						
						return false;
					}
					else if( completado_3 != true )
					{
						Dialog('Debes completar el paso de encuadernaci&oacute;n');
						
						cambiarPaso(3);
						
						return false;
					}
					else
					{
						if( $defined(mySlide5) )
						{
							document.getElementById('dv_precio').style.display = "";
							mySlide5.slideIn();
						}
						
						paso_5_load = true;
					}
			}
			else if( paso_5_load )
			{
				$('tab_paso_5').className = "procesoseleccion_parts_title procesoseleccion_parts_title_ok";					
			}
			else if( curr_paso != 5 && !paso_5_load )
			{
				$('tab_paso_5').className = "procesoseleccion_parts_title procesoseleccion_parts_title_off";					
			}
		}
		
function updatePortadaSizes()
{
	var ref = 'publicar/getPortadaSizes/' + $('tamano').value +  '/' + $('papel').value + '/' + (parseInt($('paginas_total').value) > 0 ? parseInt($('paginas_total').value) : 1) + '/1';

	new Ajax(ref, {
						onComplete: function(html) {
							if( $('completa_sizes') )
							{
								var sp = [];
								sp = html.split("|");
								$('completa_sizes').setHTML(sp[0]);
								var lomo_sizes = sp[2];
								var sp2 = [];
								sp2 = sp[3].split("x");

								var portada_sizes = (sp[1]-lomo_sizes)/2;
								
								$('lomo_sizes').setHTML(lomo_sizes);
								$('portada_sizes').setHTML(portada_sizes);
								$('contraportada_sizes').setHTML(portada_sizes);
								
								$('portada_cx').setHTML(sp2[0]);
								$('portada_cy').setHTML(sp2[1]);
							}
						}
	}).request();
	
}

function llenarDatosPortadas()
{
	if( $('portada_titulo').value == "" && $('titulo') )
	{
		$('portada_titulo').value = $('titulo').value;
	}

	if( $('portada_sinopsis') && !$('portada_sinopsis').value.match(/\w/) && $('sinopsis') )
	{
		// Moz
		var reg = new RegExp("((\\r\\n)?<span(.+)>(\\r\\n)?(\\s\\s)?)", "g");
		var reg2 = new RegExp("((\\r\\n)?</span>(\\r\\n])?)", "g");
		var sinop = $('sinopsis').value;
		
		sinop = sinop.replace(reg, "");
		sinop = sinop.replace(reg2, "");
		sinop = sinop.replace(/(\r|\n)/g, "");
		sinop = sinop.replace(/\s+/g, " ");
		sinop = sinop.replace(/(<br>(\s)?)/gi, "\r\n");
		
		//IE
		var reg3 = new RegExp("(<(\/)?(em|strike|bold|strong|underline|i|s|b|u)>)", "gi");
		
		sinop = sinop.replace(reg3, "");
		sinop = sinop.replace(/&nbsp;/gi, "");
		sinop = sinop.replace(/<p>/gi, "");
		sinop = sinop.replace(/<\/p>/gi, "\r\n");
		
		
		$('portada_sinopsis').value = sinop;
	}

	var daut = document.getElementsByName("autores[]");
	var taut = document.getElementsByName("autores_tipos[]");
	var sautores = "";
			
	for(var i = 0; i < daut.length; i++)
	{
			sautores += ( sautores == "" ? "" : ", ") + daut[i].value;
	}

	if( $('portada_autor').value == "" && sautores != "" )
	{
			$('portada_autor').value = sautores;
	}
}

function pedirEstado() {
	var objeto;
	var nombre = "main";
	
	if (navigator.appName.indexOf ("Microsoft") !=-1) 
	{
		objeto = window[nombre];
	}
	else
	{
		objeto = document[nombre];
	}
	
	objeto.enviarEstadoJS();
}

function recibeEstado(estado){
	alert(estado); 
}


function checkAllowed()
{
	var paginas_total = $("paginas_total").value;
	
	if( paginas_total >= 1 && paginas_total < 12 )
	{
		$("libro_enpapel").checked = false;
		$("libro_enpapel").disabled = true;
		
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "none";
			$("maque_4").style.display = "none";
			$("maque_5").style.display = "none";
		}
		
	}
	else if( paginas_total >= 12 && paginas_total < 20 )
	{
		$("libro_enpapel").disabled = false;		
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "none";
			$("maque_4").style.display = "";
			$("maque_5").style.display = "none";
			
			if( $("maquetacion").value == 3 || $("maquetacion").value == 5 )
				$("maquetacion").value = 4;
		}
		
	}
	else if( paginas_total >= 20 && paginas_total < 32 )
	{
		$("libro_enpapel").disabled = false;		
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "none";
			$("maque_4").style.display = "";
			$("maque_5").style.display = "";
			
			if( $("maquetacion").value == 3 )
				$("maquetacion").value = 4;
		}
	}
	else if( paginas_total >= 32 && paginas_total < 68 )
	{
		$("libro_enpapel").disabled = false;
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "";
			$("maque_4").style.display = "";
			$("maque_5").style.display = "";
		}
	}
	else if( paginas_total >= 68 && paginas_total < 800 )
	{
		$("libro_enpapel").disabled = false;		
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "";
			$("maque_4").style.display = "none";
			$("maque_5").style.display = "";
			
			if( $("maquetacion").value == 4 )
				$("maquetacion").value = 3;
		}
	}
	else // paginas_total > 800
	{
		$("libro_enpapel").checked = false;
		$("libro_enpapel").disabled = true;
		
		$("libro_enebook").disabled = false;
		
		if( $("maque_3") )
		{
			$("maque_3").style.display = "none";
			$("maque_4").style.display = "none";
			$("maque_5").style.display = "none";
		
		
		}
	}
}


function correctWys()
{
	if( $('sinopsis') )
	{
		$('sinopsis').value = $('wysfrm').contentWindow.document.body.innerHTML; 
	}
}