// ---------------------------------------------
// Calendar
// ---------------------------------------------

YAHOO.util.Event.addListener(window, "load", loadCals);
YAHOO.util.Event.addListener("checkInDate", "keypress", hideCheckInCal, true);
YAHOO.util.Event.addListener("checkInContainer", "blur", hideCheckInCal, true);
YAHOO.util.Event.addListener("checkInDate", "focus", showCheckInCal, true);

addLoadListener(menuItemListener);
addLoadListener(navItemListener);
addLoadListener(runSlideShow);

var calCheckIn;

function loadCals() {
	if ( null != document.getElementById("checkInDate") ) {
		this.today = new Date();
		var thisMonth = this.today.getMonth() + 1;
		var thisDay = this.today.getDate();
		var thisYear = this.today.getFullYear();
	
		var checkInDate = YAHOO.widget.DateMath.add(this.today, YAHOO.widget.DateMath.DAY, 18);
		if ( document.getElementById("checkInDate").value != "" ) {
			var dates = document.getElementById("checkInDate").value.split("-");
			checkInDate.setFullYear(dates[2],dates[1]-1,dates[0]);
		}
		var checkInMonth = checkInDate.getMonth() + 1;
		var checkInDay = checkInDate.getDate();
		var checkInYear = checkInDate.getFullYear();
		calCheckIn = new YAHOO.widget.Calendar("calCheckIn","checkInContainer",checkInMonth+"/"+checkInYear,checkInMonth+"/"+checkInDay+"/"+checkInYear);
		calCheckIn.title = "Select a check in date";
		calCheckIn.onSelect = setCheckInDate;
		calCheckIn.minDate = this.today;
		calCheckIn.render();
		calCheckIn.oDomContainer.style.display = "none";
	
		calCheckIn.select(checkInDate);
	}
}

function showCheckInCal() {
	linkElement = this;
	var pos = YAHOO.util.Dom.getXY(linkElement);
	if ( calCheckIn.oDomContainer.style.display=='block' ) calCheckIn.oDomContainer.style.display='none';
	else calCheckIn.oDomContainer.style.display='block';
	YAHOO.util.Dom.setXY(calCheckIn.oDomContainer, [pos[0],pos[1]+linkElement.offsetHeight+1]);
}

function hideCheckInCal() {
	if ( calCheckIn.oDomContainer.style.display == 'block' ) 
		calCheckIn.oDomContainer.style.display='none';
}

function setCheckInDate() {
	var date = calCheckIn.getSelectedDates()[0];
	document.getElementById('checkInDate').value = leadingZero(date.getDate()) + "-" + leadingZero(date.getMonth() + 1)+ "-" + date.getFullYear();
	calCheckIn.pageDate = date;
	calCheckIn.oDomContainer.style.display = "none";
}

function leadingZero(x){
	// adds leading zero to dates
	return (x>9)?x:'0'+x;
}

function menuItemListener() {
	if ( document.getElementById("menu") ) 
		attachEventListener(document.getElementById("menu"), "click", toggleChildren, true);
	if ( document.getElementById("nav") ) 
	attachEventListener(document.getElementById("nav"), "click", toggleChildren, true);
}

function toggleChildren(e) {
	var thisLink = getEventTarget(e);
	var childList = thisLink.parentNode.getElementsByTagName("ul");
	if ( thisLink.className.indexOf("nolink") > -1 ) {
		if ( childList[0] ) {
			if ( childList[0].style.display == "block" ) {
				childList[0].style.display = "none";
				YAHOO.util.Event.stopEvent(e);
			} else {
				childList[0].style.display = "block";
				YAHOO.util.Event.stopEvent(e);
			}
		}
	}

//	alert(thisLink.id);

/*
	if ( thisLink.className.indexOf("nolink") > -1 ) {
		var topLevelLists = document.getElementById("menu").getElementsByTagName("li");
		for ( var i=0; i<topLevelLists.length; i++ ) {
			if ( topLevelLists[i].getElementsByTagName("a")[0].className.indexOf("nolink") < 0 ) {
				for ( var j=0; j<topLevelLists[i].getElementsByTagName("ul").length; j++ ) {
					topLevelLists[i].getElementsByTagName("ul")[j].style.display = 'none';
				}
			}
		}
		var childList = thisLink.parentNode.getElementsByTagName("ul");
		for ( var i=0; i<childList.length; i++ ) {
			if ( childList[i].style.display == 'block' ) {
				childList[i].style.display = 'none';
				YAHOO.util.Event.stopEvent(e);
			} else {
				childList[i].style.display = 'block';
				YAHOO.util.Event.stopEvent(e);
			}
		}
	}
*/
}

function navItemListener() {
	attachEventListener(document.getElementById("nav"), "click", clickAction, true);
}

function clickAction(e) {
	var thisLink = getEventTarget(e);
	var childList = thisLink.parentNode.getElementsByTagName("ul");
	if ( thisLink.className.indexOf("nolink") > -1 ) {
		if ( childList[0] ) {
			YAHOO.util.Event.stopEvent(e);
		}
	}
}

function ImageBuilder ( img, caption ) {
	this.img = img;
	this.caption = caption;
}

function rotateImgs(imgDivID, imagesArray) {
	if ( isArray(imagesArray) ) {
		while ( i < imagesArray.length ) {
			window.setTimeout("swapImg('"+imgDivID+"','"+imagesArray[i].img+"','"+imagesArray[i].caption+"')", 3000);	
			if ( i==(imagesArray.length-1) ) i=0;
		}
	}
}

function swapImg(imgDivID,src,caption) {
	if ( document.getElementById(imgDivID) ) {
		document.getElementById(imgDivID).src = src;
		document.getElementById(imgDivID).alt = caption;
		document.getElementById(imgDivID).title =caption;
	}
}

function isArray(obj) {
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}

function setSelectValue(sObj,v){
	for(var i=0;i<=sObj.options.length;i++){
		if(sObj.options[i].value==v){
			sObj.options[i].selected=true;
			sObj.value=v;
			break;
		}
	}
	return;
}

function getSelectValue(sObj){
	for(var i=0;i<=sObj.options.length;i++){
		if(sObj.options[i].selected==true){
			return sObj.options[i].value;
		}
	}
	return;
}

var monthDays = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

function getDayCount(m,y){
	return ((m==1)&&((y%400==0)||((y%4==0)&&(y%100!=0))))?29:monthDays[m];
}

function getMonthNumber(s){
	if(s=="Jan"){return 1;}
	else if(s=="Jan"){return 1;}
	else if(s=="Feb"){return 2;}
	else if(s=="Mar"){return 3;}
	else if(s=="Apr"){return 4;}
	else if(s=="May"){return 5;}
	else if(s=="Jun"){return 6;}
	else if(s=="Jul"){return 7;}
	else if(s=="Aug"){return 8;}
	else if(s=="Sep"){return 9;}
	else if(s=="Oct"){return 10;}
	else if(s=="Nov"){return 11;}
	else if(s=="Dec"){return 12;}
	return -1;
}

function searchPop() {
	window1=window.open("","popupWindow", "width=640, height=480, left=0, top=0, menubar=yes, status=yes, location=yes, toolbar=yes, scrollbars=yes, resizable=yes, directories=yes")
}

function validateForm() {
	var r, re, str;
	if (document.idFareSearchForm.idDepartureCity.value == '' || document.idFareSearchForm.idDestinationCity.value == '') {
	if (document.idFareSearchForm.idInitType.value == 'ORI' || document.idFareSearchForm.idInitType.value == 'EUR' || document.idFareSearchForm.idInitType.value == 'SP3' || document.idFareSearchForm.idInitType.value == 'STH' || document.idFareSearchForm.idInitType.value == 'NTH') {      
		return true;    
	} 
		else {              
			alert('You must specify a departure city and destination city');
			return false;
		}
	} else {
		str = document.idFareSearchForm.idDepartureCity.value;
		re = /[^\w\s]+/;
		r = str.match(re);    
		if (r) {
			alert('Departure City must be only alphanumeric characters');
			return false;
		}
		str = document.idFareSearchForm.idDestinationCity.value;
		r = str.match(re);
		if (r) {
			alert('Destination City must be only alphanumeric characters');
			return false;
		}
		var dObj, mObj, yObj;
		var dValue, mValue, yValue;
		// Check the date
		dObj=document.getElementById("idDepartureDay");
		mObj=document.getElementById("idDepartureMonth");
		yObj=document.getElementById("idDepartureYear");
		dValue=getSelectValue(dObj);
		mValue=getSelectValue(mObj);
		mValue=getMonthNumber(mValue);
		yValue="20"+getSelectValue(yObj);
		var dayCount=getDayCount(mValue,yValue);
		if(dValue>dayCount){
			setSelectValue(dObj,dayCount);
		}
		// Check the date
		dObj=document.getElementById("idReturnDay");
		mObj=document.getElementById("idReturnMonth");
		yObj=document.getElementById("idReturnYear");
		dValue=getSelectValue(dObj);
		mValue=getSelectValue(mObj);
		mValue=getMonthNumber(mValue);
		yValue="20"+getSelectValue(yObj);
		var dayCount=getDayCount(mValue,yValue);
		if(dValue>dayCount){
			setSelectValue(dObj,dayCount);
		}
		return true;
	}
}

function flightsTabSelect(selectedTab) {
	var tabs = new Array('tabd', 'tabi');	
	var divs = new Array('flightd', 'flighti');	
	for ( var i=0; i<tabs.length; i++ ) {
		if ( selectedTab == tabs[i] ) {
			document.getElementById(divs[i]).style.visibility = "visible"; 
			document.getElementById(divs[i]).style.display = "block"; 
			document.getElementById(tabs[i]).className = "selected";
		} else {
			document.getElementById(divs[i]).style.visibility = "hidden"; 
			document.getElementById(divs[i]).style.display = "none"; 
			document.getElementById(tabs[i]).className = "";
		}
	}
	return false;
}

function searchArnold() {
	window1=window.open("","popupWindow", "width=640, height=480, left=0, top=0, menubar=yes, status=yes, location=no, toolbar=yes, scrollbars=yes, resizable=yes, directories=yes")
}

// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header
// NS4-6,IE4-6
// Fade effect only in IE; degrades gracefully

// =======================================
// set the following variables
// =======================================

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 3000

// Duration of crossfade (seconds)
var crossFadeDuration = 1
// Specify the image files

// =======================================
// do not edit anything below this line
// =======================================

var t
var j = 0

var imagesArray = new Array();

/*
var preLoad = new Array()
if ( isArray(imagesArray) ) {
	var p = imagesArray.length
	alert(imagesArray.length);
	for (i = 0; i < p; i++){
		 preLoad[i] = new Image()
		 preLoad[i].src = imagesArray[i]
	}
}
*/

function runSlideShow(){
	var p = imagesArray.length;
	if ( imagesArray.length > 0 && document.getElementById("img") ) {
   if (document.all){
      document.getElementById("img").style.filter="blendTrans(duration=2)"
      document.getElementById("img").style.filter="blendTrans(duration=crossFadeDuration)"
      document.getElementById("img").filters.blendTrans.Apply()      
   }
   document.getElementById("img").src = imagesArray[j];
   if (document.all){
      document.getElementById("img").filters.blendTrans.Play()
   }
   j = j + 1
   if (j > (p-1)) j=0
   t = setTimeout('runSlideShow()', slideShowSpeed)
	}
}