var shouting = false;

function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
 
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function shout() {
    var xmlHttp;
    xmlHttp = getXmlHttp();
    
    inName = document.getElementById("name").value;
    inMessage = document.getElementById("message").value;
    
    if (inMessage=="Сообщение" || inMessage.length < 1 ||checkChars(inMessage))
    {
	  alert('Введите текст сообщения!');
      return false;
	}
	
	
	document.getElementById("message").value = "";
	document.getElementById("message").focus();
	
	var httpParams = "name=" + inName + "&message=" + inMessage;
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4)
        {
            var xmlDoc = xmlHttp.responseXML.documentElement.getElementsByTagName("status");
            if (xmlDoc[0].firstChild.nodeValue == 200) 
            {
			  shouting = true;
			  shout_read();
			}
        }
    };
    
    xmlHttp.open('POST', shousurl+'shout.php', true);
 	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;');
 	xmlHttp.setRequestHeader('Content-length', httpParams.length);
 	xmlHttp.setRequestHeader('Connection','close');
    xmlHttp.send(httpParams);
}

function shout_read() {
  var xmlHttp;
  xmlHttp = getXmlHttp();

  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4)
    {
	  obj = document.getElementById("shoutbox");
	  obj.innerHTML = "";	  	
	  var xmlDoc = xmlHttp.responseXML.documentElement.getElementsByTagName("entry");
	  var odd = true; var cname = "odd";
	  for (i = 0; i <= xmlDoc.length-1 ; i++)
	  {
	    if (odd) { cname = "odd"; odd = false; }
	        else { cname = "even"; odd = true; }
			var loadtime = xmlDoc[i].getElementsByTagName("timestamp")[0].firstChild.nodeValue;
			var loadname = xmlDoc[i].getElementsByTagName("name")[0].firstChild.nodeValue;
			var loadmess = xmlDoc[i].getElementsByTagName("message")[0].firstChild.nodeValue;
	    if (loadname == "Guest") {stylename = "nameguest";}
                            else {stylename = "nameusers";}
                loadmess = replaceSmileys(filterBW(autoLinks(autoEmail(loadmess))));
            var new_el = document.createElement("div"); 
			new_el.innerHTML = '<span class=\'topav\'>'+loadtime+' </span><span class=\''+stylename+'\' onclick = "tagSmiley(\''+loadname+':\');">'+loadname+':</span> '+loadmess; 
			new_el.className = cname; obj.appendChild(new_el);
      }
      if (!shouting)
	      setTimeout(shout_read,5000);
	  else
	  	  shouting = false;
	}
  }
  
  xmlHttp.open('GET', shousurl+'shout.php', true);
  xmlHttp.send(null);
}

function replaceSmileys(message) {
  var smps = shousurl+'engine/classes/shout/shoutsmileys/';
  var sm = message.replace(/(:\)|:\-\))/g, "<img src="+smps+"01.gif' width='17' height='17' alt=':)' title=':)' />").
  replace(/(:D)/g, "<img src='"+smps+"02.gif' width='17' height='17' alt=':D' title=':D' />").
  replace(/(\;\)|\;\-\))/g, "<img src='"+smps+"03.gif' width='17' height='17' alt=';)' title=';)' />").
  replace(/(:-P)/g, "<img src='"+smps+"04.gif' width='17' height='17' alt=':-P' title=':-P' />").
  replace(/(\(h\))/g, "<img src='"+smps+"05.gif' width='17' height='17' alt='(h)' title='(h)' />").
  replace(/(:o)/g, "<img src='"+smps+"06.gif' width='17' height='17' alt=':o' title=':o' />").
  replace(/(:\$)/g, "<img src='"+smps+"07.gif' width='17' height='17' alt=':$' title=':$' />").
  replace(/(:\|)/g, "<img src='"+smps+"08.gif' width='17' height='17' alt=':|' title=':|' />").
  replace(/(8-\))/g, "<img src='"+smps+"09.gif' width='17' height='17' alt='8-)' title='8-)' />").
  replace(/(\*-\))/g, "<img src='"+smps+"10.gif' width='17' height='17' alt='*-)' title='*-)' />").
  replace(/(:\^\))/g, "<img src='"+smps+"11.gif' width='17' height='17' alt=':^)' title=':^)' />").
  replace(/(\^o\))/g, "<img src='"+smps+"12.gif' width='17' height='17' alt='^o)' title='^o)' />").
  replace(/(:-#)/g, "<img src='"+smps+"13.gif' width='17' height='17' alt=':-#' title=':-#' />").
  replace(/(\*o\()/g, "<img src='"+smps+"14.gif' width='17' height='17' alt='*o(' title='*o(' />").
  replace(/(\|-\))/g, "<img src='"+smps+"15.gif' width='17' height='17' alt='|-)' title='|-)' />").
  replace(/(:S)/g, "<img src='"+smps+"16.gif' width='17' height='17' alt=':S' title=':S' />").
  replace(/(:@)/g, "<img src='"+smps+"17.gif' width='17' height='17' alt=':@' title=':@' />").
  replace(/(:\()/g, "<img src='"+smps+"18.gif' width='17' height='17' alt=':(' title=':(' />").   
  replace(/(QQ)/g, "<img src='"+smps+"19.gif' width='17' height='17' alt='QQ' title='QQ' />").
  replace(/(8o\|)/g, "<img src='"+smps+"20.gif' width='17' height='17' alt='8o|' title='8o|' />").
  replace(/(\(b\))/g, "<img src='"+smps+"21.gif' width='17' height='17' alt='(b)' title='(b)' />").
  replace(/(\(d\))/g, "<img src='"+smps+"22.gif' width='17' height='17' alt='(d)' title='(d)' />").
  replace(/(\(c\))/g, "<img src='"+smps+"23.gif' width='17' height='17' alt='(c)' title='(c)' />").
  replace(/(\(\^\))/g, "<img src='"+smps+"24.gif' width='17' height='17' alt='(^)' title='(^)' />").
  replace(/(\(ci\))/g, "<img src='"+smps+"25.gif' width='17' height='17' alt='(ci)' title='(ci)' />").
  replace(/(\(y\))/g, "<img src='"+smps+"26.gif' width='17' height='17' alt='(y)' title='(y)' />").
  replace(/(\(n\))/g, "<img src='"+smps+"27.gif' width='17' height='17' alt='(n)' title='(n)' />").
  replace(/(\(f\))/g, "<img src='"+smps+"28.gif' width='17' height='17' alt='(f)' title='(f)' />").
  replace(/(\(t\))/g, "<img src='"+smps+"29.gif' width='17' height='17' alt='(t)' title='(t)' />").
  replace(/\[b\](.+?)\[\/b\]/g, "<b>$1</b>").
  replace(/\[i\](.+?)\[\/i\]/g, "<i>$1</i>").
  replace(/\[s\](.+?)\[\/s\]/g, "<s>$1</s>").
  replace(/\[u\](.+?)\[\/u\]/g, "<u>$1</u>").
  replace(/\[color=(.+?)\](.*?)\[\/color\]/g, "<span style='color:#$1'>$2</span>").   
  replace(/(\(\?\?\?\))/g, "<img src='"+smps+"30.gif' width='17' height='17' alt='(???)' title='(???)' />")  
  ; 
  return sm;}
  
 // выделяем URL
function autoLinks(msg) {
  var text = msg.replace(/((?:ht|f)tps?:\/\/([^\s,]*))/gi,
    "<a class=\"shouturl\" href='$1' target='_blank' title='откроется в новом окне'>&laquo;url&raquo;</a>"); 
  return text;} 
  
  // выделяем URL 
function autoEmail(msg) {
  var text = msg.replace(/[\.\-_A-Za-z0-9]+?@[\.\-A-Za-z0-9]+?[\.A-Za-z0-9]{2,}/gi,
    "<a class=\"shoutmail\" href='mailto:$1' title='адрес електронной почты'>&laquo;mail&raquo;</a>"); 
  return text;} 
  
// Add a smiley tag to a message
function tagSmiley(tag) {
  
  var chat_message = document.getElementById('message');
  
  if (chat_message.value == "Сообщение")
  {
  chat_message.value = '';
  }
  
  var cache = chat_message.value;
  this.tag = tag;
  chat_message.value = cache + tag;

}
// список запрещенных знаков
var characters = ['<','¤','>','&lt;','&gt;'];
function checkChars(msg) { 
  var error = false; 
  for (var i=0; i < characters.length; i++) {  
         if (msg.indexOf(characters[i])!= -1){ error = true; }  
  }  
  return error;
} 
var badwords = ['fuck', 'fucking', 'fuckoff', 'ass', 'suck', 'shit',
                'asshole', 'sucks'];
// Replace bad words with symbols
function filterBW(message) {
  
  for (var i=0; i < badwords.length; i++) {
  var pattern=new RegExp("\\b("+badwords.join("|")+"){1,}\\b",'gi');
  var replacement = "*!*?*";
  text = message.replace(pattern,replacement);
  }
  
  return text;

}				