<!--<script type="text/javascript">-->

chatsiteid = "";
chatuserid = "";  

function chatbox_post(url, div) {
  try {
    var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
  } catch (e) { 
    // browser doesn't support ajax. handle however you want 
  }
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4)
    {
      if(xmlhttp.responseText=="") {
        
        document.getElementById(div).style.display="none"; 
      } else {
        //alert(xmlhttp.responseText);
        document.getElementById(div).style.display="";
      }
      document.getElementById(div).innerHTML = xmlhttp.responseText; 
    }
  }
  xmlhttp.open("GET", url, true);
  xmlhttp.send(null);
}

function chatbox_submit(siteid, userid) {
  var div = "chatboxpost";
  var url = "http://" + window.location.hostname + "/livechat/chat.post.php";
  var form = document.getElementById("chatboxform");
  var name = form.elements["name"].value;
  var message = form.elements["message"].value;
  form.elements["message"].value = "";
  name = escape(name);
  message = escape(message); 
  args = "?siteid="+siteid+"&userid="+userid+"&name="+name+"&message="+message; //alert(url+args);
  chatbox_post(url+args, div);
  set_user_status(siteid, userid,'chatting');
  //playSound("doorbell2");
  return false;
}

function chat_users(siteid, userid, title, status) {
  var title = (title==null) ? "" : title;
  var status= (status==null) ? "" : status;
  var div = "chatusers";
  var url = "http://" + window.location.hostname + "/livechat/chat.users.php?siteid="+siteid+"&userid="+userid+"&title="+title+"&status="+status; //alert(url);
  chatbox_post(url, div);
  return false;
}

function chatbox_list(siteid, userid) {
  var div = "chatboxlist";
  var url = "http://" + window.location.hostname + "/livechat/messages.list.php?siteid="+siteid+"&userid="+userid; //alert(url);
  chatbox_post(url, div);
  return false;
}

var chatwin;
function chatbox_popup(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/chatbox.php?siteid="+siteid+"&userid="+userid;
  winid = siteid+userid;
  var width = "400";
  var height = "500";
  var scrollbar = "true";
  var resizable = "";
	var href;
	if (typeof(url) == 'string')
	   href=url;
	else
	   href=url.href;
  chatwin = window.open(href, winid, "width="+width+",height="+height+",scrollbars="+scrollbar+",resizable="+resizable);
  if (!chatwin.focus) return true;
	return false;
}

function chat_last_message() {
  var url = "http://" + window.location.hostname + "/livechat/lastmessage.get.php";
  
  var div = "lastmessage";
  chatbox_post(url, div);
  return false;
}

function ignore_last_message(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/ignoremessage.php?siteid="+siteid+"&userid="+userid;
  //alert(url);
  var div = "livemessagelink";
  chatbox_post(url, div);
  return false;
}

function menu_page(page) {
  var url = "http://" + window.location.hostname + "/"+page;
  var div = "content";
  chatbox_post(url, div);
  return false;
}

function chat_owners_link(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/live.owners.link.php?siteid="+siteid+"&userid="+userid;
  var div = "liveownerlink";
  //alert(url);
  chatbox_post(url, div);
}

function chat_admins_link(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/live.admins.link.php?siteid="+siteid+"&userid="+userid;
  var div = "liveadminlink";
  //alert(url);
  chatbox_post(url, div);
}

function chat_message_link(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/live.message.link.php?siteid="+siteid+"&userid="+userid;
  var div = "livemessagelink";
  //alert(url);
  chatbox_post(url, div);
}

function get_user_status(siteid, userid) {
  var url = "http://" + window.location.hostname + "/livechat/chat_get_user_status.php?siteid="+siteid+"&userid="+userid;
  var div = "chatboxuserstatus";
  //alert(url);
  chatbox_post(url, div);
}

function set_user_status(siteid, userid, status) {
  var status = (status==null) ? "" : status;
  var url = "http://" + window.location.hostname + "/livechat/chat_set_user_status.php?siteid="+siteid+"&userid="+userid+"&status="+status;
  var div = "";
  //alert(url);
  chatbox_post(url, div);
}

function playSound(file) {
    var div = document.getElementById('livechatsound');
    //div.innerHTML = '<embed id="'+id+'" src="'+file+'" autostart="1" loop="0" height="0" width="0" type="'+getMimeType()+'"></embed>';
    div.innerHTML = '<embed id="'+file+'" src="http://coopvr.cooprentals.com/livechat/'+file+'.wav" autostart="1" loop="0" height="0" width="0" type="'+getMimeType()+'"></embed>';
           
} 	

function embedSounds() {
  mime = getMimeType();
  document.write('<object id="music" data="livechat/open.wav" type="application/x-mplayer2" style="heigth:0px; width:0px"></object>');

  //document.write('<embed id="open"  src="livechat/open.wav"  autostart="1" loop="0" height="0" width="0" type="'+getMimeType()+'></embed>');
  //document.write('<embed id="send"  src="livechat/send.wav"  autostart="0" loop="0" height="0" width="0" type="'+getMimeType()+'></embed>');
  //document.write('<embed id="alert" src="livechat/alert.wav" autostart="0" loop="0" height="0" width="0" type="'+getMimeType()+'></embed>');
}

function getMimeType(){
  var mimeType = "application/x-mplayer2"; //default
  var agt=navigator.userAgent.toLowerCase();
  if (navigator.mimeTypes && agt.indexOf("windows")==-1) {
  //non-IE, no-Windows
    var plugin=navigator.mimeTypes["audio/mpeg"].enabledPlugin;
    if (plugin) mimeType="audio/mpeg" //Mac/Safari & Linux/FFox
  }//end no-Windows
return mimeType
}//end function getMimeType



var soundplayed = false;
var lastchatmsg = "";

//LOAD FUNCTIONS
window.onload = new function () {
  setInterval(function() {
    chat_admins_link(chatsiteid,chatuserid);
    chat_owners_link(chatsiteid,chatuserid);
    chat_message_link(chatsiteid,chatuserid);
        
    msg = document.getElementById("livemessagelink");
    if(lastchatmsg!=msg.innerHTML && msg.innerHTML!="") {
      playSound("doorbell");
    }
    lastchatmsg = msg.innerHTML;
  }, 3000);               
}