
/**
 * ?????? ?????? maxLength ??? ?????????????????? text ????????? ????????? ?????? ??? ?????? ?????? ????????????.
 *  f : form object
 *   return boolean  (true : ?????? maxLength ?????? ??????????????? ???????????????,false : maxLength ?????? ????????? ??????
 */
function doMaxLengthCheck(f){
	var e = f.elements;			
	var flag = true;
	for(i = 0 ; i< e.length ; i++){						
		if(e[i].type == "text" && e[i].getAttribute("maxlength") < 2147483647){				
			if(e[i].getAttribute("maxlength") < getByteLength(e[i].value)){

				alert(e[i].alt+"????????? ??????????????? ?????? ?????? ????????? "+e[i].getAttribute("maxlength")+" Byte ??????, ???????????? ????????? "+getByteLength(e[i].value)+"Byte ?????????.\n????????? ????????? ???????????? ?????????");
				e[i].focus();
				flag = false;
			}
		}
	}
	return flag;
}

/**
 * 입력항목 검사 2004-09-17 sukjin chang
 * 파라미터)> ctype:구분
 * 사용예)>  CheckData(f.obj, "E")
 */
function CheckData(ctype)
{
    
	//alert("event.keyCode == [" + event.keyCode + "]");
	switch(ctype) {
        case "A" :
                  // 영문자, 숫자 0~9 문자만 입력가능
                  if( ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122)) )
                  {
                     // event.keyCode=0;
                  } else {
                      event.keyCode=0;
                  }
                  break;
		case "E" :
                  // 영문자만 입력가능
                  if( ((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122)) )
                  {
                     // event.keyCode=0;
                  } else {
                      event.keyCode=0;
                  }
                  break;
        case "N" :
                  // 숫자 0~9 문자만 입력가능
                  if( event.keyCode < 48 || event.keyCode > 57)
                  {
                      event.keyCode=0;
                  }
                  break;
        case "D" :
                  break;
        default :
    }

}

/**
 * 대문자로 반환
 * 파라미터)> obj:객체 명
 * 사용예)>  f_Upper(f.obj)
 */
function f_Upper(obj)
{	
	obj.value = obj.value.toUpperCase();
}


function doKeyUpEvent(str_value, type){
	if(type == "NUMBER"){
		return str_value.replace(/[^0-9]/gi, ""); 
	}else if(type == "FLOAT"){
		return str_value.replace(/[^0-9.]/gi, ""); 
	}else if(type == "NUMBER_MINUS"){
		return str_value.replace(/[^0-9-]/gi, ""); 
	}else if(type == "FLOAT_MINUS"){
		return str_value.replace(/[^0-9-.]/gi, ""); 
	}
}


function doKeyPressEvent(obj, type) {
    try {
		if( event.keyCode == 13 ) {
			return;
		}
		
		if( type == "NUMBER" ) {
		    if( (event.keyCode < 48) || (event.keyCode > 57) ) {
	    	    event.returnValue = false;
	    	}
		}
		else if( type == "NUMBER_MINUS" ) {
		    if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 45)) ) {
	    	    event.returnValue = false;
	    	}
			if( event.keyCode == 45 && obj.value.indexOf('-') > 0 ) {
				event.returnValue = false;
			}
		}
		else if( type == "FLOAT" ) {
	    	if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 46)) ) {
	        	event.returnValue = false;
	    	}
			if( event.keyCode == 46 && obj.value.indexOf('.') > 0 ) {
				event.returnValue = false;
			}
		}
		else if( type == "FLOAT_MINUS" ) {
	    	if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 46) && (event.keyCode != 45)) ) {
	        	event.returnValue = false;
	    	}
			if( event.keyCode == 46 && obj.value.indexOf('.') > 0 ) {
				event.returnValue = false;
			}
			if( event.keyCode == 45 && obj.value.indexOf('-') > 0 ) {
				event.returnValue = false;
			}
		}
		else if(type == "ID" ) {
		    if( (event.keyCode < 48) || ((event.keyCode > 57) && ( (event.keyCode < 65) || (event.keyCode > 128) )) ) {
	    	    event.returnValue = false;
	    	}
		}
		else if(type == "TEL" ) {
	    	if( ((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 45) ) {
	        	event.returnValue = false;
	    	}
		}
		else if(type == "ENGLISH"){
			if(event.keyCode < 65) {
	        	event.returnValue = false;
	    	}
	    	else if( ((event.keyCode > 90) && (event.keyCode < 97)) || (event.keyCode > 122)) {
	    		event.returnValue = false;
	    	}
	    }else if(type =="NO"){
	    	if(event.keyCode != 45){
	    		if( (event.keyCode < 48) || ((event.keyCode > 57) && ( (event.keyCode < 65) || (event.keyCode > 128) ) ) ) {
	    			event.returnValue = false;
	    		}
	    		
	    	}
	    }
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}

//통화에 따라 넘버형인지 플롯인지 선택하도록 한다. 2010.08.19 김영수.
function doKeyPressCurrencyEvent(obj, currency, type ) {
   	
    try {
		if( event.keyCode == 13 ) {
			return;
		}
		
		if( currency == "KRW" ) {
	    	
	    	if( type != undefined && type != null && type == "minus"){
				
		    	 if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 45)) ) {
		    	    event.returnValue = false;
		    	}
				if( event.keyCode == 45 && obj.value.indexOf('-') > -1 ) {
					event.returnValue = false;
				}
			}
			else{
				if( (event.keyCode < 48) || (event.keyCode > 57) ) {
		    	    event.returnValue = false;
		    	}
			}
		}
		else {
		
			if( type != undefined && type != null && type == "minus"){
								
		    	if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 46) && (event.keyCode != 45)) ) {
		        	event.returnValue = false;
		    	}
				if( event.keyCode == 46 && obj.value.indexOf('.') > -1 ) {
					event.returnValue = false;
				}

				if( event.keyCode == 45 && obj.value.indexOf('-') > -1 ) {
					
					event.returnValue = false;
				}
			}
			else{
			
				if( ((event.keyCode < 48) || (event.keyCode > 57)) && ((event.keyCode != 46)) ) {
	        		event.returnValue = false;
		    	}
				if( event.keyCode == 46 && obj.value.indexOf('.') > -1 ) {
					event.returnValue = false;
				}
			}
			
		}

	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}

function doNumberFloatFocusEvent(obj) {

	removeMinusMask(obj);
}


function doNumberFloatBlurEvent(obj) {

	try {

      	var oNumber = obj.value.replace(/,/g,"");
		var sNumber = oNumber;
		var lNumber = "";
        var tNumber = "";
        var dotIndex ="";

		var bSign = "";
		var bDot ="";

		if( oNumber.indexOf('-') != -1) {
			bSign = 1 ;
		}
		
		if( (dotIndex = oNumber.indexOf('.')) != -1) {
			bDot = 1 ;
		}

		if( bSign ) {

			if( bDot ) {

				sNumber = obj.value.substring(1, dotIndex);
				lNumber = obj.value.substring(dotIndex);
			}
			else {

				sNumber = obj.value.substring(1);
			}
		}
		else {
			if( bDot ) {

				sNumber = obj.value.substring(0, dotIndex);
				lNumber = obj.value.substring(dotIndex);
			}
		} 
			
        var i;
       // var j = 0; 
        var tLen = sNumber.length;

      	if( sNumber.length <= 3 ) {
			return ;
      	}

		if( bSign ) {

			tNumber +="-";
		}

      	for( i = 0 ; i < tLen ; i++ ) {
         	if( i != 0 && ( i % 3 == tLen % 3) )
         		tNumber += ",";
         	if( i < sNumber.length ) 
         		tNumber += sNumber.charAt(i);
    	}
    	
      	obj.value = tNumber + lNumber;
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}

function doCheckMinMaxValue(obj) {

	try {

		var value = obj.value.replace(/,/g,"");
	
		if( obj.minvalue != null ) {
	
			if( parseFloat(value) < parseFloat(obj.minvalue) ) {
	
				obj.value = obj.minvalue;
			}
		}
	
		if( obj.maxvalue != null ) {
	
			if( parseFloat(value) > parseFloat(obj.maxvalue) ) {
	
				obj.value = obj.maxvalue;
			}
		}
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}

function doDateFocusEvent(obj) {

	removeMask(obj);
}

function doDateBlurEvent(obj) {

	try {
		//IBSheet??? ?????? Object.OnBlur Event??? 2??? Call?????? ????????? ????????????
		//Masking????????? ?????? ?????? ??????????????? ?????? ????????? ???????????? Masking ????????? ????????? ??????.
		//by bestop
		removeMask(obj);
		
		var yyyy   = "";
		var mm     = "";
		var dd     = "";
		var c_date = "";
		var i_date = obj.value;
		var i_date_length = i_date.length;

		if( i_date_length > 8 ) {
			i_date = i_date.substring(0, 8);
			i_date_length = i_date.length;		
		}

		if( i_date_length <= 4 ) {
			return;
		}
		else if( i_date_length <= 6 ) {
			yyyy = i_date.substring(0, 4);
			mm   = i_date.substring(4, i_date_length);
		}
		else if( i_date_length <= 8 ) {
			yyyy = i_date.substring(0, 4);
			mm   = i_date.substring(4, 6);
			dd   = i_date.substring(6, i_date_length);
		}

		c_date = yyyy;

		if( mm != null && mm != "" ) {
			if( parseInt(mm, 10) < 1 ) {
				mm = "01";			
			}
			else if( parseInt(mm, 10) > 12 ) {
				mm = "12";			
			}
			c_date += "-" + mm;
		}

		if( dd != null && dd != "" ) {
			var last_day = getLastDay(parseInt(yyyy,10), parseInt(mm,10));

			if( parseInt(dd,10) < 1 ) {
				dd = "01";
			}
			else if( parseInt(dd,10) > last_day ) {
				dd = last_day;
			}
			c_date += "-" + dd;
		}

		obj.value = c_date;
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}

// ?????? ?????? ????????? ????????? ?????? ??????.
function getLastDay(year, month) {
	try {
	
    	var Last_DAY = new Array(     31,            // 1???
                                      28,            // 2???
                                      31,            // 3???
                                      30,            // 4???
                                      31,            // 5???
                                      30,            // 6???
                                      31,            // 7???
                                      31,            // 8???
                                      30,            // 9???
                                      31,            // 10???
                                      30,            // 11???
                                      31        );   // 12???

     // ??????
     var a_leap_year;

     if( year % 4 == 0 && year % 100 != 0 )
          a_leap_year = true;
     else
          a_leap_year = false;

     if( year % 1000 == 0 )
          a_leap_year = true;

     // 2?????? ?????? ??????????????? ?????? ??????.
     Last_DAY[1] = (a_leap_year) ? 29 : 28 ;

     return Last_DAY[month - 1];
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}


function doTimeFocusEvent(obj) {

	removeMask(obj);
}

function doTimeBlurEvent(obj) {

	try {

		var hh     = "";
		var mm     = "";
		var ss     = "";
		var c_time = "";
		var i_time = obj.value;
		var i_time_length = i_time.length;

		if( i_time_length > 6 ) {
			i_time = i_time.substring(0, 6);
			i_time_length = i_time.length;		
		}

		if( i_time_length <= 2 ) {
			return;
		}
		else if( i_time_length <= 4 ) {
			hh   = i_time.substring(0, 2);
			mm   = i_time.substring(2, i_time_length);
		}
		else if( i_time_length <= 6 ) {
			hh   = i_time.substring(0, 2);
			mm   = i_time.substring(2, 4);
			ss   = i_time.substring(4, i_time_length);
		}

		if( hh != null && hh != "" ) {
			if( parseInt(hh,10) < 0 ) {
				hh = "00";			
			}
			else if( parseInt(hh,10) > 23 ) {
				hh = "23";			
			}
			c_time += hh;
		}

		if( mm != null && mm != "" ) {
			if( parseInt(mm,10) < 0 ) {
				mm = "00";			
			}
			else if( parseInt(mm,10) > 59 ) {
				mm = "59";			
			}
			c_time += ":" + mm;
		}

		if( ss != null && ss != "" ) {
			if( parseInt(mm,10) > 59 ) {
				ss = "59";			
			}

			if( ss.length == 1 ) {
				ss = "0" + ss;
			}

			c_time += ":" + ss;
		}

		obj.value = c_time;
	}catch(errorObject){
        showErrorDlg(errorObject);
    }
}

function autoTab(obj, len) {

	try {

		var keyCode = event.keyCode; 
		var filter  = [0,8,9,16,17,18,37,38,39,40,46];
	
		if( obj.value.length >= len && !containsElement(filter, keyCode) ) {
	
			obj.value = obj.value.slice(0, len);
			obj.form[(getIndex(obj)+1) % obj.form.length].focus();
		}
	
		return true;
	}
    catch(errorObject) {
    }
}

function enterKeyPress(obj) {

	try {

		var keyCode = event.keyCode;
	
		if( keyCode == 13 ) {
			obj.form[(getIndex(obj)+1) % obj.form.length].focus();
		}
	}
    catch(errorObject) {
    }
}

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;

	while (i < obj.form.length && index == -1)
		if (obj.form[i] == obj)
			index = i;
		else 
			i++;
	
	return index;
}



function removeMask(obj) {

/**
 * ?????? obj.select() ?????? ?????? ???????????? ????????? ?????? ??????...
 */
	try {

		if( obj.length == null ) {
		  	obj.value = obj.value.replace(/(\,|\-|\:)/g,""); 
		}
		else {
			for( var i = 0 ; i < obj.length ; i++ ) {
			
			  	obj[i].value = obj[i].value.replace(/(\,|\-|\:)/g,""); 
			}		
		}
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}



function removeMinusMask(obj) {

/**
 * ?????? obj.select() ?????? ?????? ???????????? ????????? ?????? ??????...
 */
	try {

		if( obj.length == null ) {
		  	obj.value = obj.value.replace(/(\,|\:)/g,""); 
		}
		else {
			for( var i = 0 ; i < obj.length ; i++ ) {
			
			  	obj[i].value = obj[i].value.replace(/(\,|\:)/g,""); 
			}		
		}
		
	}
    catch(errorObject) {
        showErrorDlg(errorObject);
    }
}


function removeMaskForString(value) {

	return value.replace(/(\,|\-|\:)/g,"");
}


function removeMaskForNumber(value) {

	return value.replace(/(\,|\:)/g,"");
}


function isValidDate(data)
{
	var str;
	var yyyy;
	var y_year;
	var m_month;
	var mm_month;
	var d_day;
	str = data;
	yyyy = parseInt(str.substring(0,4), 10);
    var end = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if ((yyyy % 4 == 0 && yyyy % 100 != 0) || yyyy % 400 == 0) {
        end[1] = 29;
    }
	y_year = parseInt(str.substring(0,4), 10);
	m_month = parseInt(str.substring(4,6), 10);
	mm_month = m_month - 1;
	d_day = parseInt(str.substring(6), 10);
	if( str.length == 8 )
	{
		if ( 1900 < y_year  &&  y_year<= 2100)
		{
			if( 0 < m_month  &&  m_month <= 12)
			{
				if( 0 < d_day  && d_day <= end[mm_month] )
					return true;
				else
					return false;
			}
			else
				return false;
		}
		else
			return false;
	}
	else
		return false;
}

/**
 * ??????????????? ????????? ??????
 *  obj : form object
 *	checkLen : ????????? ?????????
 *   return boolean  (true :  ??????????????? ???????????????,		false : checkLen ?????????.
 */

function doPointLengthCheck(obj,checkLen){
	//var f = document.form;
    var dotIndex ="";
	var lNumber = "";
	var bDot ="";
  	var oNumber = obj.value.replace(/,/g,"");
  	var rtnValue = true;

	if( (dotIndex = oNumber.indexOf('.')) != -1) {
		bDot = 1 ;
		rtnValue = false;
	}

	
	if( bDot ){
		lNumber = oNumber.substring(dotIndex+1);
		
		if(lNumber.length > checkLen){
			rtnValue = false;
		}else{
			rtnValue = true;
		}
	}
	
	return rtnValue;

}

/**
 * ??????????????? ????????? ?????????
 *  obj : form object
 *	checkLen : ????????? ?????????
 *   return boolean  (true :  ??????????????? ???????????????,		false : checkLen ?????????.
 */
function cutPointLength(obj,checkLen){
	//var f = document.form;
	//var lNumber = "";
	//var bDot ="";
  	var oNumber = obj.value.replace(/,/g,"");
    var dotIndex = oNumber.indexOf('.');
  	var rtnValue = "";
  	
  	if( dotIndex == -1 ){ 
  		rtnValue = oNumber;
  	}else{
		rtnValue = oNumber.substring(0,dotIndex);
		
		if( checkLen>0 ){
			rtnValue = rtnValue + "." + oNumber.substring(parseInt(dotIndex+1),parseInt(dotIndex+1)+parseInt(checkLen)); // + oNumber.substring(parseInt(dotIndex+1),  parseInt(dotIndex+checkLen-1));
		}
	}


	return rtnValue;

}

/**
 * 占쌥억옙 占쏙옙' 占쌘몌옙占쏙옙 체크
 *  obj : form object
 *	checkLen : 占쌍억옙占쏙옙 占쌘몌옙占쏙옙
 *   return boolean  (true :  d占쏙옙占쏙옙8占쏙옙 占쌉력되억옙=,		false : checkLen 占싼는댐옙.
 */
function doMoneyLengthCheck(obj,checkLen){
	//var f = document.form;
    var dotIndex ="";
	var lNumber = "";
	//var bDot ="";
  	var oNumber = obj.value.replace(/,/g,"");
  	var rtnValue = true;

	if( (dotIndex = oNumber.indexOf('.')) == -1) {
		lNumber = oNumber;
	}else{
		lNumber = oNumber.substring(0, dotIndex);
	}
	
	if( lNumber < 0 ){
		lNumber = lNumber.substring(1);
	}

	if(lNumber.length > checkLen){
		rtnValue = false;
	}else{
		rtnValue = true;
	}
	
	return rtnValue;

}

/********************************************************************************/
// 금액필드 자릿수 체크
/********************************************************************************/
function check_Amount_Digit(obj, point_digit, error_Msg) {
	var digit = parseInt(point_digit);
	
	if( ! doMoneyLengthCheck(obj, OCCUR_DIGIT) ){
		ErrMsg(obj, error_Msg + " is allowed to hold up to " + OCCUR_DIGIT + " figures.[10 billions]");
		return false;
	}
	
	if( ! doPointLengthCheck(obj, digit) ){
		ErrMsg(obj, error_Msg + " is allowed to hold up to " + digit + " decimal places.");
		return false;
	}
	
	return true;
}

function check_Amount_Digit2(obj, point_digit) {
	var digit = parseInt(point_digit);
	
	if( ! doMoneyLengthCheck(obj, OCCUR_DIGIT) ){
		ErrMsg(obj, "Allowed to hold up to " + OCCUR_DIGIT + " figures.[10 billions]");
		return false;
	}
	
	if( ! doPointLengthCheck(obj, digit) ){
		ErrMsg(obj, "Allowed to hold up to " + digit + " decimal places.");
		return false;
	}
	
	return true;
}
/********************************************************************************/


/**
 * 이인오 textbox 온포커스 아웃포커스
 */
function inputTextOnFocus(obj){
//	if(!obj.readOnly) {
		obj.style.border = "1px solid";
		obj.style.borderColor = "#ff0000";
//	}
}
function inputTextOutFocus(obj){
//	if(!obj.readOnly) {
		obj.style.border = "1px solid";
		obj.style.borderColor = "#d0d7e4";
//	}
}


