//해당 객체의 val()메서드로 추출된는 값이 바뀔때마다 숫자가 아니면 초기화한다.
function SetOnlyNumber(obj) {
	$(obj).bind("keyup", function () {
		if (isNaN($(this).val())) {
			$(this).val("");
			return false;
		}
	});
	$(obj).bind("blur", function () {
		if (isNaN($(this).val())) {
			$(this).val("");
			return false;
		}
	});
	$(obj).bind("keydown", function () {
		if (isNaN($(this).val())) {
			$(this).val("");
			return false;
		}
	});
}



//이메일 선택자 기능용 함수 (빈값이면 목표 개체의 readonly속성을 제거)
function setEmail(obj, target) {

	if (!obj.value) {
		$(target).attr("readonly", false);
		$(target).val("");
		target.focus();
	}
	else {
		$(target).attr("readonly", true);
		$(target).val(obj.value);
	}
}



//이미지 리사이징
//페이지 내의 모든 img중 cls값과 일치하는 class값을 가진 이미지를 width, height 영역 안으로 비율에 맞춰 축소시킨다.
//width, height값은 최소 1보다 커야한다.
//로딩중 페이지 틀어짐을 막기위해 div로 감싸주는것이 좋다
function ImageResize(cls, width, height) {
	if (width == null || width <= 1 || height == null || height <= 1) {
		alert("[ImageResize()] width, height값은 반드시 1이상의 값이 들어가야합니다.");
		return false;
	}

	if (cls == null || cls == "") {
		alert("[ImageResize()] cls값이 필요합니다.");
		return false;
	}

	var images = $("." + cls);

	images.each(function() {
		if (this.nodeName.toUpperCase() == "IMG") {
			var imgWidth;
			var imgHeight;
			var widthRatio;
			var heigthRatio;
			var resizeRatio;

			if (this.style.width) {
				imgWidth = parseInt(this.style.width);
			}
			else {
				imgWidth = parseInt(this.width);
			}

			if (this.style.height) {
				imgHeight = parseInt(this.style.height);
			}
			else {
				imgHeight = parseInt(this.height);
			}

			widthRatio = width / imgWidth;
			heigthRatio = height / imgHeight;

			if (widthRatio > heigthRatio) {
				resizeRatio = heigthRatio;
			}
			else {
				resizeRatio = widthRatio;
			}

			this.style.width = (imgWidth * resizeRatio) + "px";
			this.style.height = (imgHeight * resizeRatio) + "px";
		}
	});

}


//객체 속성 지정
function addAttr(obj, attrName, attrValue) {
	$(obj).attr(attrName, $(obj).attr(attrName) + " " + attrValue);
}



//지정한 class값을 가지는 체크박스를 클릭된 체크박스 또는 라디오버튼 상태대로 변경 (.NET 체크박스용)
function checkAll(obj, targetClass) {
	$("." + targetClass).each(function () {
		$(this).attr("checked", obj.checked);
	});
}



//text박스에 기본 문자열 삽입
function SetDefaultText(obj, defaultValue) {
	if (!obj.value) {
		obj.value = defaultValue;
	}

	$(obj).bind("focus", function () {
		if (!obj.value || obj.value == defaultValue) {
			this.value = "";
		}
	});

	$(obj).bind("blur", function () {
		if (!obj.value) {
			this.value = defaultValue;
		}
	});
}


//이미지 파일버튼 생성
function SetImageFileButton(targetObj, fileObjId, txtObjId) {
	var fileObj = $("#" + fileObjId);
	var txtObj = $("#" + txtObjId);

	fileObj.css("cursor", "pointer");
	fileObj.css("width", "10px");
	fileObj.css("height", "10px");
	fileObj.css("position", "absolute");
	fileObj.css("border", "0px solid");
	fileObj.css("fileter", "alpha(opacity=0)");
	fileObj.css("opacity", "0");
	fileObj.css("-moz-opacity", "0");

	$(targetObj).bind("mouseover", function (e) {
		fileObj.css("left", e.pageX - 7);
		fileObj.css("top", e.pageY - 7);
	});

	$(targetObj).bind("mousemove", function (e) {
		fileObj.css("left", e.pageX - 5);
		fileObj.css("top", e.pageY - 5);
	});

	if (txtObj) {
		document.getElementById(fileObjId).onchange = function () {
			txtObj.val(this.value);
		};
	}
}



//입력된 객체의 text값을 html값으로 변경
function TextToHtml(obj) {
	obj = $(obj);
	obj.html(obj.text());
}



//입력된 클래스 값을 가지는 컨트롤에 포커스 지정.
function SetFocus(css) { 
	$("." + css).each(function(i) { if (i == 0) this.focus(); });
}


//선택된 객체의 DOM노드 트리구조상 모든 자식객체 리스트 반환
function GetAllChildObjs(obj) {
	var returnList = new Array();

	var childrens = $(obj).children();

	$(childrens).each(function () {
		if (this.nodeType == 1) {
			returnList = returnList.concat(this);
			returnList = returnList.concat(GetAllChildObjs(this));
		}
		
	});

	return returnList;
}

//배열 Concat함수 재정의
Array.prototype.concat = function (addTarget) {
	var ori = this;
	var returnArr = new Array();
	var i = 0;

	$(ori).each(function () {
		returnArr[i] = this;
		i++;
	});

	$(addTarget).each(function () {
		returnArr[i] = this;
		i++;
	});

	return returnArr;
}


//같은 이름값을 가지는 checkbox 객체 전체선택 기능 삽입
function SetCheckAllButton(obj) {
	var targets = $("[name=" + $(obj).attr("name") + "]");

	$(obj).click(function () {
		targets.each(function () {
			if ($(this).attr("type").toLowerCase() == "checkbox") {
				$(this).attr("checked", obj.checked);
			}
		});
	});
}
