var numMSPerDay = 86400000;
var monthsArray = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var daysArray = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

Date.prototype.addDay = function(numDays) {
	if (!numDays) numDays = 1; // if specific number not specified add one day
	var numHours = this.getHours() + 0;
	this.setTime( this.getTime() + numMSPerDay*numDays );
	this.setHours(numHours);
};

Date.prototype.clone = function() {
	return new Date(this.getTime());
};

Date.prototype.subtractDay = function() {
	var numHours = this.getHours();
	this.setTime( this.getTime() - numMSPerDay );
	this.setHours(numHours);
};

Date.prototype.getFirstDayOfMonth = function() {
	var newDate = new Date( this.getTime() );
	newDate.setDate(15); // avoid issues with diff # of days in month
	newDate.setHours(12);
	while ( newDate.getDate() != 1 ) newDate.subtractDay();
	return newDate;
}

Date.prototype.getLastDayOfMonth = function() {
	var newDate = new Date( this.getTime() );
	newDate.setDate(15); // avoid issues with diff # of days in month
	newDate.setHours(12);
	newDate.addDay();
	while ( newDate.getDate() > 1 ) newDate.addDay();
	newDate.subtractDay();
	return newDate;
}

Date.prototype.format = function(syntax) {
	var month = this.getMonth() + 1;
	if ( month <= 9 && syntax.indexOf("0m") >= 0 ) month = "0" + month;
	var date = this.getDate();
	if ( date <= 9 && syntax.indexOf("0d") >= 0 ) date = "0" + date;
	syntax = syntax.replace("DD",daysArray[this.getDay()]);
	syntax = syntax.replace("MM",monthsArray[this.getMonth()]);
	syntax = syntax.replace("mm",month);
	syntax = syntax.replace("0m",month);
	syntax = syntax.replace("dd",date);
	syntax = syntax.replace("0d",date);
	syntax = syntax.replace("yyyy",this.getFullYear());
	var shortenedYear = (this.getFullYear() + "").substring(2);
	syntax = syntax.replace("yy",shortenedYear);
	return syntax;
}

Date.prototype.getFullMonthName = function() {
	return monthsArray[ this.getMonth() ];
}

function validatedDate(dateString) {
  	try {
		var now = getCurrentDateRef();now.setHours(0);now.setMinutes(0);now.setSeconds(0);now.setMilliseconds(0);
		dateString = dateString.replace(/-/g,"/");
		dateString = dateString.replace(/\./g,"/");
		var dataFields = dateString.split("/");
		var myMonthStr = parseInt(dataFields[0],10);
		var myDayStr = parseInt(dataFields[1],10);
		var myYearStr;
		if ( dataFields.length == 3 ) {
			myYearStr = parseInt(dataFields[2].replace(/^0/g,""),10);
		}
		if ( dataFields.length == 2 ) {
			myYearStr = now.getFullYear();
		}
		if ( myYearStr < 100 ) myYearStr += 2000;
		var myDate = new Date(myYearStr, myMonthStr - 1, myDayStr);
		//alert(myDate + "-" + now);
		if ( myDate < now ) myDate.setYear( myDate.getFullYear() + 1 );
		myDate.setHours(12); // to resolve Daylight Savings Time issues
		//alert(myDate);
		if ( ( myDate.getMonth() + 1 ) != myMonthStr ) return null;
		else return myDate;
	}
	catch(e) { return null };
}

/***********************************************/

var dateOne = "outboundDate";
var dateTwo = "inboundDate";

function chooseDate(dateFieldID,dateTime) {
	var chosenDate = new Date(dateTime);
	if ( dateFieldID == "outboundDate" ) {
		SearchForm.setOutboundDate(chosenDate,true);
		if(get("searchBox")!= null && (get("searchBox").className == "roundTrip" || get("searchBox").className == "oneWayTrip")){
		
		if(get("outboundFlexibility")!=null){
			get("outboundFlexibility").style.display = "block";
		}
		}
	}
	else if ( dateFieldID == "inboundDate" ) {
		SearchForm.setInboundDate(chosenDate,true);
		if(get("searchBox")!= null && get("searchBox").className == "roundTrip"){
		if(get("inboundFlexibility")!=null){
			get("inboundFlexibility").style.display = "block";
		}}
		
	}
	hideCalendar();
	if ( get("calendarDiv") ) get("calendarDiv").style.display = "none";
}

function hideCalendar() {
	get("calendarDiv").style.display = "none";
	if(get("searchBox")!= null && get("searchBox").className == "roundTrip"){
		if (get("inboundFlexibility")!=null){
			get("inboundFlexibility").style.display = "block";
			}}
	if(get("searchBox")!= null && (get("searchBox").className == "roundTrip" || get("searchBox").className == "oneWay")){
		if (get("outboundFlexibility")!=null){
		get("outboundFlexibility").style.display = "block";}}
		
}
