/*
changes:
2005-01-20 - in GoToSite function - changed the redirection by using 'window.location'
2005-01-23 - changed the way onreadystatechange is initialized (it should work now on Firefox too)
2005-01-24 - added functions _SetDates_, _SincronizeDDL_ for sincronizing the dates comboboxes
2005-01-26 - added SetTodayInCruises function
*/
window.onload = function()
{
    try { InitSpeedBookContent(); } catch( e ) {}
}

function _SetDates_(sIdCheckIn, sIdCheckOut)
{
	var CheckInMonth = sIdCheckIn + "_ddlMonth";
	var CheckInDay = sIdCheckIn + "_ddlDay";
	var CheckOutMonth = sIdCheckOut + "_ddlMonth";
	var CheckOutDay = sIdCheckOut + "_ddlDay";
	
	_SincronizeDDL_M(CheckInMonth, CheckOutMonth, 0);
	debugger;
	CheckDateValue('sp_Hotels_dpCheckIn', true);
	CheckDateValue('sp_Hotels_dpCheckOut', true);
	_SincronizeDDL_D(CheckInDay, CheckOutDay, CheckInMonth, CheckOutMonth, 1);
}

function _SincronizeDDL_M(CheckIn, CheckOut, advance)
{
	try
	{//debugger;
		var ddlCheckIn = document.getElementById(CheckIn);
		var ddlCheckOut = document.getElementById(CheckOut);
		if (ddlCheckIn.selectedIndex != ddlCheckIn.length-1)
			ddlCheckOut.selectedIndex = ddlCheckIn.selectedIndex + advance;
		else
		{
			if (advance == 0)
				ddlCheckOut.selectedIndex = ddlCheckIn.selectedIndex + advance;
			else
				ddlCheckOut.selectedIndex = 1;		
		}
	}catch (e){}
}

function _SincronizeDDL_D(CheckInDay, CheckOutDay, CheckInMonth, CheckOutMonth, advance)
{
	try
	{//debugger;
		var ddlCheckInDay = document.getElementById(CheckInDay);
		var ddlCheckOutDay = document.getElementById(CheckOutDay);
		var ddlCheckInMonth = document.getElementById(CheckInMonth);
		var ddlCheckOutMonth = document.getElementById(CheckOutMonth);
		
		if (ddlCheckInDay.selectedIndex != ddlCheckInDay.length-1)
			ddlCheckOutDay.selectedIndex = ddlCheckInDay.selectedIndex + advance;
		else
		{
			if (advance == 0)
				ddlCheckOutDay.selectedIndex = 1;
			else
				ddlCheckOutDay.selectedIndex = 1;	
    
			_SincronizeDDL_M(CheckInMonth, CheckOutMonth, 1);	
		}
	}catch (e){}
}
  
function InitSpeedBookContent()
{
    
  /* set Hotel Days */
	SetToday( "sp_Hotels_dpCheckIn", 0 );
	SetToday( "sp_Hotels_dpCheckOut", 1 );
	
	SetTodayInCruises();
}

function GetStaticPage( pagename, ob )
{
	try
	{
		GetStaticPageAndSpill( pagename, ob );
	}
	catch( e ) {}
}
function GetStaticPageAndSpill( pagename, ob )
{
/************ define the xmlhttp object *****************/
	var __lmt_xmlhttp
	
	if (!__lmt_xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
 		try 
 		{
  			__lmt_xmlhttp = new XMLHttpRequest();
 		} 
 		catch (e) 
		{
	  		__lmt_xmlhttp=false
	 	}
	}
	
/*********** now can work,  *****************************/
	if (__lmt_xmlhttp) 
	{
		__lmt_xmlhttp.open("GET", pagename, false);
    __lmt_xmlhttp.send(null)
      document.write(__lmt_xmlhttp.responseText);
	}
}

function GoToSite( baseurl, queryString )
{
	var aunID = "__lmt_aun";
	var apassID = "__lmt_apass";
	var qString;
	
	
	
	qString = queryString;
	
	var aun = document.getElementById( aunID );
	var apass = document.getElementById( apassID );
	if( null!=aun && null!=apass && aun.value!="" && apass.value!="" )
		qString += "&aun=" + aun.value + "&apas=" + apass.value;
	window.location= baseurl + "/" + qString;

}
/*********** Hotel Search methods ****************************/

function PerformSearchHotel( baseurl, affiliate )
{       
    var checkInMonth, checkInDay, checkOutMonth, checkOutDay;
	var rooms;
	var r1a, r2a, r3a, r1c, r2c, r3c, r1ca, r1ca1, r2ca, r2ca1, r3ca, r3ca1;
	var hotelName, brand, stars;
	var destination;	
	
	/**** temporary variables to hold controls ID's ****/
	
	var checkInMonthID = "sp_Hotels_dpCheckIn_ddlMonth";
	var checkInDayID = "sp_Hotels_dpCheckIn_ddlDay";
	var checkOutMonthID = "sp_Hotels_dpCheckOut_ddlMonth";
	var checkOutDayID = "sp_Hotels_dpCheckOut_ddlDay";
	var roomID = "sp_Hotels_ddlRooms";
	var r1aID="sp_Hotels_Room1_ddlAdult";
	var r2aID="sp_Hotels_Room2_ddlAdult";
	var r3aID="sp_Hotels_Room3_ddlAdult";
	var r1cID="sp_Hotels_Room1_ddlChildren";

	var r2cID="sp_Hotels_Room2_ddlChildren";	
	var r3cID="sp_Hotels_Room3_ddlChildren";
	var r1caID="sp_Hotels_Room1_rpChildAge__ctl0_txtChildrenAge";
	var r1ca1ID="sp_Hotels_Room1_rpChildAge__ctl1_txtChildrenAge";
	var r2caID="sp_Hotels_Room2_rpChildAge__ctl0_txtChildrenAge";
	var r2ca1ID="sp_Hotels_Room2_rpChildAge__ctl1_txtChildrenAge";
	var r3caID="sp_Hotels_Room3_rpChildAge__ctl0_txtChildrenAge";
	var r3ca1ID="sp_Hotels_Room3_rpChildAge__ctl1_txtChildrenAge";
	var hotelNameID="sp_Hotels_txtHotelName";
	var starsID="sp_Hotels_ddlCategories";
	var brandID="sp_Hotels_ddlChains";
    var destinationID="";
    var ageString = "";
    var tempage = "";

    /* ADDED BY JASON TO GET DESTINATION FIELD */
    var destinationID="sp_Hotels_hdDestinations__ctl0_txtLocation";
    var destination = document.getElementById( destinationID ).value    
    
    /* END ADDED BY JASON TO GET DESTINATION FIELD */

	/********** END of teporary variables **************/
	
	//var ciMonth = document.getElementById( checkInMonthID );
	checkInMonth = document.getElementById( checkInMonthID ).selectedIndex;
	checkInDay = document.getElementById( checkInDayID ).selectedIndex;
	checkOutMonth = document.getElementById( checkOutMonthID ).selectedIndex;
	checkOutDay = document.getElementById( checkOutDayID ).selectedIndex;
    rooms = document.getElementById( roomID ).selectedIndex + 1;
	
	/** adults **/
	if( document.getElementById( r2aID ).style.display != "none" )
		r2a = document.getElementById( r2aID ).selectedIndex + 1;
	if( document.getElementById( r3aID ).style.display != "none" )
		r3a = document.getElementById( r3aID ).selectedIndex + 1;

	/** room 1 guests **/
	r1a = document.getElementById( r1aID ).selectedIndex + 1;
	r1c = document.getElementById( r1cID ).selectedIndex;
	
    // JMC NOTES //
    // Assigns r1ca & r1ca1 values if they have are visible on the form.
    // If they are not on the form
    
    r1ca = document.getElementById( r1caID ).value;
    r1ca1 = document.getElementById( r1ca1ID ).value;    
  
    // JMC NOTES //
    // NUMBER OF ADULTS & CHILDREN ADDED TO STRING
    ageString += "&r1a=";
    ageString +=  r1a != "" ?  r1a : "2";  
    if(r1c!="")
    {
        ageString +=  "&r1c=" + r1c + "&r1ca=" ;
        //In the future take ages from r1ca and r1ca1 fields
        switch (r1c) {
            case 1:
                ageString += "11"; break;
            case 2:
                ageString += "11,11";
                r1ca1
                break;
            default: 
                ageString+= "0"; break;
        }
    }
    else
    {
        ageString += "&r1c=0&r1ca=11"
    }

    tempage = "";

	/** room 1 guests **/
	if( rooms > 1 )
	{
		r2a = document.getElementById( r2aID ).selectedIndex + 1;
		r2c = document.getElementById( r2cID ).selectedIndex;
		r2ca = document.getElementById( r2caID ).value;
        r2ca1 = document.getElementById( r2ca1ID ).value;

  
        // JMC NOTES //
        // NUMBER OF ADULTS & CHILDREN ADDED TO STRING
        ageString += r2a != "" ? "&r2a=" + r2a : "";
        ageString += r2c != 0 ? "&r2c=" + r2c + "&r2ca=" : "";
        
        // JMC NOTES //
        // if r2ca & r2ca1 contain values, then form values are added
        // else default ages are added since the user selected the number
        // of children correctly.
        switch (r2c) {
        case 1:
          tempage += r2ca != "" ? r2ca : "12"; break;
        case 2:
          tempage += r2ca != "" ? r2ca : "12";
          tempage += r2ca1 != "" ? "," + r2ca1 : ",13"; break;
        default: 
          tempage = ""; break;
    }

    ageString += tempage;
    tempage = "";
	}
	if( rooms > 2 )
	{
		r3a = document.getElementById( r3aID ).selectedIndex + 1;
		r3c = document.getElementById( r3cID ).selectedIndex;
		r3ca = document.getElementById( r3caID ).value;
		r3ca1 = document.getElementById( r3ca1ID ).value;


    // JMC NOTES //
    // NUMBER OF ADULTS & CHILDREN ADDED TO STRING
    ageString += r3a != "" ? "&r2a=" + r3a : "";
    ageString += r3c != 0 ? "&r2c=" + r3c + "&r2ca=" : "";
    
    // JMC NOTES //
    // if r3ca & r3ca1 contain values, then form values are added
    // else default ages are added since the user selected the number
    // of children correctly.
    switch (r3c) {
    case 1:
      tempage += r3ca != "" ? r3ca : "12"; break;
    case 2:
      tempage += r3ca != "" ? r3ca : "12";
      tempage += r3ca1 != "" ? "," + r3ca1 : ",13"; break;
    default: 
      tempage = ""; break;
    }

    ageString += tempage;
    tempage = "";
	}

	if( null != document.getElementById( hotelNameID ) )
		hotelName = document.getElementById( hotelNameID ).value;
	
	if( null != document.getElementById( brandID ) )
		brand = document.getElementById( brandID ).value;
		
	if( null != document.getElementById( starsID ) )
		stars = document.getElementById( starsID ).value;

	var qString = "?st=2" + "&cin=" + BuildDateString( checkInMonth, checkInDay ) + "&cout=" + BuildDateString( checkOutMonth, checkOutDay );
	qString += "&afId=" + affiliate + "&mod=2&dst=" + destination + ageString;

    qString += hotelName != null && hotelName != "" ? "&hn=" + hotelName : "";
	qString += brand != null && brand != "" ? "&brnd=" + brand : "";
	qString += "&str=5";  //just 5 star hotels
	qString += "&scf=1"; // session clear false: tells PageAffiliateLanding.aspx.cs  page not to reset the session

	GoToSite( baseurl, qString );
	
}

/******************* End Search Hotel *********************************/


/****************** Cruise Search Methods *******************/

function PerformSearchCruise(  baseurl, affiliate )
{
	var destinationID = "sp_spanModuleCruise_ddlDestinations";
	var cruiselineID = "sp_spanModuleCruise_ddlCruiseLine";
	var departID = "sp_spanModuleCruise_ddlDepartMonth";
	var lengthID = "sp_spanModuleCruise_ddlLength";
	var adultID = "sp_spanModuleCruise_ddlAdults";
	var childID = "sp_spanModuleCruise_ddlChildren";
	
	var destination, cruiseline, depart ,length ,adult ,child;
	
	destination = document.getElementById( destinationID ).options[ document.getElementById( destinationID ).selectedIndex ].value;
	cruiseline = document.getElementById( cruiselineID ).options[ document.getElementById( cruiselineID ).selectedIndex ].value;
	depart = document.getElementById( departID ).options[ document.getElementById( departID ).selectedIndex ].value;
	length = document.getElementById( lengthID ).options[ document.getElementById( lengthID ).selectedIndex ].value;
	adult = document.getElementById( adultID ).options[ document.getElementById( adultID ).selectedIndex ].value;
	child = document.getElementById( childID ).options[ document.getElementById( childID ).selectedIndex ].value;
	
	var qString	= "?st=4&mod=2" + "&afId=" + affiliate;
	qString += "&dst=" + destination;
	qString += "&crl=" + cruiseline;
	qString += "&dep=" + depart;
	qString += "&len=" + length;
	qString += "&adt=" + adult;
	qString += "&cnn=" + child;
	qString += "&scf=1";
	location.replace( baseurl + qString );
		
}

/****************** End Cruise Search *******************/


function GetMonthDayYear( month, day )
{
	var d = new Date();
	if( month < d.getMonth() )
		return d.getFullYear() + 1;
	else if( month == d.getMonth() )
	{
		if( day < d.getDate() )
			return d.getFullYear() + 1;
	}
	
	return d.getFullYear();
}

function SetToday( ddlsPrefix , addDays )
{

	var today =  new Date(new Date().setHours( 0,0,0,0 ) );

	if( addDays != null  )
		today.setDate( today.getDate() + addDays );

	var objDdlMonth = document.getElementById( ddlsPrefix + "_ddlMonth" );
	var objDdlDay = document.getElementById( ddlsPrefix + "_ddlDay" );
	
	
	objDdlDay.selectedIndex = today.getDate();
	objDdlMonth.selectedIndex = today.getMonth()+1; // JMC // objDdlMonth.selectedIndex = today.getMonth() + 1;
	
	SyncDayByMonth( objDdlDay , today );
	
}

function SetTodayInCruises(){

	var oCruiseComboDate = document.getElementById("sp_spanModuleCruise_ddlDepartMonth");
	if(oCruiseComboDate==null)return;
	var today = new Date(new Date().setHours(0,0,0,0));
	var i;
	var m = today.getMonth()+2; // next month
	if(m<10)m = "0" + m ;
	var formattedToday = m+"/"+today.getFullYear();
	for(i=0;i<oCruiseComboDate.length;i++)
		if(oCruiseComboDate[i].value==formattedToday){
			oCruiseComboDate.selectedIndex=i;
			return;
		}
}

function BuildDateString( month, day )
{
	return month + "/" + day + "/" + GetMonthDayYear( month, day );
}