/**
  * Open a connection to the specified URL, which is
  * intended to respond with an XML message.
  * 
  * @param string method The connection method; either "GET" or "POST".
  * @param string url    The URL to connect to.
  * @param string toSend The data to send to the server; must be URL encoded.
  * @param function responseHandler The function handling server response.
  */
 function xmlOpen(method, url, toSend, responseHandler)
 {
     if (window.XMLHttpRequest)
     {
         // browser has native support for XMLHttpRequest object
         req = new XMLHttpRequest();
     }
     else if (window.ActiveXObject)
     {
         // try XMLHTTP ActiveX (Internet Explorer) version
         req = new ActiveXObject("Microsoft.XMLHTTP");
     }
     
     if(req)
     {	 		 	
         req.onreadystatechange = responseHandler;
         req.open(method, url, true);
         req.setRequestHeader("content-type","text/xml");
         req.send(toSend);	
		 //alert (req.responseXML);
     }
     else
     {
         alert('Your browser does not seem to support XMLHttpRequest.');
     }
 } 
 
 function FillItems()
 { 	

	var i = 0;

	// Make sure the request is loaded (readyState = 4)
     if (req.readyState == 4)
     {
         // Make sure the status is "OK"
         if (req.status == 200)
         {
		 	
             var mapitemContainer = document.getElementById('mapitemContainer');
			 
             var notes = req.responseXML.getElementsByTagName('mappoint');
             var str = '';
             for(i=0; i < notes.length; i++)			
             {				 
                 var noteNode = notes.item(i);
                 if(noteNode != null && noteNode.hasChildNodes())
                 {					 	
                    if (i % 2)
						str += '<div class="row1">';
					else
						str += '<div class="row2">';
								
					 str += '<a href="javascript:getMapItem(this,'+noteNode.firstChild.lastChild.nodeValue+');">'+noteNode.lastChild.lastChild.nodeValue+'</a></div>';
                 }
             }
             mapitemContainer.innerHTML = str;			 		 
         }
         else
         {
             alert("There was a problem retrieving the XML data:\n" +
                     req.statusText);
         }
     }
 }
 
 
 function FillMap()
 { 	

     // Make sure the request is loaded (readyState = 4)
     if (req.readyState == 4)
     {
         // Make sure the status is "OK"
         if (req.status == 200)
         {			 	 
             var infoContainer = document.getElementById('infoContainer');							 
			 
             var notes = req.responseXML.getElementsByTagName('mapitem');
             var str = '';
			 
             for(i=0; i < notes.length; i++)
             {
                 var noteNode = notes.item(i);
				 var itemTitle = "";
				 var itemText = "";
				 var itemLink = "";
				 var itemImage = "";
				 var arcads_approved = 0;
				 var arcads_adcreativeID = 0;
				 var arcads_accountID = 0;
				 var arcResID = "";
				 var arcResEnabled = 0;
				 var arcResBookingModel = "";
				 				
                 if(noteNode != null && noteNode.hasChildNodes())
                 {	
				 	
					if (noteNode.childNodes[1].childNodes[0]){
						itemText = noteNode.childNodes[1].childNodes[0].nodeValue;
					}
					
					if (noteNode.childNodes[2].childNodes[0]){
						itemTitle = noteNode.childNodes[2].childNodes[0].nodeValue;
					}
				 
				 	if (noteNode.childNodes[3].childNodes[0]){
						itemLink = noteNode.childNodes[3].childNodes[0].nodeValue;
						
						if (noteNode.childNodes[7].childNodes[0]){
							arcads_approved = noteNode.childNodes[9].childNodes[0].nodeValue;
							arcads_adcreativeID = noteNode.childNodes[8].childNodes[0].nodeValue;
							arcads_accountID = noteNode.childNodes[7].childNodes[0].nodeValue;
						}
						
						if (noteNode.childNodes[10].childNodes[0]){
							arcResID = noteNode.childNodes[10].childNodes[0].nodeValue;
							arcResEnabled = noteNode.childNodes[11].childNodes[0].nodeValue;
							
							if (noteNode.childNodes[12].childNodes[0]){
								arcResBookingModel = noteNode.childNodes[12].childNodes[0].nodeValue;
							}
						}
						
						if (arcads_approved == 1){
							itemLink = 'http://axses.net/adlog/?event=ad.logClick&adChannelID=8&&accountID=' + arcads_accountID + '&adPlaceID=334&adCreativeID=' + arcads_adcreativeID + '&adGroupID=&webAddress=' + escape(itemLink); 							
						}
					}
					
					if (noteNode.childNodes[4].childNodes[0]){
						itemImage = noteNode.childNodes[4].childNodes[0].nodeValue;
					}					
				 				 					
					if (itemTitle.length > 0){
						str = str + '<h1>' + itemTitle + '</h1>';
					}
														
					if (itemImage.length > 0){
						str += '<img src="';
						str += itemImage;
						str += '" align="right" vspace="3" hspace="3">';
					 }
					 
					 if (itemText.length > 0){
						str += itemText;
					}
																
					if (itemLink.length > 0){
						//str += '<div id="mapitemLink"><a href="' + itemLink + '">Read more...</a></div>';
						itemLinkPopUp = 'onClick="mapitemBox=dhtmlwindow.open(\'mapitemBox\', \'iframe\', \'' + itemLink +'\', \'Barbados\', \'width=900px,height=550px,left=0px,top=0px,resize=0,scrolling=1,recal=1\'); return false"';
						
						str += '<div id="mapitemLink"><a href="#" ' + itemLinkPopUp + '>Read more...</a></div>';
					}
						
					if ((arcResEnabled == 1) && (arcResID.length > 0) && (arcads_accountID > 0)){												
						itemQuoteLinkURL = 'http://bookingsbarbados.com/encyc/archive/arcres/arcolrs/reservations-quote.cfm?iconmap='+arcResID+'&oemmisc=ads';
						itemQuoteLinkURL = 'http://axses.net/adlog/?event=ad.logClick&adChannelID=8&accountID=' + arcads_accountID + '&adPlaceID=334&adCreativeID=0&adGroupID=&webAddress=' + itemQuoteLinkURL;
						itemQuoteLinkPopUp = 'onClick="mapitemBox=dhtmlwindow.open(\'mapitemBox\', \'iframe\', \'' + itemQuoteLinkURL + '\', \'Booking Quote - '+ itemTitle + '\', \'width=900px,height=550px,left=0px,top=0px,resize=0,scrolling=1,recal=1\'); return false"';
						str += '<br><span class="itemQuoteLink"><a href="#" ' + itemQuoteLinkPopUp + '>Get a quote online from ' + itemTitle + '</a></span>';						
					}
					
					htmlcode = '<b>' + itemTitle + '</b><br>';
					htmlcode = htmlcode + '<br><input type="button" value="Zoom In" onclick="ZoomIn();" class="formBtn"/>&nbsp;&nbsp;<input type="button" value="Zoom Out" onclick="ZoomOut();"  class="formBtn"/>';

					if (itemLink.length > 0){
						htmlcode = htmlcode + '<br><br><a href="#" ' + itemLinkPopUp + '>Read more...</a>';
					}
										
					
					map.removeOverlay(currentPoint);
					
					point = new GLatLng(noteNode.childNodes[5].childNodes[0].nodeValue,noteNode.childNodes[6].childNodes[0].nodeValue);
					marker = new GMarker(point);
					map.addOverlay(marker);
					map.panTo(point);					
					map.openInfoWindowHtml(point,htmlcode);
					
					currentPoint = marker;
                 }
             }
             
			 infoContainer.innerHTML = str;				 
         }
         else
         {
             alert("There was a problem retrieving the XML data:\n" +
                     req.statusText);
		 }
     }
 }
 
function getMapItems(oElem, mapsetID){
	
	var mapitemContainer = document.getElementById('mapitemContainer');
	mapitemContainer.innerHTML = 'Loading map places...';
	
	var strParams = '?mapsetID='+mapsetID;
	var strFullURL = 'getItems.php?mapsetID='+mapsetID;	
	xmlOpen('GET',strFullURL,'',FillItems);	
}

function getMapItem(oElem,mapitemID){	
	var infoContainer = document.getElementById('infoContainer');
	infoContainer.innerHTML = 'Loading place...';

	var strFullURL = 'getItem.php?mapitemID='+mapitemID;	
	xmlOpen('GET',strFullURL,'',FillMap);
}

