try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

function RoomBookingSelect(oRadio) {

	var sBaseID=oRadio.id;
	var iPropertyID=sBaseID.split('_')[1];
	var iPropertyRoomBookingID=sBaseID.split('_')[2];
	var iRoomTypeID=sBaseID.split('_')[3];
	
	//get the radios
	var aRadios=f.GetObjectsByIDPrefix('rad_');
				
	//hide non-matching properties and highlight rows
	var sRowID
	for (var i=0;i<aRadios.length;i++) {
	
		var iComparePropertyID=aRadios[i].id.split('_')[1];	
		var sRowID='tr'+s.Substring(aRadios[i].id,3);
		var sClass=f.GetClass('tr'+s.Substring(aRadios[i].id,3));	
	
		if (iComparePropertyID!=iPropertyID) {
			aRadios[i].checked=false;
		}
			
		if (aRadios[i].checked && sClass.indexOf(' selected')==-1) {
			f.SetClass(sRowID, sClass+' selected');
		} else if (!aRadios[i].checked) {
			f.SetClass(sRowID, sClass.replace(' selected',''));
		}
	}
	
}

function MultiBooking(iPropertyID) {

	//get the property room bookingids
	var aPropertyRoomBookingIDs=f.GetValue('hidPropertyRoomBookings').split(',');
	var aPropertyRoomBookingCounts=f.GetValue('hidPropertyRoomBookingCounts').split(',');
	
	//get the radios
	var aRadios=f.GetObjectsByIDPrefix('rad');
	
	//make sure we have a selection for each
	var iFound=0;
	for (var i=0;i<aPropertyRoomBookingIDs.length;i++) {
		for (var j=0;j<aRadios.length;j++) {
			if (aRadios[j].id.split('_')[2]==aPropertyRoomBookingIDs[i] && aRadios[j].checked) {
				iFound++;
				break;
			}
		}
	}
	
	var iRoomBookingCount=0;
	for (var k=0;k<aPropertyRoomBookingCounts.length;k++) {
		if (aPropertyRoomBookingCounts[k].split('_')[0]==iPropertyID) {
			iRoomBookingCount=aPropertyRoomBookingCounts[k].split('_')[1];
		}
	}
	
	if (iFound==aPropertyRoomBookingIDs.length) {
		Postback('MultiBook');
	} else if (iFound>0 && iFound==iRoomBookingCount) {
		if (confirm('The number of selected rooms does not equal your initial requirements.\nAre you sure you want to continue booking only the selected rooms?')==true) {
			Postback('MultiBook');
		}
	} else {
		ShowInfoBox('Please make sure you have selected a rate for each room you wish to book');
	}
		
}


var oGetPropertyRoomTypeInventory=new WebService();
oGetPropertyRoomTypeInventory.Go=function(PropertyID, PropertyRoomTypeID, RoomType) { 
	aParams=new Array(['PropertyRoomTypeID',PropertyRoomTypeID]);
	this.RunWebService('webservices/search.asmx','http://intuitivesystems', 'GetPropertyRoomTypeInventory', aParams, this, false);
	
	//pop in the start to the div and a waiting logo
	var sHTML='<h5>'+RoomType+'</h5><a href="#" onclick="CloseInventory('+PropertyID+');return false" title="Close">x</a>';
	sHTML+='<div class="waiting">please wait, retrieving information...</div>';
	AddInventoryDiv(PropertyID,sHTML);
}

oGetPropertyRoomTypeInventory.Done=function(oXML) {
	var oReturn=new GetPropertyRoomTypeInventoryResult(this.GetTagValue(oXML, 'PropertyID'), 
	    this.GetTagValue(oXML, 'HTML'));
	
	AddInventoryDiv(oReturn.PropertyID, oReturn.HTML);
}

function GetPropertyRoomTypeInventoryResult(PropertyID, HTML) {
	this.PropertyID=PropertyID;
	this.HTML=HTML;
}

function AddInventoryDiv(iPropertyID, sHTML) {

    //go get the reference to the rate table we're inserting after
	var oTable=f.GetObject('tblRates_'+iPropertyID);
	
	//check to see if there's an existing div and if so remove it
	var sDivID='divPropertyRoomInventory_'+iPropertyID;
	var oExistingDiv=f.GetObject(sDivID);
	if (oExistingDiv!=null) {
	    oTable.parentNode.removeChild(oExistingDiv);
	}
		
	//create a new div element and insert after
	var oDiv=document.createElement('div');
	oDiv.setAttribute('id',sDivID);
	f.SetClass(oDiv,'propertyroominventory');
	oDiv.innerHTML=sHTML;
	oTable.parentNode.insertBefore(oDiv,oTable.nextSibling)
}

function CloseInventory(iPropertyID) {
    
   var oDiv=f.GetObject('divPropertyRoomInventory_'+iPropertyID);
   oDiv.parentNode.removeChild(oDiv);
}



/* inventory search */
var dInventoryStartDate, iInventoryDuration, iPropertyID;
function InventoryHover(oCell) {

    iPropertyID=n.SafeInt(oCell.id.split('_')[1]);
    var sSQLDate=oCell.id.split('_')[2];
   
    var iDuration=n.SafeInt(dd.GetText('ddlDuration'));
    var aCells=f.GetObjectsByIDPrefix('td_'+iPropertyID+'_','td');
    var bSetInventory=false;
    var iCurrentDuration=0;
    
   
    for (var i=0;i<aCells.length;i++) {
           
        if (!bSetInventory && aCells[i]==oCell) {
            aCells[i].className+=' requested';            
            iCurrentDuration=1;
            bSetInventory=iCurrentDuration<iDuration;
            dInventoryStartDate=InventoryConvertDate(sSQLDate);
        } else if (bSetInventory) {
        
            if (aCells[i].className.indexOf('notavailable')<0) {
                aCells[i].className+=' requested';
                iCurrentDuration+=1;
                bSetInventory=iCurrentDuration<iDuration;
            } else {
                bSetInventory=false;
            }
        } else if (aCells[i].className.indexOf('requested')>-1) {
            //aCells[i].className=s.Replace(aCells[i].className,'requested','');
        }
    }

    iInventoryDuration=iCurrentDuration;    

}

function InventorySetSelected(oCell) {
    oCell.style.backgroundPositionY='0';
}

function InventorySetUnSelected(oCell) {
   oCell.style.backgroundPositionY='40px';
}


function InventoryOut(oCell) {

    
    var iPropertyID=n.SafeInt(oCell.id.split('_')[1]); 
    var aCells=f.GetObjectsByIDPrefix('td_'+iPropertyID+'_','td');
    for (var i=0;i<aCells.length;i++) {   
        if (aCells[i].className.indexOf('requested')>-1) {
            aCells[i].className=s.Replace(aCells[i].className,'requested','');
        }
    }
}

function InventoryConvertDate(sSQLDate) {
    return new Date(sSQLDate.substring(0,4), 
        n.SafeInt(sSQLDate.substr(5,2))-1,sSQLDate.substr(8,2));
}

function InventorySelect(oCell,PropertyID,RoomType) {

    //work out departure date and call search routine
    var dArrivalDate=new Date(dInventoryStartDate.getFullYear(),dInventoryStartDate.getMonth(),dInventoryStartDate.getDate());
    var dDepartureDate=d.AddDays(dInventoryStartDate,iInventoryDuration); 
    Search('',PropertyID,dArrivalDate,iInventoryDuration,dDepartureDate);
    
    //set the contents of the div back to a waiting gif
    var sHTML='<div class="waiting">please wait, searching...</div>';
	AddInventoryDiv(PropertyID,sHTML);   
    
}