﻿$(document).ready(function () {
	//DefaultFocus 속성을 가진 개체를 찾아 첫번째 개체만 포커스 지정 (없어도 상관없음)
	try {
		SetFocus("DefaultFocus");

	}
	catch (e) {
		setTimeout("SetFocus('DefaultFocus')", 1000);
	}


	//jsIsOnlyNumber 속성을 가지는 input text개체를 찾아 값변경시 숫자여부 체크
	$(".jsIsOnlyNumber[type=text]").each(function () {
		var numCheckExp = new RegExp(/[0-9]/gi);

		$(this).keyup(function () {
			if (this.value.replace(numCheckExp, "")) {
				this.value = "";
			}
		});

		$(this).blur(function () {
			if (this.value.replace(numCheckExp, "")) {
				this.value = "";
			}
		});
	});


	//jsActivateImage 클래스를 가지는 이미지의 mouseOver, mouseOut이벤트시에 이미지 경로값 변경 (_o를 붙이거나 뗀다)
	$(".jsActivateImage").each(function () {

		if (this.nodeName.toLowerCase() == "img") {
			$(this).mouseover(function () {
				$(this).attr("src", $(this).attr("src").split(".")[0] + "_o.gif");
			});

			$(this).mouseout(function () {
				$(this).attr("src", $(this).attr("src").split(".")[0].substring(0, $(this).attr("src").split(".")[0].length - 2) + ".gif");
			});
		}
	});


	//jQuery Ajax 에러발생시 기본 처리기
	$(document).ajaxError(function (e, jqxhr, settings, exception) {
		alert("시스템 오류로 요청 처리 실패\n - errorType : " + exception + "\n - status : " + jqxhr.status + "\n - errMsg : " + jqxhr.statusText);
	});


	//form객체 내부에 있는 jsSubmitObj 클래스를 가지는 모든 객체에 Submit 유발 trigger 추가
	$("form").each(function () {
		var children = $(GetAllChildObjs(this)).filter(".jsSubmitObj");
		var targetfo = this;

		children.each(function () {
			//input type=text, type=password, textarea일때만 엔터키이벤트 추가
			if ($(this).attr("type") == "text" || $(this).attr("type") == "password" || this.nodeName.toLowerCase() == "textarea") {
				$(this).bind("keydown", function (e) {
					if (e.which == 13) {
						e.preventDefault();
						$(targetfo).trigger("submit");
					}
				});
			}
			//select 객체는 change 이벤트 발생시에 submit
			else if (this.nodeName.toLowerCase() == "select") {
				$(this).bind("change", function () {
					$(targetfo).trigger("submit");
				});
			}
			else {
				$(this).bind("click", function () {
					$(targetfo).trigger("submit");
				});
			}
		});
	});


	//label태그중 for속성을 가지고 있는 모든 객체에 cursor형태 CSS추가
	$("label").each(function () {
		if ($(this).attr("for")) {
			$(this).css("cursor", "pointer");
		}
	});


	//checkbox중 jsCheckAllButton속성을 가지는 객체에 전체선택기능 삽입
	$(".jsCheckAllButton").each(function () {
		SetCheckAllButton(this);
	});
});

//jQuery Ajax 기본속성 정의
$.ajaxSetup({
	type: 'POST'
	, dataType: 'JSON'
	, cache: false
});

//jQuery ajaxSubmit 기본 옵션 개체 정의
var ajaxSubmitTargetForm;
var ajaxSubmitOptions = {
	type: "POST", dataType: "JSON"
	, beforeSubmit: function (arr, form, options) {
		ajaxSubmitTargetForm = form;
		if (form.attr("ProcIng")) {
			return false;
		}
		else {
			form.attr("ProcIng", true);
		}
	}
	, complete: function () {
		$(ajaxSubmitTargetForm).attr("ProcIng", "");
	}
	, error: function () {
		alert(ajaxSubmitTargetForm.html());
		$(ajaxSubmitTargetForm).attr("ProcIng", "");
	}
}
