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";
		}}
		
	}else if (dateFieldID == "checkOutDate"){
		var chosenDateFormat = new Date(dateTime).format("mm/dd/yy");
		$("#checkOutDate").val(chosenDateFormat).css("color","#000000");
	}else if( dateFieldID == "checkInDate"){
		var df = new Date(dateTime).format("mm/dd/yy");
		$("#checkInDate").val(df).css("color","#000000");	
		
		if(validationCheckInOutDate()){
		}else{
			var dfTemp = DateAdd("d",1,new Date(dateTime)).format("mm/dd/yy");
			$("#checkOutDate").val(dfTemp).css("color","#000000");	
		}
	}
	
	hideCalendar();
	if ( get("calendarDiv") ) get("calendarDiv").style.display = "none";
}

function   DateAdd(strInterval,   NumDay,   dtDate)   {  
	  var   dtTmp   =   new   Date(dtDate);  
	  if   (isNaN(dtTmp))   dtTmp   =   new   Date();  
	  switch   (strInterval)   {  
	  case   "s":return   new   Date(Date.parse(dtTmp)   +   (1000   *   NumDay));  
	  case   "n":return   new   Date(Date.parse(dtTmp)   +   (60000   *   NumDay));  
	  case   "h":return   new   Date(Date.parse(dtTmp)   +   (3600000   *   NumDay));  
	  case   "d":return   new   Date(Date.parse(dtTmp)   +   (86400000   *   NumDay));  
	  case   "w":return   new   Date(Date.parse(dtTmp)   +   ((86400000   *   7)   *   NumDay));  
	  case   "m":return   new   Date(dtTmp.getFullYear(),   (dtTmp.getMonth())   +   NumDay,   dtTmp.getDate(),   dtTmp.getHours(),   dtTmp.getMinutes(),   dtTmp.getSeconds());  
	  case   "y":return   new   Date((dtTmp.getFullYear()   +   NumDay),   dtTmp.getMonth(),   dtTmp.getDate(),   dtTmp.getHours(),   dtTmp.getMinutes(),   dtTmp.getSeconds());  
	  }  
}   

function validationCheckInOutDate(){
	var oo = $("#checkOutDate").val();
	
	var arroo = oo.split("/");
	if(arroo[0].length < 2){  
		arroo[0] = "0"+arroo[0];
		}
	if(arroo[1].length < 2){
		arroo[1] = "0"+arroo[1];
		}
	if(arroo[2].length < 4){
		arroo[2] = "20"+arroo[2];
		}
	var asEndDate = arroo[2]+"-"+arroo[0]+"-"+arroo[1];

	var ii = $("#checkInDate").val();
    var arrii = ii.split("/");
	if(arrii[0].length < 2){
		arrii[0] = "0"+arrii[0];
		}
	if(arrii[1].length < 2){
		arrii[1] = "0"+arrii[1];
		}
	if(arrii[2].length < 4){
		arrii[2] = "20"+arrii[2];
		}
    var asStartDate = arrii[2]+"-"+arrii[0]+"-"+arrii[1];    
	
	 var miStart = Date.parse(asStartDate.replace(/\-/g, '/'));
	 var miEnd   = Date.parse(asEndDate.replace(/\-/g, '/'));

      if((miEnd-miStart)<=0){
    	  return false;
         }else{
            return true;
             }
	
}

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";}}
		
}

