﻿// 로그인창
function fnOpenLogin()
{
    fnWinOpen('/common/pop_login.aspx', 'login', 325, 198);
}

// 비밀번호변경
function fnOpenPassword()
{
    fnWinOpen('/common/pop_password.aspx', 'password', 325, 284);
}

// 파일 다운로드 (파일위치, 원래파일명)
function fnDownload(filepath, filename)
{
    var url = '/common/download.aspx?filepath=' + filepath + '&filename=' + escape(filename);
    window.open(url, 'download', 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=10,height=10');
}


//------------------------------------------------------------------------------------
// String 프로토타입정의
//------------------------------------------------------------------------------------

// 양쪽 빈 공간 제거
String.prototype.trim = function() 
{
    return this.replace(/(^\s*)|(\s*$)|($\s*)/g, "");
}

// 모든 텍스트 변경
String.prototype.replaceAll = function(oldValue, newValue)
{
	var retValue = this;

	while (retValue.indexOf(oldValue) >= 0)
	{
		retValue = retValue.replace(oldValue, newValue);
	}

	return retValue;
}


//------------------------------------------------------------------------------------
// 창사이즈
//------------------------------------------------------------------------------------

function fnGetClientHeight()
{
    var clientHeight;

    switch(Sys.Browser.agent) 
    {
        case Sys.Browser.InternetExplorer:
            clientHeight = document.documentElement.clientHeight - 2;
            break;
        case Sys.Browser.Safari:
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default: // Sys.Browser.Firefox, etc.
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    
    return clientHeight;
}

function fnGetClientWidth()
{
    var clientWidth;

    switch(Sys.Browser.agent) 
    {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth - 2;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            break;
        default: // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            break;
    }
    
    return clientWidth;
}


//------------------------------------------------------------------------------------
// textarea 글자수 제한
//------------------------------------------------------------------------------------

// textarea 글자수 제한 (단순 길이로 계산)
// 예제 : <textarea onkeyup="fnMaxLength(this, 10);"></textarea>
function fnMaxLength(obj, maxlen)
{
	if(obj.value.length > maxlen)
	{
		alert(maxlen + '자까지 입력이 가능합니다.');
		obj.value = obj.value.substr(0, maxlen);
	}

	obj.focus();
	
	return obj.value.length;
}

// textarea 글자수 제한 (byte로 계산)
// 예제 : <textarea onkeyup="fnMaxLengthBytes(this, 10);"></textarea>
function fnMaxLengthBytes(obj, maxlen)
{
	var li_byte = 0;

	for(var i=0; i < obj.value.length; i++)
	{
		if(escape(obj.value.charAt(i)).length > 4)
		{
			li_byte += 2;
		}
		else
		{
			li_byte++;
		}
	}

	if(li_byte > maxlen)
	{
		alert(maxlen + 'bytes까지 입력이 가능합니다.');
		obj.value = fnMsgCut(obj.value, maxlen);
		li_byte = maxlen;
	}

	obj.focus();
	return li_byte;
}

// byte 단위로 문자열 자르기
function fnMsgCut(msg, maxlen)
{
	var li_byte = 0;
	var retMsg = '';

	for(var i=0; i < msg.length; i++)
	{
		var ch = msg.charAt(i);
		if(escape(ch).length > 4)
		{
			li_byte += 2;
		}
		else
		{
			li_byte++;
		}

		if(li_byte > maxlen) { break; }

		retMsg += ch;
	}

	return retMsg;
}

// 글자수 제한
function fnMaxLengthByte(text)
{
	var li_byte = 0;

	for(var i=0; i < text.length; i++)
	{
		if(escape(text.charAt(i)).length > 4)
		{
			li_byte += 2;
		}
		else
		{
			li_byte++;
		}
	}

	return li_byte;
}




//------------------------------------------------------------------------------------
// 기타 기능성 함수
//------------------------------------------------------------------------------------

// 이미지 경로 수정하기
function fnImageSrcReplace(obj, str1, str2)
{
    obj.src = obj.src.replace(str1, str2);
}

// 컨트롤의 위쪽 위치를 가져온다
function GetRealOffsetTop(o)
{
    return o ? o.offsetTop + GetRealOffsetTop(o.offsetParent) : 0;
}

// 컨트롤의 왼쪽 위치를 가져온다
function GetRealOffsetLeft(o)
{
    return o ? o.offsetLeft + GetRealOffsetLeft(o.offsetParent) : 0;
}

// 팝업
function fnWinOpen(url, name, width, height)
{
    return window.open(url, name, 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+width+',height='+height);
}

// 팝업
function fnWinOpen(url, name, width, height, top, left)
{
    return window.open(url, name, 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+width+',height='+height+',top='+top+',left='+left);
}