﻿function EnterCheck(nextobj) {
	nextobj	= document.getElementById(nextobj)
	
	try{
			if(event.keyCode ==13){  
					//nextobj.focus();
					//event.returnValue = false;
					//return false;
			}
	}
	catch(exp){
			/*alert('객체가 없습니다.');*/
	}
}

/*엔터키인지아닌지를 확인후 ture fasle를 리턴한다*/
function f_KeyIsEnterNextFunction()
{
	if(event.keyCode == 13)
	{
		event.returnValue = false;
		return true;
	}
	else
	{
		
		event.returnValue = true;
		return false;
	}
}

function f_setZero() {
    var obj = event.srcElement;
    if (obj.value == '') {
        obj.value = 0;
    }
}

/*숫자만 입력 가능*/
function f_keyIsNum()
{
	var e = event.keyCode;
	var ctrl = window.event.ctrlKey;
	
	//(ctrl && e == 67 ) ctrl + c 복사
	//(ctrl && e == 86) ctrl + v 붙여넣기

	if ((e >= 48 && e <= 57) || (e >= 96 && e <= 105) || e == 8 || e == 46 || e == 37 || e == 39 || e == 109 || e == 189 || e == 35 || e == 36 || e == 9 || e == 17 || (ctrl && e == 67 ) || (ctrl && e == 86))
		event.returnValue = true;
	else
		event.returnValue = false;
}

/*소수점입력가능*/
function f_keyIsFloat()
{
	var e = event.keyCode;
	var ctrl = window.event.ctrlKey;
	
	if ((e >= 48 && e <= 57) || (e >= 96 && e <= 105) || e == 8 || e == 46 || e == 37 || e == 39 || e == 109 || e == 189 || e == 35 || e == 36 || e == 9 || e == 190 || e == 110 || (ctrl && e == 67 ) || (ctrl && e == 86))
	{
		if(e == 190 || e == 110)
		{
			 var obj = event.srcElement;
			 var value = obj.value;
			 
			 if(value.indexOf(".") >= 0)
			 {
				event.returnValue = false;
			 }
			 else
			 {
				event.returnValue = true;
			 }
		}
		else
		{
			event.returnValue = true;
		}
	}
	else
	{
		event.returnValue = false;
	}
}

/*특수문자 입력 불가*/
function f_keyIsNotSpeciaChar()
{
	var e = event.keyCode;

	event.returnValue = true;

	
	/*
	if ( e == 16 || e == 191 || e == 222 || e == 220 || e == 111 || e == 187)
		event.returnValue = false;
	else
		event.returnValue = true;
	*/
}

function f_onKeyManage()
{
	var e = event.keyCode;
	var Obj = event.srcElement;
	if (e == 37 || e == 39 || e == 9) return;

	remove_minus(Obj);
	remove_zero(Obj);
	Obj.value = add_comma(Obj.value);
}

/*포커스 갖었을때 0이면 0선택*/
function f_onFocusManage()
{
    var Obj = event.srcElement;
    if (Obj.value == 0) {
        Obj.select();
    }
}

/*'-' 제거*/
function remove_minus()
{
	var Obj = event.srcElement;
	var oVal = Obj.value.replace(/,/g,'');
	var cut_idx = 0;

	if (oVal.charAt(0) == '-')
	{
		Obj.value = '-' + Obj.value.replace(/-/g,'');
	}
	else
	{
		Obj.value = Obj.value.replace(/-/g,'');
	}
}

/*멘 앞자리의 '0' 제거*/
function remove_zero()
{
	var Obj = event.srcElement;
	var oVal = Obj.value.replace(/,/g,'');
	var cut_idx = 0;
	var start_idx = 1;

	if (oVal.charAt(0) == '-') start_idx = 2;

	if (oVal.length > start_idx)
	{
		for (i=start_idx-1; i<oVal.length-1; i++)
		{
			if (oVal.charAt(i) == '0') cut_idx = i+1;
			else break;
		}

		oVal = oVal.substring(cut_idx);
		Obj.value = oVal;
	}
}

/*3자리마다 ',' 추가*/
function add_comma(num)
{
    num = num.toString(); //num이 숫자형으로 넘어올 경우 오류를 발생하므로 형변환한다.
	var num_amount = '';
	var flot = '';
	
	if(num.indexOf(".") >= 0)
	{
		num_amount = num.substring(0, num.indexOf("."));
		flot		= num.substring(  num.indexOf("."), num.length  );
	}
	else
	{
		num_amount = num.toString();
	}
	
	
	
	
	var fmt_amount = '';
	var minus_flag = '';

	num_amount = num_amount.replace(/,/g,'');

	if (num_amount.charAt(0) == '-')
	{
		minus_flag = 'Y';
		num_amount = num_amount.substring(1);
	}


	if (num_amount.length > 3)
	{
		var str1 = num_amount.substring(0, num_amount.length%3);
		var str2 = num_amount.substring(num_amount.length%3, num_amount.length);

		if (str1.length != 0) str1 += ',';

		fmt_amount += str1;

		for (i=0; i<str2.length; i++)
		{
			if (i%3 == 0 && i != 0) fmt_amount += ',';
			fmt_amount += str2.charAt(i);
		}			
	}
	else
	{
		fmt_amount = num_amount;
	}

	if (minus_flag == 'Y') fmt_amount = '-' + fmt_amount;

	return fmt_amount + flot;
}

/*주민번호*/
function Jumin(Msg)
{
	var Obj = event.srcElement;
	var num = Obj.value.replace(/,/g,'');

	var val			= 0;
	var NewValue	= '';

	/*6자리 뒤에 '-' 표시*/
	if (  num.length > 6  && num.length <= 14 )
	{
		if ( num.charAt( 6 ) != '-' )
		{
			NewValue = num.substring( 0, 6 ) + '-' + num.substring( 6 );
			num = NewValue;
		} 
	}
	
	/*14자리 까지만 표시*/
	if ( num.length > 14 )	
	{
		num = num.substring( 0, 14 ); 
	}

	
	/*주민번호 형식 체크*/
	if( num.length == 14 )
	{
		var SPnum = num.split('-');
		
		check = true ;
		var val = 0;
		for (var i = 0; i <=5 ; i++)
		{
			val = val + ((i%8+2) * parseInt(SPnum[0].substring(i,i+1)));
		}
		for (var i = 6; i <=11 ; i++)
		{ 
			val = val + ((i%8+2) * parseInt(SPnum[1].substring(i-6,i-5)));
		}
		val = 11 - (val %11);
		val = val % 10;
		
		//if (val != SPnum[1].substring(6,7))  
		//	check =  false;

		//if (check == false)
		//{ 
			//alert("주민등록번호가 형식에 맞지 않습니다.");
			//num = ''; 
		//	Obj.focus();
		//}
	}


	Obj.value = num;
}

function fn_sortTable(sTableID, iCol, sDataType) 
{
	var oTable = document.getElementById(sTableID);                  // 객체생성
							
	var tableColLength = oTable.rows(0).cells.length;           // Cols 갯수
				
	for(i=0; i <tableColLength; i++)
	{
		if(iCol != i)
		{
		    try
		    {
			    document.all[sTableID+"_sort_"+i].value = '0';
			    document.all[sTableID+"_sort_"+i].src = '/KOR_WEBROOT/IMAGE/COMMON/img_sort_default.gif';
			}
			catch(err)
			{
			
			}
		}
	}
	
	if( document.all[sTableID+"_sort_"+iCol].value == 0)
	{
		document.all[sTableID+"_sort_"+iCol].value = '1';
		document.all[sTableID+"_sort_"+iCol].src = '/KOR_WEBROOT/IMAGE/COMMON/img_sort_down.gif';
	}
	else if( document.all[sTableID+"_sort_"+iCol].value == 1)
	{
		document.all[sTableID+"_sort_"+iCol].value = '2';
		document.all[sTableID+"_sort_"+iCol].src = '/KOR_WEBROOT/IMAGE/COMMON/img_sort_up.gif';
	}
	else if( document.all[sTableID+"_sort_"+iCol].value == 2)
	{
		document.all[sTableID+"_sort_"+iCol].value = '1';
		document.all[sTableID+"_sort_"+iCol].src = '/KOR_WEBROOT/IMAGE/COMMON/img_sort_down.gif';
	}
	
    var oTable = document.getElementById(sTableID);
    var oTBody = oTable.tBodies[0];
    var colDataRows = oTBody.rows;
    var aTRs = new Array;

    for (var i=0; i < colDataRows.length; i++) {
        aTRs[i] = colDataRows[i];
    }

    if (oTable.sortCol == iCol) {
        aTRs.reverse();
    } else {
        aTRs.sort(generateCompareTRs(iCol, sDataType));
    }

    var oFragment = document.createDocumentFragment();
    for (var i=0; i < aTRs.length; i++) {
        oFragment.appendChild(aTRs[i]);
    }

    oTBody.appendChild(oFragment);
    oTable.sortCol = iCol;
    
}

function generateCompareTRs(iCol, sDataType) 
{
    return  function compareTRs(oTR1, oTR2) 
    {
        try
        {
            var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
            var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);

            if (vValue1 < vValue2) {
                return -1;
            } else if (vValue1 > vValue2) {
                return 1;
            } else {
                return 0;
            }
        }
        catch(err)
        {
            return 1;
        }
    };
}

function convert(sValue, sDataType) 
{
    switch(sDataType) 
    {
        case "int":
            return parseInt(fn_StringToNumber(sValue));
        case "float":
            return parseFloat(fn_StringToNumber(sValue));
        case "date":
            return new Date(Date.parse(sValue));
        default:
            return sValue.toString();
    }
}

var mousedown = false; //마우스를 누른 상태
var td = ""; //사이즈 변경할 td
var tdwith = ""; //같이 변경할 td
var td_width; //변경할 td의 width,
var x = 0; //마우스 드레그전 가로위치

function TCstartColResize(obj, obj2)
{
       mousedown = true;
       td = obj;
       tdwith = obj2;
       
       if(td.width.indexOf('%') > 0)
       {
        td_width = td.offsetWidth;
        
       }
       else
       {
        td_width = td.width;
       }
       x = event.clientX;
}

function TCColResize()
{
       if (mousedown)
       {       
              var distX = event.x - x + 15 - document.getElementById(td.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[0].id).scrollLeft; //이동한 간격
              
              
              td.width = parseInt(td_width) + parseInt(distX);              
              tdwith.style.width = parseInt(td_width) + parseInt(distX);
              
              for(var i =0; i < tdwith.parentNode.parentNode.childNodes.length; i++)
              {
                tdwith.parentNode.parentNode.childNodes[i].childNodes[tdwith.cellIndex].width = '';
                tdwith.parentNode.parentNode.childNodes[i].childNodes[tdwith.cellIndex].style.width = parseInt(td_width) + parseInt(distX);
                tdwith.parentNode.parentNode.childNodes[i].childNodes[tdwith.cellIndex].align = tdwith.parentNode.parentNode.childNodes[i].childNodes[tdwith.cellIndex].align;
              }        
       }
}

function TCstopColResize()
{
       mousedown = false;
       td = '';
       tdwith = '';       
}

function cell_left(obj)
{
	//마우스가 셀의 왼쪽인지 측정
       if(event.offsetX < 5 && obj.cellIndex!=0)
       {
              return true;
       }
       else 
       {
              return false;
         }
}

function cell_right(obj)
{
	//마우스가 셀의 오른쪽인지 측정
       if(event.offsetX > obj.width-4)
       {
              return true;
       }
       else 
       {
              return false;
        }
}

//리사이즈시작
document.onmousedown = function()
{
	try
	{
		var now_mousedown = window.event.srcElement;
		var now_mousedown_with = now_mousedown.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[now_mousedown.cellIndex];;
		
		var now_mousedown_sub = now_mousedown;
	
		if(now_mousedown.value.toUpperCase()=="COLRESIZE")
		{
			if( cell_left(now_mousedown) )
			{	
				now_mousedown = now_mousedown.parentNode.childNodes[now_mousedown.cellIndex-1];
				now_mousedown_with = now_mousedown_sub.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[now_mousedown_sub.cellIndex-1];				
			}
			else if( !cell_right(now_mousedown) )
			{
				return true;//오른쪽도 왼쪽도 아니면 사이즈 조절 안함
			}
			
			TCstartColResize(now_mousedown, now_mousedown_with);
		}
	}
	catch(e)
	{
		return true; 
	}
}

//리사이즈
document.onmousemove = function()
{
	try
	{
		var now_mousemove = window.event.srcElement;
		
		if(now_mousemove.value.toUpperCase()=="COLRESIZE" || td!="")
		{

			//셀의 가장자리면 마우스 커서 변경
			if( cell_left(now_mousemove) || cell_right(now_mousemove) )
			{
				now_mousemove.style.cursor = "col-resize";
			}
			else
			{
				now_mousemove.style.cursor = "";
			}

			TCColResize(now_mousemove);
		}
		else
		{
			now_mousemove.style.cursor = "";
		}
	}
	catch(e)
	{
		return true; 
	}
}

//리사이즈종료
document.onmouseup = function()
{
	try
	{
		var now_mouseup = window.event.srcElement;		
	    TCstopColResize(now_mouseup);
	}
	catch(e)
	{
		return true; 
	}
}

//리사이즈 도중 텍스트 선택 금지
document.onselectstart = function()
{
	try
	{
		if(td != "")
		{
			return false;
		}
	}
	catch(e)
	{ 
		return true; 
	}
}
