// Scripts for registration form management

function checkLoginForm(obj) {
	with(obj) {
		if (login.value.length == 0) { return Wrong(login, "Please enter login."); }
		if (password.value.length == 0) { return Wrong(password, "Please enter password."); }
	}

	return true;
}

function check1step(obj) {
	with(obj) {
		if (first_name.value.length == 0) { return Wrong(first_name, "Please enter your name."); }
		if (last_name.value.length == 0) { return Wrong(last_name, "PLease enter last name."); }
		if (!checkMail(email.value)) { return Wrong(email, "Please enter valid email address."); }
		if (email.value != confirm_email.value) { return Wrong(confirm_email, "Email addresses do not match."); }
		if (login.value.length == 0) { return Wrong(login, "PLease enter your user name."); }
		if (password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
		if (password.value != password2.value) { return Wrong(password2, "Passwords do not match."); }
		if (hint_answer.value.length == 0) { return Wrong(hint_answer, "Please answer the password hint question."); }
	}

	return true;
}

function check_update(obj) {
	with(obj) {
		if (first_name.value.length == 0) { return Wrong(first_name, "Please enter your name."); }
		if (last_name.value.length == 0) { return Wrong(last_name, "PLease enter last name."); }
		if (!checkMail(email.value)) { return Wrong(email, "Please enter valid email address."); }
		if (email.value != confirm_email.value) { return Wrong(confirm_email, "Email addresses do not match."); }
		if (login.value.length == 0) { return Wrong(login, "PLease enter your user name."); }
		if (password.value.length > 0 && password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
		if (password.value.length > 0 && password.value != password2.value) { return Wrong(password2, "Passwords do not match."); }
		if (org_type.value == -1 && org_type_other.value.length == 0) { return Wrong(org_type, "Please enter your organization type."); }
		if (org_type.value == 0 && org_type_other.value.length == 0) { return Wrong(org_type_other, "Please enter your organization type."); }
		if (rank_job.value == -1 && rank_job_other.value.length == 0) { return Wrong(rank_job, "Please enter your rank/job function."); }
		if (rank_job.value == 0 && rank_job_other.value.length == 0) { return Wrong(org_type_other, "Please enter your rank/job function."); }
		if (!checkPhoneNumber(phone.value)) { return Wrong(phone, "Please enter valid phone number."); }
	}

	return true;
}

function check2step(obj) {
	with(obj) {
		if (org_type.value == -1 && org_type_other.value.length == 0) { return Wrong(org_type, "Please enter your organization type."); }
		if (org_type.value == 0 && org_type_other.value.length == 0) { return Wrong(org_type_other, "Please enter your organization type."); }
		if (rank_job.value == -1 && rank_job_other.value.length == 0) { return Wrong(rank_job, "Please enter your rank/job function."); }
		if (rank_job.value == 0 && rank_job_other.value.length == 0) { return Wrong(rank_job_other, "Please enter your rank/job function."); }
		if (!checkPhoneNumber(phone.value)) { return Wrong(phone, "Please enter valid phone number."); }
	}

	return true;
}

function checkMail(mail) {
	var myRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return myRegExp.test(mail);
}

function checkPhoneNumber(phone) {
	var result = (phone != "(000) 000-000");
	for(var i = 0; i < phone.length; i++) {
		if (phone.charAt(i) != '(' && phone.charAt(i) != ')' && phone.charAt(i) != '-' && phone.charAt(i) != ' ' && isNaN(parseInt(phone.charAt(i)))) { result = false; }
	}
	return (result || phone.length == 0);
}

function Wrong(obj, message) {
	alert(message);
	obj.focus();
	return false;
}

function from2to1() {
	setStep(1);
	document.forms.regForm.submit();
}

function from3to2() {
	setStep(2);
	document.forms.regForm.submit();
}

function setStep(_step) {
	document.getElementById("step_in").value = _step;
}
