
function updateRadioState() {
if (document.getElementById('wdfonewayradio')!=null) {
var array=getValidatedDateArray(2);
var oneway=!isValidDate(array);
document.getElementById('wdfonewayradio').checked=oneway;
document.getElementById('wdfreturnradio').checked=!oneway;
}
}

function radioChangedOneWay() {
setDate(2,0,0,0);
updateDateState();
}

function radioChangedReturn(daysGap) {
var out=getValidatedDateArray(1);
if (!isValidDate(out)) {
var dt1=new Date();
out[0]=dt1.getFullYear();
out[1]=dt1.getMonth()+1;
out[2]=dt1.getDate();
setDate(1,out[0],out[1],out[2]);
}
var dt=new Date(out[0],out[1]-1,out[2]+daysGap,0,0,0,1);
setDateDate(2,dt);
updateDateState();
}

var routes="ALGL{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPATWU}AMKM{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMZVASDKASBWALBPALGLATWU}AMUR{ABBNABKMABTUABKIAODNAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ABBN{ABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ALKH{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNAMKMAMZVASDKASBWALBPALGLATWU}ALDU{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ALBP{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALGLATWU}AODN{ABBNABKMABTUABKIAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}AKCH{ABBNABKMABTUABKIAODNAMURAMYYAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}AMYY{ABBNABKMABTUABKIAODNAMURAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ALMN{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALKHAMKMAMZVASDKASBWALBPALGLATWU}AMZV{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMASDKASBWALBPALGLATWU}ALWY{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ATWU{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGL}ABTU{ABBNABKMABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ALBU{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ASDK{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASBWALBPALGLATWU}ABKI{ABBNABKMABTUAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ABKM{ABBNABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}ASBW{ABBNABKMABTUABKIAODNAMURAMYYAKCHAKUDALBUALDUALWYALMNALKHAMKMAMZVASDKALBPALGLATWU}AKUD{ABBNABKMABTUABKIAODNAMURAMYYAKCHALBUALDUALWYALMNALKHAMKMAMZVASDKASBWALBPALGLATWU}";
var destinations = null;
storeDests();

function storeDests() {
if(destinations!=null) return;
destinations=new Array();
var id=1;
var fld=document.getElementById('wdfdest'+id);
if(fld == null) fld=document.getElementById('wdfdest'+(++id));
while(fld!=null){
destinations[id]=new Array();
for(var i=0; i<fld.options.length; i++){
destinations[id][i*3]=fld.options[i].text;
destinations[id][i*3+1]=fld.options[i].value;
destinations[id][i*3+2]=null;
if (fld.options[i].parentNode.nodeName=='OPTGROUP') {
destinations[id][i*3+2]=fld.options[i].parentNode.label;
}
}
fld=document.getElementById('wdfdest'+(++id));
if(fld == null) fld=document.getElementById('wdfdest'+(++id));
}
}

repopulateDests();
function repopulateDests() {
var id=1;
var fld=document.getElementById('wdfdest'+id);
if(fld == null) fld=document.getElementById('wdfdest'+(++id));
while(fld!=null){
updateDests(id,fld.value);
fld=document.getElementById('wdfdest'+(++id));
if(fld == null) fld=document.getElementById('wdfdest'+(++id));
}
}

function addDests(id,fld,filter,selCode){
fld.options.length=0;
while (fld.firstChild){fld.removeChild(fld.firstChild);};

var lastgroup=null;
var oGroup=null;
for(var i=0; i<destinations[id].length; ){
var text=destinations[id][i++];
var value=destinations[id][i++];
var group=destinations[id][i++];
if(value.length==0 || filter==null || filter.indexOf(value)>=0) {

if (group!=null && lastgroup!=group){
oGroup=document.createElement('optgroup');
oGroup.label=group;
lastgroup=group;
fld.appendChild(oGroup);
}

if (navigator.userAgent.indexOf('MSIE') == -1) {
var option=document.createElement('option');
option.text=text;
option.value=value;
option.selected=(value==selCode);
if (oGroup!=null){
oGroup.appendChild(option);
}
if (oGroup==null){
fld.appendChild(option);
}
}

if (navigator.userAgent.indexOf('MSIE') > 1) {
fld.options.add(new Option(text, value, false, (value==selCode)));
}
}
}
for(var i=0; i<fld.options.length; i++) {
if (fld.options[i].value == selCode) {
fld.selectedIndex = i;
}
}
}

function updateDests(id,selDest) {
var ofld=document.getElementById('wdforigin'+id);
var fld=document.getElementById('wdfdest'+id);
if(routes==null || ofld==null || fld==null || destinations[id]==null) return;
if(ofld.value.length==0){
addDests(id,fld,null,selDest);
}else{
var matched='';
var selOrig=ofld.value;
selOrig=selOrig.substring(0,1)+selOrig.substring(selOrig.length-3)+"{";
var pos=routes.indexOf(selOrig);
if(pos>=0){
pos=pos+5;
var end=routes.indexOf("}",pos);
if(end>=0){
var dests=routes.substring(pos,end);
for(var i=0; i<dests.length; i+=4){
if(dests.charAt(i)=='A'){
matched+='#Airport.'+dests.substring(i+1,i+4);
}else if(dests.charAt(i)=='C'){
matched+='#City.'+dests.substring(i+1,i+4);
}else if(dests.charAt(i)=='R'){
matched+='#RailStation.'+dests.substring(i+1,i+4);
}
}
}
}
if(selDest==''){
if(matched.lastIndexOf('#')==0) selDest=matched.substring(1);
else selDest=fld.value;
}
addDests(id,fld,matched,selDest);
}
}

function originChanged(id) {
updateDests(id,'');
}

function destChanged(id) {
if(routes==null) return;
var baseOrig=document.getElementById('wdforigin1');
var dest=document.getElementById('wdfdest'+id).value;
var nextOrig=document.getElementById('wdforigin'+(id+1));
if (nextOrig!=null && nextOrig.value.length==0 && baseOrig!=null && baseOrig.value!=dest){
nextOrig.value=dest;
originChanged(id+1);
}
}

function replaceDefaultDestinationsAndReselect() {
var id=1;
var fld=document.getElementById('wdfdest'+id);
if (fld == null) fld=document.getElementById('wdfdest'+(++id));
while (fld!=null){
addDests(id,fld,null,fld.value);
fld=document.getElementById('wdfdest'+(++id));
if (fld == null) fld=document.getElementById('wdfdest'+(++id));
}
}

function updateDowState() {
for (var i=0; getField('wdfdow',++i)!=null || getField('wdfdow',++i)!=null; ) {
var array=getValidatedDateArray(i);
if (!isValidDate(array)) {
setDow(i,-1);
}else{
var dt=new Date(array[0],array[1]-1,array[2],0,0,0,1);
setDow(i,dt.getDay());
}
}
}

function setDow(id,dow) {
var dowFld=getField('wdfdow',id);
if (dow==-1) {
dow='';
}else{
dow=''+dow;
}
for (var i=0; i<dowFld.options.length; i++) {
if (dowFld.options[i].value==(dow)) {
dowFld.selectedIndex=i;
break;
}
}
}

function dowChanged(id,minDaysGap) {
var dow=getField('wdfdow',id).value;
if (dow.length==0) {
setDate(id,0,0,0);
}else{
dow=parseInt(dow,10);
var array=getValidatedDateArray(id);
var after=false;
if (!isValidDate(array)&&fieldExists(id-1)){
array=getValidatedDateArray(id-1);
after=true;
}
var dt=new Date();
if (isValidDate(array)){
dt=new Date(array[0],array[1]-1,array[2],0,0,0,1);
}
if (after){
dt=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+minDaysGap,0,0,0,1);
while (true){
if (dt.getDay()==dow) break;
dt=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+1,0,0,0,1);
}
}else{
if (dow==0) dow=7;
var day=(dt.getDate()-dt.getDay())+dow;
dt=new Date(dt.getFullYear(),dt.getMonth(),day,0,0,0,1);
}
setDateDate(id,dt);
}
updateDateState();
}

function getField(name,id){
return document.getElementById(name+id);
}

function fieldExists(id){
return (document.getElementById('wdfday'+id)!=null);
}

function setDateDate(id,dt) {
setDate(id,dt.getFullYear(),dt.getMonth()+1,dt.getDate());
}

function setDate(id,year,month,day) {
var daySel=getField('wdfday',id);
var mySel=getField('wdfmonthyear',id);
if (day==0) {
setField(daySel,"");
}else{
setField(daySel,leadingZero(day));
}
if (year==0 || month==0) {
setField(mySel,"");
}else{
setField(mySel,leadingZero(year+"-"+leadingZero(month)));
}
}

function leadingZero(value) {
if (value<10) value="0"+value;
return ""+value;
}

function setField(fld,val) {
for (var i=0; i<fld.options.length; i++) {
if (fld.options[i].value==val) {
fld.selectedIndex=i;
break;
}
}
}

function currentDateArray(){
var dt=new Date();
return new Array(dt.getFullYear(),dt.getMonth()+1,dt.getDate());
}

function previousValidDateArray(id) {
if (fieldExists(id-1)){
var dt=getValidatedDateArray(id-1);
if (isValidDate(dt)){
return dt;
}
}
return currentDateArray();
}

function getValidatedDateArray(id) {
var array=getDateArray(id);
var year=parseInt(array[0],10);
var month=parseInt(array[1],10);
var day=parseInt(array[2],10);
if (isNaN(year)) year=0;
if (isNaN(month)) month=0;
if (isNaN(day)) day=0;
return new Array(year,month,day);
}

function getDateArray(id) {
var day=getField('wdfday',id).value;
var my=getField('wdfmonthyear',id).value+'       ';
var month=my.substring(5,7);
var year=my.substring(0,4);
return new Array(year,month,day);
}

function isValidDate(array) {
return (array[2]!=0 && array[1]!=0 && array[0]!=0);
}

function bumpMonth(id) {
var thisArray=getValidatedDateArray(id);
var prevArray=previousValidDateArray(id);
if (thisArray[1]<=prevArray[1]) {
thisArray[1]=prevArray[1];
if (thisArray[2]<prevArray[2]) {
thisArray[1]=prevArray[1]+1;
if (thisArray[1]==13) {
thisArray[1]=1;
thisArray[0]++;
}
}
}
setDate(id,thisArray[0],thisArray[1],thisArray[2]);
}

function bumpNext(id, daysGap) {
if (fieldExists(id+1)){
var thisArray=getValidatedDateArray(id);
var nextArray=getValidatedDateArray(id+1);
if (isValidDate(nextArray)){
if ((thisArray[0]>nextArray[0]) ||
(thisArray[0]==nextArray[0] && thisArray[1]>nextArray[1]) ||
(thisArray[0]==nextArray[0] && thisArray[1]==nextArray[1] && thisArray[2]>nextArray[2])) {
var dt = new Date(thisArray[0],thisArray[1]-1,thisArray[2]+daysGap,0,0,0,1);
setDateDate(id+1,dt);
}
}
}
}

function updateDateState() {
if (self.updateRadioState) updateRadioState();
if (self.updateDowState) updateDowState();
if (self.updateTimeState) updateTimeState();
}

function dayChanged(id, daysGap) {
var day=getField('wdfday',id).value;
if (day.length==0) {
setField(getField('wdfmonthyear',id),"");
}else{
var my=getField('wdfmonthyear',id).value;
if (my.length==0 && fieldExists(id-1)) {
var prevMY=getField('wdfmonthyear',id-1).value;
setField(getField('wdfmonthyear',id),prevMY);
}
bumpMonth(id);
bumpNext(id, daysGap);
}
updateDateState();
}

function myChanged(id, daysGap) {
var my=getField('wdfmonthyear',id).value;
if (my.length==0) {
setField(getField('wdfday',id),"");
}else{
var day=getField('wdfday',id).value;
if (day.length==0){
var prev=previousValidDateArray(id);
var dt=getValidatedDateArray(id);
if (dt[1]==prev[1]){
setDate(id,dt[0],dt[1],prev[2]);
}else{
setDate(id,dt[0],dt[1],1);
}
}
bumpNext(id, daysGap);
}
updateDateState();
}

months = new Array;
months[0]='January'
months[1]='February'
months[2]='March'
months[3]='April'
months[4]='May'
months[5]='June'
months[6]='July'
months[7]='August'
months[8]='September'
months[9]='October'
months[10]='November'
months[11]='December'

var days = new Array;
days[0]='Mon';
days[1]='Tue';
days[2]='Wed';
days[3]='Thu';
days[4]='Fri';
days[5]='Sat';
days[6]='Sun';
var firstDayOfWeekOffset = 1;

var calPastMonthsCount = 1;
var calFutureMonthsCount = 1;
var calSelectableDaysOffsetStart = "0 Years, 0 Months, 0 Days";
var calSelectableDaysOffsetEnd = "1 Year, 0 Months, -1 Day";
var calCloseThisWindowText = "Close this window";
var calWinWidthPerCalendar = 222;
var calPrevImg = '<img src="/images/en/dates/itd_calprev.gif" border="0" />';
var calNextImg = '<img src="/images/en/dates/itd_calnext.gif" border="0" />';
function doCalendarPopup(popupBody, calWinWidthPerCalendar, scrollbars) {
	
var out_start = '<'+'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+ '\n'+ '<'+'html>'+ '\n'+ '<'+'head>'+ '\n'+ '<'+'title>Calendar<'+'/title>'+ '\n'+ '<'+'meta http-equiv="content-type" content="text/html; charset=UTF-8" />'+ '\n'+'<'+'style type="text/css">'+'\n'+'<'+'!'+'-'+'-'+'\n'+'@import url(/css/itd.css);\n'+'@import url(/css/style.css);\n'+'@import url(/itd/css/configured.css);\n'+'@import url(/css/project.css);\n'+'-'+'->'+'\n'+'<'+'/style>'+'\n'+ '<'+'/head>'+ '\n';
var out_end = '<'+'/html>'

calwin = window.open("","calendar","width="+calWinWidthPerCalendar+",height=200,resizable=no,scrollbars="+scrollbars);
calwin.document.write(out_start + popupBody + out_end);
calwin.document.close();
if (window.focus) {
calwin.focus();
}
}

var defTime=null;
function updateTimeState() {
if (defTime==null){
defTime=new Array("");
for (var id=0; getField('wdftime',++id)!=null || getField('wdftime',++id)!=null; ) {
defTime[id]=getField('wdftime',id).value;
}
}
for (var id=0; getField('wdftime',++id)!=null || getField('wdftime',++id)!=null; ) {
var select=getField('wdftime',id);
var array=getValidatedDateArray(id);
if (!isValidDate(array)){
setTimeField(select,"");
}
}
}

function setTimeField(fld,val) {
for (var i=0; i<fld.options.length; i++) {
if (fld.options[i].value==val) {
fld.selectedIndex=i;
break;
}
}
}

function timeChanged() {
}
