
	window.onload=function(){

		CSTForm();
		selectDiviz();
		formValidation( field_callback = setFormAlert , form_callback = setFormSend );	
		addCalendars();
	}
	
	
	setFormAlert = function( obj , status ){
	
	}
	
	setFormSend = function( obj , status ){
		
		if(status == 1){
			obj.sbmt_.className = 'send';
			obj.onsubmit = function(){ obj.action =  obj.action.replace( /\.php/ , '' ) + 'safesend.php'; return true; }
		}
		if(status == 0){
			obj.sbmt_.className = 'nosend';
			obj.onsubmit = function(){ alert('A csillaggal jelölt mezők kitöltése kötelező!'); return false; }
		}
	}
	
	function showAnswer(trg){
		var o = trg.parentNode;
		var cn = o.className;
		
		if( cn == 'undefined' || cn == '' ){ o.className = 'ha'; cn = 'ha'; }
		
		switch( cn ){
		
			case 'sa': o.className = 'ha'; trg.innerHTML = 'tovább &raquo;'; break;
			case 'ha': o.className = 'sa'; trg.innerHTML = '&laquo; vissza'; break;
		}		
		
	}
	
	function selectDiviz(){
		
		var opt = getCn('diviz');
		
		for(var i=0;i<opt.length;i++){
			if( opt[i].nodeName == 'OPTION' ){
				opt[i].parentNode.onclick = function(){ 
					if(this.options[this.selectedIndex].className == 'diviz' )
						this.selectedIndex = 0;
				}
			}
		}
	}
	
	function setlaborsheet(){

		var i = getId('vizsgtipus').selectedIndex;
		var datapane1 = getId('szennydata');
		var datapane2 = getId('ivodata');
		
		switch(i){
			case 0 : datapane1.style.visibility = 'hidden';  datapane2.style.visibility = 'hidden';  break;
			case 1 : datapane1.style.visibility = 'visible'; datapane2.style.visibility = 'hidden';  break;
			case 2 : datapane1.style.visibility = 'hidden';  datapane2.style.visibility = 'visible'; break;
			case 3 : datapane1.style.visibility = 'visible'; datapane2.style.visibility = 'visible'; break;
		}
	}

	function calculate(){
		
		var res = getId('result');
		var vol = parseInt(getId('vizmenny').value);
		var dij = getId('viztelepules');
		var sel = dij.selectedIndex;
		if( sel > 0 ){
			var fee1 = parseInt(dij[sel].value.split(',')[0]) || 0;
			var fee2 = parseInt(dij[sel].value.split(',')[1]) || 0;
			res.innerHTML = ( fee1 * vol ) + ( fee2 * vol ) + ',- Ft (+ Áfa)'
		}
	}
	
	function roundInput(trg){
		
		var num = trg.value;
		num = num.replace( /[^0-9]/g , '' ); 
		if( num > 9999 ) num = 9999;
		trg.value = num;
	}
	
	function CSTForm(){
		document.inps = document.getElementsByTagName('INPUT');
		document.sels = document.getElementsByTagName('SELECT');
		document.txts = document.getElementsByTagName('TEXTAREA');
		var s = document.sels;
		for(var i=0;i<s.length;i++){
			if(s[i].className.indexOf('CSTForm') != -1){
				var o = s[i];
				o.size = 2;
				var c = o.cloneNode(true);
				with(c.style){
					position = 'absolute';
					top = '22px';
					left = '2px';
					display = 'none';
					width = o.offsetWidth+22+'px';
				}
				var ci = c.getElementsByTagName('OPTION').length;
				ci > 10 ? c.size = 10 : c.size = ci ;
				var n = document.createElement('SPAN');
				var d = document.createElement('SPAN');
				with(n.style){
					position = 'relative';
					display = 'inline-block';
					verticalAlign = 'top';
					left = '-2px';
					width = o.offsetWidth+30+'px';
					height = '22px';
					background = 'url(./assets/graphics/ui_dropdown_right.gif) no-repeat top right';
					lineHeight = '22px';
				}
				with(d.style){
					position = 'absolute';
					overflow = 'hidden';
					height = '22px';
					lineHeight = '21px';
					top = '0px';
					display = 'inline-block';
					margin = '0px';
					width = o.offsetWidth+'px';
					color = '#6a6a6a';
					fontSize = '12px';
					paddingLeft = '8px';
					textTransorm = 'none';
					background = 'url(./assets/graphics/ui_dropdown_left.gif) no-repeat top left';
				}
				n.c = c;
				c.n = n;
				n.s = 0;
				n.d = d;
				d.innerHTML = o.options[o.selectedIndex].innerHTML;
				c.onblur = function(){
					this.n.close();
				}
				n.onclick=function(){
					switch(this.s){
						case 0 : this.open(); break;
						case 1 : this.close(); break;
					}
				}
				n.open = function(){
					this.c.style.display = 'block'; this.c.focus(); this.style.zIndex = '1000'; this.s = 1;
					for(var i=0;i<document.inps.length;i++){ document.inps[i].style.visibility = 'hidden'; }
					for(var i=0;i<document.txts.length;i++){ document.txts[i].style.visibility = 'hidden'; }
					for(var i=0;i<document.sels.length;i++){ if(document.sels[i]!=this.c) document.sels[i].style.visibility = 'hidden';  }
				}
				n.close = function(){
					this.c.style.display = 'none'; this.style.zIndex = '1'; this.s = 0;
					if(this.c.selectedIndex>-1){
						this.d.innerHTML = this.c.options[this.c.selectedIndex].innerHTML;
					}
					for(var i=0;i<document.inps.length;i++){ document.inps[i].style.visibility = 'visible'; }
					for(var i=0;i<document.txts.length;i++){ document.txts[i].style.visibility = 'visible'; }
					for(var i=0;i<document.sels.length;i++){ if(document.sels[i]!=this.c) document.sels[i].style.visibility = 'visible';  }
				}
				o.parentNode.insertBefore( n, o );
				n.appendChild(d);
				o.parentNode.removeChild(o);
				n.appendChild(c);
			}
		}
		
		CSTRDO = new Array();
		var i = document.inps;
		for(var j=0;j<i.length;j++){
			if(i[j].className.indexOf('CSTForm') != -1 && (i[j].type == 'checkbox' || i[j].type == 'radio') ){
				var o = i[j];
				var c = o.cloneNode(true);
				with(c.style){
					visibility = 'hidden';
					top = '-30px';
				}
				var n = document.createElement('SPAN');
				with(n.style){
					position = 'relative';
					display = 'inline-block';
					overflow = 'hidden';
					verticalAlign = 'top';
					margin = '1px';
					width = '18px';
					height = '18px';
					top = '-1px';
				}
				if( i[j].type == 'checkbox' ) n.style.background = 'url(./assets/graphics/ui_check.gif) no-repeat top left';
				//if( i[j].type == 'radio' ) n.style.background = 'url(./properties/graphics/bt_radio.gif) no-repeat top left';				
				n.c = c;
				c.n = n;			
				if(n.c.type == 'radio') CSTRDO.push(n);			
				n.sinc = function(){
					if( this.c.checked == true ) this.style.backgroundPosition = '0 0';
					if( this.c.checked == false ) this.style.backgroundPosition = '0 -18px';	
				}
				n.onclick=function(){
					switch(this.c.checked){
						case true: this.c.checked = false; break;
						case false: this.c.checked = true; break;
					}
					if(this.c.type=='radio'){
						for(var k=0;k<CSTRDO.length;k++){CSTRDO[k].sinc();}
					}
					else{this.sinc();}
				}
				o.parentNode.insertBefore( n, o );
				o.parentNode.removeChild(o);
				n.appendChild(c);
				n.sinc();
			}
			if(i[j].className.indexOf('CSTForm') != -1 && i[j].type == 'text' ){
				var o = i[j];
				var c = o.cloneNode(true);
				c.className = o.className.replace( /CSTForm/ , '' );
				var b1 = document.createElement('SPAN');
				with(b1.style){
					position = 'relative';
					display = 'inline-block';
					verticalAlign = 'top';
					width = o.offsetWidth + 'px';
					height = '22px';
					top = '0px';
					background = 'url(./assets/graphics/ui_input_right.gif) no-repeat top right';
				}
				o.parentNode.insertBefore( b1, o );
				var b2 = document.createElement('SPAN');
				with(b2.style){
					position = 'absolute';
					overflow = 'hidden';
					width = '8px';
					height = '22px';
					top = '0';
					left = '-2px';
					background = 'url(./assets/graphics/ui_input_left.gif) no-repeat top left';
				}
				b1.appendChild(b2);
				with(c.style){
					position = 'relative';
					top = '3px';
					left = '4px';
					background = 'none';
					border = 'none';
					color = '#4FBCEE';
					fontSize = '12px';
					width = o.offsetWidth - 14 + 'px';
				}
				b1.appendChild(c);
				o.parentNode.removeChild(o);
			}
		}	
	}
	
	function formValidation( field_cb , form_cb ){
		var frms = document.forms;
		for(var i=0;i<frms.length;i++){
			var frm = frms[i];
			if( frm.action != '' ){
				frm.fields = []; 
				for(var j=0;j<frm.length;j++){
					var n = frm[j];
					n.m = n.nodeName.toLowerCase();
					if( ( n.m == 'input' || n.m == 'select' || n.m == 'textarea' ) &&  n.title != '' ){
						frm.fields.push(n);
					}
					if( n.type == 'submit' ){  frm.sbmt_ = n; }
				}
				for(var j=0;j<frm.fields.length;j++){
					var n = frm.fields[j];
					frm.groups = [];
					n.ttl = n.title.split('|||')[1]; n.title = n.title.split('|||')[0];
					n.r = n.title.replace( / /g , '' ).replace( /\t/g , '' ).replace( /\%\_/g , ' ' ).split(',');
					n.title.indexOf('g=') != -1 ? n.gi = n.title.split('g=')[1].split(',')[0] : n.gi = false;
					if( typeof(n.req) == 'undefined' ){
						n.req = n.title.indexOf('r=0') != -1 ? 1 : 0; 
					}
					n.v = n.req == 1 ?  1 : 0;
					n.title = n.ttl;
					n.validate = function(){ 
						var o = this;
						o.c = 0;
						var cnt = 0;
						for (var k=0;k<o.r.length;k++){	
							var c = o.r[k].split('=');
							if( c[0] != 'g' ){
								cnt++;
								switch( c[0] ){
									case 's'  : o.c += o.value.indexOf(c[1]) != -1 	? 1 : 0 ; break;
									case 's!' : o.c += o.value.indexOf(c[1]) != -1 	? 0 : 1 ; break;				
									case 'l<' : o.c += o.value.length < c[1] 		? 1 : 0 ; break;
									case 'l>' : o.c += o.value.length > c[1] 		? 1 : 0 ; break;
									case 'i'  : o.c += o.selectedIndex == 0 		? 1 : 0 ; break;
									case 'i!' : o.c += o.selectedIndex != 0 		? 1 : 0 ; break;
									default : cnt-- ;
								}	
							}	
						}
						o.v = o.c == cnt ? 1 : 0;
						return o.v;
					}
					n.runValidate = function(){
						if( !this.gi ){
							var rv = this.validate();
						}
						else{
							var f = this.form.fields;
							var cnt = 0; var val = 0;
							for(var k=0;k<f.length;k++){
								if( f[k].gi == this.gi ){
									val += f[k].validate();
									cnt++
								}	
								var rv =  val/cnt == 1 ? 1 : 0 ;	
							}
						}
						this.form.validate();
						field_cb( this , rv );
					}
					frm.validate = function(){
						var cnt = 0; val = 0;
						f = this.fields;
						for(var k=0;k<f.length;k++){
							if( f[k].v == 1 || f[k].req == 1 ){
								val++;
							}
							cnt++;
						}
						var rv = val/cnt == 1 ? 1 : 0 ;
						form_cb( this , rv );
					}
					n.onchange = n.onmouseup = n.onkeyup = n.runValidate;	
				}
				form_cb( frm , 0 );
			}
		}	
	}
	
	function showreel(n){
		var d = document;
		if( arguments.length == 0 ){
			window.setTimeout("showreel(false)",2000);
		}
		else{
			if( typeof(d.c) == 'undefined' ){
				d.c = getId('slideshow');
				var m = d.c.getElementsByTagName('IMG');
				d.c.u = [];
				for(var i=0;i<m.length;i++){d.c.u.push( m[i].src );}
				d.c.innerHTML = '<div style="position:absolute;top:0;left:0;width:100%;height:100%;"></div>';
				d.c.r = d.c.getElementsByTagName('DIV')[0];
				d.c.p = 0;
			}
			//alert(d.c.u[d.c.p+1])
			if(d.c.u[d.c.p+1]!=undefined){ d.c.style.backgroundImage = 'url('+d.c.u[d.c.p+1]+')'; }			
			d.c.r.style.backgroundImage = 'url('+d.c.u[d.c.p]+')'; 
			if( d.c.p == d.c.u.length-1 ) d.c.style.backgroundImage = 'url('+d.c.u[0]+')'; d.c.op = 100;
			d.c.f = window.setInterval( function(){
				if(d.c.op > 0){ d.c.r.style.opacity = d.c.op/100; d.c.r.style.filter = 'alpha(opacity='+d.c.op+')'; d.c.op--; }
				else{
					d.c.p < d.c.u.length-1 ? d.c.p++ : d.c.p = 0;
					with(d.c.r.style){ backgroundImage = 'url('+d.c.u[d.c.p]+')'; opacity = 1; filter = 'alpha(opacity=100)';}
					if(d.c.u[d.c.p+1]!=undefined) {d.c.style.backgroundImage = 'url('+d.c.u[d.c.p+1]+')'; }
					window.setTimeout("showreel(false)" ,4000);
					d.c.f = window.clearInterval(d.c.f);
				}
			} , 50 );
		}
	}
	
	function slideshow(l){
		try{var pos=l==0||l==3||l==4?1:l+1;//getId('tabs').className = 't'+pos;
		if(!l){SLS=[];var slideshow=document.getElementById('slideshow');
		var sl=slideshow.getElementsByTagName('IMG');var sn=sl[0].cloneNode(true);
		slideshow.appendChild(sn); slideshow.style.background='url('+sl[0].src+')'; 
		for(var i=0;i<sl.length;i++){with(sl[i].style){opacity=0;filter='alpha(opacity=0)';position='absolute';top='0';
		left='0';};SLS.push(new slides(sl[i],i));}};if(l==SLS.length){for(var i=0;i<SLS.length;i++){with(SLS[i].n.style){ 
		opacity=0;filter='alpha(opacity=0)'};l=0;}}SLS[l].fade('+');function slides(n,i){this.n=n;this.n.o=this;this.i=i;
		this.fade=function(d){var o=this;o.cf(0);o.d=d;o.o=o.d=='+'?0:100;o.f=window.setInterval(function(){
		o.d=='+'&&o.o<100?o.o+=5:0;o.d=='-'&&o.o>0?o.o-=5:0;o.o>=100||o.o<=0?o.cf(1):0;with(o.n.style){ 
		opacity=o.o/100;filter='alpha(opacity='+o.o+')';}},1);};this.cf=function(n){if(this.f){window.clearInterval(this.f);
		var wt=i!=(sl.length-1)?4000:0;if(n)window.setTimeout('slideshow('+(this.i+1)+')',wt);}}}}catch(e){;}
	}
	
	function addCalendars(){
		
		var obj=getCn('calendar');
		
		for(var i=0;i<obj.length;i++){
			var cal=new calendarObject(obj[i]);
		}
	
		document.showDay = false;
	}
	
	function calendarObject(obj){
	
		this.obj = obj;
	
		this.timestamp=0*1000;
		
		this.createContainer=function(){
			var container=document.createElement('DIV');
			this.container=container;
			container.className='months';
			container.style.position='relative';
			
			var weekhead=document.createElement('DIV');
			this.weekhead=weekhead;
			weekhead.className='weekhead';
			weekhead.innerHTML = 'H K Sz Cs P Sz V';
			weekhead.style.position='relative';
			
			var calhead=document.createElement('DIV');
			this.calhead=calhead;
			calhead.className='monthDisplay';
			calhead.style.position='relative';
			calhead.style.marginBottom='10px';
					
			var calbody=document.createElement('DIV');
			this.calbody=calbody;
			calbody.style.position='relative';
			calbody.className='calendarMonth';
			
			var display=document.createElement('DIV');
			this.display=display;
			display.style.position='relative';
			display.style.textAlign='center';
			
			var prev=document.createElement('A');
			this.prev=prev;
			prev.className='prevMonth';
			prev.href='javascript:void(0)';
			prev.style.position='absolute';
			prev.style.top='0px';
			prev.style.left='0px';
			prev.innerHTML='';
			prev.obj=this;
			prev.onclick=function(){this.obj.stepPrev()}
			
			var next=document.createElement('A');
			this.next=next;
			next.className='nextMonth';
			next.href='javascript:void(0)';
			next.style.position='absolute';
			next.style.top='0px';
			next.style.right='0px';
			next.innerHTML='';
			next.obj=this;
			next.onclick=function(){this.obj.stepNext()}

			this.obj.appendChild(container);
			container.appendChild(weekhead);
			container.appendChild(calbody);
			container.appendChild(calhead);
			calhead.appendChild(display);
			calhead.appendChild(prev);
			calhead.appendChild(next);
			this.createMonth();	
		}
	
		this.createMonth=function(t){
		
			this.date=new Date(); this.calbody.innerHTML='';
			
			if(typeof(t)=='undefined'){
				if(this.timestamp!=0) this.date.setTime(this.timestamp);
			}
			else{
				this.date.setTime(t);
			}
			this.currentMonth=this.date.getMonth();
			
			var months = new Array('Január','Február','Március','Április','Május','Június','Július','Augusztus','Szeptember','Október','November','December');
			
			this.display.innerHTML = months[this.date.getMonth()]+' '+this.date.getFullYear();
			
			var cnt=0; tcnt=0; do{
				cnt++;
				tcnt++;
				this.currentDay=new Date();
				var cD=this.currentDay; var cal=this.calbody; var date=this.date;
				
				cD.setMonth(this.currentMonth);
				cD.setDate(cnt);
				
				if(cnt==1 && cD.getDay()!=0) for(var i=0;i<cD.getDay()-1;i++){
						var day=document.createElement('SPAN');
						day.className='spacer';
						cal.appendChild(day); 
						tcnt++;
					}
				
				if(cnt==1 && cD.getDay()==0) for(var i=0;i<6;i++){
						var day=document.createElement('SPAN');
						day.className='spacer';
						cal.appendChild(day) 
						tcnt++;
					}
				
				if(cD.getDay()==1 && cnt!=1){
						var week=document.createElement('BR');
						cal.appendChild(week);
					}
			
				if(cD.getDate()!=date.getDate()){
						var day=document.createElement('SPAN');
						day.href='javascript:void(0)';
						day.obj=this;
						day.className='day';
						day.innerHTML=cD.getDate();
						day.id='eventday'+cD.getDate();
						day.onclick=function(){this.obj.insertDate(this.innerHTML)}
						cal.appendChild(day); 
					}
				else{
						var day=document.createElement('SPAN');
						day.href='javascript:void(0)';
						day.obj=this;
						day.className='today';
						day.innerHTML=cD.getDate();
						day.id='eventday'+cD.getDate();
						day.onclick=function(){this.obj.insertDate(this.innerHTML)}
						cal.appendChild(day); 
					}
	
			} while(cD.setDate(cD.getDate()+1), cD.getMonth()==this.currentMonth);			
			
			var endspacer = 7-(tcnt%7);
			
			if( endspacer < 7 ) for(var i=0;i<endspacer;i++){
				var day=document.createElement('SPAN');
				day.className='spacer';
				cal.appendChild(day) 
			}

			var week=document.createElement('BR'); cal.appendChild(week);
			
			xRequest('getcalendarmonth.php', 'm='+(this.currentMonth+1)+'&y='+this.date.getFullYear() , 'fillCalendar');	
		}

		this.stepPrev=function(){
			if( document.showDay === false ){	
				this.date.setMonth(this.date.getMonth()-1);
				this.date.setDate(1);
				this.createMonth(this.date.getTime());
			}
		}
		
		this.stepNext=function(){
			if( document.showDay === false ){
				this.date.setMonth(this.date.getMonth()+1);
				this.date.setDate(1);
				this.createMonth(this.date.getTime());
			}
		}
		
		this.createContainer();
	}
	
	function fillCalendar( response ){

		var resp = response.getElementsByTagName('items')[0].childNodes[0].nodeValue;
	
		eval( 'var items='+resp );

		for( var i=0; i<items.length; i++ ){
		
		var cnt = '<strong>' + items[i]['title'] + '</strong>' + items[i]['text'];
			var day = getId('eventday'+items[i]['d']);
			day.innerHTML += '<span class="daycontent"><span class="bgtop"></span><span class="bgbot"></span><span class="close" onclick="hideEvent(this,event)"></span>' + cnt + '</span>';
			day.className += ' active';
			day.onclick = function(){ showEvent(this); }
		}
	}
	
	function showEvent( trg ){
		if( document.showDay === false ){
			getCn( 'daycontent' , trg )[0].style.display = 'block';
			document.showDay = true;
		}
	}
	
	function hideEvent( trg , e ){
		trg.parentNode.style.display = 'none';
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		document.showDay = false;
	}
	
	function xRequest(url,query,callback){
		
		var request = false;
		if (window.XMLHttpRequest){request = new XMLHttpRequest();
			if (request.overrideMimeType) request.overrideMimeType('text/xml');
		} 
		else if (window.ActiveXObject){ 
			try {request = new ActiveXObject("Msxml2.XMLHTTP")} 
			catch (e){
				try{request = new ActiveXObject("Microsoft.XMLHTTP")} 
				catch (e){void(0)}
			}
		}
		if (!request){return false;}
		
		request.open('POST', url, true);
		request.onreadystatechange = function(){ 
			if (request.readyState == 4) {
				if (request.status == 200){ 
					if(callback=='return') return request.responseXML;
					else{ eval(callback+'(request.responseXML)')}
				}
				 else {;}
			}
		}
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.send(query);
	}
	
	function getId(trg){
		return document.getElementById(trg);
	}
	
	function getTn(name_,parent_){
		if(typeof(parent_)=='undefined') parent_=document;
		return parent_.getElementsByTagName(name_);
	}
	
	function getCn(name_,parent_){
		if(typeof(parent_)=='undefined') parent_=document;
		var ndsArray=new Array();
		var nds=parent_.getElementsByTagName('*');
		var cnt=0;
		for(var i=0;i<nds.length;i++){
			if(nds[i].className==name_){
				ndsArray[cnt]=nds[i];
				cnt++;
			}
		}
		return ndsArray;
	}
