// If server.php is not in the same folder with this page,
// make sure you fix this URL! For instance: /supportmelive/server.php

var server = "/supportmelive/server.php";

var req;
var lang;

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 chatEntryOpenChat(lang)
{
    window.open('/supportmelive/index.php?lang='+lang,'SupportMeLive','toolbar=yes,status=yes,scrollbars=yes,location=yes,menubar=yes,directories=yes,width='+window.screen.width+',height='+window.screen.height);
    return;
}

function chatEntryCheckAndShow(language)
{
	if(language)
		lang = language;
	setTimeout('chatEntryCheckAndShow()', 60000);
	chatEntryNewRequest();

	req.onreadystatechange = chatEntryPollStateChange;
	req.open("POST", server, true);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	req.send("lang="+lang);
}

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 = "<h3>Live Support Available</h3><p>Technical support staff are currently logged in.</p> Click <a href=\"javascript:chatEntryOpenChat(\'"+lang+"\');\">here</a> to open a chat window.</p>";
							//$('chatinvite').setAttribute("onclick","javascript:chatEntryOpenChat('"+lang+"');");
							// $('chatinvite').attachEvent("onclick",chatEntryOpenChat(lang));
							$("chatinvite").innerHTML="<a href='/supportmelive/index.php?lang="+lang+"'><img src='/images/bt_online.jpg' /></a>";
					}
					else {
							//document.getElementById("chatinvite").innerHTML = "<h3>Live Support Unavailable</h3><p>Technical support staff are not currently logged in.</p>";
							$("chatinvite").innerHTML="<img src='/images/bt_offline.jpg' />";
					}
					return;
				} else {
					alert(req.responseText);
				}
			}
		} catch (e) { }
		req = null;
	}
}
