// JavaScript Document


$(function(){
		   
		 b1="<input type='button' name='Button' id='Button' value='Confirm Booking' onclick='validateFormPds()'>"; 
				 b2=" <input type='submit' name='Button' value='Pay now by credit card' ONCLICK='return validateForm()'>";	   
		   
$("input[name='pdsradio']:checked").each(function(){
											
		
							 
				var b=$(this).attr('title');
				if(b=='pds'){
				 $('#button_div').html(b1)	
				}
				if(b=='no pds'){
				 $('#button_div').html(b2);	 	
				}
				
						 });


		   
$('.payradio').change(function(){
											
											
					
							
							 if($(this).attr('title')=='pds'){
								
							 $('#button_div').html(b1);
							 }
							 else if($(this).attr('title')=='no pds'){
							 $('#button_div').html(b2);	 
							 }
							   });


						  



$("input[type='password']").focus(function(){
										 if($('#result_div').html()!=""){
										$('#result_div').html('')	 
										 }
										   });

		   });

























function submitPds(){
	
form=$('#bookingform').serialize();	
$('#Button').attr('disabled','disabled');


$.ajax({
	   type: "get",
	   url:"../controllers/controller_new_booking.php?"+form,
	   success: function(e){
		if (e!=0){
			
		var newe= "Booking has been successful<p>Continue to account</p>";	
			
		
		
		var $dialog = $("<div class='update_form'></div>")
		.html(e)
		.dialog({
			autoOpen: false,
			title: 'Booking Notification',
			 modal: true,
			 height:500,
			 width:400,
			 buttons: { "Ok": function() { $(this).dialog("close"); } },
			 close: function(e,ui){
				//window.location.reload(); 
			 }
		});


		$dialog.dialog('open');
		
		$('#name,#company,#job_title,#department,#tel,#delegate_email').val('');
		$('#Button').attr('disabled','');
		}
		else{
		alert('not done');	
		}
		   
	   }
	   
	   
	   })


	
}



function validateFormPds(){
	

							 var okSoFar=true
							 with (document.bookingform)
								 {
									  if (courseid.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please select a programme."); courseid.focus()
									  }
									  if (name.value=="" && okSoFar)
									  {
									    okSoFar=false;	alert("Please enter the delegate's name."); name.focus()
									  }
									  if (company.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please enter the Company."); company.focus()
									  }
									  if (job_title.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the delegate's job title."); job_title.focus()
									  }
									  if (department.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the delegate's department."); department.focus()
									  }
									  if (tel.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please enter a contact telephone number."); tel.focus()
									  }
									  if (okSoFar)
									  {
										  	if (ValidateEmail()==false)
										  {
										  	okSoFar=false
										  }
									  }
									  if (referral.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please select where did you hear from us."); referral.focus()
									  }
									  if (BillingSurname.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the surname of the person in charge of billing."); BillingSurname.focus()
									  }
									  if (BillingFirstnames.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the first name(s) of the person in charge of billing."); BillingFirstnames.focus()
									  }
									  if (BillingAddress1.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the billing address."); BillingAddress1.focus()
									  }
									  if (BillingCity.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the billing city."); BillingCity.focus()
									  }
									  if (BillingPostCode.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the billing post code."); BillingPostCode.focus()
									  }
									  if (country.value=="" && okSoFar)
									  {
									    okSoFar=false; alert("Please inform the billing country."); country.focus()
										
									  }

									  if (terms_check.checked == 0 && okSoFar)
									  {
									    okSoFar=false
									    alert("You have to read and accept the General Booking Conditions to proceed.")
									    terms_check.focus()
									  }
									  //if (okSoFar==true) submit();
									  
									if (okSoFar==false){ return false;}// submit();}
									else{
										submitPds();
										return false;
										}
							 }
				
	
	
}



///update password

function updateBookingForm(){
	
	if(!confirm("Are your sure you want to update this record?")){
				return false;
				}
				
var data=$('#update_booking').serialize();

$.post("../controllers/controller_update_booking.php",data,function(e){
																	
if(e==1){
	alert("Not updated, please ensure you are not entering identical information");	
	
	}
	else{
		alert("Booking updated");	
		
$('#booking_list').html(e);

$('.updateDiv').remove().html('');

$(".tablesorter").tablesorter({
								  
							headers: {
			// assign the secound column (we start counting zero)
			0: {
				// disable it by setting the property sorter to false
				sorter: false
			}
		}	  
								  
								  
								  
								  
								  
								  });



	}





	
})
	
				
return false;
	
}



///update password

function updateUserForm(){
var data=$('#update_user').serialize();

$.post("../controllers/controller_update_user.php",data,function(e){

	alert(e);
	window.location.reload();
	
//$('#result_div').html(e);		
	
})
	


	return false;

}







///update password

function updatePass(){
var data=$('#update_pass').serialize();

settings={noval:'All fields are required',nomatch:'New password does not match. Your password was not changed',invalidold:'Old password does not match. Your password was not changed',alldone:'Your password was successfully changed'};

var error=0;	

$("input[type='password']").each(function(){
									  
										 var val=$(this).val()
										 if(val==""){
											$('#result_div').html("<div class='container_notice'>"+settings.noval+"</div>");	
											error=1;
										 }


										  });


if(error==1){
return false;	
}

var oldp=$('#pds_oldpass').val();
var newp=$('#pds_newpass').val();
var cp=$('#pds_cpass').val();

if(newp!=cp){
//alert('new password does not match');	
$('#result_div').html("<div class='container_notice'>"+settings.nomatch+"</div>");	
return false;
}




$.post("../controllers/controller_update_pass.php",data,function(e){
	rdata=e;
	mess=rdata.outPut;
	error=rdata.error;
	if(error==0){
data="<div class='container_message'>"+mess+"</div>";		
	}
	else{
	data="<div class='container_notice'>"+mess+"</div>";
	}

$('#result_div').html(data);	
	
},'json')
	
	return false;
	
}




///update password

function updateContact(){
var data=$('#update_contact').serialize();

$.post("../controllers/controller_update_contact.php",data,function(e){
$('#result_div1').html(e);	
	
})

	return false;
	
}






function addContact(){
var data=$('#add_contact').serialize();


$.post("../controllers/controller_add_contact.php",data,function(e){
$('#result_div3').html(e);	


	
})

return false;
	
}




