/* **********************************************************************/
//	Eoneo Framework
//
//	@copyright		(c)2007 Eoneo inc.
//	@company		Eoneo inc. devteam
// **********************************************************************/

// NOTICE : 이 파일은 UTF-8 로 인코딩되어야 합니다.

// NOTICE : 이 파일은 Eoneo Framework 공용 파일입니다. 
//			개별 제품에 대한 스크립트는 global.js 에 기술하십시오.

var gDebug = false;

Array.prototype.has = function (s)
{
	for (var i = 0; i < this.length ; i++ )
	{
		if (this[i] == s)
		{
			return true;
		}
	}
	return false;
}

String.prototype.trim = function ()
{
	var ret = this.replace (/(^\s*)|(\s*$)/g, "");
	return ret;
}

String.prototype.spaceOptimize = function()
{
	var ret = this.replace (/(\s){2,}/g, " ");
	return ret;
}

String.prototype.cntWord = function()
{
	return this.split(/\w+/).length;
}

function selectedValue(obj)
{
	if (obj.selectedIndex < 0)
		return null;

	return obj.options[obj.selectedIndex].value;
}

function isChecked(obj)
{
	return obj.checked;	
}

function isKorean(sTarget)
{
	return /^[가-힣]+$/.test(sTarget);
}

function isEnglish(sTarget)
{
	return /^[a-zA-Z0-9\.,~!\?\$%@&'\"\\\- ]+$/.test(sTarget);
}

function isEnglish2(sTarget)
{
	var temp = sTarget.replace (/[가-힣]+/g, "");
	return (temp.length == sTarget.length);
}

function isPureEnglishWord(sTarget)
{
	return /^[a-zA-Z0-9\.,~!\?\$%@&'\"\\\-]+$/.test(sTarget);
}

function isEmail(sTarget)
{ 
	return /^[0-9a-z]([-_\.]?[0-9a-z])*@[0-9a-z]([-_\.]?[0-9a-z])*\.[a-z]{2,4}$/i.test(sTarget); 
}

function isNumber(sTarget)
{
	return /^[0-9]+$/.test(sTarget);
}

function g(s)
{
	return (document.getElementById ) ? document.getElementById(s) : document.all[s];
}

function $(s)
{
	return (document.getElementById ) ? document.getElementById(s) : document.all[s];
}

function checkedValue(name)
{
	var obj = null;

	for (var i = 0; i < 10 ; i++ )
	{
		var obj = $(name + "[" + i + "]");

		if (obj == undefined)
			break;
		
		if (isChecked(obj))
			return $(name + "[" + i + "]").value;
	}

	return null;
}

function setFocus(objName)
{
	if ($(objName).style.display != "none")
		$(objName).focus();
}

function getCheckboxValue(s)
{
	var inputs = document.getElementsByTagName("input");

	for (var i = 0; i < inputs.length; i++)
	{
		var dat = inputs[i];
		if (dat.type.toLowerCase() == 'checkbox' &&
			dat.id.indexOf(s + "[") > -1 &&
			dat.checked == true)
		{
			return dat.value;
		}
	}

	return -1;
}

function getCheckboxValues(s)
{
	var inputs = document.getElementsByTagName("input");
	var arr = [];

	for (var i = 0; i < inputs.length; i++)
	{
		var dat = inputs[i];
		if (dat.type.toLowerCase() == 'checkbox' &&
			dat.id.indexOf(s + "[") > -1 &&
			dat.checked == true)
		{
			arr.push(dat.value);
		}
	}

	return arr;
}

function getInputValues(s)
{
	var inputs = document.getElementsByTagName("input");
	var arr = [];

	for (var i = 0; i < inputs.length; i++)
	{
		var dat = inputs[i];
		if (dat.type.toLowerCase() == 'text' &&
			dat.id.indexOf(s + "[") > -1)
		{
			arr.push(dat.value);
		}
	}

	return arr;
}

function setInputValue(id, value)
{
	$(id).value = value;
}

function setCheckboxValue(s, v)
{
	var inputs = document.getElementsByTagName("input");

	for (var i = 0; i < inputs.length; i++)
	{
		var dat = inputs[i];
		if (dat.type.toLowerCase() == 'checkbox' &&
			dat.id.indexOf(s + "[") > -1)
		{
			dat.checked = dat.value == v ? true : false;
		}
	}
}

function multipleChecked(prefix, arrData, startIdx, endIdx)
{
	for (var i = startIdx; i <= endIdx; i++ )
	{
		var obj = $(prefix + i);
		if (obj != undefined && obj.type == "checkbox")
		{
			obj.checked = in_array(i, arrData);
		}
	}
}

function selectTagLock()
{
	var inputs = document.getElementsByTagName("select");

	for (var i = 0; i < inputs.length; i++)
	{
		var obj = inputs[i];
		obj.style.visibility = "hidden";	
	}
}

function selectTagUnlock()
{
	var inputs = document.getElementsByTagName("select");

	for (var i = 0; i < inputs.length; i++)
	{
		var obj = inputs[i];
		obj.style.visibility = "visible";	
	}
}

function printFlash(objParm, parm, output)
{
	var flaVer = '9,0,124,0';
	var htmls = [];
	htmls.push('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	htmls.push(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + flaVer + '"');
	for (var key in objParm)
	{
		if(key == "name") continue;
		
		htmls.push(' ' + key + '="' + objParm[key] + '"');
	}
	htmls.push('>');
	htmls.push('<PARAM name="quality" value="high">');
	for (var key in parm)
	{
		htmls.push('<PARAM name="' + key + '" value="' + parm[key] + '">');
	}
	htmls.push('<embed');
	for (var key in objParm)
	{
		if (key == "movie" || key == "id")
		{
			continue;
		}
		htmls.push(' ' + key + '="' + objParm[key] + '"');
	}
	for (var key in parm)
	{
		htmls.push(' ' + key + '="' + parm[key] + '"');
	}
	htmls.push(' quality="high"');
	htmls.push(' pluginspage="http://www.macromedia.com/go/getflashplayer"');
	htmls.push(' type="application/x-shockwave-flash"');
	htmls.push('></OBJECT>');

	if (typeof(output) == "undefined" || output == "")
		document.write(htmls.join(''));
	else
		return htmls.join('');
}

function showFlash(id, width, height, movie, flashvars, style)
{
	var objParm = {id:null,width:null,height:null,movie:null,style:null};
	var parm = {flashvars:null};

	objParm.id = id;
	objParm.name = id;
	objParm.width = width;
	objParm.height = height;
	objParm.movie = movie;
	objParm.style = style;
	parm.menu = "false";
	parm.allowScriptAccess = "sameDomain";
	parm.swLiveConnect = "true";
	parm.wmode = "transparent";
	parm.bgcolor = "#FFFFFF";
	parm.scale = "exactfit";
	parm.src = movie;
	parm.flashvars = flashvars;
	printFlash(objParm, parm);
}

function outputFlash(id, width, height, movie, flashvars, style)
{
	var objParm = {id:null,width:null,height:null,movie:null,style:null};
	var parm = {flashvars:null};

	objParm.id = id;
	objParm.name = id;
	objParm.width = width;
	objParm.height = height;
	objParm.movie = movie;
	objParm.style = style;
	parm.menu = "false";
	parm.allowScriptAccess = "sameDomain";
	parm.swLiveConnect = "true";
	parm.wmode = "transparent";
	parm.bgcolor = "#FFFFFF";
	parm.scale = "exactfit";
	parm.src = movie;
	parm.flashvars = flashvars;
	return printFlash(objParm, parm, "output");
}

function toggle(obj)
{
	if (obj==null)
		return;

	if (obj.style.display != 'none')
		hide(obj);
	else
		show(obj);
}

function show(obj, bShow)
{
	if(bShow == undefined || bShow == true)
	{
		try
		{
			if(obj.style.display == '') return;
			obj.style.display = "";
		}
		catch(e){}
	}
	else
	{
		hide(obj);
	}
}

function hide(obj)
{
	try
	{
		if(obj.style.display == 'none') return;
		obj.style.display = "none";
	}
	catch(e){}
}

function showFullScreen(id)
{
	var obj = $(id);

	if (showFullScreen[id] == undefined)
	{
		showFullScreen[id] = {
			resize:function()
			{
				$(id).style.height = (document.getElementById("body").clientHeight + 100) + "px";
//				$("body").doScroll('pageUp');
			}
		};
	}

	show(obj);

	if (typeof showFullScreen[id].resize == "function")
	{
		setTimeout("showFullScreen." + id + ".resize();", 100);
	}
}

showFullScreen.prototype = {};

function fx(nNewPadding,el)
{
	try
	{
		while (el.tagName!="TD")
			el = el.parentNode;
		var nPadding = parseInt(el.getAttribute("nPadding"));
		if (!nPadding)
		{
			var nPadding = parseInt(el.currentStyle.paddingTop);
			el.setAttribute("nPadding",nPadding);
		}

		el.style.paddingTop=nPadding+nNewPadding;
	}
	catch(e){}
}

function defx(el)
{
	try
	{
		while (el.tagName!="TD")
			el = el.parentNode;
		var nPadding = parseInt(el.getAttribute("nPadding"));
		el.style.paddingTop = nPadding;
	}
	catch(e){}
}

function addSelectOptionList(object, text, value)
{
	lObjLength=object.length;
	object.options[lObjLength] = new Option(text,value);
}

function in_array(needle, haystack)
{
	for (var i in haystack)
	{
		if (haystack[i] == needle)
		{
			return true;
		}
	}

	return false;
}

function index_search(needle, haystack)
{
	for (var i in haystack)
	{
		if (haystack[i] == needle)
		{
			return i;
		}
	}

	return -1;
}

function array_search_delete(needle, haystack)
{
	for (var i in haystack)
	{
		if (haystack[i] == needle)
		{
			delete haystack[i];
		}
	}

	return haystack;
}

function attachEvent_(obj, evt, fuc, useCapture)
{	
	if(!useCapture) 
	{
		useCapture=false;
	}

	if(obj.addEventListener) // W3C DOM 지원 브라우저
	{ 
		return obj.addEventListener(evt,fuc,useCapture);
	} 
	else if(obj.attachEvent) // MSDOM 지원 브라우저
	{
		return obj.attachEvent(evt, fuc);
	} 
	else // NN4 나 IE5mac 등 비 호환 브라우저
	{ 
		MyAttachEvent(obj, evt, fuc);
		obj[evt]=function() { MyFireEvent(obj,evt) };
	}
}

function detachEvent_(obj, evt, fuc, useCapture) 
{
	if(!useCapture) 
	{
		useCapture=false;
	}

	if(obj.removeEventListener) 
	{
		return obj.removeEventListener(evt,fuc,useCapture);
	} 
	else if(obj.detachEvent) 
	{
		return obj.detachEvent(evt, fuc);
	} 
	else 
	{
		MyDetachEvent(obj, evt, fuc);
		obj[evt]=function() { MyFireEvent(obj,evt) };
	}
}

// ***** //

function IsValidLoginID(s)
{
	if (s.trim() == "")
	{
		return false;
	}

	if (s.length < 4 || s.length > 16)
	{
		return false;
	}

	var pattern = eval("/[a-zA-Z0-9\-_]+/g");

	if (s.replace(pattern,"") != "")
	{
		return false;
	}

	return true;
}

function IsValidPassword(s1, s2)
{
	if (s1 == "")
	{
		return 0; // 값이 없음
	}

	if (s2 == "")
	{
		return 1; // 값이 없음
	}

	if (s1.length < 4 || s1.length > 12)
	{
		return 2; // 길이 제한에 위배됨
	}

	if (s1 != s2)
	{
		return 3; // 값이 일치되지 않음
	}

	return -1; // 정상임
}

function setPng24(obj)
{
	obj.width=obj.height=1;
	obj.className=obj.className.replace(/\bpng24\b/i,'');

	obj.style.filter =

	"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ obj.src +"',sizingMethod='image');"
	obj.src=''; 
	return '';
}

function autoNext(formObj, obj, prevFormName, nextFormName, len)
{
	if (nextFormName != "" && obj.value.length >= len && event.keyCode != 8 && event.keyCode != 46)
	{
		formObj[nextFormName].focus();
	}
	else if (prevFormName != "" && obj.value == "" && event.keyCode == 8)
	{
		formObj[prevFormName].focus();
	}
	else if (event.keyCode == 8)
	{
		obj.value = obj.value;
	}
}

// 오프너를 끝까지 추척하여 제일 처음 창을 열어준 창의 핸들을 반환합니다.
function getParentWindow(hWnd)
{
	if (hWnd == undefined)
		return {location:{href:""},focus:function(){},_child:function(){return null;}};

	while (hWnd != null)
	{
		if (hWnd.opener)
			return getParentWindow(hWnd.opener);
		else
			return hWnd;
	}
}

// 전체 선택/해제
function checkAll(prefix)
{
	var inputs = document.getElementsByTagName("input");

	checkAll[prefix] = !checkAll[prefix];

	for (var i = 0; i < inputs.length; i++)
	{
		var dat = inputs[i];
		if (dat.type.toLowerCase() == 'checkbox' &&
			dat.id.indexOf(prefix + '[') > -1)
		{
			dat.checked = checkAll[prefix];
		}
	}
}

checkAll.prototype = {};

// 마우스 위치 반환
function MouseObject(e)
{
	this.obj = document.layers;
	this._x = 0;
	this._y = 0;
}

MouseObject.prototype = {
	onMouseMove:function()
	{
		try
		{
			this._x = this.obj ? e.pageX : event.x + document.body.scrollLeft;
			this._y = this.obj ? e.pageY : event.y + document.body.scrollTop;

			onMouseMoveEvent(this._x, this._y);		
		}
		catch (e)
		{
		}
	}
};


function incrementCnt(id, max, fCallbackFunc)
{
	var obj = $(id);
	var cnt = parseInt(obj.value);

	if (max == undefined)
		max = 1;

	if (cnt >= max)
	{
		obj.value = max;
	}
	else
	{
		obj.value = cnt + 1;
		if (fCallbackFunc != undefined)
			fCallbackFunc();
	}
}

function decrementCnt(id, min, fCallbackFunc)
{
	var obj = $(id);
	var cnt = parseInt(obj.value);
	
	if (min == undefined)
		min = 1;

	if (cnt <= min)
	{
		obj.value = min;
	}
	else
	{
		obj.value = cnt - 1;
		if (fCallbackFunc != undefined)
			fCallbackFunc();
	}
}

function insertRow(oTable, arr)
{
	var oRow	= oTable.insertRow(oTable.rows.length);
	
	for (var i = 0; i < arr.length ; i++ )
	{
		var oCell	= oRow.insertCell(i);
		oCell.innerHTML = arr[i];
	}
}

function showCal(objName, checkPrev)
{
	if(checkPrev == undefined || checkPrev == null)
		checkPrev = false;

	window.showModalDialog(gURL_root + "/calendar.html", {target:$(objName), checkPrev:checkPrev} , "dialogWidth=240px;dialogHeight=242px;resizable=no;status=no;scroll=no");
}

function getCookie(name)
{
	
	var nameOfCookie = name + "=";
	var x = 0;
	
	while (x <= document.cookie.length)
	{
		
		var y = (x+nameOfCookie.length);
		
		if (document.cookie.substring(x,y) == nameOfCookie)
		{
		
			if ((endOfCookie=document.cookie.indexOf(";",y)) == -1)
				endOfCookie = document.cookie.length;
				return unescape(document.cookie.substring(y,endOfCookie));
		}
	
		x = document.cookie.indexOf(" ",x) + 1;
		
		if (x == 0)
		break;
	}
	return "";
}

function setCookie(name, value, expiredays)
{
	var todayDate = new Date();
	todayDate.setDate( todayDate.getDate() + expiredays );
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

// 날짜관련

// date object을 넘겨주면 우리 달력에서 전반적으로 사용하는 형태의 날짜 스트링을 리턴함
function dateToString(date)
{
	// javascript는 month가 0~11까지다
	return date.getYear() + "-" + ( date.getMonth() + 1 ) + "-" + date.getDate();
}

// 현재 날짜의 스트링을 얻는다
function getCurrentDate()
{
	return dateToString(new Date());
}

function getDateDifference(year, yearGap, month, monthGap, date, dateGap)
{
	now = new Date();
	calc = new Date(year + yearGap, month - 1 + monthGap, date + dateGap);

	return dateToString(calc);
}

function getDateDifferenceFromNow(yearGap, monthGap, dateGap)
{
	now = new Date();

	return getDateDifference(now.getYear(), yearGap, now.getMonth() + 1, monthGap, now.getDate(), dateGap);
}

function getLastNewYear()
{
	now = new Date();
	return dateToString(new Date(now.getYear(), 0, 1));
}

function dateCmp(s1, s2)
{
	s1 = s1.split("-");
	var d1 = new Date(s1[0], s1[1], s1[2]);
	
	s2 = s2.split("-");
	var d2 = new Date(s2[0], s2[1], s2[2]);
	
	if ( d1 < d2 )
		return -1;
	else if ( d1 == d2 )
		return 0;
	else if ( d1 > d2 )
		return 1;
}
