﻿// JScript File


// Unobtrusive JavaScript
window.onload = function(){ //Wait for the page to load.
    var gullSearchButton = getEle('SearchButton_gullSearch');

    // pass a vairable to a Unobtrusive function - fix for IE
    //gullSearchButton.onclick = (function(gullSearchButton){return function(){deleteOut(gullSearchButton);}})(gullSearchButton);
   
   var i;
   i = 0;
    gullSearchButton.onclick = (function(i){return function(){Search(i);}})(i);
    
    
    //var gullCalButton = getEle('OpenCalendarButton');
    //gullCalButton.onclick = function(){OpenCalenderWindow();};
    
     // gullSearchButton.onclick = (function(this){return function(){deleteOut(this);}})(this);
   //gullSearchButton.onclick = function(){deleteOut(this);};
   
    // http://forums.devshed.com/javascript-development-115/unobtrusive-javascript-add-onclick-function-with-a-variable-529265.html
    // http://2tbsp.com/node/91
};


function OpenCalenderWindow()
{
    var gullDaySel = getEle('ArrivalDay_date_sel_dd');
    var gullMonthSel = getEle('ArrivalMonthYear_date_sel_mm');
    //var gullPremCode = getEle('hdnPremisesCode');
    var gullCalendarID = getEle('hdnCalendarID');
    var gullBookNowID = getEle('hdnBookNowID');

    eval("window.open('/BookNow/BookNowCalendar.aspx?&selectedDay="+gullDaySel.selectedIndex+"&selectedMonth="+gullMonthSel.selectedIndex+"&CalendarCssID="+gullCalendarID.value+"', '', 'toolbar=0,scrollbars=0,location=1,statusbar=0,menubar=0,resizable=1,width=170,height=235');");
}

function deleteOut(relatedCalender)
{
    alert(relatedCalender.className);
}


function getEle(element)
{
    return document.getElementById(element);
}


function Search(relatedCalender) 
{
    var idStart = 'gullDateSelect_';

    //var gullLocation =  getEle('locationSelect_gullLocationSelect');
    var gullLocation =  getEle('hdnIsBookNowGroup');
    var gullDaySel = getEle('ArrivalDay_date_sel_dd');
    var gullDateSel = getEle('CalendarButton_date_sel');
    var gullMonthSel = getEle('ArrivalMonthYear_date_sel_mm');
    var gullNumAdults = getEle('Adults_gullAdultsDDL');
    var gullNumChildren = getEle('Children_gullChildrenDDL');
    var gullNumInfants = getEle('Infants_gullInfantsDDL');
    var gullNumNights = getEle('Nights_gullNightsDDL');
    var gullBookingUrl = getEle('hdnBookingURL');
    var gullAffiliateID = getEle('hdnAffiliateID');
    var gullProviderID = getEle('hdnProviderID');
    var gullPremisesCode = getEle('hdnPremisesCode');
    var gullAffiliateVisitID = getEle('hdnAffiliateVisitID');
    var gullRedirectType = getEle('hdnRedirectType');
    
    var gullYear = gullDateSel.value;
	var myStr = gullMonthSel.value.split("|");
	var gullYear = myStr[0];
	var gullMonth = myStr[1];
	
	var gullChild; 
	if(gullNumChildren == undefined)
	{gullChild = 0;}
	else{gullChild = gullNumChildren.value;}
	var gullInfants;
	if(gullNumInfants == undefined)
	{gullInfants = 0;}
	else
	{gullInfants = gullNumInfants.value;}
	 
	if(isValidDate(gullDaySel.value,gullMonth-1,gullYear))
	{
		//variable to hold our deepLink
		var deepLink
		
		//build the URL for deepLinking
		deepLink = gullBookingUrl.value 	
		deepLink = deepLink + "?affiliateid=" + gullAffiliateID.value 	
		deepLink = deepLink + "&provID=" + gullProviderID.value
		// if gullLocation exists then this is a group book now so
		// we need to add the extra arguments
		if(gullLocation)
		{
		    var gullAccomType =  getEle('hdnBookNowGroupAccomType');
            deepLink = deepLink + "&hdnLocation=AI,,,0";// + gullLocation.value;
	        deepLink = deepLink + "&Strfacilities=";
            deepLink = deepLink + "&selSharing=1";
            deepLink = deepLink + "&SelEnsuite=N";
            deepLink = deepLink + "&selAccomTypeDescription=";
            deepLink = deepLink + "&selPriceLower=0";
            deepLink = deepLink + "&selPriceUpper=0";
            deepLink = deepLink + "&hdnLocationName=";
            deepLink = deepLink + "&selAccomType=" + gullAccomType.value;
            //deepLink = deepLink + "&selAccomType=1" + gullAccomType;
		} 	
		deepLink = deepLink + "&premisesCode=" + gullPremisesCode.value 	
		deepLink = deepLink + "&affiliatevisitID=" + gullAffiliateVisitID.value
		deepLink = deepLink + "&selNumAdults=" + gullNumAdults.value 	
		deepLink = deepLink + "&selNumChildren=" + gullChild 
		deepLink = deepLink + "&selNumInfants=" + gullInfants 
		deepLink = deepLink + "&selArriveMonth=" + gullMonth
		deepLink = deepLink + "&selArriveYear=" + gullYear 
		deepLink = deepLink + "&selArriveDay=" + gullDaySel.value 
		deepLink = deepLink + "&selNumNights=" + gullNumNights.value 
		deepLink = deepLink + "&selCurrency=978"
		deepLink = deepLink + "&isAdvanced=1"
		deepLink = deepLink + "&hdnAction=search"

		//redirect page to our deepLink URL
		//window.location.href = deepLink;
		
		if(gullRedirectType.value == '2') // Pop Up
		{
		    window.open(deepLink,'Booking','resizable=1,status=1,menubar=0,toolbar=0,scrollbars=1,location=0,directories=0,width=1000,height=850,top=60,left=60');
		}
		else if(gullRedirectType.value == '3') // New Window
		{
		    window.open(deepLink,'_blank','resizable=1,status=1,menubar=1,toolbar=1,scrollbars=1,location=1,directories=1');
		}
		else // Current
		{
		    self.parent.location = deepLink;
		}
	}
	else
	{
        alert('Please Select A valid Date');
	}
}


function isValidDate(day,month,year)
{
	/* Purpose: return true if the date is valid, false otherwise
	    Arguments: day integer representing day of month
	    month integer representing month of year
	    year integer representing year */
	var dteDate;
	var todaysDateTime;
	todaysDateTime = new Date();

	//set up a Date object based on the day, month and year arguments
	//javascript months start at 0 (0-11 instead of 1-12)
	dteDate=new Date(year,month,day,"23","59","59");
	
	if (dteDate>=todaysDateTime)
	{
		/*
		Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
		*/
		
		return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));

	}
	else
	{
		return false;			
	}
}


function SetSelectedDay(day)
{
    var gullday = getEle('ArrivalDay_date_sel_dd');
    gullday.selectedIndex = day; 
}

function SetSelectedMonthYear(monthYear)
{
    var gullMonthYear = getEle('ArrivalMonthYear_date_sel_mm');
    gullMonthYear.selectedIndex = monthYear; 
}