
/* Functions used to validate the register form */

/* checks if the username is already taken */

function validate_username () {
	
	try {
		username = document.signup.username.value;
		
		if (username.length > 0)
			sendHttpRequest('components/username_available.php', 'name=' + username, handlePostValidateUsername);
	}
	catch (e) {
		try {
			document.getElementById ('username_status_label').value = 'Ett fel uppstod.';
		}
		catch (e) {
			
		}
	}
}

function handlePostValidateUsername () {
	if (sendReq.readyState == 4) {
		if (sendReq.responseText == "1") {
			try {
				statuslabelobj = document.getElementById ('username_status_label');
				
				statuslabelobj.innerHTML = 'Namnet är ledigt!';
				statuslabelobj.className = 'colsuccess';
			}
			catch (e) {
				
			}
		}
		else if (sendReq.responseText == "0") {
			try {
				statuslabelobj = document.getElementById ('username_status_label');
				
				statuslabelobj.innerHTML = 'Namnet är tyvärr upptaget.';
				statuslabelobj.className = 'colerror';
			}
			catch (e) {
				
			}
		}
		else {
			try {
				statuslabelobj = document.getElementById ('username_status_label');
				
				statuslabelobj.innerHTML = 'Ett fel uppstod.';
				statuslabelobj.className = 'colerror';
			}
			catch (e) {
				
			}
		}
	}
}

/* validate_form does a complete validation and returns true or false and sets fields accordingly */

function validate_form () {
	/* Username, can't check if it already taken here, just make sure the length is ok */
	
	var success = true;
	
	try {
		if (document.signup.username.value.length <= 0) {
			document.getElementById ('username_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('username_label').className='';
	}
	catch (e) { }
	
	try {
		/* Password, check non-empty equal to _confirm */
	
		passwordobj = document.signup.password.value;
		passwordconfirmobj = document.signup.password_confirm.value;
		
		if (passwordobj.length <= 0 || passwordobj != passwordconfirmobj) {
			document.getElementById ('password_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('password_label').className='';
		
		if (passwordconfirmobj.length <= 0) {
			document.getElementById ('password_confirm_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('password_confirm_label').className='';

	}
	catch (e) { }
	
	try {
	
		// E-mail confirm, check @ and length before and after @ > 0

		emailobj = document.signup.email.value;
		emailconfirmobj = document.signup.email_confirm.value;
		at = emailobj.value.indexOf ('@');
		
		if (at <= 0 || at == emailobj.length -1 || emailobj != emailconfirmobj) {
			document.getElementById ('email_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('email_label').className='';
		
		if (emailconfirmobj.length <= 0 || emailobj != emailconfirmobj) {
			document.getElementById ('email_confirm_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('email_confirm_label').className='';
	}
	catch (e) { }
	
	try {
	
		daysinmonths = new Array (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);	 /* Note leap year */
	
		// Born confirm, check all values are set, and that the day selected is in the range of the selected month

		year = document.signup.year;
		month = document.signup.month;
		day = document.signup.day;
		
		if (year.selectedIndex == -1 || month.selectedIndex == -1 || day.selectedIndex == -1 || year.options [year.selectedIndex].value.length <= 0 || month.options [month.selectedIndex].value.length <= 0 || day.options [day.selectedIndex].value.length <= 0 || day.options [day.selectedIndex].value > daysinmonths [month.options [month.selectedIndex].value]) {
			document.getElementById ('born_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('born_label').className='';
	}
	catch (e) { }
	
	try {
		if (!document.signup.female.checked && !document.signup.male.checked) {
			document.getElementById ('sex_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('sex_label').className='';
	}
	catch (e) { }
	
	try {
		locationobj = document.signup.location;
		
		if (locationobj.selectedIndex <= 0) {
			document.getElementById ('location_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('location_label').className='';
	}
	catch (e) { }
	
	try {
		if (!document.signup.eula.checked) {
			document.getElementById ('eula_label').className='colerror';
			success = false;
		}
		else
			document.getElementById ('eula_label').className='';
	}
	catch (e) { }
	
	return success;
}
