//munreg.js - javascript for mun registration page

//function to get an element by its id - easier to type for w3, cross browser compatible
function getEl(id)
{
	var el;

	if (document.getElementById) {
		//first clause is for w3c specifications
		el = document.getElementById(id);
	} else if (document.all) {
		//this clause is for early version MSIE browsers
		el = document.all[id];
	} else if (document.layers) {
		//this clause is for early netscape browsers
		el = document.layers[id];
	}
	
	//return the correct DOM form of the object
	return el;
}

function getElementsByAtt(tag,att,value,parent)
{
	var list = parent.getElementsByTagName(tag);
	var arr = new Array();
	for (var i = 0; i < list.length; i++) {
		if (list[i].attributes[att]) {
			if (list[i].attributes[att].value == value) {
				arr.push(list[i]);
			}
		}
	}
	return arr;
}

function toggle (letter)
{
	var el;
	var list;
	
	//hide the one(s) showing previously
	list = getElementsByAtt('div','active','1',getEl('country_box'));
	for (var i = 0; i < list.length; i++) {
		list[i].setAttribute('active','0');
		list[i].style.display = "none";
	}
	
	//show the div corresponding to the new letter
	el = getEl(letter);
	el.style.display = 'block';
	el.setAttribute('active','1');
	
	return false;
}
function countryClick (country)
{	
	if (getEl('country_select') != undefined) {
		document.forms['reg'].elements['country'].value = country;
		getEl('country_select').innerHTML = 'You will be registered as <div class="bold">' + country + '</div> Submit this form to confirm.';
	}
}

function formJS ()
{
	f = document.forms['reg'];
	//test each of the input fields
	var regex = new RegExp('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$');
	if (!f.elements['email'].value.match(regex)) {alert('Please enter a valid email address'); return false;}
	if (f.elements['pwrd_1'].value.length < 6) {alert('Please enter a password of 6-12 characters'); return false;}
	if (f.elements['pwrd_1'].value != f.elements['pwrd_2'].value) {alert('You must re-enter your password correctly'); return false;}
	if (f.elements['fname'].value.length < 2) {alert('Please enter your first name'); return false;}
	if (f.elements['sname'].value.length < 2) {alert('Please enter your surname'); return false;}
	regex = new RegExp('^[0-9]{8,10}$');
	if (!f.elements['phone'].value.match(regex)) {alert('Please enter a valid contact number (8 or 10 digits)'); return false;}
	if (f.elements['country'].value.length < 2) {alert('Please select an available country by browsing the list below'); return false;}
	return true;
}

function updateFormJS ()
{
	f = document.forms['reg'];
	//test each of the input fields
	if (f.elements['fname'].value.length < 2) {alert('Please enter your first name'); return false;}
	if (f.elements['sname'].value.length < 2) {alert('Please enter your surname'); return false;}
	regex = new RegExp('^[0-9]{8,10}$');
	if (!f.elements['phone'].value.match(regex)) {alert('Please enter a valid contact number (8 or 10 digits)'); return false;}
	if (f.elements['country'].value.length < 2) {alert('Please select an available country by browsing the list below'); return false;}
	return true;
}
