function search_init(classification,position,noresmes){
	var link = lang_s["URL"]+"json.php?";
	var strParams = "classification="+classification;
	strParams += "&noresmes=" + noresmes;
	new Autocomplete(classification + '_AddressSearch_' + position, {
		serviceUrl: link + strParams,
		classification:classification,
		position:position,
		// callback function:
		    onSelect: function(value, data){
				fillAutocompletedBoxes(classification,position,data,value);
			}
	});
}

function fillAutocompletedBoxes(classification,position,catalogId,catalogTitle){
	var catId = document.getElementById(classification+'_CatalogID_'+position);
	if(catId && catalogId != undefined){
		catId.value = catalogId;
	}
	var catTitle = document.getElementById(classification+'_CatalogTitle_'+position);
	if(catTitle && catalogTitle != undefined){
		catTitle.value = catalogTitle;
	}
	return true;
}

function activateEntry(obj){
	obj.className = "suggest_entry suggest_entry_selected";
}

function deactivateEntry(obj){
	obj.className = "suggest_entry";
}

function hideSearchDiv(classification,position){
	myDiv = document.getElementById(classification+'_suggest_popup_'+position);
	myDiv.innerHTML = "";
	myDiv.style.display = "none";
}

function selectEntry(id,title,classification,position){
	document.getElementById(classification+'_AddressSearch_'+position).value = title;
	document.getElementById(classification+'_CatalogTitle_'+position).value = title;
	document.getElementById(classification+'_CatalogID_'+position).value = id;
	hideSearchDiv(classification,position);
}

function clearData(classification,id,position){
	var posAdd = "";
	if(position){
		posAdd = "_" + position;
	}
	switch(classification){
		case "accommodation":
			document.getElementById(classification+"_Duration").value = "";
			document.getElementById("AvailabilityDate"+posAdd).value = "";
			document.getElementById(id).style.display = "none";
		break;
		case "hotel":
			document.getElementById(id).value = "";
			document.getElementById("cldata_"+id).style.display = "none";
		break;
	}
}

function selectData(classification,id,lang,postfix){
	switch(classification){
		case "accommodation":
			show_calendar(id,lang);
			if(!document.getElementById(classification+"_Duration").value)
				document.getElementById(classification+"_Duration").value = "7";
			document.getElementById("cldata").style.display = "";
		break;
		case "hotel":
			show_calendar(id,lang);
			if(document.getElementById(classification+"_Duration")){
				if(!document.getElementById(classification+"_Duration").value)
					document.getElementById(classification+"_Duration").value = "1";
			}
			if(document.getElementById(classification+"Duration")){
				if(!document.getElementById(classification+"Duration").value)
					document.getElementById(classification+"Duration").value = "1";
			}
			if(document.getElementById("hotelRoomsData" + postfix)){
				if(document.getElementById("hotelRoomsData" + postfix).innerHTML)
					document.getElementById("hotelRoomsData" + postfix).innerHTML = "";
			}

			if(document.getElementById("cldata_"+id))
				document.getElementById("cldata_"+id).style.display = "";
		break;
	}
}

function setPrice(classification){
	switch(classification){
		case "accommodation":
			if(document.getElementById("MaxPrice").value){
				var reg=/^\d+$/;
				if(reg.test(document.getElementById("MaxPrice").value)){
					document.getElementById("MaxPrice").value = document.getElementById("MaxPrice").value+".00";
				}
			}
		break;
		case "hotel":
			if(document.getElementById("maxTotalPrice").value){
				var reg=/^\d+$/;
				if(reg.test(document.getElementById("maxTotalPrice").value)){
					document.getElementById("maxTotalPrice").value = document.getElementById("maxTotalPrice").value+".00";
				}
			}
		break;
	}
}

function checkFields(classification, isAddressMandatory, position){
	var posAdd = "";
	if(position){
		posAdd = "_" + position;
	}

	if(document.getElementById("t_"+classification).value == 1){
		alert(lang_s["error.searchInProgress"]);
		return false;
	}
	
	var loadBox = document.getElementById("load_"+classification);
	if(loadBox && loadBox.style.display == ""){
		alert(lang_s["error.searchInProgress"]);
		return false;
	}

	if(isAddressMandatory){
		if(document.getElementById(classification + "_CatalogID") && !document.getElementById(classification + "_CatalogID").value){
			alert(lang_s["error.selectCountry"]);
			return false;
		}
	}

	switch(classification){
		case "accommodation":
			if(document.getElementById("AvailabilityDate"+posAdd).value){
				var objStringDate = new String(document.getElementById("AvailabilityDate"+posAdd).value);
				var day = objStringDate.substr(8,2);
				var month = objStringDate.substr(5,2)-1;
				var year = objStringDate.substr(0,4);
				d1 = new Date(year,month,day);
				d = new Date();
				d.setHours(0); d.setMinutes(0); d.setSeconds(0); d.setMilliseconds(0);
				if(d1<d){
					alert(lang_s["error.availabilityDateIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById(classification+"_Duration").value && !document.getElementById("AvailabilityDate"+posAdd).value){
				document.getElementById(classification+"_Duration").value = ""; // default duration if date not selected
			}
			if(!document.getElementById(classification+"_Duration").value && document.getElementById("AvailabilityDate"+posAdd).value){
				document.getElementById(classification+"_Duration").value = 7; // default duration if date selected
			}

			if(document.getElementById(classification+"_Duration") && document.getElementById(classification+"_Duration").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById(classification+"_Duration").value)){
					alert(lang_s["error.durationIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("MaxPrice") && document.getElementById("MaxPrice").value){
				var reg=/^\d+[.,]?\d{1,2}$/;
				if(!reg.test(document.getElementById("MaxPrice").value)){
					alert(lang_s["error.maxPriceIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("beach") && document.getElementById("beach").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("beach").value)){
					alert(lang_s["error.beachIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("bedrooms") && document.getElementById("bedrooms").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("bedrooms").value)){
					alert(lang_s["error.bedroomsIncorrectValue"]);
					return false;
				}
			}

			if(document.getElementById("railway") && document.getElementById("railway").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("railway").value)){
			        alert(lang_s["error.railwayIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("golf") && document.getElementById("golf").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("golf").value)){
			        alert(lang_s["error.golfIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("autoban") && document.getElementById("autoban").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("autoban").value)){
			        alert(lang_s["error.autobanIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("elevator") && document.getElementById("elevator").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("elevator").value)){
			        alert(lang_s["error.elevatorIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById("skier") && document.getElementById("skier").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById("skier").value)){
			        alert(lang_s["error.skierIncorrectValue"]);
					return false;
				}
			}
		break;
		case "hotel":
			if(document.getElementById("startTravelDate"+posAdd) && !document.getElementById("startTravelDate"+posAdd).value){
				alert(lang_s["error.selectDate"]);
				return false;
			}

			if(document.getElementById(classification+"_Duration").value && !document.getElementById("startTravelDate"+posAdd).value){
				alert(lang_s["error.selectDate"]);
				return false;
			}

			if(document.getElementById(classification+"_Duration") && !document.getElementById(classification+"_Duration").value){
				alert(lang_s["error.durationIncorrectValue"]);
				return false;
			}

			if(document.getElementById(classification+"_Duration") && document.getElementById(classification+"_Duration").value){
				var reg=/^\d+$/;
				if(!reg.test(document.getElementById(classification+"_Duration").value)){
					alert(lang_s["error.durationIncorrectValue"]);
					return false;
				}
			}

			if(document.getElementById("startTravelDate"+posAdd).value && document.getElementById(classification+"_Duration").value){
				var objStringDate = new String(document.getElementById("startTravelDate"+posAdd).value);
				var day = objStringDate.substr(8,2);
				var month = objStringDate.substr(5,2)-1;
				var year = objStringDate.substr(0,4);
				startDate = new Date(year,month,day);
				nowDate = new Date();
				nowDate.setHours(0); nowDate.setMinutes(0); nowDate.setSeconds(0); nowDate.setMilliseconds(0);
				if(startDate < nowDate){
					alert(lang_s["error.availabilityDateIncorrectValue"]);
					return false;
				}
			}

			if(document.getElementById(classification + "_Persons") && !document.getElementById(classification + "_Persons").value){
				alert(lang_s["error.personsIncorrectValue"]);
				return false;
			}

			if(document.getElementById("maxTotalPrice") && document.getElementById("maxTotalPrice").value){
				var reg=/^\d+[.,]?\d{1,2}$/;
				if(!reg.test(document.getElementById("maxTotalPrice").value)){
					alert(lang_s["error.maxPriceIncorrectValue"]);
					return false;
				}
			}
		break;
	}


	if(document.getElementById(classification + "_Persons") && document.getElementById(classification + "_Persons").value){
		var reg=/^\d+$/;
		if(!reg.test(document.getElementById(classification + "_Persons").value)){
			alert(lang_s["error.personsIncorrectValue"]);
			return false;
		}
	}

	document.getElementById("t_"+classification).value = 1;
	return true;
}

function setPage(page,classification){
	document.getElementById('page').value=page;
	doSearch(page,classification);
}

function doSearch(page,classification){
	try{
		var strParams = this.query;
		strParams += "&p="+page;
		var link = lang_s["URL"]+"ajax.php?grandparent=site";
		strParams += "&a="+0;
		strParams += "&m=accommodation_search";
		strParams += "&classification="+classification;
		document.getElementById("load_"+classification).style.display="";
		document.getElementById("divDisplaySearchResults_"+classification).style.display="none";
		//document.getElementById("ñh").disabled=true;
		new Ajax.Updater("divDisplaySearchResults_"+classification, link + strParams,
		    { onComplete: function () {
		        document.getElementById("load_"+classification).style.display = "none";
				document.getElementById("divDisplaySearchResults_"+classification).style.display="block";
		      }
		    }
		);
    }catch(e){
    	alert(e.toString());
    }
}

function showRooms(id,mode,startTravelDate,Duration,postfix,itemUrl){
	try{
		var strParams = "";
		var link = lang_s["URL"]+"ajax.php?grandparent=site";
		strParams += "&m=accommodation_hotels";
		strParams += "&serviceId="+id;
		switch(mode){
			case "singleHotel":
				if(document.getElementById("hotelStartTravelDate" + postfix) && document.getElementById("hotelDuration" + postfix) && document.getElementById("hotelRoomsData" + postfix) && document.getElementById("hotelRoomsDataHeader" + postfix)){
					strParams += "&a=120";
					strParams += "&startTravelDate="+document.getElementById("hotelStartTravelDate" + postfix).value;
					strParams += "&Duration="+document.getElementById("hotelDuration" + postfix).value;
					document.getElementById("hotelRoomsData" + postfix).style.align = "center";
					document.getElementById("hotelRoomsData" + postfix).innerHTML = "<table width=100%><tr><td width=47%>&nbsp;</td><td width=16px; padding=0px><img style='width:16px;border:0px;' src=\"/images/loading.gif\"/></td><td padding=0px width=100% align='left'>"+lang_s["label.common.ajaxLoading"]+"</td></tr></table>";
					document.getElementById("hotelRoomsDataHeader" + postfix).style.display = "block";
					if(postfix && postfix != undefined)
						link = link + "&postfix=" + postfix;
					var objAj = new Ajax.Updater("hotelRoomsData" + postfix, link + strParams);
				}
			break;
			default:
				strParams += "&a=110";
				strParams += "&startTravelDate="+startTravelDate;
				strParams += "&Duration="+Duration;
				strParams += "&itemUrl="+itemUrl;
				document.getElementById("hotelRooms_"+id).style.align = "center";
				document.getElementById("hotelRooms_"+id).innerHTML = "<table width=100%><tr><td width=47%>&nbsp;</td><td width=16px; padding=0px><img style='width:16px;border:0px;' src=\"/images/loading.gif\"/></td><td padding=0px width=100% align='left'>"+lang_s["label.common.ajaxLoading"]+"</td></tr></table>";
				new Ajax.Updater("hotelRooms_"+id, link + strParams,
				    { onComplete: function () {
				        document.getElementById("controllerLink_"+id).style.display = "none";
				      }
				    }
				);
		}

    }catch(e){
    	alert(e.toString());
    }
}

function doHotelsBooking(type,hotelId,roomId,url){
	var arrivalDate = formatDate(document.getElementById('startTravelDate').value);
	var duration = document.getElementById('hotel_Duration').value;
	var persons = document.getElementById('hotel_Persons').value;
    var locationUrl = url + "&arrivalDate="+arrivalDate+"&duration=" + duration +
			"&persons=" + persons + "&type="+type + "&provider=bkg&serviceId="+hotelId+"&serviceRooms="+roomId+"&serviceRoomsCount=1";
	acePopup(locationUrl,'Booking',800,600);
}

function formatDate(strDate){
	var inDate = new String(strDate);
	var year = inDate.substr(0,4);
	var month = inDate.substr(5,2);
	var day = inDate.substr(8,2);
	objStringDate = day+"."+month+"."+year;
	return objStringDate;
}

function checkHotelFields(RoomId,postfix){
	if(postfix == undefined)
		postfix = "";
	if(document.getElementById("hotelStartTravelDate" + postfix) && !document.getElementById("hotelStartTravelDate" + postfix).value){
		alert(lang_s["error.selectDate"]);
		return false;
	}
	if(document.getElementById("hotelDuration" + postfix) && document.getElementById("hotelDuration" + postfix).value && !document.getElementById("hotelStartTravelDate" + postfix).value){
		alert(lang_s["error.selectDate"]);
		return false;
	}

	if(document.getElementById("hotelDuration" + postfix) && !document.getElementById("hotelDuration" + postfix).value){
		alert(lang_s["error.durationIncorrectValue"]);
		return false;
	}

	if(document.getElementById("hotelDuration" + postfix) && document.getElementById("hotelDuration" + postfix).value){
		var reg=/^\d+$/;
		if(!reg.test(document.getElementById("hotelDuration" + postfix).value)){
			alert(lang_s["error.durationIncorrectValue"]);
			return false;
		}
	}

	if(document.getElementById("hotelDuration" + postfix) && document.getElementById("hotelStartTravelDate" + postfix) && document.getElementById("hotelStartTravelDate" + postfix).value && document.getElementById("hotelDuration" + postfix).value){
		var objStringDate = new String(document.getElementById("hotelStartTravelDate" + postfix).value);
		var day = objStringDate.substr(8,2);
		var month = objStringDate.substr(5,2)-1;
		var year = objStringDate.substr(0,4);
		startDate = new Date(year,month,day);
		nowDate = new Date();
		nowDate = new Date();
		nowDate.setHours(0); nowDate.setMinutes(0); nowDate.setSeconds(0); nowDate.setMilliseconds(0);
		if(startDate.valueOf() < nowDate.valueOf()){
			alert(lang_s["error.availabilityDateIncorrectValue"]);
			return false;
		}
	}
	showRooms(RoomId,"singleHotel","","",postfix,"");
}

function prepareBookingForHotel(type,hotelId,url,arrivalDate,tripDuration,postfix){
	var roomsList = document.getElementsByName("roomsList_"+hotelId + postfix);
	var roomId;
	var requestRooms = "";
	var requestRoomsCount = "";
	var requestPersons = 0;
	for(var i=0;i<roomsList.length;i++){
		roomId = roomsList[i].value;
		selIndex = document.getElementById("roomsCounter_"+roomId + postfix).selectedIndex;
		if(selIndex){
			if(requestRooms)
				requestRooms += ",";
			requestRooms += roomId;

			if(requestRoomsCount)
				requestRoomsCount += ",";
			requestRoomsCount += selIndex;

			requestPersons = requestPersons + (parseInt(document.getElementById("roomsMaxPersons_"+roomId + postfix).value)*selIndex);
		}
	}

	if(requestRooms && requestRoomsCount && requestPersons){
		var arrivalDate = formatDate(document.getElementById(arrivalDate).value);
		var duration = document.getElementById(tripDuration).value;
	    var locationUrl = url + "&arrivalDate="+arrivalDate+"&duration=" + duration +
				"&persons=" + requestPersons + "&type="+type + "&provider=bkg&serviceId="+hotelId+"&serviceRooms="+requestRooms+"&serviceRoomsCount="+requestRoomsCount;
		acePopup(locationUrl,'Booking',800,600);
	}else{
		alert(lang['alert.selectRoomsToBook']);
		return false;
	}
}

function setNextDay(id,changedId){
	try{
		if(document.getElementById(changedId).value == ""){
			objStringDate = new String(document.getElementById(id).value);
			day = objStringDate.substr(8,2);
			month = objStringDate.substr(5,2)-1;
			year = objStringDate.substr(0,4);
			objDate = new Date(year,month,day);
			objDate.setDate(objDate.getDate()+1);
			alert(objDate);
			alert(objDate.toString());

			document.getElementById(changedId).value
		}
    }catch(e){
    	alert(e.toString());
    }
}

function handleRoomInfo(id){
	var roomInfo = document.getElementById(id);
	if(roomInfo){
		if(roomInfo.style.display == "none")
			roomInfo.style.display = "block";
		else
			roomInfo.style.display = "none";
	}
}

function preLoadSearchBoxes(IsCityTemplate,classification,formExists){
	try{
		var strParams = "";
		var link = lang_s["URL"]+"ajax.php?grandparent=site";
		strParams += "&m=accommodation_search";
		strParams += "&a=13";
		strParams += "&classification="+classification;
		strParams += "&wrapper=1&inner=index&IsCityTemplate="+IsCityTemplate+"&formExists="+formExists;
		var objAj = new Ajax.Updater('search_boxes_wrapper', link + strParams, {evalScripts: true});
    }catch(e){
    	alert(e.toString());
    }
}

/*
 *
 */
function setActiveTab(classification,contentNodes,tabActive){
	var index = 0;
	var activeClassName = "ai_active";
	var className = "ai";
	for (key in contentNodes) {
	    var topItem = document.getElementById(key);
	    if (!topItem) {
//       alert("Can't find element with ID:" + key);
	       return;
	    }

	    var targetItem = document.getElementById(contentNodes[key]);
	    if (!targetItem) {
//       alert("Can't find element with ID:" + params.nodes[key]);
	       return;
	    }

	    if ((tabActive && tabActive == key) || (index == 0 && !tabActive)) {
	      if (activeClassName) {
	        topItem.className = activeClassName;
	      }
	      targetItem.style.display = "";
	    } else {
	      if (className) {
	        topItem.className = className;
	      }
	      targetItem.style.display = "none";
	    }

	    index++;
	}
}

/*
 *   window onload listener
 */
function addWindowOnloadEvent(customFunction,args){
	if (window.addEventListener) /* W3C */
		window.addEventListener("load", function(){
							customFunction(args)
					}, true);
	else
		if (window.attachEvent) /* IE */
			window.attachEvent("onload", function(){
							customFunction(args)
						});
		else
			window.onload = function(){
						customFunction(args)
				};
}

/*
 *   window document body onload listener
 */
function addOnloadEvent(customFunction,args){
	if (window.document.body.addEventListener){ /* W3C */
		window.document.body.addEventListener("load", function(){
							customFunction(args)
					}, true);
	}else{
		if (window.attachEvent){ /* IE */
			window.attachEvent("onload", function(){
							customFunction(args)
						});
		}else{
			window.document.body.onload = function(){
						customFunction(args)
				};
		}
	}
}

function sortObjects(type){
	document.getElementById('sorttype').value=type;
	document.getElementById('link').value=document.location;
	document.sortform.submit();
}

function handleAcmdSearch(id, lblId, lblMore, lblSimple){
	var searchBox = document.getElementById(id);
	var searchLabel = document.getElementById(lblId);
	if(searchBox){
		if(searchBox.style.display == "none"){
			searchBox.style.display = "block";
			if(lblId && searchLabel)
				searchLabel.innerHTML = lblSimple;
		}else{
			searchBox.style.display = "none";
			if(lblId && searchLabel)
				searchLabel.innerHTML = lblMore;
		}
	}
}

	function selectSTab(aTab,position){
		var tabConf;
		var tabTopConf = { 
							'tab_accommodation_top':'contentCottages_top',
							'tab_hotel_top':'contentHotel_top'
				};
		var tabLeftConf = { 'tab_accommodation_left':'contentCottages_left',
						'tab_hotel_left':'contentHotel_left'
				};
		if(position == "top")
			tabConf = tabTopConf;
		if(position == "left")
			tabConf = tabLeftConf;
		
		for(tab in tabConf){
			if(document.getElementById(tab) && document.getElementById(tabConf[tab])){
				if(aTab == tab){
					document.getElementById(tab).className = "on";
					document.getElementById(tabConf[tab]).style.display = "block";
				}else{
					document.getElementById(tab).className = "";
					document.getElementById(tabConf[tab]).style.display = "none";
				}
			}
		}
	}
	
	function setAdvSB(id,myForm,btnId){
		if(document.getElementById(id) && document.getElementById(id).style.display == "none"){
			myForm.sbm_full.value='';
			myForm.isFull.value=0;
			document.getElementById(btnId+"_adv").style.display = "none";
			document.getElementById(btnId+"_sngl").style.display = "block";
		}else{
			myForm.sbm_full.value=1;
			myForm.isFull.value=1;
			document.getElementById(btnId+"_adv").style.display = "block";
			document.getElementById(btnId+"_sngl").style.display = "none";
		}
	}

	function fillInDate(id){
		var mainDate = document.getElementById(id);
		var incomingDay = document.getElementById(id + 'Day');
		var incomingYearMonth = document.getElementById(id + 'YearMonth');
		var result = false;
		if(mainDate && incomingDay && incomingYearMonth){
			if(incomingYearMonth.value && incomingDay.value)
				mainDate.value = incomingYearMonth.value + '-' + incomingDay.value;
			else
				mainDate.value = "";
			
			result = true;
		}
		return result;
	}
