default_values = new Array();
firstTime=1;

function loader(){
	new Ajax.Updater($('page'), 'registration.php', {
		method: 'post',
		parameters:{
			page: 'page1'
		},
	}
	);
}

function submitRegForm() {
	error=false;
	if($('regForm').elements['first'].value==''){
		error=true;
		$('firstText').style.color="red";
	}
	else $('firstText').style.color="";
	if($('regForm').elements['last'].value==''){
		error=true;
		$('lastText').style.color="red";
	}
	else $('lastText').style.color="";
	if($('regForm').elements['school'].value=='' || $('regForm').elements['school'].value=='Select your School'){
		error=true;
		$('schoolText').style.color="red";
	}
	else $('schoolText').style.color="";
	if($('regForm').elements['major'].value==''){
		error=true;
		$('majorText').style.color="red";
	}
	else $('majorText').style.color="";
	if($('regForm').elements['gender'].value=='0'){
		error=true;
		$('genderText').style.color="red";
	}
	else $('genderText').style.color="";
	if($('regForm').elements['ethnicity'].value==''){
		error=true;
		$('ethnicityText').style.color="red";
	}
	else $('ethnicityText').style.color="";
	if($('regForm').elements['year'].value=='' || isNaN($('regForm').elements['year'].value) || $('regForm').elements['year'].value<1900 || $('regForm').elements['year'].value>2009){
		error=true;
		$('birthdayText').style.color="red";
	}
	else $('birthdayText').style.color="";
	if($('regForm').elements['zipcode'].value=='' || isNaN($('regForm').elements['zipcode'].value) || $('regForm').elements['zipcode'].value>99999){
		error=true;
		$('zipText').style.color="red";
	}
	else $('zipText').style.color="";
	if($('regForm').elements['arrival'].value=='0'){
		error=true;
		$('arrivalSelect').style.color="red";
	}
	else $('arrivalSelect').style.color="";
	if($('regForm').elements['email1'].value==''){
		error=true;
		$('email1Text').style.color="red";
	}
	else $('email1Text').style.color="";
	if($('regForm').elements['email2'].value==''){
		error=true;
		$('email2Text').style.color="red";
	}
	else $('email2Text').style.color="";
	if($('regForm').elements['email1'].value!=$('regForm').elements['email2'].value){
		error=true;
		$('email2Text').style.color="red";
	}
	else $('email2Text').style.color="";
	if($('regForm').elements['email1'].value==$('regForm').elements['referEmail'].value && $('regForm').elements['email1'].value!=''){
		error=true;
		$('emailReferText').style.color="red";
	}
	else $('emailReferText').style.color="";
	if($('regForm').elements['classYear'].value=='0'){
		error=true;
		$('classYearText').style.color="red";
	}
	else $('classYearText').style.color="";
	if($('regForm').elements['shirt'].value=='0'){
		error=true;
		$('shirtText').style.color="red";
	}
	else $('shirtText').style.color="";
	if($('regForm').elements['phone'].value==''){
		error=true;
		$('phoneText').style.color="red";
	}
	else $('phoneText').style.color="";
	if($('regForm').elements['emergencyName'].value==''){
		error=true;
		$('emergencyNameText').style.color="red";
	}
	else $('emergencyNameText').style.color="";
	if($('regForm').elements['emergencyNumber'].value==''){
		error=true;
		$('emergencyNumberText').style.color="red";
	}
	else $('emergencyNumberText').style.color="";
	if($('regForm').elements['emergencyEmail'].value==''){
		error=true;
		$('emergencyEmailText').style.color="red";
	}
	else $('emergencyEmailText').style.color="";
	if(error){
		$('errorMessage').style.display="";
		Event.observe('regForm', 'submit', function(event) {
			Event.stop(event); // stop the form from submitting
		});
	}
	else{
		Event.observe('regForm', 'submit', function(event) {
			$('regForm').request( {
				onFailure : function() {
				},
				onComplete : function(t) {
					$('page').update(t.responseText);
				}
			});
			Event.stop(event); // stop the form from submitting
			});
	}
}

function submitForm(formID) {
	
	Event.observe(formID, 'submit', function(event) {
		$(formID).request( {
			onFailure : function() {
			},
			onComplete : function(t) {
				$('page').update(t.responseText);
			}
		});
		Event.stop(event); // stop the form from submitting
		});
}

function populateSchool(school){
	$('schoolInput').value=school;
	$('schoolSelect').style.display="none";
}

function showSchoolSelect(){
	if(firstTime) setFormDefaults($('newSchoolInput'));
	firstTime=0;
	if($('schoolSelect').style.display=="block") $('schoolSelect').style.display="none";
	else $('schoolSelect').style.display="block";
}

function forceShowSchoolSelect(){
	if(firstTime) setFormDefaults($('newSchoolInput'));
	firstTime=0;
	$('schoolSelect').style.display="block";
}

function forceHideSchoolSelect(){
	if(firstTime) setFormDefaults($('newSchoolInput'));
	firstTime=0;
	$('schoolSelect').style.display="none";
}

function setFormDefaults(input) {
	var active_color = '#000'; // Colour of user provided text
	var inactive_color = '#777'; // Colour of default text
	input.style.color = inactive_color;
	if (!default_values[input.id]) {
		default_values[input.id] = input.value;
	}
	input.onfocus = function() {
		if (this.value == default_values[this.id]) {
			this.value = '';
			this.style.color = active_color;
		}
		this.onblur = function() {
			forceHideSchoolSelect();
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		}
	}
}