Event.observe(window, 'load', init, false);

var input_id_ok 		= false;
var input_email_ok		= false;
var input_password_ok	= false;
var input_password2_ok	= false;

function init() {
	if ($('signup_form'))
	{
		$('signup_form').onsubmit = function() { return check_inputs_on_submit(); };
		Event.observe($('check_id_button'),	'click', ajax_check_id, false);
		Event.observe($('id'), 				'blur', check_id, false);
		Event.observe($('email'), 			'blur', check_email, false);
		Event.observe($('password'), 		'blur', check_password, false);
		Event.observe($('password2'), 		'blur', check_password2, false);
		Event.observe($('verify_code'),		'blur', check_verify_code, false);
	}
}

function check_inputs_on_submit() {
	input_id_ok				= check_id();
	input_email_ok			= check_email();
	input_password_ok		= check_password();
	input_password2_ok		= check_password2();
	input_2_passwords_ok	= check_two_passwords();
	input_verify_code_ok	= need_verify_code?check_verify_code():true;
	
	input_ok = (input_id_ok && input_email_ok && input_password_ok && input_password2_ok && input_2_passwords_ok && input_verify_code_ok);
	if (input_ok) {
		hash_password();
		$('password').value = '';
		$('password2').value = '';
	}
	return input_ok;
}

function hash_password() {
	password = $F('password');
	$('password_hash').value = hex_md5(password);
}

function check_id() {
	input_id_ok = false;
	id = trim($F('id'));
	if (id.length == 0) {
		msg = "id is empty";
	} else if (id.length > id_max_length) {
		msg = 'id is too long';
	} else if (id.length < id_min_length) {
		msg = 'id is too short';
	} else {
		msg = '';
		input_id_ok = true;
	}
	setErrMsg('err_msg_id', msg, !input_id_ok);
	return input_id_ok;
}

function check_email() {
	input_email_ok = false;
	email = trim($F('email'));
	if (email.length == 0) {
		msg = "email is empty";
	} else if (!is_email_format_valid(email)) {
		msg = "email format invalid";
	} else {
		input_email_ok = true;
		msg = '';
	}
	setErrMsg('err_msg_email', msg, !input_email_ok);
	return input_email_ok;
} 

function ajax_check_id() {
	id = $F('id');
	//url = '/ws_check_signup_id.php?id='+id;
	url = '/api/signup/helper/check_signup_user_id/'+id;
	pars = null;
	var id_success = function(t) { ajax_check_id_success(t); }
	var id_failure = function(t) { ajax_check_id_failure(t); }
	var ajax = new Ajax.Request(url, {method:'get', postBody:pars, onSuccess:id_success, onFailure:id_failure});
	return false;
}

function ajax_check_email() {
	email = $F('email');
	//url = '/ws_check_signup_email.php?email='+email;
	url = '/api/signup/helper/check_signup_user_email/'+email;
	pars = null;
	var email_success = function(t) { ajax_check_email_success(t); }
	var email_failure = function(t) { ajax_check_email_failure(t); }
	var ajax = new Ajax.Request(url, {method:'get', postBody:pars, onSuccess:email_success, onFailure:email_failure});
	return false;
}

function ajax_check_id_success(t) {
	input_id_ok = false;
	var id = 'err_msg_id';
	var msg = '';
	result = t.responseText;
	//alert(result);
	if (result =='') {
		msg = 'ID is okay';
		input_id_ok = true;
	} else if (result == 'exists') {
		msg = 'ID already exists';
	} else if (result == 'err_invalid_chars') {
		msg = 'ID contains invalid chars';
	} else if (result == 'err_too_short') {
		msg = 'ID is too short';
	} else if (result == 'err_too_long') {
		msg = 'ID is too long';
	}
	setErrMsg(id, msg, !input_id_ok);
	return input_id_ok;
}

function ajax_check_id_failure(t) {
	//alert(t.responseText);
}

function ajax_check_email_success(t) {
	var msg = '';
	input_email_ok = false;
	msg_element = 'err_msg_email';
	result = t.responseText;
	if (result =='not_exists') {
		msg = '';
		input_email_ok = true;
	} else if (result == 'exists') {
		msg = 'the email is already registered';
	} else if (result == 'invalid_email_format') {
		msg = 'Invalid format';
	}
	setErrMsg(msg_element, msg, !input_email_ok);
	return input_email_ok;
}

function ajax_check_email_failure(t) {
}

function check_password() {
	input_password_ok = false;
	pwd = $F('password');
	if (pwd.length == 0) {
		msg = 'password is empty';
	} else if (pwd.length < password_min_length) {
		msg = 'password is too short';
	} else {
		input_password_ok = true;
		msg = '';
	}
	setErrMsg('err_msg_password', msg, !input_password_ok);
	return input_password_ok;
}

function check_password2() {
	input_password2_ok = false;
	pwd = $F('password2');
	if (pwd.length == 0) {
		msg = 'password is empty';
	} else if (pwd.length < password_min_length) {
		msg = 'password is too short';
	} else {
		input_password2_ok = true;
		msg = '';
	}
	setErrMsg('err_msg_password2', msg, !input_password2_ok);
	return input_password2_ok;
}

function check_two_passwords() {
	input_2_passwords_ok = false;
	pwd1 = $F('password');
	pwd2 = $F('password2');
	if (pwd1 != pwd2) {
		msg = "the two passwords are different";	
	} else {
		input_2_passwords_ok = true;
		msg = '';
	}
	setErrMsg('err_msg_password2', msg, !input_2_passwords_ok);
	return input_2_passwords_ok;
}

function check_verify_code() {
	input_verify_code_ok = false;
	code = $F('verify_code');
	if (code.length == 0) {
		msg = 'Verify code is empty';
	} else {
		input_verify_code_ok = true;
		msg = '';
	}
	setErrMsg('err_msg_verify_code', msg, !input_verify_code_ok);
	return input_verify_code_ok;
}

function setErrMsg(id, msg, isError) {
	$(id).innerHTML = msg;
	if (isError) {
		Element.addClassName(id, 'input_err');
		Element.removeClassName(id, 'input_ok');
	} else {
		Element.addClassName(id, 'input_ok');
		Element.removeClassName(id, 'input_err');
	}
	Element.show(id);
}

