function startChat(id,cid) {
 if (!window.$csid) {
  if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange=function() {
   if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    $csid=xmlhttp.responseText;
    if (chatting.style.display=="none") {
     chatting.style.display = "block";
     setInterval( "showChat($csid)", 1000 );
     $(newchat).focus();
   }
  }
 }
 xmlhttp.open("GET","includes/chat.php?startchat=1&id="+id+"&cid=" + cid + "&t=" + new Date().getTime(),true);
 xmlhttp.send();
 }
}

function submitChat(csid,cid1) {
 $newchat=newchat.value.replace(/[&]/g,"%26");
 if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
   document.getElementById("chatbox").innerHTML=xmlhttp.responseText;
   document.getElementById("chatbox").scrollTop = document.getElementById("chatbox").scrollHeight;
   $(newchat).focus();
  }
 }
 xmlhttp.open("GET","includes/chat.php?submit="+$newchat+"&showchat="+$csid+"&id="+cid1+"&t=" + new Date().getTime(),true);
 xmlhttp.send();
 newchat.value="";
}

function showChat($csid) {
 if (chatting.style.display=="none") { chatting.style.display = "block"; }
 if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
   if (document.getElementById("chatbox").innerHTML.split(":").length != xmlhttp.responseText.split(":").length) {
    document.getElementById("chatbox").innerHTML=xmlhttp.responseText;
    document.getElementById("chatbox").scrollTop = document.getElementById("chatbox").scrollHeight;
   }
  }
 }
 xmlhttp.open("GET","includes/chat.php?showchat="+$csid+"&t=" + new Date().getTime(),true); xmlhttp.send();
}

function checkChat($chr) {
 if (!window.$csid) {
  if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange=function() {
   if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    $check=xmlhttp.responseText;
    if ($check>0) {
     $csid=$check;
     setInterval( "showChat($csid)", 3000 );
     showChat($csid);
     clearInterval(checkActive);
     $(newchat).focus();
    }
   }
  }
  xmlhttp.open("GET","includes/chat.php?check="+$chr+"&t=" + new Date().getTime(),true);
  xmlhttp.send();
  ++$chatcount; if ($chatcount>200) clearInterval(checkActive);
 }
}

