var server = "support/server.php";

var req;

function chatEntryNewRequest()
{
	req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		} 
	} else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				req = false;
			}
		}
	}
}

function chatEntryCheckAndShow()
{
	setTimeout('chatEntryCheckAndShow()', 60000);
	chatEntryNewRequest();
	req.onreadystatechange = chatEntryPollStateChange;
	req.open("POST", server, true);
	req.setRequestHeader('Content-Type', 
		'application/x-www-form-urlencoded');
	req.send("");
}

function chatEntryOpenChat()
{
//    window.open('support/support.html','Hi','toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,width=700,height=600');
    window.location = 'support/chatnow.php';
    return;
}

function chatEntryPollStateChange()
{
	if (req.readyState == 4) {
		try {
			if (req.status == 200) {
				var xml = req.responseXML;
				if (xml) {
					var is_ad = xml.getElementsByTagName("ad")[0].attributes.getNamedItem("isad").value;
					var el = document.getElementById('chatinvite');
					if (is_ad) {
							document.getElementById("chatinvite").innerHTML = "<span class=\"heading\">Live Support Available</span><p class=\"genraltext\" style=\"color: black;\">Click <a class=\"bluelink\" href=\"javascript:chatEntryOpenChat();\">here</a> to open a chat window.</p>";
					}
					else {
							document.getElementById("chatinvite").innerHTML = "<span class=\"heading\">Live Support Unavailable</span><p class=\"genraltext\" style=\"color: black;\">If support is logged in, that will be indicated here, with a link to chat window. <br /> This information automatically updates at one-minute intervals.</p>";
					}
					return;
				} else {
					alert(req.responseText);
				}
			}
		} catch (e) { }
		req = null;
	}
}
