/* Validator */
var validator = function() {};

validator = {
	checkPeriod:function(oForm, beginDate, endDate)
	{
		if (beginDate == undefined)
			beginDate = 'begin_date';

		if (endDate == undefined)
			endDate = 'end_date';

		var strBeginDate = g(beginDate).value;
		var strEndDate = g(endDate).value;

		if (strBeginDate == "")
			strBeginDate = g(beginDate).value = g(endDate).value;

		if (strEndDate == "")
			strEndDate = g(endDate).value = g(beginDate).value;

		var arrBeginDate = strBeginDate.split("-");
		var arrEndDate = strEndDate.split("-");

		for (var k in arrBeginDate)
			arrBeginDate[k] = arrBeginDate[k] * 1;

		for (var k in arrEndDate)
			arrEndDate[k] = arrEndDate[k] * 1;

		if (arrBeginDate[0] > arrEndDate[0] || 
			(arrBeginDate[0] == arrEndDate[0] && arrBeginDate[1] > arrEndDate[1]) || 
			(arrBeginDate[0] == arrEndDate[0] && arrBeginDate[1] == arrEndDate[1] && arrBeginDate[2] > arrEndDate[2]) )
		{
			alert(sn2nl(loadString('J1001')));
			return false;
		}

		return true;
	},
	list:function(oForm)
	{
		return true;
	},
	importForm:function(oForm)
	{
		var arrFiles = uploader.GetUploadFiles();

		if (arrFiles.length < 1)
		{
			alert(loadString('J4028'));
			return false;
		}

		if (uploader.IsUploadingStatus())
		{
			alert(sn2nl(loadString('J4007')));
			return false;
		}

		var oTmpForm = new AnonymForm();
		oTmpForm._form = oForm;

		for (var i = 0; i < arrFiles.length; i++)
			oTmpForm.setValue('package[' + i + ']', arrFiles[i].name);

		return true;
	},
	form:function(oForm)
	{
		if (oForm.bbsID.value.toString().trim() == "")
		{
			alert(sn2nl(loadString('J4029')));
			oForm.bbsID.focus();
			return false;
		}
		else if (!/^[a-zA-Z0-9\-_]+$/.test(oForm.bbsID.value))
		{
			alert(loadString('J4015'));
			oForm.bbsID.focus();
			return ;
		}

		if (oForm.name.value.toString().trim() == "")
		{
			alert(sn2nl(loadString('J4030')));
			oForm.name.focus();
			return false;
		}

		if (oForm.mode.value == "insert")
		{
			var bbsType = getRadioValue(oForm.bbs_type);

			// 일반 게시판이거나 설문조사 게시판이라면, 화면 당 게시물 표시 수를 입력해야 합니다.
			if (bbsType == "normal" || bbsType == "poll")
			{
				if (oForm.page_view.value == "")
				{
					alert(loadString('J4031'));
					oForm.page_view.focus();
					return false;
				}

				if (!isNumber(oForm.page_view.value))
				{
					alert(loadString('J4032'));
					oForm.page_view.focus();
					return false;
				}
			}
		}

		return true;
	},
	categoryForm:function(oForm)
	{
		if (oForm.category_name.value.toString().trim() == "")
		{
			alert(loadString('J4033'))
			oForm.category_name.focus();
			return false;
		}

		return true;
	},
	topicForm:function(oForm)
	{
		// 비 로그인 사용자에게는 이름/패스워드 체크
		if (isLogined == false)
		{
			if (oForm.author.value.toString().trim() == "")
			{
				alert(loadString('J4034'));
				oForm.author.focus();
				return false;
			}

			if (oForm.passwd.value.toString().trim() == "")
			{
				if (oForm.mode.value == "insert")
				{
					alert(loadString('J4035'));
				}
				else
				{
					alert(loadString('J4036'));
				}

				oForm.passwd.focus();
				return false;
			}
		}

		if (oForm.title.value.toString().trim() == "")
		{
			alert(loadString('J4003'));
			oForm.title.focus();
			return false;
		}

		// 카테고리가 있을경우 선택하도록 유도한다.
		if (oForm.bbs_category_id != undefined)
		{
			if (oForm.bbs_category_id.options)
			{
				var opts = oForm.bbs_category_id.options;

				if (opts.length > 1 && oForm.bbs_category_id.value == '')
				{
					alert('카테고리를 선택해 주세요');
					return false;
				}
			}
			else
			{
				if (oForm.bbs_category_id.value == '')
				{
					alert('카테고리를 선택해 주세요');
					return false;
				}
			}
		}

		var oTmpForm = new AnonymForm();
		oTmpForm._form = oForm;

		if (in_array(oForm.mode.value, ['insert', 'reply']) && oForm.bbs_type.value == "poll")
		{
			if (oForm.begin_date.value == "" || oForm.due_date.value == "")
			{
				alert(loadString('J4037'));
				return false;
			}

			if (this.checkPeriod(oForm, 'begin_date', 'due_date') == false)
				return false;

			var arrItems = getInputValues('poll_item');

			if (arrItems.length < 2)
			{
				alert(loadString('J4038'));
				return false;
			}
		}

		// 음성 게시판의 경우, 음성 녹음 여부 확인
		if (oForm.bbs_type.value == 'voice')
		{
			if (voiceRecord.getFilePath() == '')
			{
				alert(loadString('J4039'));
				return false;
			}

			if (!voiceRecord.postMp3())
			{
				alert(loadString('J4040'));
				return false;
			}
		}

		if (uploader.IsUploadingStatus())
		{
			alert(sn2nl(loadString('J4007')));
			return false;
		}

		// 플래시 업로더를 통해 업로드된 파일을 hidden 값으로 전달한다.
		var arrFiles = uploader.GetUploadFiles();

		if (arrFiles.length > 0)
		{
			for (var i = 0; i < arrFiles.length; i++)
			{
				oTmpForm.setValue('upload[' + i + ']', arrFiles[i].name);
			}
		}

		// 게시물 열람자 제한 기능
		if (g('use_readerable_user_list'))
		{
			var arrUser = readerableListCtl.getUserIdList();

			for (var i = 0; i < arrUser.length ; i++ )
			{
				oTmpForm.setValue('readerable_user_id[' + i + ']', arrUser[i]);
			}
		}

		return true;
	},	
	counselTopicForm:function(oForm)
	{
		// 비 로그인 사용자에게는 이름/패스워드 체크
		if (isLogined == false)
		{
			if (oForm.author.value.toString().trim() == "")
			{
				alert(loadString('J4034'));
				oForm.author.focus();
				return false;
			}

			if (oForm.passwd.value.toString().trim() == "")
			{
				if (oForm.mode.value == "insert")
				{
					alert(loadString('J4035'));
				}
				else
				{
					alert(loadString('J4036'));
				}

				oForm.passwd.focus();
				return false;
			}
		}

		if (oForm.title.value.toString().trim() == "")
		{
			alert(loadString('J4003'));
			oForm.title.focus();
			return false;
		}

		var oTmpForm = new AnonymForm();
		oTmpForm._form = oForm;

		if (in_array(oForm.mode.value, ['insert', 'reply']) && oForm.bbs_type.value == "poll")
		{
			if (oForm.begin_date.value == "" || oForm.due_date.value == "")
			{
				alert(loadString('J4037'));
				return false;
			}

			if (this.checkPeriod(oForm, 'begin_date', 'due_date') == false)
				return false;

			var arrItems = getInputValues('poll_item');

			if (arrItems.length < 2)
			{
				alert(loadString('J4038'));
				return false;
			}
		}

		// 음성 게시판의 경우, 음성 녹음 여부 확인
		if (oForm.bbs_type.value == 'voice')
		{
			if (voiceRecord.getFilePath() == '')
			{
				alert(loadString('J4039'));
				return false;
			}

			if (!voiceRecord.postMp3())
			{
				alert(loadString('J4040'));
				return false;
			}
		}

		if (uploader.IsUploadingStatus())
		{
			alert(sn2nl(loadString('J4007')));
			return false;
		}

		// 플래시 업로더를 통해 업로드된 파일을 hidden 값으로 전달한다.
		var arrFiles = uploader.GetUploadFiles();

		if (arrFiles.length > 0)
		{
			for (var i = 0; i < arrFiles.length; i++)
			{
				oTmpForm.setValue('upload[' + i + ']', arrFiles[i].name);
			}
		}

		if(g('readerable_user_id'))
		{
			if (g('readerable_user_id').value == '')
			{
				alert(loadString('J4041'));
				return false;
			}

			// 게시물 열람자 제한 기능
			oTmpForm.setValue('readerable_user_id[]', g('readerable_user_id').value);
		}

		return true;
	},
	replyDeleteForm:function(oForm)
	{
		if (oForm.passwd.value.toString().trim() == "")
		{
			alert(loadString('J4042'));
			oForm.passwd.focus();
			return false;
		}
		
		if (confirm(loadString('J4025')) == false)
			return false;

		return true;
	},
	topicDeleteForm:function(oForm)
	{
		if (oForm.passwd.value.toString().trim() == "")
		{
			alert(loadString('J4043'));
			oForm.passwd.focus();
			return false;
		}
		
		if (confirm(loadString('J4044')) == false)
			return false;

		return true;
	}
};


