// Gestion de liste déroulante dynamique et éditable dans la partie SERVICE catégorie DEVIS
// *********************************************************************************************************************************
// Path to arrow images
	var arrowImage = './img/select_arrow.gif';	// Regular arrow
	var arrowImageOver = './img/select_arrow_over.gif';	// Mouse over
	var arrowImageDown = './img/select_arrow_down.gif';	// Mouse down

	var selectBoxIds = 0;
	var currentlyOpenedOptionBox = false;
	var editableSelect_activeArrow = false;

    function selectBox_switchImageUrl() {
		if (this.src.indexOf(arrowImage) >= 0) {
			this.src = this.src.replace(arrowImage,arrowImageOver);
		}
		else {
			this.src = this.src.replace(arrowImageOver,arrowImage);
		}
	}

	function selectBox_showOptions() {

		if (editableSelect_activeArrow && editableSelect_activeArrow!=this) {
			editableSelect_activeArrow.src = arrowImage;
			
		}
		editableSelect_activeArrow = this;
		
		var numId = this.id.replace(/[^\d]/g,'');
		var optionDiv = document.getElementById('selectBoxOptions' + numId);
		if (optionDiv.style.display=='block'){
			optionDiv.style.display='none';
			if (navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + numId).style.display='none';
			this.src = arrowImageOver;	
		}else{			
			optionDiv.style.display='block';
			if (navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + numId).style.display='block';
			this.src = arrowImageDown;	
			if (currentlyOpenedOptionBox && currentlyOpenedOptionBox!=optionDiv)currentlyOpenedOptionBox.style.display='none';	
			currentlyOpenedOptionBox= optionDiv;			
		}
	}
	
	function selectOptionValue() {

		var parentNode = this.parentNode.parentNode;
		var textInput = parentNode.getElementsByTagName('INPUT')[0];
		textInput.value = this.innerHTML;
		load_designation();
		this.parentNode.style.display='none';	
		document.getElementById('arrowSelectBox' + parentNode.id.replace(/[^\d]/g,'')).src = arrowImageOver;
		
		if(navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + parentNode.id.replace(/[^\d]/g,'')).style.display='none';
		
	}

	var activeOption;

	function highlightSelectBoxOption() {
		if(this.style.backgroundColor=='#316AC5') {
			this.style.backgroundColor='';
			this.style.color='';
		} else {
			this.style.backgroundColor='#316AC5';
			this.style.color='#FFF';			
		}	
		
		if(activeOption){
			activeOption.style.backgroundColor='';
			activeOption.style.color='';			
		}
		activeOption = this;
		
	}
	
	function updateEditableSelect(dest) {
		//remove all childs of selectBoxOptions0
		var optionDiv = document.getElementById('selectBoxOptions0');
		if ( optionDiv.hasChildNodes() ) {
			while ( optionDiv.childNodes.length >= 1 ) {
				optionDiv.removeChild( optionDiv.firstChild );       
			}
		}
		//get updated Options and create sub divs with these options
		var options = dest.getAttribute('selectBoxOptions').split(';');
		var optionsTotalHeight = 0;
		var optionArray = new Array();
		for(var no=0;no<options.length;no++) {
			var anOption = document.createElement('DIV');
			anOption.innerHTML = options[no];
			anOption.className='selectBoxAnOption';
			anOption.onclick = selectOptionValue;
			anOption.style.width = optionDiv.style.width.replace('px','') - 2 + 'px'; 
			anOption.onmouseover = highlightSelectBoxOption;
			optionDiv.appendChild(anOption);	
			optionsTotalHeight = optionsTotalHeight + anOption.offsetHeight;
			optionArray.push(anOption);
		}
		if(optionsTotalHeight > optionDiv.offsetHeight) {				
			for(var no=0;no<optionArray.length;no++) {
				optionArray[no].style.width = optionDiv.style.width.replace('px','') - 22 + 'px'; 	
			}
		}
		optionDiv.style.display='none';
		optionDiv.style.visibility='visible';
	}

	function createEditableSelect(dest) {

		dest.className='selectBoxInput';		
		var div = document.createElement('DIV');
		div.style.styleFloat = 'left';
		div.style.width = dest.offsetWidth + 16 + 'px';
		div.style.left = '140px';
                div.style.top= '-15px';
		div.style.position = 'relative';
		div.id = 'selectBox' + selectBoxIds;
		var parent = dest.parentNode;
		parent.insertBefore(div,dest);
		div.appendChild(dest);	
		div.className='selectBox';
		div.style.zIndex = 99;

		var img = document.createElement('IMG');
		img.src = arrowImage;
		img.className = 'selectBoxArrow';
		
		img.onmouseover = selectBox_switchImageUrl;
		img.onmouseout = selectBox_switchImageUrl;
		img.onclick = selectBox_showOptions;
		img.id = 'arrowSelectBox' + selectBoxIds;

		div.appendChild(img);
		
		var optionDiv = document.createElement('DIV');
		optionDiv.id = 'selectBoxOptions' + selectBoxIds;
		optionDiv.className='selectBoxOptionContainer';
		optionDiv.style.width = div.offsetWidth- 2 + 'px';
		div.appendChild(optionDiv);
		
		if(navigator.userAgent.indexOf('MSIE')>=0) {
			var iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');
			iframe.style.width = optionDiv.style.width;
			iframe.style.height = optionDiv.offsetHeight + 'px';
			iframe.style.display='none';
			iframe.id = 'selectBoxIframe' + selectBoxIds;
			div.appendChild(iframe);
		}
		
		if(dest.getAttribute('selectBoxOptions')) {
			var options = dest.getAttribute('selectBoxOptions').split(';');
			var optionsTotalHeight = 0;
			var optionArray = new Array();
			for(var no=0;no<options.length;no++) {
				var anOption = document.createElement('DIV');
				anOption.innerHTML = options[no];
				anOption.className='selectBoxAnOption';
				anOption.onclick = selectOptionValue;
				anOption.style.width = optionDiv.style.width.replace('px','') - 2 + 'px'; 
				anOption.onmouseover = highlightSelectBoxOption;
				optionDiv.appendChild(anOption);	
				optionsTotalHeight = optionsTotalHeight + anOption.offsetHeight;
				optionArray.push(anOption);
			}
			if(optionsTotalHeight > optionDiv.offsetHeight) {				
				for(var no=0;no<optionArray.length;no++) {
					optionArray[no].style.width = optionDiv.style.width.replace('px','') - 22 + 'px'; 	
				}	
			}		
			optionDiv.style.display='none';
			optionDiv.style.visibility='visible';
		}
		
		selectBoxIds = selectBoxIds + 1;
	}

	/*chargement des infos de contacts en ajax*/
	function load_contact(idcontact, idnom, idprenom, idfonction, idtelephone, idemail, idbouton, readmode) {
		/*global ActiveXObject, XMLHttpRequest, window, alert, load_marques */
		bouton = document.getElementById(idbouton);
		if (document.getElementById('contact').value == 999) {

			document.getElementById(idcontact).value = '';
			document.getElementById(idnom).value = '';
			document.getElementById(idprenom).value = '';
			document.getElementById(idfonction).value = '';
			document.getElementById(idtelephone).value = '';
			document.getElementById(idemail).value = '';


			if (bouton != null) bouton.value = 'Ajouter';

			document.getElementById(idcontact).readOnly = false;
			document.getElementById(idnom).readOnly = false;
			document.getElementById(idprenom).readOnly = false;
			document.getElementById(idfonction).readOnly = false;
			document.getElementById(idtelephone).readOnly = false;
			document.getElementById(idemail).readOnly = false;

		}
		else {
			if (bouton != null) bouton.value = 'Mettre à jour';

			var data = "id=" + document.getElementById(idcontact).value + "&idnom=" + idnom + "&idprenom=" + idprenom + "&idfonction=" + idfonction + "&idtelephone=" + idtelephone + "&idemail=" + idemail;
			var xhr_object = null; 
			 
			if (window.XMLHttpRequest) { // Firefox 
				xhr_object = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				try {
					xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			else { // XMLHttpRequest non supporté par le navigateur 
			  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
			  return;
			}
			 
			xhr_object.open("POST", "./includes/info_contact.php", true); 
			xhr_object.onreadystatechange = function() {
				if (xhr_object.readyState == 4) {
					eval(xhr_object.responseText);
					/*Put fields in Read-Only mode for devis only*/
					document.getElementById(idcontact).readOnly = readmode;
					document.getElementById(idnom).readOnly = readmode;
					document.getElementById(idprenom).readOnly = readmode;
					document.getElementById(idfonction).readOnly = readmode;
					document.getElementById(idtelephone).readOnly = readmode;
					document.getElementById(idemail).readOnly = readmode;
				}
			};
			xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			
			xhr_object.send(data);
		}
	}

	
	// Liste de matériel pour la partie SERVICE
	// ********************************************************************************************************************************

	function load_types() {
		/*global ActiveXObject, XMLHttpRequest, window, alert, load_marques */
		
		var xhr_object = null; 
		 
		if (window.XMLHttpRequest) { // Firefox 
			xhr_object = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		else { // XMLHttpRequest non supporté par le navigateur 
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		  return;
		}
		 
		xhr_object.open("POST", "./includes/liste_types.php", true); 
		xhr_object.onreadystatechange = function() {
			if (xhr_object.readyState == 4) {
				 eval(xhr_object.responseText);
				 load_marques();
			}
		};
		
		xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var data = "select=categorie"; 
		xhr_object.send(data);
	}
	
	function load_marques() {
	
		var categorie = '';
		for(i=0;i<document.getElementById("categorie").length ;i++) {
			  if (document.getElementById("categorie").options[i].selected == true) {
				categorie = document.getElementById("categorie").options[i].text;
			  }
		}

		var xhr_object = null; 
		 
		if(window.XMLHttpRequest) // Firefox 
		  xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) {
			try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		else { // XMLHttpRequest non supporté par le navigateur 
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		  return; 
		} 
		 
		xhr_object.open("POST", "./includes/liste_marques.php", true); 
		xhr_object.onreadystatechange = function() { 
		  if(xhr_object.readyState == 4) 
			 eval(xhr_object.responseText);
			 load_modeles();
		};

		xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var data = "categorie="+categorie+"&select=marque"; 
		xhr_object.send(data);
		
	}

	function load_modeles() {
		var categorie = '';
		for(i=0;i<document.getElementById("categorie").length ;i++) {
			if (document.getElementById("categorie").options[i].selected == true) {
				categorie = document.getElementById("categorie").options[i].text;
			}
		}
		
		var marque = '';
		for(i=0;i< document.getElementById("marque").length ;i++) {
			if (document.getElementById("marque").options[i].selected == true) {
				marque = document.getElementById("marque").options[i].text;
			}
		}

		var xhr_object = null; 
		 
		if(window.XMLHttpRequest) // Firefox 
		  xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) {
			try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		else { // XMLHttpRequest non supporté par le navigateur
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		  return;
		}
		
		xhr_object.open("POST", "./includes/liste_modeles.php", true);
		
		xhr_object.onreadystatechange = function() {
		  if(xhr_object.readyState == 4)
			 eval(xhr_object.responseText);
			 document.getElementById('modeletype').value = '';
			 document.getElementById('designation').value = '';
		};
		
		xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var data = "categorie="+categorie+"&marque="+marque+"&select=modeletype";
		xhr_object.send(data);
	}
	
	function load_designation() {
		var categorie = '';
		for(i=0;i<document.getElementById("categorie").length ;i++) {
			if (document.getElementById("categorie").options[i].selected == true) {
				categorie = document.getElementById("categorie").options[i].text;
			}
		}
		
		var marque = '';
		for(i=0;i< document.getElementById("marque").length ;i++) {
			if (document.getElementById("marque").options[i].selected == true) {
				marque = document.getElementById("marque").options[i].text;
			}
		}

		var modeletype = document.getElementById("modeletype").value;

		var xhr_object = null; 
		 
		if(window.XMLHttpRequest) // Firefox 
		  xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) {
			try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		else { // XMLHttpRequest non supporté par le navigateur
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		  return;
		}
		
		xhr_object.open("POST", "./includes/liste_designation.php", true);
		
		xhr_object.onreadystatechange = function() {
		  if(xhr_object.readyState == 4)
			 eval(xhr_object.responseText);
		};
		
		xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var data = "categorie="+categorie+"&marque="+marque+"&modeletype="+modeletype+"&select=designation";
		xhr_object.send(data);
	}

	//Function pour récupérer un paramètre dans l'url (en entrée: nom du paramètre de l'url en sortie: sa valeur
	function queryString(parameter) { 
	  var loc = location.search.substring(1, location.search.length);
	  var param_value = false;

	  var params = loc.split("&");
	  for (i=0; i<params.length;i++) {
		  param_name = params[i].substring(0,params[i].indexOf('='));
		  if (param_name == parameter) {
		      param_value = params[i].substring(params[i].indexOf('=')+1)
		  }
	  }
	  if (param_value) {
		  return param_value;
	  }
	  else {
		  return false; //Here determine return if no parameter is found
	  }
	}

	//Google Map
	//**********************************************************************************************************************************

	function load() {
		//if (typeof(GBrowserIsCompatible())=='function') {
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map"), { size: new GSize(490,300) } );
				map.setCenter(new GLatLng(gmap_lat,gmap_lng), gmap_zoom);
				map.addControl(new GLargeMapControl3D());
				
				var marker = new GMarker(new GLatLng(gmap_lat,gmap_lng));
				GEvent.addListener(marker, "click", function() {
				   var html = 'Ouest-Médical est ici!';
				   marker.openInfoWindowHtml(html);
				 });
				 map.addOverlay(marker);
			}
		//}
	}
	
	function map_adm() {
		if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("map"), { size: new GSize(490,300) } );
				map.setCenter(new GLatLng(gmap_lat,gmap_lng), gmap_zoom);
				map.addControl(new GLargeMapControl3D());
			
				var html = 'Ouest-Médical est ici!';
				var latlng = new GLatLng(gmap_lat,gmap_lng);
			
				var marker = new GMarker(latlng, {draggable: true});
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
				});
				GEvent.addListener(marker, "dragstart", function() {
					map.closeInfoWindow();
				});
				
				GEvent.addListener(marker, "dragend", function() {
					marker.openInfoWindowHtml(html);
					document.getElementById('zoom').value = map.getZoom();
					latlng = marker.getLatLng();
					document.getElementById('lat').value = latlng.lat();
					document.getElementById('lng').value = latlng.lng();
				});
		  
				map.addOverlay(marker);
				 
				GEvent.addListener(map,"zoomend", function(oldlevel,newlevel) {     
					document.getElementById('zoom').value = map.getZoom();
					document.getElementById('lat').value = latlng.lat();
					document.getElementById('lng').value = latlng.lng();
				});
		}
	}
