//---------- reaction when list has been modified
function changelist() {
	// get coordinates from list	
	coord=document.getElementById("list1").value;
	// set position is position field
	document.getElementById("position").value = coord;	
	// refresh
	refreshme(); 
}
//---------- reaction when radius has been modified
function changerad() {
	// get radius from radiobuttons
	for (i=0;i<document.formtweasy1.radiobutton.length;i++){
		if (document.formtweasy1.radiobutton[i].checked){
			radius = document.formtweasy1.radiobutton[i].value;
		}
	}
	// put value in radius field
	document.getElementById("rad").value = radius;
	refreshme();
}
//---------- initiates refresh depending on what is displayed (map or list)
function refreshme() { 	
	// if map is on
	if (document.getElementById("mapbox").style.display=='block') {			
		document.getElementById("map_icon").style.backgroundColor='#B41221';
		document.getElementById("list_icon").style.backgroundColor='';
		document.getElementById("logo_icon").style.backgroundColor='';
		initmap(); // get map values		
	}
	// if list is on
	if (document.getElementById("cont_list").style.display=='block') {	
		if (document.getElementById("view").value==1) {
			document.getElementById("map_icon").style.backgroundColor='';
			document.getElementById("list_icon").style.backgroundColor='#B41221';
			document.getElementById("logo_icon").style.backgroundColor='';
			AjaxCall('1'); // get list values, call type 1
		}		
		if (document.getElementById("view").value==2) {
			document.getElementById("map_icon").style.backgroundColor='';
			document.getElementById("list_icon").style.backgroundColor='';
			document.getElementById("logo_icon").style.backgroundColor='#B41221';
			AjaxCall('2'); // get list values, call type 2
		}
	}
}
//---------- switches to map
function showmap(){
	// hide list
	document.getElementById("cont_list").style.display='none';
	// show map 
	document.getElementById("mapbox").style.display='block';
	// get values
	refreshme();
}
//---------- switches to list
function backtolist(list_type){
	var list_type;
	// hide map
	document.getElementById("mapbox").style.display='none';
	// show list
	document.getElementById("cont_list").style.display='block';
	// save value of view in view field (1=list1,2= liste2)
	document.getElementById("view").value=list_type;
	// get values
	refreshme()	
}
//---------- switches to logo
function showfirms(){
	// hide map
	document.getElementById("mapbox").style.display='none';
	// show list
	document.getElementById("cont_list").style.display='block';
	// get values
	refreshme();
}
//---------- see offer details: reaction to click on offer
function showoffer(id) {
	// manage containers
	document.getElementById("cont_detail").style.display='block';
	document.getElementById("sidemenu").style.display='none';
	// get details
	if (document.getElementById("cont_detail").style.display=='block') {	
		AjaxCallOfferDetail(id); 
	}	
}
//---------- see seller details: reaction to click on offer
function showseller(id) {
	// manage containers
	document.getElementById("cont_detail").style.display='block';
	document.getElementById("sidemenu").style.display='none';
	// get details
	if (document.getElementById("cont_detail").style.display=='block') {	
		AjaxCallSellerDetail(id); 
	}	
}
//---------- see seller details: reaction to click on list offer
function showplace(id) {
	// manage containers
	document.getElementById("cont_detail").style.display='block';
	document.getElementById("sidemenu").style.display='none';
	// get details
	if (document.getElementById("cont_detail").style.display=='block') {	
		AjaxCallSellerPlace(id); 
	}	
}
//---------- send referral mail
function sendmail(id,operation) {
	// get details
	if (document.getElementById("cont_detail").style.display=='block') {	
		AjaxSendmail(id,operation); 
	}	
}
//---------- see all offers for seller: reaction to click on offer
function showoffers4seller(id) {
	// manage containers
	document.getElementById("cont_detail").style.display='block';
	document.getElementById("sidemenu").style.display='none';
	// get details
	if (document.getElementById("cont_detail").style.display=='block') {	
		AjaxCallOffers4Seller(id); 
	}	
}
//---------- hide detail containter
function hidedetail() {
	// manage containers
	document.getElementById("cont_detail").style.display='none';
	document.getElementById("sidemenu").style.display='block';	
	// if map is on hide list
	if (document.getElementById("mapbox").style.display=='block') document.getElementById("cont_list").style.display='none';
	// if list is on hide map
	if (document.getElementById("cont_list").style.display=='block') document.getElementById("mapbox").style.display='none';
}
//---------- get coordinates and radius from field
function getcoord()  { 
	// retrieve coordinates(element ID) and radius (radiobuttons)
	coord=document.getElementById("position").value;
	virg=coord.indexOf(',');
	coordx=coord.substr(1,6);
	coordy=coord.substr(virg+2,5);
	// get radius from field
	radius=document.getElementById("rad").value;
	// 
	return (coordx,coordy,radius);
}
//---------- AJAX CALL FOR LISTS (normal list and logos)
// make asynchronous HTTP request using the XMLHttpRequest object 
function AjaxCall(call_type){
	var call_type;
	//if (call_type==1) alert ('liste normale'); //liste normale
	//if (call_type==2) alert ('entreprise'); // liste par entreprise et logo 
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
	// get coordinates
	getcoord();
	// make call 1=liste normale 2=liste par logos
	if (call_type==1) xmlHttp.open("GET", "Calls/GetLmOffers.php?coordx="+coordx+"&coordy="+coordy+"&rad="+radius, true); 
	if (call_type==2) xmlHttp.open("GET", "Calls/GetLmOffers_Company.php?coordx="+coordx+"&coordy="+coordy+"&rad="+radius, true);
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxCall()', 1000);
}

//---------- AJAX CALL FOR OFFER DETAIL
function AjaxCallOfferDetail(id){
	var id;
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
		// make call 
		xmlHttp.open("GET", "Calls/GetLmOffer_Detail.php?id="+id, true); 
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxCallOfferDetail(id)', 1000);
}

//---------- AJAX SELLER DETAIL
function AjaxCallSellerDetail(id){
	var id;
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
		// make call 
		xmlHttp.open("GET", "Calls/GetLmSeller_Detail.php?id="+id, true); 
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxCallSellerDetail(id)', 1000);
}

//---------- AJAX - SEE LOCATION FOR THIS SELLER
function AjaxCallSellerPlace(id){
	var id;
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
		// make call 
		xmlHttp.open("GET", "Calls/GetLmSeller_Place.php?id="+id, true); 
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxCallSellerPlace(id)', 1000);
}
//---------- AJAX CALL FOR ALl OFFERS FROM SELLER
function AjaxCallOffers4Seller(id){
	var id;
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
		// make call 
		xmlHttp.open("GET", "Calls/GetLmOffers4Seller.php?id="+id, true); 
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxCallOffers4Seller(id)', 1000);
}

//---------- AJAX SEND MAIL TO FRIEND
function AjaxSendmail(id,operation,to,message){
	var id;
	var operation;
	// get values
	if (operation=='check') {
		var tomail=document.getElementById("sendmail_to").value;
		var frommail=document.getElementById("sendmail_from").value;
		var message=document.getElementById("sendmail_message").value;
	}
	else {
		var tomail='';
		var frommail='';
		var message='';	
	}
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){		
		// make call 
		xmlHttp.open("GET", "Calls/GetLmSendmail.php?id="+id+"&operation="+operation+"&tomail="+tomail+"&frommail="+frommail+"&message="+message, true); 
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('AjaxSendmail(id,operation)', 1000);
}
//---------- HANDLE ALL RESPONSES
function handleServerResponse()
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) {
  	// status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) {
      // extract the text received
      response = xmlHttp.responseText;
	
	  // show text in browser
	  	// RESPONSE FOR DETAIL IN DETAIL CONTAINER
	  	if (document.getElementById("cont_detail").style.display=='block'){		
			document.getElementById("cont_detail").innerHTML = response;
		}
		// RESPONSE FOR LIST IN LIST CONTAINER
		else {  
			// extract first part of text with info on number of sellers
			end=response.length;
			pos=response.indexOf(',');
			text=response.substr(0,pos);
	  		document.getElementById("cont_submenu").innerHTML = text;
			response=response.substring(pos+1,end);
			document.getElementById("cont_list").innerHTML = response;
   		}
    } 
    // a HTTP status different than 200 signals an error
    else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

//-------- REFERENCE TO  XMLHttpRequest OBJECT
// retrieves the XMLHttpRequest object
var	xmlHttp = createXmlHttpRequestObject(); 
function createXmlHttpRequestObject() {	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject){
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}
//-------- 
