(function() {
    var m = document.uniqueID /*IE*/
    && document.compatMode /*>=IE6*/
    && !window.XMLHttpRequest /*<=IE6*/
    && document.execCommand ;

    try{
        if(!!m){
            m("BackgroundImageCache", false, true) /* = IE6 only */
        }
    }catch(oh){};
});

var msgDic = {};
var trans = function() {
	var args = $A( arguments );
	var msgCategory = args.shift();
	var msgID = args.shift();

	if( !msgCategory || !msgID ) return '';

	if( !msgDic[ _LANG ] ) msgDic[ _LANG ] = _LANG_MESSAGE_FRONT;
	if( !msgDic[ _LANG ] ) return '';

	var lang = _LANG;
	var langFlag = args.last();
	if( $H( msgDic[ _LANG ][ 'selectLanguage' ] ).keys().indexOf( langFlag ) !== -1 ) {
		lang = args.pop();
		if( !msgDic[ langFlag ] ) {
			new Ajax.Request( '/js/lang/' + lang + '.js?tId=' + ( new Date ).getTime() , {
				'asynchronous' : false
				, 'onComplete' : function( R ) {
					if( !R || !R.responseText ) return;
					var s = 'var _LANG_MESSAGE_FRONT = ';
					var e = 'var _LANG_MESSAGE_ADMIN = ';
					var sp = R.responseText.indexOf( s ) + s.length;
					var l =  R.responseText.indexOf( e ) - sp;
					eval( 'msgDic["' + lang + '"] = ' + R.responseText.substr( sp , l ) );
				}
			} );
		}
	}

	var msgDicSub = msgDic[ lang ][ msgCategory ];

	var msg = msgDicSub[ msgID ] || '';
	if( msg.indexOf( '#{' ) == -1 ) return msg;

	for( var i = 0 , len = args.size() , eData = {} ; i < len ; i++ ) eData[ 'ARG_' + ( i +1 ) ] = args[ i ];

	return ( new Template( msg ).evaluate( eData ) );
};

var getSetting = function( settingID ) {
	return _LANG_SETTINGS[ settingID ] || null;
}

var regExp_email = /^[a-zA-Z\-\_\d][\w\.-]*[a-zA-Z0-9\-\_]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
var regExp_domain = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
var regExp_name = /^[^\'\"\/\\]+$/;
var regExp_phone = /^[\d\-]*$/;

var globalData = {
	get : function( type , callback ) {
		if( type == 'callbacks' ) { throw 'NOT USE callbacks'; return; }
		if( typeof( globalData.callbacks ) != 'object' ) {
			globalData.callbacks = {};
		}
		globalData.callbacks[ type ] = callback;
	}
	, set : function( type , data ) {
		if( type == 'callbacks' ) { throw 'NOT USE callbacks'; return; }

		globalData[ type ] = data;

		if( typeof( globalData.callbacks ) != 'object' ) {
			globalData.callbacks = {};
		}

		if( typeof( globalData.callbacks[ type ] ) == 'function' ) {
			globalData.callbacks[ type ]( globalData[ type ] );
		}
	}
};

// auto resizable textarea
var textareaResizer = Class.create( {
	'initialize' : function( id , limit ) {
		this.element = $( id );
		this.prevHeight = null;
		this.lineHeight = 17;
		this.limit = limit;
		if( !this.element ) return;

		this.element.observe( 'click' , this.checkIt.bindAsEventListener( this ) );
		this.element.observe( 'propertychange' , this.checkIt.bindAsEventListener( this ) );
		this.element.observe( 'input' , this.checkIt.bindAsEventListener( this ) );
		this.element.observe( 'paste' , this.checkIt.bindAsEventListener( this ) );
		this.element.observe( 'keyup' , this.checkIt.bindAsEventListener( this ) );

		this.checkIt();

		return this.element;
	}
	, 'checkIt' : function( ev ) {
		if( ev && this.eventCaptured ) {
			Event.stop( ev );
			return;
		}
		this.eventCaptured = true

		var scrH = this.element.scrollHeight;

		if( !this.prevHeight ) this.prevHeight = scrH;
		/*if( !this.lineHeight ) this.lineHeight = scrH||this.element.offsetHeight;
		if( !this.lineHeight ) {
			this.eventCaptured = false;
			return;
		}*/

		if( scrH > ( this.lineHeight * this.limit ) ) {
			this.element.style.overflowY = 'auto';
			this.element.style.height = ( this.lineHeight * this.limit ) + 'px';
			this.eventCaptured = false;
			return;
		}
		else {
			this.element.style.height = null;
			scrH = ( this.element.scrollHeight || this.lineHeight );
			this.element.style.height = scrH + 'px';
			this.element.style.overflowY = 'hidden';
		}
		if( this.prevHeight == scrH ) {
			this.eventCaptured = false;
			return;
		}

		this.element.style.height = ( scrH || this.lineHeight ) + 'px';
		this.prevHeight = scrH;

		this.eventCaptured = false;
		return;
	}
} );

function toggleObject( state ) {
	if( !Prototype.Browser.IE ) return;
	if( state === undefined ) state = true;
	$A( document.getElementsByTagName( 'object' ) ).each( function( O ) {
		var O = $( O );
		if( state ) {
			if( O.readAttribute( 'bState' ) ) return;
			O.writeAttribute( 'bState' , ( O.visible() ? 'show' : 'hide' ) );
			O.hide();
		}
		else if( O.readAttribute( 'bState' ) ) {
			O[ O.readAttribute( 'bState' ) ]();
			O.removeAttribute( 'bState' );
		}
	} );
}

// auto complete input expand class
Ajax.AutocompleterTo = Class.create( Ajax.Autocompleter , {
	'onObserverEvent' : function( $super ) {
		this.changed = false;
		this.tokenBounds = null;
		if (this.getToken().length >= this.options.minChars && !this.disabled ) {
			this.getUpdatedChoices();
			toggleObject( true );
		} else {
			this.active = false;
			this.hide();
			toggleObject( false );
		}
		this.oldElementValue = this.element.value;
	}
	,  selectEntry: function() {
    this.active = false;
    this.updateElement(this.getCurrentEntry());
  }
	, updateChoices: function(choices) {
		if (!this.changed && this.hasFocus) {
			this.update.innerHTML = choices;
			Element.cleanWhitespace(this.update);
			Element.cleanWhitespace(this.update.down());

			var suObj = $('SWFUpload_0');
			if (this.update.firstChild && this.update.down().childNodes.length > 0) {
				this.entryCount =  this.update.down().childNodes.length;
				for (var i = 0; i < this.entryCount; i++) {
				  var entry = this.getEntry(i);
				  entry.autocompleteIndex = i;
				  this.addObservers(entry);
				}
				if( suObj ) suObj.hide();
			} else {
				this.entryCount = 0;
				if( suObj ) suObj.show();
			}
			this.stopIndicator();
			this.index = 0;

			if(this.entryCount==1 && this.options.autoSelect) {
				this.selectEntry();
				this.hide();
				toggleObject( false );
			} else {
				this.render();
				if( !this.active ) toggleObject( false );
			}
		}
		else {
			toggleObject( false );
		}
	}
	, onClick: function(event) {
		var element = Event.findElement(event, 'LI');
		this.index = element.autocompleteIndex;

		if (this.index == null) {
			var li = element.up('li');
			var re_li = Element.toggleClassName(li, 'selected');
			this.index = re_li.autocompleteIndex;
		}
		this.selectEntry();
		this.hide();
		var suObj = $('SWFUpload_0');
		if( suObj ) suObj.show();
		toggleObject( false );
	  }
	, onBlur: function(event) {
		// needed to make click events working
		setTimeout(this.hide.bind(this), 250);
		this.hasFocus = false;
		this.active = false;
		var suObj = $('SWFUpload_0');
		if( suObj ) suObj.show();
		toggleObject( false );
	}
	, 'onKeyPress' : function( $super , evt ) {
		$super( evt );
		toggleObject( false );
	}
	, 'onException' : function( ev ) {
		toggleObject( false );
	}
	, 'onFailure' : function( ev ) {
		toggleObject( false );
	}
	, 'disable' : function() { this.disabled = true; toggleObject( false ); }
	, 'enable' : function() { this.disabled = false; }
} );

Element.addMethods( {
	'toHtmlString' : function( element ) {
		if( typeof( element ) != 'object' ) return element;
		if( !element.tagName ) return element;
		if( Prototype.Browser.IE ) return element.outerHTML||element;

		var attrs = [];
		$A( element.attributes ).each( function( A ) {
			if( ( A.nodeValue === null ) || ( A.nodeValue === undefined ) || ( A.nodeValue === NaN ) || ( A.nodeValue === '' ) ) return;
			if( typeof( A.nodeValue ) == 'object' ) return;
			attrs.push( A.nodeName + '="' + A.nodeValue + '"' );
		} );
		var output = '<' + element.tagName + ( attrs.length > 0 ? ' ' + attrs.join( ' ' ) : '' );
		output +=  ( element.innerHTML === undefined ) ? ' />' : '>' + element.innerHTML + '</' + element.tagName + '>';
		return output;
	}
	, 'replant' : function( element , tree , options ) {
		element = $( element );
		if( !tree ) return element;

		var node = null;
		if( Object.isArray( tree ) ) {
			if( element.readAttribute( 'treeContain' ) == 'treeContain' ) {
				node = new Element( 'ul' , { 'class' : 'treeChild' } );
				element
					.insert( new Element( 'div' , { 'class' : 'treeWrap' } )
						.insert( new Element( 'div' , { 'class' : 'node' } )
							.insert( node ) ) );
			}
			else {
				node = element;
			}

			$A( tree ).each( function( T ) {
				node.insert( new Element( 'li' , { 'class' : 'treeStem' } ).replant( T , options || {} ) );
			} );
		}
		else {
			var subName = ( typeof options == 'object' ? ( options.subName || 'children' ) : 'children' );
			var openStatus = ( tree[ 'open' ] === false ? 'closed' : 'opened' );

			if( element.readAttribute( 'treeContain' ) == 'treeContain' ) {
				openStatus = 'opened';
				node = new Element( 'div' , { 'class' : 'treeWrap' } );
				element
					.insert( node );
			}
			else {
				node = element;
			}

			var hasChild = false;
			var hasData = false;
			if( ( parseInt( tree[ 'cnt' ] || '0' ) > 0 ) ) {
				hasChild = true;
			}
			else {
				hasChild = ( Object.isArray( tree[ subName ] ) && tree[ subName ].length > 0 );
				hasData = true;
			}

			var nodeOpt = { 'class' : 'node ' + ( hasChild ? openStatus : 'leaf' ) , 'idx' : tree[ 'idx' ] || 0 };
			if( tree[ 'nodeType' ] ) nodeOpt[ 'class' ] += ' ' + tree[ 'nodeType' ];
			if( hasData ) nodeOpt[ 'hasData' ] = 'hasData';
			if( tree[ 'email' ] ) nodeOpt[ 'email' ] = tree[ 'email' ] || '';
			if( tree[ 'mobile' ] ) nodeOpt[ 'mobile' ] = tree[ 'mobile' ] || '';

			node
				.insert( new Element( 'div' , nodeOpt )
					.insert( new Element( 'a' , { 'class' : 'treeExp' , 'rel' : 'toggleChild' } ) )
					.insert( new Element( 'a' , { 'class' : 'treeTitle' , 'rel' : 'selectable' , 'title' : ( tree[ 'title' ] || '' ) } )
						.insert( tree[ 'txt' ] ) ) );

			if( hasChild && hasData ) {
				node
					.insert( new Element( 'ul'
							, { 'class' : 'treeChild' } )[ ( openStatus == 'closed' ? 'hide' : 'show' ) ]()
						.replant( tree[ subName ] ) );
			}
		}

		return element;
	}
	, 'plant' : function( element , tree , options ) {
		var options = Object.extend( {} , options || {} );

		element = $( element );
		if( element.readAttribute( 'planted' ) == 'planted' && !tree ) {
			$H( Prototype.PlantEvent ).each( function( H ) {
				Event.stopObserving( element , H.key , H.value );
			} );
			element.removeAttribute( 'plated' );
			element.removeAttribute( 'eventHandleSetted' );
			return element.update();
		}

		element.writeAttribute( 'treeContain' , 'treeContain' );
		element.replant( tree , options || {} );

		if( element.readAttribute( 'eventHandleSetted' ) != 'eventHandleSetted' ) {
			if( typeof Prototype.PlantEvent !== 'object' ) Prototype.PlantEvent = {};
			
			element.observe( 'click' , ( Prototype.PlantEvent[ 'click' ] = function( evt ) {
				evt.stop();
				var elmt = Event.element( evt );
				switch( elmt.rel ) {
					case 'toggleChild' :
						var target = elmt.up( '.node' );
						if( !target ) return;

						if( target.readAttribute( 'hasData' ) != 'hasData' ) {
							if( typeof options[ 'requestHandler' ] == 'function' ) {
								options[ 'requestHandler' ]( evt );
								target.writeAttribute( 'hasData' , 'hasData' );
							}
						}

						var child = target.next( '.treeChild' );
						if( !child ) return;

						var node = elmt.up( '.node' );
						if( node.hasClassName( 'opened' ) ) {
							node
								.removeClassName( 'opened' )
								.addClassName( 'closed' );
							child.hide();
						}
						else {
							node
								.addClassName( 'opened' )
								.removeClassName( 'closed' )
							child.show();
						}
						break;
					case 'selectable' :
						var target = elmt.up( '.node' );
						if( target.readAttribute( 'hasData' ) != 'hasData' ) {
							if( typeof options[ 'requestHandler' ] == 'function' ) {
								options[ 'requestHandler' ]( evt );
								target.writeAttribute( 'hasData' , 'hasData' );
							}
						}
						if( parseInt( target.readAttribute( 'idx' ) ) !== 0 ) {
							target.toggleClassName( 'selected' );
						}
						break;
				}
			} ) );
			element.observe( 'mouseover' , ( Prototype.PlantEvent[ 'mouseover' ] = function( evt ) {
				evt.stop();
			} ) );
			element.observe( 'mouseout' , ( Prototype.PlantEvent[ 'mouseout' ] = function( evt ) {
				evt.stop();
			} ) );
			element.writeAttribute( 'eventHandleSetted' , 'eventHandleSetted' );
		}

		return element.writeAttribute( 'planted' , 'planted' );
	}
} );

// Ajax call에 대한 상태변화 표시
function setResponders() {
	if( !Ajax ) return;
	if( !Ajax.Responders ) return;
	if( Ajax.Responders.isSetResponders ) return;

	var mEffect = null;

	Ajax.Responders.register( {
		onException : function() { throw( trans( 'error' , 'SERVICE_NOT_AVAIL' ) ); }
		, onCreate : function() {
			$( 'requestDisplay' ).show();
			if( mEffect !== null ) { mEffect.cancel(); mEffect = null; }
			$( 'requestDisplayProgress' ).setStyle( { 'width' : '50px' } );
			var rdt = $( 'requestDisplayText' );
			if( rdt ) rdt.update( 'Initializing...' );
			Element.hide.delay( 2 , $( 'requestDisplay' ) );
		}
		, onLoading : function() {
			$( 'requestDisplay' ).show();
			if( mEffect !== null ) { mEffect.cancel(); mEffect = null; }
			mEffect = new Effect.Morph( 'requestDisplayProgress' , { 'duration' : 2 , 'style' : 'width:120px' } );
			var rdt = $( 'requestDisplayText' );
			if( rdt ) rdt.update( 'Loading...' );
			Element.hide.delay( 2 , $( 'requestDisplay' ) );
		}
		, onLoaded : function() {
			$( 'requestDisplay' ).show();
			if( mEffect !== null ) { mEffect.cancel(); mEffect = null; }
			mEffect = new Effect.Morph( 'requestDisplayProgress' , { 'duration' : 0.5 , 'style' : 'width:130px' } );
			var rdt = $( 'requestDisplayText' );
			if( rdt ) rdt.update( 'Loaded...' );
			Element.hide.delay( 2 , $( 'requestDisplay' ) );
		}
		, onInteractive : function() {
			$( 'requestDisplay' ).show();
			if( mEffect !== null ) { mEffect.cancel(); mEffect = null; }
			mEffect = new Effect.Morph( 'requestDisplayProgress' , { 'duration' : 2 , 'style' : 'width:190px' } );
			var rdt = $( 'requestDisplayText' );
			if( rdt ) rdt.update( 'Receiving...' );
			Element.hide.delay( 2 , $( 'requestDisplay' ) );
		}
		, onComplete : function() {
			var rdt = $( 'requestDisplayText' );
			if( rdt ) rdt.update( 'Complete.' );
			if( mEffect !== null ) { mEffect.cancel(); mEffect = null; }
			mEffect = new Effect.Morph( 'requestDisplayProgress' , { 'duration' : 0.5 , 'style' : 'width:200px' } );
			Element.hide.delay( 1 , $( 'requestDisplay' ) );
		}
	} );
	Ajax.Responders.isSetResponders = true;
}

document.observe( 'dom:loaded' , setResponders );

// 처리 결과를 callback에 전달.. 만약 json response가 없으면 json화 해서 전달;
// callback은 function aaa( obj ) 형식.
// 반환값이 list이라면 obj.totalCnt = 10 , obj.dataCnt = 5 , obj.data[ 0 ] ~ [ 4 ]
function requestFront( url , reqData , callback , options ) {
	if( !url ) return;
	if( callback && ( typeof( callback ) != 'function' ) ) return;

	var reqParams = '';
	if( reqData ) {
		reqParams = ( typeof( reqData ) == 'object' ) ? $H( reqData ).toQueryString() : reqData
	}

	var options = Object.extend( {
		postBody : reqParams
		, evalJSON : true
		, onSuccess : function( transport ) {
			var result = null;
			if( transport && transport.responseJSON ) {
				result = transport.responseJSON;
			}
			else if( transport.responseText ) {
				result = transport.responseText.evalJSON( true );
			}
			else {
				return alert( trans( 'error' , 'DATA_LOADING_FAIL' ) );
			}

			if( result !== null && typeof result == 'object' && result.code ) {
				switch( parseInt( result.code ) ) {
					case 2001 :
					case 2002 : throw transport.responseJSON; break;
					case 5000 :
						alert( trans( 'error' , 'DATA_LOADING_FAIL' ) );
						break;
					case 9999 :
						(function(){
							if( window.document.isLogOut === true ) return;
							window.document.isLogOut = true;
							location.replace( '/intro.php' );
							alert( trans( 'top' , 'logout' ) );
						}).delay( 1 );
						break;
					default :
						if( typeof callback == 'function' ) callback( result ); break;
				}
			}
		}
	} , options || {} );
	try {
		new Ajax.Request( url + '?tId=' + ( new Date ).getTime() , options );
	} catch( e ) {
		if( typeof console !== undefined ) {
			console.log( e );
		}
		else {
			alert( typeof e == 'object' ? e.description : e );
		}
		return;
	}
}

function onlyNumber( ev ) {
	ev = Event.extend( ev );
	var target = Event.element( ev );
	var isIE = window.attachEvent ? true : false;
	var code = ( ev.charCode ? ev.charCode : ( isIE ? ev.keyCode : ev.which ) );

	if( ( ( code > 47 ) && ( code < 58 ) ) || ( isIE && ( code > 95 ) && ( code < 106 ) ) ) {
	}
	else {
		if( code == 8 ) return;
		if( code == 9 ) return;
		if( code == 46 ) return;
		Event.stop( ev );
	}
}

function alertAndFocus( msg , obj ) {
	if( obj && obj.focus && ( typeof( obj.focus ) == 'function' ) ) {
		obj.focus();
	}
	if( msg ) wmAlert( msg );

	return;
}

// 주소 찾기
document.observe( 'dom:loaded' , function() {
	$$( '#topAreaContent img.langChangeButton' ).each( function( Btn ) {
		Btn.observe( 'click' , function( ev ) {
			var lang = Event.element( ev ).readAttribute( 'lang' );
			if( _LANG == lang ) return;

			requestFront( '/api/webmail/0.2/FrontSetUserinfo.php' , { 'language_set' : lang } , function( result ) {
				if( !result || !result.data || !result.data.result ) return;
				document.location.reload();
			} );
		} );
	} , this );

	if( $( 'addressSearch' ) && getSetting( 'usePostSearch' ) ) {
		$( 'addressSearch' ).up().insert( new Element( 'div' , { 'id' : 'zipList' ,'class' : 'zipList' } ).setStyle( { 'zIndex' : 200 } ) );
		new Ajax.Autocompleter( 'addressSearch' , 'zipList' , '/common/zipcode_data.php' , {
			paramName : 'zipSearch'
			, minChars : 2
			, frequency : 0.3
			, updateElement : function( selected ) {
				var ipt = $( 'addressSearch' );
				ipt.value = selected.innerHTML;
				ipt.focus();

				var code = selected.readAttribute( 'code' );
				if( !code ) return;
				if( $( 'zipcode1' ) ) $( 'zipcode1' ).value = code.substring( 0 , 3 );
				if( $( 'zipcode2' ) ) $( 'zipcode2' ).value = code.substring( 3 );
				ipt.writeAttribute( 'isSet' , ( ipt.value != ipt.title ? 'true' : 'false' ) );
			}
		} );
	}
} );

function zipSearchClick( ev ) {
	if( !getSetting( 'usePostSearch' ) ) return;

	var src = Event.element( ev );
	if( src.value ) src.writeAttribute( '_value' , src.value );
	src.value = '';
}

function zipSearchBlur( ev ) {
	if( !getSetting( 'usePostSearch' ) ) return;

	var src = Event.element( ev );
	if( !src.value ) {
		src.value = src.readAttribute( '_value' ) || src.title;
	}
}

function centerElement( id ) {
	var target = $( id );

	var windowScroll = WindowUtilities.getWindowScroll();
	var pageSize = WindowUtilities.getPageSize();

	var top = ( pageSize.windowHeight - target.getHeight() )/2;
	top += windowScroll.top

	var left = ( pageSize.windowWidth - target.getWidth() )/2;
	left += windowScroll.left

	target.style.left = left + 'px';
	target.style.top = top + 'px';
}

function getEditorContent( id ) {
	if( typeof NN == 'object' ) {
		if( NN.appInstance ) return NN.appInstance.getText().value;
	}
	else if( typeof tinyMCE == 'object' ) {
		if( tinyMCE.get( id ) ) return tinyMCE.get(id).getContent();
	}
	return '';
}

function setEditorContent( id , value ) {
	if( typeof NN == 'object' ) {
		if( Prototype.Browser.IE ) {
			NN.appInstance.oSelection.focus();
		}
		try {
		NN.appInstance.save.saveHistory( true );
		NN.appInstance.getText().value = ( value || '' );
		NN.appInstance.getIFDoc().body.innerHTML = ( value || '' );
		} catch( err ) {}
	}
	else if( typeof tinyMCE == 'object' ) {
		if( tinyMCE.get( id ) ) {
			tinyMCE.get(id).setContent( ( value || '' ) );
		}
	}
}

function clearEditorContent( id ) {
	setEditorContent( id , '' );
}

function getEditorMode( id ) {
	var id = id;
	var tabs = $$( 'ul.tabMenu li.onTab' );
	if( tabs.size() < 1 ) return '';

	return tabs.find( function( T ) {
		T = $( T );

		if( T.readAttribute( 'editor' ) != id ) return false;

		return true;
	} ).readAttribute( 'mode' );
}

function setTextareaContent2Editor( id ) {
	var evalue = getEditorContent( id );
	if( getEditorMode( id ) == 'TEXT' ) evalue = text2html( evalue );
	setEditorContent( id , evalue );
}

function changeTab( ev ) {
	var src = Event.element( ev );
	var ul = src.up( 'ul' );
	$A( ul.getElementsByTagName( 'li' ) ).each( function( L ) {
		L.className = L.className.gsub( /onL/ , 'offL' ).gsub( /onR/ , 'offR' ).gsub( /onTab/ , 'offTab' );
	} );

	src.className = src.className.gsub( /offTab/ , 'onTab' );
	src.previous().className = src.previous().className.gsub( /offL/ , 'onL' );
	src.next().className = src.next().className.gsub( /offR/ , 'onR' );
}


function changeEditorMode( ev ) {
	var src = Event.element( ev );
	var mode = src.readAttribute( 'mode' );
	var editor = src.readAttribute( 'editor' );
	var inst = null;
	var obj = null;
	var receiveFlag = null;
	var htmlFlag = null;

	if( !( mode || !editor ) ) return;

	if( typeof NN == 'object' ) {
		obj = $( NN.appInstance.getText() );
		inst = NN.appInstance;
	}
	else if( typeof tinyMCE == 'object' ) {
		obj = $( editor );
		inst = tinyMCE.get( editor );
	}

	if( !obj ) return;

	var editorMode = obj.readAttribute( 'mode' );
	if( editorMode == mode ) return;

	obj.writeAttribute( 'modeChange' , 'modeChange' );

	if( document.formWebmail ) {
		receiveFlag = $( document.formWebmail.receive_flag );
		htmlFlag = $( document.formWebmail.html_flag );
	}


    switch( mode ) {
        case 'WYSIWYG' :
			if( editorMode == 'TEXT' ) {
				obj.value = text2html( obj.value );
			}

			if( typeof NN == 'object' ) {
				$( inst.getText() ).hide();
				$( inst.getIF() ).show();
				inst.view.evtRecoverMode();
			}
			else if( typeof tinyMCE == 'object' ) {
				inst.show();
			}

			obj.writeAttribute( 'mode' , mode );

			if( receiveFlag && receiveFlag.readAttribute( 'origReceiveFlag' ) ) {
				receiveFlag.value = receiveFlag.readAttribute( 'origReceiveFlag' );
			}
			if( htmlFlag && htmlFlag.readAttribute( 'origHtmlFlag' ) ) {
				htmlFlag.value = htmlFlag.readAttribute( 'origHtmlFlag' );
			}
            break;

        case 'TEXT' :
			if( typeof NN == 'object' ) {
				inst.view.source();
				$( inst.getIF() ).hide();
				$( inst.getText() ).show();
			}
			else if( typeof tinyMCE == 'object' ) {
				if( editorMode == 'HTML' ) inst.show();
	            inst.hide();
			}

			obj.value = html2text( obj.value );
			obj.writeAttribute( 'mode' , mode );

			if( receiveFlag ) {
				if( !receiveFlag.readAttribute( 'origReceiveFlag' ) ) {
					receiveFlag.writeAttribute( { 'origReceiveFlag' : receiveFlag.value } );
				}
				if( !htmlFlag.readAttribute( 'origHtmlFlag' ) ) {
					htmlFlag.writeAttribute( { 'origHtmlFlag' : htmlFlag.value } );
				}
				receiveFlag.value = '';
				htmlFlag.value = '';
			}
            break;

        case 'HTML' :
			if( editorMode == 'TEXT' ) {
				obj.value = text2html( obj.value );
			}

			if( typeof NN == 'object' ) {
				$( inst.getText() ).hide();
				$( inst.getIF() ).show();
				inst.view.evtRecoverMode();
				inst.view.source();
			}
			else if( typeof tinyMCE == 'object' ) {
				if( editorMode == 'TEXT' ) inst.show();
	            inst.hide();
			}

			obj.writeAttribute( 'mode' , mode );

			if( receiveFlag && receiveFlag.readAttribute( 'origReceiveFlag' ) ) {
				receiveFlag.value = receiveFlag.readAttribute( 'origReceiveFlag' );
			}
			if( htmlFlag && htmlFlag.readAttribute( 'origHtmlFlag' ) ) {
				htmlFlag.value = htmlFlag.readAttribute( 'origHtmlFlag' );
			}
            break;
	}
	obj.removeAttribute( 'modeChange' );
}

wmAlert = function( msg , fn , w , h ) {
	var alert = new Window( {
		'maximizable' : false
		, 'resizable' : false
		, 'width' : ( w ? w : 400 )
		, 'height' : ( h ? h : 100 )
		, 'showEffectOptions' : { 'duration' : 0.1 }
		, 'hideEffectOptions' : { 'duration' : 0.1 }
		, 'destroyOnClose' : true
	} );

	var container = new Element( 'div' ).setStyle( { 'margin' : '10px' , 'marginTop' : '40px' , 'marginBottom' : '20px' , 'padding' : '0px' , 'textAlign' : 'center' } );
	alert.setContent( container );

	container.insert( new Element( 'span' ).update( msg ) );
	container.insert( new Element( 'br' ) ).insert( new Element( 'br' ) );
	container.insert( new Element( 'span' , { 'id' : 'alertOk' , 'class' : 'btnContainer style6' } ).insert( new Element( 'button' , { 'hidefocus' : 'hidefocus' } ).observe( 'click' , function( bEvt ) { alert.close(); if( typeof( fn ) == 'function' ) fn(); } ).update( trans( 'common' , 'ok' ) ) ) );

	alert.showCenter( true );
};

wmConfirm = function( msg , ycb , ncb ) {
	var confirm = new Window( {
		'maximizable' : false
		, 'resizable' : false
		, 'width' : 400
		, 'height' : 100
		, 'showEffectOptions' : { 'duration' : 0.1 }
		, 'hideEffectOptions' : { 'duration' : 0.1 }
		, 'destroyOnClose' : true
	} );

	window.confirmed = -1;

	var container = new Element( 'div' ).setStyle( { 'margin' : '10px' , 'marginTop' : '40px' , 'marginBottom' : '20px' , 'padding' : '0px' , 'textAlign' : 'center' } );
	confirm.setContent( container );

	container.insert( new Element( 'span' ).update( msg ) );
	container.insert( new Element( 'br' ) ).insert( new Element( 'br' ) );
	container.insert( new Element( 'span' , { 'id' : 'confirmYes' , 'class' : 'btnContainer style6' } ).insert( new Element( 'button' , { 'hidefocus' : 'hidefocus' } ).observe( 'click' , function( bEvt ) {
		Event.stop( bEvt );
		if( typeof( ycb ) == 'function' ) ycb();
		confirm.close();
	} ).update( trans( 'common' , 'yes' ) ) ) );
	container.insert( new Element( 'span' , { 'id' : 'confirmNo' , 'class' : 'btnContainer style6 mlBlank10' } ).insert( new Element( 'button' , { 'hidefocus' : 'hidefocus' } ).observe( 'click' , function( bEvt ) {
		Event.stop( bEvt );
		if( typeof( ncb ) == 'function' ) ncb();
		confirm.close();
	} ).update( trans( 'common' , 'no' ) ) ) );

	confirm.showCenter( true );
};


function selectOutlookItem( ev ) {
	var src = Event.element( ev );

	var A = null , R = null;
	switch( src.tagName.toUpperCase() ) {
		case 'A' : A = src; R = src.down( '.selector' ); break;
		case 'INPUT' : A = src.up( '.linker' ); R = src; break;
		case 'LABEL' : A = src.up( '.linker' ); R = src.previous( '.selector' ); break;
		case 'IMG' : A = src.up( '.linker' ); R = src.up().previous( '.selector' ); break;
		default : A = src.up( '.linker' ); R = src.down( '.selector' ); break;
	}

	$$( '.outlookSelectContainer a' ).each( function( S ) { $( S ).removeClassName( 'selected' ).down( '.selector' ).checked = false; } );
	A.addClassName( 'selected' );
	R.checked = 'checked';
}

var spPopupWindow = null;
function selectSmartphoneItem( ev ) {
	Event.stop( ev );
	var anc = Event.element( ev );
	if( anc.tagName != 'a' ) {
		anc = anc.up( 'a' );
	}
	var url = anc.href;

	return window.open( url , '_blank' , "width=620,height=480,scrollbars=yes,status=no,resizable=yes" );

/*try {
	var spPopupWindow = new Window( {
		'destroyOnClose' : true
		, 'recenterAuto' : true
		, 'width' : 630 , 'height' : 480 , 'left' : 240 , 'top' : 200
		, 'showEffectOptions' : { 'duration' : 0.2 }
		, 'hideEffectOptions' : { 'duration' : 0.2 }
	} );
	spPopupWindow.setURL( url );
	spPopupWindow.showCenter( true );
} catch( err ) {}*/
}

function movePage( ev ) {
	var url = location.href.gsub( /&?pageNo=[\d]+/ , '' );
	var pageNo = Event.element( ev ).readAttribute( 'linkpage' );
	if( parseInt( pageNo ) < 1 ) return;
	location.href = url + ( url.indexOf( '?' ) == -1 ? '?' : '' ) + '&pageNo=' + pageNo;
}

function checkAll( ev ) {
	$A( document.getElementsByName( 'listCheck' ) ).each( function( C ) {
		$( C ).checked = Event.element( ev ).checked;
	} );
}

function getSelected() {
	return $A( document.getElementsByName( 'listCheck' ) ).findAll( function( C ) { return C.checked; } ).compact();
};

function centerObject( obj ) {
	if( typeof( obj ) == 'string' ) {
		if( !$( obj ) ) return;
		obj = $( obj );
	}
	if( typeof( obj ) != 'object' ) return;
	if( !obj.style || ( obj.offsetHeight === undefined ) || ( obj.offsetWidth === undefined ) ) return;

	var windowScroll = WindowUtilities.getWindowScroll( document.body );
	var pageSize = WindowUtilities.getPageSize( document.body );

	var top = ( pageSize.windowHeight - obj.offsetHeight ) /2;
	top += windowScroll.top;

	var left = ( pageSize.windowWidth - obj.offsetWidth ) /2;
	left += windowScroll.left;

	obj.style.left = left + 'px';
	obj.style.top = top + 'px';
}

function getMboxNameFromMbox( mbox , mbox_name ) {
	switch( mbox ) {
		case 'INBOX' :
		case 'Me' :
		case 'nSent' :
		case 'Drafts' :
		case 'Spam' :
		case 'Trash' :
			return trans( 'mbox' , mbox );
		default :
			return mbox_name;
	}
}

//---------------------------------------------------------------------------------
//		자바스크립트 md5
//---------------------------------------------------------------------------------
/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);

}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

/**
 * 새 창
 * @param array params
 * @example
 * var winParams = {
 *      url: 'about:blank',
 *      name: 'win',
 *      width: 800,
 *      height: 600,
 *      option: 'status=no'
 * };
 * openWin(winParams);
 */
function openWin(params) {
    var url = params.url ? params.url : null;
    var name = params.name ? params.name : '';
    var width = params.width ? parseInt(params.width) : 0;
    var height = params.height ? parseInt(params.height) : 0;
    var option = params.option ? params.option : 'scrollbars=no,status=no,left=0,top=0';

    if( !url ) return;

    if(width > 0) {
        //width += (document.all) ? 0 : 14;
        option += ',width='+width;
    }
    if(height > 0) {
        height += (document.all) ? 0 : 40;
        option += ',height='+height;
    }

    var popup = window.open(url, name, option);
    popup.focus();
}

/*
 * 텍스트를 html형태로 변환
 */
function text2html(text) {
	if( !text ) return '';
	return text.gsub( /(.*)\r?\n|$/ , function( M ) {
		return '<p>' + ( M[ 1 ] || '' ) + "</p>";
	} ) || text;
}

/*
 * html을 텍스트 형태로 변환
 */
function html2text(html) {
	if( !html ) return '';
	var returnValue = html
	.gsub( /<style[^\>]*>(.*?)<\/style>(?:\r?\n)?/i , '' )
	.gsub( /<script[^\>]*>(.*?)<\/script>(?:\r?\n)?/i , '' )
	.gsub( /<br\s?\/?>\r?\n?/i , "\n" )
	.gsub( /<p[^\>]*>(.*?)<\/p>(?:\r?\n)?/i
		, function( M ) { return M[ 1 ] + "\n"; } )
	.gsub( /&nbsp;/i , ' ' );
	return returnValue.stripTags().replace( /^\s+|\s+$/ , '' );
}

/*
 * tinymce 에디터 설정
 */

function getEditorLang( L ) {
	switch( L ) {
		case 'jp' : return 'ja'; break;
		case 'chGb' : return 'zh'; break;
		case 'chBig' : return 'hr'; break;
		default : return 'ko'; break;
	}
}

function onInsertImageNN( ev ) {
	this.layer.empty( { 'width' : 400 , 'e2' : ev , 'callback' : function() {
		$( '_contents_id_nullLayerPage' )
			.update( new Element( 'fieldset' )
				.insert( new Element( 'legend' ).insert( NNEditor_Lang[ 'image_title' ] ) )
				.insert( new Element( 'table'
						, { 'height' : '100%' , 'width' : '100%' , 'cellspacing' : '1' , 'cellpadding' : '1' , 'border' : '0' } )
					.insert( new Element( 'tbody' )
						.insert( new Element( 'tr' )
							.insert( new Element( 'td' )
								.insert( new Element( 'iframe'
										, { 'id' : '_uploadIFrame' , 'src' : '/upload_form.php?mail='
												+ ( NN.Config.isMail ? 'mail' : '' )
										, 'frameborder' : '0' , 'width' : '100%' , 'height' : '70' , 'marginwidth' : '0' , 'marginheight' : '0' } ) ) ) ) ) ) );
	}.bind( this ) } );
}

var editorSet = {
	'mode' : 'textareas',
	'theme' : 'advanced',
	'language' : getEditorLang( _LANG ),
	'plugins' : 'safari,table,advimage,advlink,contextmenu,paste,nonbreaking,xhtmlxtras,inlinepopups',
	'theme_advanced_buttons1' : 'fontselect,fontsizeselect,|,forecolor,backcolor'
		+ ',|,bold,italic,underline,strikethrough,'
		+ '|,justifyleft,justifycenter,justifyright,justifyfull,'
		+ '|,bullist,numlist,outdent,indent,|,link,unlink,image,tablecontrols',
	'theme_advanced_buttons2' : '',
	'theme_advanced_buttons3' : '',
	'theme_advanced_toolbar_location' : 'top',
	'theme_advanced_toolbar_align' : 'left',
	'theme_advanced_fonts' : ( getSetting( 'editorFonts' ) || '' ).replace( '\'' , '' ),
	'theme_advanced_font_sizes' : '8pt=8pt,9pt=9pt,10pt=10pt,11pt=11pt,12pt=12pt,14pt=14pt,18pt=18pt,24pt=24pt,36pt=36pt',
	'relative_urls' : false,
	'remove_script_host' : false,
	'forced_root_block' : false,

	'extended_valid_elements' : 'link[rel|href|type],iframe[style|src|frameborder],param[name|value]'
		+ ',embed[src|quality|bgcolor|width|height|name'
		+ '|align|allowScriptAccess|type|pluginspage],style[type]',

	'content_css' : '/editor/tinymce/examples/css/content.css'
	,'save_callback'  : 'beforeSaveEditor'
};

function beforeSaveEditor(element_id, html, body) {
	if( $( element_id ).readAttribute( 'modeChange' ) == 'modeChange' ) return html;
	if( getEditorMode( element_id ) != 'WYSIWYG' ) return html;

	if( html.match( /^\s*\<style\s[^\>]*\>(?:\<\!\-\-)?\s*p\s?\{/ ) ) return html;

	return '<style type="text/css">p {margin:3px 0px 2px 0px;}</style>' + html;
}

// progress popup layer
var PopupProgress = Class.create( {
	'defaultOptions' : { 'styles' : { 'width' : '320px' , 'height' : '62px' , 'zIndex' : 2500 , 'float' : 'left' , 'position' : 'absolute' } , 'prefix' : 'progress' , 'start' : true , 'end' : true , 'endCallback' : Prototype.emptyFunction }
	, 'id' : ''
	, 'element' : null
	, 'graph' : null
	, 'percent' : null
	, 'initialize' : function( options ) {
		this.options = $H( this.defaultOptions ).update( options ).toObject();
		this.id = this.options.prefix + new Date().getTime();
		if( this.options.start ) this.start();
	}
	, 'getId' : function() { return this.id || '' }
	, 'start' : function() {
		var progressHtml = '';
		progressHtml += '<table class="roundBox w100p h100p"><tbody><tr><td class="tl"><br/></td><td class="t"><br/></td><td class="tr"><br/></td></tr>';
		progressHtml += '<tr><td class="l"><br/></td><td class="c" style="background-color:#FFFFFF;">';
		progressHtml += '<div class="progress">';
		progressHtml += '<div class="graphContainer">';
		progressHtml += '<span class="graphBg">';
		progressHtml += '<table width="1%" class="graph" id="' + this.id + '_graph">';
		progressHtml += '<tbody>';
		progressHtml += '<tr>';
		progressHtml += '<td class="start"><br/></td>';
		progressHtml += '<td><br/></td>';
		progressHtml += '<td class="end"><div class="baloon"><span id="' + this.id + '_percent"></span></div></td>';
		progressHtml += '</tr>';
		progressHtml += '</tbody>';
		progressHtml += '</table>';
		progressHtml += '</span>';
		progressHtml += '</div>';
		progressHtml += '<table class="indicator">';
		progressHtml += '<tbody><tr>';
		progressHtml += '<td class="start"><br/></td>';
		progressHtml += '<td class="mid"><br/></td>';
		progressHtml += '<td class="end"><br/></td>';
		progressHtml += '</tr>';
		progressHtml += '</tbody></table>';
		progressHtml += '</div>';
		progressHtml += '</td><td class="r"><br/></td></tr>';
		progressHtml += '<tr><td class="bl"><br/></td><td class="b"><br/></td><td class="br"><br/></td></tr></tbody></table>';

		this.element = new Element( 'div' , { 'id' : this.id } ).setStyle( this.options.styles ).update( progressHtml );

		$( document.body ).insert( this.element );

		centerElement( this.id );

		this.graph = $( this.id + '_graph' );
		this.percent = $( this.id + '_percent' )
	}
	, 'update' : function( percent ) {
		if( percent == undefined ) return;
		var percent = parseInt( percent );
		if( percent > 100 ) percent = 100;
		if( percent < 1 ) percent = 0;
		var graphPercent = percent;

		if( graphPercent <= 0 ) graphPercent = 1;
		else if( graphPercent >= 100 ) graphPercent = 99;

		var inst = this;

		this.graph.morph( 'width:' + graphPercent + '%' , {
			duration : 1
			, afterUpdate : function( obj ) {
				var p = parseInt( obj.element.getStyle( 'width' ).replace( '%' , '' ) );
				inst.percent.update( p + '%' );
			}
			, afterFinish : function( obj ) {
				inst.percent.update( percent + '%' );
				if( inst.options.end && ( percent >= 100 ) ) inst.end.bind( inst ).delay( 0.5 );
			}
		} );
	}
	, 'end' : function() {
		$( this.element ).remove();

		if( this.options.endCallback ) this.options.endCallback();

		this.element = null;
		this.options = this.defaultOptions;
		this.id = '';
	}
} );

var TipWindow = Class.create( {
	'defaultOptions' : {
		'styles' : {
			'position' : 'absolute'
			, 'float' : 'left'
			, 'zIndex' : 3000
			, 'backgroundColor' : 'transparent'
			, 'border' : 'none'
		}
		, 'idPrefix' : 'TipWindow'
	}
	, 'id' : ''
	, 'content' : null
	, 'window' : null
	, 'element' : null
	, 'opened' : false
	, 'initialize' : function( contId , options ) {
		this.options = $H( this.defaultOptions ).update( options ).toObject();
		this.id = this.options.idPrefix + '_' + contId;
		this.content = $( contId );
	}
	, 'toggle' : function( ev , relativePosition ) {
		if( this.opened ) {
			return this.close();
		}
		else {
			return this.open( ev , relativePosition );
		}
	}
	, 'open' : function( ev , relativePosition ) {
		if( !this.content ) return;
		if( !this.content.visible() ) this.content.show();

		this.element = Event.element( ev );
		this.element.writeAttribute( 'twContentId' , this.id );
		this.window = $( this.id );

		var relativePostion = relativePosition || 'BC';

		var docBody = $( document.body );
		var obj = this;
		Event.observe( window , 'resize' , function() {
			obj.setPosition( relativePosition );
		} );

		if( !this.window ) {
			this.window = new Element( 'div' , { 'id' : this.id } ).setStyle( this.options.styles );

			docBody.observe( 'click' , function( bEvt ){
				var src = Event.element( bEvt );
				var oId = obj.id;
				if( src.readAttribute( 'twContentId' ) == obj.id ) {
					if( !src.ancestors().find( function( AE ){ return ( $( AE ).readAttribute( 'twContentId' ) == oId ); } ) ) {
						return;
					}
				}
				if( bEvt.clientX < obj.window.offsetLeft ) return obj.close();
				if( bEvt.clientX > ( obj.window.offsetLeft + obj.window.offsetWidth ) ) return obj.close();
				if( bEvt.clientY < obj.window.offsetTop ) return obj.close();
				if( bEvt.clientY > ( obj.window.offsetTop + obj.window.offsetHeight ) ) return obj.close();
			} );

			docBody.insert( this.window );

			if( !this.content.getStyle( 'margin' ) ) {
				this.content.setStyle( 'margin' , '2px' );
			}
			else {
				[ 'Top' , 'Left' , 'Right' , 'Bottom' ].each( function( K ) {
					this.content.setStyle( 'margin' + K , ( parseInt( this.content.getStyle( 'margin' + K ).sub( 'px' , '' ) ) + 2 ).toString() + 'px' );
				} , this );
			}

			this.window.appendChild( this.content );
		}

		this.setPosition( relativePosition );

		this.window.show();

		this.opened = true;

		if( Prototype.Browser.IE ) this.window.focus();
	}
	, setPosition : function( relativePosition ) {
		var wDim = this.window.getDimensions();

		var position = this.element.cumulativeOffset();
		var dimension = this.element.getDimensions();

		var xPos = 0;
		var yPos = 0;

		switch( relativePosition ) {
			default :
			case 'BC' :
				xPos = ( ( position[ 0 ] + ( dimension.width /2 ) ) - ( wDim.width /2 ) ).ceil();
				yPos = ( position[ 1 ] + dimension.height + 1 ).ceil();
				break;
			case 'BL' :
				xPos = position[ 0 ];
				yPos = ( position[ 1 ] + dimension.height + 1 ).ceil();
				break;
			case 'BR' :
				xPos = position[ 0 ] - ( wDim.width - dimension.width );
				yPos = ( position[ 1 ] + dimension.height + 1 ).ceil();
				break;
			case 'TC' :
				xPos = ( ( position[ 0 ] + ( dimension.width /2 ) ) - ( wDim.width /2 ) ).ceil();
				yPos = ( position[ 1 ] - wDim.height - 1 ).ceil();
				break;
			case 'TL' :
				xPos = position[ 0 ];
				yPos = ( position[ 1 ] - wDim.height - 1 ).ceil();
				break;
			case 'TR' :
				xPos = position[ 0 ] - ( wDim.width - dimension.width );
				yPos = ( position[ 1 ] - wDim.height - 1 ).ceil();
				break;
		}

		this.window.setStyle( { 'left' : xPos + 'px' , 'top' : yPos + 'px' } );
	}
	, close : function() {
		toggleObject( false );
		this.opened = false;
		this.window.hide();
	}
} );

TipWindow.allClose = function() {
	toggleObject( false );
	$H( TipWindow.contIds ).each( function( TW ) { TW.value.close(); } );
}

TipWindow.getInstance = function( id , options ) {
	if( TipWindow.contIds === undefined ) TipWindow.contIds = {};
	if( !TipWindow.contIds[ id ] ) {
		TipWindow.contIds[ id ] = new TipWindow( id , options );
	}
	return TipWindow.contIds[ id ];
}

//-----------------------------------------------------------------------------
// 문자열의 byte 길이 반환
// @return : int
//-----------------------------------------------------------------------------
function byteToString(data) {
    var cnt = 0;
    for (var i = 0; i < data.length; i++) {
        if (data.charCodeAt(i) > 127)
            cnt += 2;
        else
            cnt++;
    }
    return cnt;
}

function refreshLogo() {
	var logoCont = $( 'logoCont' );
	if( !logoCont ) return;

	requestFront( '/api/webmail/0.2/WebmailGetLogoInfo.php' , null , function( result ) {
		if( !result || !result.data ) return;

		var type = ( result.data.type.toLowerCase() || 'image' );
		var outerlink = false;
		var link = '/intro.php';
		if( result.data.link ) {
			outerlink = true;
			link = result.data.link;
		}

		try {
			switch( type ) {
				case 'image' :
					if( Prototype.Browser.IE ) {
						var logoImage = new Image();
						logoImage.onload = function() {
							if( logoImage.width > 185 ) {
								var nY = parseInt( Math.floor( ( 185 * logoImage.height ) / logoImage.width ) );
								logoImage.width = 185;
								logoImage.height = nY;
							}
							var cont = document.getElementById( 'logoCont' );
							cont.innerHTML = '';

							var anchor = document.createElement( 'a' );
							if( outerlink ) anchor.target = '_blank';
							anchor.href = link;
							anchor.appendChild( logoImage );
							cont.appendChild( anchor );
						};
						logoImage.id = 'logoImage';
						logoImage.src = '/images/?type=logo';
					}
					else {
						var logoImage = new Element( 'img' , { 'id' : 'logoImage' , 'src' : '/images/?type=logo' , 'alt' : 'Logo Image' } )
							.observe( 'load' , function( ev ) {
								var img = Event.element( ev );
								if( img.width > 185 ) {
									var nY = parseInt( Math.floor( ( 185 * img.height ) / img.width ) );
									img.writeAttribute( { 'width' : 185 , 'height' : nY } );
								}
								$( 'logoCont' )
									.update( new Element( 'a' , { 'href' : link , 'target' : ( outerlink ? '_blank' : '_self' ) } )
										.update( img ) );
							} );
					}
					break;
				case 'flash' :
					if( Prototype.Browser.IE ) {
						logoCont.update( '<object id="logoMovie"'
							+ ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="185" height="60">'
							+ '<param name="movie" value="/images/?type=logo">'
							+ '</object>' );
					}
					else {
						logoCont
							.update( new Element( 'embed'
								, { 'id' : 'logoMovie' , 'src' : '/images/?type=logo' , 'width' : '185px' , 'height' : '60px'  } ) );
					}
					break;
			}
		} catch( err ) {
			throw 'Logo Loading Error';
		}
	} );
}
document.observe( 'dom:loaded' , refreshLogo );