// JavaScript Document
function detectBrowser() {
	if (!navigator.cookieEnabled) {
		document.write ('<div class="error">Your browser does not support Cookies</div>');
	}
	if (navigator.appName != "Microsoft Internet Explorer") {
		document.write ('<div class="errorBig"></div>');
	}
}

function showConfirm(msg, redirectUrl) {
	var r = confirm(msg);
	if (r == true) {
		location.href = redirectUrl;
	}
	else {

	}
}

function openPopup(url, wname, w, h) {
	t = screen.height/2 - h/2;
	l = screen.width/2 - w/2;
	popupw = window.open(url,wname,'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+w+',height='+h+',top='+t+',left='+l);
	popupw.focus();
	res_h = parseInt(h) + 35;
	res_w = parseInt(w) + 10;
	popupw.resizeTo(res_w,res_h);
	//alert(url);
}

function getSelectedIds(dropdown) {
	selItems = new Array();
	for (var i=0; i < dropdown.options.length; i++) {
		if (dropdown.options[i].selected) {
			selItems.push(dropdown.options[i].value);
		}
	}
	return selItems;
}

// automatinis comboboxu filtravimas
function filterDropdown(name, arr, parent_name, filter_empty) {
	// pasalinimas visu elementu
	var parent_dropdown = document.getElementById(parent_name);
	var dropdown = document.getElementById(name);
	var total = dropdown.options.length;
	for (var i=total-1; i>=0; i--) {
		dropdown.remove(i);
	}
	// pats susiranda kurie itemai yra pazymeti
	selItems = getSelectedIds(parent_dropdown);
	// nauju addinimas
	for (var i=0; i<arr.length; i++) {
        //add = false;
        add = (selItems.length == 0);
        if (add && !filter_empty && dropdown.length > 0) {
			add = false;
		}
    	for (var j=0; j<selItems.length; j++) {
			if (selItems[j] == arr[i][1] || arr[i][1] == '' || (filter_empty && (selItems[j] == '0'))) {
				add = true;
				break;
			}
		}
		if (add) {
			var elm = document.createElement('option');
			elm.value = arr[i][0];
			elm.text = arr[i][2];
			elm.selected = arr[i][3];
			arr[i][3] = false;
			try {
				dropdown.add(elm,null); // standards compliant
			}
			catch(ex) {
				dropdown.add(elm); // IE only
			}
		}
	}
}

function addToHidden(dropdown) {
	selItems = getSelectedIds(dropdown);
	hiddenF = document.getElementById('h_'+dropdown.name);
	hiddenF.value = '[' + selItems + ']';
}

function displayFromHidden(el_name) {
    dropdown = document.getElementById(el_name);
	hiddenF = document.getElementById('h_'+el_name);
	selItems = eval(hiddenF.value);
	if (selItems) {
		for (var i=0; i < dropdown.options.length; i++) {
	        dropdown.options[i].selected = false;
    		for (var j=0; j<selItems.length; j++) {
				if (selItems[j] == dropdown.options[i].value) {
					dropdown.options[i].selected = true;
					break;
				}
			}
		}
	}
}


function loginFocus(elm, str) {
	if (elm.value == str) {
        elm.value = '';
	}
}

function loginBlur(elm, str) {
	if (elm.value == '') {
        elm.value = str;
	}
}

function redirect(url) {
	document.location.href = url;
}

function urlLocal(dropdown, pref, suff) {
	selItems = getSelectedIds(dropdown);
	redirect(pref + selItems[0] + suff);
}

function validTxt (elm, maxl) {
	if (elm.value.length > maxl) {
		elm.value = elm.value.substr(0,maxl);
	}
	var a = document.getElementById('textRemainChr');
	if (a) {
		a.innerHTML = maxl - elm.value.length;
	}
}
