
function talktoServer(URL){
	var req = newXMLHttpRequest();
	//register the callback handler function
  	var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser);
  	req.onreadystatechange = callbackHandler;
  	req.open("POST", "ajax/get_site_details.php", true);
  	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  	//get the value from the text input element and send it to server
  	var msg_value = URL;
  	req.send("msg="+msg_value);
 
}

// This is the callback functions that gets called
// for the response from the server with the XML data

var lastPing = 0;
function updateMsgOnBrowser(testXML) {

	var test = testXML.getElementsByTagName("getsite")[0];
	var description = testXML.getElementsByTagName("description")[0];
	var title = testXML.getElementsByTagName("title")[0];
    var url = testXML.getElementsByTagName("url")[0];
	
	var timestamp = test.getAttribute("timestamp");
	if (timestamp > lastPing) {
		lastPing = timestamp;

		var title_value = title.firstChild.nodeValue;
		var description_value = description.firstChild.nodeValue;
        var url_value = url.firstChild.nodeValue;
		
		document.exchange.selected_title.value=title_value;
		document.exchange.selected_URL.value=url_value;
		document.exchange.selected_desc.value=description_value;
	}
}


//the following two functions are helper infrastructure to 
//craete 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;
} 

function getReadyStateHandler(req, responseXmlHandler) {
	return function () {
	if (req.readyState == 4) {
		if (req.status == 200) {
        		responseXmlHandler(req.responseXML);
		} else {
			var hellomsg = document.getElementById("hellomsg");
			hellomsg.innerHTML = "ERROR: "+ req.status;
      		}
    	}
 	}
}



