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 js_SetC($Name,$Value,$EndH){	var exdate=new Date();	$EndH=exdate.getHours()+$EndH;	exdate.setHours($EndH);	document.cookie=$Name+ "=" +escape($Value)+(($EndH==null) ? "" : ";expires="+exdate.toGMTString()+"; path=/;");}

function js_GetC($Name){	if (document.cookie.length>0)  	{  		$Start=document.cookie.indexOf($Name + "=");  		if ($Start!=-1)    	{    		$Start=$Start + $Name.length+1;    		$End=document.cookie.indexOf(";",$Start);    		if ($End==-1) $End=document.cookie.length;    		return unescape(document.cookie.substring($Start,$End));  	   }  	}  	return "";} 

function shout() {
    var xmlHttp;
    xmlHttp = getXmlHttp();
    
	inIp = document.getElementById("inip").value;
    inName = document.getElementById("name").value;
    inMessage = document.getElementById("message").value;
    inSex = document.getElementById("m_sex").value;
	
	
	if (inName.length < 1)
    {
	  alert('Введите ник!');
      return false;
	}
    if (inSex=="")
    {
	  alert('Выберите пол!');
      return false;
	}
    if (inMessage=="Сообщение" || inMessage.length < 1 ||checkChars(inMessage))
    {
	  alert('Введите текст сообщения!');
      return false;
	}
	
	js_SetC('m_name',inName,8640);
	js_SetC('m_sex',inSex,8640); 
	
	document.getElementById("message").value = "";
	document.getElementById("message").focus();
	
	var httpParams = "name=" + inName + "&ip=" + inIp + "&m_sex=" + inSex +"&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', '/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 get_load(xmlDoc,id_tag){
res='';
	try{
	res=xmlDoc.getElementsByTagName(id_tag)[0].firstChild.nodeValue;
	}
	catch(e){}
	return res;
}
function shout_read() {
	$('b_reload').src='/engine/classes/shout/load.gif';
  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 = get_load(xmlDoc[i],"timestamp");
			var loadname = get_load(xmlDoc[i],"name");
			var loadmess = get_load(xmlDoc[i],"message");
			var loadcountry = get_load(xmlDoc[i],"country");
			var loadcity = get_load(xmlDoc[i],"city");
			var loadcountry_name = get_load(xmlDoc[i],"country_name");
			var loadregion = get_load(xmlDoc[i],"region");
			
			var loadisp = get_load(xmlDoc[i],"isp");
			var loadsex = get_load(xmlDoc[i],"sex");
			
	   if (loadsex == "zh") {loadsex_2="Пол: Женский";stylename = "nameusers";}
                            else {loadsex_2="Пол: Мужской";stylename = "nameguest";}
                loadmess = autoEmail(autoLinks(replaceSmileys(loadmess)));
            var new_el = document.createElement("div"); 
			new_el.innerHTML = '<img src=\'/templates/light_hi2/images/flags/'+loadcountry+'.gif\' width=16 height=10 alt=\''+loadcountry+'\' title="'+loadcountry_name+' '+loadcity+' '+loadregion+' '+loadisp+'" /><span class=\'topav\'>'+loadtime+' </span><span class=\''+stylename+'\' onclick = "tagSmiley(\''+loadname+':\');"  title="'+loadsex_2+'">'+loadname+'</span> '+loadmess; 
			new_el.className = cname; obj.appendChild(new_el);
      }
      if (!shouting)
	     // setTimeout(shout_read,5000);
	  else
	  	  shouting = false;
	}
  }
  
  xmlHttp.open('GET', '/shout.php', true);
  xmlHttp.send(null);  
  setTimeout(mmo_b,1000);
}

function mmo_b(){
$('b_reload').src='/engine/classes/shout/reload.png';
}
function replaceSmileys(message) {
  var sm = message.replace(/(:\)|:\-\))/g, "<img src='/engine/classes/shout/shoutsmileys/01.gif' width='17' height='17' alt=':)' title=':)' />").
  replace(/(:D)/g, "<img src='/engine/classes/shout/shoutsmileys/02.gif' width='17' height='17' alt=':D' title=':D' />").
  replace(/(\;\)|\;\-\))/g, "<img src='/engine/classes/shout/shoutsmileys/03.gif' width='17' height='17' alt=';)' title=';)' />").
  replace(/(:-P)/g, "<img src='/engine/classes/shout/shoutsmileys/04.gif' width='17' height='17' alt=':-P' title=':-P' />").
  replace(/(\(h\))/g, "<img src='/engine/classes/shout/shoutsmileys/05.gif' width='17' height='17' alt='(h)' title='(h)' />").
  replace(/(:o)/g, "<img src='/engine/classes/shout/shoutsmileys/06.gif' width='17' height='17' alt=':o' title=':o' />").
  replace(/(:\$)/g, "<img src='/engine/classes/shout/shoutsmileys/07.gif' width='17' height='17' alt=':$' title=':$' />").
  replace(/(:\|)/g, "<img src='/engine/classes/shout/shoutsmileys/08.gif' width='17' height='17' alt=':|' title=':|' />").
  replace(/(8-\))/g, "<img src='/engine/classes/shout/shoutsmileys/09.gif' width='17' height='17' alt='8-)' title='8-)' />").
  replace(/(\*-\))/g, "<img src='/engine/classes/shout/shoutsmileys/10.gif' width='17' height='17' alt='*-)' title='*-)' />").
  replace(/(:\^\))/g, "<img src='/engine/classes/shout/shoutsmileys/11.gif' width='17' height='17' alt=':^)' title=':^)' />").
  replace(/(\^o\))/g, "<img src='/engine/classes/shout/shoutsmileys/12.gif' width='17' height='17' alt='^o)' title='^o)' />").
  replace(/(:-#)/g, "<img src='/engine/classes/shout/shoutsmileys/13.gif' width='17' height='17' alt=':-#' title=':-#' />").
  replace(/(\*o\()/g, "<img src='/engine/classes/shout/shoutsmileys/14.gif' width='17' height='17' alt='*o(' title='*o(' />").
  replace(/(\|-\))/g, "<img src='/engine/classes/shout/shoutsmileys/15.gif' width='17' height='17' alt='|-)' title='|-)' />").
  replace(/(:S)/g, "<img src='/engine/classes/shout/shoutsmileys/16.gif' width='17' height='17' alt=':S' title=':S' />").
  replace(/(:@)/g, "<img src='/engine/classes/shout/shoutsmileys/17.gif' width='17' height='17' alt=':@' title=':@' />").
  replace(/(:\()/g, "<img src='/engine/classes/shout/shoutsmileys/18.gif' width='17' height='17' alt=':(' title=':(' />").   
  replace(/(QQ)/g, "<img src='/engine/classes/shout/shoutsmileys/19.gif' width='17' height='17' alt='QQ' title='QQ' />").
  replace(/(8o\|)/g, "<img src='/engine/classes/shout/shoutsmileys/20.gif' width='17' height='17' alt='8o|' title='8o|' />").
  replace(/(\(b\))/g, "<img src='/engine/classes/shout/shoutsmileys/21.gif' width='17' height='17' alt='(b)' title='(b)' />").
  replace(/(\(d\))/g, "<img src='/engine/classes/shout/shoutsmileys/22.gif' width='17' height='17' alt='(d)' title='(d)' />").
  replace(/(\(c\))/g, "<img src='/engine/classes/shout/shoutsmileys/23.gif' width='17' height='17' alt='(c)' title='(c)' />").
  replace(/(\(\^\))/g, "<img src='/engine/classes/shout/shoutsmileys/24.gif' width='17' height='17' alt='(^)' title='(^)' />").
  replace(/(\(ci\))/g, "<img src='/engine/classes/shout/shoutsmileys/25.gif' width='17' height='17' alt='(ci)' title='(ci)' />").
  replace(/(\(y\))/g, "<img src='/engine/classes/shout/shoutsmileys/26.gif' width='17' height='17' alt='(y)' title='(y)' />").
  replace(/(\(n\))/g, "<img src='/engine/classes/shout/shoutsmileys/27.gif' width='17' height='17' alt='(n)' title='(n)' />").
  replace(/(\(f\))/g, "<img src='/engine/classes/shout/shoutsmileys/28.gif' width='17' height='17' alt='(f)' title='(f)' />").
  replace(/(\(t\))/g, "<img src='/engine/classes/shout/shoutsmileys/29.gif' width='17' height='17' alt='(t)' title='(t)' />").  
  replace(/(\(\?\?\?\))/g, "<img src='/engine/classes/shout/shoutsmileys/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;} 
  
  // выделяем Email 
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;
} 
				