//*************************************************************************************
//	ÆÄÀÏ¸í		: sg_util.js
//	ÀÛ¼ºÀÚ		: ¾ÈÀçÇü
//	ÃÖÃÊ ÀÛ¼ºÀÏ	: 2003³â 7¿ù 21ÀÏ
//	ÃÖÁ¾ ¼öÁ¤ÀÚ	: ¾ÈÀçÇü
//	ÃÖÁ¾ ¼öÁ¤ÀÏ	: 2005³â 10¿ù 18ÀÏ
//*************************************************************************************

//*************************************************************************************
//	ÀÌ¸§	: removeCRLF()
//	ÀÎÀÚ	: str	- µ¥ÀÌÅÍ ½ºÆ®¸µ
//	¹ÝÈ¯°ª	: '\r'°ú '\n'ÀÌ Á¦°ÅµÈ µ¥ÀÌÅÍ ½ºÆ®¸µ
//	±â´É	: µ¥ÀÌÅÍ ½ºÆ®¸µÀ¸·ÎºÎÅÍ '\r'°ú '\n'À» Á¦°ÅÇÏ¿© ¹ÝÈ¯ÇÑ´Ù
//*************************************************************************************
function removeCRLF( str )
{
	var i = 0;
	var buf = "";
	for( i = 0; i < str.length; i++ )
	{
		if ( str.charAt(i) != '\n' && str.charAt(i) != '\r' )
		{
			buf += str.charAt(i);
		}
	}
	return buf;
}

//*************************************************************************************
//	ÀÌ¸§	: getLocalPath()
//	ÀÎÀÚ	: strFileName	- ÆÄÀÏÀÇ »ó´ë °æ·Î
//	¹ÝÈ¯°ª	: ¼º°ø	- ÆÄÀÏÀÇ Àý´ë °æ·Î
//		  ½ÇÆÐ	- ""
//	±â´É	: ·ÎÄÃ ÆÄÀÏ ½Ã½ºÅÛ¿¡ ÀúÀåµÈ HTMLÆÄÀÏ¿¡¼­
//		  ´Ù¸¥ ÆÄÀÏÀÇ »ó´ë °æ·Î¸¦ Àý´ë °æ·Î·Î ¹Ù²Ù¾î ÁØ´Ù
//*************************************************************************************
function getLocalPath( strFileName )
{
	var strLocalPath = "";
	var pos = 0;
	var i = 0;
	
	if ( strFileName == null || strFileName == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var buf = location.href.substring( 0, 8 );
	if ( buf != "file:///" )
	{
		setErrorCode( "NOT_LOCAL_FILE" );
		setErrorMessage( "" );
		return "";
	}
	
	buf = location.href.substring( 8, location.href.length );
	
	for ( i = 0; i < buf.length; i++ )
	{
		if ( buf.charAt(i) == "/" )
		{
			strLocalPath += "\\";
			pos = strLocalPath.length;
		} else if ( buf.charAt(i) == "%"
				&& buf.charAt(i+1) == "2"
				&& buf.charAt(i+2) == "0" )
		{
			strLocalPath += " ";
			i += 2;
		} else {
			strLocalPath += buf.charAt(i);
		}
	}
	
	strLocalPath = strLocalPath.substring( 0, pos ) + strFileName;

	return strLocalPath;
}

//*************************************************************************************
//	ÀÌ¸§	: insertLFtoPEMCert()
//	ÀÎÀÚ	: strCert	- '\n' ¹®ÀÚ¸¦ Á¦°ÅÇÑ PEM Çü½Ä ÀÎÁõ¼­
//	¹ÝÈ¯°ª	: ¼º°ø	- '\n'ÀÌ Æ÷ÇÔµÈ PEM Çü½Ä ÀÎÁõ¼­
//		  ½ÇÆÐ	- ""
//	±â´É	: '\n'À» Á¦°ÅÇÑ PEM Çü½Ä ÀÎÁõ¼­¿¡ '\n'À» ´Ù½Ã Ãß°¡ÇÑ´Ù
//*************************************************************************************
function insertLFtoPEMCert( strCert )
{
	if ( strCert == null || strCert == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		setErrorFunctionName( "insertLFtoPEMCert()" );
		return "";
	}

	var pemHeader	= "-----BEGIN CERTIFICATE-----";
	var pemTrailer	= "-----END CERTIFICATE-----";
	var buf = removeCRLF( strCert );

	var i = 0;
	var nCount = 0;
	for ( i = 0; i < pemHeader.length; i++ )
	{
		if ( pemHeader.charAt( i ) == buf.charAt( i ) )
		{
			nCount = nCount + 1;
		}
	}
	if  ( nCount != pemHeader.length )
	{
		setErrorCode("NOT_PEM_CERT");
		setErrorMessage( "" );
		setErrorFunctionName( "insertLFtoPEMCert()" );
		return "";
	}

	nCount = 0;
	for ( i = 0; i < pemTrailer.length; i++ )
	{
		if ( pemTrailer.charAt( i ) == buf.charAt( buf.length - pemTrailer.length + i ) )
		{
			nCount = nCount + 1;
		}
	}
	if  ( nCount != pemTrailer.length )
	{
		setErrorCode("NOT_PEM_CERT");
		setErrorMessage( "" );
		setErrorFunctionName( "insertLFtoPEMCert()" );
		return "";
	}

	var strPEMCert = "";
	nCount = 0;
	for ( i = 0; i < buf.length - pemHeader.length - pemTrailer.length; i++ )
	{
		strPEMCert += buf.charAt( i + pemHeader.length );
		nCount = nCount + 1;
		if ( nCount == 64 )
		{
			strPEMCert += '\n';
			nCount = 0;
		}
	}

	strPEMCert = pemHeader + "\n" + strPEMCert + "\n" + pemTrailer;
	
	return strPEMCert;
}

//*************************************************************************************
//	ÀÌ¸§	: fileCopy()
//	ÀÎÀÚ	: inFilePath	- ¿øº» ÆÄÀÏ Àý´ë °æ·Î
//			  outFilePath	- ¸ñÇ¥ ÆÄÀÏ Àý´ë °æ·Î
//	¹ÝÈ¯°ª	: ¼º°ø	- 'SUCCESS'
//		  	  ½ÇÆÐ	- ""
//	±â´É	: ÆÄÀÏ º¹»ç
//*************************************************************************************
function fileCopy( inFilePath, outFilePath )
{
	
	if ( inFilePath == null || inFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	if ( outFilePath == null || outFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var strResult = FileCopy(inFilePath, outFilePath);
	if ( strResult == null || strResult == "" )
	{
		setErrorCode("");
		setErrorMessage( GetLastErrMsg() );
		setErrorFunctionName( "fileCopy()" );
		return "";
	}

	return strResult;

}

//*************************************************************************************
//	ÀÌ¸§	: fileRead()
//	ÀÎÀÚ	: inFilePath	- ¿øº» ÆÄÀÏ Àý´ë °æ·Î
//			  TypeFlag		- ¹®ÀÚ¿­ Format Type [0:pass, 1:base64 EncodingÈÄ ¹ÝÈ¯, 2: Base64 Decoding ÈÄ ¹ÝÈ¯
//	¹ÝÈ¯°ª	: ¼º°ø	- 'ÆÄÀÏ³» content¸¦ TypeFlag·Î º¯È¯ÇÑ ¹®ÀÚ¿­'
//		  	  ½ÇÆÐ	- ""
//	±â´É	: ÆÄÀÏ ÀÐ±â ( TypeFlag¸¦ È°¿ëÇÏµÇ ÇØ´ç ¿øº» ÆÄÀÏÀÇ Content TypeÀ» °¨¾ÈÇØ¼­ È£ÃâÇÊ¿ä
//*************************************************************************************
function fileRead( inFilePath, TypeFlag )
{
	
	if ( inFilePath == null || inFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var strResult = FileRead(inFilePath, TypeFlag);
	if ( strResult == null || strResult == "" )
	{
		setErrorCode("");
		setErrorMessage( GetLastErrMsg() );
		setErrorFunctionName( "fileRead()" );
		return "";
	}

	return strResult;
}

//*************************************************************************************
//	ÀÌ¸§	: fileRename()
//	ÀÎÀÚ	: inFilePath	- ¿øº» ÆÄÀÏ Àý´ë °æ·Î
//			  outFilePath	- ¸ñÇ¥ ÆÄÀÏ Àý´ë °æ·Î
//	¹ÝÈ¯°ª	: ¼º°ø	- 'SUCCESS'
//		  	  ½ÇÆÐ	- ""
//	±â´É	: ÆÄÀÏ¸í º¯°æ
//*************************************************************************************
function fileRename(inFilePath, outFilePath )
{
	
	if ( inFilePath == null || inFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	if ( outFilePath == null || outFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var strResult = FileRename(inFilePath, outFilePath);
	if ( strResult == null || strResult == "" )
	{
		setErrorCode("");
		setErrorMessage( GetLastErrMsg() );
		setErrorFunctionName( "fileRename()" );
		return "";
	}

	return strResult;

}

//*************************************************************************************
//	ÀÌ¸§	: fileChange()
//	ÀÎÀÚ	: inFilePath	- ¿øº» ÆÄÀÏ Àý´ë °æ·Î
//			  outFilePath	- ¸ñÇ¥ ÆÄÀÏ Àý´ë °æ·Î
//	¹ÝÈ¯°ª	: ¼º°ø	- 'SUCCESS'
//		  	  ½ÇÆÐ	- ""
//	±â´É	: ÆÄÀÏ ±³Ã¼
//*************************************************************************************
function fileChange(inFilePath, outFilePath )
{
	
	if ( inFilePath == null || inFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	if ( outFilePath == null || outFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var strResult = FileChange(inFilePath, outFilePath);
	if ( strResult == null || strResult == "" )
	{
		setErrorCode("");
		setErrorMessage( GetLastErrMsg() );
		setErrorFunctionName( "fileChange()" );
		return "";
	}

	return strResult;

}

//*************************************************************************************
//	ÀÌ¸§	: makeDir()
//	ÀÎÀÚ	: inFilePath	- »ý¼ºÇÏ°íÀÚÇÏ´Â µð·ºÅä¸® Àý´ë °æ·Î
//	¹ÝÈ¯°ª	: ¼º°ø	- 'SUCCESS'
//		  	  ½ÇÆÐ	- ""
//	±â´É	: µð·ºÅä¸® »ý¼º
//*************************************************************************************
function makeDir(inFilePath)
{
	
	if ( inFilePath == null || inFilePath == "" )
	{
		setErrorCode("NO_DATA_VALUE");
		setErrorMessage( "" );
		return "";
	}
	
	var strResult = MakeDir(inFilePath);
	if ( strResult == null || strResult == "" )
	{
		setErrorCode("");
		setErrorMessage( GetLastErrMsg() );
		setErrorFunctionName( "makeDir()" );
		return "";
	}

	return strResult;

}
