function init(){
	debug=true;// alert(errors)
	DeactivateJavaScript = true;
	DAYSBETWEENINANDOUT = (7*24*60*60*1000);
	MAXBOOKINGDAYS = (339*24*60*60*1000);
    
	days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);	
	
	// specify month names
	if(monthNames)
	    names = monthNames;
	else
	    names = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	
	// specify day names
	if(dayNames)
	    dow = dayNames;
	else
	    dow = new Array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
	
	weekDay = new Array(6,0,1,2,3,4,5); //config for first day of week

	// today = new Date();
	// today = new Date(today.getFullYear(),today.getMonth(),today.getDate());
	// Hack for Swiss Booking after 26.10.08
	// today = new Date(2008,09,26);

	// specify active dates
	//selDates1 = null;//activatedDates1;
	//selDates2 = null;//activatedDates2;
	
	// specify calendar titles
	title1 = "Departure date";
	if(calTitle1) title1 = calTitle1;
	title2 = "Return date";
	if(calTitle2) title2 = calTitle2;
	
	firstTimeOpened = true;
	
	
}
//start the appl
function run(){
	//outDate = new Date(getDateFromOpener(document.getElementById(clientID + "DEPMONTH1").value-1,(document.getElementById(clientID + "DEPDAY1").value)));
	//if(outDate < new Date()) outDate = new Date();
	//if(show2Calendars) inDate = new Date(getDateFromOpener(document.getElementById(clientID + "DEPMONTH2").value-1,(document.getElementById(clientID + "DEPDAY2").value)));
	// outDate = new Date();
	// outDate = new Date();
	// inDate = new Date();
	// Hack for Swiss Booking after 26.10.08
	// outDate = new Date(2008,9,26);
	// inDate = new Date(2008,9,26+7);
	// outDate = new Date(today.getFullYear(),today.getMonth(),today.getDate());
	//inDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()+7);
	
	
	if(show2Calendars) hasChanged =false;//(inDate.getTime()-outDate.getTime()!=DAYSBETWEENINANDOUT);
	
	outBound = new Calendar("outBound",title1, outDate?outDate:(today.getTime()+(1*24*60*60*1000)));
	//outBound.activeDays = selDates1;	//specify active days
	
	// Calendar 2 (inbound)
	if(show2Calendars){
		inBound = new Calendar("inBound",title2,inDate?inDate:(today.getTime()+DAYSBETWEENINANDOUT)); 
		inBound.hasChanged = hasChanged;
		if(outBound.selectedDate > inBound.selectedDate){
			inBound.selectedDate = outBound.selectedDate;
			inBound.userDate = outBound.userDate;
		}
		outBound.child = inBound; //define observer
		DAYSBETWEENINANDOUT = inBound.selectedDate.getTime()-outBound.selectedDate.getTime();
	
	}
	
	if(minDate != null) inBound.minDate = minDate;
	if(maxDate != null) inBound.maxDate = maxDate;
	if(minStay != null) {
	    inBound.minStay = minStay;
	    DAYSBETWEENINANDOUT=(1*24*60*60*1000);
	}         //min days between out- & inbound. need parent
	if(maxStay != null) inBound.maxStay = maxStay;	//max days "*/
	
	outBound.draw(); //write the html code
}
function getDateFromOpener(month,day){
	var year = today.getFullYear();
	
	if(month<today.getMonth())
		year = (today.getFullYear()+1);
	
	return new Date(year,month,day>days[month]?days[month]:day);

}
function Calendar(instanceName,title,selectedDate){
	this.name = instanceName;
	this.title = title;
	this.hasChanged = false;

	this.selectedDate = new Date(selectedDate);
	this.userDate = new Date(selectedDate);
	this.minDate = null;
	this.maxDate = null;

	this.parentDate = null;
	this.minStay = 0;
	this.minStayDate = null;
	this.minStayWeekDay = null;
	this.maxStay = 0;
	this.maxStayDate = null;

	this.activeDays = null;
	
	this.child; //observable
}
// write the calendar
Calendar.prototype.draw=function(date,hasChanged){//both parameter are optional
	try{
		if(date && typeof(this.selectedDate)!="object" || this.selectedDate=="Invalid Date"){
			throw("draw.date is invalid");
		}
		if(date){
			date = new Date(date);
			this.selectedDate = new Date(date);
			if(hasChanged==true){
				this.hasChanged = true;
				this.userDate = new Date(date);	
			}
			//if(this.parentDate){
			//	DAYSBETWEENINANDOUT = date.getTime()-this.parentDate.getTime();
			//}
		} else {
			date = this.selectedDate;
		}
		if(this.parentDate){
			DAYSBETWEENINANDOUT = date.getTime()-this.parentDate.getTime();
		}

		var febYear = this.selectedDate.getFullYear();
		if (this.selectedDate.getMonth()==1 && (((febYear % 4 == 0) && (febYear % 100 != 0)) || (febYear % 400 == 0)) ){
			days[1] = 29; 
		} else {
			days[1] = 28;
		}
		var firstDayOfMonth = new Date(date.getFullYear(),date.getMonth(),1);
		var lastDayOfMonth = new Date(date.getFullYear(),date.getMonth(),days[date.getMonth()]);
		
		var tmpDate = new Date(firstDayOfMonth);
		tmpDate.setDate(tmpDate.getDate()-weekDay[firstDayOfMonth.getDay()]);
			
		var htmlMonthDropdown = ""; 
	
		//write the month dropdown
		var dropDownDate;
		var earliestMonth=null;
		var latestMonth = null
	
		for(var i=0;i<12;i++){
			dropDownDate = new Date(addMonth(today,i,date.getDate()));
			//if(this.activeDays && this.activeDays[dropDownDate.getMonth()] && this.activeDays[dropDownDate.getMonth()].length>0){
				htmlMonthDropdown += '<option value="'+dropDownDate+'"'+(date.getMonth()==dropDownDate.getMonth()?' selected>':'>')+names[dropDownDate.getMonth()]+' '+dropDownDate.getFullYear()+'</option>\n';
				if(earliestMonth==null)earliestMonth=dropDownDate.getMonth();
				latestMonth = dropDownDate.getMonth();
			//}
		}
	
		var htmlString = '<table border="0" cellspacing="0" cellpadding="0" width="240">\n';
		htmlString += '<tr class="caltitle"><td class="newcalendar-title" colspan="2" height="19" valign="middle">&nbsp;'+this.title + '</td>';
		htmlString += '</tr>\n'
		htmlString += '<tr class="calmonth"><td align="center" height="30" colspan="2" class="calmonthtd"><span id="'+this.name+'_prevMonth" onClick="'+this.name+'.draw('+addMonth(this.selectedDate,-1)+')" '+(earliestMonth==this.selectedDate.getMonth()?'style="visibility:hidden;"':'style="cursor:pointer;"')+'><<&nbsp;</span>\n';
		htmlString += '<select id="'+this.name+'_Month" onChange="'+this.name+'.draw(this.options[this.selectedIndex].value)">\n';
		htmlString += htmlMonthDropdown;
		htmlString += '</select>\n';
		htmlString += '<span id="'+this.name+'_nextMonth" onClick="'+this.name+'.draw('+addMonth(this.selectedDate,1)+')" '+(this.selectedDate.getMonth()==latestMonth?'style="visibility:hidden;"':'style="cursor:pointer;"')+'>&nbsp;>></span>\n';
	
		htmlString +='</td></tr>\n<tr class="calsheet"><td colspan="2" class="calsheettd">\n<table border="0" width="240" heigth="200">';
		htmlString+='<tr bgcolor="#999999">';
		
		//write the day names
		for(var i=0;i<dow.length;i++){
			htmlString+='<td class="newcalendar-dayheader" width="32">'+dow[i]+'</td>';
		}
		htmlString+='</tr>\n';
		// write days
		for(var i=0;i<42;i++){
			if(tmpDate.getDay()==1){
				htmlString+='<tr>';
			}
			//inactive dates
			if(tmpDate<firstDayOfMonth || tmpDate>lastDayOfMonth){
			    style=' class="newcalendar-othermonth"';	onMouseOver='';	onMouseOut='';	onClick='';
			}else if(tmpDate < today
				|| tmpDate > (today.getTime()+MAXBOOKINGDAYS) || (this.minDate && tmpDate < this.minDate) || (this.maxDate && tmpDate > this.maxDate) || (this.minStayDate && tmpDate < this.minStayDate) || (this.maxStayDate && tmpDate > this.maxStayDate) 
				|| !this.isActiveDay(tmpDate) || (this.parentDate && tmpDate < this.parentDate)

//|| (this.minStay && !this.minStayDay && tmpDate < (this.userDate.getTime()+(this.minStay*24*60*60*1000)))
//|| this.maxStay && tmpDate > (this.userDate.getTime()+(this.maxStay*24*60*60*1000)))

){
				style=' class="newcalendar-inactive"';	onMouseOver='';	onMouseOut='';	onClick='';
			}else if(tmpDate.getDate()==this.userDate.getDate() && tmpDate.getMonth() == this.userDate.getMonth()){ //selectedDate
				style=' class="newcalendar-selected"';	onMouseOver='';	onMouseOut='';	onClick='';
			} else if(tmpDate.getDay()==0){ //sunday
				style=' class="newcalendar-sunday"';
				onMouseOver=' onMouseOver="this.className=\'newcalendar-selected\'"';
				onMouseOut=' onMouseOut="this.className=\'newcalendar-sunday\'"';
				onClick='onClick="'+this.name+'.draw('+tmpDate.getTime()+',true)"';
			} else { //active dates
				style=' class="newcalendar-active"';
				onMouseOver=' onMouseOver="this.className=\'newcalendar-selected\'"';
				onMouseOut=' onMouseOut="this.className=\'newcalendar-active\'"';
				onClick='onClick="'+this.name+'.draw('+tmpDate.getTime()+',true)"';
			}
			htmlString += '<td'+ style + onMouseOver + onMouseOut + onClick + '>'+tmpDate.getDate()+'</td>\n';
				
			if(tmpDate.getDay()==0){
				htmlString+='</tr>\n';
			}
			tmpDate.setDate(tmpDate.getDate()+1);
	
		}
		htmlString +='</table></td></tr></table>';
		document.getElementById(this.name+"_Calendar").innerHTML = htmlString;
		this.notifyObserver(); //notify child
		exitCalendar_pb();		
	}catch(e){
		handleError(e);
	}
}
//check array[month][day] for selectedDate
Calendar.prototype.isActiveDay=function(date){
	try{
		if(!this.activeDays || this.activeDays.length==0) return true;
		var ca = this.activeDays[date.getMonth()];
		for(var i=0;i<=ca.length;i++)
			if (date.getDate() == ca[i]) {
				return true;
				break;
			}
		return false;
	} catch(e){
		if(debug)alert("Calendar.isActiveDay() throw\n"+e);
	}
	return true;
}
// notify child
Calendar.prototype.notifyObserver=function(){
	try{
		if(this.child)
			this.child.update(this);
	}catch(e){
		if(debug)alert("Calendar.notifyObserver() throw\n"+e);
		//continue without child updates, maybe without child.draw()
		this.child = null;
	}
}
//good moring child, do someting
Calendar.prototype.update=function(parent){
	//change here the child behaviour
	this.parentDate = new Date(parent.userDate);
	
	if(parent.hasChanged){
		parent.hasChanged=false;
		//DAYSBETWEENINANDOUT = this.userDate.getTime()-parent.userDate.getTime();
		if(parent.selectedDate>this.selectedDate){
			this.selectedDate = new Date(parent.selectedDate.getTime()+DAYSBETWEENINANDOUT);
			this.userDate = this.selectedDate;
		}
	}
	
	if(this.minStay)this.minStayDate = new Date(parent.selectedDate.getTime()+(this.minStay*24*60*60*1000));
	if(this.maxStay)this.maxStayDate = new Date(parent.selectedDate.getTime()+(this.maxStay*24*60*60*1000));	
	this.draw();
	
}
//return new Date
function addMonth(datum,value,selectedDay){
	try{
		var tmpDate = new Date(datum);
		if(typeof(tmpDate)!="object" || tmpDate=="Invalid Date"){
			alert("ppmm");
			throw("tmpDate is not a Date");
		}
		var day = !selectedDay?tmpDate.getDate():selectedDay;
		var month = (tmpDate.getMonth()+value);
		if(month>11)month-=12;
		if(value==0){
			tmpDate.setDate(day>days[month]?days[month]:day);	
		} else {
			tmpDate.setMonth((tmpDate.getMonth()+value),day>days[month]?days[month]:day);
		}
		return tmpDate.getTime();
	}catch(e){
		throw("addMonth() throw\n"+e);
	}
}
function handleError(e){
	if(debug)alert("Calendar.draw() throw\n"+e);
	//set error screen
	try{document.getElementById("error").style.visibility="visible";}catch(e){}
}

function exitCalendar(){		

	document.getElementById(clientID + "DEPDAY1").selectedIndex = outBound.userDate.getDate()-1;
	document.getElementById(clientID + "DEPMONTH1").selectedIndex = outBound.userDate.getMonth();
	if(show2Calendars)document.getElementById(clientID + "DEPDAY2").selectedIndex = inBound.userDate.getDate()-1;
	if(show2Calendars)document.getElementById(clientID + "DEPMONTH2").selectedIndex = inBound.userDate.getMonth();
	
			
	document.getElementById("DivCalendar").style.display = "none";
	HideControls(false);
}

function exitCalendar_pb(){		

	document.getElementById(clientID + "DEPDAY1").selectedIndex = outBound.userDate.getDate()-1;
	document.getElementById(clientID + "DEPMONTH1").selectedIndex = outBound.userDate.getMonth();
	if(show2Calendars)document.getElementById(clientID + "DEPDAY2").selectedIndex = inBound.userDate.getDate()-1;
	if(show2Calendars)document.getElementById(clientID + "DEPMONTH2").selectedIndex = inBound.userDate.getMonth();
	
			
	//document.getElementById("DivCalendar").style.display = "none";
	//HideControls(false);
}


function displayCalendar()
{
    if(document.getElementById('DivCalendar').style.display == "none")
    {
        document.getElementById('DivCalendar').style.display = 'block';
        run();
        if(firstTimeOpened)
        {
            document.getElementById('DivCalendar').style.left = document.getElementById('DivCalendar').offsetLeft + LeftOffset;
            document.getElementById('DivCalendar').style.top = document.getElementById('DivCalendar').offsetTop + TopOffset;
            firstTimeOpened = false;
        }
        HideControls(true);
    }
    else
    {
        document.getElementById('DivCalendar').style.display = "none";
        HideControls(false);
    }
}

// For IE, hides any select-lists that are behind the calendar
function HideControls(hide) 
{ 
   try
   {
        if (navigator.appName == 'Microsoft Internet Explorer') 
        {
          var CalDiv = document.getElementById("DivCalendar");
          var CalLeftX = GetTagPixels(CalDiv,"LEFT");
          var CalRightX = CalLeftX + CalDiv.offsetWidth;
          var CalTopY = GetTagPixels(CalDiv,"TOP");
          var CalBottomY; 
          
          //if(show2Calendars) CalBottomY = CalTopY + 500;
          if(show2Calendars) CalBottomY = CalTopY + 500;
          else CalBottomY = CalTopY + 250;
          
          var FoundCalInput = false;
          
          for (var j=0;j<document.forms.length;j++) 
          {
             for (var i=0;i<document.forms[j].elements.length;i++) 
             {
                if (typeof document.forms[j].elements[i].type == 'string') 
                {
                   if ((document.forms[j].elements[i].type == 'hidden'))
                   {
                      FoundCalInput = true;
                   }
                   if (FoundCalInput) 
                   {
                      if (hide == true && document.forms[j].elements[i].type.substr(0,6) == 'select' && document.forms[j].elements[i].id != 'outBound_Month' && document.forms[j].elements[i].id != 'inBound_Month') 
                      {
                         ListTopY = GetTagPixels(document.forms[j].elements[i], 'TOP');
                         if (ListTopY < CalBottomY) 
                         {
                            if (BehindCal(document.forms[j].elements[i], CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY)) 
                            {
                               document.forms[j].elements[i].style.visibility = 'hidden';
                            }
                         }
                         else break;
                      }
                      else if(hide == false && document.forms[j].elements[i].type.substr(0,6) == 'select')
                      {
                         document.forms[j].elements[i].style.visibility = 'visible';
                      }
                   }
                }
             }
          }
		  	if(hide == true){
				document.getElementById("DivCalendar").style.visibility = 'visible';
			}else{
				document.getElementById("DivCalendar").style.visibility = 'hidden';
			}
        }
		if(hide == true){
			document.getElementById("DivCalendar").style.visibility = 'visible';
			document.getElementById(clientID + "DEPDAY1").disabled = true;
			document.getElementById(clientID + "DEPMONTH1").disabled = true;
			document.getElementById(clientID + "DEPDAY2").disabled = true;
			document.getElementById(clientID + "DEPMONTH2").disabled = true;
		}else{
			document.getElementById("DivCalendar").style.visibility = 'hidden';
			document.getElementById(clientID + "DEPDAY1").disabled = false;
			document.getElementById(clientID + "DEPMONTH1").disabled = false;
			document.getElementById(clientID + "DEPDAY2").disabled = false;
			document.getElementById(clientID + "DEPMONTH2").disabled = false;
		}
   }
   catch(e)
   {
   }
}

// Gets the absolute pixel position of the supplied element
function GetTagPixels(StartTag, Direction) 
{
   var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) 
   {
      StartTag = StartTag.offsetParent;
      PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   }
   return PixelAmt;
}

// Is the specified select-list behind the calendar?
function BehindCal(SelectList, CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY) 
{
   var ListLeftX = GetTagPixels(SelectList, 'LEFT');
   var ListRightX = ListLeftX + SelectList.offsetWidth;
   var ListBottomY = ListTopY + SelectList.offsetHeight;
   return (((ListTopY < CalBottomY) && (ListBottomY > CalTopY)) && ((ListLeftX < CalRightX) && (ListRightX > CalLeftX)));
}

function isActive(date, activatedDates){
	try{
		if(!activatedDates || activatedDates.length==0) return false;
		var ca = activatedDates[date.getMonth()];
		for(var i=0;i<=ca.length;i++)
			if (date.getDate() == ca[i]) {
				return true;
				break;
			}
		return false;
	} catch(e){}
	return true;
}

function checkRet(Object) {
	if (Object.checked == true) {
		document.getElementById("inBound_Month").disabled = true;
		document.getElementById("inBound_Calendar").className = "noinbound";
	}
	else {
		document.getElementById("inBound_Month").disabled = false;
		document.getElementById("inBound_Calendar").className = "";
	}
}


function submitEnquiry() {
	document.getElementById("IDDepart").value = "" + outBound.userDate.getDate() + "-" + (outBound.userDate.getMonth()+1) + "-" + outBound.userDate.getFullYear();
	document.getElementById("IDInbound").value = "" + inBound.userDate.getDate() + "-" + (inBound.userDate.getMonth()+1) + "-" + inBound.userDate.getFullYear();
	// alert ("Transferring to Swiss Engine [code:" + document.getElementById("IDreverse").checked + "], please accept and continue");
	if ((document.getElementById("IDreverse").checked == true) || (document.getElementById("IDreverse").value == 1)) {
		document.getElementById("IDOrigin").value = document.getElementById("IDmyDestination").value;
		document.getElementById("IDDestination").value = document.getElementById("IDmyOrigin").value;
	} else {
		document.getElementById("IDOrigin").value = document.getElementById("IDmyOrigin").value;
		document.getElementById("IDDestination").value = document.getElementById("IDmyDestination").value;
	}
	// alert ("Transferring to Swiss Engine [code:" + document.getElementById("onewayinp").checked + "], please accept and continue");
	if (document.getElementById("onewayinp").checked == true) {
		document.getElementById("IDtriptype").value = "RadioButtonOW";
	}
	
	document.swissibe.submit();
}
