

var xp_booking_Wait;
var xp_booking_Box;

function checkDefaultFeature(){
	var selectedFeatures='';
	var selectedFeatureTypes='';
	form=document.search_form;
	
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == "checkbox" && form.elements[i].checked==true){
			//alert(form.elements[i].id);
			mysplitString=(form.elements[i].id).split('^');	
		    selectedFeatures=selectedFeatures+'^'+mysplitString[1];
			selectedFeatureTypes=selectedFeatureTypes+'^'+mysplitString[0];
		}
		document.getElementById('selectedFeatures').value=selectedFeatures;
		document.getElementById('selectedFeatureTypes').value=selectedFeatureTypes;
	}
}

function checkSelectedFeature(){
	checkDefaultFeature();
   /*	Update region list and category list*/
	advanceSearchAjaxUpdator('','');
	
}

//////////////// Global variables

numOfDaysToDisplay=21;  // Number of days in a page (select days to book)

function showMoreOptions(){
	
	
	//document.getElementById('advanceSearch').style.display='inline';
	document.getElementById('showOptions').style.display='none';
	document.getElementById('hideOptions').style.display='inline';
	
	
	
}

function hideMoreOptions(){
	
	//document.getElementById('advanceSearch').style.display='none';
	document.getElementById('showOptions').style.display='inline';
	document.getElementById('hideOptions').style.display='none';
	
	
	
}




function populateEndDate(){
	
	startDate=document.getElementById('StartDate').value;
	if(startDate){
		//alert(startDate);
		tmp = startDate.split("-");
		var start = new Date(tmp[2],(tmp[1]-1),tmp[0]);
		//alert(start);
		start.setDate (start.getDate() + 21);
		
		var end_date = start.getDate();
	
	    var end_month = start.getMonth();
	
	    var end_year = start.getFullYear();
	    
	    if(end_date<10){
	    	
	    	end_date='0'+end_date;
	    }
	    
	    endDate=end_date+'-'+(end_month+1)+'-'+end_year;
	    
	   document.getElementById('endDate').value=endDate;
	}
	
}

/*
  Check whether there is any unavailable dates between startDate and endDate  
*/

function checkNonContinuousDatesSelection(startDate,endDate){
	
	
	
	   for (var i = startDate; i < endDate; i++) {
				//alert(123);
				
	
				  
					if(document.getElementById(i).className=='sold')
				    
					return false;
				 }
			   
			   
	  return true;
}

/*
CHECK BOX implementation is in common.js backup file
Event triggered when a day is selected in the date selector   

*** Each time this event is triggered, it is considered to be a new booking session, all booking info is refreshed (how many rooms selected for a day, which extras are selected) 

*/


var numOfSelection=0;   // 1 stands for first selection, 2 for second selection
var currentSelection;   // the id of the date currently selected
//var nonContinuousDaysSelection;


function bookingSelectDates(id){

		  /// switch between 1 and 2
			if(numOfSelection!=2){
				
				numOfSelection++;
			}
			else{
				
				numOfSelection=1;
			}
		

		
//*************** first selection
		  if(numOfSelection==1){
		  	
		    // unhighlight all existing highlighted cell
		    for(i=0;i<numOfDaysToDisplay;i++){
		  		
		  		document.getElementById(i).className=idClassNameArray[i];
		  		
		  	}
		  	
		  	highlight(id);  //highlight selected date
		  	currentSelection=id;
		  	//### Reset selectedExtrasId to blank. Comment this out when it needs to keep the extras selected  
			 document.getElementById('selectedExtrasId').value='';
			 document.getElementById('selectedExtrasType').value='';
			 document.getElementById('selectedExtrasUnitPrice').value='';

			step2_1=translate('step2_1');
			step2_2=translate('step2_2');
			step2_3=translate('step2_3');
			 
			document.getElementById('instruction').innerHTML="<div id=\"error\">1."+step2_1+" <br>2."+step2_2+"<br>3."+step2_3+"</div>";
		  	
			
			showPaymentSummary();
		   
		  	
		  }
		  
//*************** second selection
		 if(numOfSelection==2){
		 	
		 	id=parseInt(id);
		  	currentSelection=parseInt(currentSelection);
		  
		  	    //*********** When second is greater than first
		  		if(id>currentSelection){
		  			
		  			 if(checkNonContinuousDatesSelection(currentSelection,id)){
			  			for (var i = currentSelection; i <= id; i++) {
			  				
			  				document.getElementById(i).className='highlight';
			  				
			  			}
			  			//### Reset selectedExtrasId to blank. Comment this out when it needs to keep the extras selected  
			  			document.getElementById('selectedExtrasId').value='';
			  			document.getElementById('selectedExtrasType').value='';
			            document.getElementById('selectedExtrasUnitPrice').value='';
			  			showPaymentSummary();
		  			 }
		  			 else{
		  			 	
		  			 	numOfSelection=1;  // Reset to 1 so that user can make another selection based on current startDate
		  	        	alert(translate('continuous_dates'));
		  	        	return;
		  			 }
		  			step3_1=translate('step3_1');
			        step3_2=translate('step3_2');
		  			document.getElementById('instruction').innerHTML="<div id=\"error\"><br>1."+step3_1+"<br>2."+step3_2+"</div>";
		  		}
		  		 //*********** When first is greater than second
		  		else if(id<currentSelection){
		  			if(checkNonContinuousDatesSelection(id,currentSelection)){
			  			for (var i = id; i <= currentSelection; i++) {
			  				
			  				document.getElementById(i).className='highlight';
			  				
			  			}
			  			//### Reset selectedExtrasId to blank. Comment this out when it needs to keep the extras selected  
			  			document.getElementById('selectedExtrasId').value='';
			  			document.getElementById('selectedExtrasType').value='';
			            document.getElementById('selectedExtrasUnitPrice').value='';
			  			showPaymentSummary();
		  			}
		  			else{
		  				
		  				numOfSelection=1; // Reset to 1 so that user can make another selection based on current startDate
		  				alert(translate('continuous_dates'));
		  	        	return;
		  			}
		  			
//		  			alert(123);
		  			document.getElementById('instruction').innerHTML="<div id=\"error\"><br>1.Click on ANY date to reset the selection<br>2.Select number of rooms and optional extras and preceed with your booking.</div>";
		  		}
		  		
		  		//*********** When first is equal to second (cancel the first selection)
		  		 else if(id==currentSelection){
		  			
		  		 	        step1=translate('step1');
			  				document.getElementById('instruction').innerHTML='<div id="error">'+step1+'</div>';
			  				document.getElementById(id).className=idClassNameArray[id];
			  				document.getElementById('paymentSummary').innerHTML='';
			  				document.getElementById('subtotalList').innerHTML='';
			  				clearHiddenBookingDataAM();
			  				//showPaymentSummary();
			  				
		  			
		  		}
		  		
		 	
		 } 
  
}

/*
To clear hidden booking data for AM products
*/
 function clearHiddenBookingDataAM(){
 	
 	
 	document.getElementById('numOfSelectedDate').value='';
    document.getElementById('selectedExtrasType').value='';
    document.getElementById('selectedExtrasUnitPrice').value='';
 }

 function bookingSelectDateAT(id){
	     
          // don;t do anything if the highlighted cell is clicked
 	      if(currentSelection!=id){
           
 	            highlight(id);  //highlight selected date
 	            document.getElementById('numOfSelectedDate').value=1;
 	            document.getElementById('selectedSessions').value=0;
 	            document.getElementById('selectedDate').value=id;
 	            
// 	            document.getElementById('instruction').innerHTML="<div id=\"error\"><br>1.Click on ANY date to reset the selection<br>2.Select number of sessions and preceed with your booking.</div>";
 	            step2_1=translate('AT_step2_1');
			    step2_2=translate('AT_step2_2');
			 
			    document.getElementById('instruction').innerHTML="<div id=\"error\">1."+step2_1+" <br>2."+step2_2+"</div>";
     
                document.getElementById('subtotalList').innerHTML='';
 	            
 	             
 	            //show session list
 	            showSessions(id);
		  		
 	            if (document.getElementById(currentSelection)){
		  		   document.getElementById(currentSelection).className=idClassNameArray[currentSelection];
		         }

		   		
		  	    currentSelection=id;  
 	       }
 	       
 	      

	
}

function showSessions(id){
	
	var startDate=document.getElementById('startDate').value;
    var pid=document.getElementById('pid').value;
    var url='ajax_booking.php?mode=show_sessions&startDate='+startDate+'&dateSelected='+id+'&pid='+pid;
    ajax_action_booking(url,'sessionList','N');
	
}

function showPaymentSummary(){
	
			var selectedDates='';
			var selectedPrices='';
			
			var numOfSelectedDate=0;
			
			for(i=0;i<numOfDaysToDisplay;i++){
				
				if(document.getElementById(i).className=='highlight'){
					
					
					selectedDates=selectedDates+'^'+i;
					selectedPrices=selectedPrices+'^'+idPriceArray[i];
					numOfSelectedDate++;
					
				}
				
			}
			
			//assign value to hidden field
			document.getElementById('numOfSelectedDate').value=numOfSelectedDate;
			
			//alert(selectedDates);
			startDate=document.getElementById('startDate').value;
			pid=document.getElementById('pid').value;
			max_people=document.getElementById('max_people').value;
			
			url='ajax_booking.php?mode=show_payment_summary&selectedDates='+selectedDates+'&startDate='+startDate+'&selectedPrices='+selectedPrices+'&pid='+pid+'&max_people='+max_people;
			//alert(url);
			
			document.getElementById('paymentSummary').innerHTML='<img src="images/loading_animation_liferay.gif" alt="Loading payment summary" border="0">';
		    ajax_action_booking(url,'paymentSummary','N');
		    MultipleAjax='subtotal';  //set paramenter for second Ajax request
		    //Set hidden value
		    
              document.getElementById('selectedPrices').value=selectedPrices;
              document.getElementById('selectedDates').value=selectedDates;
              
		    
}


var idAvailArray = new Array();
var idClassNameArray = new Array();
var idPriceArray = new Array();

var restore_mode;
var xp_ajax_restore;
var restore_string = new Array();
function iniCheckOutdetails(){
		var restore_flag = arguments[0];
	             clearHiddenBookingDataAM();  //clear hidden booking data;			
	
			/*********** prepare the id and availablilty array*******************/
			   
				for(i=0;i<numOfDaysToDisplay;i++){
					
					var availID='avail'+i;
					var priceID='price'+i;
					
					priceIDOBJ=document.getElementById(priceID);
					
					idAvailArray[i]=document.getElementById(availID).value;
					idClassNameArray[i]=document.getElementById(i).className;
					
					if(priceIDOBJ)
					  idPriceArray[i]=priceIDOBJ.value;
					else
					  idPriceArray[i]='';
					
					
					//alert(idClassNameArray[i]);
					
				}
				
				step1=translate('step1');
				document.getElementById('instruction').innerHTML='<div id="error">'+step1+'</div>';
				if (restore_flag == 'restore_am'){restore_begin(); restore_mode = restore_flag;}
}

function restore_begin(){
		xp_ajax_restore = new xpAjax('xp_ajax_restore');
		xp_ajax_restore.aget('ajax_booking.php?mode=restore','restore_page_step1()');
}

function restore_page_step1(){
	//document.getElementById('restore').innerHTML = xp_ajax_restore.data;
	var id=xp_ajax_restore.data.split('^');	
	id.shift();
	xp_ajax_restore = null;
	restore_string.splice(0);
	restore_string[0] = 'bookingSelectDates('+id[0]+')';
	if (id.length > 1){	
		restore_string.push('bookingSelectDates('+parseInt(id[id.length-1])+')');
		//restore_string = 'bookingSelectDates('+id[id.length-1]+', "restore_page_step2()")';
	}
	restore_string.push('restore_page_exchange("ajax_booking.php?mode=restore_roomNo","restore_page_step2()")');
	eval(restore_string.shift());
}
function restore_page_step2(){
	var id=xp_ajax_restore.data.split('^');
	restore_string.splice(0);
	if (id[0] == ''){id.shift();}
	for (i =0; i<id.length; i++ ){
		//alert(num_book_id);
		document.getElementById('num_book'+(i+1)).selectedIndex = parseInt(id[i])-1;
	}
	restore_string.push('restore_page_exchange("ajax_booking.php?mode=restore_peopleNo","restore_page_step3()")');
	eval(restore_string.shift());
}
function restore_page_step3(){
	
	//alert(xp_ajax_restore.data);
	var id=xp_ajax_restore.data.split('^');
	restore_string.splice(0);
	if (id[0] == ''){id.shift();}
	for (i = 0; i<id.length; i++ ){
		document.getElementById('num_people'+(i+1)).selectedIndex = parseInt(id[i])-1;
	}
	//restore_string.push('changePeopleNum("num_people'+id.length+'",'+parseInt(id.pop())+')');
	restore_string.push('restore_page_exchange("ajax_booking.php?mode=restore_Extra","restore_page_step4()")');
	eval(restore_string.shift());
}


function restore_page_step4(){
	//alert(xp_ajax_restore.data);
	var id=xp_ajax_restore.data.split(',');
	restore_string.splice(0);
	if (id[0] == ''){id.shift();}
	for (i = 0; i<id.length; i++ ){
		document.getElementById(id[i]).checked = 'checked';
	}
	restore_string.push('selectOptionalExtra("","'+parseInt(id.pop())+'")');
	restore_string.push('restore_page_end()');
	eval(restore_string.shift());
}

function restore_page_end(){
	restore_string.splice(0);
}
function restore_page_exchange(url, fnName){
	restore_string.splice(0);
	xp_ajax_restore = new xpAjax('xp_ajax_restore');
	xp_ajax_restore.aget(url, fnName);
}



function iniCheckOutdetailsActivity(restore_flag){

				
			/*********** prepare the id and availablilty array*******************/
			   
				for(i=0;i<numOfDaysToDisplay;i++){
					
					var availID='avail'+i;
					var priceID='price'+i;
					
					priceIDOBJ=document.getElementById(priceID);
					
					idAvailArray[i]=document.getElementById(availID).value;
					idClassNameArray[i]=document.getElementById(i).className;
					
					if(priceIDOBJ)
					  idPriceArray[i]=priceIDOBJ.value;
					else
					  idPriceArray[i]='';
					
					
					//alert(idClassNameArray[i]);
					
				}
				
				document.getElementById('instruction').innerHTML='<div id="error">'+translate('step1')+'</div>';
				if (restore_flag == 'restore_at_ev'){restore_begin_at_ev();restore_mode = restore_flag;}
}

function restore_begin_at_ev(){
		xp_ajax_restore = new xpAjax('xp_ajax_restore');
		xp_ajax_restore.aget('ajax_booking.php?mode=restore_at_ev','restore_page_step_at_ev_1()');
}

function restore_page_step_at_ev_1(){
	var id=xp_ajax_restore.data.split('^');	
	xp_ajax_restore = null;
	restore_string.splice(0);
	MultipleAjax='total';
	
	restore_string.push('bookingSelectDateAT('+id[0]+')');
	restore_string.push('document.getElementById(\'num_book0\').selectedIndex = '+id[1]+';changeSessionNum();');
	
	eval(restore_string.shift());
}


function iniCheckOutStep1(){
	
			url='ajax_booking.php?mode=show_booking_summary';
			
		    ajax_action_booking(url,'bookingSummary','N');
	
}

function proceedToPersonalDetails(){

	var numOfSelectedDate=document.getElementById('numOfSelectedDate').value
	
	
/*	Accommodation*/

	if(document.getElementById('min_night')){
		var min_night=document.getElementById('min_night').value
		if(parseInt(numOfSelectedDate)<parseInt(min_night)||numOfSelectedDate==''){
			
//			alert('You need to select at least '+min_night+' day(s) to proceed');
            minStay=min_night;  //assign value to global var in Language javascrit
            alert(translate('min_stay'));
			return false;
		}
	}
	else{
		if(document.getElementById('selectedSessions')){
			if(document.getElementById('selectedSessions').value<1){
				alert(translate('min_session'));
			    return false;
			}
		}
	}
	return true;
	//document.selectDates.submit();
}
var isIE;
try {
	var el_test = document.createElement('input name=test');
	isIE = 'isIE';
}catch(err){
	isIE = 'notIE';
}
var xp_ajax_payment;
function Confirm_booking(){
	if (proceedToPersonalDetails() == true){
		document.forms['final_Submit_form'].innerHTML='';
		moveElements('selectDates','final_Submit_form');
		moveElements('checkout_person_info','final_Submit_form');
		moveElements('checkout_payment','final_Submit_form');
		var el;
		if (isIE == 'isIE'){
			el = document.createElement('input name="_submit_for_booking"');
		}else{
			el = document.createElement('input');
			el.name = "_submit_for_booking";
		}
		el.type = "hidden";
		el.value = "finished";
		document.forms['final_Submit_form'].appendChild(el);
		document.getElementById('_rest').style.display="inline";
		document.getElementById('_submit').style.display="inline";
		document.getElementById('_confirm').style.display="none";
		
		var i = 0;
		var e = document.getElementById('selectedDateList');
		if (e==null){return;}
		//alert(e.innerHTML);
		for(var tBody = e.firstChild; tBody != null; tBody = tBody.nextSibling){
			var tB_c = document.createElement(e.firstChild.tagName);
			var tR_c = new Array();
			var i_tR = 0;
			if (tBody.nodeType == 1){//it is tr tag
				for(var tR = tBody.firstChild; tR != null; tR = tR.nextSibling){
					tR_c[i_tR] = document.createElement(tBody.firstChild.tagName);
					var tD_c = new Array();
					var i_tD = 0;
					if (tBody.nodeType == 1){//it is td or th tag
						for(var tD = tR.firstChild; tD != null; tD = tD.nextSibling){
							tD_c[i_tD] = document.createElement(tR.firstChild.tagName);
							var el = tD.cloneNode(true);
							if (tD.nodeType != 1 || tD.firstChild == null){
								continue;
							}else{
								var insideTag = tD.firstChild;
								if (insideTag.nodeType == 1 && insideTag.tagName.toLowerCase() == 'select'){
									el = document.createTextNode(insideTag.value);
								}else{
									el = insideTag.cloneNode(true);
								}
							}
							
							tD_c[i_tD].appendChild(el);
							i_tD++;
						}
						i_tD = 0;
						while (i_tD < tD_c.length){
							tR_c[i_tR].appendChild(tD_c[i_tD]);
							i_tD++;
						}
						i_tR++;
					}
				}
				i_tR = 0;
				while (i_tR < tR_c.length){
					tB_c.appendChild(tR_c[i_tR]);
					i_tR++;
				}
			}
		}
		var tAble;
		tAble = document.createElement('table'); 
		tAble.setAttribute('class','selectedDateList'); 
		tAble.setAttribute('border','0');
		tAble.setAttribute('width','347');
		tAble.appendChild(tB_c);
		
		document.getElementById('recipt_display').innerHTML += ''
		+'<div class="Confirm_detail">'
		+'	<h3>'+translate_language("Booking_Detail")+':</h3>';
		
		document.getElementById('recipt_display').appendChild(tAble);
		
		var extraTemp = document.createElement('div');
		extraTemp.id='extraTemp';
		document.getElementById('recipt_display').appendChild(extraTemp);
			
		document.getElementById('recipt_display').innerHTML += ''
		+'	<h3>'+translate_language("Personal_Detail")+':</h3>'
		+'	<table border="0" cellpadding="2" cellspacing="2" class="selectedDateList" width="347">'
      	+'	  <tr>'
        +'			<th colspan="5">'+translate_language("lbl_yourdetails")+'</td>'
      	+'	  </tr>'
	    +'	  <tr>'
	    +'	    <td>'+translate_language("lbl_fname")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtFirstName.value+'</td>'
	    +'	    <td rowspan="5">&nbsp;</td>'
	    +'	    <td>'+translate_language("lbl_lname")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtLastName.value+'</td>'
	    +'	  </tr>'
	    +'	  <tr>'
	    +'	    <td>'+translate_language("lbl_phone")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtPhone.value+'</td>'
	    +'	    <td>'+translate_language("lbl_mobile")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtMobilePhone.value+'</td>'
	    +'	  </tr>'
	    +'	  <tr>'
	    +'	    <td>'+translate_language("lbl_email")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtEmail.value+'</td>'
	    +'	    <td>'+translate_language("lbl_address")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtAddress.value+'</td>'
	    +'	  </tr>'
	    +'	  <tr>'
	    +'	    <td>'+translate_language("lbl_suburb")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtSuburb.value+'</td>'
	    +'	    <td>'+translate_language("lbl_postcode")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtPostCode.value+'</td>'
	    +'	  </tr>'
	    +'	  <tr>'
	    +'	    <td>'+translate_language("lbl_state")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtState.value+'</td>'
	    +'	    <td>'+translate_language("lbl_country")+':</td>'
	    +'	    <td>'+document.forms['checkout_person_info'].txtCountry.value+'</td>'
	    +'	  </tr>'
	    +'	</table>'
		+'	<h3>'+translate_language("lbl_secure_billing")+'</h3>'
		+'	<table border="0" cellpadding="2" cellspacing="2" class="selectedDateList" width="347">'
		+'	    <tr>'
		+'	      <th colspan="5">'+translate_language("lbl_secure_billing")+'</th>'
		+'	    </tr>'
		+'	    <tr>'
		+'	      <td>'+translate_language("Card Holder Name")+':</td>'
		+'	      <td>'+document.forms['checkout_payment'].cc_name.value+'</td>'
		+'	    </tr>'
		+'	    <tr>'
		+'	      <td>'+translate_language("Card Number")+':</td>'
		+'	      <td>'+document.forms['checkout_payment'].cc_number.value+'</td>'
		+'	    </tr>'
		+'	    <tr>'
		+'	      <td>'+translate_language("Expiry")+':</td>'
		+'	      <td nowrap="nowrap">'+document.forms['checkout_payment'].cc_month.value+'/'+document.forms['checkout_payment'].cc_year.value+'</td>'
		+'	    </tr>'
		+'	    <tr>'
		+'	      <td>'+translate_language("Card Type")+':</td>'
		+'	      <td>'+document.forms['checkout_payment'].creditcard_type.value+'</td>'
		+'	    </tr>'
		+'	    <tr>'
		+'	      <td>'+translate_language("CVN")+':</td>'
		+'	      <td>'+document.forms['checkout_payment'].cc_cvn.value+'</td>'
		+'	    </tr>'
		+'	  </table>'
		+'</div>';
		document.getElementById('recipt_display').style.display = 'none';

		xp_ajax_payment = new xpAjax('xp_ajax_payment');
		if (document.getElementById('ptype').value == 'AM'){
			selectedExtrasId=document.getElementById('selectedExtrasId').value;
			xp_ajax_payment.aget('ajax_booking.php?mode=show_confirm_payment&selectedExtrasId='+selectedExtrasId, "ajax_confirm_booking_render('extraTemp')");
		}else{
			selectedSessionId = document.getElementById('selectedSessionId').value;
    		selectedSessionNum = document.getElementById('selectedSessionNum').value;
			xp_ajax_payment.aget('ajax_booking.php?mode=update_session_subtotal&subtotal=0&selectedSessionId='+selectedSessionId+'&selectedSessionNum='+selectedSessionNum, "ajax_confirm_booking_render('extraTemp')");
		}
		return false;
	}
}

function ajax_confirm_booking_render(divName){
	document.getElementById('recipt_display').style.display = '';
	document.getElementById(divName).innerHTML = xp_ajax_payment.data;
	xp_ajax_payment = null;
	xp_booking_Wait = new xpWait('xp_booking_Wait',0.7,"#000",1);
	xp_booking_Box = new xpBox('xp_booking_Box');
	xp_booking_Box.setContent(document.getElementById('recipt_box').innerHTML);
	document.getElementById('recipt_box').innerHTML = '';
	return false;
}

function submit_for_booking(){
//	var el;
//	if (isIE == 'isIE'){
//			el = document.createElement('input name="restore"');
//		}else{
//				el = document.createElement('input');
//				el.name= 'restore';
//		}
//		
//	el.type='hidden';
//	el.value = document.getElementById('restore').innerHTML;
//	
//	document.forms['final_Submit_form'].appendChild(el);
	
	//alert(document.final_Submit_form.innerHTML);
	document.final_Submit_form.submit();
}
function submit_for_booking_finish(){
	//alert(document.final_Submit_form.innerHTML);
	
	alert(document.bookingSelectDatesForm.innerHTML);
	document.final_Submit_form.submit();
}
function rest_recipt(){
	document.getElementById('recipt_box').innerHTML = xp_booking_Box.getContainer().innerHTML;
	xp_booking_Box.stop();
	xp_booking_Wait.stop();
	document.final_Submit_form.innerHTML='';
	document.getElementById('recipt_display').innerHTML = '';
	document.getElementById('_rest').style.display="none";
	document.getElementById('_submit').style.display="none";
	document.getElementById('_confirm').style.display="inline";
}
//Get values from form elements

function moveElements(sourceTag, targetTag){
	var el;
	var i = 0;
	var L = document.forms[sourceTag].elements.length;
	while (i < L){
		//get value
		var eo = document.forms[sourceTag].elements[i]	
		//ONLY get checked values
		if (eo.type=="checkbox" || eo.type=="radio"){
			if(eo.checked == false){
			i++;
			continue;	
			}
		}
		//create element
		
		if (isIE == 'isIE'){
			el = document.createElement('input name="'+eo.name+'"');
		}else{
				el = document.createElement('input');
				el.name= eo.name;
		}
		
		el.type='hidden';
		el.value = eo.value;
		document.forms[targetTag].appendChild(el);
		i++;
	}
	return;
}

/*

when user changes the numbers of rooms for a day

*/

var roomNoString;  // Define as global, so it can be used when updating optional extras 

function changeRoomNum(id,value){
	
/*	Initialize no of people  */
  
                var peopleNoID='num_people'+id.substr(id.length-1,1);
                document.getElementById(peopleNoID).value=value;
                
                updateAccommSubtotal();
			    
			  /*  
                var numOfSelectedDate=document.getElementById('numOfSelectedDate').value  //num of dates selected from calendar
				var dropDownId;    //id of the "no of rooms" dropdown box
			    var selectedExtraIdString='';
                
			    roomNoString='';
			    peopleNoString='';	
				
				//populate the  tring contains numbers booked for each room string 
				for(i=1;i<=numOfSelectedDate;i++){
					
					dropDownId='num_book'+i;
					peopleNoId='num_people'+i;
					roomNoString=roomNoString+'^'+document.getElementById(dropDownId).value; //pass to php to get the total
					peopleNoString=peopleNoString+'^'+document.getElementById(peopleNoId).value;
					
				}
					
//				alert(peopleNoString);
				
				    
				selectedExtrasPrice=document.getElementById('selectedExtrasPrice').value; //pass to php to get the total
				selectedExtrasId=document.getElementById('selectedExtrasId').value;  //passed to php to keep the selected extra
				
			
				
			url='ajax_booking.php?mode=update_subtotal&roomNoString='+roomNoString+'&selectedExtrasPrice='+selectedExtrasPrice+'&selectedExtrasId='+selectedExtrasId+'&peopleNoString='+peopleNoString;
				
				
				//alert(url);
			
				ajax_action_booking(url,'subtotalList','N');*/
	
}

/* When total number of a day is changed */

function changePeopleNum(id,value){
	
         /*	Initialize no of people  */
  
                var roomNoID='num_book'+id.substr(id.length-1,1);

                var NoOfRooms=document.getElementById(roomNoID).value;
                
                var maxPeoplePerRoom=document.getElementById('max_people').value;
        /* Exceed max people allowed               */
                if(value>maxPeoplePerRoom*NoOfRooms){
                	
                	
                	alert(translate('max_num'));
                	document.getElementById(id).value=NoOfRooms; // reset the people total to default
                	 updateAccommSubtotal();
                	return false;
                }
       /* Less than room number            */
       
                 if(value<NoOfRooms){
                	
                 	alert(translate('less_people_than_room'));
                	document.getElementById(id).value=NoOfRooms; // reset the people total to default
                	 updateAccommSubtotal();
                	return false;
                }
                	
                updateAccommSubtotal();
	
}

/*
update subtotal when user make anychange on room booking page
*/
function updateAccommSubtotal(){
	
	  
                var numOfSelectedDate=document.getElementById('numOfSelectedDate').value  //num of dates selected from calendar
				var dropDownId;    //id of the "no of rooms" dropdown box
			    var selectedExtraIdString='';
                
			    roomNoString='';
			    peopleNoString='';	
				
				//populate the  tring contains numbers booked for each room string 
				for(i=1;i<=numOfSelectedDate;i++){
					
					dropDownId='num_book'+i;
					peopleNoId='num_people'+i;
					roomNoString=roomNoString+'^'+document.getElementById(dropDownId).value; //pass to php to get the total
					peopleNoString=peopleNoString+'^'+document.getElementById(peopleNoId).value;
					
				}
					
//				alert(peopleNoString);
				
				  
				selectedExtrasUnitPrice=document.getElementById('selectedExtrasUnitPrice').value; //pass to php extra prices
				selectedExtrasId=document.getElementById('selectedExtrasId').value;  //passed to php to keep the selected extra
				selectedExtrasType=document.getElementById('selectedExtrasType').value;  //passed to php to keep the selected extra type
				
			url='ajax_booking.php?mode=update_subtotal&roomNoString='+roomNoString+'&selectedExtrasUnitPrice='+selectedExtrasUnitPrice+'&selectedExtrasType='+selectedExtrasType+'&selectedExtrasId='+selectedExtrasId+'&peopleNoString='+peopleNoString;
				
				
				//alert(url);
			
				ajax_action_booking(url,'subtotalList','N');
	
}

/*

when user changes the numbers of sessions for a day

*/

function changeSessionNum(){
	
	var numOfSelectedSession=document.getElementById('numOfSelectedSession').value  //num of sessions for the selected date
	var selectedSession=0;
	var subtotal=0;
	var selectedSessionId='';
	var selectedSessionPrice='';
	var selectedSessionNum='';
	

	for(i=0;i<numOfSelectedSession;i++){
		
		dropDownId='num_book'+i;
		priceId='price'+i;
		sid='sid'+i;
		thisSessionNum=document.getElementById(dropDownId).value;
		thisSessionPrice=document.getElementById(priceId).value;
		thisSessionID=document.getElementById(sid).value;
		
		
		selectedSession+=parseInt(thisSessionNum); //total sessions selected
		subtotal+=thisSessionNum*thisSessionPrice
		
		if(thisSessionNum!=0){
		selectedSessionId=selectedSessionId+'^'+thisSessionID;
		selectedSessionPrice=selectedSessionPrice+'^'+thisSessionPrice;
		selectedSessionNum=selectedSessionNum+'^'+thisSessionNum;
		}
	}
	
    document.getElementById('selectedSessions').value=selectedSession;
    document.getElementById('selectedSessionId').value=selectedSessionId;
    document.getElementById('selectedSessionPrice').value=selectedSessionPrice;
    document.getElementById('selectedSessionNum').value=selectedSessionNum;
	
    //alert(subtotal+'^'+selectedSession);
	
    url='ajax_booking.php?mode=update_session_subtotal&subtotal='+subtotal+'&selectedSessionId='+selectedSessionId+'&selectedSessionNum='+selectedSessionNum;
				
				
				//alert(url);
			
				ajax_action_booking(url,'subtotalList','N');
}


//var selectedExtraId = new Array();    //Array contains the ids of extras selected

/// Function to update subtatal when an extra is selected or unselected

function selectOptionalExtra(obj,id){
	
//				alert(roomNoString);
//				
//	            mysplitString=id.split('^');
//				
//				selectedExtraIdString='';
//				roomTotal=0;  //total numbers for all rooms selected
//			    
//				get updated selectedExtraId list since 'selectedExtrasId' is reset in bookingSelectDates each time use click on date selector
//				selectedExtraId=(document.getElementById('selectedExtrasId').value).split(',');
//				
//				 if selectedExtraId is blank (new booking session), set the price hidden field to 0
//				if(selectedExtraId==''){
//					selectedExtrasPrice=0;
//				}
//				else
//				selectedExtrasPrice=document.getElementById('selectedExtrasPrice').value;
//			     
//				
//			    
//				selectedId=mysplitString[2];
				
	/*	Update the hidden field	*/
	
	  			   var selectedExtraIdString='';
	  			   var selectedExtraTypeString='';
	  			   var selectedExtraUnitPriceString='';
	  			   
				   form=document.optinalExtraForm;
				
					for (var i = 0; i < form.elements.length; i++) {
		                 
						 if (form.elements[i].type == "checkbox"  && !form.elements[i].disabled&&form.elements[i].checked){
//		                 	 alert(form.elements[i].checked+form.elements[i].id);
		                 	 mysplitStringNew=form.elements[i].id.split('^');
                             selectedExtraIdString=selectedExtraIdString+','+mysplitStringNew[2];
                             selectedExtraTypeString=selectedExtraTypeString+','+mysplitStringNew[3];
                             selectedExtraUnitPriceString=selectedExtraUnitPriceString+','+mysplitStringNew[1];
   
		                 }
		                     
	                }

	                document.getElementById('selectedExtrasId').value=selectedExtraIdString;
	                document.getElementById('selectedExtrasUnitPrice').value=selectedExtraUnitPriceString;
	                document.getElementById('selectedExtrasType').value=selectedExtraTypeString;
	                updateAccommSubtotal();
//	                
//				
///*	When the check boxed is checked		*/
//				if(obj.checked==true){
//					
//					if(selectedExtraId!='')
//					  index=(selectedExtraId.length);
//					else   //when hidden field selectedExtrasId is blank
//					  index=(selectedExtraId.length-1);
//
//					selectedExtraId[index]=selectedId;
//					
//					document.getElementById('selectedExtrasId').value=selectedExtraId;
//					document.getElementById('selectedExtrasPrice').value=parseInt(selectedExtrasPrice)+parseInt(mysplitString[1]);
//				
//				}
///*	When the check boxed is unchecked		*/			
//				else{
//					
//					
//				   //alert(selectedExtraId);
//					selectedExtraId.remove(selectedId);
//					document.getElementById('selectedExtrasId').value=selectedExtraId;
//					document.getElementById('selectedExtrasPrice').value=parseInt(selectedExtrasPrice)-parseInt(mysplitString[1]);
//				}
//				
//				
//				   
//				    //if room numbers have been changed from default (1)
//            
//				   if(roomNoString){
//                   	numStringOfRooms=roomNoString.split('^');
//				    currentNumber=0;
//			     		for(i=0;i<numStringOfRooms.length;i++){
//				     	
//			     		//alert(numStringOfRooms.length);
//			     		//alert(numStringOfRooms[i]);
//			     		currentNumber=convertToIntForSum(numStringOfRooms[i]);
//				     	roomTotal=roomTotal+currentNumber;
//				     	
//				     	selectedExtrasPriceTotal=roomTotal*document.getElementById('selectedExtrasPrice').value;
//				     	
//			     		}
//				   }				
//				 else
//				    selectedExtrasPriceTotal=document.getElementById('selectedExtrasPrice').value*document.getElementById('numOfSelectedDate').value
//				    
//				  //alert(selectedExtrasPriceTotal);
//				     //alert(selectedExtrasPriceTotal);
//			
//				     for(i=0;i<selectedExtraId.length;i++){
//				     	
//				     	selectedExtraIdString=selectedExtraIdString+'^'+selectedExtraId[i];
//				     }
//				
				     
				     
//				url='ajax_booking.php?mode=update_extras&selectedExtrasPriceTotal='+selectedExtrasPriceTotal+'&selectedExtraIdString='+selectedExtraIdString;
				
				//alert(url);
//				ajax_action_booking(url,'subtotalList','N');
	
}












///####################################  GENERAL Functions start####################################################################

var currentID='';
var originalClass;
function highlight(id){
    //alert(id);

	originalClass=document.getElementById(id).className;
	
	//unhighlight(currentID);
	currentID=id;

//alert(currentID+Globalhighlightcolor);
	document.getElementById(currentID).className='highlight';
}

function changeBackTo(id,highlightcolor)
{
	OBJToChange=document.getElementById(id);
	if(OBJToChange)
	OBJToChange.style.background=highlightcolor;
}

function unhighlight(id)
{
	alert(id);
	OBJToChange=document.getElementById(id);
	if(OBJToChange)
	OBJToChange.className=originalClass;
	
}



////////////////////////////////////////////////////Ajax booking Start//////////////////////////////////////////////////////////////////////////////////
var xmlHttp;
//var globalDIV;
var CurrentPopUpDIvUser;
var page_scroll;
var MultipleAjax;

function ajax_action_booking(url,div,page_scroll) {

	CurrentPopUpDIvUser=div;
	page_scroll=page_scroll;

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	url=url+"&sid="+Math.random();



	xmlHttp.onreadystatechange=stateChangedUser
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}



function stateChangedUser()
{

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var testError = /Error\scode\s\d+/;
		var urlTest_a =/activity_booking\.php/;
		var urlTest_b =/room_booking\.php/;
		if(testError.test(xmlHttp.responseText) && ( urlTest_a.test(window.location) || urlTest_b.test(window.location))){
			window.location = window.location +'&'+Math.random();
		}
		document.getElementById(CurrentPopUpDIvUser).innerHTML=xmlHttp.responseText
         
		if(MultipleAjax=='subtotal'){
			
			ajax_action_booking('ajax_booking.php?mode=show_subtotal','subtotalList','N');
			MultipleAjax='total';
			
		}
		
		if(MultipleAjax=='total' && restore_string[0]!='' && restore_string[0]!=null){
			//alert(restore_string[0]);
			eval(restore_string.shift());
		}
		
		/*if(MultipleAjax=='total'){
			ajax_action_booking_second('ajax_booking.php?mode=show_total','totalList','N');
		}*/
		
		
		if(page_scroll=='Y'){
		  setTimeout("pageScroll();",200);
		}

	}
}




function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


////////////////////////////////////////////////////Ajax booking End/////////////////////////////////////////////////////////////////////////

//Remove an element from array implement: array.remove(elementValue);

Array.prototype.remove=function(s){
	
  for(i=0;i<this .length;i++){
  	
    if(s==this[i]) this.splice(i, 1);
    
  }
  
}

Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}


function convertToIntForSum(num){
	
	newNum=(num=='')?0:parseInt(num);
	return newNum;
}

function fnCheckAgree(input, form){
	if (input.checked == true ){
		form._confirm.disabled = false;
	}else{
		form._confirm.disabled = true;
	}
}

///####################################  GENERAL Functions####################################################################


