<?xjs 
	/*
	 * Unfortunately, this page is "modified" whenever the password
	 * is changed.  No caching
	 */

	http_reply.header['Content-Type']='text/javascript';
	load(cwd+'functions.js');
?>

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

// Script from http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var body_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'body.xjs')) ?>";
var subs_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'subs.xjs')) ?>";
var messages_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'messages.xjs')) ?>";
var groups_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'groups.xjs')) ?>";
var plus_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'plus.gif')) ?>";
var minus_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'minus.gif')) ?>";
var login_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'login.xjs')) ?>";
var update_ptr_url="<?xjs write(http_request.virtual_path.replace(/client_functions\.xjs$/,'update_ptr.xjs')) ?>";
var user=<?xjs if(user.alias != '') write(user.alias.toSource()); else write(user.name.toSource()) ?>;
var password=<?xjs write(user.security.password.toSource()) ?>;
var read_ptr=new Object();

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
		var expires_date = new Date( today.getTime() + (expires) );
		var expires_string = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][expires_date.getUTCDay()]+", ";
		if(expires_date.getUTCDate() < 10)
			expires_string += '0';
		expires_string += expires_date.getUTCDate() + '-';
		expires_string += ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][expires_date.getUTCMonth()]+'-';
		if(expires_date.getUTCFullYear()%100 < 10)
			expires_string += '0';
		expires_string += (expires_date.getUTCFullYear()%100) + ' ';
		if(expires_date.getUTCHours() < 10)
			expires_string += '0';
		expires_string += expires_date.getUTCHours() + ':';
		if(expires_date.getUTCMinutes() < 10)
			expires_string += '0';
		expires_string += expires_date.getUTCMinutes() + ':';
		if(expires_date.getUTCSeconds() < 10)
			expires_string += '0';
		expires_string += expires_date.getUTCSeconds() + ' UTC';
	}

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_string : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function clean_id(id)
{
	return(id.replace(/[^A-Za-z0-9]/g,'_'));
}

function toggle_quotes(container)
{
	if(container.checked)
		Set_Cookie("hide_quotes", "yes", 90);
	else
		Set_Cookie("hide_quotes", "no", 90);
}

function toggle_ansi(container)
{
	if(container.checked)
		Set_Cookie("ansi_messages", "yes", 90);
	else
		Set_Cookie("ansi_messages", "no", 90);
}

function toggle_replies(sub_code, message_number)
{
	var containerid='replies-'+clean_id(sub_code)+'-'+message_number;
	var container=document.getElementById(containerid);
	var expanderid='reply-expander-'+clean_id(sub_code)+'-'+message_number;
	var expander=document.getElementById(expanderid);

	if(container.innerHTML=='') {
		ajaxpage(messages_url+"?sub_code="+encodeURIComponent(sub_code)+"&msg_number="+message_number+'&user='+encodeURIComponent(user),containerid,expanderid,minus_url);
		container.style.display='block';
	}
	else {
		if(container.style.display=='none') {
			container.style.display='block';
			expander.src=minus_url;
		}
		else {
			container.style.display='none';
			expander.src=plus_url;
		}
	}
}

function toggle_quote(container)
{
	for(child in container.parentNode.childNodes) {
		if(container.parentNode.childNodes[child].nodeName=='BLOCKQUOTE') {
			switch(container.parentNode.childNodes[child].style.display) {
			case 'none':
				container.parentNode.childNodes[child].style.display='block';
				container.innerHTML="Hide quote";
				break;
			default:
				container.parentNode.childNodes[child].style.display='none';
				container.innerHTML="Show quote";
				break;
			}
			break;
		}
	}
}

function load_ansi(container, sub_code)
{
	var message=container.parentNode.parentNode;
	var message_number;
	var m;

	if((m=container.id.match(/^message-body-(.*)-([0-9]+)$/))!=null) {
		message_number=m[2];
		ajaxpage(body_url+"?sub_code="+encodeURIComponent(sub_code)+"&msg_number="+message_number+'&user='+encodeURIComponent(user)+"&ansi=true",container.id);
		container.style.display='block';
	}
}

function load_html(container, sub_code)
{
	var message=container.parentNode.parentNode;
	var message_number;
	var m;

	if((m=container.id.match(/^message-body-(.*)-([0-9]+)$/))!=null) {
		message_number=m[2];
		ajaxpage(body_url+"?sub_code="+encodeURIComponent(sub_code)+"&msg_number="+message_number+'&user='+encodeURIComponent(user),container.id);
		container.style.display='block';
	}
}

function toggle_body(sub_code, message_number)
{
	var containerid='message-body-'+clean_id(sub_code)+'-'+message_number;
	var container=document.getElementById(containerid);
	var message=container.parentNode.parentNode;

	if(container.innerHTML=='') {
		ajaxpage(body_url+"?sub_code="+encodeURIComponent(sub_code)+"&msg_number="+message_number+'&user='+encodeURIComponent(user)+"&ansi="+document.getElementById('ansi_default_checkbox').checked, container.id);
		container.style.display='block';
		/* If this is higher than the current read_ptr, update it */
		if(read_ptr[sub_code] < message_number)
			read_ptr[sub_code] = message_number;
		/* If this item was new, "un-new" it */
		if(message.className=='message_is_new') {
			/* No longer new... update it and it's parents... */
			message.className='message';
			/* Now for the parents.. */
			parentLoop: while(1) {
				message=message.parentNode.parentNode.parentNode;
				if(message==undefined || message==null)
					break;
				switch(message.className) {
				case 'message_inherit_new':
					message.className='message';
					break;
				case 'message_is_new_inherit_new':
					message.className='message_is_new';
					break parentLoop;
				default:
					break;
				}
			}
		}
		else if(message.className=='message_is_new_inherit_new') {
			/* No longer new update it... */
			message.className='message_inherit_new';
		}
	}
	else {
		if(container.style.display=='none')
			container.style.display='block';
		else
			container.style.display='none';
	}
}

function load_more_messages(sub_code, offset, count)
{
	var containerid='message-list-'+clean_id(sub_code);
	var container=document.getElementById(containerid);
	var m=container.innerHTML.match(/^([\u0000-\uffff]*?)<[Dd][Ii][Vv] class="load_more_messages"><[Aa] href="javascript:load_more_messages[\u0000-\uffff]*$/);

	if(m!=null) {
		ajaxpage(messages_url+"?sub_code="+encodeURIComponent(sub_code)+'&msg_count='+count+'&msg_offset='+offset+'&user='+encodeURIComponent(user),containerid, undefined, undefined, false, m[1]);
	}
	else {
		alert("No match!");
	}
}

function toggle_messages(sub_code)
{
	var containerid='message-list-'+clean_id(sub_code);
	var container=document.getElementById(containerid);

	if(container.innerHTML=='') {
		ajaxpage(messages_url+"?sub_code="+encodeURIComponent(sub_code)+'&user='+encodeURIComponent(user),containerid);
		container.style.display='block';
		if(read_ptr[sub_code]==undefined)
			read_ptr[sub_code]=0;
	}
	else {
		if(container.style.display=='none')
			container.style.display='block';
		else
			container.style.display='none';
	}
}

function toggle_subs(group_code)
{
	var containerid='group-subs-'+clean_id(group_code);
	var container=document.getElementById(containerid);

	if(container.innerHTML=='') {
		ajaxpage(subs_url+"?group_code="+encodeURIComponent(group_code)+'&user='+encodeURIComponent(user),containerid);
		container.style.display='block';
	}
	else {
		if(container.style.display=='none')
			container.style.display='block';
		else
			container.style.display='none';
	}
}

function reload_groups()
{
	var containerid='group-list';
	ajaxpage(groups_url+'?user='+encodeURIComponent(user),containerid);
}

function ajaxpage(url, containerid, buttonid, buttonurl, is_script, prefix, suffix)
{
	var page_request = false;
	if(prefix==undefined)
		prefix='';
	if(suffix==undefined)
		suffix='';
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else
		return false;
	if(containerid != undefined) {
		if(is_script) {
			page_request.onreadystatechange=function() {
				evalpage(page_request, containerid, prefix, suffix);
			}
		}
		else {
			page_request.onreadystatechange=function(){
				loadpage(page_request, containerid, buttonid, buttonurl, prefix, suffix);
			}
		}
		document.body.style.cursor="progress";
		document.getElementById(containerid).innerHTML=spin('');
	}
	page_request.open('GET', url, true);
	page_request.setRequestHeader('Authorization',"Basic "+encode64(user+":"+password));
	page_request.send(null);
}

function spin(current)
{
	var spins=[
			['&#9474;','/','&#9472;','\\'],
			['&#9617;','&#9618;','&#9619;','&#9608;','&#9619;','&#9618;'],
			['-', '=', '&#8801;', '='],
			['&#9484;','&#9492;','&#9496;','&#9488;'],
			['&#9604;','&#9616;','&#9600;','&#9612;']
		];
	var spin;
	var pos;
	var m;

	m=current.match(/^\<span style="font-family: monospace;">\<!-- (\d+),(\d) -->Loading (.)\<\/span>$/);
	if(m == null) {
		spin=parseInt(Math.random()*spins.length);
		pos=0;
	}
	else {
		spin=parseInt(m[1]);
		pos=parseInt(m[2])%spins[spin].length;
	}
	return '<span style="font-family: monospace;"><!-- '+spin+','+pos+1+' -->Loading '+spins[spin][pos]+'</span>';
}

function evalpage(page_request, containerid, prefix, suffix)
{
	if(page_request.readyState==4) {
		document.body.style.cursor="auto";
		if ((page_request.status==200 || window.location.href.indexOf("http")==-1)) {
			document.getElementById(containerid).innerHTML=prefix+eval(page_request.responseText)+suffix;
		}
		else
			document.getElementById(containerid).innerHTML=prefix+'ERROR!'+suffix;
	}
	else {
		document.getElementById(containerid).innerHTML=prefix+spin(document.getElementById(containerid).innerHTML)+suffix;
	}
}

function loadpage(page_request, containerid, buttonid, buttonurl, prefix, suffix)
{
	if(page_request.readyState==4) {
		document.body.style.cursor="auto";
		if ((page_request.status==200 || window.location.href.indexOf("http")==-1)) {
			document.getElementById(containerid).innerHTML=prefix+page_request.responseText+suffix;
			if(buttonid != undefined && buttonurl != undefined)
				document.getElementById(buttonid).src=buttonurl;
		}
		else
			document.getElementById(containerid).innerHTML=prefix+suffix;
	}
	else {
		document.getElementById(containerid).innerHTML=prefix+spin(document.getElementById(containerid).innerHTML)+suffix;
	}
}

function login()
{
	var new_user=document.getElementById('login_username').value;
	var new_password=document.getElementById('login_password').value;
	/* Clear newest read pointers */
	read_ptr=new Object();
	ajaxpage(login_url+'?user='+encodeURIComponent(new_user)+'&pass='+encodeURIComponent(new_password)+'&killcache='+new Date().getTime()+Math.random(), 'current_user',undefined,undefined,true);
}

function update_pointers()
{
	var args=new Array();

	for(sub_code in read_ptr)
		args.push(encodeURIComponent(sub_code)+'='+read_ptr[sub_code]);

	ajaxpage(update_ptr_url+"?"+args.join('&'));
}
