var frm;
var chainId;
var domain;
function initResLauncher(){

	// *** must be set per instance ***
	domain = "https://reservations.synxis.com/xbe/rez.aspx"
	chainId = "5175";
	frm = document.resLauncher;
	// ********************************
	
	//
	// Begine creation of Month/Year <select>
	//
	var mnths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	
	currDt   = new Date();
	currMnth = currDt.getMonth();
	currYear = currDt.getFullYear();
	currDay  = currDt.getDate();
	
	var mt,yr,dy,mnthYr,len;
	
	// Iterate
	for (var mnthIdx=currMnth;mnthIdx<=currMnth+12;mnthIdx++) {
		yr = currYear;
		mt = mnthIdx;
		if (mt>11) {
			mt -= 12;
			yr += 1;
		}
		len  = frm.arrMnthYr.options.length;
		sel  = (yr==currYear && mt==currMnth) ? true : false;
		currDay = (daysInMonth(mt, yr) > currDay) ? currDay : 1;
		tmpDt = new Date(yr,mt,currDay);
		
		// Create <option>
		frm.arrMnthYr.options[len] = new Option(mnths[mt] + " " + yr,tmpDt,sel,sel);
	}
	
	// Call Other Methods
	setDays(currDt);
	setDates();
}

function daysInMonth(iMonth, iYear) {
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

// 
// Build the days of the CURRENT month (in the corresponding year)
// dt is sent as a JavaScript new Date() object
//
function setDays(dt, isflash) {
	var days,yr,mt,day,tm,len,sel,tmpDt;
	yr = dt.getFullYear();
	mt = dt.getMonth();
	day = (mt==currMnth && yr==currYear) ? dt.getDate() : 1;
	
	/***********/
	// added via alex@still-water.com
	// on flash posts we need to use the incoming date only
	
	if (isflash)
	{
		day = dt.getDate();
		currDt = dt; // set currDt to the incoming date
	}
	/***********/

	var tmpMt = mt+1;
	var tmpYr = yr;
	if (tmpMt>11) {
		var tmpMt = mt-11;
		var tmpYr = yr+1;
	}
	tmpDt = new Date(tmpYr,tmpMt);
	tm = tmpDt.getTime();
	tmpDt.setTime(tm-86400000);
	dayCnt = tmpDt.getDate();

	//
	// Begine creation of date <select>
	//
	
	// Remove all <select> data
	frm.arrDay.options.length = 0;
	// Iterate
	for (var i=day;i<=dayCnt;i++) {
		len   = frm.arrDay.options.length;
		tmpDt = new Date(yr,mt,i);
		sel   = (tmpDt == currDt) ? true : false;
		
		// Create <option>
		frm.arrDay.options[len] = new Option(i,tmpDt,sel,sel);
	}
}

function setDates(updateDays) {
	
	// When Changing Months we need to reset the
	// days to allow for for selection of any day
	if (updateDays) setDays(new Date(frm.arrMnthYr.options[frm.arrMnthYr.selectedIndex].value));
	
}

//
// Submit!
//
function launchBE() {
	//
	// Retrieve form variables
	//
	
	
	// Nights
	var nights = frm.nights.options[frm.nights.selectedIndex].value;
	// Month/Year
	var mnthYr   = new Date(frm.arrMnthYr.options[frm.arrMnthYr.selectedIndex].value);
	var thisMnth = mnthYr.getMonth();
	var thisYear = mnthYr.getFullYear();
	// Day
	var dt		= new Date(frm.arrDay.options[frm.arrDay.selectedIndex].value);
	var thisDay = dt.getDate();
	
	//
	// Create Arrival Date & Calculate Departure Date values
	//
	
	// Arrival Date
	arrivalDt = (thisMnth+1)+"/"+thisDay+"/"+thisYear;
	// Calculate
	tmpDt = new Date(thisYear,thisMnth,thisDay);
	tm    = tmpDt.getTime();
	tmpDt.setTime(tm+(86400000*nights));
	//Departure Date
	departureDt = (tmpDt.getMonth()+1)+"/"+tmpDt.getDate()+"/"+tmpDt.getFullYear();
	
	//
	// Create the URL String
	//
	
	//var features = "width=790,height=590,scrollbars=yes,resizable";
	var url = domain;
	url += "?Chain="+chainId;
	var hotelId,arrDt,depDt,adult,child,promo,group;
	hotelId = frm.hotelId.value;
	arrDt = arrivalDt;
	depDt = departureDt;
	//adult = frm.adult.value;
	//child = frm.child.value;
	promo = frm.promo.value;
	group = frm.group.value;
	url 
	+= "&Hotel="+hotelId
		+"&Arrive="+arrDt
		+"&Depart="+depDt
		//+"&Adult="+adult
		//+"&Child="+child
		+"&Promo="+promo
		+"&Group="+group
		//+"&pResvFlag=1"
		//+"&pHotelShell=1"
		+"&altdest=SJC"
		+"&Start=1";
	
	//__utmLinker(url);
	window.open(url);
}