function focusElement(which) {
		if(!focusedElement) {
			which.focus(); 
			focusedElement = ! focusedElement;
			}
		isFormValid = false;
}

function validateForm() {
	isFormValid=true;
	focusedElement = false;
	
	frm=document.forms["frmreg"];

	// ************** remove spaces from non-password elements **************
	elems = frm.elements;
	for (i=0; i< elems.length; i++) {
		elem = elems[i];
		if (elem.value && elem.type != "password") {
			elem.value = elem.value.replace(/^\s*(.*)\s*$/gi, '$1');
		}
	}


	// ************** username validation **************
	username = frm.USERNAME;
	usernameHasValidMaxLength = (username.value.length > 12 ? false : true);
	usernameHasValidMinLength = (username.value.length < 4 ? false : true);	
	usernameHasValidStrings = ((username.value.toLowerCase()).indexOf('www') > -1 ? false : true);
	usernameHasValidCharacters = (!username.value.match(/^[a-z\d\-\_]*$/) ? false : true);

	if(!usernameHasValidMaxLength || !usernameHasValidMinLength || !usernameHasValidStrings || !usernameHasValidCharacters) {
		document.getElementById('username_error').style.display='block';
		focusElement(username)
	}
	else {
		document.getElementById('username_error').style.display='none';
	}


	document.getElementById('password_matching_error').style.display = 'none'
	document.getElementById('username_already_exists').style.display = 'none'	

	
	// ************** password validation **************
	password = frm.PASSWORD
	passwordHasValidMaxLength = (password.value.length > 12 ? false : true);
	passwordHasValidMinLength = (password.value.length < 4 ? false : true);
	passwordHasValidCharacters = (!password.value.match(/^[a-zA-Z\d\s]*$/) ? false : true); 	

	if(!passwordHasValidMaxLength || !passwordHasValidMinLength || !passwordHasValidCharacters) {
		document.getElementById('password_error').style.display='block';
		frm.PASSWORD.value="";
		frm.PASSWORD2.value="";
		focusElement(password)
		}
	else {
		document.getElementById('password_error').style.display='none';
	}

	// ************** password 2 validation **************
	password2 = frm.PASSWORD2
	password2isEntered = (password2.value.length > 0 ? true : false)	
	if(!password2isEntered) {
		document.getElementById('password2_error').style.display = 'block';
		focusElement(password2)		
	}
	else {
		document.getElementById('password2_error').style.display = 'none';
	}

	
	// ************** password matching validation **************
	if(password2.value!="" && (password.value != password2.value) ) {
		document.getElementById('password_matching_error').style.display = 'block';
		frm.PASSWORD.value="";
		frm.PASSWORD2.value="";
		focusElement(password)	
	}
	else {
		document.getElementById('password_matching_error').style.display = 'none';
	}




	// ************** email validation **************
	email = frm.EMAIL;
	emailIsEntered = (email.value.length > 0 ? true : false);
	isEmailValid = email.value.match(/^[a-z\d\.\-\_]+@[a-z\d\.\-\_]{2,}\.[a-z]{2,10}$/);

	if(!emailIsEntered  || !isEmailValid) {
		document.getElementById('email_error').style.display='block';
		focusElement(email)
	}
	else {
		document.getElementById('email_error').style.display='none';
	}


	// ************** accept terms and conditions **************
	areTermsAccepted = (frm.ACCEPT.checked ? true : false );
	if(!areTermsAccepted) {
			document.getElementById('terms_and_conditions_error').style.display='block';
		isFormValid = false;			
		}
	else {
		//document.getElementById('terms_and_conditions_error').style.display='none';
	}

	if(isFormValid) {
		frm.submit()
	} else {
	document.getElementById('notification_of_form_error').style.display='block';
		return;
	}
}

