function openMessageWindow(url, width, height) {
	var theindow;
	var windowName = "messageWindow";

	var x = 100;
	var y = 100;

	theWindow = window.open(url,windowName, 'left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y + ',width=' + width + ',height=' + height + ',directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
	theWindow.focus();
}

function validateMessage() {
	var theForm = document.getElementById('newMessageForm');
	
	if(theForm.name.value == "") {
		alert("Please enter your name.");
		theForm.name.focus();
		return;
	}
	if(theForm.subject.value == "") {
		alert("Please enter a subject.");
		theForm.subject.focus();
		return;
	}
	if(theForm.message.value == "") {
		alert("Please enter your message.");
		theForm.message.focus();
		return;
	}
	if(theForm.recaptcha_response_field.value == '') {
		alert('Please type the text in the image shown.');
		theForm.recaptcha_response_field.focus();
		return;
	}
	
	theForm.style.display = 'none';
	document.getElementById('submitting').style.display = 'block';
	
	submitForm(theForm);
}

function validatePoll() {
	var theForm = document.getElementById('newPollForm');
	
	if(theForm.name.value == "") {
		alert("Please enter your name.");
		theForm.name.focus();
		return;
	}
	if(theForm.subject.value == "") {
		alert("Please enter a subject.");
		theForm.subject.focus();
		return;
	}
	if(theForm.options.value == "") {
		alert("Please enter your poll options.");
		theForm.poll.focus();
		return;
	}
	if(theForm.recaptcha_response_field.value == '') {
		alert('Please type the text in the image shown.');
		theForm.recaptcha_response_field.focus();
		return;
	}
	
	theForm.style.display = 'none';
	document.getElementById('submitting').style.display = 'block';
	
	submitForm(theForm);
}

function vote() {
	var theForm = document.getElementById('voteForm');
	var i;
	var optionsNo = theForm.optionsNo.value;
	var valid = false;

	for(i=0;i<optionsNo;i++) {
		if(theForm.option[i].checked) {
			valid = true;
			break;
		}
	}

	if(!valid) {
		alert("Please choose one option.");
	} else {
		theForm.submit();
	}
}

function submitForm(theForm) {
	var xmlHttpRequest;
	var parameters;

	try {
		xmlHttpRequest = new XMLHttpRequest();
	} catch(e) {
		xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	
	xmlHttpRequest.onreadystatechange = function() {
		if(xmlHttpRequest.readyState != 4) return;
		
		submitFormFinished(theForm, xmlHttpRequest);
	};
	
	parameters = getFormParameters(theForm);
	
	xmlHttpRequest.open('POST', theForm.action, true);
	xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttpRequest.setRequestHeader("Content-length", parameters.length);
	xmlHttpRequest.send(parameters);
}

function getFormParameters(theForm) {
	var parameters = '';
	var i;
	
	for(i=0;i<theForm.elements.length;i++) {
		if(!theForm.elements[i].name) continue;
		if(!theForm.elements[i].value) continue;
		if(!theForm.elements[i].type) continue;
		
		parameters += theForm.elements[i].name + '=' + encodeURI(theForm.elements[i].value) + '&';
	}
	
	if(parameters != '') parameters = parameters.substr(0, parameters.length - 1);
	
	return parameters;
}

function submitFormFinished(theForm, xmlHttpRequest) {
	var result = xmlHttpRequest.responseText;
	var resultLines = result.split('\n');
	
	if(resultLines[0] == 'success') {
		document.location.href = resultLines[1];
	} else {
		Recaptcha.reload();
		theForm.recaptcha_response_field.value = '';
		
		theForm.style.display = 'block';
		document.getElementById('submitting').style.display = 'none';
		
		setTimeout("alert('The text you typed did not match the image, please try again')", 1);
	}
}
