var roomAvailability;
var hotelResults;
var hotelList = '';
var paxList = '';
var roomIsAvailable = '';


function disableFormElements(formD)
{
alert("I am here");
formD = document.getElementById(formD);
alert("I am here 2");
alert("Elements=" + formD.elements.length);
for (var i=0;i<formD.elements.length;i++)
{
    var e = document.formD.elements[i];
    if (e.type.toLowerCase() == "button" 
    	|| e.type.toLowerCase() == "reset" 
    		|| e.type.toLowerCase() == "submit") 
    		{
				e.disabled=true;
				 alert("I am here 4")
			}
  	}
}

function disableFormElementsAfterSubmit(in_Name)
{
   setTimeout("disableFormElements(" + in_Name + ")", 10);
}

function getObjectPosition(theobject)
{
	var left = 0;
	var top = 0;
	
	f = $(theobject);

	while (f.offsetParent){
		left += f.offsetLeft;
		top += f.offsetTop;
		f = f.offsetParent;
	}
	
	left += f.offsetLeft;
	top += f.offsetTop;
	
	return {x:left,y:top};
}

function startAvailabilityUpdater(placeid,holidayid,hide)
{
	updateTimer = setTimeout('startAvailabilityUpdater(' + placeid + ',' + holidayid + ',\'1\');',60000);
	loadAvailableRooms(placeid,holidayid,hide);
}

function loadAvailableRooms(placeid,holidayid,hide)
{
	roomAvailability = [];
	if(hide != '1')
	{
		Element.hide('roomSelectResults');
	}
	Element.show('loadingRoomChoice');
	
	var url = '/members/load_available_rooms.html';
	var d = new Date();
	var time = d.getTime();

	new Ajax.Request(url, {
	  method: 'post',
	  requestHeaders: {Accept: 'application/json'},
		parameters: { 'holiday': holidayid, 'placeid': placeid, 'r': time },	 
	  onSuccess: function(transport)
	  {	 	
  		hotelResults = transport.responseText.evalJSON();
  		displayAvailableRooms(placeid,holidayid);
  		
		},
		onFailure: function()	 	
		{
			Element.hide('loadingRoomChoice');
			Element.update('roomSelectResults','<font color="red"><b>Failed loading room list</b></font>');
			Element.show('roomSelectResults');
		}		
	});
}

function displayAvailableRooms(placeid,holidayid)
{
	hotelList = '';
	if(hotelResults.length == 0)
	{
		hotelList = '<font color="red"><i>No rooms available - check back later</i></font>';		
	}
	else
	{ 		
		for(i=0;i<hotelResults.length;i++)
		{	  				  			
			roomResults = hotelResults[i].rooms;
			if(roomResults.length > 0)
			{
  			hotelList += '<div class="roomSelectHotel"><div class="roomSelectHotelName">' + hotelResults[i].hotel_name + '</div>';
  			
  			for(r=0;r<roomResults.length;r++)
  			{	
  				if(roomResults[r].places_available > 0)
  				{  					
	  				var paxInRoom = false; 				
	  				//load allocated people
	  				paxList = '';	 
	  				paxResults = roomResults[r].pax;		  				
	  				if(paxResults.length > 0)
						{
							paxList += '<div class="roomPaxList"><ul>';
							for(p=0;p<paxResults.length;p++)
	  					{		  	 						  						
	  						if(paxResults[p].code == placeid)
	  						{
	  							paxList += '<li class="paxYou">' + paxResults[p].name + '</li>';
	  							paxInRoom = true; 	
	  						}
	  						else
	  						{
	  							paxList += '<li class="' + paxResults[p].sex + '">' + paxResults[p].name + '</li>';
	  						}
	  					}	
	  					paxList += '</ul></div>';	
	  				}							
							
						var thisRoom = { code:roomResults[r].room_code, available:roomResults[r].places_available, taken:roomResults[r].places_taken };
						Object.toJSON(thisRoom);
						roomAvailability.push(thisRoom);
						roomIsAvailable = '';
						if(!paxInRoom) 
  					{
							roomIsAvailable = ' onclick="chooseRoom(' + roomResults[r].room_code + ',' + placeid + ',' + holidayid + '); return false;"';
						}
						hotelList += '<div id="room' + roomResults[r].room_code + '"' + roomIsAvailable + ' class="roomSelectRoom" onmouseover="this.style.cursor=\'pointer\';Element.addClassName(this,\'roomSelectRoomHover\');" onmouseout="Element.removeClassName(this,\'roomSelectRoomHover\')">';  							  				
	  				hotelList += '<div class="roomSelectRoomName">' + roomResults[r].room_name + '</div>';
	  				hotelList += '<span class="roomAvailablePlaces">' + roomResults[r].places_available + ' Place Available</span>';		  				  				
	  				hotelList += '<br />' + paxList;		  				  						  				
	  				hotelList += '</div>';
		  			
		  		}
  			}		  			
  			hotelList += '</div>';	
  		}
  		else
			{
				hotelList = '<font color="red"><i>No rooms available - check back later</i></font>';	
			}						
		}	  						
	}
	
	Element.hide('loadingRoomChoice');
	Element.update('roomSelectResults',hotelList);
	Element.show('roomSelectResults');
	
}

function roomAvailablility(roomid)
{
	for(i=0;i<roomAvailability.length;i++)
	{
		if(roomAvailability[i].code == roomid)
		{			
			return { available:roomAvailability[i].available, taken:roomAvailability[i].taken };
		}
	}
	return false;	
}

function chooseRoom(roomid,placeid,holidayid)
{
	var f = $('room'+roomid);
	
	var left = 0;
	var top = 0;

	while (f.offsetParent){
		left += f.offsetLeft;
		top += f.offsetTop;
		f = f.offsetParent;
	}
	
	left += f.offsetLeft;
	top += f.offsetTop;
	
	scrollx = left;
	scrolly = top;

	top = top + "px";
	left = left - 40 + "px";
	
	var thisRoom = roomAvailablility(roomid);
	
	if(thisRoom.available > 0)
	{
		Element.show('addPaxDisplay');
		Element.show('addPaxHead');
		Element.setStyle('addPaxDisplay',{ 'top':top, 'left':left });
		Element.update('addPaxList');
		
		//add "you" the person selecting the room
		new Insertion.Bottom('addPaxList', '<br /><b>You</b><br />OTP000' + placeid);
		
		//add the remaining available places to the list for them to add
		personnum = 2;
		for(p=0;p<thisRoom.available-1;p++)
		{			
			new Insertion.Bottom('addPaxList', '<br /><b>Person ' + personnum + '</b><br /><input type="text" name="addPax' + p + '" value="" />');
			personnum++;
		}		
		new Insertion.Bottom('addPaxList', '<br /><br /><button type="button" id="roomSaveButton" onclick="saveRoom(' + roomid + ',' + placeid + ',' + holidayid + '); Element.update(this,\'Saving...\'); return false;">Book this room</button>');
		new Insertion.Bottom('addPaxList', ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" onclick="closeRoomChoice(); return false;">Cancel</button>');
	}	
	scrollTo(scrollx,scrolly);
}

function closeRoomChoice()
{
	new Effect.Fade('addPaxDisplay',{duration:0.5});
}

function saveRoom(roomid,placeid,holidayid)
{
	var savestring = '';	
	var addInputList = $('addPaxList').getElementsBySelector('input');
	
	for(i=0;i<addInputList.length;i++)
	{
		if((addInputList[i].name.indexOf('addPax') != -1) && (addInputList[i].value != ''))
		{
			savestring += '&addPax='+addInputList[i].value;
		}
	}
		
	var url = '/members/save_room_choice.html';
	new Ajax.Request(url, {
	  method: 'post',
	  requestHeaders: {Accept: 'application/json'},
		parameters: 'holiday=' + holidayid + '&placeid=' + placeid + '&roomid=' + roomid + '' + savestring ,	 
	  onSuccess: function(transport)
	  {	  	
  		var saveResults = transport.responseText.evalJSON();  		
  		if(saveResults.status == 'OK')
  		{
  			//loadAvailableRooms(placeid,holidayid,'1');
  			Element.update('addPaxList','<font color="green"><b>Saved room choice</b></font>');
  			new Effect.Fade('addPaxDisplay', {duration:0.3,delay:0.5});
  			loadAvailableRooms(placeid,holidayid,'1');
  		}	
  		else
  		{  			
				Element.hide('addPaxHead');
				Element.update('addPaxList','<p style="color:red;"><b>' + saveResults.message + '</b></p><button type="button" onclick="closeRoomChoice(); return false;">Cancel</button>');	
			}  		
		},
		onFailure: function()		
		{
			Element.update('addPaxList','<font color="red"><b>Failed to save room choice</b></font><br /><button type="button" onclick="chooseRoom(' + roomid + ',' + placeid + ',' + holidayid + '); return false;">Try Again</Button>');
		}		
	});	
}

/* SHOW/HIDE Holiday */
function showHoliday(hol)
{
	var holidaystatus = document.getElementById('holiday_' + hol).style.display;
	if(holidaystatus == 'none')
	{
		document.getElementById('holiday_' + hol).style.display = 'display';
	}
	else
	{
		document.getElementById('holiday_' + hol).style.display = 'none';
	}	
}


/*
ROOM LIST
*/

function startRoomListUpdater(holidayid,hide)
{
	updateTimer = setTimeout('startRoomListUpdater(' + holidayid + ',\'1\');',30000);
	loadAvailableRooms('g',holidayid,hide);
}

function setFreeCoachPlaces()
{
	for(p=coachAvailablePlaces+1;p<=coachAllocatedPlaces;p++)
	{
		$(p + '-seat').removeClassName('seat_available');
	}	
	for(p=1;p<=coachAvailablePlaces;p++)
	{
		$(p + '-seat').addClassName('seat_available');
	}
	
	var ccXY = getObjectPosition('coachContainer');
	var coachContHeight = $('coachContainer').getHeight();
	//$('coachDropView').setStyle({left:(ccXY.x+2)+'px',top:(ccXY.y+20)+'px',height:(coachContHeight-22)+'px',zIndex:1000});
	
	if(coachAvailablePlaces == 0)
	{
		$('coachFullOverlay').setStyle({left:(ccXY.x+2)+'px',top:(ccXY.y+20)+'px',height:(coachContHeight-22)+'px',zIndex:1000});
		$('coachFullOverlay').zIndex = 1000;
		$('coachFullOverlay').show();		
		Droppables.remove('coachDropView');
	}
}

function updateAllocatedCount(amount,updatetype)
{
	if(updatetype == 'a')
	{
		coachAvailablePlaces -= amount;
		currentAllocatedCalc = parseInt($('currentAllocated').innerHTML) + amount;
		Element.update('currentAllocated',currentAllocatedCalc);
		currentWaitingAllocationCalc = parseInt($('currentWaitingAllocation').innerHTML) - amount;
		Element.update('currentWaitingAllocation',currentWaitingAllocationCalc);
	}
	else if(updatetype == 'r')
	{
		coachAvailablePlaces += amount;
		currentAllocatedCalc = parseInt($('currentAllocated').innerHTML) - amount;
		Element.update('currentAllocated',currentAllocatedCalc);
		currentWaitingAllocationCalc = parseInt($('currentWaitingAllocation').innerHTML) + amount;
		Element.update('currentWaitingAllocation',currentWaitingAllocationCalc);
	}
	setFreeCoachPlaces();
}

function loadRoomList(holidayid,hide)
{
	roomAvailability = [];
	if(hide != '1')
	{
		Element.hide('roomSelectResults');
	}
	Element.show('loadingRoomChoice');
	
	var hotelList = '';
	var unallocatedRooms = '';
	var url = '/members/load_coach_rooms.html';
	var d = new Date();
	var time = d.getTime();

	new Ajax.Request(url, {
	  method: 'post',
	  requestHeaders: {Accept: 'application/json'},
		parameters: { 'holiday': holidayid, 'r': time },	 
	  onSuccess: function(transport)
	  {	 	
  		var hotelResults = transport.responseText.evalJSON();
  		if(hotelResults.length == 0)
  		{
  			hotelList = '<font color="orange"><i>No rooms to allocate</i></font>';		
  		}
  		else
  		{  	  				
	  		for(i=0;i<hotelResults.length;i++)
	  		{
	  			roomResults = hotelResults[i].rooms;	
	  			hotelList += '<div class="roomSelectHotel"><div class="roomSelectHotelName">' + hotelResults[i].hotel_name + '</div>';
	  			if(roomResults.length == 0)
  				{
  					hotelList += '<font color="orange"><i>No rooms to allocate</i></font></div>';		
  				}
  				else
  				{		  				  					  			
		  			for(r=0;r<roomResults.length;r++)
		  			{
		  				//load allocated people
		  				var roomPaxObject = Array();
		  				paxList = '';	 
		  				paxResults = roomResults[r].pax;		  				
		  				if(paxResults.length > 0)
  						{  							
  							paxList += '<div class="roomPaxList"><ul>';
  							for(p=0;p<paxResults.length;p++)
		  					{
		  						if(paxResults[p].travel == 'none' || paxResults[p].travel == '')
		  						{
		  							paxList += '<li class="paxOwnTravel">' + paxResults[p].name + ' - OT</li>';
		  							var paxObject = {
			  							code: paxResults[p].code,
			  							name: paxResults[p].name,
			  							sex: paxResults[p].sex,
			  							travel: paxResults[p].travel,
			  							coach: paxResults[p].coach
		  							};		
		  						}
		  						else if(paxResults[p].coach)
		  						{
		  							paxList += '<li class="paxOwnTravel">' + paxResults[p].name + ' (' + paxResults[p].coach + ')</li>';
		  							var paxObject = {
			  							code: paxResults[p].code,
			  							name: paxResults[p].name,
			  							sex: paxResults[p].sex,
			  							coach: paxResults[p].coach
		  							};		
		  						}
		  						else
		  						{
		  							paxList += '<li class="' + paxResults[p].sex + '">' + paxResults[p].name + ' (' + paxResults[p].travel + ')</li>';
		  							var paxObject = {
			  							code: paxResults[p].code,
			  							name: paxResults[p].name,			  							
			  							travel: paxResults[p].travel,
			  							sex: paxResults[p].sex
		  							};
		  						}		  						
		  						roomPaxObject.push(paxObject);
		  					}	
		  					paxList += '</ul></div>';	  							
  						}  						
  						var thisRoom = {
  							code:roomResults[r].room_code,
  							available:roomResults[r].places_available,
  							taken:roomResults[r].places_taken,
  							pax: []
  						};
  						thisRoom.pax = roomPaxObject;
  						Object.toJSON(thisRoom);
  						roomAvailability.push(thisRoom);
  			
  						hotelList += '<div id="room' + roomResults[r].room_code + '" class="roomSelectRoom">';  							  				
		  				hotelList += '<div class="roomSelectRoomName" onmouseover="this.style.cursor=\'move\';Element.addClassName(\'room' + roomResults[r].room_code + '\',\'roomSelectRoomHover\');" onmouseout="Element.removeClassName(\'room' + roomResults[r].room_code + '\',\'roomSelectRoomHover\')">' + roomResults[r].room_name + '</div>';
		  				if(roomResults[r].places_available > 0)
		  				{
		  					hotelList += '<span class="roomAvailablePlaces">' + roomResults[r].places_available + ' Place Available</span>';				
		  				}
		  				else
	  					{
	  						hotelList += '<span class="roomChoiceFull">Room Full</span>';	
	  					}		  				
		  				hotelList += '<br />' + paxList;
		  					  						  				
		  				hotelList += '</div>';
		  				unallocatedRooms += 'new Draggable(\'room' + roomResults[r].room_code + '\',{revert:true,handle:\'roomSelectRoomName\'}); ';
		  			}		  			
		  			hotelList += '</div>';	
		  		} 							
	  		}	  						
  		}  		
  		Element.hide('loadingRoomChoice');
	  	Element.update('roomSelectResults',hotelList);
	  	Element.show('roomSelectResults');
	  	eval(unallocatedRooms); //eval the pax so they are draggable
	  	setFreeCoachPlaces();
		},
		onFailure: function()		
		{
			Element.hide('loadingRoomChoice');
			Element.update('roomSelectResults','<font color="red"><b>Failed loading room list</b></font>');
			Element.show('roomSelectResults');
		}		
	});
}

function checkRoomPlaces(roomid)
{	
	for(r=0;r<roomAvailability.length;r++)
	{
		if(roomAvailability[r].code == roomid)
		{
			return roomAvailability[r].taken;
		}
	}
	return 0;
}

function getRoomPax(roomid)
{
	var roomPaxList = '';
	for(r=0;r<roomAvailability.length;r++)
	{
		if(roomAvailability[r].code == roomid)
		{
			roomPax = roomAvailability[r].pax;	
			for(p=0;p<roomPax.length;p++)
			{	
				if(roomPax[p].travel == 'none' || roomPax[p].travel == '')
				{
					roomPaxList += '<label class="paxOwnTravel">' + roomPax[p].name + ' - OT<br />';		
				}
				else if(roomPax[p].coach)
				{
					roomPaxList += '<label class="paxOwnTravel">' + roomPax[p].name + ' (' + roomPax[p].coach + ')</label><br />';		
				}
				else
				{					
					roomPaxList += '<input type="checkbox" onclick="return roomToCoachPartialCheck(); return false;" name="roomAllocatePax" id="roomAllocatePax_' + roomPax[p].code + '" value="' + roomPax[p].code + '" /> <label for="roomAllocatePax_' + roomPax[p].code + '" class="' + roomPax[p].sex + '">' + roomPax[p].name + ' (' + roomPax[p].travel + ')</label><br />';
				}
			}
			return roomPaxList;		
		}
	}	
	return 0;	
}

function getRoomPaxAllocated(roomid)
{
	for(r=0;r<roomAvailability.length;r++)
	{
		if(roomAvailability[r].code == roomid)
		{
			roomPax = roomAvailability[r].pax;	
			for(p=0;p<roomPax.length;p++)
			{
				if(roomPax[p].coach)
				{					
					return 1;
				}				
			}
		}	
	}		
	return 0;	
}

function roomToCoach(roomid,coachid,holidayid)
{
	Element.hide(roomid);
	Element.show('roomAllocateChoice');
	
	var roomreg = /^room(\d+)$/i;
	var room = roomreg.exec(roomid);		
	var room_id = room[1];
	
	var f = $('coachContainer');
	
	var left = 0;
	var top = 0;

	while (f.offsetParent){
		left += f.offsetLeft;
		top += f.offsetTop;
		f = f.offsetParent;
	}
	
	left += f.offsetLeft;
	top += f.offsetTop;

	top = top + 100 + "px";
	left = left + 10 + "px";
	
	Element.setStyle('roomAllocateChoice',{ 'top':top, 'left':left });
	
	var partialOnly;
	var coachFull;
	
	//check if there is enough room on this coach
	roomPax = checkRoomPlaces(room_id);
	if(roomPax > 0)
	{
		if(coachAvailablePlaces > 0)
		{
			if(roomPax > coachAvailablePlaces)
			{
				//cannot allocation whole room only give partial option
				roomToCoachPartial(room_id,coachid,holidayid);
				return false;
			}
			else
			{				
				if(getRoomPaxAllocated(room_id) > 0)
				{
					//there are pax that have already been allocated,
					//force individual selection
					roomToCoachPartial(room_id,coachid,holidayid);
					return false;
				}
				else
				{
					roomChoiceList = '<b>Allocate Seats</b><br />Please choose how you wish to allocate this room:<br />';		
					roomChoiceList += '<button onclick="roomToCoachSave(\'' + room_id + '\',\'' + coachid + '\',\'' + holidayid + '\',\'f\'); return false;">Fully</button>&nbsp;';	
					roomChoiceList += '<button onclick="roomToCoachPartial(\'' + room_id + '\',\'' + coachid + '\',\'' + holidayid + '\'); return false;">Partially</button>';
					Element.update('roomAllocateChoice',roomChoiceList);
				}	
			}
		}
		else
		{
			Element.update('roomAllocateChoice','<font color="red"><b>Coach is full</b></font><br />');
		}
	}
	else
	{
		Element.update('roomAllocateChoice','<font color="red"><b>Room is empty</b></font><br />');			
	}	
	
	new Insertion.Bottom('roomAllocateChoice', ' <button type="button" onclick="cancelRoomToCoach(\'' + room_id + '\'); return false;">Cancel</button>');
		
}

function roomToCoachPartialCheck()
{
	roomSelectedPax = $('roomAllocateChoice').getElementsBySelector('[name="roomAllocatePax"]');
	var paxSelectedCount = 0;
	for(p=0;p<roomSelectedPax.length;p++)
	{
		if(roomSelectedPax[p].checked)
		{
			paxSelectedCount++;
		}
	}
	if(paxSelectedCount > coachAvailablePlaces)
	{
		Element.update('roomAllocateError','Sorry, There are only ' + coachAvailablePlaces + ' places remaining.<br />');
		return false;
	}	
	return true;
}	

function roomToCoachPartialSelect(roomid,coachid,holidayid)
{
	roomSelectedPax = $('roomAllocateChoice').getElementsBySelector('[name="roomAllocatePax"]');
	var paxSelected = Array();
	var paxSelectedString;
	for(p=0;p<roomSelectedPax.length;p++)
	{
		if(roomSelectedPax[p].checked)
		{
			paxSelected.push(roomSelectedPax[p].value);
		}
	}
	if(paxSelected.length > coachAvailablePlaces)
	{
		Element.update('roomAllocateError','Sorry, There are only ' + coachAvailablePlaces + ' places remaining.<br />');
		return false;
	}
	else
	{
		paxSelectedString = paxSelected.join("&addPax=");	
		roomToCoachSave(roomid,coachid,holidayid,'p',paxSelectedString);
	}
	return true;
}

function roomToCoachPartial(roomid,coachid,holidayid)
{
	Element.update('roomAllocateChoice','<span id="roomAllocateError"></span>');
	
	roomPax = checkRoomPlaces(roomid);
	if(roomPax > coachAvailablePlaces)
	{	
		new Insertion.Bottom('roomAllocateChoice', '<b>Allocate Seats</b><br />The coach is almost full you can only choose ' + coachAvailablePlaces + ' more passenger(s) to allocate.<br />');		
	}
	else
	{
		new Insertion.Bottom('roomAllocateChoice', '<b>Allocate Seats</b><br />Please check the boxes of the people you wish to allocate.<br />');	
	}		
	new Insertion.Bottom('roomAllocateChoice', getRoomPax(roomid));
	new Insertion.Bottom('roomAllocateChoice', '<br /><br /><button onclick="roomToCoachPartialSelect(\'' + roomid + '\',\'' + coachid + '\',\'' + holidayid + '\'); return false;">Allocate</button>');
	new Insertion.Bottom('roomAllocateChoice', '<button onclick="cancelRoomToCoach(\'' + roomid + '\'); return false;">Cancel</button>');	
}

function roomToCoachSave(roomid,coachid,holidayid,savetype,addPax)
{	
	Element.update('roomAllocateChoice','<font color="orange"><b>Allocating room</b></font> <img src="/images/loadingdots.gif" width="21" height="5" />');
	
	var url = '/members/save_coach_allocation.html';
	var d = new Date();
	var time = d.getTime();	
	
	var params = {
		'roomid': roomid,
		'coachid': coachid,
		'holiday': holidayid,
		'full':1,
		'r': time
	};
	
	if(savetype == 'p')
	{
		var params = 'roomid='+roomid+'&coachid='+coachid+'&holiday='+holidayid+'&addPax='+addPax+'&r='+time;		
	}
	
	new Ajax.Request(url, {
	  method: 'get',
	  requestHeaders: {Accept: 'application/json'},
		parameters: params,	 
	  onSuccess: function(transport)
	  {
	 		var saveResults = transport.responseText.evalJSON();  		
  		if(saveResults.status == 'OK')
  		{  			
  			updateAllocatedCount(saveResults.addedcount,'a');
  			Element.update('roomAllocateChoice','<font color="green"><b>' + saveResults.message + '</b></font>');
				new Effect.Fade('roomAllocateChoice', {duration:0.3,delay:0.5});
				droppeditem = false;
				if(addPax)
				{
					loadRoomList(holidayid);	
				}		
  		}
  		else
  		{
  			Element.update('roomAllocateChoice','<font color="red"><b>' + saveResults.message + '</b></font>');
				new Insertion.Bottom('roomAllocateChoice', '<br /><br /><button onclick="cancelRoomToCoach(\'' + roomid + '\'); return false;">OK</button>');		
  		} 	
	  },
		onFailure: function()		
		{
			Element.update('roomAllocateChoice','<font color="red"><b>Sorry, We could not allocate the room at this time.</b></font>');
			new Insertion.Bottom('roomAllocateChoice', '<br /><br /><button onclick="cancelRoomToCoach(\'' + roomid + '\'); return false;">OK</button>');	
		}	
	});
}

function closeRoomToCoach()
{
	Element.hide('roomAllocateChoice');
	droppeditem = false;
}

function cancelRoomToCoach(roomid)
{
	Element.show('room'+roomid);
	Element.hide('roomAllocateChoice');
	droppeditem = false;
}

function toggleCoachView(view,holidayid,coachid)
{
	if(view == 'current')
	{
		
		Element.show('coachCurrentView');
		Element.hide('coachDropView');	
		Element.hide('coachFullOverlay');
		loadCoachAllocation(holidayid,coachid);		
		
	}
	else
	{
		Element.hide('coachCurrentView');
		Element.show('coachDropView');
		setFreeCoachPlaces();
	}
}

function loadCoachAllocation(holidayid,coachid)
{	
	Element.update('coachCurrentLoading','<img src="/images/loadingdots.gif" width="21" height="5" />');
	Element.show('coachCurrentLoading');
	
	var url = '/members/load_coach_allocation.html';
	var d = new Date();
	var time = d.getTime();
	
	new Ajax.Request(url, {
	  method: 'get',
	  requestHeaders: {Accept: 'application/json'},
		parameters: {holiday: holidayid, coachid: coachid, r: time},	 
	  onSuccess: function(transport)
	  {	  	
	  	//clear current list
	  	Element.update('coachCurrentPaxList');	  	
	 		var coachPax = transport.responseText.evalJSON(); 		
	 		if(coachPax.length > 0)
	 		{
	 			var prevroom = coachPax[0].room;
	 			for(p=0;p<coachPax.length;p++)
	 			{ 	
	 				roomEndClass = ' ';
	 				if(coachPax[p].room != prevroom)
	 				{
	 					roomEndClass = ' class="roomSeperator"';	 						
	 				}
					new Insertion.Bottom('coachCurrentPaxList', '<div id="coachPax_' + coachPax[p].code + '"' + roomEndClass + '><input type="checkbox" onclick="removeFromCoach(this.value,' + holidayid + ');" value="' + coachPax[p].code + '" /> <label class="' + coachPax[p].sex + '">' + coachPax[p].name + ' (' + coachPax[p].travel + ')</label></div>');	
					prevroom = coachPax[p].room;
				}
				Element.hide('coachCurrentLoading');		
	 		}
	 		else
 			{
 				Element.update('coachCurrentLoading','<font color="red"><b>No people from your holiday are currently allocated to this coach, please click OK to allocate.</b></font>');
				new Insertion.Bottom('coachCurrentLoading', '<br /><br /><button onclick="toggleCoachView(\'allocate\'); return false;">OK</button>');		
 			}		
  		
	  },
		onFailure: function()		
		{
			Element.update('coachCurrentLoading','<font color="red"><b>Sorry, We could not load the current coach allocation at this time.</b></font>');
			new Insertion.Bottom('coachCurrentLoading', '<br /><br /><button onclick="toggleCoachView(\'allocate\'); return false;">OK</button>');	
		}	
	});		
}

function removeFromCoach(placeid,holidayid)
{	
	Element.update('coachCurrentLoading','<img src="/images/loadingdots.gif" width="21" height="5" />');
	Element.show('coachCurrentLoading');
	
	var url = '/members/save_coach_remove.html';
	var d = new Date();
	var time = d.getTime();
	
	new Ajax.Request(url, {
	  method: 'get',
	  requestHeaders: {Accept: 'application/json'},
		parameters: {placeid: placeid, holiday: holidayid, r: time},	 
	  onSuccess: function(transport)
	  {
	 		var removeResult = transport.responseText.evalJSON();	 		
	 		if(removeResult.status == 'OK')
	 		{	 			
				Element.hide('coachCurrentLoading');	
				Element.hide('coachPax_' + placeid);
				updateAllocatedCount(1,'r');
				loadRoomList(holidayid);				
	 		}
	 		else
 			{
 				Element.update('coachCurrentLoading','<font color="red"><b>' + removeResult.message + '</b></font>');
 			}		
  		
	  },
		onFailure: function()		
		{
			Element.update('coachCurrentLoading','<font color="red"><b>Sorry, We could not remove this person, try again later.</b></font>');			
		}	
	});
}
