function Regist(job) {

	this.messageDiv = "";
	this.valid = false;
}

function makeBirthday () {
	
	var birthyear = $("birthyear").value;
	var birthmonth = $("birthmonth").value;
	var birthday = $("birthday").value;
	var lunarcheck = $("lunarcheck").value;
	
	var fullday = "";
	
	if (birthyear != "" && birthmonth != "" && birthday != "") {
		fullday = birthyear+birthmonth + birthday;
		var birth = Date.parseExact(fullday,"yyyyMd");
		
		fullday = birth.toString("yyyyMMdd");
		
		fullday += lunarcheck;
	}
	
	return fullday;

}

Regist.prototype = {

	checkAll: function(){
		var valid = true;
		var checkObj = null;
		
		var job = $("job").value;
		
		if (job == "regist") {
			checkObj = new Array("userID", "email", "password", "password_confirmation");
		} else if (job == "modify") {
			checkObj = new Array("email", "oldpassword", "password", "password_confirmation");
		}
		
		for (var i=0; i < checkObj.length ; i++) {
			var e = $(checkObj[i]);
			e.onkeyup && e.onkeyup();
			var j = 1 + 1;
			
			valid = valid && e.valid;
			if ( e.valid == undefined )
				valid = true;

			if (!valid) 
			{
				e.focus();
				return false;
			}
		}
		
		if (!valid) return false;
		
		if (job == "regist") {
			if (!$("agree").checked) {
				alert('약관에 동의하셔야 합니다.');
				return false;
			}
		}
		
		$("fullday").value = makeBirthday();
		return true;
	},
	
	checkValidUserid: function(e){
		//alert(e);
		
		if (this.value.length == 0) {
			return;
		}
		
		var b = /^[0-9a-z]{4,16}$/i.test(this.value);
		var Validator = new Regist.Validation(Regist.Validation.UserID);
		
		if (b) {
			var callParms = [];
			callParms.push("userBean.userID=" + encodeURIComponent(this.value));
			
			var ajaxHelper = new net.ContentLoader(Validator, Validator.url, "POST", [], true);
			ajaxHelper.sendRequest.apply(ajaxHelper, callParms);
			return false;
		} else {
			if (this.value.length > 16) {
				Validator.tooLong();
			}
			else {
				Validator.inValidId();
			}
		}
	},
	
	checkValidNickName: function(){
		
		var Validator = new Regist.Validation(Regist.Validation.NickName);
		
		var job = $("job").value;
		
		if (this.value.length > 0) {
			var b = this.value.length >= 2;
			if (b) {
				var callParms = [];
				callParms.push("userBean.nickName=" + encodeURIComponent(this.value));
				callParms.push("job=" + job);

				var ajaxHelper = new net.ContentLoader(Validator, Validator.url, "POST", [], true);
				ajaxHelper.sendRequest.apply(ajaxHelper, callParms);
				return false;
			}
			else Validator.inValidNickName();
		}
		else Validator.off();
	},
	
	checkOldPasswd: function(){
		
		var b = this.value.length == 0;
		
		if (b) {
			$("message_oldpassword").className = 'check_off;'
			$("message_oldpassword").innerHTML = "";
			this.valid = b;
			return;			
		}
		
		b = this.value.length >= 6;

		if (!b) {
			$("message_oldpassword").className = 'check_no';
			$("message_oldpassword").innerHTML = '<sapn> 비밀번호가 짧습니다. 6자리 이상 가능합니다 </span>';
			this.valid = b;
			return;
		}
		
		var Validator = new Regist.Validation(Regist.Validation.OldPassword);
		
		var callParms = [];
		callParms.push("userBean.userID=" + $("userID").value);
		callParms.push("userBean.password=" + this.value);
		
		var ajaxHelper = new net.ContentLoader(Validator, Validator.url, "POST", [], true);
		ajaxHelper.sendRequest.apply(ajaxHelper, callParms);
		return false;
	},
	
	checkValidPasswd: function(){
		var job = $("job").value;
		
		if (job == "regist") {
			var b = this.value.length >= 6;
			
			$("message_password").className = 'check' + (b ? '_ok' : '_no');
			$("message_password").innerHTML = b ? '<sapn> 사용가능합니다.</span>' : '<sapn> 비밀번호가 짧습니다. 6자리 이상 가능합니다 </span>';
			this.valid = b;
		} else if (job == "modify") {
			var b = this.value.length == 0;
			
			if (b) {
				$("message_password").className = 'check_off;'
				$("message_password").innerHTML = "";
				this.valid = b;
				return;
			}
			
			b = this.value.length >= 1;
			if (b) {
				if ($("oldpassword").length <= 6 || !$("oldpassword").valid) {
					$("message_password").className = 'check_no';
					$("message_password").innerHTML = '<sapn> 현재 비밀번호를 먼저 확인해 주세요.</span>';
					this.valid = false;
					return;
				}
			}
					

			b = this.value.length >= 6;
			
			$("message_password").className = 'check' + (b ? '_ok' : '_no');
			$("message_password").innerHTML = b ? '<sapn> 사용가능합니다.</span>' : '<sapn> 비밀번호가 짧습니다. 6자리 이상 가능합니다 </span>';
			this.valid = b;
		}
	},
	
	checkValidPasswdEquality: function(){
		var job = $("job").value;
		
		if (job == "regist") {
			var b = this.value.length >= 6 && this.value == $("password").value;
			$("message_password_confirmation").className = 'check' + (b ? '_ok' : '_no');
			$("message_password_confirmation").innerHTML = b ? '<sapn> 사용가능합니다.</span>' : '<sapn> 두 비밀번호가 서로 같지 않습니다.</span>';
			this.valid = b;
		} else if (job == "modify") {
			var b = this.value.length == 0 && this.value == $("password").value; 
			
			if (b) {
				$("message_password_confirmation").className = 'check_off;'
				$("message_password_confirmation").innerHTML = "";
				this.valid = b;
				return;
			} 
			
			b = this.value.length >= 6 && this.value == $("password").value;
			
			$("message_password_confirmation").className = 'check' + (b ? '_ok' : '_no');
			$("message_password_confirmation").innerHTML = b ? '<sapn> 사용가능합니다.</span>' : '<sapn> 두 비밀번호가 서로 같지 않습니다.</span>';
			this.valid = b;
						
		}
	},
	
	checkValidEmail: function(){
		var b = /^[\w.-]+@\w[\w-]+(\.\w[\w-]+)+$/i.test(this.value);
		
		if (!b) {
			$("message_email").className = 'check' + (b ? '_ok' : '_no');
			$("message_email").innerHTML = b ? '<sapn> 사용가능합니다.</span>' : '<sapn> 이메일 형식을 맞추어 주세요.</span>';
			this.valid = b;
			return;
		}
		
		var Validator = new Regist.Validation(Regist.Validation.Email);
		
		var job = $("job").value;
		
		var callParms = [];
		callParms.push("userBean.userID=" + $("userID").value);
		callParms.push("userBean.email=" + this.value);
		callParms.push("job=" + job);
		
		var ajaxHelper = new net.ContentLoader(Validator, Validator.url, "POST", [], true);
		ajaxHelper.sendRequest.apply(ajaxHelper, callParms);
		return false;
				
		this.valid = b;
	}
}

Regist.Validation = function(type) {
	this.type		= type;
	this.url		= "";
	this.messageDiv	= "";
	this.Input		= "";
	
	if (this.type == Regist.Validation.UserID) {
		this.url 		= "/service/checkExistUser.action";
		this.messageDiv = $("message_userID");
		this.Input		= $("userID");
	} else if (this.type == Regist.Validation.NickName) {
		this.url = "/service/checkExistUser.action";
		this.messageDiv = $("message_nickName");
		this.Input		= $("nickName");
	} else if (this.type == Regist.Validation.OldPassword) {
		this.url = "/service/checkOldPasswd.action";
		this.messageDiv = $("message_oldpassword");
		this.Input		= $("oldpassword");
	} else if (this.type == Regist.Validation.Password) {
		this.messageDiv = $("message_password");
		this.Input		= $("password");
	} else if (this.type == Regist.Validation.Email) {
		this.messageDiv = $("message_email");
		this.Input		= $("email");
		this.url = "/service/checkEmail.action";
	}
};

Regist.Validation.UserID = "USERID";
Regist.Validation.NickName = "NickName";
Regist.Validation.Password = "Password";
Regist.Validation.OldPassword = "OldPassword";
Regist.Validation.Email 	= "Email";

Regist.Validation.prototype = {
	
	off : function() {
		this.messageDiv.className = 'check2_off';
	},
	
	tooLong : function() {
		this.messageDiv.className = 'check_ing';
		this.messageDiv.innerHTML = '<sapn>아이디는 4-16자 입니다.</span>';
		this.Input.valid = false;		
	},
	
	inValidId : function() {
		this.messageDiv.className = 'check_ing';
		this.messageDiv.innerHTML = '<sapn>아이디는 영문자와 숫자만 가능합니다.</span>';
		this.Input.valid = false;
	},
	
	inValidNickName : function() {
		this.messageDiv.className = 'check_ing';
		this.messageDiv.innerHTML = '<sapn>닉네임은 두자이상이어야 합니다.</span>';
		this.Input.valid = false;
	},
	
	ajaxUpdate : function(request) {
		
		var response = {};
		
		var xmlDoc = request.responseXML;
		var result = xmlDoc.getElementsByTagName('resultStatus');
		if (result.length > 0) { response.status = result[0].firstChild.data;}
		
		result = xmlDoc.getElementsByTagName('message');
		if (result.length > 0) {
			response.message = '';
			if (result[0].firstChild) {
				response.message = result[0].firstChild.data;
			}
		}
		this.viewResult(response);	
	},
	
	viewResult : function(response) {
		
		if (this.type == Regist.Validation.UserID) {
			if (response.status == "true") {
				this.messageDiv.className = 'check_no';
				this.messageDiv.innerHTML = '<sapn>이미 존재하는 아이디 입니다.</span>';
				this.Input.valid = false;
			} else {
				this.messageDiv.className = 'check_ok';
				this.messageDiv.innerHTML = '<sapn>사용가능한 아이디입니다.</span>';
				this.Input.valid = true;
			}
		} else if (this.type == Regist.Validation.NickName) {
			if (response.status == "true") {
				this.messageDiv.className = 'check_no';
				this.messageDiv.innerHTML = '<sapn>이미 존재하는 닉네임입니다.</span>';
				this.Input.valid = false;
			} else {
				this.messageDiv.className = 'check_ok';
				 this.messageDiv.innerHTML = '<sapn>사용가능한 닉네임입니다.</span>';
				 this.Input.valid = true;
			}
		} else if (this.type == Regist.Validation.OldPassword) {
			if (response.status == "true") {
				this.messageDiv.className = 'check_ok';
				this.messageDiv.innerHTML = '<sapn>비밀번호가 일치합니다.</span>';
				this.Input.valid = true;
			} else {
				this.messageDiv.className = 'check_no';
				 this.messageDiv.innerHTML = '<sapn>비밀번호가 일치하지 않습니다.</span>';
				 this.Input.valid = false;
			}			
		} else if (this.type == Regist.Validation.Email) {
			if (response.status == "true") {
				this.messageDiv.className = 'check_no';
				this.messageDiv.innerHTML = '<sapn>이미 존재하는 이메일 주소입니다.</span>';
				this.Input.valid = false;
			} else {
				this.messageDiv.className = 'check_ok';
				 this.messageDiv.innerHTML = '<sapn>사용가능합니다.</span>';
				 this.Input.valid = true;
			}			
		}
	},
	
	ajaxUpdating : function() {
		this.messageDiv.className = 'check_ing';
		this.innerHTML = '<sapn>잠시만 기다려 주세요.</span>';
	}
}



