function InitRoomListQuery(existRoomListStr)
{
    if (!$("roomCount"))
        return;
    if (existRoomListStr != "") {
        // Init combox with values from existing pfile
        roomCount = 0;
        var arr = existRoomListStr.split("|");
        if (arr.length > 0) {
            if (arr[0] == "roomList") {
                var roomDescCount = arr[1];
                for (var i = 0; i < roomDescCount; i++) {
                    var arr2 = arr[i+2].split(",");
                    for (var j = 0; j < arr2[0]; j++) {
                        $("adultCount"+roomCount).value=arr2[1];
                        $("childCount"+roomCount).value=arr2[2];
                        for (var k = 0; k < arr2[2]; k++) {
                            if (typeof(arr2[2+k]) != "undefined")
                                $("childAge"+roomCount+"_"+k).value=arr2[2+k];
                        }
                        roomCount++;    
                    }
                }
                $("roomCount").value=roomCount;
            }
            else if (arr[0] == "paxList") {
                var adultCount=0, childCount=0;
                var childAge=0, childAgeIndex=0;
                for (var i = 1; i < arr.length; i++) {
                    var arr2 = arr[i].split(":");
                    if (arr2[0] == "pax_child" || arr2[0] == "pax_infant") {
                        var arr3 = arr2[1].split(",");
                        var chdCount = parseInt(arr2[1],10);
                        childCount += chdCount;
                        if (arr2[0] == "pax_child") 
                            childAge = 12;
                        else
                            childAge = 1; // Infant
                        for (var j = 0; j < chdCount; j++) {
                            $("childAge0"+"_"+childAgeIndex).value=childAge;
                            childAgeIndex++;
                        }
                    }
                    else 
                        adultCount += parseInt(arr2[1],10);                 
                    $("adultCount0").value = adultCount;
                    $("childCount0").value = childCount;
                }
            }
        }
    }
    var roomCount = $('roomCount').value;
    for (var i = 0; i < roomCount; i++) 
        $("roomQtyRow"+i).style.display='';
    DisplayChildAgeTable(i);
    for (var i = 0; i < roomCount; i++) {
        var adultCount = $("adultCount"+i).value;
        var childCount = $("childCount"+i).value;
        if (childCount == "")
            childCount = 0;
        if (childCount > 0) {
            $("childAgeRow"+i).style.display='';
            for (var j = 0; j < childCount; j++)
                $("childAgeCol"+i+"_"+j).style.display='';
        }
    }
    // Show/hide child titles
    var maxChildCount = 0;
    for (var k = 0; k < roomCount; k++) {
        var childCount = $("childCount"+k+".value");
        if (childCount > maxChildCount)
            maxChildCount = childCount;
    }
    for (var j = 0; j < maxChildCount; j++) 
        $("childAgeTitle"+j).style.display='';
    for (var j = maxChildCount; j < 2; j++) 
        $("childAgeTitle"+j).style.display='none';
}

function RoomCountChange()
{
    //var roomCount = hotel_search.roomCount.value;
    var roomCount = $('roomCount').value;
    var maxRooms = $('maxRooms').value;
    for (var i = 0; i < roomCount; i++) {
        //eval("roomQtyRow"+i+".style.display=''");
        $("roomQtyRow"+i).style.display='';
        //if (eval("hotel_search.childCount"+i+".value > 0"))
        if ($("childCount"+i).value > 0)
        {
            //eval("childAgeRow"+i+".style.display=''");
            $("childAgeRow"+i).style.display='';
        }
    }
    for (var i = roomCount; i < maxRooms; i++) 
    {
        //eval("roomQtyRow"+i+".style.display='none'");
        //eval("childAgeRow"+i+".style.display='none'");
        $("roomQtyRow"+i).style.display='none';
        $("childAgeRow"+i).style.display='none';
    }

	var i = i - 1;

    DisplayChildAgeTable(i);
}

function AdultCountChange(i)
{
    var roomCount = $('roomCount').value;
    var adultCount = $("adultCount"+i).value;
}

function ChildCountChange(i)
{
    var roomCount = $('roomCount').value;
    var childCount = $("childCount"+i).value;
    if (childCount == "")
        childCount = 0;
    DisplayChildAgeTable(i);
    // Show/hide combox
    if (childCount > 0) {
        $("childAgeRow"+i).style.display='';
        for (var j = 0; j < childCount; j++) 
            $("childAgeCol"+i+"_"+j).style.display= '';
        for (var j = childCount; j < 2; j++) 
            $("childAgeCol"+i+"_"+j).style.display='none';
    }
    else {
        $("childAgeRow"+i).style.display='none';
    }
    // Show/hide child titles
    var maxChildCount = 0;
    for (var k = 0; k < roomCount; k++) {
        var childCount = $("childCount"+k).value;
        if (childCount > maxChildCount)
            maxChildCount = childCount;
    }
    for (var j = 0; j < maxChildCount; j++) 
        $("childAgeTitle"+j).style.display='';
    for (var j = maxChildCount; j < 2; j++) 
        $("childAgeTitle"+j).style.display='none';
}

function ValidRoomCount()
{
    var roomCount = $("roomCount").value;
    return (roomCount > 0);
}

function ValidChildSelect()
{
    var roomCount = $("roomCount").value;
    var totalAdultCount = 0, totalChildCount = 0;
    for (var i = 0; i < roomCount; i++) {
        var adultCount = $("adultCount"+i).value;
        var childCount = $("childCount"+i).value;
        totalAdultCount += parseInt(adultCount,10);
        totalChildCount += parseInt(childCount,10);
    }
    if (totalAdultCount == 0 && totalChildCount > 0)
        return false;
    return true;
}

function ValidChildAge()
{
    var roomCount = $("roomCount").value;
    for (var i = 0; i < roomCount; i++) {
        var childCount = $("childCount"+i).value;
        if (childCount > 0) {
            for (var j = 0; j < childCount; j++) {
                childAge = $("childAge"+i+"_"+j).value;
                if (childAge == 0)
                    return false;
            }
        }
    }
    return true;
}

function ValidRoomList(roomList)
{
    var roomCount = $("roomCount").value;
    var arr = roomList.split(";")
    if (roomList.length > 0 && roomList != "0") {
        for (var i = 0; i < roomCount; i++) {
            var adultCount = $("adultCount"+i).value;
            var childCount = $("childCount"+i).value;
            var validRoomList = false;
            for (var j = 0; j < arr.length; j++) {
                var arr2 = arr[j].split(",")
                if (adultCount == arr2[0] && childCount == arr2[1]) {
                    validRoomList = true;
                    break;
                }
            }
            if (!validRoomList)
                return false;
        }
    }
    return true;
}

function DisplayChildAgeTable(i)
{
    var roomCount = $("roomCount").value;
    var hasChild = false;
    for (var k = 0; k < roomCount; k++) {
        if ($("childCount"+k).value > 0)
            hasChild = true;
    }
    if (hasChild)
        $("childAgeTable"+i).style.display='';
    else
        $("childAgeTable"+i).style.display='none';
}



// Set roomDesc hidden fields with roomList string stored for product.
// Use this to set alternative roomList which bring valid price for product.
function SetRoomList(hotel_search, s)
{
    if (!$("roomDescCount"))
        return;
    var arr = s.split(";")
    $("roomDescCount").value = arr.length;
    for (var i = 0; i < arr.length; i++) {
        var fld = $("roomDesc"+i);
        fld.value = arr[i];
    }
}

// Restore original roomList as select in query page.
// Use this to cancel the alternative roomList which set before and user press back.
function RestoreOrgRoomList(hotel_search)
{
    if (!$("roomDescCount"))
        return;
    for (var i = 0; i < $("roomDescCount").value; i++) {
        var fld = $("roomDesc"+i);
        var fldOrg = $("orgroomDesc"+i);
        fld.value = fldOrg.value;
    }
}

function setCheckout()
{
    var day = $F("day");
    var month = $F("month");
    var year = $F("year");
    var nights = $F("nights");

	var date = $("date");
    
    //alert("Day: " + day + " Month: " + month + " Year: " + year + " Nights: " + nights);

    var url = "getCheckout.php";
    var params = "day="+day+"&month="+month+"&year="+year+"&nights="+nights;
    var ajax = new Ajax.Updater('checkout', url, {method:'post',parameters: params});

	date.value = day + " " + month + " " +year;
    
	var validate = day + "/" + month + "/" + year;

    
	var testDate = year+ " " + month + " " +day;
    var todaysDate = $('todaysDate').value;
    
	/*
	if( (testDate.replace(' ','') < todaysDate.replace(' ','')) || validateDate(validate) == false )
	{
		alert('You have chosen an invalid date. Please choose another date.');
	}
	*/
}



function setHiddenDate()
{
	var day = $F("day");
    var month = $F("month");
    var year = $F("year");	

	var date = $("date");

	date.value = day + " " + month + " " +year;

	var validate = day + "/" + month + "/" + year;

    var testDate = year+ " " + month + " " +day;
    var todaysDate = $('todaysDate').value;

	if( (testDate.replace(' ','') < todaysDate.replace(' ','')) || validateDate(validate) == false )
	{
		//alert('You have chosen an invalid date. Please choose another date.');
		return false;
	}
	else
	{
		return true;
	}
}


function setHiddenDateOut()
{
	var day = $F("dayOut");
    var month = $F("monthOut");
    var year = $F("yearOut");	

	var date = $("dateOut");

	date.value = day + " " + month + " " +year;

	var validate = day + "/" + month + "/" + year;

    var testDate = year+ " " + month + " " +day;
    var todaysDate = $('todaysDate').value;

	if( (testDate.replace(' ','') < todaysDate.replace(' ','')) || validateDate(validate) == false )
	{
		//alert('You have chosen an invalid date. Please choose another date.');
		return false;
	}
	else
	{
		return true;
	}
}


function setHiddenDates()
{
	if( ( setHiddenDate() == false ) || ( setHiddenDateOut() == false ) )
	{
		alert('You have chosen an invalid date. Please choose another date.');
	}
	else
	{
		document.hotel_search.submit();
	}
}


function validateDate(d)
{
	var dateValue = d;
    
	var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;

 
	if ( dateValue.match(RegExPattern) )
	
	{
		return true;
	}
	else
	{
		return false;
 
	}
}

function numGuestsChanged(row)
{
    // Depends on roomtype drop down order of single-double-twin-twinForSoleUse-triple-quad
    adultCountName = "adultCount" + row;
    childCountName = "childCount" + row;
    roomTypeName = "room" + row + "Type";
    
    numAdults = parseInt($(adultCountName).value);
    numChildren = parseInt($(childCountName).value);
    roomType = $(roomTypeName);
    
    
    // Output alert error
    if((numAdults == 3 && numChildren == 2) || (numAdults == 4 && numChildren == 1) || (numAdults == 4 && numChildren == 2))
    {
        alert('Room type not available');
    }
    // Force Quad
    else if(numAdults == 4 || (numAdults == 3 && numChildren == 1) || (numAdults == 2 && numChildren == 2))
    {
        roomType.selectedIndex = 4;
    }
    // Force Triple
    else if( numAdults == 3)
    {
    	roomType.selectedIndex = 3;
    }
    else if( ( numAdults == 3 || (numAdults == 2 && numChildren == 1) || (numAdults == 1 && numChildren == 2) )
        && (roomType.selectedIndex != 4 && roomType.selectedIndex != 3))
    {
    	roomType.selectedIndex = 3;
    }
    //force twin
    else if(numAdults == 1 && numChildren == 1)
    {
        roomType.selectedIndex = 2;
    }
    // Force double (not twin)
    else if( numAdults == 2) // 2 adults always double
    {
    	roomType.selectedIndex = 1;
    }
    else if( ( numAdults == 2 || (numAdults == 2 && numChildren == 1) || (numAdults == 1 && numChildren == 1) ) 
        && (roomType.selectedIndex != 5 && roomType.selectedIndex != 4 && roomType.selectedIndex != 2 && roomType.selectedIndex != 1 ))
    {
    	roomType.selectedIndex = 1;
    }
    else if(numAdults == 1)
    {
    	roomType.selectedIndex = 0;
    }
}

function roomTypeChanged(row)
{
    adultCountName = "adultCount" + row;
    childCountName = "childCount" + row;
    roomTypeName = "room" + row + "Type";
        
    roomType = $(roomTypeName).value;
    
    if(roomType == 'DB' || roomType == 'TB')
    {
        if($(adultCountName).value != 1 || $(childCountName).value != 1)
        {
            $(adultCountName).selectedIndex = 1;
        }
    }
    
    if(roomType == 'TR')
    {
        $(adultCountName).selectedIndex = 2;
        $(childCountName).selectedIndex = 0;
        ChildCountChange(row);
    }
    
    if(roomType == 'Q')
    {
        $(adultCountName).selectedIndex = 3;
        $(childCountName).selectedIndex = 0;
        ChildCountChange(row);
    }
    
    if(roomType == 'SB')
    {
        $(adultCountName).selectedIndex = 0;
        $(childCountName).selectedIndex = 0;
        ChildCountChange(row);
    }
}

function validateSearch()
{
    // Ensure some search is at least entered
    if($('destinationName').value.length < 3)
    {
    	alert("Please choose a valid destination.");
        return false;
    }

/*
	if( $('destination').value == '' )
    {
        alert('This is not a valid destination. Please refine your destination option.');
		return false;
    }
*/

    // First validate that we have a max of 9 passengers
    var roomCount = $('roomCount').value;
    var totalAdults = 0;
    var totalChildren = 0;
    for(i=0; i < roomCount; i++)
    {
	   var adultField = 'adultCount' + i;
       var childField = 'childCount' + i;
       totalAdults += parseInt($(adultField).value);
       totalChildren += parseInt($(childField).value);
    }
    var totalPax = totalAdults + totalChildren;
    if(totalPax > 9)
    {
    	alert("The maximum  number of passengers allowed is 9.\nFor 10 passengers or more please click on the Groups link above.");
        return false;
    }
    
    if(!validateRoomOccupancy())
    {
    	return false;
    }
    
    var day = $F("day");
    var month = $F("month");
    var year = $F("year");
    
    var validate = day + "/" + month + "/" + year;
    var testDate = year+ " " + month + " " +day;
    var todaysDate = $('todaysDate').value;

    if( (testDate.replace(' ','') < todaysDate.replace(' ','')) || validateDate(validate) == false )
    {
        alert('You have chosen an invalid date. Please choose another date.');
        return false;
    }
    
    return true;
}

function validateRoomOccupancy()
{
	var roomCount = $('roomCount').value;
    for(i=0; i < roomCount; i++)
    {
    	var numAdults = parseInt($('adultCount'+i).value);
        var numChildren = parseInt($('childCount'+i).value);
        var roomType = $('room'+i+'Type').value;
        var total = numAdults + numChildren;
        if(roomType == 'Q')
        {
        	if(total > 4)
            {
            	alert("You cannot fit more than 4 people into a quad room");
                return false;
            }
        }
        else if (roomType == 'TR')
        {
        	if(total > 3)
            {
            	alert("You cannot fit more than 3 people into a triple room");
                return false;
            }
        }
        else if (roomType == 'TB' || roomType == 'DB')
        {
        	if(total > 3)
            {
            	alert("You have too many people selected for a double/twin room");
                return false;
            }
            if(numAdults > 2)
            {
            	alert("You cannot put more than 2 adults into a double/twin room");
                return false;
            }
        }
        else if(roomType == 'TS' || roomType == 'SB')
        {
        	if(total > 2)
            {
            	alert("You have too many people selected for a single/twin for single use room");
                return false;
            }
            if(numAdults > 1)
            {
            	alert("You cannot put more than 1 adult into a single/twin for single use room");
                return false;
            }
        }
    }
    return true;
}