﻿function OpenMapWindow(url, windowName, showSearchBox) {
    // velikost okna
    var mapWidth = [640, 800, 1024];
    var mapHeight = [480, 600, 768];
    var width = mapWidth[0];
	var height = mapHeight[0];
    for (var i = 1; i < mapHeight.length; i++){
        if (window.screen.availHeight > mapHeight[i] && window.screen.availWidth > mapWidth[i]) {
            width = mapWidth[i];
	        height = mapHeight[i];
        }
    }
		
	var options = 'width=' + width + ', height=' + height + ', status=yes, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=no';
	window.open(url, windowName, options);
}

// vyber objektu z mapy
function OpenObjectChoiceMapWindow(url, windowName, formItemName, searchUrl, searchUrlBack) {
	var txtObject = document.getElementById(formItemName + '_txtObject');
	var cboObject = document.getElementById(formItemName + '_cboObject');
	var cboCategory = document.getElementById(formItemName + '_cboCategory');
	var txtVirtListItemCode = document.getElementById(formItemName + '_txtVirtListItemCode');
	var txtFormState = document.getElementById(formItemName + '_txtFormState');
	
	if (txtObject) {url += '&mask=' + encodeURIComponent(txtObject.value);}
	var checkSearch = true;
	if (cboObject && (cboObject.style.display != 'none')) {
		var objectVListCode = cboObject.options[cboObject.selectedIndex].value
		if (objectVListCode.length > 0) {url += '&vListCode=' + objectVListCode;}
		checkSearch = false;
	} else if (txtObject && cboCategory && txtVirtListItemCode && txtFormState) {
		var txtFormStateNow = txtObject.value + ';' + cboCategory.options[cboCategory.selectedIndex].value;
		if ((txtVirtListItemCode.value.length > 0) && (txtFormStateNow == txtFormState.value)) {
			url += '&vListCode=' + txtVirtListItemCode.value;
			checkSearch = false;
		}
	}
	if (txtObject && checkSearch) {
		var text = TrimStr(txtObject.value);
		if (text.length > 0 && !IsMapObject(text)) {
			url = searchUrl + '&srcForm=Search&phrase=' + encodeURIComponent(text) + '&back=' + encodeURIComponent(searchUrlBack);
		}
	}
	
	OpenMapWindow(url, windowName, true)
}

// vyhledavani objektu na mape
function OpenObjectSearchMapWindow(url, windowName, textBoxId, searchUrl, searchUrlBack) {
	var txtMask = document.getElementById(textBoxId);
	if (txtMask) {
		var text = TrimStr(txtMask.value);
		if (text.length > 0) {
			url = searchUrl + '&srcForm=Search&phrase=' + text + '&back=' + encodeURIComponent(searchUrlBack);
		}
	}
	OpenMapWindow(url, windowName, true)
}

function TrimStr(text) {
	var start = 0; var end = 0; var fchar = false;
	for (var i = 0; i < text.length; i++) {
		if (text.substring(i, i + 1) == ' ') {
			if (! fchar) {start = i + 1;}
		} else {
			fchar = true;
			end = i + 1;
		}
	}
	if (start > end) {end = start;}
	return text.substring(start, end);
}

function IsMapObject(text) {
	if (text.length < 2) {return false;}
	if (text.substring(0, 2) == 'm:') {return true;}
	if (text.substring(0, 2) == 'M:') {return true;}
	return false;
}

function SetFocus(itemName) {
	oItem = document.getElementById(itemName);
	if (oItem) {try {oItem.focus();} catch(e) {}}
}

