﻿/*****************************************************************************************
		※ DOM 설정 관련..
*****************************************************************************************/

	//-----------------------------------------------------------------------------
	//					※ 레이어 다이얼로그 Show ※
	//
	//	@input:
	//		- oDialog: 레이어 참조값..
	//		- iWidth: 레이어 넓이값..
	//		- iHeight: 레이어 높이값..
	//-----------------------------------------------------------------------------
	function showDialog( oDialog, iWidth, iHeight ){
		var oDisabledZone = $("__divDisabledZone__");
		
		if( !oDisabledZone ){
			oDisabledZone = document.createElement("div");

			oDisabledZone.id = "__divDisabledZone__";
			oDisabledZone.style.zIndex = 0;
			oDisabledZone.style.left = "0px";
			oDisabledZone.style.top = "0px";
			oDisabledZone.style.width = document.body.scrollWidth;
			oDisabledZone.style.height = document.body.scrollHeight;
			oDisabledZone.style.display = "none";
			oDisabledZone.style.position = "absolute";
			oDisabledZone.style.backgroundColor = "black";
			oDisabledZone.style.opacity = 0.2;
			oDisabledZone.style.MozOpacity = 0.2;
			oDisabledZone.style.KhtmlOpacity = 0.2;
			oDisabledZone.style.filter = "alpha(opacity=20)";

			document.body.appendChild( oDisabledZone );

			window.setTimeout( function(){}, 500 );
		}

		oDisabledZone.style.zIndex = 9000;
		oDisabledZone.style.display = "block";

		oDialog.style.position = "absolute";
		oDialog.style.width = iWidth + "px";
		oDialog.style.height = iHeight + "px";
		oDialog.style.left = ((document.body.scrollWidth / 2) - (iWidth/2)) + "px";
		oDialog.style.top = ((document.body.scrollHeight / 2) - (iHeight)) + "px";

		oDialog.style.zIndex = 9050;
		oDialog.style.display = "block";

		oDialog.focus();
	}


	//-----------------------------------------------------------------------------
	//					※ 레이어 다이얼로그 Hide ※
	//
	//	@input:
	//		- oDialog: 레이어 참조값..
	//-----------------------------------------------------------------------------
	function hideDialog( oDialog ){
		var oDisabledZone = $("__divDisabledZone__");

		oDisabledZone.style.zIndex = oDialog.style.zIndex = -100;
		oDisabledZone.style.display = oDialog.style.display = "none";
	}



	//-----------------------------------------------------------------------------
	//	id에 해당하는 노드의 참조를 얻는다.
	// @return : Object
	//-----------------------------------------------------------------------------
	function $(id){
		return document.getElementById(id);
	}


	//-----------------------------------------------------------------------------
	//	해당 노드의 모든 자식노드를 지운다.
	// @return : null
	// ex) removeAllNodes(부모노드)
	//-----------------------------------------------------------------------------
	function removeAllNodes(argElement){
		if(argElement != null){
			if(argElement.childNodes){
				var Len = argElement.childNodes.length;  
				for (i=0; i<Len; i++){
					argElement.removeChild(argElement.childNodes[0]);
				}
			}
		}
	}


	//-----------------------------------------------------------------------------
	//	해당 노드의 모든 자식노드를 지운다.
	// @return : null
	// ex) removeAllChildNodes(부모노드)
	//-----------------------------------------------------------------------------
	function removeAllChildNodes(obj) {
		if ( obj )
			while(obj.childNodes[0])
				obj.removeChild(obj.childNodes[0]);
		return ;
	}



	//-----------------------------------------------------------------------------
	//	해당 노드의 모든 자식노드를 지우고 새로운 텍스트노드를 추가한다.
	// @return : null
	// ex) replaceTextNode(노드, 추가할문자열)
	//-----------------------------------------------------------------------------
	function replaceTextNode(argElement, argText) {
	  if (argElement != null) {
		var newNode = document.createTextNode(argText);
		removeAllNodes(argElement);
		argElement.appendChild(newNode);
	  }
	}



	//-----------------------------------------------------------------------------
	//	해당 노드의 모든 텍스트를 얻는다.
	// @return : String
	// ex) getText(노드)
	//-----------------------------------------------------------------------------
	function getText(argElement) {
	  var text = "";
	  if (argElement != null) {
		if (argElement.childNodes) {
		  for (var i = 0; i < argElement.childNodes.length; i++) {
			var childNode = argElement.childNodes[i];
			if (childNode.nodeValue != null) {
			  text = text + childNode.nodeValue;
			}
		  }
		}
	  }
	  return text;
	}




	function setOffsets(inputField) {
		var end = inputField.offsetWidth;
		var left = calculateOffset(inputField, "offsetLeft");
		var top = calculateOffset(inputField, "offsetTop") + inputField.offsetHeight;

		completeDiv.style.border = "black 1px solid";
		completeDiv.style.left = left + "px";
		completeDiv.style.top = top + "px";
		autoSearchTable.style.width = end + "px";
	}


	function calculateOffset(field, attr) {
		var offset = 0;

		while(field) {
			offset += field[attr]; 
			field = field.offsetParent;
		}
		
		return offset;
	}




	/*
		isChildNode
			- obj 노드의 자식 노드 중 ID가 id를 가진 노드가 있으면 true 아니면 false
			- ★deep 검색 안됨
	*/
	function isChildNode(obj, id) {
		if ( obj )
			for ( var i = 0; i < obj.childNodes.length; i++ )
				if ( obj.childNodes[i].nodeType == 1 //ELEMENT_NODE:1
				  && obj.childNodes[i].getAttribute('id') == id ) return true;
		return false;
	}


	/*
		setChildNodeDisplay
			- obj 노드의 자식 노드를 숨김/보임
			- option = 0: 1차 하위 노드만 ( default value )
	*/
	function setChildNodesDisplay(obj, isVisible, option) {
		if (!option) option = 0;

		if ( obj )
			for ( var i = 0; i < obj.childNodes.length; i++ )
				if ( obj.childNodes[i].nodeType == 1 )//ELEMENT_NODE:1
					obj.childNodes[i].style.display = ( (isVisible) ? 'block' : 'none' );
		return ;
	}


	/*
		getFirstElementNode
			- obj 노드의 자식 노드 중 첫번째 ElementNode를 반환한다.
	*/
	function getFirstElementNode(obj)
	{
		if ( obj )
			for ( var i = 0; i < obj.childNodes.length; i++ )
				if ( obj.childNodes[i].nodeType == 1 )//ELEMENT_NODE:1
					return obj.childNodes[i];
		return false;
	}






	//-----------------------------------------------------------------------------
	// 해당 객체에 드래그 이벤트를 설정한다.
	// @return : null
	// ex) attachDragEvent(document.getElementById("divLayer"));
	//-----------------------------------------------------------------------------
	function attachDragEvent(oElement){
		oElement.style.position = "absolute";

		oElement.onmousedown = function(){
			oElement.style.cursor = "move";

			this.oldPosX = window.event.x;
			this.oldPosY = window.event.y;
						
			this.onmousemove = function(){
				this.newPosX = this.offsetLeft + (window.event.x - this.oldPosX) + "px";
				this.newPosY = this.offsetTop + (window.event.y - this.oldPosY) + "px";

				this.style.left = this.newPosX;
				this.style.top = this.newPosY;

				this.oldPosX = window.event.x;
				this.oldPosY = window.event.y;
			}
		}
					
		oElement.onmouseup = oElement.onmouseleave = oElement.onmouseout = function(){
			this.onmousemove = "";
			this.style.cursor = "default";
		}
	}






	//-----------------------------------------------------------------------------
	//	기준사이즈로 고정된 이미지를 생성하여 삽입한다.
	// @return : null
	// ex) getFixedImage(이미지경로, 최대넓이, 최대높이, 삽입할객체);
	// ex2) getFixedImage("/upload/employee/IMG1234.jpg", 130, 130, document.getElementById("spanPicture"))
	//-----------------------------------------------------------------------------
	function getFixedImage(imageSrc, fixWidth, fixHeight, oTarget){
		var oImage = new Image();
		var newImage;
		var orgWidth, orgHeight;
		var newWidth, newHeight;

		oImage.src = imageSrc;
		oImage.style.display = "none";

		document.body.appendChild( oImage );

		oImage.onload = function(){
			var orgWidth = this.width;
			orgHeight = this.height;

			if(orgWidth > fixWidth){
				newWidth = fixWidth;
				newHeight = Math.floor( orgHeight * (fixWidth / orgWidth) );
			}

			if(orgHeight > fixHeight){
				newWidth = Math.floor( orgWidth * (fixHeight / orgHeight) );
				newHeight = fixHeight;
			}

			newImage = new Image(newWidth, newHeight);
			newImage.src = this.src;

			document.body.removeChild( this );

			oTarget.appendChild( newImage );
		};
	}




/*****************************************************************************************
		※ 투명도 설정관련..
*****************************************************************************************/

	//-----------------------------------------------------------------------------
	// 해당 객체의 투명도를 설정한다.
	// @return : null
	// ex) setOpacity(40);
	//-----------------------------------------------------------------------------
	function setOpacity(obj, value){
		if( typeof obj == "string")
			obj = $(obj);

		obj.style.opacity = (value / 100);
		obj.style.MozOpacity = (value / 100);
		obj.style.KhtmlOpacity = (value / 100);
		obj.style.filter = "alpha(opacity=" + value + ")";
	}




	/*---------------------------------------------------------------------------------*\
	*	투명도 효과를 제어하기위한 클래스 정의..
	\*---------------------------------------------------------------------------------*/
	var Opacity = function(id){
		this.obj = (typeof id == "string") ? $(id) : id;
				
		this.opacity = 0;
		this.style = this.obj.style;
		this.timer = null;
	}
	

	Opacity.prototype = {
		// 투명도 설정..
		setOpacity: function(value){
			this.style.display = (value == 0) ? "none" : "block"; 
					
			this.style.opacity = (value / 100);
			this.style.MozOpacity = (value / 100);
			this.style.KhtmlOpacity = (value / 100);
			this.style.filter = "alpha(opacity=" + value + ")";
		},

				
		// FadeIn 효과 (서서히 밝아짐..)
		FadeIn: function(){
			var oThis = this;
			
			this.timer = window.setInterval( function() { oThis.FadeInTimer(); }, 10);
		},

				
		// FadeOut 효과 (서서히 사라짐..)
		FadeOut: function(){
			var oThis = this;
					
			this.timer = window.setInterval( function() { oThis.FadeOutTimer(); }, 10);					
		},
				
				
		FadeInTimer: function(){
			if(this.opacity < 100){
				this.opacity += 4;
				this.setOpacity(this.opacity);
					
				return false;
			} else{
				clearInterval(this.timer);
						
				return true;
			}
		},
				
		FadeOutTimer: function(){
			if(this.opacity > 0){
				this.opacity -= 4;
				this.setOpacity(this.opacity);
						
				return false;
			} else{
				clearInterval(this.timer);
						
				return true;
			}
		}
	};







/*****************************************************************************************
		※ String Buffer 객체..
*****************************************************************************************/
	var StringBuffer = function(){ 
		this.buffer = new Array(); 
	}


	StringBuffer.prototype.append = function(str){ 
		this.buffer[this.buffer.length] = str; 
	}


	StringBuffer.prototype.toString = function(){ 
		return this.buffer.join(""); 
	}




/*****************************************************************************************
		※ Array 객체 확장..
*****************************************************************************************/
	Array.prototype.push = function(){
		var startLength = this.length;

		for(var i=0; i<arguments.length; i++)
			this[startLength + i] = arguments[i];

		return this.length;
	}


/*****************************************************************************************
		※ Number 객체 확장..
*****************************************************************************************/
	//-----------------------------------------------------------------------------
	// 숫자의 자리수(cnt)에 맞도록 반환
	// @return : 변환된 String			ex) 33.digits(4) => "0033";
	// ex) 숫자.digits(자리수);
	//-----------------------------------------------------------------------------
	Number.prototype.digits = function(cnt) {
		var sThis = this.toString();
		var digit = "";

		if (sThis.length < cnt) {
			for(var i = 0; i < cnt - sThis.length; i++) {
				digit += "0";
			}
		}

		return digit + sThis;
	};
	
	

/*****************************************************************************************
		※ String 객체 확장..
*****************************************************************************************/

	//-----------------------------------------------------------------------------
	// 문자의 좌, 우 공백 제거
	// @return : String
	// ex) 문자열.trim();
	//-----------------------------------------------------------------------------
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, "");
	};



	//-----------------------------------------------------------------------------
	// 문자의 좌 공백 제거
	// @return : String
	// ex) 문자열.ltrim();
	//-----------------------------------------------------------------------------
	String.prototype.ltrim = function() {
		return this.replace(/(^\s*)/, "");
	};



	//-----------------------------------------------------------------------------
	// 문자의 우 공백 제거
	// @return : String
	// ex) 문자열.rtrim();
	//-----------------------------------------------------------------------------
	String.prototype.rtrim = function() {
		return this.replace(/(\s*$)/, "");    
	};




	//-----------------------------------------------------------------------------
	// 이메일의 유효성을 체크
	// @return : boolean
	// ex) 문자열.isEmail();
	//-----------------------------------------------------------------------------
	String.prototype.isEmail = function() {
		return (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-zA-Z]{2,4}$/).test(this.trim());
	};





	//-----------------------------------------------------------------------------
	// 문자열의 바이트수 리턴
	// @return : int
	// ex) 문자열.bytes();
	//-----------------------------------------------------------------------------
	String.prototype.bytes = function() {
		var cnt = 0;

		for (var i = 0; i < this.length; i++) {
			if (this.charCodeAt(i) > 127)
				cnt += 2;
			else
				cnt++;
		}

		return cnt;
	};




	//-----------------------------------------------------------------------------
	// 정수형으로 변환
	// @return : int
	// ex) 문자열.toInteger();
	//-----------------------------------------------------------------------------
	String.prototype.toInteger = function(){
		if(!isNaN(this)){
			return parseInt(this, 10);
		} else{
			return null;
		}
	};



	//-----------------------------------------------------------------------------
	// 숫자에 3자리마다 , 를 찍어서 반환
	// @return : 변환된 String ( ex) 12,345,678 )
	// ex) 문자열.money();
	//-----------------------------------------------------------------------------
	String.prototype.money = function() {
		var num = this.trim();

		while((/(-?[0-9]+)([0-9]{3})/).test(num)) {
			num = num.replace((/(-?[0-9]+)([0-9]{3})/), "$1,$2");
		}

		return num;
	};




	//-----------------------------------------------------------------------------
	// 숫자의 자리수(cnt)에 맞도록 반환
	// @return : 변환된 String			ex) "33".digits(4) => "0033";
	// ex) 문자열.digits(자리수);
	//-----------------------------------------------------------------------------
	String.prototype.digits = function(cnt) {
		var digit = "";

		if (this.length < cnt) {
			for(var i = 0; i < cnt - this.length; i++) {
				digit += "0";
			}
		}

		return digit + this;
	};



	//-----------------------------------------------------------------------------
	// 문자열에 포함된 숫자만 가져 오기
	// @return : String					ex) "-123$asdf456".num() => "123456";
	// ex) 문자열.num();
	//-----------------------------------------------------------------------------
	String.prototype.num = function() {
		return (this.trim().replace(/[^0-9]/g, ""));
	};



	//-----------------------------------------------------------------------------
	// 문자열을 원하는 바이트만큼 자르기..
	// @return : String					ex) "abcdefghijklmn".cut(5) => "abcde";
	// ex) 문자열.cut(바이트);
	//-----------------------------------------------------------------------------
	String.prototype.cut = function(iCount) {
		var strReturn = this;
		var intLength = 0;

		for (var i=0; i<strReturn.length; i++) {
			intLength += (strReturn.charCodeAt(i) > 128) ? 2 : 1;

			if (intLength > iCount)
				return strReturn.substring(0,i) + "..";
		}

		return strReturn;
	};




	//-----------------------------------------------------------------------------
	// 문자열에 포함된 특정문자를 모두 바꾸기..
	// @return : String					ex) "asdflkj&&&qwerpio".replaceAll("&", "-") => "asdflkj---qwerpio";
	// ex) 문자열.replaceAll(원본문자, 바꿀문자);
	//-----------------------------------------------------------------------------
	String.prototype.replaceAll = function(source, target) {
		source = source.replace(new RegExp("(\\W)", "g"), "\\$1");
		target = target.replace(new RegExp("\\$", "g"), "$$$$");

		return this.replace(new RegExp(source, "gm"), target);
	};



	//-----------------------------------------------------------------------------
	// 문자열에 포함된 특정문자의 갯수 반환
	// @return : int					ex) "abczzzkk".count("z") => 3;
	// ex) 문자열.count(문자);
	//-----------------------------------------------------------------------------
	String.prototype.count = function(str) {
		var matches = this.match(new RegExp(str.replace(new RegExp("(\\W)", "g"), "\\$1"), "g"));

		return matches ? matches.length : 0;
	}



	String.prototype.htmlspecialchars = function()	{ 
		return this.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll("<", "&gt;"); 
	}

	String.prototype.unhtmlspecialchars = function() {
		return this.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">"); 
	}

	String.prototype.stripquote = function() {	
		return this.replaceAll("'", "").replaceAll('"', '').replaceAll("&#39;", "").replaceAll("&#039;", "").replaceAll("&quote;", ""); 
	}
