login_user_univ = '';
login_user_faculty = '';

/*======================================================================*/
/*  大学検索
/*======================================================================*/
var univ = function () {
}
univ.prototype = {
    onSuccess : function (httpObj) {

        var response = eval('(' + httpObj.responseText + ')');
        var select = $('univlist');
        select.innerHTML = ''; // エレメント初期化
        
        for (var i=0; i < response.length; i++) {
            var option = document.createElement('option');
            option.value = response[i]['univ'];
            var string = document.createTextNode(response[i]['univ']);
            if (response[i]['univ'] == login_user_univ) {
                option.setAttribute('selected', true);
            }
            option.appendChild(string);
            select.appendChild(option);
        }
        // 検索にヒットしないとき
        if (response.length == 0) {
            var option = document.createElement('option');
            option.value = '';
            var string = document.createTextNode('候補がありません');
            option.appendChild(string);
            select.appendChild(option);

            var facultyOption = document.createElement('option');
            facultyOption.value = '';
            var string = document.createTextNode('候補がありません');
            facultyOption.appendChild(string);
            $('faculty').innerHTML = '';
            $('faculty').appendChild(facultyOption);

            var marketOption = document.createElement('option');
            marketOption.value = '';
            var string = document.createTextNode('候補がありません');
            marketOption.appendChild(string);
            $('market').innerHTML = '';
            $('market').appendChild(marketOption);
        } else {
            $('market').disabled = false;
        }
    },
    onFailure : function (httpObj) {
        alert("[通信エラー]\nしばらく待って検索してください");
    },
    onComplete : function (httpObj) {
        if ($('univlist').value) {
            // 選択候補先頭の大学の学部を検索
            facultySearch.query($('univlist').value);
        }
    }
}
var univSearch = {
    
    _word: '',
    
    query: function (word) {

        this._word = trim(word);
        this._request();
    },
    
    _request: function () {
        
        var univObj = new univ(); 
        new Ajax.Request('/',
        {
            method: 'get',
            parameters: 'module=api&action=univ&univ=' + this._word + '&rand=' + Math.floor(1000000 * Math.random()),
            onSuccess: univObj.onSuccess.bindAsEventListener(univObj),
            onFailure: univObj.onFailure.bindAsEventListener(univObj),
            onComplete: univObj.onComplete.bindAsEventListener(univObj)
        });
    }
}


/*======================================================================*/
/*  学部検索
/*======================================================================*/
var faculty = function () {
}
faculty.prototype = {
    onSuccess : function (httpObj) {

        var response = eval('(' + httpObj.responseText + ')');
        var select = $('faculty');
        select.innerHTML = ''; // エレメント初期化
        
        for (var i=0; i < response.length; i++) {
            var option = document.createElement('option');
            option.value = response[i]['faculty'];
            var string = document.createTextNode(response[i]['faculty']);
            if (response[i]['faculty'] == login_user_faculty &&
                $('univlist').value == login_user_univ) {
                option.setAttribute('selected', true);
            }
            option.appendChild(string);
            select.appendChild(option);
        }
        if (response.length == 0) {
            var option = document.createElement('option');
            option.value = '';
            var string = document.createTextNode('候補がありません');
            option.appendChild(string);
            select.appendChild(option);
                }
    },
    onFailure : function (httpObj) {
        alert("[通信エラー]\nしばらく待って検索してください");
    },
    onComplete : function (httpObj) {
        if ($('faculty').value) {
            // 選択した大学・選択候補先頭の学部の内定先業界を検索
            marketSearch.query($('univlist').value, $('faculty').value);
        }
    }
}
var facultySearch = {
    
    _word: '',
    
    query: function (word) {

        this._word = trim(word);
        this._request();
    },
    
    _request: function () {
        
        var facultyObj = new faculty(); 
        new Ajax.Request('/',
        {
            method: 'get',
            parameters: 'module=api&action=faculty&univ=' + this._word + '&rand=' + Math.floor(1000000 * Math.random()),
            onSuccess: facultyObj.onSuccess.bindAsEventListener(facultyObj),
            onFailure: facultyObj.onFailure.bindAsEventListener(facultyObj),
            onComplete: facultyObj.onComplete.bindAsEventListener(facultyObj)
        });
    }
}


/*======================================================================*/
/*  業界検索
/*======================================================================*/
var market = function () {
}
market.prototype = {
    onSuccess : function (httpObj) {

        var response = eval('(' + httpObj.responseText + ')');
        var select = $('market');
        select.innerHTML = ''; // エレメント初期化
        
        for (var i=0; i < response.length; i++) {
            var option = document.createElement('option');
            option.value = response[i]['bbs_id'];
            var string = document.createTextNode(response[i]['bbs_id_name']);
            option.appendChild(string);
            select.appendChild(option);
        }
        if (response.length == 0) {
            var option = document.createElement('option');
            option.value = '';
            var string = document.createTextNode('候補がありません');
            option.appendChild(string);
            select.appendChild(option);
                }
    },
    onFailure : function (httpObj) {
        alert("[通信エラー]\nしばらく待って検索してください");
    }
}
var marketSearch = {
    
    _univ: '',
    _faculty: '',
    
    query: function (univ, faculty) {

        this._univ = trim(univ);
        this._faculty = trim(faculty);
        this._request();
    },
    
    _request: function () {
        
        var marketObj = new market(); 
        new Ajax.Request('/',
        {
            method: 'get',
            parameters: 'module=api&action=market&univ=' + this._univ + '&faculty=' + this._faculty + '&rand=' + Math.floor(1000000 * Math.random()),
            onSuccess: marketObj.onSuccess.bindAsEventListener(marketObj),
            onFailure: marketObj.onFailure.bindAsEventListener(marketObj)
        });
    }
}


function trim(value){
    return String(value).replace(/^[ 　]*/gim, "").replace(/[ 　]*$/gim, "");
}



// 連続入力 (true:連続入力中 / false:入力間隔があいている)
var continuousInput;

// 前回検索実行したときのキーワード
var lastKeyword;

/**
 * 大学名検索実行
 */
function univSuggest() {

    // 検索キーワードが1文字以下なら検索しない
    var trimedUniv = trim($('univ').value); 
    if (trimedUniv.length <= 1) {
        return;
    }

    // 最後の入力から1秒以内は検索しない
    if (continuousInput) {
        continuousInput = false;
        return;
    }

    // キーワードが前回と同じなら検索しない
    if ($('univ').value == lastKeyword) {
        return;
    }

    // 検索実行
    univSearch.query($('univ').value);

    // 検索キーワードを保持する
    lastKeyword = $('univ').value;
}


/**
 * 学部名検索実行
 */
function facultySuggest() {

    // 検索実行
    facultySearch.query($('univlist').value);
}


/**
 * 業界名検索実行
 */
function marketSuggest() {
    // 検索実行
    marketSearch.query($('univlist').value, $('faculty').value);
}


/**
 * 検索自動実行タイマー開始
 */
function startTimer() {
    continuousInput = true;
    lastKeyword = '';
    var timer = new PeriodicalExecuter(univSuggest, 0.5);
}


/**
 * キー入力時に連続入力状態にする
 */
function keepOnKeyUpTime() {
    continuousInput = true;
}

