var addEvent= (function(){
    if(document.addEventListener) {    
        return function(obj,name,func){
        obj.addEventListener(name, func, false);    
        }  
    }
    else if (document.attachEvent) {   
        return function(obj,name,func){
            obj.attachEvent('on'+name, func); 
        }      
    }else return function(){throw 'Error'}
})()

var current_id = -1;

function show_tab(sel) {
	var obj = document.getElementById(sel);
	obj.style.display = '';
	
}

function close_tab(sel) {
	var obj = document.getElementById(sel);
	obj.style.display = 'none';
	obj.innerHTML = '';
}

function hotels_handle() {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
       req.onreadystatechange = readystate; // назначим обработчик событию объекта 
	   
	   var country = document.getElementById('country_s').value;
	   var resort = document.getElementById('resort_s').value;
	   var city = document.getElementById('city_s').value;
	   var category = document.getElementById('category_s').value;
	   var hotel = document.getElementById('hotel').value;
	   alert(country);
	   req.open("GET", '/include/hotels_handle.php?country='+country+'&resort='+resort+'&city='+city+'&category='+category+'&hotel='+hotel, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}

function load_resorts() {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
       req.onreadystatechange = loadresorts; // назначим обработчик событию объекта 
	   
	   var country = document.getElementById('country_s').value;
	
	   req.open("GET", '/include/filter.php?country='+country, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}

function load_cities() {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
       req.onreadystatechange = loadcities; // назначим обработчик событию объекта 
	   
	   var country = document.getElementById('country_s').value;
	   var resort = document.getElementById('resort_s').value;
	   
	   req.open("GET", '/include/filter.php?country='+country+'&resort='+resort, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}

function load_categories() {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
       req.onreadystatechange = loadcategory; // назначим обработчик событию объекта 
	   
	   var country = document.getElementById('country_s').value;
	   var resort = document.getElementById('resort_s').value;
	   var city = document.getElementById('city_s').value;
	   
	   req.open("GET", '/include/filter.php?country='+country+'&resort='+resort+'&city='+city, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}

function load_hotels() {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
       req.onreadystatechange = loadhotel; // назначим обработчик событию объекта 
	   
	   var country = document.getElementById('country_s').value;
	   var resort = document.getElementById('resort_s').value;
	   var city = document.getElementById('city_s').value;
	   var category = document.getElementById('category_s').value;
	   
	   req.open("GET", '/include/filter.php?country='+country+'&resort='+resort+'&city='+city+'&category='+category, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}


function loadresorts() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок
        	document.getElementById('resort').innerHTML = ""+req.responseText+"";
        	document.getElementById('city').innerHTML = '<select name="city_n" id="city_s" onchange="load_categories(); show_button();"><option value="">Город...</option></select>';
			document.getElementById('category').innerHTML = '<select name="category_n" id="category_s" onchange="show_button(); show_button();"><option value="">Категория...</option></select>';
       	
        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }	
}

}

function loadcities() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок
        	document.getElementById('city').innerHTML = ""+req.responseText+"";
			document.getElementById('category').innerHTML = '<select name="category_n" id="category_s" onchange="show_button();"><option value="">Категория...</option></select>';
        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }	
}
}

function loadcategory() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок
        	document.getElementById('category').innerHTML = ""+req.responseText+"";
        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }	
	}
}

function loadhotel() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок
        	hotel = document.getElementById('hotel');
        	if (hotel) hotel.innerHTML = ""+req.responseText+"";
        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }	
	}
}

function readystate() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок	
		document.getElementById('hotels_box').innerHTML = ""+req.responseText+"";
			var i = 1;
			while (document.getElementById('text'+i)) {
				document.getElementById('text'+i).style.display = 'none';
				i++;
			}

        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }
    }
}

function hotel_handle(id) {

    req=false;
	
	if (window.navigator.userAgent.indexOf("MSIE")==-1) {
		if(window.XMLHttpRequest){ // сработает в Mozilla и Safari
            req=new XMLHttpRequest();
        }
	} else {
	try { // определить метод поддержки
        req=new ActiveXObject('Msxml2.XMLHTTP'); 
    } catch (e) {
        try {
            req=new ActiveXObject('Microsoft.XMLHTTP'); // сработает в Internet Explorer
        } catch(e) { }
	}
	}

    if (req){// если какой-то из вариантов поддерживается
        req.onreadystatechange = hotelreadystate; // назначим обработчик событию объекта 
		current_id = id;
		req.open("GET", '/include/hotels_handle.php?id='+id, true);
	   
	   var busy = document.createElement('div');
	   busy.setAttribute('id','busy');
	   
	    busy.style.position   = 'absolute';
        busy.style.top        = '50%';
        busy.style.left       = '50%';
        busy.style.width      = '300px';
        busy.style.lineHeight = '100px';
        busy.style.margin     = '-50px 0 0 -150px';
        busy.style.textAlign  = 'center';
        busy.style.border     = '1px solid #000';
        busy.style.background = '#fff';

        // выводим текст сообщения о&nbsp;загрузке данных
        busy.appendChild(document.createTextNode('Загрузка...'));
		document.body.appendChild(busy); 
		req.send(null);
    }
}
function hotelreadystate() {
    if (req.readyState == 4){
			var busy = document.getElementById("busy");
			document.body.removeChild(busy);
			
	// если запрос завершен
        if (req.status == 200) { // если он завершен без ошибок	
		document.getElementById('text'+current_id).innerHTML = ""+req.responseText+"";
		show_tab('text'+current_id);
        } else {
            alert("Произошла ошибка "+ req.status+":\n" + req.statusText);
        }
    }
}

addEvent(window,'load',init);

function show_hotel(id) {
	if (current_id > -1) {
		close_tab('text'+current_id);
		}
	hotel_handle(id);
}

function init() {
	//document.getElementById('hotels_box').innerHTML='';
}

function show_button() {
	btn = document.getElementById('submitbtn');
	if (btn) {
		if ($('#country_s').val()) btn.style.display='inline';
		else btn.style.display='none';
	}
}

function change_hotel_img (id) {
	document.getElementById('large_img').src = '/images/hotels/image_' + id + '.jpg';
}
