var MaxData = 8;
var idData = new Array(MaxData);
var ActId = 0;
var now = 0;
// var myYear =0;
// var myMonth = 0;
function change_controll(id, a){
	//document.adminForm.task.value=pressbutton;
	document.adminForm.action = document.adminForm.action + "?id=" + id + "&id2=5" + a.value + '#rend';
	//alert(document.adminForm.action);
	try {
		document.adminForm.onsubmit();
		}
	catch(e){}
	document.adminForm.submit();
}

function createXmlHttp() {
	var request = null;
	if (window.XMLHttpRequest) {
		 request = new XMLHttpRequest();
	}
	else if (typeof ActiveXObject != undefined) {
		 request =
			   new ActiveXObject('Microsoft.XMLHTTP');
	}
	return request;
}
var ajaxRequest = null;

// function step_date(step){
	// // alert(myYear);
	// // if(myYear == 0)myYear = year;
	// // if(myMonth == 0)myMonth = month;
	// if(step > 0){
		// if(myMonth < 12)myMonth++;
		// else if(myMonth == 12){myMonth = 1;myYear++;}
	// }
	// else{
		// if(myMonth > 1)myMonth--;
		// else if(myMonth == 1){myMonth = 12;myYear--;}
	// }
	// //alert('ok');
	// var whichKey = 16;
	// if(document.adminForm.k_1.checked)whichKey += 1;
	// if(document.adminForm.k_2.checked)whichKey += 2;
	// if(document.adminForm.k_3.checked)whichKey += 4;
	// if(document.adminForm.k_4.checked)whichKey += 8;

	// // if(whichKey > 16){
		// // alert(whichKey);
		// ajaxRequest = createXmlHttp();
		// if (ajaxRequest) {
			 // // alert(myYear + ' - ' + myMonth);
			 // ajaxRequest.open('post', 'naptar.php?ev=' + (myYear) + '&honap=' + (myMonth) + '&k=' + whichKey, true);
			 // ajaxRequest.onreadystatechange = ajaxCustomerInfo;
			 // ajaxRequest.send();
		// }
	// // }
// }

// function ajaxCustomerInfo() {
	// if (ajaxRequest.readyState != 4) {
		 // return;
	// }
	// // alert(ajaxRequest.responseText);
	// var mycalendar = $('mycalendar');
	// mycalendar.innerHTML = ajaxRequest.responseText;
	// // mycalendar.innerHTML = 'teszt';
	// new AjaxHint('tableHintDisplayer', 'divHint');
// }

// Dobozokhoz a timer
function Counter(divID, start, intervall, from, num){
	//this.divID = divID;
	this.mydiv = $(divID);
	this.navdiv = $(divID + 'nav');
	this.mybutton = $(divID + 'p');
	this.count = start;
	this.intervall = intervall;
	this.from = from;
	this.num = num;
	var oThis = this;
	oThis.doTimer();
	Event.observe(divID + 'b', 'click', function(event) { oThis.step(-1); });
	Event.observe(divID + 'p', 'click', function(event) { oThis.go(); });
	//Event.observe(divID + 'f', 'onclick', function(event) { oThis.step(1); });
	Event.observe(divID + 'f', 'click', function(event) { oThis.step(1); });

}	
function AjaxHint(id, divHint) {
		var oThis = this;
		this.divHint = $(divHint);
		
		if(this.divHint){
			Event.observe(id, 'mouseover', function(e) { oThis.getHint(e);});
			Event.observe(id, 'mouseout', function(e) { oThis.hideHint(e);});
		}
		// alert('ok12');
	}
	
AjaxHint.prototype = {
	ajaxRequest: null,
	hintedObject:  null,
	hintTimer: null,
	
	cancelActivity: function () {
		if (this.hintTimer) {
			clearTimeout(this.hintTimer);
			this.hintTimer = null;
		}
		if (this.ajaxRequest) {
			this.ajaxRequest.transport.abort();
			this.ajaxRequest = null;
		}
	},
	
	getHint: function (event) {
  
		element = Event.element(event);
    
		if (element.tagName.toLowerCase() != 'td') {
			return;
		}
		this.hintedObject = element;
		//alert(2);
		//alert(this.hintedObject.id.substring(4));
		if(isNaN(this.hintedObject.id)){//alert('nan');
			//this.hintedObject.style.cursor = 'wait';
     // alert(this.hintedObject.id);
			var oThis = this;
			this.hintTimer = setTimeout(
				function() {oThis.timerGetHint()}, 
				500
			);
		}
		else{
			//new Effect.Fade(this.divHint, {duration:1});
			this.divHint.style.display = 'none';
			this.hintedObject = null;
		}
	},
	
	timerGetHint: function () {
		if (! this.hintedObject) {
			return;
		}
    targyalatt = 0;
		var whichKey = 16;
		if(document.adminForm.k_1.checked)whichKey += 1;
		if(document.adminForm.k_2.checked)whichKey += 2;
		if(document.adminForm.k_3.checked)whichKey += 4;
		if(document.adminForm.k_4.checked)whichKey += 8;
    if(document.adminForm.k_5.checked) targyalatt = 1; else targyalatt = 0;
   
    var day = ''; 
		if(whichKey > 16){
			if(myYear == 0 || myMonth == 0){
				now = new Date();
				myYear = now.getYear();
				myMonth = now.getMonth()+1;
        
			}
      
      idforcut = this.hintedObject.id.split("#");
      
			var oThis = this;    
			this.ajaxRequest = new Ajax.Request(
				'gethint.php',
				{
					method: 'get',
					parameters: '&aktdate='+myYear+'-'+myMonth+'-'+idforcut[0]+'&targyalatt='+targyalatt+'&datid=' + myYear + 'a' + myMonth + 'a' + whichKey + 'a' +
						idforcut[1].substring(4)+'&'+Math.random(),
					onComplete: function(request) 
						{ oThis.ajaxGetHint(request); }
				}
			);
		}
	},
	
	hideHint: function (event) {
		element = Event.element(event);
		if (element.tagName.toLowerCase() != 'a') {
			return;
		}
		this.cancelActivity();
		//this.hintedObject.style.cursor = 'hand';
		this.hintedObject = null;
		this.divHint.style.display = 'none';
		// new Effect.Fade(this.divHint);
		
	},
	
	ajaxGetHint: function (request) {
		//this.hintedObject.style.cursor = 'hand';
		this.divHint.innerHTML = request.responseText;
		if(request.responseText == ""){
			this.hintedObject = null;
			this.divHint.style.display = 'none';
		}
		else{
			var pos = Position.cumulativeOffset(this.hintedObject);
			// alert(this.divHint.style.height);
			// this.divHint.style.left = pos[0] + 'px';
			this.divHint.style.left = (pos[0]-62) +  'px';
			this.divHint.style.top = (pos[1] + this.hintedObject.offsetHeight) + 'px';
			// this.divHint.style.bottom = (pos[1] + this.hintedObject.offsetHeight) + 'px';
			this.divHint.style.display = 'block';
		}
		//new Effect.Appear(this.divHint, {duration:0.2});
		//alert(this.hintedObject.style.cursor);
		//alert(this.hintedObject.style.cursor);
	}	
}

function SetDiary(divDiary, back, forw, param){
	var oThis = this;
	this.param=(!param?'':param);
	this.mycalendar = $(divDiary);
	// Event.observe(back, 'click', function() { oThis.step_date(-1);});
	// Event.observe(forw, 'click', function() { oThis.step_date(1);});
	Event.observe(back + '2', 'click', function() { oThis.step_date(-1);});
	Event.observe(forw + '2', 'click', function() { oThis.step_date(1);});
	// alert(param);
}

SetDiary.prototype = {
	ajaxRequest: null,

	step_date: function(step){
		// alert(this.param);
		if(step > 0){
			if(myMonth < 12)myMonth++;
			else if(myMonth == 12){myMonth = 1;myYear++;}
		}
		else{
			if(myMonth > 1)myMonth--;
			else if(myMonth == 1){myMonth = 12;myYear--;}
		}
		// alert('ok');
		var whichKey = 16;
    targyalatt = 1
		if(!this.param){
			if(document.adminForm.k_1.checked)whichKey += 1;
			if(document.adminForm.k_2.checked)whichKey += 2;
			if(document.adminForm.k_3.checked)whichKey += 4;
			if(document.adminForm.k_4.checked)whichKey += 8;
      if(document.adminForm.k_5.checked) targyalatt = 1; else targyalatt = 0;
   }
		else{ whichKey+=15;whichKey = whichKey + '&param=1';}
		
		var oThis = this;
		this.ajaxRequest = new Ajax.Request(
			'naptar.php',
			{
				method: 'get',
				parameters: 'ev=' + (myYear) + '&honap=' + (myMonth) + '&targyalatt=' + targyalatt + '&k=' + whichKey+'&'+Math.random(),
				onComplete: function(request) 
					{ oThis.ajaxDisplay(request); }
			}
		);
	},

	ajaxDisplay: function(request) {
		// alert(ajaxRequest.responseText);
		this.mycalendar.innerHTML = request.responseText;
		if(!this.param)new AjaxHint('tableHintDisplayer', 'divHint');
		new SetDiary('mycalendar', 'diaryback', 'diaryforw', this.param);
	}
}

function step_date(step, div, param){
	mycalendar = $(div);
	if(step > 0){
		if(myMonth < 12)myMonth++;
		else if(myMonth == 12){myMonth = 1;myYear++;}
	}
	else{
		if(myMonth > 1)myMonth--;
		else if(myMonth == 1){myMonth = 12;myYear--;}
	}
	ajaxRequest = new Ajax.Request(
		'naptar.php',
		{
			method: 'get',
			parameters: 'ev=' + (myYear) + '&honap=' + (myMonth)+'&'+Math.random(),
			onComplete: function(request) 
			{
				// alert(request.responseText);
				mycalendar.innerHTML = request.responseText;
			}
		}
	);
};
function getElementsByClassName( myName )
{
	var CONTROLS = [ 'div' ];

	var result = [];

	for (var i = 0; i < CONTROLS.length; i++ )
	{
		var objects = document.getElementsByTagName( CONTROLS[ i ] );
		for (var j = 0; j < objects.length; j++ )
		if ( objects[ j ].className == myName )
		result.push( objects[ j ] );

	};

	return result;
};

function ChangeRend(a, wk){
	var mySpecials = getElementsByClassName('k_' + wk);
	if(a.checked){
		for (var i = 0; i < mySpecials.length; i++ ){
			mySpecials[i].style.display = "";
		}
	}
	else{
		for (var i = 0; i < mySpecials.length; i++ ){
			mySpecials[i].style.display = "none";
		}
	}
}

function ChangeTargyalatt(a){
//  alert(a.checked);
  var mySpecials = $('mycalendar').getElementsByClassName('targyalatt');
  
  if(a.checked){
    
    for (var i = 0; i < mySpecials.length; i++ ){
      
      mySpecials[i].addClassName('targyalatt2');
    }
  }
  else{
    for (var i = 0; i < mySpecials.length; i++ ){
      
      mySpecials[i].removeClassName('targyalatt2');
    }
  }
}	
