/**
 * $Id$
 **/

function submitSearch(_check){
    
    var ie6=$.browser.msie&&parseInt($.browser.version)==6&&typeof window['XMLHttpRequest']!="object";
    var _params = {'get':[], 'path':[]};
    var condition = null;

    if(_check == true || typeof _check == 'undefined'){
        condition = checkCount();
    }else{
        condition = true;
    }
    
    if(condition == false){
        return false;
    }

    //ボタン二度押し対策
    if(ie6){
        button = $('input');
        for(var i=0; i<button.length; i++){
           if(button[i].type == 'image'){
               button[i].disabled = true;
           }
        }
    }else{
        $(':image').attr('disabled', true);
    }

    //input用処理
    var _nodes = document.getElementsByTagName('input');
    for(i=0; i<_nodes.length; i++){

        //type
        var _type = _nodes[i].type;
        //name
        var _name = _nodes[i].name;
        //value
        var _value = _nodes[i].value;

        if(in_array(_type, type) == true && typeof _value != 'undefined' && _value != ''){
            
            isValue = true;
            switch(_type){
                case 'checkbox':
                case 'radio':
                    if(!_nodes[i].checked){
                        isValue = false;
                    }
                    break;
                case 'text':
                    if(_nodes[i].value == '店名、駅名、業種、カテゴリ名など' || _nodes[i].value == 'サロン名、駅名、業種、カテゴリなど'){
                        isValue = false;
                        keyjoinCtl = false;
                    }
                    break;
            }

            if(isValue == true){

                //パラメータ区分をgetとpathに振り分ける
                if(in_array(_name, getScorp)){
                    if(!(_params['get'][_name] instanceof Array)){
                        _params['get'][_name] = new Array();
                    }
                    _params['get'][_name].push(encodeURIComponent(_value));
                }
                if(in_array(_name, pathScorp)){
                    if(!(_params['path'][_name] instanceof Array)){
                        _params['path'][_name] = new Array();
                    }
                    _params['path'][_name].push(encodeURIComponent(_value));
                }
            }
        }
    }
    
    //select用処理
    

//日付してがない場合のパラメータ整形を行わないためコメント（仕様変更対応用）
//    //日付用
//    var fmYear= null;
//    var fmMonth= null;
//    var fmDay= null;
//    var toYear= null;
//    var toMonth= null;
//    var toDay= null;

    var fmdateFlg = true;
    var todateFlg = true;

    _nodes = $('select option:selected');
    for(i=0; i<_nodes.length; i++){

        //name
        _name = _nodes[i].parentNode.name;
        //value
        _value = _nodes[i].value;
        //id
        _id = _nodes[i].parentNode.id;
        //disabled
        _disabled = _nodes[i].parentNode.disabled;

        if(fmdateFlg && _name == 'fmdate' && _disabled == true){
            fmdateFlg = false;
        }
        if(todateFlg && _name == 'todate' && _disabled == true){
            todateFlg = false;
        }

        if(typeof _value != 'undefined' && _value != '' && _disabled == false){

//日付してがない場合のパラメータ整形を行わないためコメント（仕様変更対応用）
//            //開始日
//            if(_id == 'sdNrYear'){fmYear = _value;}
//            if(_id == 'sdNrMonth'){fmMonth = _value;}
//            if(_id == 'sdNrDay'){fmDay = _value;}
//
//            //終了日
//            if(_id == 'sdNrYear2'){toYear = _value;}
//            if(_id == 'sdNrMonth2'){toMonth = _value;}
//            if(_id == 'sdNrDay2'){toDay = _value;}
//
            //日付整形
            //開始日
            if( _id == 'sdNrMonth' ||
                _id == 'sdNrDay' ||
                _id == 'sdNrMonth2' ||
                _id == 'sdNrDay2'){
                if(_value < 10){
                    _value = '0'+　_value;
                }
            }

            //パラメータ区分をgetとpathに振り分ける
            if(in_array(_name, getScorp)){
                if(array_key_exists(_name, _params['get'])){

                    _params['get'][_name] = _params['get'][_name] + encodeURIComponent('/') + _value;

                }else{

                    _params['get'][_name] = _value;

                }
            }
            if(in_array(_name, pathScorp)){
                if(array_key_exists(_name, _params['path'])){

                    _params['path'][_name] = _params['path'][_name] + encodeURIComponent('/') + _value;

                }else{
                    
                    _params['path'][_name] = _value;

                }
            }
        }
    }
    
    if ((!array_key_exists('line',_params['path'])) && (!array_key_exists('stn',_params['path']))){
        delete _params['get']['val'];
    }
    //選択路線駅パラメータは、選択駅で再生成する
    lstnParams = lstnFilter(_params['get'],_params['path']);
    if (lstnParams == null){
        delete(_params['get']['lstn']);
    }else{
        _params['get']['lstn'] = lstnParams;
    }

    //選択親エリアパラメータは、エリア選択で再生成する
    pAreaParams = pFilter(_params['get'],_params['path'],'parea','area');
    if (pAreaParams == null){
        delete(_params['get']['parea']);
    }else{
        _params['get']['parea'] = pAreaParams;
    }

    //選択親エリアパラメータは、エリア選択で再生成する
    pLineParams = pFilter(_params['get'],_params['path'],'pline','line');
    if (pLineParams == null){
        delete(_params['get']['pline']);
    }else{
        _params['get']['pline'] = pLineParams;
    }

    //選択親カテゴリは、カテゴリ選択で再生成する
    pCateParams = pFilter(_params['get'],_params['path'],'pcate','cate');
    if (pCateParams == null){
        delete(_params['get']['pcate']);
    }else{
        _params['get']['pcate'] = pCateParams;
    }

    //開始日制御
    //年チェック
    if(targetPath.match(/beauty\/cam\/list/) != null){
        date = new Date();
//日付してがない場合のパラメータ整形を行わないためコメント（仕様変更対応用）
//        if(fmdateFlg == false){
//            delete _params['get']['fmdate'];
//        }else{
//            if(fmYear == null){
//                fmMonth = null;
//                fmYear = date.getYear();
//                if(fmYear < 2000){
//                    fmYear += 1900;
//                }
//            }
//
//            if(fmMonth == null){
//                fmDay = null;
//            }
//            //月調整
//            if(fmYear != null && fmMonth == null){
//                fmMonth = '01';
//            }
//            //日調整
//            if(fmYear != null && fmMonth != null && fmDay == null){
//                fmDay = '01';
//            }
//            _params['get']['fmdate'] = fmYear  + encodeURIComponent('/') + zerofill(fmMonth,2) + encodeURIComponent('/') + zerofill(fmDay,2);
//        }

        //終了日制御
        //年チェック
//日付してがない場合のパラメータ整形を行わないためコメント（仕様変更対応用）
//        if(todateFlg == false || fmdateFlg == false){
//            delete _params['get']['todate'];
//        }else{
//            if(toYear == null){
//
//                toMonth = null;
//                toYear = date.getYear();
//                if(toYear < 2000){
//                    toYear += 1900;
//                }
//            }
//            if(toMonth == null){
//                toDay = null;
//            }
//
//            //月調整
//            changeToMonth = false;
//            if(toYear != null && toMonth == null){
//                changeToMonth = true;
//                toMonth = '12';
//            }
//            //alert(changeToMonth);
//            //日調整
//            if((toYear != null && toMonth != null && toDay == null) || changeToMonth){
//                toDay = getMonthEndDay(toYear, toMonth);
//            }
//            _params['get']['todate'] = toYear  + encodeURIComponent('/') + zerofill(toMonth,2) + encodeURIComponent('/') + zerofill(toDay,2);
//        }

        //日付パラメータデフォルト制御
        if(typeof _params['get']['fmdate'] != 'undefined' ){

            var year = date.getYear();
            if(year < 2000){
                year += 1900;
            }
            var month =date.getMonth() + 1;
            if(month < 10){
                month = '0'+month;
            }
            var day = date.getDate()
            if(day < 10) {
                day = '0'+day;
            }
            var _today = year + encodeURIComponent('/') + month + encodeURIComponent('/') + day;
            if(_today == _params['get']['fmdate'] && typeof _params['get']['todate'] == 'undefined'){
               delete _params['get']['fmdate'];
            }
        }

        //日付制御ラジオボタン
        if(fmdateFlg == false){
            delete _params['get']['daytype'];
        }
    }
    
    //keyjoin制御
    if(typeof keyjoinCtl != 'undefined' && keyjoinCtl == false){
        delete _params['get']['keyjoin'];
    }
    
    //パスパラメータ作成処理
    var _pathParam = '';

    //ソート配列ループ
    for(i=0; i<pathScorp.length; i++){
        var addPathParam = '';
        if(array_key_exists(pathScorp[i], _params['path'])){
            if(_params['path'][pathScorp[i]] instanceof Array){
                _params['path'][pathScorp[i]].sort(sortfunc);
                for(var pathI=0; pathI < _params['path'][pathScorp[i]].length; pathI++){
                    if(addPathParam == ''){
                        addPathParam = _params['path'][pathScorp[i]][pathI];
                    }else{
                        addPathParam += '-' + _params['path'][pathScorp[i]][pathI];
                    }
                }
            }else{
                addGetParam = _params['path'][pathScorp[i]];
            }

            _pathParam = _pathParam + pathScorp[i] + '-' + addPathParam + '/';

        }
    }

    //GETパラメータ作成処理
    var _getParam = '';

    //ソート配列ループ
    for(i=0; i<getScorp.length; i++){
        var addGetParam = '';
        if(array_key_exists(getScorp[i], _params['get'])){
            if(_params['get'][getScorp[i]] instanceof Array){
                _params['get'][getScorp[i]].sort(sortfunc);
                for(var getI=0; getI < _params['get'][getScorp[i]].length; getI++){
                    if(addGetParam == ''){
                        addGetParam = _params['get'][getScorp[i]][getI];
                    }else{
                        addGetParam += '-' + _params['get'][getScorp[i]][getI];
                    }
                }
            }else{
                addGetParam = _params['get'][getScorp[i]];
            }
            
            if(_getParam == ''){
                if(getScorp[i] == 'Sort'){
                    _getParam = '?sort=' + addGetParam;
                }else{
                    _getParam = '?' + getScorp[i] + '=' + addGetParam;
                }
            }else{
                if(getScorp[i] == 'Sort'){
                    _getParam += '&sort=' + addGetParam;
                }else{
                    _getParam += '&' + getScorp[i] + '=' + addGetParam;
                }
            }
        }
    }
    
    //リンク先URLを作成
    targetPath = targetPath + _pathParam + _getParam;

    location.href = targetPath;

    return false;
    
}

