// JavaScript Document
var AKV = false;
var PH = false;

function get(id) {
	return document.getElementById(id);
}

function getAjax()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				//A böngésző nem támogatja az ajaxot
				return false;
			}
		}
	}
	return xmlHttp;
}

function ajax(url,querystr,complete) {
	var xmlHttp = new getAjax();
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4)
		{
			complete(xmlHttp.responseText);
		}
	}
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=iso-8859-2");
	xmlHttp.setRequestHeader("Request-type","XMLHttpRequest");		
	if (xmlHttp.overrideMimeType)	xmlHttp.setRequestHeader("Connection","close");
	xmlHttp.send(querystr);
}

function iecheck()
{
	var pos = navigator.userAgent.indexOf("MSIE ");
	if (pos == -1) return false;
	var version = navigator.userAgent.substring(pos + 5);
	return (((version.indexOf("5.5") == 0) || (version.indexOf("6") == 0)  || (version.indexOf("7") == 0)));
}

function ie_png_check()
{
	var pos = navigator.userAgent.indexOf("MSIE ");
	if (pos == -1) return false;
	var version = navigator.userAgent.substring(pos + 5);
	return ((version.indexOf("5.5") == 0) || (version.indexOf("6") == 0));
}

// PNG ALPHA CHANNEL IMG-RE //
function iepng_alphachannel(imgid,timg,size)
{
	if(ie_png_check())
	{
		d = document.getElementById(imgid);
		origsrc = d.src;
		d.src = timg;
		d.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + origsrc + "',sizingMethod='image')";
		d.width = size[0];
		d.height = size[1];
	}
}

// PNG ALPHA CHANNEL DIV BACKGROUND //
function iepng_alphachannel_bg(divid,bgimg)
{
	d = document.getElementById(divid);
	if(iecheck())
	{
		d.style.background = 'none';
		d.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bgimg + "',sizingMethod='scale')";
	}
}

function isempty(str)
{
	r = new RegExp("^[ ]*$");
	return (r.test(str) || str.length == '');
}

function insertFlash(swf,size,cel,fv)
{
	var str = "";
	var fvstr = "";
	for(var i = 0; i<fv.length; i++)
	{
		fvstr += fv[i][0]+"="+fv[i][1]+"&";
	}
	fvstr = fvstr.substr(0,fvstr.length-1);
	if(iecheck())
	{
		str +='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0" width="'+size[0]+'" height="'+size[1]+'">';
		str +='	<param name="movie" value="'+swf+'" />';
		str +='	<param name="quality" value="high" />';
		str +=' <param name="menu" value="false" />';
		if(fvstr) str += '<param name="flashvars" value="'+fvstr+'" />';
		str +='</object>';

	}
	else
	{
		
		str +='<object type="application/x-shockwave-flash" data="'+swf+'" width="'+size[0]+'" height="'+size[1]+'">';
		str +='<param pluginurl="http://www.macromedia.com/go/getflashplayer">';
		str +='	<param name="menu" value="false" />';
		str +='	<param name="quality" value="high" />';
		if(fvstr) str += '<param name="flashvars" value="'+fvstr+'" />';
		str +='</object>';
	}
	document.getElementById(cel).innerHTML = str;
}

function appendInput(tmb, frm)
{
	for(var i=0;i<tmb.length;i++)
	{
		var n   = document.createElement("INPUT");
		n.type  = "hidden";
		n.name  = tmb[i][0];
		n.value = tmb[i][1];
		frm.appendChild(n);
	}
}

function checkValidEmail(mailstr) {
	r = new RegExp("^[0-9a-zA-Z._-]{2,}@([0-9a-zA-Z_-]{2,}\\.){1,7}[a-zA-Z]{2,3}$");
	return r.test(mailstr);
}

function naptar2_step(ev,ho)
{
	var ajaxLoad = new ajax(
		"/ekozig/includes/naptar_akv.php",
		"&year="+ev+"&month="+ho,
		function(res) {
			document.getElementById("mainnaptar").innerHTML = res;
		}
	);
}

var PRECLASS;
function colorize(obj, cls)
{
	PRECLASS = obj.className;
	obj.className = obj.className+" "+cls;
}

function colorizeOff(obj)
{
	obj.className=PRECLASS;
}

function naptar_step(ev,ho)
{
	var ajaxLoad = new ajax(
		"/ekozig/includes/naptar.php",
		"&year="+ev+"&month="+ho+"&agid="+AGID,
		function(res) {
			document.getElementById("mainnaptar").innerHTML = res;
		}
	);
}

function ph_naptar_step(ev,ho)
{
	var ajaxLoad = new ajax(
		"/ekozig/includes/ph_naptar.php",
		"&year="+ev+"&month="+ho+"&agid="+AGID,
		function(res) {
			document.getElementById("mainnaptar").innerHTML = res;
		}
	);
}

function setDay(datum, realDatum)
{
	zoompic(new Array(500,500),"Időpont foglalás ("+realDatum+")", datum);
	window.onresize = function() { reSetPage(datum, realDatum); };
}

function reSetPage(datum, realDatum)
{
	setDay(datum, realDate);
}

function zoompic(meret, nev, datum)
{
	setAllSelect('hidden');
	var c;
	var size = getPageSize();
	var scrl = getPageScroll();

	var o = document.getElementById("overlay");
	o.style.position = "absolute";
	o.style.top  = '0px';
	o.style.left = '0px';
	o.style.width = "100%";
	o.style.zIndex = '99';
	o.style.height = size[1]+"px";
	o.style.backgroundColor = "#000000";	
	o.style.display = "block";
	if(iecheck())
	{
		o.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=70)";
	}
	else
	{
		o.style.opacity = 0.7;
	}
	
	var obox = document.getElementById("overbox");
	obox.style.position = "absolute";
	obox.style.left = "0px";
	obox.style.overflow = "hidden";
	obox.style.width = o.style.width;
	obox.style.height = o.style.height;
	obox.style.zIndex = "101";
	obox.style.display = "block";
	
	var head = 35;
	var brd = 5;
	var clr = "#e6e6e6";
	var p = document.getElementById("overpic");
	var top = (((size[3]-(Number(meret[1])+head))/2)+scrl[1]);
	if(top<0) top = 0;
	p.style.position = "absolute";
	p.style.backgroundColor = clr;
	p.style.top = top+"px";
	p.style.left = ((size[2]-Number(meret[0]))/2)-brd+"px";
	p.style.width = (Number(meret[0])+brd*2)+"px";
	p.style.height = (Number(meret[1])+head)+"px";
	p.style.zIndex = "100";
	
	if(p.childNodes.length)
	{
		// ablak fejlec
		c = document.getElementById("pictitle");
		if(c) p.removeChild(c);

		// flash fejlec hide
		c = document.getElementById("flashfej");
		if(c) document.getElementById("header").removeChild(c);
		
		// belso resz
		c = document.getElementById("otart");
		if(c) p.removeChild(c);
	}
	
	// flash fejlec csere
	var f = new Image();
	f.src = "/ekozig/images/"+SEASON+"/headbg.jpg";
	f.id = "flashfej";
	f.width = 980;
	f.height = 155;
	f.style.position = "absolute";
	document.getElementById("header").appendChild(f);
	
	var t = document.createElement("DIV");
	t.className = "freetimehead";
	t.innerHTML = nev;
	t.id = "pictitle";
//	t.style.fontSize = "19px";
	t.style.height = head+"px";
	t.style.lineHeight = head+"px";
//	t.style.paddingLeft = "6px";
	p.appendChild(t);
	p.style.display = "block";

	var trt = document.createElement("DIV");
	trt.id = "otart";
	trt.style.padding = "5px";
	trt.innerHTML = "Időpontok betöltése folyamatban, kérem várjon...";
	p.appendChild(trt);

	if(AKV) {
		var ajaxLoad = new ajax(
			"/ekozig/includes/freetimes_akv.php",
			"datum="+datum+"&id="+UID,
			function(res) {
				trt.innerHTML = res;
			}
		);
	} else if(PH) {
		var ajaxLoad = new ajax(
			"/ekozig/includes/freetimes_ph.php",
			"datum="+datum+"&id="+UID+"&t="+TYP,
			function(res) {
				trt.innerHTML = res;
			}
		);
	} else {
		var ajaxLoad = new ajax(
			"/ekozig/includes/freetimes.php",
			"datum="+datum+"&id="+UID+"&t="+TYP,
			function(res) {
				trt.innerHTML = res;				
			}
		);
	}
}

function closepic()
{
	setAllSelect('visible');
	var o = document.getElementById("overlay");
	o.style.display = "none";
	
	var p = document.getElementById("overpic");
	p.style.display = "none";
	
	var c = document.getElementById("pictitle");
	p.removeChild(c);
	
	var f = document.getElementById("flashfej");
	document.getElementById("header").removeChild(f);
	
	var obox = document.getElementById("overbox");
	obox.style.display = "none";
	
	window.onresize = null;
	RESERVABLE = false;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
		
		// ie 7 eseten
		var pos = navigator.userAgent.indexOf("MSIE ");
		var version = navigator.userAgent.substring(pos + 5);
		var r = new RegExp("opera","i");
		if(version.indexOf("7") == 0 || r.test(navigator.userAgent))
		{
			xScroll = document.documentElement.scrollWidth;
			yScroll = document.documentElement.scrollHeight;
		}
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function getPageScroll()
{
	var yScroll;
	if (self.pageYOffset)
	{
		yScroll = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	}
	else if (document.body)
	{ // all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function setAllSelect(visible)
{
	// elrejti az ossze selectet, mert ie alatt mindig felul van!!
	var i;
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++)
	{
		selects[i].style.visibility = visible;
	}
	// flash-t is elrejti
	flashes = document.getElementsByTagName("object");
	for(i=0;i!=flashes.length;i++)
	{
		flashes[i].style.display = (visible=="visible")?"":"none";
	}
}

function settime(id, ugyintezo, stm)
{
	var block = document.getElementById("freetimesdiv");
	var as = block.getElementsByTagName("A");
	var i;
	for(i=0;i<as.length;i++)
	{
		var pics = as[i].getElementsByTagName("IMG");
		if(pics[0])	as[i].removeChild(pics[0]);
	}
		
	var sor = document.getElementById(id);
	var setPipa = new Image();
	setPipa.src = "/ekozig/images/"+SEASON+"/foglalom.gif";
	setPipa.style.position = "absolute";
	setPipa.style.marginTop = "-3px";
	setPipa.style.marginLeft = "6px";
	setPipa.width = 26;
	setPipa.height = 19;
	sor.appendChild(setPipa);
	RESERVABLE = true;
	IDOPONTOK[0] = ugyintezo;
	IDOPONTOK[1] = stm;
}

function saveReserve(datum, id, tipus)
{
	var xmlHttp = new getAjax();
	if(RESERVABLE)
	{
		if(confirm("BIZTOS LEFOGLALJA A BEJELÖLT IDŐPONTOT?"))
		{
			var block = document.getElementById("freetimesdiv");
			var as = block.getElementsByTagName("A");
			var i;
			for(i=0;i<as.length;i++)
			{
				var pics = as[i].getElementsByTagName("IMG");
				if(pics[0])
				{
					xmlHttp.open("POST","/ekozig/process/savereverse.php",true);
					xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=iso-8859-2");
					xmlHttp.setRequestHeader("Request-type","XMLHttpRequest");		
					if (xmlHttp.overrideMimeType)	xmlHttp.setRequestHeader("Connection","close");
					querystr = "datum="+datum+"&id="+id+"&t="+tipus+"&ui="+IDOPONTOK[0]+"&ido="+IDOPONTOK[1];
					xmlHttp.send(querystr);
					break;
				}
			}
		}
	}
	else
	{
		alert("Jelöljön be egy időpontot!");
	}
	
	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.responseText)
			{
				if(xmlHttp.responseText == "outoftime")
				{
					alert("Megszakadt a kapcsolat az ügyfélhívó rendszerrel!\nKérjük próbálja később, vagy foglaljon időpontot egy későbbi napra.");
				}
				else
				{
					var frm = document.createElement("FORM");
					frm.action = "/ekozig/sikeres_idopont_foglalas.html";
					frm.method = "POST";
					frm.style.display = "none";
					var n = new Array(new Array("fid",xmlHttp.responseText));
					appendInput(n, frm);
					document.body.appendChild(frm);
					frm.submit();
				}
			}
			else
			{
				closepic();
				alert("Az időpontfoglalás nem sikerült! Kérjük próbálja újra!");
			} // IF RESPONSETEXT VEGE
		} // IF READYSTATE VEGE
	}	//ONREADY VEGE
}

function saveReservePh(datum, id, tipus)
{
	var xmlHttp = new getAjax();
	if(RESERVABLE)
	{
		if(confirm("BIZTOS LEFOGLALJA A BEJELÖLT IDŐPONTOT?"))
		{
			var block = document.getElementById("freetimesdiv");
			var as = block.getElementsByTagName("A");
			var i;
			for(i=0;i<as.length;i++)
			{
				var pics = as[i].getElementsByTagName("IMG");
				if(pics[0])
				{
					xmlHttp.open("POST","/ekozig/process/savereverse_ph.php",true);
					xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=iso-8859-2");
					xmlHttp.setRequestHeader("Request-type","XMLHttpRequest");		
					if (xmlHttp.overrideMimeType)	xmlHttp.setRequestHeader("Connection","close");
					querystr = "datum="+datum+"&id="+id+"&t="+tipus+"&ui="+IDOPONTOK[0]+"&ido="+IDOPONTOK[1];
					xmlHttp.send(querystr);
					break;
				}
			}
		}
	}
	else
	{
		alert("Jelöljön be egy időpontot!");
	}
	
	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.responseText)
			{
				
				var frm = document.createElement("FORM");
				frm.action = "/ekozig/sikeres_idopont_foglalas_ph.html";
				frm.method = "POST";
				frm.style.display = "none";
				var n = new Array(new Array("fid",xmlHttp.responseText));
				appendInput(n, frm);
				document.body.appendChild(frm);
				frm.submit();
			}
			else
			{
				closepic();
				alert("Az időpontfoglalás nem sikerült! Kérjük próbálja újra!");
			} // IF RESPONSETEXT VEGE
		} // IF READYSTATE VEGE
	}	//ONREADY VEGE
}

function belep(url) {
	if(isempty(get('nev').value)) {
		alert("Név megadása kötelező!");
	}
	else if(!checkValidEmail(get('email').value)) {
		alert("Nem megfelelő e-mail cím!");
	}
	else
	{
		hlev = '';
		var ipts = document.getElementById('nlgroups').getElementsByTagName('input');
		for(i=0;i<ipts.length;i++)
		{
			if(ipts[i].checked) hlev += '&hlev[]='+ipts[i].value;
		}
		
		
		var ajaxLoad = new ajax(
			"/ekozig/process/login.php",
			"&nev="+get('nev').value+"&email="+get('email').value+hlev,
			function(res) {
				location = url;
			}
		);
	}
}

function print_this() {
	alert('nyomtat');
}
