document.observe('dom:loaded',function(e) {
	
	if($('evnentlets_table')){
		initialise_costs();
	}

	if($('registration_form')){	
		
		$('registration_form').observe('submit', function(event){			
				
			if(! ($('terms_agree').checked && $('privacy_agree').checked)){
				alert("Please indicate that you have read and agree with the terms and conditions and the privacy policy");
				Event.stop(event);	
			}
			
		});	
	}
	
	if($('paypal_form') && $('paypal_form').readAttribute('rel')=='auto'){		
		$('paypal_form').submit();	
	}
	
	if($('my_account_paynow_button')){
		$('my_account_paynow_button').observe('click', function(event){
			var cid = event.element().readAttribute('rel').split('_')[0];
			var cost = event.element().readAttribute('rel').split('_')[1];
			new Ajax.Request("/pb/controllers/customerController.php?", {
				method: "post",
				parameters: "action=updateCost&id=" +  cid + "&c=" + cost,
				onSuccess: function(transport){					
					$('paypal_form').submit();					
				}				
			});		
		});
	}
	
	
	if($('invite_spouse')){		
		if($('invite_spouse').checked){
			$('spouse_info').style.display="block";
		}else{
			$('spouse_info').style.display="none";
			$('spouse_name').value="";
		}
		
		$('invite_spouse').observe('click', function(e){			
			if($('invite_spouse').checked){
				Effect.Appear($('spouse_info'));
			}else{
				Effect.Fade($('spouse_info'));
				$('spouse_name').value="";
			}
			
			var totalInvoiceCost = parseFloat($('totalInvoiceCost').innerHTML);
			$$('.eventlet').each(function(item){
				if(item.readAttribute('rel').split("_")[0]=="1" && item.checked){					
					var cost = parseFloat(item.readAttribute('rel').split('_')[1]);
							
					if($('invite_spouse').checked){
						totalInvoiceCost += cost;							
					}else{
						totalInvoiceCost -= cost;	
					}
					
					updateCosts(totalInvoiceCost);
				}	
			});
								
		});
	}
	
	if($('need_visa')){		
		if($('need_visa').checked){
			$('visa_info').style.display="block";
			$('visa_info').disabled=false;	
		}else{
			$('visa_info').style.display="none";
			$('visa_info').disabled=true;	
		}
		
		$('need_visa').observe('click', function(e){
			if($('need_visa').checked){
				Effect.Appear($('visa_info'));
				$('visa_info').disabled=false;	
			}else{
				Effect.Fade($('visa_info'));
				$('visa_info').disabled=true;
			}	
		});
	}
	
	if($('need_room')){		
		if($('need_room').checked){
			$('room_info').style.display="block";
			$('room_info').disabled=false;	
		}else{
			$('room_info').style.display="none";
			$('room_info').disabled=true;	
		}
		
		$('need_room').observe('click', function(e){
			if($('need_room').checked){
				Effect.Appear($('room_info'));
				$('room_info').disabled=false;	
			}else{
				Effect.Fade($('room_info'));
				$('room_info').disabled=true;
			}	
		});
	}
	
	
	if($('num_rooms')){
		$('num_rooms').observe('change', function(event){			
			var num_rooms = $('num_rooms').value;
			var room_selects = $$('div.room_select');
			
			if(num_rooms>room_selects.length){
				add_rooms((num_rooms-room_selects.length), room_selects.length);
			}else{
				remove_rooms( room_selects.length-num_rooms, room_selects.length );
			}
				
		});	
	}
	
	if($('multiBookingNumber')){
		$('multiBookingNumber').observe('change', function(event){			
			var num_extra = $('multiBookingNumber').value;
			var cost = $('multiBookingNumber_'+num_extra).readAttribute('cost');
			$('groupTotalCost').innerHTML = cost;
			$('group_total_cost').value = cost;
			
			var discount_code = $('input_discount_code').value;
			updateTotalSaving();
			new Ajax.Request("/pb/controllers/eventBookingController.php?", {
				method: "post",
				parameters: "action=add&n=" +  num_extra,
				onSuccess: function(transport){					
					$('multiDiscountFormContainer').innerHTML =  transport.responseText ;
					$('multiDiscountFormContainer').style.display='block';
					
				}				
			});		
		});	
	}
	
	if( $('update_code_button') ){
		$('update_code_button').observe('click', function(event){
			updateTotalSaving();
		});
	}
	
	$$('.eventlet').each(function(item){
		item.observe('click', function(event){			
			var perPerson = item.readAttribute('rel').split('_')[0];
			var cost = parseFloat(item.readAttribute('rel').split('_')[1]);
			var totalInvoiceCost = parseFloat($('totalInvoiceCost').innerHTML);
			var spouseName = $('invite_spouse').checked;			
			
			if(item.checked){
				if(spouseName && perPerson=="1"){
					totalInvoiceCost = totalInvoiceCost + 2*cost;	
				}else{
					totalInvoiceCost = totalInvoiceCost + cost;	
				}				
			}else{
				if(spouseName && perPerson=="1"){
					totalInvoiceCost = totalInvoiceCost - 2*cost;	
				}else{
					totalInvoiceCost = totalInvoiceCost - cost;	
				}				
			}
			updateCosts(totalInvoiceCost);		
		});	
	});
	
	
	
});

function initialise_costs(){
	var totalInvoiceCost = parseFloat($('totalInvoiceCost').innerHTML);
	var invite_spouse = $('invite_spouse').checked;
	$$('.eventlet').each(function(item){
		var perPerson = item.readAttribute('rel').split('_')[0];
		if(item.checked){					
			var cost = parseFloat(item.readAttribute('rel').split('_')[1]);
			
			if(perPerson=="1" && invite_spouse){
				totalInvoiceCost += cost*2;	
			}else{
				totalInvoiceCost += cost;	
			}			
			
			updateCosts(totalInvoiceCost);
		}	
	});
}

function updateCosts(totalCost){
	$('totalInvoiceCost').innerHTML = totalCost.toFixed(2);
	if($('paypal_payment_option')){
		var paypal_surcharge = parseFloat($('paypal_surcharge').innerHTML);
		$('totalPayPalCost').innerHTML = (totalCost + ((paypal_surcharge/100) * totalCost)).toFixed(2);
	}		
}

function updateTotalSaving(){
	var num_extra = $('multiBookingNumber').value;
	var event_id = $('input_event_id').value;
	var discount_code = $('input_discount_code').value;
	new Ajax.Request("/pb/controllers/eventBookingController.php?", {
		method: "post",
		parameters: "action=updateCost&n=" +  num_extra + "&dc=" + discount_code + "&eid="+event_id,
		onSuccess: function(transport){
			var json = transport.responseText.evalJSON();
			$('groupTotalCost').innerHTML = json.totalCost;
			$('group_total_cost').value = json.totalCost;
			
			$('totalInvoiceCost').innerHTML = json.totalCost;
			if($('totalInvoiceCost')){
				$('totalPayPalCost').innerHTML = json.totalPayPalCost;
			}
			
			$('group_total_saving').value = json.totalSaving;
			$('groupTotalSaving').innerHTML = json.totalSavingText;
			
		}				
	});	
	
	
	
}

/**
<label style="width:auto; margin-right:30px" for="single_1">Room 1</label>
<label style="width:auto; margin-left:20px; margin-right:10px" for="single_1">Single:</label>
<input type="radio" name="room_1" checked="checked"/>													
<label style="width:auto; margin-left:20px;margin-right:10px" for="double_1">Double:</label>
<input type="radio" name="room_1"/>
**/

function add_rooms(number, current){
	for(var x=1; x<=number; x++){
		var container_div = new Element('div', {"class":"room_select", "id":"room_select_"+(x+current), "style":"display:none"});
		var label = new Element('label', {"style":"width:auto; margin-right:30px", "for":"single_"+(x+current)+""}).update("Room "+(x+current));
		var label_single = new Element('label', {"style":"width:auto; margin-left:20px; margin-right:10px", "for":"single_"+(x+current)+""}).update("Single:");
		var input_single = new Element('input',{"type":"radio", "name":"room_"+(x+current)+"","checked":"checked", "value":"single"});
		var label_double = new Element('label', {"style":"width:auto; margin-left:20px;margin-right:10px", "for":"double_"+(x+current)+""}).update("Double:");
		var input_double = new Element('input',{"type":"radio", "name":"room_"+(x+current)+"", "value":"double"});
		
		container_div.appendChild(label);
		container_div.appendChild(label_single);
		container_div.appendChild(input_single);
		container_div.appendChild(label_double);
		container_div.appendChild(input_double);
		
		$('room_types').appendChild(container_div);		
		Effect.Appear(container_div, {
			delay:(x/10) 
		});
	}
	
			
}

function remove_rooms(number, current){
	
	for(var x=current; x>(current-number); x--){
		Effect.Fade($('room_select_'+x), {
			delay:((current+1)-x)/10, 			
			afterFinish:function(effect){				
				effect.element.remove();				
			}	
		});
		
	}
}

