//requests file



function showActiveDetails() {
		img= new Image();
		img.src='js/getdetails.php';
		var path = img.src.split('js/getdetails.php');
		path = path[0]+'js/getdetails.php';
		path = path+'?lower_select='+calendar.select_low+'&upper_select='+calendar.select_up+calendar.special;
		data = makeRequest(path,'details');
}

function setActiveDetails() {
		if(calendar.active_hotel >0) {
			var index = parseInt(calendar.active_hotel);
			showDetails(index);
		}
		
}


function showDetails(id) {
	if(calendar.hotels_status || (calendar.special && calendar.special !='') ) {
		calendar.active_hotel = id;
		index = eval(id-1); // starting index is 0
		if(calendar.details[index] != 0){
		  // patch TBE
		  if ( !calendar.div ) calendar.div = "prehled";
			var elem = document.getElementById(calendar.div);
			for(i=0;i<calendar.hotels_status.length;i++) {
				del = eval(i+1);
				var active_link = document.getElementById('h'+del);
				if(calendar.hotels_status[i]=='2'){
					active_link.className = 'hotel_enable';		
				}
				else if(calendar.hotels_status[i]=='1'){
					active_link.className = 'hotel_disable';					
				}
	
				if(del == id) {
					active_link.className = 'hotel_active';
					calendar.active_hotel = id;
					}
			}	
			// generate room selects
			elem.innerHTML = calendar.details[index]+'<div style="clear: left;"></div>';
			var selects = document.getElementsByName('room_input');
			var a = 0;
			if(calendar.is_selected == calendar.active_hotel) {
				var a; // detects if cancelled room is the first one
				for(var i in calendar.selected_rooms) {
					// calls error if it is first unavailable room
					if(calendar.selected_rooms[i] > 0 && calendar.max_rooms[i] == undefined && a!=1) {
						alert(calendar.err_norooms);
						a=1; // sets 1 when first room is not available
						calendar.selected_rooms[i] = undefined;
					}
					// only sets the value on 0
					else if(calendar.selected_rooms[i] && calendar.max_rooms[i] == undefined && a==1) {
						calendar.selected_rooms[i] = 0;					
					}
				}
			}
			// for each select in table
			for(i=0;i<selects.length;i++) {
				var room_id = selects[i].id.split('room_input')[1]; //id of room
				var options = calendar.max_rooms[room_id]; // no of options in select
				if(calendar.selected_rooms && options<calendar.selected_rooms[room_id]) {
					alert(calendar.err_rooms);
					calendar.selected_rooms[room_id] = options;
				}
				for(k=0;k<(options+1);k++) {
					var option = document.createElement('option');
					option.setAttribute('value',k);
					text = document.createTextNode(k);
					option.appendChild(text);
					if(calendar.selected_rooms && calendar.selected_rooms[room_id] == k) {
						option.setAttribute('selected','selected');
					}
					selects[i].appendChild(option);
				}
				
			}
			
		}
	}
	else {
		alert(calendar.err_select);
	}

		//	set height of vertical separator
		
		var cells = document.getElementsByTagName('p');
		var fields = eval(cells.length);
		var counter = 0;		

		for(i=0;i<fields;i++) {
			var a = i+1;
			var implicit_height = 20;
			if(calendar.special) {
				var implicit_height = 150;
			}
			var height =  document.getElementById('room'+a).clientHeight;
			if(implicit_height > height) {
				height = implicit_height;
			}
			height = eval(height-10)+'px';
			counter++;
			var cell = document.getElementById('rooms_count'+eval(i+1));
			cell.style.height = height;
			var vr = document.getElementById('vr'+counter);
			vr.style.height = height;
		}
		// recount prices of 
		if(calendar.active_hotel == calendar.is_selected && calendar.selected_rooms) {
			for(var i in calendar.selected_rooms) {
				if(calendar.selected_rooms[i] != undefined && calendar.selected_rooms[i] != 0 && i!=0) {
					document.getElementById('room_input'+i).onchange();
					/* setTimeout(document.getElementById('room_input'+i).onchange,1); // updates price with setTimeout*/
				}	
			}
		}
}

function makeRequest(url,type) {
        var http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { 
			alertContents(http_request,type);
		}
		//alert(url);
        http_request.open('GET', url, true);
        http_request.send(null);
    }

    function alertContents(http_request,type) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
					var data = http_request.responseText;
					eval(data);
					if(type=='details') {
						setActiveDetails();
					}
					else if(type=='hotels') {
						setActiveHotels();
						showActiveDetails();
					}
					else if(type=='values') {
						calendar.subRender();
					}
            } else {
                alert('We are sorry but you need newer browser.');
            }
        }

    }


