// JavaScript Document


// Create the Divtag Handler -- Mainly an IE 6 Fix 
var divhandler = new handleDivTag(null); 
var timestamp = new Date().getTime();

// The path of where the PHP script
var phpscript = '../includes/ajax.php?timestamp='+timestamp+'&f=';

// Function for making the AJAX object
function createRequestObject() {
   var request;
	// Firefox, Safari, Opera... 
	if(window.XMLHttpRequest){ 
		request = new XMLHttpRequest();
	// Internet Explorer 5+ 
   	} else if(window.ActiveXObject) {
      	request = new ActiveXObject("Msxml2.XMLHTTP");
	// There is an error or an old browser is being used.
	} else if(window.ActiveXObject) {
      	request = new ActiveXObject("Microsoft.XMLHTTP");
	// There is an error or an old browser is being used.
	}else {
		request = null;
		alert('Problem creating the XMLHttpRequest object');
   	}
   	return request;
} 

// Creating the divtag object
function handleDivTag(divtag) { 
   var divtag; 
   return divtag; 
} 

// Make the XMLHttpRequest object 
var http = createRequestObject(); 


// Main AJAX request function, called via event handlers
function sendRequest(act, divtag,  value1, value2, value3) { 
	// Open PHP script for requests and send values via GET
   http.open('get', phpscript+act+'&value1='+escape(value1)+'&value2='+escape(value2)+'&value3='+escape(value3)); 
   // When ready, handle the response
   http.onreadystatechange = handleResponse;
   // Pass the divtag through
   divhandler.divtag = divtag;
   // Send nothing (GET has already passed values)
   http.send(null);
}

function handleResponse() { 
//	document.getElementById(divhandler.divtag).innerHTML = '<center>Loading...</center>';
	// Checking if server is ready
	if(http.readyState == 4){
//		alert('this > '+showAlert);
		if(http.status == 200){ 
			var response = http.responseText; 
			if(document.getElementById(divhandler.divtag) != null){
				document.getElementById(divhandler.divtag).innerHTML = response;
			}
		}/* else{ 
			alert("ERROR! Request status is "+ http.status); 
		}*/
	}
}

function updateCart(prodId, prodCode, price, quantity)
{
	sendRequest('updateCart', 'buyItem_'+prodCode, prodId, prodCode, quantity, price);
	
	price = parseFloat(price);
	quantity = parseInt(quantity);

	basketItems = parseInt(document.getElementById('shopBasketItems').value);
	basketTotal = parseFloat(document.getElementById('shopBasketTotal').value.substr(2));
	
	if(basketItems == '')
	{basketItems = 0;}	
	
	basketItems = (basketItems + quantity);
	
	totalPrice = quantity*price;
	basketTotal = (basketTotal + totalPrice);
	document.getElementById('shopBasketItems').value = basketItems;
	document.getElementById('shopBasketTotal').value = "\u00A3 "+basketTotal.toFixed(2);
	if(document.getElementById('quantity_'+prodCode) != null){
		document.getElementById('quantity_'+prodCode).selectedIndex = 0;
	}
	alert(quantity+' x '+prodCode+' added to your order')
	
}

function showHide(divtag)
{
	div = document.getElementById(divtag);
	if(div.style.display == 'none')
	{
		div.style.display = 'block';	
	}else{
		div.style.display = 'none';	
	}
}

function drawMarkers(cid, cat, rid, stext, pid){
	if (!map) {
		alert('Error !');
		return;
	}
	//map.setMapType(G_HYBRID_MAP);
	//alert('this is where the markers would be drawn');
	obj = getHTTPObject();
	var url = 'includes/xmlhttp.php';
	url += '?cmd=listposts' + '&cid=' + cid + '&cat=' + cat + '&rid=' + rid + '&stext=' + stext + '&id=' + pid;
	
	if(obj){
	
	
	obj.onreadystatechange = function() {
		if (obj.readyState == 4)  {
			//stuff to make it work
			
			var xmldoc = obj.responseXML;
			var db_nodes = xmldoc.getElementsByTagName('db_record'); 
			var n_records = db_nodes.length;
			var p_x, p_y, p_html, p_marker_html;
			var noItems = GetXMLValue(xmldoc.getElementsByTagName('all_items'));
			map.clearOverlays();
			//var mgr = new GMarkerManager(map);
			//clear div
			if(document.getElementById('recent-posts-content') != null){
				document.getElementById('recent-posts-content').innerHTML = '';
				if(n_records == 0){
					document.getElementById('recent-posts-content').innerHTML = '<p class="recent-posts-link">Sorry there were no posts found, please try again.</p>';
				}
			}
			
			//alert('record='+n_records);
			var batch = [];
			for (i=0; i < n_records; i++) {
				p_x = GetXMLValue(db_nodes[i].getElementsByTagName('p_x'));
				p_y = GetXMLValue(db_nodes[i].getElementsByTagName('p_y'));
				p_html = GetXMLValue(db_nodes[i].getElementsByTagName('p_html'));
				p_marker_html = GetXMLValue(db_nodes[i].getElementsByTagName('p_marker_html'));
				id = GetXMLValue(db_nodes[i].getElementsByTagName('p_id'));
				cat_Category = GetXMLValue(db_nodes[i].getElementsByTagName('cat_Category'));
				if(document.getElementById('recent-posts-content') != null){
					document.getElementById('recent-posts-content').innerHTML += p_html;
				}
				point = new GLatLng(p_y, p_x);
				if(pid > 0){
					//var marker = new GMarker(point, poicon);
					var marker = createMarker(point,p_marker_html, cat_Category)
				}else{
					var marker = createMarker(point,p_marker_html, cat_Category)
				}
				
				//if(i == 0){
				//	alert('|' + cat_Category + '|');
				//}
				
				//batch.push(marker);
				//alert('markerid=' + id);
				map.addOverlay(marker);
				markerIDs[id] = marker;
			}
			//mgr.addMarkers(batch, 4, 17);
			//mgr.refresh();
			//alert(mgr.getMarkerCount(4));
			if(pid > 0){
				//alert(p_y+'='+p_x)
				map.setCenter(new GLatLng(p_y, p_x), 15);
				//map.setMapType(G_NORMAL_MAP);
				//ViewIncident(pid, p_x, p_y);
			}
					
		}  
	 }   
	obj.open('GET', url, true);
	obj.send(null);
	
	}
}

function GetXMLValue(obj)	{
	if (obj)	{
		var node = obj;
		if (node[0])	{
			var node_val = node[0].firstChild.nodeValue;	
			return node_val;
		}
		else	{
			//window.location.reload(true);
		}
	}
	return "";
}
