/* Custom jQuery plugins */
(function($) {
	var tooltip_created = false;
	var tooltip_showing = false;
	var tooltip_x_offset = 0;
	var tooltip_y_offset = 0;

	$.fn.extend({
		/* Tooltip plugin for jQuery */
		tooltip: function(settings) {
			var default_settings = {
				title:      '',
				tip:        'This is a tip!',
				opacity:    .80,
				style:      'default',
				track:      false,
				x_offset:   20,
				y_offset:   0
			};
			settings = $.extend({}, default_settings, settings);
			return this.each(function() {
				this.settings = settings;
				//if(!tooltip_created) { create_tooltip(); }
			})
			.hover(show_tooltip, hide_tooltip);
		},
		disable: function(type) {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					if(typeof type == 'string' && type == 'readonly')
					{
						this.readOnly = true;
					}
					else
					{
						this.disabled = true;
					}
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					var blah = '';
					for(key in elements)
					{
						if(typeof elements[key] != 'undefined' && elements[key].type)
						{
							if(typeof type == 'string' && type == 'readonly')
							{
								elements[key].readOnly = true;
							}
							else
							{
								elements[key].disabled = true;
							}
						}
					}
				}
			});
		},
		enable: function(type) {
			return this.each(function() {
				// If it is a form input
				if(this.type)
				{
					if(typeof type == 'string' && type == 'readonly')
					{
						this.readOnly = false;
					}
					else
					{
						this.disabled = false;
					}
				}
				// If it is a form
				else
				{
					var elements = this.getElementsByTagName('*');
					for(key in elements)
					{
						if(typeof elements[key] != 'undefined' && elements[key].type)
						{
							if(typeof type == 'string' && type == 'readonly')
							{
								elements[key].readOnly = false;
							}
							else
							{
								elements[key].disabled = false;
							}
						}
					}
				}
			});
		}
	});

	/* Tooltip helper functions */
	function create_tooltip() {
		$(document.body).append('<div id="tooltip_container" style="display: none; position: absolute;"><div id="tooltip_title"></div><div id="tooltip_tip"></div></div>');
		tooltip_created = true;
	}
	function show_tooltip() {
		if(!tooltip_created) { create_tooltip(); }

		// Remember this for tracking
		tooltip_x_offset = this.settings.x_offset;
		tooltip_y_offset = this.settings.y_offset;
		
		// Start tracking if we need to
		if(this.settings.track)
		{
			$(this).bind('mousemove', move_tooltip);
		}

		var x = 0;
		var y = 0;
		// Get the starting position for the tooltip
		if(this.settings.track)
		{
			x = mX;
			y = mY;
		}
		else
		{
			// Get the position of the element
			data = find_pos(this);
			// Add the height of the source
			x = data.x;
			y = data.y;
		}
		// Move tooltip to the right a bit
		x += tooltip_x_offset;
		y += tooltip_y_offset;
		

		$('#tooltip_container').css('left', x);
		$('#tooltip_container').css('top', y);

		$('#tooltip_title').html(this.settings.title);
		$('#tooltip_tip').html(this.settings.tip);

		$('#tooltip_container').show();

		// make sure the height isn't not more than the viewable space
//		if($('#tooltip_container').width() > this.settings.max_width)
//		{
//			$('#tooltip_container').width(this.settings.max_width);
//		}
	}
	function hide_tooltip() {
		$(this).unbind('mousemove', move_tooltip);
		$('#tooltip_container').hide();
	}
	function move_tooltip(e)
	{
		// Calculate top and left offset for the prompt
		var arrayPageSize = getPageSize();

		var x = mX+tooltip_x_offset;
		var y = mY+tooltip_y_offset;
		
		if(x+$('#tooltip_container').width() > arrayPageSize[0])
		{
			x = mX-tooltip_x_offset-$('#tooltip_container').width();
		}
		
		$('#tooltip_container').css('left', x);
		$('#tooltip_container').css('top', y);
	}
	/* End Tooltip helper functions */
})(jQuery);


jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

function update_status(status)
{
	ajax_request({app:'user',mod:'social',act:'change_status',status:status},{
		success: function(data) {
			var status       = data.status;
			var status_class = data.status_class;

			$('#user_status').html(status);
			document.getElementById('user_status').className='status_'+status_class;
			my_menu.close();
		}
	});
}
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;
var IEVersion = 0.0;

if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
  i || (i = 0);
  var length = this.length;
  if (i < 0) i = length + i;
  for (; i < length; i++)
    if (this[i] === item) return i;
  return -1;
};

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}

// src: http://blog.stevenlevithan.com/archives/faster-trim-javascript
String.prototype.trim = function() {
	var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while(ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
String.prototype.reg_quote = function() {
	var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\'];
	var reg = new RegExp('(\\'+specials.join('|\\')+')','g');
	
	return this.replace(reg, '\\$1');
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize() {

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

/* Used in Form.class.php */
// Keep track of mouse x & y for clicking on text
var orig_x = 0;
var orig_y = 0;
// mX and mY come from general.js (mouse coords are recorded constantly in mX and mY)
function track_mouse()
{
	orig_x = mX;
	orig_y = mY;
}

function mouse_was_dragged()
{
	if(orig_x == mX && orig_y == mY)
	{
		return false;
	}
	return true;
}
/* END */

function addslashes(str)
{
	str=str.replace(/'/g, "\\'");
	return str;
}

if(IE) {
	var temp = navigator.appVersion.split("MSIE");
	IEVersion = parseFloat(temp[1]);
}

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use get_mouse_pos function onmousemove
document.onmousemove = get_mouse_pos;

// Temporary variables to hold mouse x-y pos.s
var mX = 0;
var mY = 0;

// Main function to retrieve mouse x-y pos.s
function get_mouse_pos(e) {
  if(IE) { // grab the x-y pos.s if browser is IE
  	var scrollLeft = 0;
  	var scrollTop = 0;
  
	if(document.documentElement)
	{
		scrollLeft = document.documentElement.scrollLeft;
		scrollTop  = document.documentElement.scrollTop;
	}
  	else if(document.body)
	{
		scrollLeft = document.body.scrollLeft;
		scrollTop  = document.body.scrollTop;
	}
  
    mX = event.clientX + scrollLeft;
    mY = event.clientY + scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mX = e.pageX;
    mY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mX < 0){mX = 0}
  if (mY < 0){mY = 0}  
  return true;
}

// function: find_pos
function find_pos(obj)
{
	if(typeof obj == "undefined" || obj == null)
	{
		return {"x":0,"y":0};
	}
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while(obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {"x":curleft,"y":curtop};
}

/* General JS Functions: tick for textarea */
function set_chars_left(name, max_len)
{
	var len = $('#'+name+'_input').val().length;
	var chars_left = 0;
	if(len >= max_len)
	{
		$('#'+name+'_input').val($('#'+name+'_input').val().substring(0, max_len));
	}
	else
	{
		chars_left = max_len-len;
	}
	$('#'+name+'_char_lim').html(chars_left.toString());
}

/* Set Cookie */
function set_cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

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

/* Get Cookie */
function get_cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

/* GENERAL CHECKS */
function is_empty(value)
{
	var tmp_val = value;
	tmp_val = tmp_val.replace(/ /ig,"");
	if(tmp_val == '' || tmp_val == null)
	{
		return true;
	}
	return false;
}
function is_len_gt(value, len)
{
	if(value.length > len)
	{
		return true;
	}
	return false;
}
function is_len_gte(value, len)
{
	if(value.length >= len)
	{
		return true;
	}
	return false;
}
function is_len_eq(value, len)
{
	if(value.length == len)
	{
		return true;
	}
	return false;
}
function is_len_lt(value, len)
{
	if(value.length < len)
	{
		return true;
	}
	return false;
}
function is_len_lte(value, len)
{
	if(value.length <= len)
	{
		return true;
	}
	return false;
}
function is_valid_email(value)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(filter.test(value))
	{
		return true;
	}
	return false;
}
function is_valid_domain(value)
{
	var filter = /^(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(filter.test(value))
	{
		return true;
	}
	return false;
}
function is_valid_username(value)
{
	var filter = /[^a-zA-Z0-9\-]/;
	if(filter.test(value))
	{
		return false;
	}
	return true;
}
function is_valid_date(field, value)
{
	// Are there any invalid characters?
	var filter = /[^0-9\-]/;
	if(filter.test(value))
	{
		return false;
	}

	var date = value.split('-');
	if(date.length != 3)
	{
		return false;
	}

	if(date[0] > 10 && date[0] < 100)
	{
		date[0] = parseInt(date[0]) + 1900;
		$('#'+field).val(date[2] + '-' + date[1] + '-' + date[0]);
	}

	var filter  = /^[0-9]{1,2}$/;
	var filter2 = /^[0-9]{4}$/;
	
	if(filter2.test(date[0]) && filter.test(date[1]) && filter.test(date[2]))
	{
		return true;
	}
	return false;
}
function is_valid_date_time(field, value)
{
	var parts = value.split(' ');
	var date = parts[0];
	var time = parts[1];

	// Are there any invalid characters?
	var filter = /[^0-9\-]/;
	if(filter.test(parts[0]))
	{
		return false;
	}

	var date = parts[0].split('-');
	if(date.length != 3)
	{
		return false;
	}

	if(date[2] > 10 && date[2] < 100)
	{
		date[2] = parseInt(date[2]) + 1900;
		$('#'+field).val(date[0] + '/' + date[1] + '/' + date[2]);
	}

	var filter  = /^[0-9]{1,2}$/;
	var filter2 = /^[0-9]{4}$/;
	
	if(filter.test(date[0]) && filter.test(date[1]) && filter2.test(date[2]))
	{
		return true;
	}
	return false;
}
function contains_only(value, regex)
{
	var filter = new RegExp("[^" + regex + "]");
	
	if(filter.test(value))
	{
		return false;
	}
	return true;
}
function make_lowercase(field, value)
{
	$('#'+field).val(value.toLowerCase());
	return true;
}




/* BUBBLE FUNCS */
var is_bubble_created = false;
function post_event2(type, message, container_id)
{
	if(!message) return;
	
	if(!is_bubble_created) create_bubble();
	
	// Set the bubble message
	set_event_bubble_message(type, message);

	// Handle bubble (appear, disappear, etc.)
	if(typeof container_id == "string" && container_id.indexOf(':') != -1)
	{
		var data = container_id.split(':');
		if(typeof data[1] == "string")
		{
			container_id = data[1];
			if($('#'+container_id).attr('type') == "hidden")
			{
				container_id = data[0];
			}
		}
		else
		{
			container_id = data[0];
		}
	}
	create_event_bubble(message, container_id);
}

function create_bubble()
{
	var bubble_html = '<div id="event_bubble_container" style="width: 200px; display: none;">'+
		''+
		'<!-- top -->'+
		'<div style="overflow: hidden; height: 25px;">'+
			'<div id="tlc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="tlc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/tlc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="tlc_image" src="images/event_bubble/tlc.png" />';
			}
			bubble_html += '</div>'+
			'<div id="event_bubble_top_bg" style="float: left; height: 25px; background: url(\'/images/event_bubble/top_bg.png\') bottom repeat-x;">&nbsp;</div>'+
			'<div id="trc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="trc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/trc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="trc_image" src="images/event_bubble/trc.png" />';
			}
			bubble_html += '</div>'+
			'<div class="spacer">&nbsp;</div>'+
		'</div>'+
		'<!-- end top -->'+
		''+
		'<!-- center container -->'+
		'<div>'+
		''+
			'<!-- left -->'+
			'<div id="event_bubble_left_bg" style="padding-left: 10px; background: url(\'/images/event_bubble/left_bg.png\') left repeat-y;">'+
		''+
				'<!-- right -->'+
				'<div id="event_bubble_right_bg" style="padding-right: 10px; background: url(\'/images/event_bubble/right_bg.png\') right repeat-y;">'+
		''+
					'<!-- message -->'+
					'<div id="event_bubble_message_container">'+
						'<div id="event_message"></div>'+
						'<div style="text-align: right; color: #000000;"><a href="#" onClick="$(\'#event_bubble_container\').hide(); return false;">close</a></div>'+
					'</div>'+
					'<!-- end message -->'+
		''+
				'</div>'+
				'<!-- end right -->'+
		''+
			'</div>'+
			'<!-- end left -->'+
		''+
		'</div>'+
		'<!-- end center container -->'+
		''+
		'<!-- bot -->'+
		'<div style="overflow: hidden; height: 25px;">'+
			'<div id="blc_image_container" style="float: left; width: 10px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="blc_image" style="width: 10px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/blc.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="blc_image" src="images/event_bubble/blc.png" />';
			}
			bubble_html += '</div>'+
			'<div id="event_bubble_bot_bg" style="float: left; height: 25px; background: url(\'/images/event_bubble/bot_bg.png\') top repeat-x;">&nbsp;</div>'+
			'<div id="brc_image_container" style="float: left; width: 58px; height: 25px;">';
			if(IE)
			{
				bubble_html += '<div id="brc_image" style="width: 58px; height: 25px; float: left; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/event_bubble/brs.png\', sizingMethod=\'image\');"></div>';
			}
			else
			{
				bubble_html += '<img id="brc_image" src="images/event_bubble/brs.png" />';
			}
			bubble_html += '</div>'+
			'<div class="spacer">&nbsp;</div>'+
		'</div>'+
		'<!-- end bot -->'+
		''+
	'</div>';
	
	$(document.body).append(bubble_html);
	is_bubble_created = true;
}

var bubble_message = '';
var bubble_status = 'closed';
var max_bubble_width = 300;
var min_bubble_width = 50;
// Every time this is called, we'll try to bring down the fader if
// it's not down and set a timer to bring it back up.  However, we
// may lock the fader so that it can't get a down call while going
// up or vice-versa
function create_event_bubble(message, container_id)
{
	// Calculate the width of the bubble
	var bubble_width = message.length * 7;
	if(bubble_width > max_bubble_width)
	{
		var words = message.split(" ");
		var char_count = 0;
		var first = true;
		for(var i = 0; i < words.length; i++)
		{
			var count = words[i].length;
			if(first == true)
			{
				first = false;
			}
			else
			{
				count++;
			}

			// If this word puts the width over the max, stay with char_count as the width
			if((char_count + count)*7 > max_bubble_width)
			{
				break;
			}
			char_count += count;
		}
		bubble_width = char_count*7;
	}
	
	if(bubble_width < min_bubble_width)
	{
		bubble_width = min_bubble_width;
	}

	if(typeof container_id == "undefined" || container_id == "null" || container_id == null || $(container_id) == null)
	{
		container_id = null;
	}
	
	var is_generic = true;
	if(container_id != null && container_id.indexOf('_input') != -1)
	{
		is_generic = false;
	}
	
	// Get container dimensions and position
	if(is_generic)
	{
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var container_dim = {width: arrayPageSize[0], height: arrayPageSize[1]};
		var container_pos = {x:arrayPageScroll[0], y:arrayPageScroll[1]};
}
	else
	{
		var container_dim = { width: $('#'+container_id).width(), height: $('#'+container_id).height() };
		var container_pos = find_pos(document.getElementById(container_id));
	}

	if(container_dim.width == 0)
	{
		// Default to this so the bubble doesn't go off the screen
		container_dim.width = 800;
	}

	if(is_generic)
	{
		// Calculate the position for the bubble to be in the exact middle of the container
		var bubble_x = container_pos.x + container_dim.width/2 + bubble_width/2;
		var	bubble_y = mY;//container_pos.y + container_dim.height/2;
	}
	else
	{
		// Calculate the position for the bubble to be just above the container to the left
		var bubble_x = container_pos.x-4;
		var	bubble_y = container_pos.y-4;
	}

	// Set the bubble widths
	$('#event_bubble_message_container').width(bubble_width);
	$('#event_bubble_container').width(bubble_width+20);

	// Set the bubble position
	var bubble_dim = { width: $('#event_bubble_container').width(), height: $('#event_bubble_container').height() }
	// Move the bubble left and up equal to its width and height
	bubble_y -= bubble_dim.height;
	bubble_x -= bubble_dim.width;
	// If the bubble y position is negative (off the screen), let's reposition it
	if(!is_generic)
	{
		var speech_y_pos = 'bot';
		var speech_x_pos = 'right';

		// Determine speech position
		if(bubble_y < 0)
			speech_y_pos = 'top';
		else
			speech_y_pos = 'bot';

		if(bubble_x < 0)
			speech_x_pos = 'left';
		else
			speech_x_pos = 'right';

		// Reset widths
		var tlc_width = 10;
		var trc_width = 10;
		var blc_width = 10;
		var brc_width = 10;
		if(speech_y_pos == 'top')
		{
			$('#event_bubble_top_bg').width(bubble_width-48);
			$('#event_bubble_bot_bg').width(bubble_width);

			if(speech_x_pos == 'left')
				tlc_width = 58;
			else
				trc_width = 58;
		}
		else
		{
			$('#event_bubble_top_bg').width(bubble_width);
			$('#event_bubble_bot_bg').width(bubble_width-48);

			if(speech_x_pos == 'left')
				blc_width = 58;
			else
				brc_width = 58;
		}
		$('#tlc_image_container').width(tlc_width);
		$('#trc_image_container').width(trc_width);
		$('#blc_image_container').width(blc_width);
		$('#brc_image_container').width(brc_width);

		// Reposition bubble
		if(speech_y_pos == 'top')
		{
			bubble_y += bubble_dim.height+container_dim.height+2;
			bubble_x += 1;
			// Make the speech on the top
		}
		if(speech_x_pos == 'left')
		{
			bubble_x += bubble_dim.width+container_dim.width+2;
			bubble_y += 1;
		}

		// Set corner images
		if(speech_y_pos == 'top' && speech_x_pos == 'left')
			if(IE)
				$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tlc', 'tls'));
			else
				$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tlc', 'tls'));
		else
			if(IE)
				$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tls', 'tlc'));
			else
				$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tls', 'tlc'));

		if(speech_y_pos == 'top' && speech_x_pos == 'right')
			if(IE)
				$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trc', 'trs'));
			else
				$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trc', 'trs'));
		else
			if(IE)
				$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trs', 'trc'));
			else
				$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trs', 'trc'));

		if(speech_y_pos == 'bot' && speech_x_pos == 'left')
			if(IE)
				$('#blc_image').css('filter', $('#blc_image').css('filter').replace('blc', 'bls'));
			else
				$('#blc_image').attr('src', $('#blc_image').attr('src').replace('blc', 'bls'));
		else
			if(IE)
				$('#blc_image').css('filter', $('#blc_image').css('filter').replace('bls', 'blc'));
			else
				$('#blc_image').attr('src', $('#blc_image').attr('src').replace('bls', 'blc'));

		if(speech_y_pos == 'bot' && speech_x_pos == 'right')
			if(IE)
				$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brc', 'brs'));
			else
				$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brc', 'brs'));
		else
			if(IE)
				$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brs', 'brc'));
			else
				$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brs', 'brc'));
	}
	else
	{
		if(bubble_y < 0)
		{
			bubble_y = container_pos.y;
		}

		// Make no speech
		$('#event_bubble_top_bg').css('width', bubble_width);
		$('#event_bubble_bot_bg').css('width', bubble_width);
		$('#trc_image_container').css('width', 10);
		$('#brc_image_container').css('width', 10);
		$('#tlc_image_container').css('width', 10);
		$('#blc_image_container').css('width', 10);
		// Reset corner images
		if(IE)
		{
			$('#trc_image').css('filter', $('#trc_image').css('filter').replace('trs', 'trc'));
			$('#tlc_image').css('filter', $('#tlc_image').css('filter').replace('tls', 'tlc'));
			$('#brc_image').css('filter', $('#brc_image').css('filter').replace('brs', 'brc'));
			$('#blc_image').css('filter', $('#blc_image').css('filter').replace('bls', 'blc'));
		}
		else
		{
			$('#trc_image').attr('src', $('#trc_image').attr('src').replace('trs', 'trc'));
			$('#tlc_image').attr('src', $('#tlc_image').attr('src').replace('tls', 'tlc'));
			$('#brc_image').attr('src', $('#brc_image').attr('src').replace('brs', 'brc'));
			$('#blc_image').attr('src', $('#blc_image').attr('src').replace('bls', 'blc'));
		}
	}
	$('#event_bubble_container').css('top', bubble_y);
	$('#event_bubble_container').css('left', bubble_x);
	
	if(bubble_status == 'closed')
	{
		$('#event_bubble_container').show();
	}

	bubble_shake();
}

function bubble_shake()
{
	$('#event_bubble_container').animate({left:'-=2px'}, 10);
	$('#event_bubble_container').animate({left:'+=4px'}, 10);
	$('#event_bubble_container').animate({left:'-=4px'}, 10);
	$('#event_bubble_container').animate({left:'+=4px'}, 10);
	$('#event_bubble_container').animate({left:'-=4px'}, 10);
}

function set_event_bubble_message(type, message)
{
	if(bubble_message != message)
	{
		$('#event_message').html(message);
		bubble_message = message;
	}
}






function post_event(type, message, container_id)
{
	if(IE && IEVersion < 7.0)
	{
		alert(message);
	}
	else
	{
		// Handle the event (insert, swap, fade, update, etc.)
		handle_event(type, message);
	
		// Handle fader (go up, go down, etc.)
		handle_fader();
	}
}

var slide_up_timer = null;
var fader_status = 'up';
// Every time this is called, we'll try to bring down the fader if
// it's not down and set a timer to bring it back up.  However, we
// may lock the fader so that it can't get a down call while going
// up or vice-versa
function handle_fader()
{
	// If handle_fader is locked, then try again in .1 sec
	if(func_is_locked('handle_fader'))
	{
		setTimeout("handle_fader();", 100);
		return;
	}

	// Reset the slide_up_timer
	clearTimeout(slide_up_timer);

	// Try to slide the fader down
	// Lock the handle_fader function for .5 sec
	if(fader_status == 'up')
	{
		$('#mxEventBar').animate({ top: '34px' }, 500);
		$('body').animate({ marginTop: '68px' }, 500);
		fader_status = 'down';
		setTimeout("lock_func('handle_fader', 500);", 3000);
	}
	
	// Set the mxEventBar to slide up in 10 sec
	// Once it's sliding up, lock the handle_fader function for .5 sec
	slide_up_timer = setTimeout("$('#mxEventBar').animate({ top: '0px' }, 500); $('body').animate({ marginTop: '34px' }, 500); lock_func('handle_fader', 500); fader_status = 'up';", 10000);
}

var event_msg = '';
var event_exists = false;
var event_fade_timer = null;
function handle_event(type, message)
{
	// If handle_event is locked, then try again in .1 sec
	if(func_is_locked('handle_event'))
	{
		setTimeout("handle_event('"+type+"', '"+addslashes(message)+"');", 100);
		return;
	}

	// Reset the event_fade_timer
	clearTimeout(event_fade_timer);

	// If we enounter the same message and the event exists, then let's not give it again
	// (spam protection!)
	if(!event_exists || event_msg != message)
	{
		if(type == 'error')
		{
			error_issued = true;
		}
		
		if(event_exists)
		{
			// If there is currently an event, then fade it out
			$('#mxEventText').fadeTo(500, 0.0);
		}
		
		// Replace content with new message in .5 sec
		// Also, toggle the class name so it appears correctly (colors, style, etc.)
		//message.evalScripts();
		//message = message.stripScripts();
		setTimeout("$('#mxEventText').html('"+addslashes(message)+"'); $('#mxEventText').attr('class', '"+type+"'); event_exists=true;", 500);

		// Fade in event in .5 sec
		setTimeout("$('#mxEventText').fadeTo(500, 1.0);", 500);

		// Store the event message for later comparison
		event_msg = message;
	}

	// Set the event to fade out (slow 1 sec fade) automatically in 9 sec
	// and lock this function for 1 sec
	event_fade_timer = setTimeout("$('#mxEventText').fadeTo(1000, 0.0); lock_func('handle_event', 1000); event_exists=false;", 9000);
}

lock_array = Array();
function lock_func(name, lock_time)
{
	// set the lock status for the function to true
	lock_array[name] = true;

	// unlock the function after the lock_time has expired
	setTimeout("unlock_func('"+name+"')", lock_time);
}
function unlock_func(name)
{
	lock_array[name] = false;
}
function func_is_locked(name)
{
	if(lock_array[name])
	{
		return true;
	}
	return false;
}


/* Slider functions */
function get_page_y_offset()
{
	if(IE)
	{
		if (document.documentElement && document.documentElement.scrollTop)
		{
			page_y_offset = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			page_y_offset = document.body.scrollTop;
		}
	}
	else
	{
		page_y_offset = window.pageYOffset;
	}
	return page_y_offset;
}

function slider(id, option)
{
	this.self = $('#'+id);

	if(option.margin_top) { this.margin_top = option.margin_top; }
	else { this.margin_top = 0; }

	if(option.type == 'fixed') { this.type = option.type; }
	else { this.type = 'normal'; }

	this.self.css('position', 'relative');

	var pos = find_pos(document.getElementById(id));
	this.orig_y_pos = pos.y;
	this.cur_y_pos = pos.y;

	this.self.css('top', 0);
	this.self.css('left', 0);
}
slider.prototype.check_y_pos = function()
{
	// Get page_y_offset
	var page_y_offset = get_page_y_offset()+this.margin_top;

	// If the type is fixed, once it reaches a scroll point, make it fixed
	if(this.type == 'fixed')
	{
		if(page_y_offset > this.orig_y_pos)
		{
			this.self.css('position', 'fixed');
			this.self.css('top', this.margin_top);
			return;
		}
		else
		{
			this.self.css('position', 'relative');
			this.self.css('top', 0);
			return;
		}
	}

	// Slow down as we reach the origin, or the top of the page
	if(page_y_offset < this.orig_y_pos)
	{
		var y_dif = Math.abs(this.orig_y_pos-this.cur_y_pos);
	}
	else
	{
		var y_dif = Math.abs(page_y_offset-this.cur_y_pos);
	}
	var y_move = parseInt(y_dif/10)+10;

	// Moving down
	if(page_y_offset > this.cur_y_pos)
	{
		if(this.cur_y_pos + y_move > page_y_offset)
		{
			this.self.css('top', (page_y_offset - this.orig_y_pos));
			this.cur_y_pos = page_y_offset;
		}
		else
		{
			this.self.css('top', (parseInt(this.self.css('top')) + y_move));
			this.cur_y_pos += y_move;
		}
	}
	// Moving up
	else if(page_y_offset < this.cur_y_pos && this.cur_y_pos > this.orig_y_pos)
	{
		if(this.cur_y_pos - y_move < this.orig_y_pos)
		{
			this.self.css('top', 0);
			this.cur_y_pos = this.orig_y_pos;
		}
		else
		{
			this.self.css('top', (parseInt(this.self.css('top')) - y_move));
			this.cur_y_pos -= y_move;
		}
	}
}
/* END Slider functions */

/* PrototypeJS functions ported over */
String.prototype.stripScripts = function() {
	return this.replace(new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'img'), '');
};

String.prototype.extractScripts = function() {
	var matchAll = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'img');
	var matchOne = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'im');
	return (this.match(matchAll) || []).map(function(scriptTag) {
		return (scriptTag.match(matchOne) || ['', ''])[1];
	});
};

String.prototype.evalScripts = function() {
	return this.extractScripts().map(function(script) { return eval(script) });
};

/* END PrototypeJS functions */

/* prompt */
function prompt_obj() {
	this.is_created = false;
	this.is_open    = false;
}
prompt_obj.prototype.initialize = function() {
	$(document.body).bind('keydown', function(e) {
		if(IE) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
			deleteKey = 46;
		} else { // mozilla
			keycode = e.keyCode;
			if(typeof e.DOM_VK_ESCAPE != 'undefined')
			{
				escapeKey = e.DOM_VK_ESCAPE;
				deleteKey = e.DOM_VK_DELETE;
			}
			else
			{
				escapeKey = 27;
				deleteKey = 46;
			}
		}

		key = String.fromCharCode(keycode).toLowerCase();

		if(my_prompt.is_open == true)
		{
			// close prompt
			if(keycode == escapeKey)
			{
				my_prompt.close();
			}
		}
	});
}
prompt_obj.prototype.open = function(settings) {
	var default_settings = {
		width: 400
	};
	settings = $.extend({}, default_settings, settings);
	if(!this.is_created) this.create(settings.style);

	// Subtract the size of the left and right sides of the header
	var header_width    = settings.width-40;
	var content_width   = settings.width-40;
	var left_width      = settings.width-40;
	var footer_width    = settings.width-40;

	var right_width     = settings.width-20;
	var right_bg_offset = settings.width-110;

	$('#prompt_window').css('width', settings.width);

	$('#prompt_top_row').css('width', settings.width);
	$('#prompt_mid_row').css('width', settings.width);
	$('#prompt_bot_row').css('width', settings.width);

	$('#prompt_right').css('width', right_width);
	$('#prompt_right').css('background-position', right_bg_offset+'px');

	$('#prompt_left').css('width', left_width);

	$('#prompt_top').css('width', header_width);
	$('#prompt_mid').css('width', content_width);
	$('#prompt_bot').css('width', footer_width);

	if(settings.content != null)
	{
		if(settings.close_button == true)
		{
			settings.content += '<div style="margin-top: 10px;"><input type="button" value="Close" onclick="my_prompt.close();" /></div>';
		}
		this.finalize({content: settings.content, title: settings.title});
	}
	else
	{
		ajax_request(settings.data, {success: my_prompt.finalize});
	}
}
prompt_obj.prototype.close = function() {
	// Hide overlay
	$('#prompt_container').hide();
	my_overlay.close();

	// Show flash and select boxes
	show_selects();
	show_flash();
	this.is_open = false;
}
prompt_obj.prototype.create = function(style) {
	if(this.is_created) return;
	
	if(!style) style = 'home';

	$(document.body).append('<div id="prompt_container" style="display: none;"><div id="prompt_window"><div id="prompt_window_content"></div><!-- end prompt window content --></div><!-- end prompt window --></div><!-- end prompt container -->');

	$('#prompt_window').prepend(''+
'	<div id="prompt_top_row">'+
'		<div id="prompt_tlc" class="prompt_handle"></div>'+
'		<div id="prompt_top" class="prompt_handle">'+
'			<div id="prompt_title"></div>'+
'		</div>'+
'		<div id="prompt_trc" class="prompt_handle"></div>'+
'		<div class="spacer"><!-- spacer --></div>'+
'	</div>'+
''+
'	<div id="prompt_mid_row">'+
'		<div id="prompt_right">'+
'			<div id="prompt_left">'+
'				<div id="prompt_mid">'+
'				</div>'+
'			</div>'+
'		</div>'+
'	</div>'+
''+
'	<div id="prompt_bot_row">'+
'		<div id="prompt_blc"></div>'+
'		<div id="prompt_bot"></div>'+
'		<div id="prompt_brc"></div>'+
'		<div class="spacer"><!-- spacer --></div>'+
'	</div>'+
''+
'	<div id="prompt_options">'+
'		<div id="prompt_x" onmousedown="my_prompt.close();"></div>'+
'	</div>');

	$('#prompt_window').draggable({handle: $('.prompt_handle')});
	this.is_created = true;
}
prompt_obj.prototype.finalize = function(data) {
	var auto_submit = data.auto_submit;
	var width = data.width;
	var title = data.title;
	var error = data.error;
	var content = data.content;
	var focus_id = data.focus_id;

	my_overlay.open();

	$('#prompt_title').html(title);
	$('#prompt_mid').html(content);

	// Set the prompt window width
	if(width != null)
	{
		$('#prompt_window').css('width', width);
	}
	
	// Calculate top and left offset for the prompt
	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();
	var promptTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var promptLeft = arrayPageScroll[0];
	$('#prompt_container').css('top', promptTop);
	$('#prompt_container').css('left', promptLeft);

	$('#prompt_container').show();

	// make sure the height isn't not more than the viewable space
	if($('#prompt_mid').height() > arrayPageSize[1]*.75)
	{
		$('#prompt_mid').height(arrayPageSize[1]*.75);
	}

	// Focus on the default focus input
	if($('#'+focus_id) != null)
	{
		$('#'+focus_id).focus();
	}
	my_prompt.is_open = true;
}
my_prompt = new prompt_obj();
my_prompt.initialize();

/* overlay */
function overlay_obj() {
	this.is_created = false;
	this.is_open = false;
	this.id = 'overlay';
}
overlay_obj.prototype.initialize = function() {
	my_overlay.create();
}
overlay_obj.prototype.open = function(settings) {
	var default_settings = {
		duration: 300,
		opacity: .5
	};
	settings = $.extend({}, default_settings, settings);
	if(!this.is_created) this.create();

	// Hide flash and select boxes
	hide_selects();
	hide_flash();

	$('#'+this.id).css('opacity', 0);
	$('#'+this.id).show();
	$('#'+this.id).fadeTo(settings.duration, settings.opacity);

	my_overlay.is_open = true;
}
overlay_obj.prototype.close = function() {
	// Hide overlay
	$('#'+this.id).fadeTo(300, 0.0, function() {
		$('#'+this.id).css('opacity', 0.0);
		$('#'+this.id).hide();
	});

	// Show flash and select boxes
	show_selects();
	show_flash();
	this.is_open = false;
}
overlay_obj.prototype.create = function() {
	if(this.is_created) return;
	
	$(document.body).append('<div id="'+this.id+'" style="display: none;"></div>');

	// Set overlay width and height
	var arrayPageSize = getPageSize();
	$('#'+this.id).css('width', arrayPageSize[0]);
	$('#'+this.id).css('height', arrayPageSize[1]);

	// Set overlay opacity to 0
	$('#'+this.id).css('opacity', 0);

	this.is_created = true;
}
my_overlay = new overlay_obj();
$(window).bind('load', my_overlay.initialize);

function show_selects()
{
	var selects = document.getElementsByTagName("select");
	for(i = 0; i != selects.length; i++)
	{
		selects[i].style.visibility = "visible";
	}
}

function hide_selects()
{
	var selects = document.getElementsByTagName("select");
	for(i = 0; i != selects.length; i++)
	{
		var node = selects[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the selects that are IN the prompt window!
		if(!keep)
			selects[i].style.visibility = "hidden";
	}
}

function show_flash()
{
	var flashObjects = document.getElementsByTagName("object");
	for(i = 0; i < flashObjects.length; i++)
	{
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for(i = 0; i < flashEmbeds.length; i++)
	{
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hide_flash(){
	var flashObjects = document.getElementsByTagName("object");
	for(i = 0; i < flashObjects.length; i++)
	{
		var node = flashObjects[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the flash objects that are IN the prompt window!
		if(!keep)
			flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for(i = 0; i < flashEmbeds.length; i++)
	{
		var node = flashEmbeds[i];
		var keep = false;
		
		while(node.id != null)
		{
			if(node.id == 'prompt_window_content') keep = true;
			node = node.parentNode;
		}
		// Don't remove the flash objects that are IN the prompt window!
		if(!keep)
		flashEmbeds[i].style.visibility = "hidden";
	}

}
/* end prompt */

/* upload progress */
var upload_url  = '';
var upload_pars = '';
var upload_id   = '';
// record last time and last amount to help record upload rate
var last_time = 0;
var last_amt = 0;
var upload_fin_js = '';
var fails_in_a_row = 0;
function init_progress_bar(server, id, success_js)
{
	upload_fin_js = success_js;

	upload_url  = site_url + '/scripts/retrieve_upload_progress.php';
	upload_pars = {id:id, server:server};

	setTimeout("click_progress_bar()", 250);
}

function click_progress_bar()
{
	var pars = upload_pars;
	pars['last_time'] = last_time;
	pars['last_amt'] = last_amt;
	ajax_request(pars, {success:update_progress_bar, url:upload_url});
}

function update_progress_bar(response)
{
	var amount_uploaded  = response.amount_uploaded;
	var size             = response.size;
	var percent_uploaded = response.percent_uploaded;
	var speed            = response.speed;
	var eta              = response.eta;
	// helps record download rate
	last_time            = response.last_time;
	last_amt             = response.last_amt;

	if(last_time < 0)
	{
		last_time = 0;
	}

	if(amount_uploaded == 'no_stat')
	{
		amount_uploaded = 0;
		fails_in_a_row++;
	}
	else
	{
		fails_in_a_row = 0;
	}
	if(fails_in_a_row >= 3)
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign','center');
		$('#upload_progress').html('Upload failed: File exceeds maximum size.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
		document.location=document.location;
	}

	if(amount_uploaded == 'fin')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload Complete!');
		$('#cancel_upload').hide();
		$('#upload_again').show();
		if(typeof upload_fin_js == 'string')
		{
			eval(upload_fin_js);
		}
		else if(typeof upload_fin_js == 'function')
		{
			upload_fin_js.apply(this,[]);
		}
	}
	else if(amount_uploaded == 'too_big')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign','center');
		$('#upload_progress').html('Upload failed: File exceeds maximum size.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'no_file')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: No file uploaded.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'no_space')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: File too large. Not enough space available.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else if(amount_uploaded == 'not_image')
	{
		$('#upload_progress').height(25);
		$('#upload_progress').css('textAlign', 'center');
		$('#upload_progress').html('Upload failed: File was not an image file.');
		$('#cancel_upload').hide();
		$('#upload_again').show();
	}
	else
	{
		$('#upload_progress').height(50);
		$('#upload_progress').css('textAlign', 'left');
		if(parseFloat(size) > 0 || $('#upload_progress').html() == '')
		{
			var margin = 10;
			var width = $('#prompt_window').width()-20-4-margin;
			var block_width = 1;
			var total_blocks = width/block_width;
			var bar = '<div style="margin-top: 5px; margin-left: '+(margin/2)+'px; padding: 1px; border: 1px solid #000000; background: #FFFFFF; overflow: hidden; height: 14px; width: '+width+'px;">';
			var num_blocks = parseInt(percent_uploaded/(100/total_blocks));
			if(!num_blocks || num_blocks == null || num_blocks < 1)
			{
				num_blocks = 1;
			}
			for(var i = 0; i < num_blocks; i++)
			{
				bar += '<div style="float: left; width: '+block_width+'px; height: 14px; background: #4d83ad;"></div>';
			}
			bar += '<div class="spacer">&nbsp;</div></div><div style="margin-left: '+(margin/2)+'px; overflow: hidden; width: '+width+'px; font-size: 10px;">';
			bar += amount_uploaded + '/' + size + '(' + percent_uploaded + '%) @ ' + speed + ' ETA: ' + eta;
			bar += '</div>';

			$('#upload_progress').html(bar);
		}
		setTimeout("click_progress_bar()", 250);
	}
}
/* end upload progress */

/* ajax methods */
function ajax_update(data, container, options)
{
	var defaults = {};
	options = $.extend(defaults, options);

	options.update = container;
	ajax_request(data, options);
}

function ajax_request(data, options)
{
	var defaults = {
		url:        'index.php'
	};

	options = $.extend(defaults, options);

	// secure the request
	if(typeof data == 'string')
		data += '&tb_secure='+get_cookie('tb_secure');
	else
		data.tb_secure = get_cookie('tb_secure');

	$.ajax({
		url:  options.url,
		data: data,
		type: 'post',
		dataType: 'json',
		success: function(response, textStatus) {
			var e = response.error_messages;
			var s = response.success_messages;
			var c = response.content;
			var f = response.field;
			var scripts = null;

			if(typeof c != 'undefined')
			{
				scripts = c.extractScripts();
				response.content = c.stripScripts();
				c = response.content;
			}

			// pass to error and success functions if able
			if(e.length == 0 && typeof options == 'object' && typeof options.success == 'function')
			{
				options.success.apply(this, [response]);
			}
			else if(e.length > 0)
			{
				if(typeof options == 'object' && typeof options.error == 'function')
				{
					options.error.apply(this, [response]);
					return;
				}
				else
				{
					var error_text = '';
					for(var i=0;i<e.length;i++)
					{
						error_text += e[i]+'<br />';
					}
					my_prompt.open({content:error_text,close_button:true});
				}
			}

			// update container
			if(typeof options.update != 'undefined')
			{
				$('#'+options.update).html(c.stripScripts());
			}

			// execute scripts if able
			if(scripts != null)
			{
				for(i in scripts)
				{
					eval(scripts[i]);
				}
			}
		},
//		error: function(XMLHttpRequest, textStatus, errorThrown) {
//			alert('XMLHttpRequest: '+XMLHttpRequest);
//			alert('textStatus: '+textStatus);
//			alert("Error: " + errorThrown);
//		},
		complete: function(response, textStatus) {
			if(response.status == '404')
			{
				my_prompt.open({'content':'Page not found.','close_button':true});
			}
			else if(textStatus == 'parsererror')
			{
				my_prompt.open({'content':'Parser error.','close_button':true});
			}
			else if(textStatus == 'error')
			{
				my_prompt.open({'content':'An error occurred.','close_button':true});
			}

			var blah = '';
			for(i in response)
			{
				if(i != 'channel')
					blah += i+'='+response[i]+"\n";
			}
//			alert(blah);

			var DEBUG = true;
			if(typeof DEBUG == 'boolean' && DEBUG == true)
			{
				var the_url = options.url;
				if(typeof data == 'string')
				{
					the_url += data;
				}
				else
				{
					for(i in data)
					{
						the_url += i+'='+data[i]+',';
					}
				}
				if(textStatus == 'error')
				{
//					alert('Parser Error: '+response.responseText);
					alert('Incorrect ajax call: check URL.'+the_url);
				}
				else if(textStatus == 'parsererror')
				{
					alert('Parser Error: '+response.responseText+'. '+the_url);
				}
			}

			if(typeof data.form_id == "string")
			{
				$('#'+data.form_id).enable();
			}
		}
	});
}
/* end ajax methods */