/**
 * 선택된 체크박스의 value 를 쉼표(,) 로 구분하여 반환한다.
 * @param objName	체크박스객체의 name
 * @return
 */
function getQueryStringByCheckbox(objName) {
	var checkCount = 0;
	var queryString = "";
	var box = document.getElementsByName(objName);
	for(var i = 0; i < box.length; i++) {
		if(box[i].checked) {
			if(checkCount > 0) queryString += ",";
			queryString += box[i].value;
			checkCount++;
		}
	}
	return queryString;
}

/**
 * 팝업 가운데로 띄우기
 * @param url
 * @param w 가로길이
 * @param h 세로길이
 * @param options 기타 팝업창 옵션
*/
function winPop(url, w, h, options) {
	var left = Math.ceil( (window.screen.width  - w) / 2 );
	var top = Math.ceil( (window.screen.height - h) / 2 );
	if(options.length > 0) options = ", " + options;

	window.open(url, '', 'left='+ left + ',top=' + top + ',width=' + w + ',height=' + h + options);
}

/**
 * @flag true/false
 * @objName 체크가 스위칭될 대상 객체명
*/
function switchCheckbox(flag, objName) {
	var obj = document.getElementsByName(objName);
	for(var i=0; i < obj.length; i++) {
		obj[i].checked = flag;
	}
}

/**
 *	이메일 형식 체크
 *	@param str 체크하고자 하는 문자열
 */
function isEmail(str) {
	// regular expression 지원 여부 점검
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}



/*
 * 텍스트 요약
 * @Param text 요약할 텍스트
 * @Param size 텍스트 길이
 */
function Summary() {
	this.max = 0;
	this.postfix = "...";
	this.cutText = function(text) {

		var length = text.length;
		var byteLength = 0;
		for (var i =0;i < length ;i++ ){
			var c = text.charAt(i);
			if (escape(c).length > 4){
				byteLength +=2;
			} else {
				++byteLength;
			}
			
			if(byteLength == this.max) {
				return text.substring(0, i+1);
			} else if( byteLength >= this.max ) {
				return text.substring(0, i);
			}
		}
		return text;
	}

	this.toText = function(text) {
		var elm = document.createElement("div");
		elm.innerHTML = text;
		if(document.all) {
			return elm.innerText;
		} else {
			return elm.textContent;
		}
		
	}

	this.cut = function(text, size) {
		this.max = size - this.postfix.length;
		text = this.toText(text);
		var result = this.cutText(text) + this.postfix;
		return result;
	}
}
var summary = new Summary();

/*
 * select tag안에 시간 및 분의 option들을 생성
 * @param id = select tag의 id
 * @param hId = 시간 id
 * @param mId = 분 id
 * @param selected = 초기값 e.g. "10:20"
 */
function SelectUtil () {
	
	this.setOptions = function(id, size, selected, tag) {
		
		var elm = document.getElementById(id);
		for( i=-1; i < size; i++) {
			var child = document.createElement("option");
			var value = "";
			if (i == -1 ) {
				value = "";
			} else if( i < 10 ) {
				value = "0" + i;
			} else {
				value = i;
			}
			if( value == selected) {
				child.selected = selected;
			}
			child.value = value;
			if( value == "") {
				child.innerHTML = tag;
			} else {
				child.innerHTML = value;
			}
			
			elm.appendChild(child);
		}
	}

	this.setOptions2 = function(id, from, to, selected, tag) {
		
		var elm = document.getElementById(id);
		for( i= from-1; i <= to; i++) {
			var child = document.createElement("option");
			var value = "";
			if (i ==  from-1) {
				value = "";
			} else if( i < 10 ) {
				value = "0" + i;
			} else {
				value = i;
			}
			if( value == selected) {
				child.selected = selected;
			}
			child.value = value;
			if( value == "") {
				child.innerHTML = tag;
			} else {
				child.innerHTML = value;
			}
			
			elm.appendChild(child);
		}
	}
	
	this.setHours = function (id, selected) {
		if (selected == undefined || selected == null) {
			this.setOptions(id, 24, '', "시");
		} else {
			this.setOptions(id, 24, selected, "시");
		}
	}

	this.setMinutes = function (id, selected) {
		if (selected == undefined || selected == null) {
			this.setOptions(id, 60, '', "분");
		} else {
			this.setOptions(id, 60, selected, "분");
		}
	}

	this.setTime2 = function (hId, mId, hSelected, mSelected) {
		this.setHours(hId, hSelected);
		this.setMinutes(mId, mSelected);
	}

	this.setTime = function (hId, mId, selected) {
		if (selected == undefined || selected == null) {
			this.setHours(hId, "");
			this.setMinutes(mId, "");
		} else {
			var sArray = selected.split(":");
			this.setHours(hId, sArray[0]);
			this.setMinutes(mId, sArray[1]);
		}
			
	}

	this.setMonth = function (id, selected) {
		if (selected == undefined || selected == null) {
			this.setOptions2(id, 1, 12, '', "월");
		} else {
			this.setOptions2(id, 1, 12, selected, "월");
		}
	}

	this.setYear = function (id, from, to, selected, title) {
		if (title == undefined || title == null) {
			title = '년';
		}
		if (selected == undefined || selected == null) {
			this.setOptions2(id, from, to, '', title);
		} else {
			this.setOptions2(id, from, to, selected, title);
		}
	}
	
}

/**
 *	입력된 문자열의 Byte단위의 길이를 반환한다.
 */
function getTextByteLength(text) {
	var length = text.length;
	var byteLength = 0;
	for (var i =0;i < length ;i++ ){
		var c = text.charAt(i);
		if (escape(c).length > 4){
			byteLength +=2;
		} else {
			++byteLength;
		}
	}
	return byteLength;
}

var selectUtil = new SelectUtil();

/**
 *	해당 객체에 오늘 날짜를 넣어준다.
 */
function printToday(id) {
	var day = new Date();
	var month = (day.getMonth() + 1);
	var date = day.getDate();
	if( month < 10) {
		month = "0"+ month;
	}
	if(date < 10) {
		date = "0" + date;
	}
		
	var form = day.getFullYear() + "." + month + "." + date;
	if(id == undefined || id == null ) {
		return form;
	} else {
		document.getElementById(id).innerHTML = form;
	}
}

/**************************************************************************
    Function Name   : lpad
    Description     : str에 len만큼 rep를 붙여서 리턴한다
    parameters      : str(대상 문장), len(길이), rep(붙일 문자열)
    return          : String
**************************************************************************/
function lpad(str, len, rep) {
 	var slen = new String(str).length;
 	var rStr = "";
 	if(len > slen){
 		var addCnt = len- slen;
 		for(var i=0; i <addCnt; i++)
 			rStr+=escape(rep);
 		rStr+=escape(str);
 		return new String(rStr);
 	}else{
 		return new String(str);
 	}
}


/**
 * 숫자 체크(소수점x)
 */
function numCheck(elm) {
	if(elm.value.match(/[^0-9]/)){
		alert("숫자만 입력하세요.");
		elm.value = elm.value.replace(/[^0-9]/g,"");
	}
}

/**
 * 소숫점 및 - 포함 숫자 입력 기능
 */
function toNum(elm, underSize) {
	if( underSize == undefined || underSize == null ) {
		underSize = 2;
	}
	var num = elm.value;

	if( num.indexOf("0") == 0 && num.length > 1) {
		if( num.indexOf(".") == 1 ) {
		} else {
			elm.value = num.substring(1, num.length);
			return toNum(elm, underSize);
		}
	}
	var sign = "-";

	if( num.indexOf(".") == 0) {
		num = "0." + num.replace(/[^0-9]/g, "");
	} else if( num.indexOf(".") > -1) {
		num = num.substring(0, num.indexOf(".")).replace(/[^0-9-]/g, "") + "." + num.substring(num.indexOf("."), num.length).replace(/[^0-9-]/g, "").substring(0,underSize);
	}
	if( num.indexOf(sign) == 0 ) {
		num = sign + num.replace(/[^0-9.]/g, "");
	} else {
		num = num.replace(/[^0-9.]/g, "");
	}
	
	elm.value = num;
}
/**
 * 최대 길이가 있는 숫자
 */
function toMaxedNum(elm, max) {
	
	var num = elm.value;

	if( num.indexOf("0") == 0 && num.length > 1) {
		if( num.indexOf(".") == 1 ) {
		} else {
			elm.value = num.substring(1, num.length);
			return toMaxedNum(elm, max);
		}
	}
	num = num.replace(/[^[0-9]/g, "");
	num = num.substring(0, max);
	elm.value = num;
}

/**
 * 플래시 삽입
 */
function swfprint(furl,fwidth,fheight,transoption, Id) {
	if (Id) {
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ fwidth +'" height="' + fheight +'" align="top" id="'+Id+'">');
	} else {
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ fwidth +'" height="' + fheight +'" align="top">');
	}
	document.write('<param name="movie" value="'+ furl +'"/>');
	document.write('<param name="allowScriptAccess" value="always"/>');
	if (transoption == "t") {
		document.write('<param name="wmode" value="transparent"/>');
	} else if (transoption == "o") {
		document.write('<param name="wmode" value="opaque"/>');
	} else if (transoption == "w") {
		document.write('<param name="wmode" value="window"/>');
	}
	document.write('<!-- Hixie method -->');
	document.write('<!--[if !IE]> <-->');
	document.write('<object type="application/x-shockwave-flash" allowScriptAccess="always" align="top" data="'+ furl +'" width="'+ fwidth +'" height="' + fheight +'"');
	if (transoption == "t") {
		document.write(' wmode="transparent"');
	} else if (transoption == "o") {
		document.write(' wmode="opaque"');
	}
	if (Id) {
		document.write(' id="'+Id+'"');
	}
	document.write('></object>');
	document.write('<!--> <![endif]-->');
	document.write('</object>');
	
	
}

function CalenderDetail() {
	this.id = -1;
	this.eventDay = null;
	this.eventDetail = null;
	this.eventDayTd = null;
	this.setElm = function(id) {
		this.id = id;
		this.eventDay = document.getElementById('eventofday' + id);
		this.eventDetail = document.getElementById('event_detail' + id);
		this.eventDetail.style.display = "block";
		var left=0,top=0;		
		var elm = this.eventDay;
		while(elm.tagName != "TR") {

			top += parseInt(elm.offsetTop);
			left += parseInt(elm.offsetLeft);
			this.eventDayTd = elm;
			elm = elm.parentNode;
			//alert(elm.tagName + "\n" + top + "\n" + left);
		}
		
		var tr = this.eventDayTd.parentNode;
		var index = 0;
		for(var i =0; i < tr.children.length; i++) {
			if( tr.children[i] == this.eventDayTd) {
				index = i;
			}
		}
		
		this.eventDetail.style.top = top+ 55 + 'px';
		//오른쪽에 그리기
		if( index < 4 ) {
			this.eventDetail.style.left = (left + parseInt(this.eventDayTd.offsetWidth) - 10) + 'px';
		} else {
			this.eventDetail.style.left = (left - parseInt(this.eventDayTd.offsetWidth)+ -140) + 'px';
		}
		
		//alert(elm.tagName + "\n" + top + "\n" + left);
		this.setEvent();
	};
	
	this.setEvent = function() {
		if( this.id == -1 ) {
			return;
		}
		this.eventDayTd.onmouseout = function(e) {
			if(cDetail.getDetail() == null ) {
				return;
			}
			cDetail.getDetail().style.display = "none";
			this.onmouseover = function() {};
			//cDetail.delDetail();
		}
		this.eventDayTd.onmouseover = function(e) {
			if(cDetail.getDetail() == null ) {
				return;
			}
			cDetail.getDetail().style.display = "block";
			this.onmouseover = function() {};
			//cDetail.delDetail();
		}
		this.eventDetail.onmouseout = function(e) {
			if(cDetail.getDetail() == null ) {
				return;
			}
			cDetail.getDetail().style.display = "none";
			//cDetail.delDetail();
		}
		this.eventDetail.onmouseover = function(e) {
			if(cDetail.getDetail() == null ) {
				return;
			}
			cDetail.getDetail().style.display = "block";
			//cDetail.delDetail();
		}
	};
	
	this.getDetail = function() {
		return this.eventDetail;
	};
	
	this.delDetail = function() {
		this.eventDetail = null;
		this.id = -1;
	};
	
}
var cDetail = new CalenderDetail();

/* roll text(up) */
var mRollEl = new Array();
var mRollElsum = 1;

function initRollText(mRollContainer, mRollContent, delay) {
	mRollEl[mRollEl.length] = mRollContainer;
	var speed = 20; 
	mRollContainer.delay = delay/(speed/mRollElsum);
	//mRollContainer.moveOffset = mRollContainer.offsetHeight;
	mRollContainer.moveOffset = 13;
	mRollContainer.count = 0;
	mRollContainer.mRollOver = false;
	mRollContainer.cont = mRollContent;
	mRollContainer.cont.currentHeight = 0;
	mRollContainer.move = setInterval("movemRoll()", speed);
	for (i=0; i<mRollEl.length; i++) {
		mRollEl[i].onmouseover = function() { this.mRollOver=true; }
		mRollEl[i].onmouseout = function() { this.mRollOver=false; }
	}
}
function movemRoll() {//
	for (i=0; i<mRollEl.length; i++) {
		if (mRollEl[i].cont.currentHeight % mRollEl[i].moveOffset == 0 && mRollEl[i].count < mRollEl[i].delay) {
			if(!mRollEl[i].mRollOver) mRollEl[i].count++;
		} else {
			mRollEl[i].count = 0;
			mRollEl[i].cont.currentHeight -= mRollEl[i].moveOffset;
			if (mRollEl[i].cont.currentHeight % (mRollEl[i].cont.offsetHeight) == 0) {
				mRollEl[i].cont.currentHeight = 0;
			}
			mRollEl[i].cont.style.top = mRollEl[i].cont.currentHeight + "px";
		}
	}
}
function prevmRoll(mRollElnum) {
	var mRollElnum = mRollElnum-1;
	mRollEl[mRollElnum].count = 0;
	mRollEl[mRollElnum].cont.currentHeight+= mRollEl[mRollElnum].moveOffset;
	if (-mRollEl[mRollElnum].cont.currentHeight < 0) {
		mRollEl[mRollElnum].cont.currentHeight = mRollEl[mRollElnum].moveOffset-mRollEl[mRollElnum].cont.offsetHeight;
	}
	mRollEl[mRollElnum].cont.style.top = mRollEl[mRollElnum].cont.currentHeight + "px";
}
function nextmRoll(mRollElnum) {
	var mRollElnum = mRollElnum-1;
	mRollEl[mRollElnum].count = 0;
	mRollEl[mRollElnum].cont.currentHeight-= mRollEl[mRollElnum].moveOffset;
	if (-mRollEl[mRollElnum].cont.currentHeight >= mRollEl[mRollElnum].cont.offsetHeight) {
		mRollEl[mRollElnum].cont.currentHeight = 0;
	}
	mRollEl[mRollElnum].cont.style.top = mRollEl[mRollElnum].cont.currentHeight + "px";
}
/* //roll text(up) */

//현재 브라우저가 ie 인지 판별한다.
function isIE() {
	return navigator.appName == "Microsoft Internet Explorer";
}

/* 현재브라우저의 쿠키값을 가져온다.*/
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 ""; 
}

/* targetObj의 내용을 클립보드에 복사 */
function copyClipboard(isInput, targetObj) {
	var isCopy = false;
	var source = "";
	if (isInput) source = document.getElementById(targetObj).value;
	else source = document.getElementById(targetObj).innerHTML;

	if (window.clipboardData) {
		isCopy = window.clipboardData.setData("Text", source);
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			if (!clip) return false;

			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans) return false;

			trans.addDataFlavor('text/unicode');
			var str = new Object();
			var len = new Object();
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			var copytext = source;
			str.data = copytext;
			trans.setTransferData("text/unicode", str, copytext.length * 2);
			var clipid = Components.interfaces.nsIClipboard;
			if (!clip) return false;
			isCopy = clip.setData(trans, null, clipid.kGlobalClipboard);
		} catch (e) {
			alert('소스복사 오류입니다. 파이어폭스를 사용하는 회원님께선 about:config 고급설정에서 Signed.applets.codebase_principal_support 항목을 true 로 설정해주시기 바랍니다.');
		}
	}
	if (isCopy) alert("클립보드에 복사되었습니다.");
}