/* URL to the PHP page called for receiving suggestions for a keyword*/
var getFunctionsUrl = "/scripts/ajaxcomment.php?comment=";

// when set to true, display detailed error messages
var debugMode = false;
/* the XMLHttp object for communicating with the server */
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try {
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try {
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function send_message(comment) {
	
	document.getElementById('comment_message').innerHTML = '<span style="background-color:#cc0000;color:#ffffff;">sending comment...</span>'
	
	var message = document.getElementById('post_comment').value;
	var pname = document.getElementById('post_name').value;
	var webpage = document.getElementById('post_webpage').value;
	
	if (document.getElementById('post_identity1').checked == true) {
		var identity = document.getElementById('post_identity1').value;
	} else {
		var identity = document.getElementById('post_identity2').value;
	}
	
	if(xmlHttp) {
		try {
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
				xmlHttp.open("GET", getFunctionsUrl + encode(comment) + '&message=' + encode(message) + '&name=' + encode(pname) + '&webpage=' + encode(webpage) + '&identity=' + identity, true);
				xmlHttp.onreadystatechange = handleSend;
				xmlHttp.send(null);
			}
		}
		catch(e) {
			displayError("Can't connect to server:\n" + e.toString());
			document.getElementById('comment_message').innerHTML = ''
		}
	}
}

/* function that displays an error message */
function displayError(message) {
	// display error message, with more technical details if debugMode is true
	alert("Error accessing the server! "+(debugMode ? "\n" + message : ""));
	document.getElementById('comment_message').innerHTML = ''
}

/* function that escapes a string */
function encode(uri) {
	if (encodeURIComponent) {
		return encodeURIComponent(uri);
	}
	if (escape) {
		return escape(uri);
	}
}

function handleSend() {
	//if the process is completed, decide what to do with the returned data
	if (xmlHttp.readyState == 4) {
		// only if HTTP status is "OK"
		if (xmlHttp.status == 200) {
			try {
				// process the server's response
				var response = xmlHttp.responseText;
				var message = document.getElementById('comment_message');
				message.innerHTML = response;
				if(response == '<span style="background-color:#00cc00;color:#ffffff;">Your comment has been saved and will be visible after blog owner approval.</span>') {
					document.getElementById('post_comment').value = '';
					document.getElementById('post_name').value = '';
					document.getElementById('post_webpage').value = 'http://';
				}
			}
			catch(e) {
				// display the error message
				displayError(e.toString());
			}
		} else {
			displayError("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}
