function check() {
	var errors = "";

	//trim the username
	$('#frm_signin_username').attr("value", jQuery.trim($('#frm_signin_username').attr("value")));


	if ($('#frm_signin_username').attr("value") == "") {
		errors += "\n - Enter the username.";
	}

	if (jQuery.trim($('#frm_signin_password').attr("value")) == "") {
		errors += "\n - Enter the password.";
	}

	if (errors == "") {
		$('#div_signin').hide();
		$('#div_signin_text').show();
		$('#div_signin_text').html('Validating...');

		$.ajax(
			{
				type: "POST",
				url: $('#frm_signin').attr("action"),
			 	cache: false,
				data: $('#frm_signin').serialize(),
				complete: function(resp) {
					//public account
					if (resp.responseText == 1) {
						alert("Please create your personal account for this property.");
						$('#div_signin_text').html('');
						$('#div_signin_text').hide();
						$('#div_public').show();
					}
					//account needs to be personalized
					else if (resp.responseText == 2) {
						alert("Please personalize your account.");
						$('#div_signin_text').html('');
						$('#div_signin_text').hide();
						$('#div_generic').show();
					}
					//normal login
					else if (resp.responseText == 3) {
						$('#div_signin_text').html('Loading...');
						location.href = 'aa_home.php';
					}
					else {
						$('#div_signin_text').html(resp.responseText);
						$('#div_signin').show();
						alert(resp.responseText);
					}
				}
			}
		);
	}
	else {
		errors = "Please fix the following errors:\n" + errors;
		alert(errors);
	}
}


function tour_login() {
	$('#frm_signin_username').attr("value", "guest123");
	$('#frm_signin_password').attr("value", "signmeup");
	check();
}


function generic() {
	var errors = "";

	//trim the username
	$('#frm_generic_username').attr("value", jQuery.trim($('#frm_generic_username').attr("value")));


	var re = /^[a-zA-Z0-9\-\_]{6,20}$/;
	if (!re.test($('#frm_generic_username').attr("value"))) {
		errors += "\n - Invalid username.";
	}

	if (!re.test($('#frm_generic_password').attr("value"))) {
		errors += "\n - Invalid password.";
	}

	if ($('#frm_generic_password').attr("value") != $('#frm_generic_c_password').attr("value")) {
		errors += "\n - The password fields should match.";
	}

	if (!valid_email($('#frm_generic_email').attr("value"))) {
		errors += "\n - Invalid email address.";
	}

	if ($('#frm_generic_email').attr("value") != $('#frm_generic_c_email').attr("value")) {
		errors += "\n - The email fields should match.";
	}

	if ($('#frm_generic_relation').attr("value") == "") {
		errors += "\n - Property association.";
	}

	if (errors == "") {
		$('#div_generic').hide();
		$('#div_signin_text').show();
		$('#div_signin_text').html('Validating...');

		$.ajax(
			{
				type: "POST",
				url: $('#frm_generic').attr("action"),
			 	cache: false,
				data: $('#frm_generic').serialize()+'&org_login='+$('#frm_signin_username').attr("value"),
				complete: function(resp) {
					if (resp.responseText == 1) {
						$('#div_signin_text').html('Validating...');
						//alert("Your account has been setup.");

						//login automatically with the new username and password
						auto_log_in($('#frm_generic_username').attr("value"), $('#frm_generic_password').attr("value"));
					}
					else {
						$('#div_signin_text').html(resp.responseText);
						alert(resp.responseText);
						$('#div_generic').show();
					}
				}
			}
		);
	}
	else {
		errors = "Please fix the following errors:\n" + errors;
		alert(errors);
	}
}



function public() {
	var errors = "";

	//trim the username
	$('#frm_public_username').attr("value", jQuery.trim($('#frm_public_username').attr("value")));


	var re = /^[a-zA-Z0-9\-\_]{6,20}$/;
	if (!re.test($('#frm_public_username').attr("value"))) {
		errors += "\n - Invalid username.";
	}

	if (!re.test($('#frm_public_password').attr("value"))) {
		errors += "\n - Invalid password.";
	}

	if ($('#frm_public_password').attr("value") != $('#frm_public_c_password').attr("value")) {
		errors += "\n - The password fields should match.";
	}

	if (!valid_email($('#frm_public_email').attr("value"))) {
		errors += "\n - Invalid email address.";
	}

	if ($('#frm_public_email').attr("value") != $('#frm_public_c_email').attr("value")) {
		errors += "\n - The email fields should match.";
	}

	if ($('#frm_public_relation').attr("value") == "") {
		errors += "\n - Property association.";
	}

	if (errors == "") {
		$('#div_public').hide();
		$('#div_signin_text').show();
		$('#div_signin_text').html('Validating...');

		$.ajax(
			{
				type: "POST",
				url: $('#frm_public').attr("action"),
			 	cache: false,
				data: $('#frm_public').serialize()+'&org_login='+$('#frm_signin_username').attr("value"),
				complete: function(resp) {
					if (resp.responseText == 1) {
						$('#div_signin_text').html('Validating...');
						//alert("Your account has been setup.")

						//login automatically with the new username and password
						auto_log_in($('#frm_public_username').attr("value"), $('#frm_public_password').attr("value"));
					}
					else {
						$('#div_signin_text').html(resp.responseText);
						alert(resp.responseText);
						$('#div_public').show();
					}
				}
			}
		);
	}
	else {
		errors = "Please fix the following errors:\n" + errors;
		alert(errors);
	}
}


function auto_log_in(username, password) {
	$('#frm_signin_username').attr("value", username);
	$('#frm_signin_password').attr("value", password);

	check();
}
