// 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;
}


var flagPassword = 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 (password.value.length > 0 && password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
    if (!(password.value == password.defaultValue && password2.value == password2.defaultValue && flagPassword)){
    if(!checkpwd(password.value)) { return Wrong(password, "Your password must be 6 characters including 1 number. Passwords cannot have spaces or the following characters: !@#$%^&*()<>/=+.");} }
    if (password.value.length > 0 && 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 checkpassword(pwd) {
 if(pwd.length<6) return false;
 
 var regExp = new RegExp(/['"<>`%#]/g);
 var regExp2 = new RegExp(/\d/g);
 return (!regExp.test(pwd) && regExp2.test(pwd));
}

function checklogin(login) {
 if(login.length<4) return false;
 var regExp = new RegExp(/['"<>`%#]/g);
 return (!regExp.test(login));
}

function check1stepNew(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 (!checklogin(login.value)) { return Wrong(login, "User name must have at least 4 characters. Characters ', \", <, >, `, %, # are not allowed."); }
		if (password.value.length > 0 && password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
    		if (!(password.value == password.defaultValue && password2.value == password2.defaultValue && flagPassword)){
		if (!checkpassword(password.value)) { return Wrong(password, "Your password must have at least 6 characters including 1 number. Characters ', \", <, >, `, %, # are not allowed.");} }
		if (password.value.length > 0 && 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 (password.value.length > 0 && password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
    if (!(password.value == password.defaultValue && password2.value == password2.defaultValue && flagPassword)){
    if(!checkpwd(password.value)) { return Wrong(password, "Your password must be 6 characters including 1 number. Passwords cannot have spaces or the following 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 check_update2(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 (password.value.length > 0 && password.value.length < 6) { return Wrong(password, "Password must have 6 characters."); }
    if (!(password.value == password.defaultValue && password2.value == password2.defaultValue && flagPassword)){
    if(!checkpwd(password.value)) { return Wrong(password, "Your password must be 6 characters including 1 number. Passwords cannot have spaces or the following 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 obj.submit();
}





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 onKeyDownPassword(obj1,obj2){
 if (flagPassword) { obj1.value = '';  obj2.value = '';   }
 flagPassword = false;
}
function checkpwd(pwd) {
 if(pwd.length<6) return false; 
 var regExp = new RegExp(/\W/g);
 var regExp2 = new RegExp(/\d/g);
 return (!regExp.test(pwd) && regExp2.test(pwd));
}

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 setStep(_step) {
	document.getElementById("step_in").value = _step;
}

