var xmlHttp;
var isComplete=0;

function createXMLHttpRequest() {
var xh;
if (window.XMLHttpRequest) {
xh = new window.XMLHttpRequest();
}else if (window.ActiveXObject) {
xh = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
return xh;
}
function createOptionNode(text,value){
		var newOption = document.createElement("option");
		var newValue = document.createTextNode(text);
		newOption.appendChild(newValue);
		newOption.setAttribute("value",value);
		return newOption;
	}
	function setRoomsLimit(limit,lang){
		var k = parseInt(limit.substring(limit.indexOf("-")+1,limit.lastIndexOf("-")));
		var p = parseInt(limit.substring(limit.lastIndexOf("-")+1));
		var str = new String("");
		var list = null, persons = null;
		persons = document.getElementById("human1");
		if (persons.childNodes.length != p){
			if (p==2){				if (lang=="en"){persons.appendChild(createOptionNode("Double","2"));}
				else {persons.appendChild(createOptionNode("Двухместное","2"));}
        	}else{
         		persons.removeChild(persons.lastChild);
         	}
		}
		list = document.getElementById("roomsCount");
		if (list !=null){
        	while (list.hasChildNodes()){
        		list.removeChild(list.lastChild);
        	}
         	for (var i=1;i<=k;i++){
            	list.appendChild(createOptionNode(i.toString(),i.toString()));
		    }
		}
		else{alert("Ошибка формы бронирования. Перезагрузите страницу и повторите запрос.");}

	}
function getPrices(lang){
    var cur = "руб.";
    switch(lang){    	case "en": case "de":
    		cur = "RUR";
    	break;    }
    var checkIn=new Date((document.getElementById('arrival_date').value).replace("\.",", ").replace("\.",", "));
    var checkOut=new Date((document.getElementById('departure_date').value).replace("\.",", ").replace("\.",", "));
    /*if (checkIn>=checkOut){    	alert("Проверьте правильность заполнения формы. Дата выезда не может быть раньше даты заезда.");
    	return;    }*/
    document.getElementById("sum").innerHTML ="";
    document.getElementById("progressGif").style.display="inline";
    var category=document.getElementById('category').value;
    category = category.substring(0,category.indexOf("-"));
    var roomsNum=document.getElementById('roomsCount').value;
    var persons=document.getElementById('human1').value;
    xmlHttp = createXMLHttpRequest();
	xmlHttp.open("GET", "php/ajax.php?mode=getPrice&startDate="+document.getElementById('arrival_date').value
	                  + "&endDate="+document.getElementById('departure_date').value+"&category="+category
	                   +"&roomsNum="+roomsNum+"&persons="+persons,true);
    xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
            document.getElementById("progressGif").style.display="none";
			document.getElementById("cost").style.display="block";
			if (!xmlHttp.responseText){                document.getElementById("sum").innerHTML = "По заданным параметрам поиска результатов не найдено, возможно, цены на выбранный вами период еще не опубликованы.";
				return;			}else{
            	var str = xmlHttp.responseText;
            	document.getElementById("sum").className="sum";
            	if (str.indexOf("-") != -1){            		str = str.substring(0,str.indexOf("-"))+" "+cur
            	 //   	                + " в главном корпусе<br/>"
            	 //       	            + str.substring(str.indexOf("-")+1)
            	//            	        + " " + cur
            	//                	    + " во втором корпусе";            	}else{                	str = str + " " + cur+" в главном корпусе";
                	document.getElementById("sum").className="sum";            	}
            	document.getElementById("sum").innerHTML = str;
        	}
        }    };
	xmlHttp.send(null);
}
