// JavaScript Document
var monthName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
 var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
 var calendarCtrlStrings = new Array ("","","Close");
 
 //Inline code to force existance of session var
setPropertyCode(document.mainform.menu2.options[document.mainform.menu2.selectedIndex].value );
//set javascript var to FID
 var FID =  "" ; 
var lf = {};
lf["080328"]  = "716";
lf["080432"]  = "721";
lf["080427"]  = "718";
lf["043344"]  = "715";
lf["080425"]  = "717";
lf["080428"]  = "719 ";
lf["080430"]  = "720";
lf["080437"]  = "722";

function GetValue1(Cobj,sValue)
{	

	var l = Cobj.options.length;
	
	for (var i=0; i < l; i++)
	{
		if (Cobj.options[i].value == sValue)
		{
			try
			{
				Cobj.options[i].selected = true;
			}
			catch(ex)
			{
				//alert(sVal + " " + sTxt);
			}
			break;
		}
	}
}


				function GetDate(){

						var mydate=new Date()
						
						var year=(mydate.getFullYear())
						var day=mydate.getDay()
						var month=mydate.getMonth()+1
						var daym=mydate.getDate()
						
						GetValue1(document.getElementById("DATERANGESTART_DAY"), daym);
						GetValue1(document.getElementById("DATERANGESTART_MONTH"), month);
						GetValue1(document.getElementById("DATERANGESTART_YEAR"), year);
						
						
						
						
						
						
						

						}


GetDate();


function setPropertyCode(code)
	{

    // check for area code selection, indicated by - in front
	  if ( code.charAt(0) == '-')
	  	{


	    document.getElementById('propertyCode').value = ""
	//    document.getElementById('AREAPROPLIST').value = "1";
	    document.getElementById('areacode').value = code.substring(1, code.length) ;
//	    document.getElementById('SEARCHOPERATION').value = "AreaPropertyAvailabilitySearchResult";
	    document.getElementById('SEARCHOPERATION').value = "AreaPropertyAvailabilitySearch";
	     


      document.getElementById('lookAndFeelId').value = hashCheckKey(code);
	  	}
	   else
	   {
	  
	  
	    document.getElementById('propertyCode').value = code;

	    document.getElementById('areacode').value = "";

		 document.getElementById('SEARCHOPERATION').value = "SinglePropertySearch";	     
  
       document.getElementById('lookAndFeelId').value = hashCheckKey(code);
	    }
		
	}


function hashCheckKey(key)
		{
    var value = "100";
    for (var i in lf) 
    	{
		//alert(i);
      if (i == key && lf[i] != null) 
        {
        value = lf[i];
        break;
        }
    	}
		
    return value;
		}
		
		
		
		 //************************************************************************
      // Function:  validateDay
      // Purpose:   Ensure that a selected day is valid. Example 2/31 is an
      //            invalid day. If an invalid date is selected, select the
      //            previous valid day.
      // Input:     yearCtrl - Year dropdown.
      //            monthCtrl - Month dropdown.
      //            dayCtrl - Day dropdown.
      // Output:    None
      //************************************************************************

      function validateDay (formStr, yearCtrl, monthCtrl, dayCtrl)
      {

   //alert("in validate date");
        eval ("var year = parseInt (document.getElementById('" + yearCtrl + "').options[document.getElementById('" + yearCtrl + "').selectedIndex].value, 10)");
        eval ("var month = parseInt (document.getElementById('" + monthCtrl + "').options[document.getElementById('" + monthCtrl + "').selectedIndex].value, 10)");
        eval ("var day = parseInt (document.getElementById('" + dayCtrl + "').options[document.getElementById('" + dayCtrl + "').selectedIndex].value, 10)");

        if (day > (maxDay = getDaysInMonth (month, year)))
        {
          for (index = 0; (index < eval ("document.getElementById('" + dayCtrl + "').length")) &&
                          (eval ("document.getElementById('" + dayCtrl + "').options[" + index+ "].value != " + maxDay));
               index++);
          eval ("document.getElementById('" + dayCtrl + "').options[" + index + "].selected = true");
        }

      }
	  
	 
	  // $RCSfile: datefunctions.js,v $ $Revision: 1.2 $
      //************************************************************************
      // Function:  getFullYear
      // Purpose:   Get the four digit year given a two digit year.
      // Input:     date - date to get the four digit year for.
      // Output:    Four digit year.
      //
      // Function required because NE 3.01 does not support date.getFullYear.
      // Code copied from O'Reilly - JavaScript The Definitive Guide
      //************************************************************************

      function getFullYear (date)
      {
        var year = date.getYear ();
        if (year < 1000) year += 1900;
        return (year);
      }

      //************************************************************************
      // Function:  getDayOfWeek
      // Purpose:   Get the day of the week for a given date.
      // Input:     date - date to get the four digit year for.
      // Output:    String containing the Day of the Week (Monday, Tuesday, etc).
      //************************************************************************

      function getDayOfWeek (crsDate)
      {
        var myDate = new Date (crsDate.substring (0, 4), parseInt(crsDate.substring (4, 6), 10) - 1,  parseInt(crsDate.substring (6, 8), 10));
        //alert(myDate);
		return (dayName [myDate.getDay ()]);
      }

      //************************************************************************
      // Function:  getDayOfWeek
      // Purpose:   Get the day of the week for a given date.
      // Input:     year, month, day
      // Output:    String containing the Day of the Week (Monday, Tuesday, etc).
      //************************************************************************

      function getDayOfWeek2 (year, month, day)
      {
        var myDate = new Date (year, parseInt(month, 10) - 1,  parseInt(day, 10));
       //alert(dayName [myDate.getDay ()]);
	    return (dayName [myDate.getDay ()]);
      }

      //************************************************************************
      // Function:  getMonth
      // Purpose:   Get the month description for a given date.
      // Input:     month - month to get the description for.
      // Output:    String containing the month (January, February, etc).
      //************************************************************************

      function getMonth (month)
      {
        return (monthName [month - 1]);
      }

      //************************************************************************
      // Function:  formatDateDisplay
      // Purpose:   Format a date in the form of January 1, 1999 Tuesday
      // Input:     crsDate - date to format
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    String containing the formatted date.
      //************************************************************************

      function formatDisplayDate (crsDate, language, locale)
      {
        var monthStr = getMonth (parseInt (crsDate.substring (4, 6), 10));
        return (monthStr  + " " + crsDate.substring (6, 8) + ", " + crsDate.substring (0, 4)
                  + " " + getDayOfWeek (crsDate));
      }

      //************************************************************************
      // Function:  formatDateDisplay
      // Purpose:   Format a date in the form of January 1, 1999 Tuesday
      // Input:     year
      //            month
      //            day
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    String containing the formatted date.
      //************************************************************************

      function formatDisplayDate2 (year, month, day, language, locale)
      {
        var monthStr = getMonth (parseInt (month, 10));
        return (monthStr  + " " + day + ", " + year
                  + " " + getDayOfWeek2 (year, month, day));
      }

      //************************************************************************
      // Function:  formatDateDisplayShort
      // Purpose:   Format a date in the form of January 1
      // Input:     crsDate - date to format
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    String containing the formatted date.
      //************************************************************************

      function formatDisplayDateShort (crsDate, language, locale)
      {
        monthStr = getMonth (parseInt (crsDate.substring (4, 6), 10));
        return (monthStr  + " " + crsDate.substring (6, 8));
      }

      //************************************************************************
      // Function:  buildCRSDate
      // Purpose:   Format a date in the form YYYYMMDD
      // Input:     year
      //            month
      //            day
      // Output:    String containing the formatted date.
      //************************************************************************

      function buildCRSDate (year, month, day)
      {
        if (day < 10)
        {
          dayString = "0" + day;
        }
        else
        {
          dayString = new String (day);
        }

        if (month < 10)
        {
          monthString = "0" + month;
        }
        else
        {
          monthString = new String(month);
        }

        return (year + monthString + dayString);
      }
	  
	  
	  
	  
      //************************************************************************
      // Function:  isLeapYear
      // Purpose:   Determine if a year is a leap year.
      // Input:     yrStr - year to determine leapness..
      // Output:    true - if the year is a leap year.
      //            false - otherwise.
      //************************************************************************

      function isLeapYear(yrStr)
      {
        var leapYear=false;
        // every fourth year is a leap year
        if ((parseInt(yrStr, 10)%4) == 0)
        {
          leapYear=true;
        }
        return leapYear;
      }

      //************************************************************************
      // Function:  getDaysInMonth
      // Purpose:   Determine the number of days in a month.
      // Input:     mthIdx - month
      //            yrStr - year
      // Output:    true - if the year is a leap year.
      //            false - otherwise.
      //************************************************************************

      function getDaysInMonth(mthIdx, YrStr)
      {
        // all the rest have 31
        var maxDays = 31
        // expect Feb. (of course)
        if (mthIdx==2)
        {
          if (isLeapYear(YrStr))
          {
            maxDays=29;
          }
          else
          {
            maxDays=28;
          }
        }
        // thirty days hath...
        if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
        {
          maxDays=30;
        }
        return maxDays;
      }
	  
	  
	  //************************************************************************
      // Function:  updateDOW
      // Purpose:   Set the Day of the Week to the valid string (Monday, Tuesday, etc.)
      // Input:     yearCtrl - Year dropdown.
      //            monthCtrl - Month dropdown.
      //            dayCtrl - Day dropdown.
      //            dowCtrl - Day of the Week Control - Control to set.
      //            language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    None
      //************************************************************************

      function updateDOW (formStr, yearCtrl, monthCtrl, dayCtrl, dowCtrl, language, locale)
      {
        eval ("var year = parseInt (document.getElementById('" + yearCtrl + "').options[document.getElementById('" + yearCtrl + "').selectedIndex].value, 10)");
        eval ("var month = parseInt (document.getElementById('" + monthCtrl + "').options[document.getElementById('" + monthCtrl + "').selectedIndex].value, 10)");
        eval ("var day = parseInt (document.getElementById('" + dayCtrl + "').options[document.getElementById('" + dayCtrl + "').selectedIndex].value, 10)");

        eval ("document.getElementById('" + dowCtrl + "').value = getDayOfWeek (buildCRSDate (year, month, day))");
      }

      //************************************************************************
      // Function:  updateDATERANGEEND
      // Purpose:   Update the Departure Date to the Arrival Date + 1.
      // Input:     language - language used by the WEB page.
      //            locale - locale used by the WEB page.
      // Output:    None
      //************************************************************************

      function updateDATERANGEEND (formStr, language, locale)
      {
;

      }




function checkFormBrief()
      {
        var exit=true;


    if (document.mainform.menu2.options[document.mainform.menu2.selectedIndex].value  == "")
       	{
        alert("Please select a Hotel or City.");
     //   return false;
	
       	}

       	else
       	{

	        // Get the current arrival date.
	        var startYear =   parseInt (document.getElementById('DATERANGESTART_YEAR').options[document.getElementById('DATERANGESTART_YEAR').selectedIndex].value, 10);
	        var startMonth =  parseInt (document.getElementById('DATERANGESTART_MONTH').options[document.getElementById('DATERANGESTART_MONTH').selectedIndex].value, 10);
	        var startDay =    parseInt (document.getElementById('DATERANGESTART_DAY').options[document.getElementById('DATERANGESTART_DAY').selectedIndex].value, 10);
	        var numNights =  1;


	//Calculate the depart date, why they need it when we have the night I DONT KNOW!

       var departMonth = startMonth;
       var departYear  = startYear;
       var departDay   = startDay + numNights;

    		if (departDay > getDaysInMonth(departMonth, departYear))
					{
					departDay = 1;
					departMonth = departMonth + 1;
					if (departMonth > 12)
						{
						departYear = departYear + 1;
						departMonth = 1;
						}
					}

	     var adults   = 1;
	     var children = 0;
	     var lookFeel = document.getElementById('lookAndFeelId').value;

 
			var newurl1 = "https://meritushotels.ibe.netbooker.com/web/FrontController.nb4?" +
			"module"                 + "=" +     "PropertySearch"             + "&" +           
			"operation"              + "=" +     GetSearchOperation()         + "&" +           
			"execute"                + "=" +     "yes"                        + "&" +           
			"propertyCodeType"       + "=" +     "RV"                         + "&" +           
			"chainCode"              + "=" +     "GM"                         + "&" +           
			"instanceId"             + "=" +     "75"                         + "&" +           
			                                           GetPropertyCode()      + "&" +           
			"adults"                 + "=" +     adults                       + "&" +           
			"children"               + "=" +     children                     + "&" +           
			"numberOfRooms"          + "=" +     "1"                          + "&" +           
			"promotionCode"          + "=" +     ""                           + "&" +           
			"rateCode"               + "=" +     ""                           + "&" +           
			"frequentGuestId"        + "=" +     getmyFID()                   + "&" +
			"corporateId"            + "=" +     ""                           + "&" +           
			"travelAgentId"          + "=" +     ""                           + "&" +           
			"arriveDate"             + "=" +     startDay                     + "&" +           
			"arriveMonth"            + "=" +     startMonth                   + "&" +           
			"arriveYear"             + "=" +     startYear                    + "&" +           
			"departDate"             + "=" +     departDay                    + "&" +           
			"departMonth"            + "=" +     departMonth                  + "&" +           
			"departYear"             + "=" +     departYear                   + "&" +           
//			"specialRate"            + "=" +     ""                           + "&" +           
			"numberOfNights"         + "=" +     numNights                    + "&" +           
			"lookAndFeelId"          + "=" +     lookFeel                     +"&"+
			"locale"                  +"="+      "en" ;  

  
  window.open(newurl1,"Reservations","toolbar=no,width=830,height=600,status=yes,scrollbars=yes,directories=no,resizable=yes,menubar=no");
   }
   }
 
   function GetPropertyCode()
	{


	  if ( document.getElementById('propertyCode').value == "")
	  	{
        return "&areacode=" +  document.getElementById('areacode').value ;
	  	}
	   else
	   {
  return "propertyCode=" +  document.getElementById('propertyCode').value;

	    }
	}
	function getmyFID()
	{
		if( typeof(FID) !="undefined"  )    
			{
			//alert("heres FID: " + FID);
			
			//Check if number greater than 0
	   myRegExp = /[0-9]+/g
      if(myRegExp.test(FID)) 
    		{
    			if ( FID != "0") return FID;
    		  else return "";
    		}	
			}	 

			return "";
		
	}	
	
	function GetSearchOperation()
	{

  return   document.getElementById('SEARCHOPERATION').value;

	}
