
// Cleans the whitespaces(' ') from the left part of the string.
function ltrim(s)
{
	while (' ' == s.substring(0,1)) {
		s = s.substring(1, s.length);
	}
	return s;	
}

// Cleans the whitespaces(' ') from the right part of the string.
function rtrim(s)
{
	while (' ' == s.substring(s.length - 1, s.length)) {
		s = s.substring(0, s.length - 1);
	}
	return s;
}

// Cleans the whitespaces(' ') from left and right parts of the string.
function trim(s)
{
	s = ltrim(s);
	s = rtrim(s);
	return s;
}

// Checks whether the input string is blank.
function is_blank(s)
{
	return ('' == trim(s));
}

// Returns true if the string only contains characters A-Z, a-z or 0-9.
function is_alpha_numeric(str)
{
	var r = /[^a-zA-Z0-9]/g;
	return (!r.test(str));
}

// Returns true if the string is valid e-mail, otherwise returns false.
function is_valid_email(str)
{
	  if(is_blank(str)) return false;
	  var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	  return re.test(str);
}

// Returns true if the specified date is valid, otherwise returns false.
function is_date_valid(yyyy, mm, dd)
{
	if (yyyy.length != 4 || mm.length != 2 || dd.length != 2) {
		return false;
	}
	yyyy = parseInt(yyyy);
	mm = parseInt(mm);
	dd = parseInt(dd);
	var is_leap = false;
	if ((yyyy % 4 == 0) || (yyyy % 100 == 0) || (yyyy % 400 == 0)) {
		is_leap = true;
	}
	if (2 == mm && is_leap && dd > 28) {
		return false;
	}
	if ((dd > 31) && ((1 == mm) || (3 == mm) || (5 == mm) || (7 == mm) || (8 == mm) || (10 == mm) || (12 == mm))) {
		return false;
	}
	if ((dd > 30) && ((4 == mm) || (6 == mm) || (9 == mm) || (11 == mm))) {
		return false;
	}
	return true;
}

// Validates username.
function validate_username()
{
	var s = document.getElementById('username');
	if (null == s) {
		return true;
	} else if (is_blank(s.value) || (is_alpha_numeric(s.value) && 
						s.value.length > 4 && s.value.length < 16)) {
		return true;
	}
	alert('Error: invalid username is specified.');
	s.focus();
	s.select();
	return false;
}

// Validates e-mail.
function validate_email()
{
	var s = document.getElementById('email');
	if (null == s) {
		return true;
	} else if (is_valid_email(s.value)) {
		return true;
	}
	alert('Error: invalid email is specified.');
	s.focus();
	s.select();
	return false;	
}

// Validates question title.
function validate_question_title()
{
	var s = document.getElementById('title');
	if (null == s) {
		return true;
	} else if (!is_blank(s.value) && ltrim(s.value) == s.value && 
										rtrim(s.value) == s.value) {
		return true;
	}
	alert('Error: invalid question title is specified.');
	s.focus();
	s.select();
	return false;
}

function validate_passwords()
{
	var p = document.getElementById('current_password');
	if (null == p) {
		return true;
	}
	var n = document.getElementById('new_password');
	if (null == n) {
		return true;
	}
	var c = document.getElementById('confirm_password');
	if (null == c) {
		return true;
	}
	if (('' != c.value || '' != n.value) && '' == p.value) {
		alert('Error: current password is not specified.');
		p.focus();
		p.select();
		return false;
	}
	if (c.value != n.value) {
		alert('Error: new password and confirmation password doesn\'t match.');
		n.focus();
		n.select();
		return false;
	}
	if ('' != n.value && n.value.length < 6) {
		alert('Error: too short password is specified.');
		n.focus();
		n.select();
		return false;
	}
	return true;
}

function validate_birth_date()
{
	return false;
}

// Validates "Ask a question" form.
function validate_question_ask_form()
{
	var b;
	b = validate_username();
	if (!b) { return false; }
	b = validate_email();
	if (!b) { return false; }
	b = validate_question_title();
	if (!b) { return false; }
	b = validate_question_body();
	return b;
}

// Validates "Edit profile" form.
function validate_edit_profile_form()
{
	var b;
	b = validate_username();
	if (!b) { return false; }
	b = validate_email();
	if (!b) { return false; }
	b = validate_passwords();
	return b;
}
