// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw "For XML Elements Only";}
   }
}
//var chat_window='';
function ajax_start(){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return http_request;
}
function check_admin_online(){
	online_count=ajax_start();
	online_count.onreadystatechange = onchange_check_admin_online;
	if (!online_count) {
		//alert('Your Browser Not Support');
		return false;
	}
	get_url="./chat.php?action=check_admin_xml";
	// check chat window is opened
	if ( (typeof(chat_window)) == 'object' ){
		if ( typeof(chat_window.name) == 'string' ) {
			get_url+="&chating=1";
		}
	}
	online_count.open('GET', get_url, true);
	online_count.send(null);
}
function onchange_check_admin_online(){
	if (online_count.readyState == 4) {
		if (online_count.status == 200) {
			var xmldoc = online_count.responseXML;
			var xItems = xmldoc.documentElement.selectNodes("//Msg/admin_count/text()");
			admin_count= xItems[0].nodeValue;
			var xItems = xmldoc.documentElement.selectNodes("//Msg/new_message_count/text()");
			new_message_count= xItems[0].nodeValue;
			var xItems = xmldoc.documentElement.selectNodes("//Msg/new_message_last_date/text()");
			new_message_last_date= xItems[0].nodeValue;
			var xItems = xmldoc.documentElement.selectNodes("//Msg/new_message_admin_id/text()");
			new_message_admin_id= xItems[0].nodeValue;
			

			if ( typeof(document.getElementById('admin_status'))== 'object'){
				if ( admin_count > 0 ){
					// display online icon
					document.getElementById('admin_status').innerHTML="<a href='javascript://' onclick='open_chat(0)'><img src='"+online_icon+"' border='0'></a>";
				} else {
					// display offline icon
					document.getElementById('admin_status').innerHTML="<img src='"+offline_icon+"'>";
				}
			}
			if ( typeof(chat_window) != 'object' ){
				if ( admin_count > 0 ){
					if ( new_message_count > 0 && confirm(message_alert+new_message_last_date) == true ){
						//popup here.
						open_chat(new_message_admin_id);
					}
				}
			} else {
				if ( typeof(chat_window.name) != 'string' ){
					if ( admin_count > 0 ){
						if ( new_message_count > 0 && confirm(message_alert+new_message_last_date) == true ){
							//popup here.
							open_chat(new_message_admin_id);
						}
					}
				}
			}
		} else {
			//alert('There was a problem with the request.');
		}
	}
}
function auto_run(){
	check_admin_online();
	setTimeout('auto_run()',5000);
}
function open_chat(a){
	openurl='./chat.php';
	if ( a != '' && a != '0' ){
		openurl+='?admin_id='+a
	}
	chat_window=window.open (openurl, chat_window_name, 'height=310, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
}
document.onload=setTimeout('auto_run()',1000);
