/* Version: $Id: searchform.js 916 2010-04-23 13:28:10Z vysohlidm $ */

function searchFormSubmitSimple() {
    clrKeyword('keyword');
    var isFormEmpty = isItemEmpty({
        'branchList' : '',
        'professionList' : '',
        'local' : '',
        'keyword' : '',
        'workchar' : '201300001;201300002;201300003',
        'minsal' : '',
        'joblang' : ''
    });
/*    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('keyword');
    }*/
    
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        fillKeyword('keyword');
        return false;
    }
//    return !isFormEmpty;
    return true;
}

function searchFormSubmitInternational() {
    clrKeyword('keyword');
    var isFormEmpty = isItemEmpty({
        'branchList' : '',
        'local' : '',
        'keyword' : ''
    });
/*    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('keyword');
    }*/
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        fillKeyword('keyword');
        return false;
    }
//    return !isFormEmpty;
    return true;
}

function searchFormSubmitCompany() {
    var isFormEmpty = isItemEmpty({
        'cmpAct' : '',
        'cmpTp' : '',
        'maxEmp3' : '',
        'cmpOwn3' : ''
    });
/*    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
    }*/
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        return false;
    }
//    return !isFormEmpty;
    return true;
}

function searchFormSubmitAdvanced() {
    clrKeyword('keyword');
    var isFormEmpty = isItemEmpty({
        'branchList' : '',
        'professionList' : '',
        'local' : '',
        'keyword' : '',
        'cmpAct2' : '',
        'reqLang' : '',
        'minSal' : '',
        'bnft' : '',
        'adtype': '',
        'adtime': '',
        'joblang' : ''
    });
/*    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('keyword');
    }*/
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        fillKeyword('keyword');
        return false;
    }
//    return !isFormEmpty;
    return true;
}

function searchFormAbsolventSubmit() {
    clrKeyword('search-klic', __('graduates.searchbox.keywords.default'));
    var isFormEmpty = isItemEmpty({
        'search-obory' : '',
        'search-lokality' : '',
        'search-klic' : '',
        'search-plat' : ''
    });
/*    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('search-klic', __('graduates.searchbox.keywords.default'));
    }
*/    
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        fillKeyword('search-klic', __('graduates.searchbox.keywords.default'));
        return false;
    }
//    return !isFormEmpty;
    return true;
}

function clrKeyword(kwId, translation) {
    if (!translation) {
        translation = __('Jobs_JD_CE_SearchFastFulltext');
    }
    p = document.getElementById(kwId);
    if ( p && p.value == translation) {
        p.value = '';
    }
}

function fillKeyword(kwId, translation) {
    if (!translation) {
        translation = __('Jobs_JD_CE_SearchFastFulltext');
    }
    p = document.getElementById(kwId);
    if ( p && p.value == '') {
        p.value = translation;
    }

}

function isItemEmpty(items) {
    var result = true;
    for (elementId in items) {

        if (result != true) {
            break;
        }

        var element = document.getElementById(elementId);
        if (!element) { // element not found -> continue to next element
            continue;
        }
        //alert(element.type);
        switch (element.type) {
            case 'select-multiple':
                for (var j = 0; j < element.options.length; j++) {
                    if (element.options[j].selected && element.options[j].value != items[elementId]) {
                        result = false;
                    }
                }
                break;
            case 'select-one':
                if (element.selectedIndex != -1) {
                    if (element.options[element.selectedIndex].value != items[elementId]) {
                        result = false;
                    }
                }
                break;
            case 'text':
                if (element.value != items[elementId]) {
                    result = false;
                }
                break;
            default:
                alert('Unknown item '+element.type);
                break;
        }
    }
    return result;
}

// fce pro validaci formularu (pokud neni nic vybrano, form se neodesila)
var updated = {};
function upd(el) {
    if (el.name.indexOf('srch') > -1) {
        if (el.value == 0 || el.value == '') updated[el.name] = null;
        else updated[el.name] = 1;
    }
}

function ret() {
    var len = 0;
    for (it in updated) {
        if (updated[it] != null && updated[it] != 'undefined') {
            len++;
        }
    }
    return (len > 0);
}
// end of validace


function advSrch() {
    formData = {};
    // obor
    formData['prof'] = {};
    cntr = 0;
    a = document.getElementById('branchList');
    if (a != null) {
        for (i=0;i<a.options.length;i++) {
            if (a.options[i].selected == true) {
                formData['prof'][cntr] = a.options[i].value;
                cntr++;
            }
        }
    }
    // profese
    formData['skill'] = {};
    cntr = 0;
    a = document.getElementById('professionList');
    if (a != null) {
        for (i=0;i<a.options.length;i++) {
            if (a.options[i].selected == true) {
                formData['skill'][cntr] = a.options[i].value;
                cntr++;
            }
        }
    }
    // lokalita
    a = document.getElementById('local');
    if (a != null) formData['local'] = a.options[a.selectedIndex].value;
    // keyword
    formData['key'] = document.getElementById('keyword').value != Dictionary.get('Jobs_JD_CE_SearchFastFulltext') ? document.getElementById('keyword').value : '';
    // minsal
    a = document.getElementById('minsal');
    if (a != null) formData['minsal'] = a.options[a.selectedIndex].value;
    // emptype
    a = document.getElementById('workchar');
    if (a != null) formData['emp'] = a.options[a.selectedIndex].value;
    // pa
    a = document.getElementById('ckAgency');
    if (a != null) formData['agent'] = (a.checked) ? a.value : '';
    var url = URLManager.getUrl('bookmark.AdvancedSearch') + '?lang=' + Dictionary.get('language_code');
    for (i in formData) {
        if (formData[i] != 'undefined') {
            if (formData[i].toString().indexOf('object') > 0) {
                for (ii in formData[i]) {
                    // all of them
                    url = url + '&srch[' + i + '][]=' + formData[i][ii];
                }
            } else {
                // one
                url = url + '&srch[' + i + ']=' + formData[i];
            }
        }
    }
    document.location.href = encodeURI(url);
    return false;
}


// select max X items from listbox obj
function selectMax(cnt, obj) {
    selCnt = 0;
    for (itm=0;itm<obj.options.length;itm++) {
        if (obj.options[itm].selected == true) {
            selCnt++;
            if (selCnt > cnt) obj.options[itm].selected = false;
        }
    }
    if (selCnt > cnt) {
        return false;
    }
    return true;
}

var globalItemCounter = {};
var MAX_SELECTED_ITEMS = 50;

function itemsCounter(e) {		
    var el = YAHOO.util.Event.getTarget(e);
    switch (el.tagName) {
        case 'SELECT':
            if (el.id == 'branchList') { // exception for branch list
                globalItemCounter['professionList'] = 0;
            }
            if (el.id == 'branchList2') { // exception for branch list
                globalItemCounter['professionList2'] = 0;
            }
				
            var count = 0;
            for (var i = 0; i < el.options.length; i++) {
                if (el.options[i].selected && el.options[i].value != '') {
                    count++;
                }
            }
            globalItemCounter[el.id] = count;
            break;
        case 'INPUT':
            if (el.value != '') {
                globalItemCounter[el.id] = Math.round(el.value.length/5);
            } else {
                globalItemCounter[el.id] = 0;
            }
            break;
    }
    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
    }
}	

function getCounter() {
    var total = 0;
    for (var i in globalItemCounter) {
        total += globalItemCounter[i];
    }
    return total;
}

function resetCounter() {
    globalItemCounter = {};
}

// for select-one, should be called with offset 1 => ignore first line
function hasSelected(oSel, pOffset) {
    var len = oSel.options.length;
    for (var i = (1*pOffset); i < len; i++) {
        if (oSel.options[i].selected && oSel.options[i].value) {
            return true; //at least an option is selected
        }
    }
    return false; //nothing is selected
}
// new generation of search form inputs validation
function validateEmpty(oFrm) {

    if (getCounter() > MAX_SELECTED_ITEMS) {
        Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
        return false;
    }

    var len = oFrm.length;
    for (var i = 0; i < len; i++) {
        //alert(oFrm.elements[i].type); continue;
        switch (oFrm.elements[i].type) {
            case "select-one":
                //alert (oFrm.elements[i].name + " x " + hasSelected(oFrm.elements[i], 1));
                if (hasSelected(oFrm.elements[i], 1)) return true;
                break;
            case "select-multiple":
                //alert (oFrm.elements[i].name + " x " + hasSelected(oFrm.elements[i], 0));
                if (hasSelected(oFrm.elements[i], 0)) return true;
                break;
            case "text":
                //alert (oFrm.elements[i].name + " x " + oFrm.elements[i].value);
                if (oFrm.elements[i].value) return true;
                break;
            case "textarea":
                if (oFrm.elements[i].value) return true;
                break;
            case "password":
                if (oFrm.elements[i].value) return true;
                break;
            default: // for hidden, submit, file, undefined, radio, checkbox...
                break;
        }
    }
    // show modal window with warning message
    Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
    return false;
}
// end of validation

function callLoadProfession() {
    if (typeof(selectedBranch) != "undefined" && selectedBranch.length > 0) loadProfession('branchList', 'professionList');
}

YAHOO.util.Event.addListener(window, 'load', callLoadProfession);