//es: Make a POST to the server 
//es: and pass on any data from browser
//es: via the XMLHTTPRequest

function talktoServer(product){  //cmc: pass variablbe to function, this is needed for multipule counters  
window.product = product;        //cmc: make variable global for use in multipule functions 
	if (checkCookie())
	{
		cookie_status='set';
	}
	else
	{
		if (isCookieEnabled())
		{
			cookie_status='enabled';
		}
		else
		{
			cookie_status='disabled';
		}			
	}
	
	var req = newXMLHttpRequest();
	//es: register the callback handler function
  	var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser);
  	req.onreadystatechange = callbackHandler;
  	req.open("POST", "server_value.php?cookie_status="+cookie_status+"&product="+product, true); //cmc: expanded query string in include product detials
  	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  	//es: get the value from the text input element and send it to server
  	var msg_value = ""
  	req.send("msg="+msg_value);
  	delete(req);
	callbackHandler = null;
	msg_value = null;
}

//es: This is the callback functions that gets called
//es: for the response from the server with the XML data
var lastPing = 0;
function updateMsgOnBrowser(testXML) {

	var test = testXML.getElementsByTagName("test")[0];
	var message = testXML.getElementsByTagName("message")[0];
	var ip = testXML.getElementsByTagName("ip")[0];

	var timestamp = test.getAttribute("timestamp");
	if (timestamp > lastPing) {
		lastPing = timestamp;

		var ip_value = ip.firstChild.nodeValue;
		var message_value = message.firstChild.nodeValue;
		var result = parseFloat(message_value);  //cmc: need to convert result to float integer 
		var result2 = result.toFixed(2);  //cmc: round result to 2 decimal plces
		var result3 = result.toString();  //cmc: convert to string so we can perfrom substr()
		var result4 = result3.substr(0,result3.indexOf('.')+3);  //result has 2 decimal places but is not rounded
		var result5 = result3.substr(result3.indexOf('.')+3,4);  //this gets the remainder we knocked off above - to four decimal places
		//es: Here comes the server's answer
		//var remainder = result - result2
		var x = document.getElementById("x");
		x.innerHTML =	message_value;
		var msg_display = document.getElementById("msg_display");
		msg_display.innerHTML = 
			
			">> Bid Price (Rounded): £<span id='paypalpriceset'>" + result2 + "</span>"; 
	//		"<hr>Your IP: "+ ip_value + 
	//		" Server Timestamp: \""+ timestamp + "\"" ;
	}	
}
lastPing=null;


//es: the following two functions are helper infrastructure to 
//es: create a XMLHTTPRequest and register a listner callback function
function newXMLHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
    		// Try ActiveX
		try { 
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) { 
			// first method failed 
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				 // both methods failed 
			} 
		}
 	}
   	return xmlreq;
} 

//es: Getting status of handler. If status=200 - everthing if fine
function getReadyStateHandler(req, responseXmlHandler) {
	return function my_func() {
	if (req.readyState == 4) {
		if (req.status == 200) {
        		responseXmlHandler(req.responseXML);
		} else {
			var hellomsg = document.getElementById("hellomsg");
			hellomsg.innerHTML = "ERROR: "+ req.status;
      		}
		hellomgs = null;
    	}
 	}
 	req = null;
 	responseXmlHandler=null
}

//es: function for setting a cookie. parameters cookie_name, cookie_value, cookie_lifetime 
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
	exdate = null;
}
	
//es: function for gettting cookie by it's name
function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1 
	    c_end=document.cookie.indexOf(";",c_start)
	    if (c_end==-1) c_end=document.cookie.length
	    return unescape(document.cookie.substring(c_start,c_end))
	    } 
	  }
	return ""
}

//es: function for checking if speicifc cookie exsists, if it doesn't call setCookie function.
function checkCookie()  
{
	counter_cookie=getCookie(window.product) //cmc: pass in variable cookie name
	if (counter_cookie!=null && counter_cookie!="")
	{
	    return 1;
	}
	else 
    {
	  	setCookie(window.product, 1, 1);
	  	return 0;
	}
}

//es: function to test if cookie are enabled
//es: simple algorithm: set a cookie, check this cookie
function isCookieEnabled()
{
	setCookie('test_cookie', 1, 1);
	test_cookie=getCookie('test_cookie');
	if (test_cookie!=null && test_cookie!="")
	{
  		return 1;
	}
	else 
  	{
    	return 0;
  }
}


