// <![CDATA[
/**
 * ÆÐÅ°Áö °Ë»ö JavaScript
 */
var SearchPackage = Class.create({
    /**
     * SearchPackage Initialize
     *
     * @param   string      pkgItem
     * @param   object      date object (¿¹¾à °¡´ÉÀÏ)
     * @param   object      date object (¿¹¾à °¡´ÉÀÏ)
     */
    initialize: function(pkgItem, rsvSdate, rsvEdate)
    {
        this.rsvSdate   = rsvSdate;
        this.rsvEdate   = rsvEdate;

        this.pkgItemName = [
                {item: 'A', name: 'Ç×°ø'}
                , {item:'H', name: '¼÷¼Ò'}
                , {item:'R', name: '·»ÅÍÄ«'}
        ];

        this.setPkgItem(pkgItem);

    },  // end of the 'initialize()' funciton


    /** 
     * ÆÐÅ°Áö Æ÷ÇÔ »óÇ° ¼³Á¤
     *
     * usage:
     *  <input type="hidden" id="pkgItem">
     *  ...
     *  <TAG id="container_id">
     *          ~~~~~~~~~~~~~~
     *      <input type="checkbox" id="pkgItem_A" value="A" ...>
     *      <input type="checkbox" id="pkgItem_H" value="H" ...>
     *      <input type="checkbox" id="pkgItem_R" value="R" ...>
     *  </TAG>
     *
     * @param   string      DOM element
     *
     * @param   string      pkgItem
     */
    getPkgItem: function(container_id)
    {
        var inputs  = $(container_id).getElementsByTagName('input');
        var pkgItem = '';

        $A(inputs).each(function(c, index) {
            if (c && c.type == 'checkbox' && c.checked) {
                pkgItem += c.value;
            } // end if
        });

        this.setPkgItem(pkgItem);
        $('pkgItem').value = this.pkgItem;
    }, // end of the 'getPkgItem()' funtion


    /**
     * ÆÐÅ°Áö Æ÷ÇÔ »óÇ° ¼³Á¤
     *
     * @param   string      pkgItem
     */
    setPkgItem: function(pkgItem)
    {
        this.pkgItem = '';

        this.pkgItemName.each(function(element) {
            if (pkgItem.indexOf(element.item) == -1) {
                eval('this.INCLUDE_'+element.item+' = false;');
            }
            else {
                eval('this.INCLUDE_'+element.item+' = true;');
                this.pkgItem += element.item;
            } // end if
        }.bind(this));

    }, // end of the 'setPkgItem()' function


    /**
     * ¸¶Áö¸· ÀÏÀÚ º¯°æ
     *
     * @param   string          ±¸ºÐ (start / end)
     */
    changeLastDay: function(gubun)
    {
        var tmp     = null;
        var lastDay = 0;

        switch (String(gubun).toUpperCase()) {
            case 'END':
                gubun = 'e';
                break;

            //case 'START':
            default:
                gubun = 's';
                break;
        } // end switch
       
        if ($('stay').type == 'text') {
            $('stay').value = '';
        } // end if

        // lib.date.js: getLastDay()
        lastDay = getLastDay(new Date($F(gubun+'yy'), $F(gubun+'mm')-1, 1));

        if ($(gubun+'dd').length > lastDay) {
            $(gubun+'dd').selectedIndex = 0;

            // Á¦°Å
            for (var i=$(gubun+'dd').length-1; i>=lastDay; i--) {
                $(gubun+'dd').options[i] = null;
            }
        }
        else if ($(gubun+'dd').length < lastDay) {
            // Ãß°¡
            for (var i=$(gubun+'dd').length; i<lastDay; i++) {
                $(gubun+'dd').options[i] = new Option(i+1, i+1);
            }
        } // end if
        
    }, // end of the 'changeLastDay()' funciton


    /**
     * Á¶È¸
     *
     * @param   string      form element id
     * @param   string      action url
     * @param   string      DOM element id (default: null)
     *
     * @return  boolean
     */
    search: function(frmId, actionUrl, container_id)
    {
        var sdate   = new Date($F('syy'),$F('smm')-1,$F('sdd'));

        /**
         * ÆÐÅ°Áö »óÇ°°ú ´ÜÀÏ »óÇ°À» ¸ðµÎ °Ë»öÇÒ ¶§´Â
         * ÀÌ ¸Þ¼Òµå¸¦ overridingÇÑ ¸Þ¼Òµå¿¡¼­
         * super.search() ¿¡ container_id¸¦ ³Ñ±âÁö ¾Ê°í
         * this.getPkgItem(container_id)¸¦ º°µµ·Î
         * super.search() È£ÃâÇÏ±â Àü¿¡ È£ÃâÇÑ´Ù.
         */
        if (typeof container_id != 'undefined' && $(container_id) != null) {
            this.getPkgItem(container_id);

            // ¼±ÅÃ »óÇ° È®ÀÎ (¼÷¼Ò ÇÊ¼ö)
            if (!this.INCLUDE_H) {
                alert('¼÷¼Ò´Â ÇÊ¼ö ¼±ÅÃÀÔ´Ï´Ù.');
                $('pkgItem_H').checked = true;

                return false;
            } // end if

            // 2°³ ÀÌ»ó ¼±ÅÃ
            if (this.pkgItem.length <= 1) {
                alert('ÆÐÅ°Áö »óÇ°Àº 2°³ ÀÌ»óÀ» ¼±ÅÃÇÏ¼Å¾ß ÇÕ´Ï´Ù.');
                return false;
            } // end if
        } // end if

        /**
         * ÆÐÅ°Áö ÀÌ¸§(id=pkgNameList)À¸·Î °Ë»ö½Ã ¼±ÅÃ°¡´ÉÇÑ »óÇ°(A,H,R,...) È®ÀÎ
         */
        if ($('pkgNameList') != null) {
            if ($F('pkgNameList').empty()) {
                $('pkgName').value = '';    // ÀüÃ¼º¸±â
            }
            else {
                var isAlert     = true;
                var alertMsg    = '';
                var pkgItemList = $F('pkgNameList').split('|');     // AHR|AH|HR
                var pkgName     = $('pkgNameList').options[$('pkgNameList').selectedIndex].text;
                var sPkgItemName= this.pkgItemName;
                var sPkgItem    = this.pkgItem;

                pkgItemList.each(function(pkgItem) {
                    var msg = '';

                    sPkgItemName.each(function(element) {
                        if (pkgItem.indexOf(element.item) != -1) {
                            msg += ' + '+element.name;
                        } // end if
                    });

                    if (!msg.empty()) {
                        alertMsg += ', \n['+msg.substr(3)+']';
                    } // end if

                    if (pkgItem == sPkgItem) {
                        isAlert = false;
                    } // end if

                });

                if (isAlert) {
                    alert('¼±ÅÃÇÏ½Å »óÇ°['+pkgName+']Àº\n'
                            +alertMsg.substr(2)+'\n\n'
                            +'À» ¼±ÅÃÇßÀ» ¶§¸¸ ÀÌ¿ëÀÌ °¡´ÉÇÕ´Ï´Ù.');
                    return false;
                } // end if

                $('pkgName').value = pkgName;
            } // end if
        } // end if

        $(frmId).action = actionUrl;

        if (Number($F('adultNum')) == 0 || (Number($F('adultNum')) + Number($F('childNum'))) < 2) {
            alert('¼ºÀÎÀ» Æ÷ÇÔÇÑ 2ÀÎ ÀÌ»ó¸¸ ¿¹¾àÀÌ °¡´ÉÇÕ´Ï´Ù.');

            return false;
        } // end if

        if (this.rsvSdate > sdate || this.rsvEdate < sdate) {
            // lib.date.js: getDateFmt()
            alert('¿¹¾à °¡´É ±â°£À» ¹þ¾î³³´Ï´Ù.\n\n'
                    + '¿¹¾à °¡´É ±â°£:\n'
                    + getDateFmt(this.rsvSdate, 'Y³â m¿ù dÀÏºÎÅÍ ')
                    + getDateFmt(this.rsvEdate, 'Y³â m¿ù dÀÏ±îÁö '));
            return false;
        } // end if

        return true;
    }, // end of the 'search()' function


    /**
     * Ãâ¹ßÁö È°¼ºÈ­
     *
     * usage:
     *  <input type="checkbox" id="pkgItem_A" onclick="enableSAirport('container_id')">Ç×°ø
     *         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *  ...
     *  <TAG id="container_id">
     *          ~~~~~~~~~~~~~~
     *      <input type="radio" ...>
     *      <input type="radio" ...>
     *      ...
     *  </TAG>
     *  OR
     *  <select name="sAirport" id="container_id">...</select>
     *
     * @param   string      DOM element
     *
     */
    enableSAirport: function(container_id)
    {
        var inputs;

        // select
        if ($(container_id).type == 'select-one') {
            $(container_id).disabled = !$('pkgItem_A').checked;
            return;
        } // end if
        
        // input (radio)
        inputs = $(container_id).getElementsByTagName('input');

        $A(inputs).each(function(r, index) {
            if (r && r.type == 'radio') {
                if ($('pkgItem_A').checked) {
                    if (index == 0) {
                        r.checked = true;
                    } // end if

                    r.disabled = false;
                }
                else {
                    r.disabled = true;
                } // end if
            } // end if
        });
    }, // end of the 'enableSAirport()' funciton


    /**
     * ¿©ÇàÀÏÀÚ ¼³Á¤
     *
     * @param   integer     sdate
     * @param   integer     stay
     */
    setRsvDate: function(sdate, stay)/*{{{*/
    {
        var tmp = getDateFmt(new Date(sdate*1000),'y-m-d-h').split('-');

        // lib.input.js: set_option_elem()
        set_option_elem($('syy'), Number(tmp[0]));
        set_option_elem($('smm'), Number(tmp[1]));
        set_option_elem($('sdd'), Number(tmp[2]));
        if ($('shh') != null) {
            set_option_elem($('shh'), Number(tmp[3]));
        } // end if
        set_option_elem($('stay'), Number(stay));

    }, // end of the 'setRsvDate()' function/*}}}*/


    /**
     * ÆÐÅ°Áö ÀÌ¸§ Á¶È¸
     *
     * <div id="pkgNameListContainer" class="inline">
     *   <select name="pkgNameList" id="pkgNameList"><option value="">:: ÀüÃ¼º¸±â ::</option>{SF_PKG_NAME_OPT_LIST}</select>
     * </div>
     *
     * @param   string      DOM element
     */
    getPkgNameList: function(container_id)
    {
        this.getPkgItem(container_id);

        if ($('pkgNameList') == null) {
            return false;
        }
        else if (this.pkgItem.length <= 1) {
            $('pkgNameListContainer').update(
                '<select name="pkgNameList" id="pkgNameList">'
                + '<option value="">:: ÀüÃ¼º¸±â ::</option>'
                + '</select>'
            );

            return false;
        } // end if

        $('pkgNameListContainer').update('Á¶È¸Áß......');

        new Ajax.Request(
                _CZ_PATH + '/_ajax/ajaxGetPkgNameList.php',
                {
                    parameters: {pkgItem: $F('pkgItem')
                                , exceptedPkgName: $('exceptedPkgName') != null ? $F('exceptedPkgName') : ''},
                    onSuccess: function(req) {
                        eval('var rData = '+req.responseText+';');
                        var pkgNameOptList = '';

                        if (!rData.IS_ERROR) {
                            rData.LIST.each(function(item) {
                                pkgNameOptList += '<option value="'+item.PKG_ITEM+'">'+item.PKG_NAME+'</option>';
                            });                            
                        } // end if

                        $('pkgNameListContainer').update(
                            '<select name="pkgNameList" id="pkgNameList">'
                            + '<option value="">:: ÀüÃ¼º¸±â ::</option>'
                            + pkgNameOptList
                            + '</select>'
                        );
                    } // end of the 'onSuccess' function
                }
        );

    } // end of the 'getPkgNameList()' function
});
// ]]>