/***************************\
*         cChatBox         *
*    Javascript Engine     *
*      by ChriStrato       *
*  www.vbulletin-italia.it *
****************************/


function cChatBox_Engine(intervalchat,intervalchatul,ordermessage,userlistactive)
{

this.form = document.forms["formcchatbox"];
this.intervalchat = intervalchat;
this.intervalchatul = intervalchatul; 
this.userlistactive = userlistactive;
this.cchatboxshow_obj = fetch_object("cchatboxshow");
this.cchatboxuserlist_obj = fetch_object("cchatboxuserlist");
this.ordermessage = ordermessage;
this.listmessage = new Object;
this.postmess = new Object;
this.editmessage = new Object;
this.userlist = new Object;
this.pmmessage = new Object; 
this.postmessage = false;

this.init = function()
{
this.showmessage();
this.chat = setInterval("cChatBox.showmessage();", this.intervalchat * 1000);

if(this.userlistactive)
{
this.showuserlist();
this.chatuserlist = setInterval("cChatBox.showuserlist();", this.intervalchatul * 1000);
} 
} 


this.clearmessage = function()
{
this.form.message.value = ''
return true;
}


this.showmessage = function()
{

if(this.postmessage)
{
return false;
} 

this.listmessage.ajax = new vB_AJAX_Handler(true);
this.listmessage.ajax.onreadystatechange(this.showmessage_change);
this.listmessage.ajax.send('cchatbox.php?do=listmessage');

}

this.showmessage_change = function()
{

smajax = cChatBox.listmessage.ajax;
if (smajax.handler.readyState == 4 && smajax.handler.status == 200) 
{ 
cChatBox.cchatboxshow_obj.innerHTML = '<table cellpadding="1" cellspacing="3" border="0" width="95%" align="left">' + smajax.handler.responseText + '</table>'; 
if(cChatBox.ordermessage == 0)
{
document.getElementById('cchatboxshow').scrollTop = 999999;
}
}

}


this.sendmessage = function() 
{

chatmessage = this.form.message.value;
	
if (this.postmessage)
	{
		alert('الرسالة تحت المعالجة..');
		return false;
	}

if (chatmessage.replace(/ /g, '') == '')
{
alert('يجب ان تدخل رسالة!');
return false;
}

this.postmess.ajax = new vB_AJAX_Handler(true);
this.postmess.ajax.onreadystatechange(this.messageposted);
this.postmess.ajax.send('cchatbox.php', 'do=messagepd&message=' + PHP.urlencode(chatmessage) + this.ccbinput())


this.clearmessage();
this.postmessage = true;
return false;


}

this.messageposted = function()
{
mpajax = cChatBox.postmess.ajax;

	if (mpajax.handler.readyState == 4 && mpajax.handler.status == 200)
	{
	      cChatBox.postmessage = false;
            cChatBox.showmessage();
	}
}


this.save_editmessage = function() {

Message = this.form.editmess.value;
MessageID = this.form.editidmess.value;

this.editmessage.ajax = new vB_AJAX_Handler(true)
this.editmessage.ajax.onreadystatechange(this.messageedited)
this.editmessage.ajax.send('cchatbox.php', 'do=edit&message=' + PHP.urlencode(Message) + '&messageid=' + MessageID)

}

this.delete_editmessage = function() {

MessageID = this.form.editidmess.value;

this.editmessage.ajax = new vB_AJAX_Handler(true)
this.editmessage.ajax.onreadystatechange(this.messageedited)
this.editmessage.ajax.send('cchatbox.php', 'do=delete&messageid=' + MessageID)

}

this.messageedited = function()
{

meajax = cChatBox.editmessage.ajax;

	if (meajax.handler.readyState == 4 && meajax.handler.status == 200)
	{
		toggle_collapse('editmess');
            cChatBox.showmessage();

	}
}


this.showeditmessage = function(mess,id) {

this.form.editmess.value = mess;
this.form.editidmess.value = id;

toggle_collapse('editmess');

return true;
}

this.ccbinput = function()
{
	string = '';
	input = this.form.getElementsByTagName('input');
	for (i = 0; i < input.length; i++)
	{
		if (input[i].type == 'hidden' && input[i].value != '')
		{
			string += '&' + input[i].name + '=' + PHP.urlencode(input[i].value);
		}
	}

	return string;
}

this.cc_changebutton = function(button, prop)
{
      if(button.style.borderColor != '')
      {
      button.style.borderColor = '';
      valuebutton = ''; 
      } else {
      button.style.borderColor = 'highlight';
      
      switch (prop)
	{
		case 'fontWeight':
		valuebutton = 'bold';
		break;

		case 'textDecoration':
		valuebutton = 'underline';
		break;

		case 'fontStyle':
		valuebutton = 'italic';
		break;
	}
      }
      
eval('this.form.message.style.' + prop + ' = "' + valuebutton + '"');
eval('this.form.' + prop + '.value = "' + valuebutton + '"');

return false;
}

this.cc_changecolor = function(textcolor)
{
eval('this.form.message.style.color = "' + textcolor + '"');
eval('this.form.color.value = "' + textcolor + '"');
return false;
}

this.cc_changefonts = function(font)
{
eval('this.form.message.style.fontFamily = "' + font + '"');
eval('this.form.fontFamily.value = "' + font + '"');
return false;
}

this.send_smilie = function(smilietext)
{
this.form.message.value += ' ' + smilietext;
return false;
}

this.cc_removeformat = function()
{

document.images["text_bold"].style.borderColor = '';
document.images["text_italic"].style.borderColor = '';
document.images["text_underline"].style.borderColor = '';
this.form.message.style.fontWeight = '';
this.form.message.style.textDecoration = '';
this.form.message.style.fontStyle = '';
this.form.message.style.color = '';
this.form.message.style.fontFamily = '';
this.form.fontWeight.value = '';
this.form.textDecoration.value = '';
this.form.fontStyle.value = '';
this.form.color.value = '';
this.form.fontFamily.value = '';

}


this.showuserlist = function()
{

this.userlist.ajax = new vB_AJAX_Handler(true);
this.userlist.ajax.onreadystatechange(this.statechangedul);
this.userlist.ajax.send('cchatbox.php?do=userlist');

}


this.statechangedul = function()
{

ulajax = cChatBox.userlist.ajax;

if (ulajax.handler.readyState == 4 && ulajax.handler.status == 200 && ulajax.handler.responseText) 
{
cChatBox.cchatboxuserlist_obj.innerHTML = '<table cellpadding="1" cellspacing="3" border="0" width="95%" align="left">' + ulajax.handler.responseText + '</table>' 

scripts = cChatBox.cchatboxuserlist_obj.getElementsByTagName("script");         
for(var i=0;i<scripts.length;i++){
eval(scripts[i].innerHTML);
} 
}
   
}

this.sendfriendreq = function(userid) 
{

this.pmmessage.ajax = new vB_AJAX_Handler(true);
this.pmmessage.ajax.onreadystatechange(this.sendpmstatechange);
this.pmmessage.ajax.send('cchatbox.php','do=sendfriendreq' + '&userid=' + userid);

return true;
}

this.sendpmpopup = function(userid) 
{

defaultvaluepm = '';
phrasepm = 'أدخل لارسال رساله الى :';
forceltr = true; 

if (is_ie7)
{

pmmessage = window.showModalDialog("clientscript/ieprompt.html?", { value: defaultvaluepm, label: phrasepm, dir: document.dir, title: document.title, forceltr: (typeof(forceltr) != "undefined" ? forceltr : false) }, "dialogWidth:320px; dialogHeight:150px; dialogTop:" + (parseInt(window.screenTop) + parseInt(window.event.clientY) + parseInt(document.body.scrollTop) - 100) + "px; dialogLeft:" + (parseInt(window.screenLeft) + parseInt(window.event.clientX) + parseInt(document.body.scrollLeft) - 160) + "px; resizable: No;");

} else {

pmmessage = prompt(phrasepm, defaultvaluepm);

}


if(pmmessage != '')
{
this.pmmessage.ajax = new vB_AJAX_Handler(true);
this.pmmessage.ajax.onreadystatechange(this.sendpmstatechange);
this.pmmessage.ajax.send('cchatbox.php','do=sendpm&message=' + PHP.urlencode(pmmessage) + '&userid=' + userid);
}

return false;
}

this.sendpmstatechange = function()
{

pmajax = cChatBox.pmmessage.ajax;

	if (pmajax.handler.readyState == 4 && pmajax.handler.status == 200)
	{ 
            cChatBox.showmessage();
	}
}



this.cc_prompt = function(type)
{

switch (type)
{
		case 'url':
		phrase = 'الرجاء إدخال عنوان الموقع الخاص بك :';
            bbcode = 'URL';
		break;

		case 'img':
		phrase = 'الرجاء إدخال عنوان رابط الصوره :';
            bbcode = 'IMG';
		break;
}

defaultval = 'http://';
forceltr = true;

if (is_ie7)
{
text = window.showModalDialog("clientscript/ieprompt.html?", { value: defaultval, label: phrase, dir: document.dir, title: document.title, forceltr: (typeof(forceltr) != "undefined" ? forceltr : false) }, "dialogWidth:320px; dialogHeight:150px; dialogTop:" + (parseInt(window.screenTop) + parseInt(window.event.clientY) + parseInt(document.body.scrollTop) - 100) + "px; dialogLeft:" + (parseInt(window.screenLeft) + parseInt(window.event.clientX) + parseInt(document.body.scrollLeft) - 160) + "px; resizable: No;");
} else {
text = prompt(phrase, defaultval);
}

if (text!=null) 
{
this.form.message.value += '[' + bbcode + ']' + text + '[/' + bbcode + ']';
}

return false;

}

this.previewsmilie = function(imgsrc)
{

document.images["smiliepreview"].src = imgsrc;
return true;

}



}
