/*
 * Copyright Co 2006-2009 Wecon Communications, Inc. All right reserved.
 */
var StringUtility =
{
	encodeURL: function(str)
	{
		var s0, i, s, u;
	    s0 = "";                // encoded str
	    for (i = 0; i < str.length; i++){   // scan the source
	        s = str.charAt(i);
	        u = str.charCodeAt(i);          // get unicode of the char
	        if (s == " "){s0 += "+";}       // SP should be converted to "+"
	        else {
	            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
	                s0 = s0 + s;            // don't escape
	            }
	            else {                  // escape
	                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
	                    s = "0"+u.toString(16);
	                    s0 += "%"+ s.substr(s.length-2);
	                }
	                else if (u > 0x1fffff){     // quaternary byte format (extended)
	                    s0 += "%" + (0xf0 + ((u & 0x1c0000) >> 18)).toString(16);
	                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
	                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
	                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	                }
	                else if (u > 0x7ff){        // triple byte format
	                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
	                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
	                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	                }
	                else {                      // double byte format
	                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
	                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	                }
	            }
	        }
	    }
	    return s0;
	},
	decodeURL: function(str)
	{
	    var s0, i, j, s, ss, u, n, f;
	    s0 = "";                // decoded str
	    for (i = 0; i < str.length; i++){   // scan the source str
	        s = str.charAt(i);
	        if (s == "+"){s0 += " ";}       // "+" should be changed to SP
	        else {
	            if (s != "%"){s0 += s;}     // add an unescaped char
	            else{               // escape sequence decoding
	                u = 0;          // unicode of the character
	                f = 1;          // escape flag, zero means end of this sequence
	                while (true) {
	                    ss = "";        // local str to parse as int
	                    for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
	                    	sss = str.charAt(++i);
	                        if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
	                        	ss += sss;      // if hex, add the hex character
	                        } else {--i; break;}    // not a hex char., exit the loop
	                    }
	                    n = parseInt(ss, 16);           // parse the hex str as byte
	                    if (n <= 0x7f){u = n; f = 1;}   // single byte format
	                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
	                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
	                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
	                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
	                    if (f <= 1){break;}       // end of the utf byte sequence
	                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
	                    else {break;}             // abnormal, format error
	                }
	                s0 += String.fromCharCode(u); // add the escaped character
	            }
	        }
	    }
	    return s0;	
	},
	getLength: function(str) {
		// 문자열 길이 검사
		return (str.length + (escape(str) + "/%u").match(/%u/g).length-1);
	},
	emailCheck :function(emailValue){
		var email = emailValue.trim();
		if(email == ''){
			return false;
		}else{ return charFormatCheck(email, 'email'); }
	},
	loginAlert:function(){
		toastPopupObj.show('로그인후 이용해 주십시오.');
	},
	ajaxLoginCheck:function(){
		var returnValue = true;
		new Ajax.Request('/common/login/check',{
			asynchronous: false,
			method:'post',
			onSuccess:function(transport) {
				var resp = transport.responseText.evalJSON();
				if(resp.isLogin == false) {
					PopupManager.topClose();
					loginPopup = new Popup('/user/login/pop', {onSuccess: parent.PopupLoginCheck.initLoginForm.bind(parent.PopupLoginCheck)});
					
					returnValue = false;
				}
			}
		});
		return returnValue;
	}
}

var Cookie =
{
	set: function(name, value, daysToExpire)
	{
		var expire = '';
		if (daysToExpire != undefined) {
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
			expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
	},
	get: function(name)
	{
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (cookie ? unescape(cookie[2]) : null);
	},
	erase: function(name)
	{
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	},
	accept: function()
	{
		if (typeof navigator.cookieEnabled == 'boolean') {
			return navigator.cookieEnabled;
		}
		Cookie.set('_test', '1');
		return (Cookie.erase('_test') === '1');
	}
};


//iframe안에서 ajax로 페이지 변경시 이전 내용을 모두 없앤다. 제거
//특히 에디터와 자동저장을 위한 오브젝트 삭제.
function removeAllContents()
{
	var targetFrameWindow = $('target').contentWindow;
	if(targetFrameWindow.tinyMCE)
	{
		if(targetFrameWindow.tinyMCE.get('editor'))
		{
			targetFrameWindow.tinyMCE.remove(targetFrameWindow.tinyMCE.get('editor'));
			
			if(targetFrameWindow.postManager)
			{
				targetFrameWindow.postManager.remove();
			}
		}
	}
	
	if(targetFrameWindow.$('starContents'))
	{
		targetFrameWindow.$('starContents').descendants().reverse().each(function(element){
			element.parentNode.removeChild(element);
		});
	}
	
	if(starMapReadyFlag == true)
	{
		mapFAService.call('showStarDecoration', true);
	}
	starMapDecorationFlag = true;
	starMapDecorationImageFlag = false;
}


function charFormatCheck(str, type)
{
	var charPattern;
	switch(type){
		case(0) :// 첫글자 영문, 영문, 숫자, _ 사용가능
			charPattern = /^[a-zA-Z]{1}[a-zA-Z0-9_]+$/;
			break;
		case(1) :// 영문만 사용가능
			charPattern = /^[a-zA-Z]+$/;
			break;
		case(2) :// 한글만 사용가능
			charPattern = /^[가-힣]+$/;
			break;
		case(3) :// 숫자만 사용가능
			charPattern = /^[0-9]+$/;
			break;
		case(4) :// 영문, 숫자 사용가능
			charPattern = /^[a-zA-Z0-9]+$/;
			break;
		case(5) :// 한글, 영문 사용가능
			charPattern = /^[가-힣a-zA-Z ]+$/;
			break;
		case(6) :// 한글, 숫자 사용가능
			charPattern = /^[가-힣0-9]+$/;
			break;
		case(7) :// 영문, 숫자, 한글 사용가능
			charPattern = /^[가-힣a-zA-Z0-9]+$/;
			break;
		case(8) :// 숫자, 영문 소문자만 사용가능
			charPattern = /^[a-z0-9]+$/;
			break;
		case(9) :// 한글을 포함하는지 여부
			charPattern = /[가-힣]/;
			break;
		case('email') :// 전자우편 형식 검사
			return str.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g) >= 0;
	}
	return charPattern.test(str);
};