// Checks if browser is Netscape 2.0x since the options array properties don't work with Netscape 2.0x
function isBrowserSupp() {
    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    } else {
        return true;
    }

    return true;
}

 function isLeapYear( yrStr ) {
    var leapYear = false;
    var year = parseInt( yrStr, 10 );
    // every fourth year is a leap year
    if ( year % 4 == 0 ) {
        leapYear = true;
        // unless it's a multiple of 100
        if( year % 100 == 0 ) {
            leapYear = false;
            // unless it's a multiple of 400
            if( year % 400 == 0 ) {
                leapYear=true;
            }
        }
    }
    return leapYear;
}

function getDaysInMonth( mthIdx, YrStr ) {
    // all the rest have 31
    var maxDays = 31
    // expect Feb. (of course)
    if( mthIdx == 1 ) {
        if( isLeapYear( YrStr ) ) {
            maxDays=29;
        } else {
            maxDays=28;
        }
    }

    // thirty days hath...
    if( mthIdx == 3 || mthIdx == 5 || mthIdx == 8 || mthIdx == 10 ) {
        maxDays=30;
    }
    return maxDays;
}

//the function which does some magic to the date fields
// return non-zero if it is the last day of the month
function adjustDate( mthIdx, Dt ) {
    var value = 0;

    var today = new Date()
    var theYear = parseInt( today.getFullYear(), 10 )

    if( mthIdx < today.getMonth() ) {
        theYear = ( parseInt( today.getFullYear(), 10 ) + 1 )
    }
    if( theYear < 100 ) {
        theYear = "19" + theYear
    } else {
        if( ( theYear - 100 ) < 10 ) {
            theYear = "0" + ( theYear - 100 )
        } else {
            theYear = ( theYear - 100 ) + ""
        }
        theYear = "20" + theYear
    }


    var numDays = getDaysInMonth( mthIdx, theYear );

    if( mthIdx == 1 ) {
        if( Dt.options.selectedIndex + 1 < numDays ) {
            return 0;
        } else {
            Dt.options.selectedIndex=numDays - 1;
            //check for leap year
            if( numDays == 29 ) {
                return 99;
            } else {
                return 1;
            }
        }
    }

    if( Dt.options.selectedIndex + 1 < numDays ) {
        value = 0;
    } else {
        if ( Dt.options.selectedIndex + 1 > numDays ) {
            Dt.options.selectedIndex--;
            value = 3;
        } else {
            //index is 31 or 30
            value = 2;
        }
    }
    return value;
}

//changes departure month when arrival month is changed
function amadChange( inM, inD, outM, outD ) {
    if ( !isBrowserSupp() ) {
        return;
    }

    var res = adjustDate( inM.options.selectedIndex, inD );
    if( res != 0 ) {
           outD.options.selectedIndex = 0;
           if ( outM.options.selectedIndex == 11 ) {
                outM.options.selectedIndex = 0
           } else {
                outM.options.selectedIndex=inM.options.selectedIndex + 1;
                outD.options.selectedIndex = 1;
           }
    } else {
        outM.options.selectedIndex = inM.options.selectedIndex;
        if (outD.options.selectedIndex <= inD.options.selectedIndex) {
            outD.options.selectedIndex = inD.options.selectedIndex + 2;
        }
    }
    return;
}


function dmddChange( outM, outD ) {
    if ( !isBrowserSupp() ) {
        return;
    }

    adjustDate( outM.options.selectedIndex, outD );
    return;
}

function loadDates(arrivalMonthSelect,  arrivalDaySelect, departureMonthSelect, departureDaySelect)
{
	var calendar = new Date();
	var calendar2 = new Date();
	var cal
	var cal2
	var date;
	var month;
	var year;
	var date2;
	var month2;
	var year2;

	//Here is the variable to change for to advance the arrival Date
	 var advanceArrival = 14;
	//Here is the variable to change for to advance the departure Date
	 var advanceDeparture = 16;


	//Set the arrival Days
	calendar.setDate(calendar.getDate()+ advanceArrival);
	date = calendar.getDate();
	month = calendar.getMonth();

	//Set the Departure Days
	calendar2.setDate(calendar2.getDate()+advanceDeparture);
	date2 = calendar2.getDate();
	month2 = calendar2.getMonth();

	arrivalMonthSelect.value=month;
	arrivalDaySelect.value=date;
	departureMonthSelect.value=month2;
	departureDaySelect.value=date2;
}

function update(themonth,theday){
    document.forms[fName].elements[formType+'Day'].options[theday - 1].selected = true;
    document.forms[fName].elements[formType+'Month'].options[themonth].selected = true;

    testWindow.close();
}

function loadCalendar(viewMonth) {
	testWindow.document.close();
	testWindow.document.open();

    var day_of_week = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	var month_of_year = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	var dateObj = new Date();
	var year = dateObj.getFullYear();	 // Returns year
	var month = dateObj.getMonth();  // Returns month (0-11)
	var today = dateObj.getDate();   // Returns day (1-31)
	var weekday = dateObj.getDay();  // Returns day (1-31)

	var DAYS_OF_WEEK = 7;   // "constant" for number of days in a week
	var DAYS_OF_MONTH = 31; // "constant" for number of days in a month
	var cal;                // Used for printing

	if (viewMonth < month) {
		year = year + 1;
	}

	if(year < 1900){
		year += 1900; //done to solve the problem of netscape and mozilla returning year in form 10X for years > 2000
	}

	month = viewMonth;
	dateObj.setDate(1);      // Start the calendar day at '1'
	dateObj.setMonth(month); // Start the calendar month at now
	dateObj.setFullYear((year));

	// VARIABLES FOR FORMATTING
	var TR_start = '<tr>';
	var TR_end = '</tr>';
	var highlight_start = '<td width="15" style="font-family:Arial, Helvetica, sans-serif; font-size:9px; color:#27318f;"><table cellspacing=0 border=1 bgcolor=DEDEFF bordercolor=CCCCCC><tr><td width=15><b><center>';
	var highlight_end   = '</center></td></tr></table></b>';
	var TD_start = '<td width="15" style="font-family:Arial, Helvetica, sans-serif; font-size:9px; color:#27318f;"><center>';
	var TD_end = '</center></td>';
	var prevM = month - 1;
	var nextM = month + 1;

	if (nextM == 12) {
		nextM = 0;
	}

	if (prevM == -1) {
		prevM = 11;
	}

// BEGIN CODE FOR CALENDAR
	cal =  '<html><head><title>Calendar</title></head><body>';
	cal += "<style>background: #d1d3ff; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color:#27318f;a:link{cursor: hand}a:active{cursor: hand}a:hover{cursor: hand}a:visited{cursor: hand}body{cursor: default}</style>";

    // NOTE:
    // Update the calendar.js reference below to reference where you
    // put the calendar.js file.

   // cal += '<SCRIPT SRC="calendar.js"></SCRIPT>';

    cal += '<table border="1" cellspacing="0" cellpadding="0" bordercolor="#27318f" width="175" height="130" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#27318f;"><tr><td>';
	cal += '<table border="0" cellspacing="0" cellpadding="0" width="175">' + TR_start;
	cal += '<td colspan="' + DAYS_OF_WEEK + '" bgcolor="#d1d3ff" style="font-family:Arial, Helvetica, sans-serif; font-size:9px; color:#27318f;"><center><b>';
	cal += month_of_year[month]  + '   ' + year + '</b>' + TD_end + TR_end;
	cal += TR_start;

    ///////////////////////////////////
    // DO NOT EDIT BELOW THIS POINT  //
    ///////////////////////////////////

	// LOOPS FOR EACH DAY OF WEEK
	for(index=0; index < DAYS_OF_WEEK; index++) {
        // PRINTS DAY
        cal += TD_start + day_of_week[index] + TD_end;
	}

	cal += TD_end + TR_end;
	cal += TR_start;

	// FILL IN BLANK GAPS UNTIL TODAY'S DAY
	for(index=0; index < dateObj.getDay(); index++) {
	    cal += TD_start + '  ' + TD_end;
    }

	// LOOPS FOR EACH DAY IN CALENDAR
    for(index=0; index < DAYS_OF_MONTH; index++) {
        if( dateObj.getDate() > index ) {
            // RETURNS THE NEXT DAY TO PRINT
            week_day =dateObj.getDay();

            // START NEW ROW FOR FIRST DAY OF WEEK
            if(week_day == 0) {
                cal += TR_start;
            }

            if(week_day != DAYS_OF_WEEK) {
                // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
                var day  = dateObj.getDate();

                // PRINTS DAY
                cal += TD_start + "<span onclick='window.opener.update(" + month + "," + day + ")' style='cursor:hand;font-weight:bold;background:#EFEFEF;width:18px;'>" + day + "</span>" + TD_end;
            }

            // END ROW FOR LAST DAY OF WEEK
            if(week_day == DAYS_OF_WEEK) {
                cal += TR_end;
            }
        }

        // INCREMENTS UNTIL END OF THE MONTH
        dateObj.setDate(dateObj.getDate()+1);

    }// end for loop

	cal += '</td></tr></table></table>';
    cal += '<div align="center"><a href="javascript:" onClick="window.opener.loadCalendar(' + prevM + ');"><img src="http://affiliate.travelnow.com/searchboximages/prev.gif" alt="Previous" border="0" /></a>&nbsp;';
	cal += '&nbsp;<a href="javascript:" onClick="window.opener.loadCalendar(' + nextM + ');"><img src="http://affiliate.travelnow.com/searchboximages/next.gif" border="0" alt="Next" /></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:window.close();">close</a></div>';
	cal += '<!--<div align="right"><a href="javascript:window.close();">close</a></div> -->';
	cal += '</body></html>';


	//  PRINT CALENDAR
	testWindow.document.write(cal);
}

var testWindow;
var formType;
var fName;

function openCalendar(formName,type) {
	formType=type;
	fName=formName;
	testWindow = window.open('','dates','width=190,height=170,status=no,menubar=no,toolbar=no,scrollbars=no,location=no,statusbar=no,resizable=no,dependent=no,left=200,top=225,titlebar=no');
	loadCalendar(document.forms[fName].elements[formType+'Month'].selectedIndex);
}
function setCSC(city,state,country) {
    if (city != "") {
        document.hotSearch.cityRadio2.checked=false;
        document.hotSearch.cityText.value='';
        document.hotSearch.city.value=city;
        document.hotSearch.stateProvince.value=state;
        document.hotSearch.country.value=country;
    }
}

function submitForm(form) {
    if (form.numberOfRooms.selectedIndex == 8) {
        window.location="http://www.travelnow.com/index.jsp?pageName=groups&cid=254495";
        return false;
    }
    if (document.hotSearch.cityText.value == "" && document.hotSearch.city.value == "") {
        alert('must enter city');
        return false;
    }
    if (document.hotSearch.cityText.value != "") {
        document.hotSearch.city.value=document.hotSearch.cityText.value;
        document.hotSearch.stateProvince.value='';
        document.hotSearch.country.value='';
    }
        return submitGuestInfoForm(form);
}

function submitForm2(form) {
    if (form.numberOfRooms2.selectedIndex == 8) {
        window.location="http://www.travelnow.com/index.jsp?pageName=groups&cid=254495";
        return false;
    }
	return submitGuestInfoForm2(form);
}