/*
COPYRIGHTS:

Copyright 2006 Adam S Docherty. All Rights Reserved.

www.themes.co.nz

This file is Copyright 2006 Adam S Docherty. All
rights reserved. This file may not, in whole or in any part, be
copied, reproduced or redistributed without prior consent, 
in writing, from Adam S Docherty.

You are granted a limited license to use this file. The file
may be used or copied only in accordance with the terms of that
license, which is described in the following paragraphs.

This file is intended as a library of functions for the JChat
Software Application and may only be used for this. You may
not copy or transfer these functions to use in other 3rd
party software.

THIS LICENSE AGREEMENT SHALL BE GOVERNED BY THE

LAWS OF THE UNITED STATES OF AMERICA, AND SHALL INURE TO 
THE BENEFIT OF Adam S Docherty OR ITS ASSIGNS.

DISCLAIMER / LIMITATION OF LIABILITY:
THE USER ACKNOWLEDGES THAT THE SOFTWARE MAY NOT BE
FREE FROM DEFECTS AND MAY NOT SATISFY ALL OF THE USER'S
NEEDS. THE SOFTWARE AND ANY ACCOMPANYING WRITTEN
MATERIALS ARE LICENSED "AS IS". IN NO EVENT WILL 
Adam S Docherty BE LIABLE FOR
DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGE
OR DAMAGES RESULTING FROM LOSS OF USE, OR LOSS OF
ANTICIPATED PROFITS RESULTING FROM ANY DEFECT IN THE
PROGRAM, EVEN IF IT HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. 
*/


var timerID = '448941';

function sendmessage(message) 
{	
	scrollbott("content");
	changeVisibility('jc_error', 1, false);
	changeVisibility('jc_loading', 1, 'inline');
	
	message = escape(message);
	
	if(!message){
		changeVisibility('jc_loading', 1, false);
		return false;
	}
	
	jc_insertusermsg(unescape(message));
	
	var xmlhttpGeneric = getHTTPObject();
	var modname = 'com_jchat';
	var func = 'sendchatdata';
	var targetDiv = 'content';
	var roomid    = $('jc_roomid').value;
	var recipient = $('jc_recipient').value;
	
	var url = "index.php?option="+modname;
	var param = "&func="+func+"&message="+message+"&roomid="+roomid+"&recipient="+recipient;
	
  	xmlhttpGeneric.open("post",url,true);
 	xmlhttpGeneric.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 	xmlhttpGeneric.send(param);
	
	xmlhttpGeneric.onreadystatechange=function() {
		
  		if (xmlhttpGeneric.readyState==4){	
		
			if (xmlhttpGeneric.status == '200'){
				
				//getdata0();
				
			} else {				
				jc_ajaxError(xmlhttpGeneric.status, url);
			}
			changeVisibility('jc_loading', 1, false);
  		}				
 	}	
	scrollbott("content");	
	
 	return false
}

function getdata0() 
{		
	scrollbott("content");
	changeVisibility('jc_error', 1, false);
	changeVisibility('jc_loading', 1, 'inline');
	
	var xmlhttpGeneric = getHTTPObject();
	var modname = 'com_jchat';
	var func = 'getdata';
	var targetDiv = 'content';
	var roomid    = $('jc_roomid').value;
	
	if($('jc_lastid')){
		var lastid    = $('jc_lastid').value;
	}
	
	var url = "index.php?option="+modname;
	var param = "&func="+func+"&roomid="+roomid+"&lastid="+lastid;
	
	xmlhttpGeneric.open("post",url,true);
 	xmlhttpGeneric.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 	xmlhttpGeneric.send(param);
	
	xmlhttpGeneric.onreadystatechange=function() {
		
  		if (xmlhttpGeneric.readyState==4){	
		
			if (xmlhttpGeneric.status == '200'){
				
				results = xmlhttpGeneric.responseText.split('^^');
				
				var privlevel 	= results[0];
				var userlist  	= results[1];
				var lastid    	= results[2];
				var messagehtml = results[3];
				var noaccess	= results[5];
				
				if(lastid){
					jc_setparam('jc_lastid', lastid);
				}
				
				var userlistdiv 		= document.getElementById('jc_userlist');
				
				if(userlist){
					userlistdiv.innerHTML 	= userlist;
				}
				
				if(messagehtml){
					var target = document.getElementById(targetDiv);		
					var content = document.createElement('span');		
					content.innerHTML = messagehtml;
					
					target.appendChild(content, target.lastChild);
					
					var x = target.getElementsByTagName("script"); 
				   for(var i=0;i<x.length;i++)
				   {
					   eval(x[i].text);
				   }
				}
			} else {				
				jc_ajaxError(xmlhttpGeneric.status);
			}
			
			if(lastid){
			changeVisibility('jc_loading', 1, false);
			}
			
  		}				
 	}
	scrollbott("content");
 	return false
}

function getdata(fromsend) 
{		
	
	//changeVisibility('jc_error', 1, false);
	//changeVisibility('jc_loading', 1, 'inline');
	scrollbott("content");
	var xmlhttpGeneric = getHTTPObject();
	var modname = 'com_jchat';
	var func = 'getdata';
	var targetDiv = 'content';
	var roomid    = $('jc_roomid').value;
	
	if($('jc_lastid')){
		var lastid    = $('jc_lastid').value;
	}
	
	var url = "index.php?option="+modname;
	var param = "&func="+func+"&roomid="+roomid+"&lastid="+lastid+"&browserid="+browserid;
	
	xmlhttpGeneric.open("post",url,true);
 	xmlhttpGeneric.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 	xmlhttpGeneric.send(param);
	
	xmlhttpGeneric.onreadystatechange=function() {
		
  		if (xmlhttpGeneric.readyState==4){	
		
			if (xmlhttpGeneric.status == '200'){
				
				results = xmlhttpGeneric.responseText.split('^^');
				
				var privlevel 	= results[0];
				var userlist  	= results[1];
				var lastid    	= results[2];
				var messagehtml = results[3];
				var noaccess	= results[5];
				
				if(results[5]){
				  window.location="index.php?option=com_jchat&func=accessdenied&type=" + results[5];
				}
								
				jc_setparam('jc_lastid', lastid);
				
				var userlistdiv 		= document.getElementById('jc_userlist');
				userlistdiv.innerHTML 	= userlist;
				
				if(messagehtml){
					
				var target = document.getElementById(targetDiv);		
				var content = document.createElement('span');		
				content.innerHTML = messagehtml;
				
				target.appendChild(content, target.lastChild);
				
				var x = target.getElementsByTagName("script"); 
				   for(var i=0;i<x.length;i++)
				   {
					   eval(x[i].text);
				   }
				   
				}
				
			} else {				
				jc_ajaxError(xmlhttpGeneric.status);
			}
			//changeVisibility('jc_loading', 1, false);
  		}				
 	}
	
	if(!fromsend){
		if(results){
			if(results[4]){
				
				if (jc_detectopera()) {
					clearTimeout(timerID);
					alert(idlemessage);
					jc_accessfunc('', 'resettime');
					jc_setparam('jc_lastid', 'timeout');
					window.location="index.php?option=com_jchat";
				} else {
					clearTimeout(timerID);
					alert(idlemessage);
					jc_accessfunc('', 'resettime');
					jc_setparam('jc_lastid', 'timeout');
				}
			}
		}
	}
	
	timerID = setTimeout("getdata()", timeout);
	scrollbott("content");
 	return false
}

function gettabdata(id, force) 
{			
	
	param = id.split('_');
	
	var xmlhttpGeneric 	= getHTTPObject();
	var func 			= param[2];
	var targetDiv 		= id;
	var url 			= 'index.php?option=com_jchat';
	var param 			= '&func='+func+'&tabid='+id;
	
	if(!force){
		if($(targetDiv).innerHTML){
			return false;
		}
	}
	
	changeVisibility('jc_error', 1, false);
	changeVisibility('jc_loading', 1, 'inline');
	
	xmlhttpGeneric.open("post",url,true);
 	xmlhttpGeneric.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 	xmlhttpGeneric.send(param);
	
	xmlhttpGeneric.onreadystatechange=function() {
		
  		if (xmlhttpGeneric.readyState==4){	
		
			if (xmlhttpGeneric.status == '200'){
				
				result = xmlhttpGeneric.responseText;
				
				var target 	= $(targetDiv);		
				var content = document.createElement('span');		
				target.innerHTML = xmlhttpGeneric.responseText;
				
				var x = target.getElementsByTagName("script"); 
				   for(var i=0;i<x.length;i++)
				   {
					   eval(x[i].text);
				   }
				
			} else {				
				jc_ajaxError(xmlhttpGeneric.status);
			}
			changeVisibility('jc_loading', 1, false);
  		}				
		//scrollbott("content");
 	}

 	return false
}

function jc_accessfunc(divid, func, formid) 
{			
	
	
	
	var xmlhttpGeneric 	= getHTTPObject();
	var targetDiv 		= divid;
	var url 			= 'index.php?option=com_jchat';
	var formparams		= '';
	

	
	if(formid){
		var formparams = getFormValues(formid,'');
	}
	
	var param 			= '&func='+func+'&'+formparams;
	
	changeVisibility('jc_error', 1, false);
	changeVisibility('jc_loading', 1, 'inline');
	
	xmlhttpGeneric.open("post",url,true);
 	xmlhttpGeneric.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 	xmlhttpGeneric.send(param);
	
	if(divid == 'noreturn'){
		return;
	}
	
	xmlhttpGeneric.onreadystatechange=function() {
		
  		if (xmlhttpGeneric.readyState==4){	
		
			if (xmlhttpGeneric.status == '200'){
				
				result = xmlhttpGeneric.responseText;
				
				if(divid){
					var target 	= $(targetDiv);		
					var content = document.createElement('span');					
					target.innerHTML = xmlhttpGeneric.responseText;
					
					var x = target.getElementsByTagName("script"); 
					   for(var i=0;i<x.length;i++)
					   {
						   eval(x[i].text);
					   }
				}
				
			} else {				
				jc_ajaxError(xmlhttpGeneric.status);
			}
			changeVisibility('jc_loading', 1, false);
  		}				
		//scrollbott("content");
 	}

 	return false
}

function getHTTPObject() 
{
  	var xmlhttp;
  	/*@cc_on
  	@if (@_jscript_version >= 5)
    	try {
      		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      	try {
        	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      	} catch (E) {
        xmlhttp = false;
      	}
    }
  	@else
  	xmlhttp = false;
  	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    	try {
      	xmlhttp = new XMLHttpRequest();
    	} catch (e) {
      	xmlhttp = false;
    	}
  	}
  	return xmlhttp;
}
