function center(x) {

	var offsetX,offsetY;
	if($.browser.msie) {
		offsetX = document.documentElement.scrollLeft;
		offsetY = document.documentElement.scrollTop;
	} else {
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	};

	var left = ($(window).width() / 2) - ($(x).width() / 2) + offsetX;
	var top = ($(window).height() / 2) - ($(x).height() / 2) + offsetY;
	
	$(x).css('position', 'absolute');
	$(x).css('left', left);
	$(x).css('top', top);
	
}

var zindex = 1001;
function display(x) { $(x).show("slow"); }
function hide(x) { $(x).hide("slow"); }
function cad(x) { zindex++; $(x).css('z-index',zindex); center(x); display(x); }
function focusOn(x) { $(x).focus(); }

function transmit(f,send,place) {

	var url = 'server.php';
	send = 'f=' + f + '&' + send;

	$.ajax({
		type: "POST",
		url: url,
		dataType: "html",
		data: send,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$(place).html('Transmission error. Please try again.');
		},
		success: function(data, textStatus){
			$(place).hide().html(data).show("slow");
		}
	});

}


function process(event,target) {

	var f = $(event).attr('action');
	target = '#' + $(target).attr('id');

	if(f == 'login') {
		if($('#username').val().length == 0) { $(target).append('<div class="error"><span class="notice">The username field is empty.</span></div>').fadeTo(1600,0,function () { $('#topLoginBar').show(); $('.error').remove();  }).fadeTo(0,1); return false; }
		if($('#password').val().length == 0) { $(target).append('<div class="error"><span class="notice">You did not type your password.</span></div>').fadeTo(1600,0,function () { $('#topLoginBar').show(); $('.error').remove();  }).fadeTo(0,1); return false; }
	}
	
	formData = gather(event);
	transmit(f,formData,target);
	
	return false;
}


function gather(event) {

	var form = '#' + $(event).attr('id');
	var sendData,inputID,inputValue,skipInput;
		sendData = '';
	var i = 0;
	
	$(form + ' input[@type!=submit],select,textarea').each(function() {

		var inputID = $(this).attr('id');
			if(!inputID) { inputID = $(this).attr('class'); }

		var inputValue = '';
		var inputType = $(this).attr('type');
		
		skipInput = false; // Do not skip the current input.

		if(inputType == "checkbox") {
			if($(this).attr('checked')) {
				inputValue = true;
			} else {
				inputValue = false;
			}
		} else if(inputType == "radio") {
			if($(this).attr('checked')) {
				skipInput = false; // Do not skip the current radio button.
				inputValue = $(this).val();
				
				inputValue = inputValue.replace(/\+/g,'[plus]'); // replace all of the plus signs so data is properly sent to the server
				inputValue = inputValue.replace(/\&/g,'[amp]'); // replace all of the ampersands

			} else {
				skipInput = true; // The radio button is not checked, so skip it.
			}
		} else {
			inputValue = $(this).val();
			
			inputValue = inputValue.replace(/\+/g,'[plus]'); // replace all of the plus signs so data is properly sent to the server
			inputValue = inputValue.replace(/\&/g,'[amp]'); // replace all of the ampersands
		}
	
		if(skipInput == false) { sendData += '&' + inputID + '=' + inputValue; }

		i++;
	});

	return sendData;
}