
function confirma(pregunta, url, objeto){
	if (confirm(pregunta)){
		window.location.href=url;
	}else{
		objeto.checked = false;
	};
}



function orden(url){
		window.location.href=url;
}

function guardarOrden(num){
	var id, orden;
	for(i=0;i<num;i++){
		id=document.formbuscador['id['+i+']'].value;
		orden = document.formbuscador['orden['+id+']'].value;
		
		var guardo_orden = { 
		} 
		 
		var request = YAHOO.util.Connect.asyncRequest('GET', "procesos.php?accion=actualizarOrden&id="+id+"&orden="+orden, guardo_orden); 
		
	}
	return false;
}

function confirmoURL(pregunta, url){
	if(confirm(pregunta)){
		window.location.href = url;
	}
}
function modificarOrdenesNoticias(comercio,noticia,orden_ant,orden){
	
	if(confirm('Si aceptas se actualizaran todos los ordenes de esta web.')){
		//modificamos todos los ordenes superiores a el
		window.location.href = 'index.php?modulo=noticia&accion=actualizar_ordenes&comercio='+comercio+'&id='+noticia+'&orden='+orden;
	}
	else{
		//dejamos el orede como estaba anteriormente
		 document.formbuscador['orden['+comercio+']['+noticia+']'].value=orden_ant;
	}
	return false;
}

function validaBusqueda(iso639){
	var error = "";
	var enviar = false;
	if((!enviar) && (document.formbusquedaavanzada.referencia.value != "")) enviar = true;
	if((!enviar) && (document.formbusquedaavanzada.familia.value != "")) enviar = true;
	if((!enviar) && ((document.formbusquedaavanzada.precio_desde.value != "") || (document.formbusquedaavanzada.precio_hasta.value != ""))){
		enviar = true;
		if((((document.formbusquedaavanzada.precio_desde.value != "") && (document.formbusquedaavanzada.precio_hasta.value != "")))
			&& (parseFloat(document.formbusquedaavanzada.precio_desde.value) > parseFloat(document.formbusquedaavanzada.precio_hasta.value))){
				enviar = false;
				if(iso639 == "ES") error = "Parámetro precio incorrecto.";
				else if(iso639 == "EN") error = "Wrong price parameter.";
				else if(iso639 == "FR") error = "Paramètre de prix incorrect.";
				document.formbusquedaavanzada.precio_desde.value = "";
				document.formbusquedaavanzada.precio_hasta.value = "";
			}
			
	}
	if((!enviar) && ((document.formbusquedaavanzada.unidades_desde.value != "") || (document.formbusquedaavanzada.unidades_hasta.value != ""))){
		enviar = true;
		if((((document.formbusquedaavanzada.unidades_desde.value != "") && (document.formbusquedaavanzada.unidades_hasta.value != "")))
			&& (parseFloat(document.formbusquedaavanzada.unidades_desde.value) > parseFloat(document.formbusquedaavanzada.unidades_hasta.value))){
				enviar = false;
				if(iso639 == "ES") error = "Parámetro unidades incorrecto.";
				else if(iso639 == "EN") error = "Wrong units parameter.";
				else if(iso639 == "FR") error = "Paramètre d'unités incorrect.";
				document.formbusquedaavanzada.unidades_desde.value = "";
				document.formbusquedaavanzada.unidades_hasta.value = "";
			}
	}
	if(!enviar){
		if(error != "") alert(error);
		else{
			if(iso639 == "ES") alert("Debe seleccionar algún parámetro por el que realizar la búsqueda.");
			else if(iso639 == "EN") alert("Select a search criteria.");
			else if(iso639 == "FR") alert("Choissisez un critère de recherche.");
		}
		return false;
	}else{
		document.formbusquedaavanzada.submit();
	}
}
function formatoPostgresDecimalBusqueda(num){
	if(num != ""){
	  var aux,devuelve,lon,lon2,pos_coma, pos_puntos, n_puntos,desde,decimal, encuentra_coma;
      num = num.toString().replace(/\ | /g,'');
      encuentra_coma=num.indexOf(',');            

      if (encuentra_coma!='-1'){
      	 cents=num.substring(encuentra_coma+1,encuentra_coma+3);	
      	 num=num.substring(0,encuentra_coma);
      }  
      else
      {
	      if(isNaN(num))
	        num = "0";
	      
	     cents = Math.floor((num*100+0.5)%100);
	     //lo paso a cadena para ver su longitud
	     cents = ""+cents;
	     if(cents.length==1){
	     	cents = "0"+cents;
	     }
	     //	para volver a convertirlo a n�mero
	     num = Math.floor((num*100+0.5)/100).toString();
	  }
		
	  if ((cents.toString().length==1)) 
        cents = cents+ "0";
      else
      {
		if (!cents.toString().length)
			cents="00";		
      }

      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
	  
      return(num+','+cents);
	}else return("");
}

function alertaNoticia(valor){
	/*if(valor.checked==true){
		alert('Si seleccionas esta noticia como destacada modificaras todas las de esta web.');
	}*/
	return false;
}
function alertaNoticiaOrden(){
	alert('Si cambias el orden de esta noticia se modificaran todos los de esta web.');
	return false;
}

function destacaNoticiaComercio(pregunta,noticia,comercio,sel){
	
	if(confirm(pregunta)){

		window.location.href = "index.php?modulo=noticia&accion=actualizarDestacada&id="+noticia+"&comercio="+comercio+"&value="+sel.checked;	
	}else{
		if(sel.checked==true){
			sel.checked=false;
		}
		else{
			sel.checked=true;
		}
	}
	return false;
}

function activaNoticiaComercio(pregunta,noticia,comercio,sel){
	
	if(confirm(pregunta)){
		//mandamos al ajax para actualizar en BD
		ajax=nuevoAjax();
		ajax.open("GET", "procesos.php?accion=activaNoticiaComercio&id="+noticia+"&comercio="+comercio+"&value="+sel.checked,false);
		ajax.onreadystatechange=function() { 
			/*if (ajax.readyState==4) { 
			   contenedor.innerHTML = ajax.responseText 
			}else{
			   contenedor.innerHTML = ''
			}*/
		} 
		ajax.send(null) 
		
	}else{
		if(sel.checked==true){
			sel.checked=false;
		}
		else{
			sel.checked=true;
		}
	}
	return false;
}

function boletinNoticiaComercio(pregunta,noticia,comercio,sel){
	
	if(confirm(pregunta)){
			ajax=nuevoAjax();
			ajax.open("GET", "procesos.php?accion=boletinNoticiaComercio&id="+noticia+"&comercio="+comercio+"&value="+sel.checked,false);
			ajax.onreadystatechange=function() { 
		} 
		ajax.send(null) 
		
	}else{
		if(sel.checked==true){
			sel.checked=false;
		}
		else{
			sel.checked=true;
		}
	}
	return false;
}

function guardaOrdenFotoNoticia(noticia_id,foto_id,orden_ant, orden){
	var array = new Array();
	if(confirm('Si aceptas se actualizaran todos los ordenes.')){

		var cargar = { 
			success:cargarOrdenesFoto
		} 
		
		var request = YAHOO.util.Connect.asyncRequest('GET',"../ajax/guardaOrdenFotoNoticia.php?noticia_id="+noticia_id+"&id="+foto_id+"&orden="+orden, cargar); 
		
		
	}else{
		document.formfoto['orden['+foto_id+']'].value=orden_ant;
	}
	return false;
}

function cargarOrdenesFoto(o){
	var respuesta = o.responseText;
	
	var array1 = new Array();
	array1 = respuesta.split(',');
	if(array1.length>0){
		var b = array1[0];
		var temp = new Array();
		temp = b.split('|');
	
		var c = array1[1];
		var temp2 = new Array();
		temp2 = c.split('|');
		
	    for(var i=0;i<temp.length;i++){
	   		document.formfoto['orden['+temp[i]+']'].value=temp2[i];
	    }
	}
}

function validaBusquedaReferencia(iso639){
	if(document.formBusqueda.buscador_referencia.value == ''){
		if(iso639 == "ES") alert("Debe introducir la referencia por la que desea realizar la búsqueda.");
		else if(iso639 == "EN") alert("Please enter the reference to search.");
		else if(iso639 == "FR") alert("Vous devez introduire la référence de laquelle vous désirez faire la recherche.");
		return false;	
	}else{
		document.formBusqueda.submit();	
	}
}

function validaBusquedaNombre(iso639){
	if(document.formBusqueda2.buscador_nombre.value == ''){
		if(iso639 == "ES") alert("Debe introducir la descripción por la que desea realizar la búsqueda.");
		else if(iso639 == "EN") alert("Please enter the description to search.");
		else if(iso639 == "FR") alert("Vous devez introduire la description sur laquelle vous désirez faire la recherche.");
		return false;	
	}else{
		document.formBusqueda2.submit();
	}
}
function validaAccesoExtranet(iso639){
	var error = "";
	if(document.formUsuario.login.value == ''){
		if(iso639 == "ES") error += "Debe introducir su nombre de Usuario.\n";	
		else if(iso639 == "EN") error += "Please enter your usuar name.\n";	
		else if(iso639 == "FR") error += "Vous devez introduire son nom d´usager.\n";	
	}
	
	if(document.formUsuario.password.value == ''){
		if(iso639 == "ES") error += "Debe introducir su Contraseña.\n";	
		else if(iso639 == "EN") error += "Please enter your password.\n";	
		else if(iso639 == "FR") error += "Vous devez introduire son mot de passe.\n";
	}
	
	if(error != ""){
		alert(error);
		return false;
	}else{
		document.formUsuario.submit();
	}
}
function muestraError(error){
	if(error != ''){
		alert(error);
		document.formUsuario.login.focus();
		return false;
	}else return false;
}