
var all_entreprise = {};
var all_dep = {};

function loadEntreprise ()
{
	var service = $('#sel_service').val();
	var dep = $('#sel_departement').val();
	all_entreprise = {};
	
	if ( service == -1 || dep == -1 )
	{
		alert("Vous devez choisir un d\351partement ET un service");
		return;
	}
	
	var data = {id_service:service, id_dep:dep};
	
	$.ajax
	({
		type: "POST",
		url: "./php/catia_entreprise/load.php",
		data: data,
		dataType:'xml',
		
		success: function(obj)
		{
			var nb = 0;
			$(obj).find("catia_entreprise").each ( function ()
			{
				var o = {};
				o['id'] = parseInt($(this).attr('id'));				
				o['dispo_actuelle'] = parseInt($(this).attr('dispo_actuelle'));
				o['libelle'] = $(this).attr('libelle');
				o['lat'] = parseFloat($(this).attr('lat'));
				o['lng'] = parseFloat($(this).attr('lng'));
				o['load_info'] = 0;
				o['m'] = null;
				
				
				all_entreprise [o['id']] = o;
				nb++;
				
			});
			if ( nb == 0 )
			{
				alert("Votre recherche ne renvoit aucun r\351sultat.");
			}
			
			
			displayEntreprises ();
		},
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
}


function loadInfoEntreprise ( obj )
{
	$.ajax
	({
		type: "POST",
		url: "./php/catia_entreprise/load_info.php",
		data: {id_ent:obj.id},
		dataType:'xml',
		
		success: function(xml)
		{
			//alert(txt);
			$(xml).find("catia_entreprise").each ( function ()
			{
				obj.lib_service = parseFromXML ( $(this).attr('lib_service') );
				obj.lib_departement = parseFromXML ( $(this).attr('lib_dep') );
				obj.contact = parseFromXML ( $(this).attr('contact') );
				obj.adresse = parseFromXML ( $(this).attr('adresse') );
				obj.cp = parseFromXML ( $(this).attr('cp') );
				obj.ville = parseFromXML ( $(this).attr('ville') );
				obj.tel_fixe = parseFromXML ( $(this).attr('tel_fixe') );
				obj.tel_port = parseFromXML ( $(this).attr('tel_port') );
				obj.fax = parseFromXML ( $(this).attr('fax') );
				obj.courriel = parseFromXML ( $(this).attr('courriel') );
				obj.web = parseFromXML ( $(this).attr('web') );
				obj.description = parseFromXML ( $(this).attr('description') );

				obj.duree_timer = parseFromXML ( $(this).attr('duree_timer') );
				obj.is_dispo = parseFromXML ( $(this).attr('is_dispo') );
				obj.date_dispo = parseFromXML ( $(this).attr('date_dispo') );


				jQuery('#bubble-temp').html(jQuery('#for_hidden').html());
		
				jQuery('#bubble-temp #bulle-info #bulle-titre').html ( obj.libelle + " - " + obj.lib_service );
				jQuery('#bubble-temp #bulle-info #bulle-adresse').html ( obj.adresse );
				jQuery('#bubble-temp #bulle-info #bulle-ville').html ( obj.cp + " - " + obj.ville );
		
				jQuery('#bubble-temp #bulle-info #bulle-contact').html ( obj.contact );
				if ( obj.contact == "" )
					jQuery('#bubble-temp #bulle-info #bulle-contact').css ( 'display', 'none' );
				else
					jQuery('#bubble-temp #bulle-info #bulle-contact').css ( 'display', '' );
	
				jQuery('#bubble-temp #bulle-info #bulle-description').html ( obj.description );
		
				var tel_fixe = obj.tel_fixe;
				var tel_port = obj.tel_port;
				var fax = obj.fax;
				var mail = obj.courriel;
				var web = obj.web;
		
				var tot = 0;
				if ( mail != "" )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-2 a#mail').html ( 'E-Mail' );
					jQuery('#bubble-temp #bulle-info #bulle-coord-2 a#mail').attr ( 'href', 'mailto:'+ mail);
					tot +=1;
				}
				if ( web != "" )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-2 a#web').html ( 'Site Internet' );
					jQuery('#bubble-temp #bulle-info #bulle-coord-2 a#web').attr ( 'href', 'http://'+ web);
					tot +=1;
				}
				if ( tot == 2 )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-2 span#sep3').html (' - ');
				}	
				
				tot = 0;
				
		
		
				if ( tel_fixe != "" )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#tel').html ( "<b>T&eacute;l&eacute;phone : </b>"+tel_fixe );
					tot +=1;
				}
				if ( tel_port != "" )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#port').html ( "<b>Portable : </b>"+tel_port );
					tot +=1;
				}
				if ( fax != "" )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#fax').html ( "<b>Fax : </b>"+fax );
					tot +=1;
				}
		
		
				if ( tot == 2 )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#sep1').html (' - ');
				}		
				else if ( tot == 3 )
				{
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#sep2').html (' - ');
					jQuery('#bubble-temp #bulle-info #bulle-coord-1 span#sep1').html (' - ');
				}
			
		
	
				h = jQuery('#bubble-temp').html();
		
	
		
		
				obj.m.openInfoWindowHtml ( h, {maxWidth:450} );
		


			});
		},
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
}

function loadDepartement (  )
{
	$.ajax
	({
		type: "POST",
		url: "./php/catia_departement/load.php",
		data: ''/*{clause:' where id_region='+id_region}*/,
		dataType:'xml',
		
		success: function(obj)
		{
			$(obj).find("catia_departement").each ( function ()
			{
				all_dep [ $(this).attr('id') ] = {lat:parseFloat($(this).attr('lat')), lng:parseFloat($(this).attr('lng'))};
				$('#sel_departement').append($("<option></option>").attr("value",$(this).attr('id')).text($(this).attr('libelle')));
			});
		},
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
}

function loadCategorie ()
{
	$.ajax
	({
		type: "GET",
		url: "../backoffice/php/catia_categorie/load.php",
		data: '',
		dataType:'xml',
		success: function(obj)
		{
			$(obj).find("catia_categorie").each ( function ()
			{
				$('#sel_categorie').append($("<option></option>").attr("value",$(this).attr('id')).html($(this).attr('libelle')));
			});
		},
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
}

function loadServices ( id_cat )
{
	$.ajax
	({
		type: "POST",
		url: "../backoffice/php/catia_service/load.php",
		data: {id_cat:id_cat},
		dataType:'xml',
		success: function(obj)
		{
			clearSelect ('sel_service');
			$('#sel_service').append($("<option></option>").attr("value","-1").text("Choisissez un service"));
			$(obj).find("catia_service").each ( function ()
			{				
				$('#sel_service').append($("<option></option>").attr("value",$(this).attr('id')).html($(this).attr('libelle')));
			});
		},
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});
}	

function clearSelect ( id )
{
	document.getElementById (id).options.length = 0;
}

function isNumber(val){
	return /^-?((\d+\.?\d?)|(\.\d+))$/.test(val);
}


function parseFromXML(xmlStr) 
{
	xmlStr = xmlStr.replace(/&lt;/g,'<'); // 60 3C
	xmlStr = xmlStr.replace(/&gt;/g,'>'); // 62 3E
	xmlStr = xmlStr.replace(/&quot;/g,'"'); // 62 3E
	xmlStr = xmlStr.replace(/&#39;/g,"'"); // 62 3E
	xmlStr = xmlStr.replace(/&amp;/g,'#'); // 62 3E

	xmlStr = xmlStr.replace(/\\\\'/g, "'");
	xmlStr = xmlStr.replace(/\\'/g, "'");

	return xmlStr; 
} 
