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){
	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")){
				if(document.getElementById("hotelRoomsData").innerHTML)
					document.getElementById("hotelRoomsData").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;
	}
	if(document.getElementById("load_"+classification).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();
				if(d1<d){
					alert(lang_s["error.availabilityDateIncorrectValue"]);
					return false;
				}
			}
			if(document.getElementById(classification+"_Duration").value && !document.getElementById("AvailabilityDate"+posAdd).value){
				alert(lang_s["error.selectDate"]);
				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("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();
				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){
	try{
		var strParams = "";
		var link = lang_s["URL"]+"ajax.php?grandparent=site";
		strParams += "&m=accommodation_hotels";
		strParams += "&serviceId="+id;
		switch(mode){
			case "singleHotel":
				strParams += "&a=120";
				strParams += "&startTravelDate="+document.getElementById("hotelStartTravelDate").value;
				strParams += "&Duration="+document.getElementById("hotelDuration").value;
				document.getElementById("hotelRoomsData").style.align = "center";
				document.getElementById("hotelRoomsData").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").style.display = "block";
				var objAj = new Ajax.Updater("hotelRoomsData", link + strParams);
			break;
			default:
				strParams += "&a=110";
				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){
	if(document.getElementById("hotelStartTravelDate") && !document.getElementById("hotelStartTravelDate").value){
		alert(lang_s["error.selectDate"]);
		return false;
	}
	if(document.getElementById("hotelDuration").value && !document.getElementById("hotelStartTravelDate").value){
		alert(lang_s["error.selectDate"]);
		return false;
	}

	if(document.getElementById("hotelDuration") && !document.getElementById("hotelDuration").value){
		alert(lang_s["error.durationIncorrectValue"]);
		return false;
	}

	if(document.getElementById("hotelDuration") && document.getElementById("hotelDuration").value){
		var reg=/^\d+$/;
		if(!reg.test(document.getElementById("hotelDuration").value)){
			alert(lang_s["error.durationIncorrectValue"]);
			return false;
		}
	}

	if(document.getElementById("hotelStartTravelDate").value && document.getElementById("hotelDuration").value){
		var objStringDate = new String(document.getElementById("hotelStartTravelDate").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();
		if(startDate < nowDate){
			alert(lang_s["error.availabilityDateIncorrectValue"]);
			return false;
		}
	}
	showRooms(RoomId,"singleHotel");
}

function prepareBookingForHotel(type,hotelId,url,arrivalDate,tripDuration){
	var roomsList = document.getElementsByName("roomsList_"+hotelId);
	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).selectedIndex;
		if(selIndex){
			if(requestRooms)
				requestRooms += ",";
			requestRooms += roomId;

			if(requestRoomsCount)
				requestRoomsCount += ",";
			requestRoomsCount += selIndex;

			requestPersons = requestPersons + (parseInt(document.getElementById("roomsMaxPersons_"+roomId).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
		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){
	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;
		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)
				};
		}
	}
}
