var nowDate= new Date();
var clientTimeCMP=Math.round((nowDate-srvDate)/3600000);
if(clientTimeCMP>13 || clientTimeCMP<-13)
	nowDate=srvDate;
var rangeMonths=11;
var minSelectDate=new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),0,0,0);
var maxSelectDate=new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate()+330,0,0,0);
var monthDays= new Array();
for (i=0; i<=rangeMonths; i++){
	var month=new Date(minSelectDate.getFullYear(),minSelectDate.getMonth()+i,1,0,0,0);
	value=month.getFullYear()+'-'+month.getMonth();
	var maxDay=28;
	var checkMonth=new Date(minSelectDate.getFullYear(),minSelectDate.getMonth()+i,maxDay,0,0,0);
	while (checkMonth.getMonth()==month.getMonth()){
		maxDay++;
		checkMonth=new Date(minSelectDate.getFullYear(),minSelectDate.getMonth()+i,maxDay,0,0,0);
	}
	monthDays[value]=maxDay-1;
}

function setToCalendar(){
	var calendar = calFrom;
	var y = calendar.date.getFullYear();
	var m = calendar.date.getMonth();     // integer, 0..11
	var d = calendar.date.getDate(); 
	var minDate = new Date(y,m,d,0,0,0)
	if (calTo.date<minDate) calTo.setDate(minDate);
	else {
		calTo.refresh();
	}
	dateChangedTo(calTo);
}


function getMonthList(idSelect,activeMonth){
	var idSelectDay=idSelect.replace("month","day");
	var idDirection=idSelect.replace("_month","");
	var html='<select id="'+idSelect+'" name="'+idSelect+'" onchange="changeDay(\''+idSelectDay+'\',this.value,false); changeCal(\''+idDirection+'\');">';
	for (i=0; i<=rangeMonths; i++){
		var month=new Date(minSelectDate.getFullYear(),minSelectDate.getMonth()+i,1,0,0,0);
		var value=month.getFullYear()+'-'+month.getMonth();
		html+='<option '+((activeMonth==value)?'selected="selected"':'')+' value="'+value+'">'+Calendar._MN[month.getMonth()]+' '+month.getFullYear()+'</option>';
	}
	html += '</select>';
	return html;
}

function getDayList(idSelect, activeDate, activeDay){
	var minActiveDate=minSelectDate.getFullYear()+"-"+minSelectDate.getMonth();
	var idDirection=idSelect.replace("_day","");
	if (!activeDate) activeDate=minActiveDate;
	var html='<select id="'+idSelect+'" name="'+idSelect+'" onchange="changeCal(\''+idDirection+'\')">';
	if (activeDate==minActiveDate)
		var minMonthDay=minSelectDate.getDate();
	else
		var minMonthDay=1;
	var maxMonthDay = monthDays[activeDate];
	for (i=minMonthDay; i<=maxMonthDay; i++){
		html+='<option '+((activeDay==i)?'selected="selected"':'')+' value="'+i+'">'+i+'</option>';
	}
	html += '</select>';
	return html;
}

function changeDayOptions(idSelect, activeDate, activeDay){
	if (is_opera) {
		if (idSelect=="to_day") calTo.refresh();
		if (idSelect=="from_day") calFrom.refresh();
	}
	var minActiveDate=minSelectDate.getFullYear()+"-"+minSelectDate.getMonth();
	var maxActiveDate=maxSelectDate.getFullYear()+"-"+maxSelectDate.getMonth();
	var idDirection=idSelect.replace("_day","");
	if (!activeDate) activeDate=minActiveDate;
	var selectObj = document.getElementById(idSelect);
	selectObj.options.length = 31;

	if (idDirection=="from") {
		if (activeDate==minActiveDate)
			var minMonthDay=minSelectDate.getDate();
		else
			var minMonthDay=1;
	} else {
		if (activeDate==calFrom.date.getFullYear()+"-"+calFrom.date.getMonth())
			var minMonthDay=calFrom.date.getDate();
		else
			var minMonthDay=1;
	}
	if (activeDay<minMonthDay) activeDay=minMonthDay;
	if (activeDate==maxActiveDate)
		var maxMonthDay=maxSelectDate.getDate();
	else
		var maxMonthDay = monthDays[activeDate];
	if (activeDay>maxMonthDay) activeDay=maxMonthDay;
	var j=0;
	for (i=minMonthDay; i<=maxMonthDay; i++){
		selectObj.options[j].text=i;
		selectObj.options[j].value=i;
		j++;
	}
	selectObj.options.length = j;
	selectObj.value=activeDay;
}

function changeDay(idSelect, activeDate, activeDay){
	if (!activeDay) {
		activeDay=document.getElementById(idSelect).value;
		if (activeDay>monthDays[activeDate]) activeDay=monthDays[activeDate];
	}
	var splitDate = activeDate.split("-")
//	document.getElementById(idSelect+"_contaner").innerHTML=getDayList(idSelect, activeDate, activeDay);
	changeDayOptions(idSelect, activeDate, activeDay);
}

function changeCal(idDirection){
	var activeDate=document.getElementById(idDirection+"_month").value;
	var activeDay=document.getElementById(idDirection+"_day").value;
	var splitDate = activeDate.split("-")
	if (idDirection=="from"){
		calFrom.setDate(new Date(splitDate[0],splitDate[1],activeDay,0,0,0));
		setToCalendar();
	} else {
		calTo.setDate(new Date(splitDate[0],splitDate[1],activeDay,0,0,0));
		setToCalendar();
	}
}

function changeMonth(idSelect, activeMonth){
	document.getElementById(idSelect+"_contaner").innerHTML=getMonthList(idSelect, activeMonth);
}

function dateChangedFrom(calendar) {
	var y = calendar.date.getFullYear();
	var m = calendar.date.getMonth();     // integer, 0..11
	var d = calendar.date.getDate();      // integer, 1..31
	document.getElementById("from_month").value=y+"-"+m;
	changeDay("from_day",y+"-"+m,d)
};

function dateChangedTo(calendar) {
	var y = calendar.date.getFullYear();
	var m = calendar.date.getMonth();     // integer, 0..11
	var d = calendar.date.getDate();      // integer, 1..31
	document.getElementById("to_month").value=y+"-"+m;
	changeDay("to_day",y+"-"+m,d)
};


function getCitiesSource(){
	var html='<select id="citySource" name="origin" onchange="changeCitiesDestination(); setFlashMapCitySource()">';
	html+='<option value="">'+Lang["select_city"]+'</option>';
	if (orderCities) {
		for (var i in orderCities){
	//		if(orderCities[i]["flights"]!="")
				html+='<option value="'+i+'">'+orderCities[i]["name"]+'</option>';
		}
	}
	html += '</select>';
	return html;
}

function changeCitiesDestination(){
	var selectObj = document.getElementById('cityDestination');
	var sourceObj = document.getElementById('citySource');
	if (selectObj && sourceObj && orderCities){
		selectObj.options.length = 100;
		var j=0;
		selectObj.options[j].text=Lang["select_city"];
		selectObj.options[j].value="";
		j++;
		var destCities=new Array();
		if (orderCities[sourceObj.value] && orderCities[sourceObj.value]["flights"]!="") destCities=orderCities[sourceObj.value]["flights"].split(",");
		for (var i=0; i<destCities.length; i++){
		    selectObj.options.length = j+1;
		    if(citiesNames[destCities[i]]) {
			    selectObj.options[j].text=citiesNames[destCities[i]];
		    } else {
		        selectObj.options[j].text=destCities[i];   
		    }
			selectObj.options[j].value=destCities[i];
			j++;
		}
		if (is_opera8) selectObj.outerHTML=selectObj.outerHTML;
		selectObj.value='';
	}
	
	
}

function setFlashMapCitySource(){
    if(orderCities[document.getElementById('citySource').value]['existsFlites'] == 1){
	   window.document.flightsMap.SetVariable('_root.idCity1',document.getElementById("citySource").value);
    } else {
       window.document.flightsMap.SetVariable('_root.iClean','1');
    }
}

function setFlashMapCityDestination(){
	window.document.flightsMap.SetVariable('_root.idCity2',document.getElementById("cityDestination").value);
}

function setCitySource(value){
	if (value==" ") value="";
	document.getElementById("citySource").value=value;
	changeCitiesDestination();
}

function setCityDestination(value){
	if (value==" ") value="";
	document.getElementById("cityDestination").value=value;
}

function stringToDateTime(str){
	var tmp = new Array();
	tmp = str.split("-");
	tmp[1]++;
	tmp[2] = Number(tmp[2]);
	if (tmp[1]<10) tmp[1]="0"+tmp[1];
	if (tmp[2]<10) tmp[2]="0"+tmp[2];
	return tmp.join("-");
}

function checkOrder(){
	var departureDateStr=document.getElementById("from_month").value+"-"+document.getElementById("from_day").value;
	var returnDateStr=document.getElementById("to_month").value+"-"+document.getElementById("to_day").value;
	document.order.departureDate.value=stringToDateTime(departureDateStr);
	document.order.returnDate.value=stringToDateTime(returnDateStr);
	var spots=0;
	var seatAdult=Number(document.order.numAdults.value);
	var seatTeen=Number(document.order.numYouths.value);
	var seatChild=Number(document.order.numChildren.value);
	var seatInfant=Number(document.order.numInfants.value);
	spots = seatAdult+seatTeen+seatChild+((seatInfant>seatAdult+seatTeen)?seatInfant-seatAdult-seatTeen:0);
	if (spots>5) {
		alert(Lang["order_condition_5_places"]);
		return false;
	} else return true;
}
var GETttCitySource = false;
var GETttCityDestination = false;

function getttCitySource(){
	var html='<select id="ttCitySource" name="ttCitySource" onchange="changettCityDestination()">';
	if (GETttCitySource==undefined) GETttCitySource=false;
	html+='<option value="">'+Lang["select_city"]+'</option>';
	if (flightCities) {
		for (var i in flightCities){
			if (flightCities[i]["flights"]!='')
				html+='<option value="'+i+'" '+((GETttCitySource==i)?'selected="selected" ':'')+'>'+flightCities[i]["name"]+'</option>';
		}
	}
	html += '</select>';
	return html;
}

function changettCityDestination(){
	var selectObj = document.getElementById('ttCityDestination');
	var sourceObj = document.getElementById('ttCitySource');
	if (selectObj && sourceObj && flightCities){
		var selectedFlag=false;
		selectObj.options.length = 100;
		var j=0;
		selectObj.options[j].text=Lang["select_city"];
		selectObj.options[j].value="";
		j++;
		var destCities=new Array();
		if (flightCities[sourceObj.value] && flightCities[sourceObj.value]["flights"]!="") destCities=flightCities[sourceObj.value]["flights"].split(",");
		for (var i=0; i<destCities.length; i++){
			selectObj.options[j].text=flightCities[destCities[i]]["name"];
			selectObj.options[j].value=destCities[i];
			if (GETttCityDestination==destCities[i]) selectedFlag=true;
			j++;
		}
		selectObj.options.length = j;
		if (is_opera8) selectObj.outerHTML=selectObj.outerHTML;
		selectObj.value=(selectedFlag)?GETttCityDestination:'';
	}
}

function getttCityDestination(){
	var html='<select id="ttCityDestination" name="ttCityDestination">';
	if (GETttCitySource==undefined) GETttCitySource=false;
	if (GETttCityDestination==undefined) GETttCityDestination=false;
	html+='<option value="">'+Lang["select_city"]+'</option>';
	if (flightCities && GETttCitySource) {
		destCities=flightCities[GETttCitySource]["flights"].split(",");
		for (var i=0; i<destCities.length; i++){
			html+='<option value="'+destCities[i]+'" '+((GETttCityDestination==destCities[i])?'selected="selected" ':'')+'>'+flightCities[destCities[i]]["name"]+'</option>';
		}
	}
	html += '</select>';
	GETttCitySource='';
	GETttCityDestination='';
	return html;
}


function getTimeTable(){
	if (is_ie5_5up || is_firefox || is_chrome || is_safari) {
		var backflight = (document.getElementById('board-type-1').checked)?1:0;
		var contPath="/plugins/ajax/time-table.php?backflight="+backflight+"&ttCitySource="+document.timeTableForm.ttCitySource.value+"&ttCityDestination="+document.timeTableForm.ttCityDestination.value;
		datadoc = new DataContent('dataContent', 'dataStatusLine', document.timeTableForm.id.value, document.timeTableForm.siteid.value, contPath);
		datadoc.getDoc();
		return false;
	} else {
		return true;
	}
}

function getFlightTable(){
	if (is_ie5_5up || is_firefox || is_chrome || is_safari) {
		var departure = (document.getElementById('board-type-2').checked)?1:0;
		var contPath="/plugins/ajax/flight-table.php?departure="+departure+"&airport="+document.FlightTable.airport.value+"&date="+document.FlightTable['inboard-date'].value;
		datadoc = new DataContent('dataContent', 'dataStatusLine', document.FlightTable.id.value, document.FlightTable.siteid.value, contPath);
		datadoc.getDoc();
		return false;
	} else {
		return true;
	}
}

function checkReserve(){
	var code=document.getElementById("reserve-code").value;
	var name=document.getElementById("reserve-name").value;

	if (code=="" || name=="") {
		alert(Lang["fill_all_fields"]);
		return false;
	} if (code.length>10 || code.length<6) {
		alert(Lang["reserver_code_condition"]);
		return false;
	} else return true;
}

function getAsRTF(){
	var frm=document.getElementById('timeTableForm');
	if(!frm) return false;

	frm.target="_blank";
	frm.action="/plugins/services/rtf_time_table.php";
	frm.submit();
	frm.target="_self";
	frm.action="/time-table/";
	return true;

}

function convertTo2Sim(str){
	if (str<10) str="0"+str;
	return str;
}

var selectedDate = false;
function getTTDays(){
	var html='<select name="date" id="inboard-date">';
	var today = new Date();
	if(clientTimeCMP>12 || clientTimeCMP<-12)
		today=srvDate;
	if (selectedDate=='') selectedDate = convertTo2Sim(today.getDate())+"."+convertTo2Sim(today.getMonth()+1)+"."+today.getFullYear();
	for (i=-2; i<3; i++) {
		tempDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()+i);
		tempDay = convertTo2Sim(tempDate.getDate())+"."+convertTo2Sim(tempDate.getMonth()+1)+"."+tempDate.getFullYear();
		html+='<option value="'+tempDay+'" '+((selectedDate==tempDay)?'selected="selected" ':'')+'>'+tempDay+'</option>';
	}
	html += '</select>';
	selectedDate='';
	return html;
}

function showToCal(){
	document.getElementById('toCalPlace').style.display="block";
}

function hideToCal(){
	document.getElementById('toCalPlace').style.display="none";
}
