function showEnviament(divId){
	$(".Enviament").each(function(){
		$(this).hide();
	});
	
	$("div#map_canvas").hide();
	$("#"+divId).show();
}
 	

function CarregaBotigaPoblacions(idpais,idprovincia){
	$('#tiendas').empty();
	$('#tiendas').append("<option value='' selected>"+msgEnviament["SeleccionarTienda"]+"</option>");
	$('#poblaciones').empty();
	$('#poblaciones').append("<option value='0' selected>"+msgEnviament["CargandoPoblaciones"]+"</option>");
	$('#horariolv').html("");
	$('#horariosb').html("");
	$('#telefono').html("");
	$("div#map_canvas").hide();
	$.post("/index.php", {
			resource: 		"FTPX900040",
			idpais:			idpais,
			idprovincia:	idprovincia
			}, function( xml )	{
					var poblacio	="";
					var txt_poblacio="";
	
					$('#poblaciones').empty();
					$('#poblaciones').append("<option value='0' selected>"+msgEnviament["SeleccionarPoblacion"]+"</option>");
					$(xml).find('poblacio').each(function(){
		        		poblacio	=$(this).attr('nom');
		        		txt_poblacio=poblacio.replace("'","|");
	
		        		$('#poblaciones').append("<option value='"+txt_poblacio+"'>"+poblacio+"</option>");
		        });
				
		});
}



function CarregaBotigaBotigues(idpais,poblacion){
	$('#tiendas').empty();
	$('#horariolv').html("");
	$('#horariosb').html("");
	$('#telefono').html("");
	$("div#map_canvas").hide();
	$('#tiendas').append("<option value='' selected>"+msgEnviament["CargandoTienda"]+"</option>");
	$.post("/index.php", {
			resource: 	"FTPX900050",
			idpais:		idpais,
			poblacion:	poblacion
			}, function( xml )	{
					var id			="";
					var direccion	="";
	
					$('#tiendas').empty();
					$('#tiendas').append("<option value='' selected>"+msgEnviament["SeleccionarTienda"]+"</option>");
					$(xml).find('tienda').each(function(){
		        		id			=$(this).attr('id');
		        		direccion	=$(this).attr('direccion');
	
		        		$('#tiendas').append("<option value='"+id+"'>"+direccion+"</option>");
		        });
				
		});
	
}


function CarregaBotigaBotiga(idpais,idtienda){
	$('#horariolv').html("");
	$('#horariosb').html("");
	$('#telefono').html("");
	$("div#map_canvas").hide();
	
	$.post("/index.php", {
			resource: 	"FTPX900050",
			idpais:		idpais,
			idtienda:	idtienda
			}, function( xml )	{
					var id				="";
					var direccion		="";
					var horariolv		="";
					var horariosb		="";
					var telefono		="";
					var google_params	="";
					
					$(xml).find('tienda').each(function(){
		        		id				=$(this).attr('id');
		        		direccion		=$(this).attr('direccion');
		        		horariolv		=$(this).attr('horariolv');
						horariosb		=$(this).attr('horariosb');
						telefono		=$(this).attr('telefono');
						google_params	=$(this).attr('google_params');
						
						$('#horariolv').html(horariolv);
						$('#horariosb').html(horariosb);
						$('#telefono').html(telefono);
						$('#GoogleParams').val(google_params);
						$('#GoogleDireccion').val(direccion);
		        });
		        
		        if (($('#GoogleParams').val()!="") && ($('#GoogleDireccion').val()!="")) {
		        	$("#map_canvas").show();
		        	carga_googlemaps();
		        }
		        
		});
	
	$('#EnviamentBotons').show();
}


function CarregaDadesDomicili(f){
	var tipus=$("input[name='seldades']:checked").val();
	
	if (tipus=="mismo"){
		for (i=0;i<f.elements.length;i++)	{
			var type	=f.elements[i].type;
			var id		=f.elements[i].id;
			var newid	=id.replace("new","old");
			
			
			if (id!=""){
				if (id.substr(0,3)=="new"){
					$('#'+id).val('');
					if (type=="text") 		$('#'+id).attr("readonly",true);
					if (type=="select-one") $('#'+id).attr("disabled",true);
					
				}
			}
		}
		
		$('#codpostal').val("");
		$('#sel_poblacion').val("");
		$('#sel_poblacion').attr("disabled",true);
	}
	
	if (tipus=="otros"){
		for (i=0;i<f.elements.length;i++)	{
			var type	=f.elements[i].type;
			var id		=f.elements[i].id;
			var newid	=id.replace("new","old");
			
			
			if (id!=""){
				if (id.substr(0,3)=="new"){
					if (type=="text") 		$('#'+id).attr("readonly",false);
					if (type=="select-one") $('#'+id).attr("disabled",false);
					$('#'+id).val($('#'+newid).val());
				}
			}
		}
		
		$('#codpostal').val($('#oldcp').val());
		$('#sel_poblacion').attr("disabled",false);
		
		obtenirProvincies('newprovincia',$("#provincia").val());
		setTimeout("obtenirPoblacions('sel_poblacion')",600);
	}
	
	$('#EnviamentBotons').show();
}


function ValidarEnviament(){
	var seldades=$('input[name=seldades]:checked').val();
	var opcio1	=$('#FrmDomicili input[id=opcio]').val();
	var opcio2	=$('#FrmBotiga input[id=opcio]').val();
	
	var opcio	=(opcio1=="") ? opcio2 : opcio1;
	
	
	if (opcio=='botiga'){
		if ($('#tiendas').val()==""){
			alert(msgEnviament["NoTienda"]);
		}else{
			$("#FrmBotiga").trigger("submit");
		}
	}else{
		if (seldades=='mismo'){
			$("#FrmDomicili").trigger("submit");
		}else{
			if ($('#newnom').val()==""){
				alert(msgEnviament["NoNombre"]);
				$('#newnom').focus();
			}else{
				if ($('#newcognom').val()==""){
					alert(msgEnviament["NoApellido"]);
					$('#newcognom').focus();
				}else{
					if ($('#newdireccion').val()==""){
						alert(msgEnviament["NoDireccion"]);
						$('#newdireccion').focus();
					}else{
						if ($('#newpais').val()==""){
							alert("Tienes que seleccionar un pais");
							$('#newpais').focus();
						}else{
							if (($('#newprovincia').val()=="") || ($('#newprovincia').val()=="0")){
								alert(msgEnviament["NoProvincia"]);
								$('#newprovincia').focus();
							}else{
								if (($('#sel_poblacion').val()=="") || ($('#sel_poblacion').val()=="0")){
									alert(msgEnviament["NoPoblacion"]);
									$('#sel_poblacion').focus();
								}else{
									if ($('#newcp').val()==""){
										alert(sgEnviament["NoCP"]);
										$('#newcp').focus();
									}else{
										$("#FrmDomicili").trigger("submit");
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

function SinEnvios(){
	var seldades=$('input[name=seldades]:checked').val();
	var opcio1	=$('#FrmDomicili input[id=opcio]').val();
	var opcio2	=$('#FrmBotiga input[id=opcio]').val();
	var opcio	=(opcio1=="") ? opcio2 : opcio1;
	var prv_prohibidas=new Array('35','38');
	var cp='';
	
	if (opcio=='domicili'){
		if (seldades=='mismo'){
			cp=$('#oldcp').val();
		}else{
			cp=$('#codpostal').val();
		}
		var i=0;
		while(i<prv_prohibidas.length && prv_prohibidas[i]!=cp.substring(0,2)){
			i++;
		}
		if(i<prv_prohibidas.length && prv_prohibidas[i]==cp.substring(0,2)){
			$("#envio_denegado").show();
			return false;
		}
	}
	return true;
}