//문자열의 좌우 공백제거
function trim ( str )
{
	for ( var start=0 ; start < str.length ; start++ )
	{
		if ( str.charAt(start) != ' ' )
			break;
	}

	for ( var end=str.length-1 ; end >= 0 ; end-- )
	{
		if ( str.charAt(end) != ' ' )
			break;
	}

	if ( start > end )
		return "";

	return str.substring ( start, end+1 );
}

//소수점 둘째자리 반올림
function round ( num )
{
	var num1 = String(num).split(".")[1];

	if(num1.length <= 2)
		return num;
	else
		return Math.round(Number(num)*100) / 100;
}

// 소수점 네자리 반올림
function round1 ( num )
{
    var no = String( num ).split(".")[1];
    
    if ( no.length <= 4 )
        return num ;
    else
        return Math.round (  Number( num ) * 10000 )/ 10000  ;
}

//프로그래스 로딩바 숨기기
function loadingbar_hide ( )
{
	if (document.getElementById)
		document.getElementById('loadingbar').style.visibility = 'hidden'; //none
	else
	{
		if (document.layers)
			document.loadingbar.visibility = 'hide'; //hide
		else
			document.all.loadingbar.style.visibility = 'hidden';
	}
}

//프로그래스 로딩바 보이기
function loadingbar_show ( )
{
	if (document.getElementById)
		document.getElementById('loadingbar').style.visibility = 'visible'; //block
	else
	{
		if (document.layers)
			document.loadingbar.visibility = 'show';
		else
			document.all.loadingbar.style.visibility = 'visible';
	}

//	parent.left.flag_enable = 0;
}

// 리셋  ( input box )
function reset_input()
{
	_Input = document.all.tags("INPUT");

    if ( _Input.length )
    {
    	for (i=0; i<_Input.length; i++)
    	{
    		if( _Input[i].type == "text")
                _Input[i].value = "" ;
            if ( _Input[i].type == "radio" )
                _Input[i].checked = false ;
            if ( _Input[i].type == "password" )
                _Input[i].value = "" ;     
    	}
    }
}

//리셋 ( select box )
function reset_select ( )
{
    _Select = document.all.tags ( "SELECT" ) ;

    for ( i= 0; i < _Select.length;i++ )
    {
         _Select[i].options[0].selected = true ;
    }
}

//리셋 all
function reset_all ( )
{
    _Input = document.all.tags("INPUT");

    if ( _Input.length )
    {
    	for (i=0; i<_Input.length; i++)
    	{
    		if( _Input[i].type == "text")
                _Input[i].value = "" ;
            if ( _Input[i].type == "radio" )
                _Input[i].checked = false ;
            if ( _Input[i].type == "password" )
                _Input[i].value = "" ;     
            if ( _Input[i].type == "checkbox" )
                _Input[i].checked = false ;
    	}
    }
    _Select = document.all.tags ( "SELECT" ) ;

    for ( i= 0; i < _Select.length;i++ )
    {
         _Select[i].options[0].selected = true ;
    }
}
// (')작은따옴표 처리
function add_Quotation  ( str )
{
    var temp = "" ;

    for ( i =0 ; i < str.length ; i++)
    {
        if ( str.charAt(i) == "'" ) temp += "''" ;
        else temp += str.charAt ( i ) ;
    }
    return trim ( temp ) ;
}

// 영자만을 기입
function only_Eng ( obj )
{
    str = obj.value ;
    for ( i =0 ; i < str.length ; i++ )
    {
	    code = str.charCodeAt(i);
	    if ( ( code < 65 ) || ( code > 90 ) )         //  65  <  code < 90
		    if ( ( code < 97 ) || ( code > 122 ) )    //   97  <  code < 122
		    {
		        alert ( "영문자만 입력가능합니다." );
		        obj.select () ;
		        return false;
		    }
    }
    return true ;
}

//숫자만을 기입
function only_Number ( obj )
{
    str = obj.value ;
    for ( i =0 ; i < str.length ; i++ )
    {
        code = str.charCodeAt(i);
   	    if( ( code < 48 ) || ( code > 57 ) )    // 48 < code < 57
   	    {
      		alert ( "숫자만 입력 가능합니다. " ) ;
      		obj.select () ;
      		return false ;
      	}
    }
    return true;
}

//한글만 입력
function only_Kor ( obj )
{
    str = obj.value ;
    for ( i =0; i< str.length ; i++ )
    {
        code = str.charCodeAt(i);
        if ( code > 128 )           // code < 128
        {
            continue ;
        }
        else
        {
            alert ( "한글만 입력 가능합니다." ) ;
            obj.select ();
            return false ;
        }
    }
    return true ;
}

// 한글 포함한 문자열 크기
// return integer
function getStringLength ( str )
{
	var count = 0;

	for ( i = 0 ; i < str.length ; i++ )
	{
		var code = str.charCodeAt ( i );
		if ( code > 128 ) count += 2; // 한문자가 한글일 경우
		else count++;
	}
	return count;
}

// 한글 포함한 문자열 크기 검사
// return boolean
function checkLength ( obj )
{
	var str = obj.value;
	var str_length = getStringLength ( str );
	var max_length = obj.maxLength;

	if ( str_length > max_length )
	{
		alert ( "영문은 " + max_length + "글자,  한글은 그것의 반크기 입력할 수 있습니다." );
		obj.value = str.substring ( 0, obj.value.length - ( str_length - max_length ) / 2 );
		obj.select ( );
		//obj.select ( );

		return false;
	}
	return true ;
}

//날짜유효성검색
function isDate ( y, m, d )
{
    if ( !isYear ( y ) )
        return false ;

    if ( !isMonth ( m ) )
        return false ;

    if ( !isDay ( y, m, d ) )
        return false;

    return true;
}

//년도 검색
function isYear ( y )
{
    if ( !only_Number ( y ) )
        return false;

    if ( trim ( y.value ).length != 4 )
    {
        alert ( "년도의 입력범위가 틀렸습니다." ) ;
        y.select ( );
        return false;
    }
    return true;
}

//월검색
function isMonth ( m )
{
    if ( !only_Number ( m ) )
        return false;

    month = parseInt ( trim ( m.value ) );

    if ( month < 1 || month > 12 )
    {
        alert ( "월의 입력범위가 틀렸습니다." );
        m.select ( );
        return false ;
    }
    else
        return true ;
}

//일 검색
function isDay ( y, m, d )
{
    if ( !only_Number ( y ) )
        return false;
    if ( !only_Number ( m ) )
        return false;
    if ( !only_Number ( d ) )
        return false;

    year    = parseInt ( trim ( y.value ) );
    month   = parseInt ( trim ( m.value ) );
    day     = parseInt ( trim ( d.value ) );

    var _day = new Array();
    _day[0]  = 31
	_day[1]  = ( yoonYear ( year ) ) ? 29 : 28
	_day[2]  = 31
	_day[3]  = 30
	_day[4]  = 31
	_day[5]  = 30
	_day[6]  = 31
	_day[7]  = 31
	_day[8]  = 30
	_day[9]  = 31
	_day[10] = 30
	_day[11] = 31

    if ( day > _day[ month-1 ] || day < 1 )
    {
        alert ( "일의 입력범위가 틀렸습니다." );
        d.select ( );
        return false;
    }
    else
        return true;
}

//윤년계산
function yoonYear( y )
{
	if ( year % 4 == 0 && year % 100 != 0  )
	    return true;
    else
        return false;
}

// 월, 일 0붙이기
function addObjZero ( obj )
{
    str = obj.value ( ) ;
    if ( str.length == 1 )
        str = "0"+str ;

    return str;
}

function addStrZero ( str )
{
    if ( str.length == 1 )
        str = "0"+str;
    return str ;
}

// 탭 이동
function moveTab( obj, e )
{
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	var len = obj.maxLength;
	if(obj.value.length >= len && !containsElement(filter,keyCode))
	{
		obj.value = obj.value.slice(0, len);
		obj.form[(getIndex(obj)+1) % obj.form.length].focus();
	}
	function containsElement(arr, ele)
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
		return found;
	}
	function getIndex(obj)
	{
		var index = -1, i = 0, found = false;
		while (i < obj.form.length && index == -1)
		if (obj.form[i] == obj)index = i;
		else i++;
		return index;
	}
	return true;
}

// 숫자만 기입 ( string )
/**
//function isNum ( str )
//{
//    for ( i =0 ; i < str.length ; i++ )
//    {
//        code = str.charCodeAt(i);
//   	    if( ( code < 48 ) || ( code > 57 ) )    // 48 < code < 57
//   	    {
//      		alert ( "숫자만 입력 가능합니다. " ) ;
//      		return false ;
//      	}
//    }
//    return true;
//}
*/

// 숫자 확인하기
// return boolean
function isNum( str ){
	 var strVal = "-0123456789" ;
	 for (i=0; i< str.length; i++)
	 {
		  ch = str.charAt(i) ;
		  for(j=0; j< strVal.length; j++)
  		  	if(ch == strVal.charAt(j))
  			   break;
    	  if( j == strVal.length )
    	  {
    	    alert ( " '숫자' 와 '-'만 입력 가능합니다.");
    		 return false;
          }    		 
  	 }
  return true;
}
// 콤마 추가, 정수부 길이 체크, 소수점 체크( object, 정수 자리수, 소수자리수 )
function setComma( obj, num, point )
{
        str =  obj.value ;
        var strTemp = "";
        var strArr ;
        var flag = "1";
        var reValue = "";
        var minus = "" ;
        str = checkZero ( str );      

        if ( str.indexOf ( "-" ) != -1 )
        {
            minus = "-";
            str = str.substring ( 1, str.length );            
        }            

        if ( str.indexOf ( "." ) == -1 )
            strTemp = del3Comma ( str ) ;
        else
        {
            strArr = str.split ( "." ) ;
            strTemp = del3Comma ( strArr[0] );
            flag = "2" ;
        }
        
        if ( !isNum ( strTemp ) )
            obj.select ( );

        if ( flag == 2 )
        {
            if ( !isNum ( strArr[1] ) )
                obj.select ( );

            if ( strArr[1].length > point )
            {
                alert ( "소숫점이하 " + point +" 자리만 입력가능합니다.");
                obj.select ( );
            }
        }

        if ( strTemp.length <= num )
        {
            for( i=0; i< strTemp.length; i++)
            {
                    if(i > 0 && (i%3)==0)
                        reValue = strTemp.charAt(strTemp.length - i -1) + "," + reValue;
                    else
                        reValue = strTemp.charAt(strTemp.length - i -1) + reValue;
            }

            if ( flag == 2 )
                reValue = reValue + "." + strArr[1] ;
            reValue = minus + reValue ;
            obj.value = reValue ;
        }
        else
        {
            alert ( "정수부분은 " + num + "자리 이하만 입력가능합니다." );
            obj.select ( );
        }
}

// 컴마 삭제 ( string )
function del3Comma ( str )
{
	str = trim ( str );
	var str_new = "";

    str = checkZero ( str );
    
	if ( str.length != 0 )
		for ( var i = 0 ; i < str.length ; i++ )
			if ( str.charAt (i) != ',' )
				str_new += str.charAt (i);
	
	return str_new;
}

function checkZero ( str )
{
    while( str.indexOf ('0') == 0 )
    {
        str = str.substring ( 1, str.length );
        if ( str == "" )
        {
            str = "0";
            break;
        }                
    }
    
    return str ;    
}
// '-' 부호삭제 ( str )
function delMinus ( str )
{
    if ( trim ( str ).indexOf ( "-" ) == 0 )
        str = str.substring ( 1, str.length );
            
    return str ;
}


// 자리수 체크 ( string )
function checkStrPosition (str, num, point )
{
    if ( str.indexOf ( "." ) == -1 )
    {
        if ( str.length > num )
        {
            alert ( "정수부는 " + num + "자리 까지 입력 가능합니다" );
            return false;
        }
    }
    else
    {    
        strTemp = str.split ( "." ) ;
        
        if ( strTemp[0].length > num || strTemp[1].length > point )
        {
            alert ( "정수부는 "+ num +"자리 "+"수수부는 "+point+"자리 까지만 입력 가능합니다." );
            return false ;
        }
    }        
    return true ;
}

// 자리수체크 ( object )
function checkObjPosition ( obj, num , point )
{
    strTemp = obj.value ;
    
    if ( checkStrPosition ( strTemp, num, point ) == false )
    {
        obj.select();
        return false;
    }
    return true;        
}

// 숫자, - , . 만 가능 
function isNumAndMinusDot( obj ){
	 
	 var strVal = "-0123456789." ;
	 var str = trim(obj.value);
	 
	 var cnt = 0;
	 
	
	 
	 for (i = 0; i < str.length; i++)
	 {
		  ch = str.charAt(i) ;
		  
		  if ( strVal.indexOf(ch) == -1 ){
		    cnt++;
		    
	        break;      	      	 
         }
  	 }
  	 
  	 if ( cnt*1 > 0 ) 
        {
    	     alert( " '숫자' , '-', '.' 만 입력 가능합니다.");
    	     obj.select();
  			 return false; 
     } 
        
        
    
  	 if ( (str.indexOf("-") == -1  || str.split("-").length > 2 ) && cnt*1 > 0 ) 
  	 {
  	        alert ("  값을 정확히 입력해 주세요 ");
    	    obj.select();
    	    return false
  	 }
  	 
  	  	    
  	 if (str.split(".").length > 2  || str.charAt(str.length-1) == '.' )
  	  { 
    	    alert (" Ҽ Է ߸ ϴ " )
            obj.select();    	   
    	    return false;
     }      
  	 
  	 
   return true;
}

