﻿/*============================================================================
 * File Name : form.js
 * Desc      : form javascript
 * Auther    : 박종명
 * Date      : 2007/01/02 최초작성
 * Copyright (c) 2006 witcom.co.kr. All Rights Reserved.
 *
 * 수정내역
 *
 ============================================================================*/

/**
* 브라우져의 타입을 설정
*/
var ns = (document.layers)? true:false;
var ie = (document.all)? true:false;
var doc = (ie)? document.all : document;

/**
 * 폼필드를 체크한다. ( sub function ) 
 */
function checkfield(field, msg){
	if(field.value==null || field.value==''){
		alert(msg);
		field.focus(); // focus  
		return false;
	}
	return true;
}

/**
 * Check all ( multi ) combo ( name 이 같은 여러개의 combo 를 동시에 체크한다 )
 */
function checkAllCombo(field,msg){
	if(field){
		if(field[0].length){ // multi combobox 인 경우 
			for(i=0; i<field.length; i++){
				if(field[i].value == ''){
					alert(msg);
					field[i].focus();
					return false;
				}
			}
		}
		else{
			if(field.value == ''){ // 단일 combo box 인 경우 
				alert(msg);
				field.focus();
				return false;
			}
		}
	}
	return true;
}

/**
 * 주어진 길이가 채워지면 다음 form field 로 자동 이동 
 * - 신용카드번호 입력, 전화번호 입력 등 
 */
function autoShift(fromFld, nextFld, satisfyFldLen){
	if(fromFld.value.length==satisfyFldLen){
		nextFld.focus();
		return;
	}
}

/**
 * 동일한 이름을 갖는 checkbox 에서 check 된 count 를 return 
 */
function getCheckedCount(checkBoxObject){
    var checkedCount = 0;

    if(checkBoxObject && checkBoxObject.length){
        for(i=0; checkBoxObject.length > i; i++){
            if (checkBoxObject[i].checked == true){
                checkedCount++;
            }
        }
    }
    else if(checkBoxObject){
        if(checkBoxObject.checked==true) checkedCount++;
    }
    return checkedCount;
}

/**
 * check box selection 을 반전시킴 - 동일한 이름을 같는 checkbox 전체  
 */
function checkreverse(checkObject) {
	if(checkObject==undefined){
		return;
	}

    if(checkObject.length){ // 2개이상 
        for(i=0; i<checkObject.length; i++){
            checkObject[i].checked = !checkObject[i].checked;
        }
    }
    else if(checkObject){
        checkObject.checked = !checkObject.checked;
    }
}

/**
 * check box selection 전체 선택
 */
function checkBoxAll(checkObject1,checkObject2) {
	if(!checkObject1|| !checkObject2) return;
	if(!isObject(checkObject2))
	{
		return;
	}
    if(checkObject2.length){ // 2개이상 
        for(i=0; i<checkObject2.length; i++){
			if(checkObject1.checked)
			{	

				checkObject2[i].checked=true;
			}
			else
			{
				checkObject2[i].checked=false;
			}
        }
    }
	else
	{	
		if(checkObject1.checked)
		{
			checkObject2.checked=true;
		}
		else
		{
			checkObject2.checked=false;
		}
	}
}

/**
* 선택된 라디오버튼이 있는지 체크
*/
function hasCheckedRadio(input) {
   if (input.length > 1) {
       for (var inx = 0; inx < input.length; inx++) {
           if (input[inx].checked) return true;
       }
   } else {
       if (input.checked) return true;
   }
   return false;
}

/**
* 선택된 체크박스가 있는지 체크
*/
function hasCheckedBox(input) {
   return hasCheckedRadio(input);
}


/**
* 달력 선택 팝업창을 출력하기 위한 스크립트 (날짜만:datetime='date')
*/
function callCalendar(datetime,field,monthTerm){
	dateField = field;
	__showCalendarPanel(datetime,'::','','','',monthTerm);
} 

/**
* 실제 달력 팝업창을 출력한다.
*/
function __showCalendarPanel(type, param, title, x, y,monthTerm)
{
	if(param == null){
		alert("Javascript:__showCalendarPanel: You must specify the parameter");
		return;
	}
	var separator = ":";
	var pa = param.split(separator);

	if(pa.length != 3)	//<= 1 || (pa[0].length <= 0 && pa[1].length <= 0)) 
	{
		alert("Javascript:__showCalendarPanel: You must specify INPUT objects name to be set to local datetime or GMT one");
		return;
	}

	var left = 100;
	var top = 200;
	if(x != null) left = x;
	if(y != null) top = y;

	var url = "/common/calendar.do?type=" + type + "&objLocalDate=" + pa[0] + "&objGMTDate=" + pa[1] + "&initDateTime=" + pa[2] + ( title != null ? "&title=" + title : "")+"&monthTerm="+monthTerm;
	var width  = (ie) ? "230":"240";
	if( type == "date" )
		var height = (ie) ? "288":"293";
	else
		var height = (ie) ? "318":"323";
	var ws  = "left=" + left + ",top=" + top + ",menubar = no,width=" + width + ",height=" + height + ",resizable=no,scrollbars=no";
	window.open(url, "__newWin", ws);
}

/**
* 실제 달력 팝업창에서 선택한 내용을 입력하고자 하는 폼에 전달한다.
*/
function setDateTime(local, gmt, type,monthTerm,mycalendar){

	var lyear;
	var lmont;
	var ldate;

	var adddate;

	if(type=='date'){
		dateField.value = local.substring(0, 4)+local.substring(4, 6)+local.substring(6, 8);
	}
	else{
		dateField.value = local.substring(0, 4)+local.substring(4, 6)
			+ local.substring(6, 8)
			+ local.substring(8, 10)
			+ local.substring(10, 12);
	}
	
	if(monthTerm=='startTerm'){
		if(!isEmpty(document.actFrm.eDate.value)){
			mycalendar.changeMonth(1);
			lyear = mycalendar.year;
			lmont = mycalendar.month+1;
			ldate = mycalendar.day;

			if(parseInt(lmont,10)<10){
				lmont = "0"+lmont;
			}
			if(parseInt(ldate,10)<10){
				ldate = "0"+ldate;
			}

			adddate = lyear+lmont+ldate;
			if(adddate < document.actFrm.eDate.value){
				document.actFrm.eDate.value = adddate;
			}
		}
	}
	else if(monthTerm=='endTerm'){
		if(!isEmpty(document.actFrm.sDate.value)){
			mycalendar.changeMonth(-1);
			lyear = mycalendar.year;
			lmont = mycalendar.month+1;
			ldate = mycalendar.day;

			if(parseInt(lmont,10)<10){
				lmont = "0"+lmont;
			}
			if(parseInt(ldate,10)<10){
				ldate = "0"+ldate;
			}
			adddate = lyear+lmont+ldate;
			if(adddate > document.actFrm.sDate.value){
				document.actFrm.sDate.value = adddate;
			}
		}
	}
}

/**
* 우편번호 찾기 팝업
*/
function zipCode(frm,zip,addr,zipid){
	open_window_scroll2("/common/zipCodeSearch.do?frm="+frm+"&zip="+zip+"&addr="+addr+"&zipid="+zipid,"zip",405,500);
}
