//Used to keep text input fields from submitting a form
function noSubmit()
{
	if (window.event) {
		if (window.event.keyCode == 13 || window.event.keyCode == 3) {
			return false;
		}
	}

	return true;
}

//Set the focus on a particular field
function setFocus(field)
{
	eval("obj = document.forms['site_form']." + field);

	obj.focus();

	if (obj.type == 'text' || obj.type == 'textarea') {
		obj.select();
	}
}

//Load the selected page
function dropNav(url)
{
	window.location.href = url;
}

//Open a page in a new window in an XHTML compliant manner
function newWindow(url)
{
	window.open(url, '_blank');
}

//Set the initial hex value for fadeText()
hex = 255;

function fadeText(id)
{
	//Check if we still need to fade the color
	if (hex > 0) {
		hex -= 5;
		document.getElementById(id).style.color = 'rgb(255,' + hex + ',' + hex + ')';
		setTimeout('fadeText("' + id + '")',15);
	} else {
		//Reset the color
		hex = 255;
	}
}

function checkError()
{
	//Check that the error id exists
	if (obj = document.getElementById('error')) {
		//Check that the error text exists
		if (obj.innerHTML != '') {
			//Add line breaks
			obj.innerHTML += '<br /><br />';

			//Fade the error text
			fadeText('error');
		}
	}
}

//Create the ajax class
function Ajax()
{
}

//Retrieve the data at a given URL and pass it to a handler function
Ajax.prototype.sendRequest = function()
{
	//Set the var so we can scope the callback
	var _this = this;

	if (window.XMLHttpRequest) {
		this.request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.request = new ActiveXObject("Msxml2.XMLHTTP");

		if (!this.request) {
			this.request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (this.request) {
		this.request.onreadystatechange = function() {_this.requestHandler()};

		try {
			this.request.open("GET", this.url, true);
		} catch(e) {
			alert('Ajax object error: call() could not open a connection.\n' + e);
		}

		this.request.send(null);
	} else {
		alert('Ajax object error: Could not generate an XMLHttpRequest object.');
	}
}

//Handle the XML request object
Ajax.prototype.requestHandler = function()
{
	if (this.request.readyState == 4) {
		if (this.request.status == 200) {
			this.callback(this.content_id, this.request.responseText);
		} else {
			if (this.request.statusText == '') {
				problem_text = 'Request for ' + this.url + ' timed out.';
			} else {
				problem_text = this.request.statusText;
			}

			alert(problem_text);
		}
	}
}

//Update the status script
function statusUpdate(id, message, count)
{
	if (!count) {
		start = 1;
		count = 1;
	} else {
		start = 0;

		if (count == 5) {
			count = 1;
		} else {
			count++;
		}
	}

	dot_string = '';

	for (inc = 1; inc < count; inc++) {
		dot_string += ' .';
	}

	obj = document.getElementById(id);
	sub_string = obj.innerHTML.substr(0, message.length);

	if (sub_string == message || start == 1) {
		obj.innerHTML = message + dot_string;
		setTimeout(function() {statusUpdate(id, message, count)}, 1000);
	}
}

//Grab the site form
var site_form = document.getElementById('site_form');