/* Version: $Id: searchform.js 211 2009-07-15 09:48:46Z raidam $ */

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

function searchFormSubmitInternational() {
    clrKeyword('keyword3');
    var isFormEmpty = isItemEmpty({'branchList3' : '', 'localityList3' : '', 'keyword3' : ''});
    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('keyword3');
    }
	if (getCounter() > MAX_SELECTED_ITEMS) {
		Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
		return false;
	}    
    return !isFormEmpty;
}

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

function searchFormSubmitAdvanced() {
    clrKeyword('keyword2');
    var isFormEmpty = isItemEmpty({'branchList2' : '', 'professionList2' : '', 'localityList2' : '', 'keyword2' : '', 'cmpAct2' : '', 'reqLang' : '', 'minSalary2' : '', 'workChar2' : '201300001;201300002;201300003', 'bnft' : ''});
    if (isFormEmpty) {
        Modal.showTpl(__('empty_search_warning_title'), 'emptySearchModalTemplate', {});
        fillKeyword('keyword2');
    }
	if (getCounter() > MAX_SELECTED_ITEMS) {
		Modal.showTpl(__('overload_search_warning_title'), 'overloadSearchModalTemplate', {});
		return false;
	}    
    return !isFormEmpty;
}

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

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

}

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;
		}
		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;
        // 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('Jobs_Search_Extended') + '?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 = {};
}
