var dynamicModules = new Array();
var statusVisible = false;
var componentEventListeners = new Array();
var openedDialog = "";

var VALID_NOTEMPTY = 1;
var VALID_MORETHAN = 2;
var VALID_EQUALS = 4;

function displayStatus(statusString) {
	if (!statusVisible) {
		statusVisible = true;
		var status = document.getElementById("modulestatus");
		with (status) {
			innerHTML = statusString;
		}
		$("modulestatus").fade(1);
	}
	setTimeout("hideStatus()", 2000);
}

function hideStatus() {
	$("modulestatus").fade(0);
	statusVisible = false;
}

function listenComponentEvents(listenerComponentName, componentName) {
	componentEventListeners[componentEventListeners.length] = {
		listenerName : listenerComponentName,
		name : componentName
	};
}

function fireEvent(componentName, eventName, eventParams) {
	for (x in componentEventListeners) {
		if (componentEventListeners[x].name == componentName) {
			eval(componentEventListeners[x].listenerName + ".handleEvents('" + eventName + "', new Array('" + eventParams.join("','") + "'))");
		}
	}
}

function deserialize(array) {
	var result = new Array();
	for (var x = 0; x < array.length; x += 2) {
		result[array[x]] = array[x+1];
	}
	return result;
}

function in_array(arr, value) {
	var result = false;
	for (var i = 0; i<arr.length; i++) {
		result = arr[i] == value;
		if (result) { break; }
	}
	return result;
}

function closeOpenedDialog() {
	if (openedDialog) {
		openedDialog.style.display = "none";
	}
}

function setOpenedDialog(dlg) {
	openedDialog = dlg;
}

function loadDayList(daylistItem, year, month, value) {
	for (var i = 0; i < daylistItem.options.length; daylistItem.remove(0));
	var daysinmonth = monthDays[month - 1];
	if (month == 2 && year % 4 == 0) {
		daysinmonth = 29;
	}
	for (var i = 0; i < daysinmonth; i++) {
		var opt = document.createElement("option");
		opt.text = i + 1;
		opt.value = i + 1;
		if (i + 1 == value) {
			opt.selected = true;
		}
		try {
			daylistItem.add(opt);
		}
		catch (e) {
			daylistItem.add(opt, null);
		}
	}
}

function toggleVisibility(id, style) {
	$(id).style.display = ($(id).style.display == "none" ? style : "none");
}

function highlightButton(item) {
	item.className = "adminbutton buttonhighlighted";
	//item.addClass("buttonhighlighted");
}

function unhighlightButton(item) {
	item.className = "adminbutton";
	//item.removeClass("buttonhighlighted");
}

function highlightClass(item, classname) {
	item.addClass(classname);
}

function unhighlightClass(item, classname) {
	item.removeClass(classname);
}

function highlightMenu(item) {
	item.className = "menu menuhi";
}

function unhighlightMenu(item) {
	item.className = "menu menuunhi";
}

function logout() {
	var PHPFilename = getPHPFilename(window.location.pathname);
	if (PHPFilename == "admin.php") {
		window.location = PHPFilename + "?module=login&service=adminlogout";
	} else {
		window.location = PHPFilename + "?module=login&service=logout";
	}
}

function setStatus(statusString, type) {
	var status = $("modulestatus");
	var statusSlide = new Fx.Slide("modulestatus");
	if (type == "error") {
		status.className += " error";
	}
	status.innerHTML = statusString;
	statusSlide.slideIn();
}

function openDialog(id) {
	if ($("modal")) {
		$("modal").style.display = "block";
		$("modal").style.width = getPageSizeWithScroll()[0];
		$("modal").style.height = getPageSizeWithScroll()[1];
	}
	setOpenedDialog($(id));
	with ($(id).style) {
		display = "block";
		position = "absolute";
		top = "100px";
		left = "100px";
	}
}

function orderChanged(item) {
	var s = new Array();
	s[0] = document.getElementsByName("project_order")[0];
	s[1] = document.getElementsByName("employee_order")[0];
	s[2] = document.getElementsByName("role_order")[0];
	s[3] = document.getElementsByName("function_order")[0];
	for (var i = 0; i < s.length; i++) {
		if (s[i].options[s[i].selectedIndex].value == item.options[item.selectedIndex].value && s[i] != item && document.getElementById(s[i].name + "_").value != "0") {
			s[i].selectedIndex = document.getElementById(item.name + "_").value - 1;
			var tmp = document.getElementById(item.name + "_").value;
			document.getElementById(item.name + "_").value = document.getElementById(s[i].name + "_").value;
			document.getElementById(s[i].name + "_").value = tmp;
		}
	}
}

function refreshMinutes(item, minuteName) {
	var minValues = new Array(
		"00",
		"15",
		"30",
		"45"
	);
	var minItem = document.getElementsByName(minuteName)[0];
	if (item.options[item.selectedIndex].value == "24") {
		for (var i = 1; i < minItem.options.length; minItem.remove(1));
	}
	else {
		for (var i = 0; i < minItem.options.length; minItem.remove(0));
		minValues.each(function(current) {
			opt = document.createElement("option");
			opt.value = current;
			opt.text = current;
			try {
				minItem.add(opt);
			}
			catch (e) {
				minItem.add(opt, null);
			}
		});
	}
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function gup( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
//  var tmpURL = window.location.href;
  var tmpURL = $("contentframe").src;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function showDynamicModules(moduleList) {
	hideDynamicModules();
	for (var i = 0; i < moduleList.length; i++) {
		$("menu_" + moduleList[i]).style.display = "inline";
	}
}

function hideDynamicModules() {
	for (var i = 0; i < dynamicModules.length; i++) {
		$("menu_" + dynamicModules[i]).style.display = "none";
	}
}

function existingKey(obj, value) {
	for (var ndx in obj) {
		if (ndx == value) { return true; }
	}
	return false;
}

function createUrl(obj) {
	var result = "?";
	var count = 0;
	for (var ndx in obj) {
		result += (count == 0 ? "" : "&") + ndx + "=" + obj[ndx];
		count++;
	}
	return result;
}

function correctContentUrl() {
	var mainparams = getUrlParams();
	var contentframeparams = getUrlParams($("contentframe").src);
	for (var i in mainparams) {
		if (existingKey(contentframeparams, i)) {
			contentframeparams[i] = mainparams[i];
		}
	}
	$("contentframe").src = createUrl(contentframeparams);
}


function displayHintMessage(txt) {
	document.getElementById("matchDetails").addClass("hintMessage_highlite");
	document.getElementById("matchDetails").innerHTML="<div class='content'>" + txt + "</div>";
}

function hideHintMessage() {
	document.getElementById("matchDetails").removeClass("hintMessage_highlite");
	document.getElementById("matchDetails").innerHTML = "&nbsp;";
}

function submitRegisterForm(form) {
	if (document.getElementById("pass").value == document.getElementById("passconfirm").value) {
		document.getElementById(form).submit();
	}
	else {
		alert("A beg�pelt �s meger�s�tett jelsz� nem egyezik meg!");
	}
}
var openedmenucontent = "";
var openedmenuid = 0;
function opensubmenu(menuid, menulist, title, contentphp, yearparam) {
	if (openedmenucontent.length != 0) {
		document.getElementById(openedmenuid).innerHTML = openedmenucontent;
		document.getElementById(openedmenuid).style.height = "15px";
	}
	openedmenucontent = document.getElementById(menuid).innerHTML;
	openedmenuid = menuid;
	document.getElementById(menuid).style.height = "50px";
	document.getElementById(menuid).innerHTML = "<div class='menulink_active' width=100%>" + title + "</div><br><table width=100%>";
	for (i in menulist) {
		document.getElementById(menuid).innerHTML += "<tr><td class='menulink' width=100% style='text-align:left'><a href='" + contentphp + "?teamid=" + i + (yearparam.length > 0 ? "&year=" + yearparam : "") + "' class='menulink2'>" + menulist[i] + "</a><br></td></tr>";
	}
	document.getElementById(menuid).innerHTML += "</table>";
}

function opensubmenu2(menuid, menulist, title, contentphp) {
	if (openedmenucontent.length != 0) {
		document.getElementById(openedmenuid).innerHTML = openedmenucontent;
		document.getElementById(openedmenuid).style.height = "15px";
	}
	openedmenucontent = document.getElementById(menuid).innerHTML;
	openedmenuid = menuid;
	document.getElementById(menuid).style.height = "50px";
	document.getElementById(menuid).innerHTML = "<div class='menulink_active' width=100%>" + title + "</div><br><table width=100%>";
	for (i in menulist) {
		document.getElementById(menuid).innerHTML += "<tr><td class='menulink' width=100% style='text-align:left'><a href='" + contentphp + "&teamid=" + i + "' class='menulink2'>" + menulist[i] + "</a><br></td></tr>";
	}
	document.getElementById(menuid).innerHTML += "</table>";
}

var gh;
var ghActive = false;
function showGaleryHint(x, y) {
	if (!ghActive) {
		try
		{
			gh = document.createElement("<div class='content' style='position:absolute; top:" + (y+document.body.scrollTop+5) + "px; left:" + (x+5) + "px; background-color:#ffffcc'>");
		}
		catch (e)
		{
			gh = document.createElement("div");
			gh.setAttribute("style", "position:absolute; top:" + (y+document.body.scrollTop+5) + "px; left:" + (x+5) + "px; background-color:#ffffcc");
			gh.setAttribute("class", "content");
		}
		document.all.bodyid.appendChild(gh);
		gh.innerHTML = "Kattintson a k�pre a gal�ria megtekint�s�hez!";
		ghActive = true;
	}
}

function hideGaleryHint(x, y) {
	if (ghActive)
	{
		document.all.bodyid.removeChild(gh);
		ghActive = false;
	}
}

function displayGalery(id) {
	window.location='viewgaleria.php?galeryid=' + id;
}

function highlightHeadermenu(item) {
	item.style.backgroundImage="url('images/button_highlight.png')";
}

function unhighlightHeadermenu(item) {
	item.style.backgroundImage="url('images/button_normal.png')";
}

function highlightSidemenu(item) {
	item.style.backgroundImage="url('images/sidemenu_highlight.png')";
	if (item.id != "admin")
	{
		item.style.fontWeight="bold";
	}
}

function unhighlightSidemenu(item) {
	item.style.backgroundImage="none";
	if (item.id != "admin")
	{
		item.style.fontWeight = "normal";
	}
}

function highlightSidemenuRight(item) {
	item.style.backgroundImage="url('images/rightsidemenu_highlight.png')";
	if (item.id != "admin")
	{
		//item.style.fontWeight="bold";
	}
}

function unhighlightSidemenuRight(item) {
	item.style.backgroundImage="none";
	if (item.id != "admin")
	{
		//item.style.fontWeight = "normal";
	}
}

var prevStyle = "";
function highlightCell(item) {
	prevStyle = item.style.className;
	item.className = "highlighted";
}

function unhighlightCell(item) {
	item.className = prevStyle;
}

function highlightcsapatmenuitem(item) {
	item.className = "csapatmenuitems csapatmenuitems_hover";
}

function unhighlightcsapatmenuitem(item) {
	item.className = "csapatmenuitems";
}

function highlightMeccs(item) {
	item.addClass("tablemeccs_hover");
}

function unhighlightMeccs(item) {
	item.removeClass("tablemeccs_hover");
}

function addPointer(item) {
	item.addClass("pointer");
}

function removePointer(item) {
	item.removeClass("pointer");
}

function changeCsapat(id) {
	//element.addClass("");
	var PHPFilename = getPHPFilename(window.location.pathname);
	var module = gup("module");
	var source = $("contentframe").src;
	window.location = PHPFilename + "?csapat=" + id;
}

function viewBeszamolo(id, parent) {
	//alert($(id).id+'-'+$(parent).id);
	x = $(parent).getElements("tr");
	for (var i=0; i<x.length; i++) {
		if (left(x[i].id, 10) == "beszamolo_" && x[i].id!=id) {
			x[i].removeClass("beszamoloshow");
			x[i].addClass("beszamolohide");
		}
	}
	if ($(id).hasClass("beszamoloshow")) {
		$(id).removeClass("beszamoloshow");
		$(id).addClass("beszamolohide");
	} else {
		$(id).removeClass("beszamolohide");
		$(id).addClass("beszamoloshow");
	}
}
 
function changeLanguage(ls) {
	var module = gup("module");
	var lang = gup("lang");
	var count = 0;
	var source = $("contentframe").src;
	if (lang!=ls) {
		window.location = 'kupa.php?lang=' + ls + '&chlang=' + ls;
	}
}
 
