var	url = '../imu.php';
var	xmlHttpGetMessages = null;
var	cache = Array();
var	updateInterval = 3000;
var	imcWindow = null;
var	GlassWindow = null;
var	imcMsgWnd = null;
var	imcLoginWnd = null;
var	imcMsgInputWnd = null;
var	isIE = window.navigator.userAgent.indexOf("MSIE") > -1;

xmlHttpGetMessages = imcCreateXmlHttpRequestObject();
if(imcuser.length > 0)	setTimeout("imcRequestNewMessages()", updateInterval);
// function ///////////////////////////////////////////////////////////////////
function imcCreateXmlHttpRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																		"MSXML2.XMLHTTP.5.0",
																		"MSXML2.XMLHTTP.4.0",
																		"MSXML2.XMLHTTP.3.0",
																		"MSXML2.XMLHTTP",
																		"Microsoft.XMLHTTP");
    for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}

	if(!xmlHttp)
		alert("Error XMLHttpRequest");
	else
		return xmlHttp;	
}

function imcInit(name1,value1,name2,value2)
{
	if(xmlHttpGetMessages == null) xmlHttpGetMessages = imcCreateXmlHttpRequestObject();
	var params = 'lg=' + encodeURIComponent(lg) + '&id=login' +
								'&name=' + encodeURIComponent(value1) +
								'&email=' + encodeURIComponent(value2);
	cache.push(params);
	imcRequestNewMessages();
}

function imcRequestNewMessages()
{
	if(xmlHttpGetMessages)
	{
		try
		{
			if(xmlHttpGetMessages.readyState == 4 ||
					xmlHttpGetMessages.readyState == 0)
			{
				var params = '';
				if(cache.length > 0) params = cache.shift();
				else params = "id=getmsg&last=" + encodeURIComponent(lastmsg) +
				"&lg=" + encodeURIComponent(lg);
     		xmlHttpGetMessages.open("POST", url, true);
     		xmlHttpGetMessages.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttpGetMessages.onreadystatechange = imcHandleReceivingMessages;
   	  	xmlHttpGetMessages.send(params);
			}
			else
			{
				setTimeout("imcRequestNewMessages()", updateInterval);
			}
		}
		catch(e)
		{
			imcDisplayError(e.toString());
		}
	}
}

function imcHandleReceivingMessages()
{
	if(xmlHttpGetMessages.readyState == 4)
	{
		if(xmlHttpGetMessages.status == 200)
		{
			try
			{
				imcReadMessages();
			}
			catch(e)
			{
				imcDisplayError(e.toString());
			}
		}
		else
		{
			imcDisplayError(xmlHttpGetMessages.statusText);
		}
	}
}

function imcReadMessages()
{
	var response = xmlHttpGetMessages.responseText;
	if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0
			|| response.length == 0)
	throw(response.length == 0 ? "Void server response." : response);
	response = xmlHttpGetMessages.responseXML.documentElement;
	var st = response.getElementsByTagName("status")[0].firstChild.data;
	if(st == 'loginok') 
	{
		document.getElementById('imcmsgwnd').innerHTML = '';
		var admin_login = response.getElementsByTagName("status_admin")[0].firstChild.data;
		imcuser = decodeURIComponent(response.getElementsByTagName("imcuser")[0].firstChild.data);
		imcSetMsgWnd(admin_login);
	}
	if(st == 'getmsg')
	{
		var admin_login = response.getElementsByTagName("status_admin")[0].firstChild.data;
		var last = response.getElementsByTagName("last")[0].firstChild.data;
		var dt = response.getElementsByTagName("dt");
		var tm = response.getElementsByTagName("tm");
		var color = response.getElementsByTagName("color");
		var name = response.getElementsByTagName("name");
		var msg = response.getElementsByTagName("msg");
		imcDisplayMessages(last,dt,tm,color,name,msg);
		imcSetMsgInput(admin_login);
	}
	setTimeout("imcRequestNewMessages()", updateInterval);
}

function imcDisplayMessages(last,dt,tm,color,name,msg)
{
  for(var i = 0; i < dt.length; i++)
  {
    var dtm = dt.item(i).firstChild.data.toString();
    var tmm = tm.item(i).firstChild.data.toString();
    var namem = name.item(i).firstChild.data.toString();
    var colorm = color.item(i).firstChild.data.toString();
    var msgm = msg.item(i).firstChild.data.toString();
    var htmlMessage = "";
    htmlMessage += '<p><span style="font-size: 8px">'; 
    htmlMessage += dtm.toString();
    htmlMessage += " [";
    htmlMessage += tmm.toString();
    htmlMessage += "]</span> ";
    htmlMessage += ' <span style="color: ';
    htmlMessage += colorm.toString();
		htmlMessage += '">';
    htmlMessage += namem.toString();
    htmlMessage += ".</span> ";
    htmlMessage += msgm.toString();
    htmlMessage += "</p>";
	  var msgwnd = document.getElementById("imcmsgwnd");
 		var scrollDown = (msgwnd.scrollHeight - msgwnd.scrollTop <= 
    	                msgwnd.offsetHeight );
		msgwnd.innerHTML += htmlMessage;
	  msgwnd.scrollTop = scrollDown ? msgwnd.scrollHeight : msgwnd.scrollTop;
  }
	if(dt.length > 0) imcShowModalWindow(true);
}

function imcDisplayError(message)
{
	alert("Error: \n" + message);
}

function imcSetMsgWnd()
{
	if(imcuser.length > 0)
	{
		if(imcMsgWnd == null) imcMsgWnd = document.getElementById('imcsendmsgwnd');
		if(imcMsgInputWnd == null) imcMsgInputWnd = document.getElementById('imcmsginput');
		imcMsg = document.getElementById('imcmsg');
		var msg = imcMsg.value;
		imcMsgWnd.innerHTML = imcMsgInputWnd.innerHTML;
		imcMsg = document.getElementById('imcmsg');
		imcMsg.value = msg;
	}
}

function imcSetMsgInput(admin_login)
{
	if(imcMsgWnd == null) imcMsgWnd = document.getElementById('imcsendmsgwnd');
	if(imcMsgInputWnd == null) imcMsgInputWnd = document.getElementById('imcmsginput');
	if(admin_login == 'false')
		imcMsgWnd.innerHTML = warn_imcstatus;
	if(admin_login == 'true' && imcMsgWnd.innerHTML == warn_imcstatus)
		imcMsgWnd.innerHTML = imcMsgInputWnd.innerHTML;
}

function imcSendMessage()
{
	var imcmsg = document.getElementById('imcmsg');
	var params = "id=sendmsg&msg="+encodeURIComponent(imcmsg.value);
	cache.push(params);
	imcRequestNewMessages();
	imcmsg.value = '';
	var imcmsglen = document.getElementById('imcmsglen');
	var str = '<p>0/500</p>';
	imcmsglen.innerHTML = str;
	imcmsg.focus();
}

function imcChgMsg(inputValue,fieldID)
{
	if(fieldID)
	{
		var field = document.getElementById(fieldID);
		var len = inputValue.length;
		if(inputValue.length > 500)
		{
			field.value = inputValue.substring(0,500);
			len = 500;
		}
		var imcmsglen = document.getElementById('imcmsglen');
		var str = '<p>'+len+'/500</p>';
		imcmsglen.innerHTML = str;
	}
}

function imcVerifyIMCLogin(id1,id2,msg1,msg2,lang)
{
	var ID1 = document.getElementById(id1);
	var ID2 = document.getElementById(id2);
	if(ID1.value == "")
	{
		alert(msg1 + '?');
		ID1.focus();
		return false;
	}
	if(ID2.value == '' ||	ID2.value == "E-mail" || verify.validEmail(ID2.value) == 0)
	{
		alert(msg2 + '?');
		return false;
	}
	imcInit(id1,ID1.value,id2,ID2.value,lang);
}

function imcShowModalWindow(show)
{
	imcShowGlassWindow(show);
	if(imcWindow == null) imcWindow = document.getElementById('imcwnd');
	if(show)
	{
		var c = imcGetClientCenter();
		imcWindow.style.left = (c[0]-255) + 'px';
		imcWindow.style.top  = (c[1]-200) + 'px';
		imcWindow.style.display = 'block';
		imcWindow.focus();
	}
	else
		imcWindow.style.display='none';
	if(imcuser.length == 0)
	{
		if(imcMsgWnd == null) imcMsgWnd = document.getElementById('imcsendmsgwnd');
		if(imcLoginWnd == null) imcLoginWnd = document.getElementById('imclogin');
		imcMsgWnd.innerHTML = imcLoginWnd.innerHTML;
	}
	if(imcuser.length > 0) imcSetMsgWnd();
}

function imcShowGlassWindow(show)
{
	if(GlassWindow == null)
	{
		GlassWindow = document.createElement('DIV');
		with(GlassWindow.style)
		{
			display = 'none';
			position = 'absolute';
			height = 0;	
			width = 0;	
			zIndex = 1;
			if(isIE)
			{
				backgroundColor = '#000';
				filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=50, Style=0)";
			}
			else
				backgroundImage = 'url(../pic/afon.png)';
		}
		document.body.appendChild(GlassWindow);
	}
	if(show)
	{
		var s = imcGetDocumentSize();
		with(GlassWindow.style)
		{
			left = top = 0;
			width = s[0] + 'px';
			height = s[1] + 'px';
		}
	}
	GlassWindow.style.display = show ? 'block' : 'none';
}

function imcGetDocumentSize()
{
	return [document.body.scrollWidth > document.body.offsetWidth ? document.body.scrollWidth : document.body.offsetWidth,
					document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight];
}

function imcGetClientSize()
{
	if(document.compatMode == 'CSS1Compat'/* && !window.opera*/)
		return [document.documentElement.clientWidth, document.documentElement.clientHeight];
	else
		return [document.body.clientWidth, document.body.clientHeight];
}

function imcGetDocumentScroll()
{
	return [self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft),
 		self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)];
}

function imcGetClientCenter()
{
	var sizes = imcGetClientSize();
	var scrl = imcGetDocumentScroll();
	return [parseInt(sizes[0] / 2) + scrl[0], parseInt(sizes[1] / 2) + scrl[1]];
}

