/*
# js calendar
*/

var cnt = 0;

function Calendar() {
	var year; //current year
	var month; //current month
	var calendar_status;
	var calendar = new Array(84); //current calendar content in array(84) from PHP
	var month_name; //array of month names
	var implicit_year; //implicit year
	var implicit_month; //implicit month
	var lang;
	var day_name; //array of abbr day names
	var sequence; //which calendar to render (1/2)
	var years = new Array; // current years' field content
	var months = new Array(12); // current months' field content
	var select_low; // lower selected region
	var select_up; //upper selected region
	var minimum_length = 0; //take from PHP if there is one
	var shift; //determines shift in calendar
	var is_loaded = 0; //state of loading AJAX
	var active_hotel;
	var is_selected; // defined when hotel room is selected
	var selected_rooms; // array for saving selected rooms
	var max_rooms; // array of max free space in rooms
	
	var special; // special parameter.. detects special offers or lastminute offers
	var vrs; // no of vr elements in code;
	var div = "prehled"; // settings for div element with table of details
	var came_back;
	var back_counter;
	var step;


				

	
	switch(window.lang) {
		case 'de':					
			this.month_name = Array('Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
			this.day_name = Array('Mo','Di','Mi','Do','Fr','Sa','So');	
			this.header_year_name = 'Jahr';
			this.header_month_name = 'Monat';		
		break;			
		case 'ho':			
			this.month_name = Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus',	'Septemeber','Oktober',	'November','December');
			this.day_name = Array('Ma','Di','We','Do','Vr','Za','Zo');	
			this.header_year_name = 'Jaar';
			this.header_month_name = 'Maand';
		break;
		case 'da':
			this.month_name = Array('januar','februar',	'marts','april','maj','juni','juli','august','september','oktober','november','december');
			this.day_name = Array('Mo','Di','Mi','Do','Fr','Sa','So');	
			this.header_year_name = '&Aring;r';
			this.header_month_name = 'M&aring;ned'; 		
		break;
		case 'en':					
			this.month_name = Array('January','February','March','April','May','Juny','July','August','September','October','November','December');
			this.day_name = Array('Mo','Tu','We','Th','Fr','Sa','Su');	
			this.header_year_name = 'Year';
			this.header_month_name = 'Month';
		break;					
		default:					
			this.month_name = Array('Leden','&Uacute;nor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec');
			this.day_name = Array('Po','Ut','St','Ct','Pa','So','Ne');	
			this.header_year_name = 'Rok';
			this.header_month_name = 'Měs&iacute;c';
		break;			
	}
		
		
this.err_range = 'Ve vybraném období se vyskytuje minimálně jeden den, kdy je hotel plně obsazen.';
this.err_length1 = 'Minimální délka pobytu jsou '
this.err_length2 =' dny.'
this.err_select = 'Nejprve musíte vybrat termín.';
this.err_full = 'V tomto období je vybraný typ pokoje nebo hotel obsazen. ';
this.err_delete = 'Nelze provést.';
this.err_rooms = 'Nelze provést. 2 ';
this.err_norooms = 'Nelze provést. 3 ';
this.err_nodate = 'Nelze provést. 4 ';
this.err_noroom = 'Nelze provést. 5 ';
var err_input = 'Vyplňte pole ';
var err_form = Array('Jméno','PAÖijmení','Ulice a —?.p.','Město','PS—O','Země','Jazyk','E-mail','Telefon','PoznA!mka');
var err_change_link = 'VA!echny VA!mi zadané Aodaje budou vymazA!ny. PAÖejete si pAÖesto pokra—?ovat?';
var err_exit = 'Opravdu si přejete opustit rezervační systém?';
 //**important** includes language pack
	
	this.drawCaption = function(sequence) {  
		if(isNaN(this.month)) {
			this.month = this.months[0];
		}
		if(typeof(sequence)=='undefined' || sequence==0 || sequence>2) {
			sequence = 1;
		}
		month_no = eval(this.month -1 + sequence);
		if(isNaN(this.year)) {
			this.year = this.implicit_year;
		}
		year = this.year;
		if(month_no > 12) {month_no = 1;year=eval(this.year+1)}
		month = this.month_name[month_no-1]+' '+year ;
	
		//caption =
		obj = document.getElementById('caption'+sequence); //
		if (obj) {
			obj.innerHTML = month;
		}
	}
	
	this.drawHeader = function(sequence) {  
		if(typeof(sequence)=='undefined' || sequence<1 || sequence>2) {
			sequence = 1;
		}	
	    var content = '';
		header = document.getElementById('calendar'+sequence).tHead
		header.removeChild(header.firstChild);
		tr = document.createElement('tr');
		header.appendChild(tr);
		
		for(i=0;i<7;i++) {
			td = document.createElement('td');
			td_text = document.createTextNode(this.day_name[i]);
			td.appendChild(td_text);
			tr.appendChild(td);
			if(i>4) {
				td.className = 'weekend';
			}			
			
		}
	}
	
	this.drawContent = function(sequence) {
		if(typeof(sequence)=='undefined' || sequence<1 || sequence>2) {
			sequence = 1;
		}		
		var calendar = this.calendar;	
		a=0; //counter
		var start_count = (sequence-1)*42;
		var stop_count = sequence*42;
		var day = 1;
		table = document.getElementById('calendar'+sequence).tBodies[0];
		//reseting table content
		while(table.hasChildNodes()) {
			table.removeChild(table.lastChild);
		}
		for(a=0;a<6;a++) {
			tr = document.createElement('tr');
			for(i=0;i<7;i++) {
				td = document.createElement('td');
				//create text ---------------------------------------------
				if(calendar[start_count] == 1) {
    				td_content = document.createTextNode(day);
					day++;
				}
				// -------------------------------------------------------
				else if(calendar[start_count]==2 || calendar[start_count]==3 || calendar[start_count]==4) {
					//correct syntax of day

					var month = eval(this.month+sequence-1);
					if(month>12) {
						month = 1;
						year = eval(this.year+1);
					}
					else{
						year = this.year;
					}
					if((disp_month = month) < 10) {
						var disp_month = '0'+disp_month;
					}
					if(day < 10) {
						disp_day = '0'+day;
					}
					else {
						disp_day = day;
					}
					var id = year+''+disp_month+disp_day;
					link = document.createElement('a');
					link.setAttribute('id',id);
					//link.setAttribute('href','#');
					// coz of onclick, have to call external function to finish cooperation within the class				
					link.onclick = external;
					link.onmouseover = function() {
						if(this.className != 'active') {
							this.className = 'hover';
						}
					}
					link.onmouseout = function() {
						if(this.className == 'hover'){
							this.className = '';
						}	
					}

					content = document.createTextNode(day);
					link.appendChild(content);
					td_content = link;
					day++;
				}				
				else {
					td_content = document.createTextNode(" ");
				}	
				//append class for weekend days
				if(i>4) {
					td.className = 'weekend';
				}
				// rh ---
				if(calendar[start_count] == 3) 	{ 
					td.className = 'full';
				}
				// ***
				// rh ---
				if(calendar[start_count] == 4) 	{ 
					td.className = 'part';
				}
				// ***				
				td.appendChild(td_content);
				tr.appendChild(td);				
				start_count++;
			}
			table.appendChild(tr);
		}
	}


	this.renderCalendar =  function() {
		if(this.special) {
			this.div = 'prehled';
		}
		else {
			this.div = 'prehled';
		}

		//disableHotels();		
		//document.getElementById(this.div).innerHTML = '';		
		
		if(this.came_back !=true) {
					
		}
		if(this.came_back == true) {
			this.back_counter++;
		}
		if(this.came_back != true || this.back_counter > 1) {
			this.getYear();
		}
		this.changeMonth();
		this.getValues();
		

		
	}
	this.resetPeriod = function() {
		this.getYear();
		this.changeMonth();
		this.getValues();
		//document.getElementById(this.div).innerHTML = '';
		this.hotels_status = undefined;
		this.selected_rooms = undefined;
		this.is_selected = undefined;
		this.active_hotel = 0;
		this.details = 0;
		this.is_loaded = 0;
		this.select_low = undefined;
		this.select_up = undefined;
		calendar.active_hotel = '';
		//disableHotels();
		this.subRender();
	}
	this.subRender = function() { 
		this.drawYear();
		this.drawMonth();
		this.drawCaption(1);
		this.drawHeader(1);
		this.drawContent(1);
		this.drawCaption(2);
		this.drawHeader(2);
		this.drawContent(2);
		if(this.select_low > 0 || this.select_up > 0) {
			this.checkRange();
		} 
	/*	if(this.special) {
			showActiveHotels();
			showActiveDetails();
		}*/
	}
	
	this.getYear = function() {
			this.year=parseInt(document.getElementById('year').value);
			this.select_low = undefined;
			this.select_up = undefined;
	}
	this.drawYear = function() {
		var content = "";
		if(this.came_back == true && this.back_counter ==1) {
			
		}
		else {
		    
			this.year = parseInt(document.getElementById('year').value);
			if ( isNaN(this.year) ) this.year = this.implicit_year;
		}
		var select = document.getElementById('year_block'); //pick the element
		var years = this.years;
		content += '<strong>'+this.header_year_name+'</strong> ';
		content += '<select id="year" onchange="calendar.select_low=0;calendar.select_up=0;calendar.is_selected=0;calendar.active_hotel=0;calendar.selected_rooms=0;calendar.renderCalendar();">';
		
		for(i=0 ; i < years.length ; i++) {		   
		   content+='<option ' ;
		   content+='value="'+years[i]+'" ';
		   
		    if(this.year == years[i]) {
				content+=' selected="selected"'
			}
			content+='>';
			content+=years[i];
			content+=' &nbsp;</option>'
		}
		content+='</select>';
		select.innerHTML = content;
		
	}	
	
	this.getValues = function() {

		img= new Image();
		img.src='templates/default/js/calend/getvalues.php';
		var path = img.src.split('getvalues.php');
		path = path[0]+'getvalues.php';
		if(this.special == undefined) {
			this.special = '';
		}
		cnt++;
		path = path+'?year='+this.year+'&month='+this.month+this.special+'&cnt='+cnt+'&lang='+this.lang;
		//alert(path);		
		makeRequest(path,'values');
	} 
	
	this.drawMonth = function() {
		var content ="";
		var select = document.getElementById('month_block'); //pick the element
		var months = this.months;
		content += '<strong>'+this.header_month_name+'</strong> ';
		content += '<select id="month" onchange="calendar.select_low=0;calendar.select_up=0;calendar.active_hotel=0;calendar.is_selected=0;calendar.selected_rooms = 0;calendar.renderCalendar();">';
		for(i=0 ; i < months.length ; i++) {
		   content+='<option ';
		   content+='value="'+months[i]+'" ';
		    if(this.month == months[i]) {
				content+=' selected="selected"'
			}
			content+='>';
			content+=this.month_name[months[i]-1];
			content+='</option>'
		}
		content+='</select>';				
		select.innerHTML = content;
		
	}

	this.shiftMonth = function(shift) {
		this.shift = shift;
		this.resetPeriod();
	}
	this.changeMonth = function() {
		if( typeof(this.months) == 'undefined') {
			return;
		}
		
		if(this.shift == 0 || typeof(this.shift) == 'undefined' ) {
			this.month = parseInt(document.getElementById('month').value);
		}
		
		for(i=0;i<this.months.length;i++) {
			if(this.month == this.months[i]) {
				var index=i;
			}
		
		}
		if(this.shift=='+') {
			if(index < eval(this.months.length-1)) {
				index++;
				this.month=this.months[index];
			}
			else if(index = eval(this.months.length-1) && this.month==12){
					var new_year = eval(this.year+1);
					for(i=0;i<this.years.length;i++) {
						if(new_year == this.years[i]) {
							var is_year = true;
						}
					}
					if(is_year != true) {
						return;
					}
					else {
						this.year++;
						this.month = 1;
						this.back_counter = 1;
						this.came_back = true;
					}
			}
		}
		else if(this.shift=='-') {
			if(index > 0 && this.month>0) {
				index--;
				this.month=this.months[index];
			}
			else if(index == 0 && this.month==1){
				this.year--;
				this.month = 12;
				this.back_counter = 1;
				this.came_back = true;				
			}
		}		
		if(this.shift) {
			this.select_low = undefined;
			this.select_up = undefined;
			this.is_selected = undefined;
			this.active_hotel = undefined;
			this.selected_rooms = undefined;
			this.shift=0;
		}
	}
	
	
	this.checkRange = function() {
		var continuous = true;
		var count = 0;
		
		
		// if it steps over the month line
		if(this.select_up>this.limit_low && this.select_low<this.limit_up) {
			for(i=this.select_low;i<=this.limit_low;i++) {
				elem = document.getElementById(i);
				if(elem) {
					count++;
					elem = document.getElementById(i);
					elem.className = 'active';
				}	
				else {
					continuous = false;
					break;
				}
			}
			for(i=this.limit_up;i<=this.select_up;i++) {
				elem = document.getElementById(i);
				if(elem) {
					elem = document.getElementById(i);
					elem.className = 'active';
					count++;
				}	
				else {
					continuous = false;
					break;
				}
			}
		}
		//when it is within one month range
		else {
			count = 0;
			for(i=this.select_low;i<=this.select_up;i++) {
				elem = document.getElementById(i);
				if(elem) {
					elem = document.getElementById(i);
					elem.className = 'active';
					count++;
				}
				else {
					continuous = false;
					break;
				}
			}			
		}	
		if(continuous == false) {
			alert(this.err_range); //take it from language pack
			this.select_low = undefined;
			this.select_up = undefined;
			this.resetPeriod();
		}
		else if(count>=this.minimum_length) {
			 var obj=document.getElementById('d_od');
			 obj.value=this.select_low;
			 var obj=document.getElementById('d_do');
			 obj.value=this.select_up;
			 
		 	 var obj=document.getElementById('sel_month');
			 obj.value=this.month;
			 
		}
		else if(this.select_up == this.select_low && this.select_low !=0) {
			//showActiveHotels();
			alert(this.err_length1+this.minimum_length+this.err_length2);			
		}	
				
	}	

/*  end of class calendar   */	
}

calendar = new Calendar();

// external function for listener
function external() {
	var sel = this.id;
	if(calendar.select_up > sel) {
		for(i=calendar.select_up;i>=sel;i--) {
			elem = document.getElementById(i);
			if(elem != null) {
				elem.className = 'inactive';	
			}
		}					 	
	}

	if(sel<calendar.select_low && (calendar.select_up ==0 || typeof(calendar.select_up)=='undefined')) {
		calendar.select_up = calendar.select_low;
		calendar.select_low = sel;
	}
	// click on the upper select day
	else if(calendar.select_up == sel && calendar.select_low) {
		active_td = document.getElementById(calendar.select_up);
		active_td.className = '';	

		if(calendar.select_up == calendar.limit_up) {
			calendar.select_up = calendar.limit_low;
		}
		else {
			calendar.select_up = eval(parseInt(calendar.select_up) -1);
		}	

	}
	// click on the low select day
	else if(calendar.select_low == sel && calendar.select_up) {
		if(calendar.select_low == calendar.limit_low) {
			calendar.select_low = calendar.limit_up;
		}
		else {
			calendar.select_low = eval(parseInt(calendar.select_low) +1);
		}	
		sel = calendar.select_low;
	}		
	else if(typeof(calendar.select_low)=='undefined' || calendar.select_low ==0 || sel<calendar.select_low) {
		calendar.select_low = sel;
		if(calendar.select_up && calendar.select_low) {
		}
		else {
			active_td = document.getElementById(calendar.select_low);
			active_td.className = 'active';	
		}	
	}
	else if(typeof(calendar.select_low) !='undefined' || sel>calendar.select_low && calendar.select_up != sel) {
		calendar.select_up = sel;
	}
	//call function to check if consistent
	if(	(typeof(calendar.select_up) !='undefined' || calendar.select_up>0) && 
	(typeof(calendar.select_low) !='undefined' || 	calendar.select_low>0)) {
		calendar.checkRange();
	}
}








