/* ================================================================================
Function :  Text Box에서 Calendar 팝업 display
Param1 :     Date		// document.form1.txtDate
Param2 :     format		// YYYYMMDD, YYYY/MM/DD
Return   :  없음
===========================================+++++++============================== */
function popup_Calendar_Ctrl(Date, format, ParentEvent) {
	var result ="";
	var oArgObj = new Object();
	oArgObj.format = format;
	result = window.showModalDialog("/comn/calendar.html",oArgObj,"dialogWidth:200px; dialogHeight:257px; resizable=no; help:no; status:no; scroll=no;center=true; ");

	// 값을 선택하지 않았을 경우 exit
	if(result == null || result == "" ) {
		return ;
	}

	Date.value = result;
	
	if(ParentEvent != null && ParentEvent != '') eval(ParentEvent);
	
	Date.focus();
}


/**
 * isValid(Object) 날짜가 유효한 날짜인지 체크하는 함수 
 * 예) 20020328
 * @param       theDate        날짜 (예 20020328)
 * @return      true, false
 * @histroy     2007.06.26  
 */ 
function isValid(theDate)
{
	
	try{

			yy = parseInt(theDate.substring(0,4),10); 
			mm = parseInt(theDate.substring(4,6),10); 
			dd = parseInt(theDate.substring(6,8),10); 
			if (mm == 1) 
			        max_days = 31 
			else if (mm == 2) { 
			if ((( yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) 
			                max_days = 29; 
			        else 
			                max_days = 28; 
			} 
			else if (mm == 3) 
			        max_days = 31;
			else if (mm == 4) 
			        max_days = 30;
			else if (mm == 5) 
			        max_days = 31;
			else if (mm == 6) 
			        max_days = 30;
			else if (mm == 7) 
			        max_days = 31;
			else if (mm == 8) 
			        max_days = 31;
			else if (mm == 9) 
			        max_days = 30;
			else if (mm == 10) 
			        max_days = 31;
			else if (mm == 11) 
			        max_days = 30;
			else if (mm == 12) 
			        max_days = 31;
			else { 
			        alert("월확인"); 
			        return false; 
			} if (dd < 1 || dd > max_days) { 
			        alert(mm + "월에는" + max_days + "일까지만 있습니다."); 
			        return false; 
			} else 
			        return true;
	
	} catch(e) {	
		yy = parseInt(theDate.substring(0,4),10); 
		mm = parseInt(theDate.substring(4,6),10); 
		dd = parseInt(theDate.substring(6,8),10); 
		if (mm == 1) 
		        max_days = 31 
		else if (mm == 2) { 
		if ((( yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) 
		                max_days = 29; 
		        else 
		                max_days = 28; 
		} 
		else if (mm == 3) 
		        max_days = 31;
		else if (mm == 4) 
		        max_days = 30;
		else if (mm == 5) 
		        max_days = 31;
		else if (mm == 6) 
		        max_days = 30;
		else if (mm == 7) 
		        max_days = 31;
		else if (mm == 8) 
		        max_days = 31;
		else if (mm == 9) 
		        max_days = 30;
		else if (mm == 10) 
		        max_days = 31;
		else if (mm == 11) 
		        max_days = 30;
		else if (mm == 12) 
		        max_days = 31;
		else { 
		        alert("월확인"); 
		        return false; 
		} if (dd < 1 || dd > max_days) { 
		        alert(mm + "월에는" + max_days + "일까지만 있습니다."); 
		        return false; 
		} else 
		        return true;
      	}	
	
	

}



/**
 * isFieldBlank(Object) 필드가 블랭크인지를 체크하는 함수 
 * @param       theField               필드객체
 * @return      true, false
 * @histroy     2007.06.26  
 */     
function isFieldBlank(theField) 
{ 

	try{
        		var str = theField.value;
        		return (str == "" || str.charAt(0) == " ") ? true : false;
	
	} catch(e) {	
        	var str = theField.value;
        	return (str == "" || str.charAt(0) == " ") ? true : false;
      	}	
	
} 



/* 현재 일자를 가져오는 내부 function */
function curr_date() {
	

	d = new Date();
	
	month = d.getMonth();
	month = (month * 1) + 1;
	day = d.getDate();
	year = d.getYear();

//	if (month < 10) month = "0"+month;
//	if (day < 10) day = "0"+day;

//	js_currdate = year+month+day;

	if (parseInt(month) < 10) month = "0"+month;
	if (parseInt(day) < 10) day = "0"+day;

	js_currdate = String(year)+String(month)+String(day);

	return js_currdate;
}

/**
 * isValidDate(Object) 날짜 체크루틴  
 *   예) YYYYMMDD or YYYY/MM/DD
 *       <input type="text" name="Tb_CntrYyyymm" size="8" maxlength=10
 *              onBlur="javascript:isValidDate(form.Tb_CntrYyyymm);">
 * @param       theDate        
 * @return      true, false
 * @see         RemoveSlash(theDate)
 * @see         isFieldBlank(theDate)  
 * @histroy     2007.06.26  
 */
function isValidDate(theDate)
{
        
	try{
		
			if (!isFieldBlank(theDate)) {
				

				try{			        							
        			var currdate = curr_date();
        			

        			if (theDate.value.length <= 2) {
        				if (theDate.value.length == 1 ) {
        					theDate.value = currdate.substring(0,6)+"0"+theDate.value;
        				} else {
        					theDate.value = currdate.substring(0,6)+theDate.value;
        				}
        				
        			} else if (theDate.value.length <= 4) {
        				
        				if(theDate.value.length == 3) {
        					theDate.value = currdate.substring(0,4)+"0"+theDate.value;        		
        				} else {
        					theDate.value = currdate.substring(0,4)+theDate.value;        		        		
        				}
        				
        			} else if (theDate.value.length <= 6) {
        				if(theDate.value.length == 5) {
        					theDate.value = currdate.substring(0,2)+"0"+theDate.value;        		
        				} else {
        					theDate.value = currdate.substring(0,2)+theDate.value;        		        		
        				}        	
        			}        	

        			} catch(e) {
      			
        		}
    			
        			
        			var checkStr = RemoveSlash(theDate.value);        	
        			
        		        var numStr = "/0123456789";
        		
        		        for ( var j = 0 ; j < theDate.value.length ; j++ )
        		        {
        		                if ( numStr.indexOf( theDate.value.charAt(j) ) == -1 ) {
        		                        alert("YYYY/MM/DD또는 YYYYMMDD형태로입력하세요!");
        		                        theDate.value = "";
        		                        theDate.focus();
//        		        		theDate.select();        		                        
        		                        return false;
        		                }
        		        }
        		
        		        if (checkStr.length != 8) { 
        		                alert("YYYY/MM/DD또는 YYYYMMDD형태로입력하세요!"); 
        		                theDate.value = "";                     
//        		                theDate.focus();
     		        		theDate.select();        		                                		                
        		                return false; 
        		        } else if (!isValid(checkStr)) {
        		                theDate.value = "";                     
        		                theDate.focus();
//    		        		theDate.select();        		                                		                
        		                return false;
        		        } else{
        		                theDate.value = checkStr.substring(0,4) + "/" + checkStr.substring(4,6) + "/" + checkStr.substring(6,8)
        		                return true;
        		        }
        		}
        		return true;
	
	} catch(e) {	

        	if (!isFieldBlank(theDate)) {
        		try{
        		var currdate = curr_date();
        		
        		if (theDate.value.length <= 2) {
        			if (theDate.value.length == 1 ) {
        				theDate.value = currdate.substring(0,6)+"0"+theDate.value;
        			} else {
        				theDate.value = currdate.substring(0,6)+theDate.value;
        			}
        			
        		} else if (theDate.value.length <= 4) {
        			
        			if(theDate.value.length == 3) {
        				theDate.value = currdate.substring(0,4)+"0"+theDate.value;        		
        			} else {
        				theDate.value = currdate.substring(0,4)+theDate.value;        		        		
        			}
        			
        		} else if (theDate.value.length <= 6) {
        			if(theDate.value.length == 5) {
        				theDate.value = currdate.substring(0,2)+"0"+theDate.value;        		
        			} else {
        				theDate.value = currdate.substring(0,2)+theDate.value;        		        		
        			}        	
        		}        	
        		}catch(e) {}
        		
        		var checkStr = RemoveSlash(theDate.value);        	
        		
        	        var numStr = "/0123456789";
        	
        	        for ( var j = 0 ; j < theDate.value.length ; j++ )
        	        {
        	                if ( numStr.indexOf( theDate.value.charAt(j) ) == -1 ) {
        	                        alert("YYYY/MM/DD또는 YYYYMMDD형태로입력하세요!");
        	                        theDate.value = "";
        	                        theDate.focus();
//    		        		theDate.select();        		                                	                        
        	                        return false;
        	                }
        	        }
        	
        	        if (checkStr.length != 8) { 
        	                alert("YYYY/MM/DD또는 YYYYMMDD형태로입력하세요!"); 
        	                theDate.value = "";                     
        	                theDate.focus();
//	        		theDate.select();        		                                	                
        	                return false; 
        	        } else if (!isValid(checkStr)) {
        	                theDate.value = "";                     
        	                theDate.focus();
//	        		theDate.select();        		                                	                
        	                return false;
        	        } else{
        	                theDate.value = checkStr.substring(0,4) + "/" + checkStr.substring(4,6) + "/" + checkStr.substring(6,8)
        	                return true;
        	        }
        	}
        	return true;
      	}        
        

}


/**
 * RemoveSlash(Object)문자열의 Slash 를 제거하는 함수 
 * @param       theDate         입력문자열
 * @return      SlashString     Slash가 제거된 문자열
 * @histroy     2007.06.26  
 */
function RemoveSlash(theDate)
{
        var SlashString="";

        for( j=0; j<=theDate.length-1; j++)
                if( theDate.charAt(j) != "/" )
                        SlashString = SlashString + theDate.charAt(j);

        return SlashString;
}


/**
 * RemoveSlashEdit(Object) 날짜 input 박스 선택시 RemoveSlash
 *   예) YYYY/MM/DD -> YYYYMMDD
 * @param       obj            
 * @return      Object.value
 * @see         RemoveSlash(Object.value)
 * @exam        onFocus="RemoveSlashEdit(this);
 * @histroy     2002.5.10  
 */
function RemoveSlashEdit( obj){
        if (!isFieldBlank(obj)) obj.value = RemoveSlash(obj.value);
//        obj.focus();        
        obj.select();
}



/**
 * isFieldBlank(Object) 필드가 블랭크인지를 체크하는 함수 
 * @param       theField               필드객체
 * @return      true, false
 * @histroy     2007.06.26  
 */     
function isFieldBlank(theField) 
{ 

	try{
        		var str = theField.value;
        		return (str == "" || str.charAt(0) == " ") ? true : false;
	
	} catch(e) {	
        	var str = theField.value;
        	return (str == "" || str.charAt(0) == " ") ? true : false;
      	}	
	
} 


/**
 * isFieldBlank(Object) 필드가 블랭크인지를 체크하는 함수 
 * @param       theField               필드객체
 * @return      true, false
 * @histroy     2007.06.26  
 */     
function isFieldBlank(theField) 
{ 

	try{
        		var str = theField.value;
        		return (str == "" || str.charAt(0) == " ") ? true : false;
	
	} catch(e) {	
        	var str = theField.value;
        	return (str == "" || str.charAt(0) == " ") ? true : false;
      	}	
	
} 