	$(document).ready(function(){
		reloadMiniCrate();
		
 $('.test').live("click",function(event){
 		
 			alert('test');
   		event.preventDefault();
})	


$(".course tr:odd").css("background-color", "#f2f2f2");
$(".course tr:even").css("background-color", "#E5ECF9");
$(".course tr:first-child").css("border-bottom", "1px dotted gray");
$(".course tr:first-child").css("background-color", "#ffffff");
	          
    		 $dlg = $('#dlg');
 
    		$dlg.dialog({ 	autoOpen: false });
	
	 	$(".LINK").live("hover",function() {
		
		
			$(this).css("cursor", "pointer"); 
		 }); 
	 	
	
		
		
	
	 	$(".BUTTON").live("hover",function() {
		
		
			$(this).css("cursor", "pointer"); 
		 });




function reloadMiniCrate(){
	 		$('#minicrate').html('<img src="/images/ajax-loader-small.gif">'); 	
	   $('#minicrate').load('/ajax/ajax_minicrate.php');
  	
}

	

	$('.AJAXUPDATE').live("keyup",function(event){
		
		var refstring = this.getAttribute('id');
				
				var explode = refstring.split("_"); 
				var tablename = explode[0];
				var fieldname = explode[1];
				var refid = explode[2];
				var dbaction = explode[3];
				//alert(dbaction)
				if(dbaction =='UPDATE'){
						var updateFieldTo = $(this).val();
						//alert(updateFieldTo);
						
						var spinner = $("#"+refstring+"_SPINNER");
						
						spinner.html('<img src="/images/ajax-loader-small.gif">'); 	
				  				
				    	spinner.load('/ajax/ajax_updateField.php',{'TABLENAME':tablename,'FIELDNAME':fieldname,'REFID':refid,'UPDATETO':updateFieldTo});
				  
		  	
		   }
	 });


	$('.AJAXUPDATE').live("change",function(event){
		
		var refstring = this.getAttribute('id');
				
				var explode = refstring.split("_"); 
				var tablename = explode[0];
				var fieldname = explode[1];
				var refid = explode[2];
				var dbaction = explode[3];
				//alert(dbaction)
				if(dbaction =='UPDATE'){
						var updateFieldTo = $(this).val();
						//alert(updateFieldTo);
						
						var spinner = $("#"+refstring+"_SPINNER");
						
						spinner.html('<img src="/images/ajax-loader-small.gif">'); 	
				  				
				    	spinner.load('/ajax/ajax_updateField.php',{'TABLENAME':tablename,'FIELDNAME':fieldname,'REFID':refid,'UPDATETO':updateFieldTo});
				  
		  	
		   }
	 });

$('.AJAXMULTIADD').live("change",function(event){
		
		var refstring = this.getAttribute('id');
	
				var explode = refstring.split("_"); 
				var tablename = explode[0];

				var refid = explode[1];
				var dbaction = explode[2];
				//alert(dbaction)
				if(dbaction =='ADD'){
						var updateTo = $(this).val();
					//	alert(updateTo);
						
						var spinner = $("#"+refstring+"_SPINNER");
						
						spinner.html('<img src="/images/ajax-loader-small.gif">'); 	
				  				
				    	spinner.load('/ajax/ajax_multiAdd.php',
				    						{'TABLENAME':tablename,'REFID':refid,'UPDATETO':updateTo},
				    						
				    						function(){
				    							location.reload();
				    							
				    						}
				    						);
				  
		  	
		   }
		   
		   
	 });



$('.AJAXMULTIDEL').live("click",function(event){
		
		var refstring = this.getAttribute('id');
	
				var explode = refstring.split("_"); 
				var tablename = explode[0];

				var refid = explode[1];
				var linkid = explode[2];
				var dbaction = explode[3];
				//alert(tablename+' refid:'+refid+' linkid:'+linkid+' dbaction:'+dbaction)
				if(dbaction =='DEL'){
					
						var spinner = $("#"+refstring+"_SPINNER");
						
						spinner.html('<img src="/images/ajax-loader-small.gif">'); 	
				  				
				    spinner.load('/ajax/ajax_multiDel.php',{'TABLENAME':tablename,'REFID':refid,'LINKID':linkid});
				  
		  	
		   }
		   
		   	event.preventDefault();
	 });


 $('.ACCOUNT_EDIT').live("click",function(event){
 		
 			var refstring =  $(this).attr('name');
			
			var explode = refstring.split("_"); 
			var webuserid = explode[1];
 			
			$dlg.load('/ajax/ajax_editAccount.php',{'WEBUSERID':webuserid});
   		$dlg.dialog('option', 'title','edit your account?');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',430);  
			$dlg.dialog('option', 'width',800);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'open');
			$dlg.dialog('option', 'buttons', ({


				

        
				
				'close': function() {
					window.location ='/account';
					$dlg.dialog('close');
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
})	





$('.FORGOT_PASSWORD').live("click",function(event){
 			$dlg.html('<img src="/images/ajax-loader-small.gif">'); 	
				  				
			$dlg.dialog('close');

			$dlg.load('/ajax/ajax_forgotPassword.php');
   		$dlg.dialog('option', 'title','Retrieve my password');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',300);  
			$dlg.dialog('option', 'width',300);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'open');
			
			
			$dlg.dialog('option', 'buttons', ({


				
				'send me my password': function() {
				//	alert('adding');
						formParams = $("#FORM").serialize();
						
						$(this).load('/ajax/ajax_forgotPassword.php',formParams,function(){
							//window.location="/account";
					});
					
					//$(this).dialog('close');
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
})	

 



 $('.SUBACCOUNT_EDIT').live("click",function(event){
 			var refstring =  $(this).attr('href');
			//	alert(refstring);
			var explode = refstring.split("/"); 
			var SUBUSERID = explode[3];
 	
		

			$dlg.load('/ajax/ajax_editSubaccount.php',{'SUBUSERID':SUBUSERID});
   		$dlg.dialog('option', 'title','edit sub-account');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',430);  
			$dlg.dialog('option', 'width',800);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'open');
			
			
			$dlg.dialog('option', 'buttons', ({


				

        
				
				'close': function() {
					window.location ='/account';
					$dlg.dialog('close');
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
})	







 $('.COURSE_BUY').live("click",function(event){
 		
 				var COURSEID =  $(this).attr('name');
			
		//alert('buy COURSEID:'+COURSEID);
		
 			
			$dlg.load('/ajax/ajax_buyCourse.php?asdfasddfasasdadsfa',{'COURSEID':COURSEID
});
   		$dlg.dialog('option', 'title','Book a place on this course');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',300);  
			$dlg.dialog('option', 'width',600);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'open'
			);
			
			
			$dlg.dialog('option', 'buttons', ({

				'book place...': function() {
				//console.log($("#foo").val());
					var isforuserid = ($("#isforuserid").val());
				
					if(isforuserid.length > 0){
						formParams = $("#FORM").serialize();
						
						$(this).load('/ajax/ajax_buyCourse.php',formParams,function(){
						//reloadEvents(refstring);
					});
					
						$dlg.dialog('option', 'buttons', ({

		
				

        
				
							'close and and return to site': function() {
								$(this).dialog('close');
								reloadMiniCrate();
							},
							
							'go to bookings and checkout': function() {
								$(this).dialog('close');
								window.location="/crate";
							}
						}));
					
					
				}else{
					alert('please choose from the pulldown');
					
				}
				},
				

        
				
				Cancel: function() {
					$(this).dialog('close');
	
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
})	






 $('.SUBACCOUNT_ADD').live("click",function(event){
 		
 			
 			
			$dlg.load('/ajax/ajax_addSubaccount.php');
   		$dlg.dialog('option', 'title','add a sub account?');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',400);  
			$dlg.dialog('option', 'width',800);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'open'
			);
			
			
			$dlg.dialog('option', 'buttons', ({

				'add...': function() {
				//	alert('adding');
						formParams = $("#FORM").serialize();
						
						$(this).load('/ajax/ajax_addSubaccount.php',formParams,function(){
							//window.location="/account";
					});
					
					//$(this).dialog('close');
				},
				

        
				
				Cancel: function() {
					$(this).dialog('close');
					window.location="/account";
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
})	



	 	$("#button_login").hover(function() {
		
		
			$(this).css("cursor", "pointer"); 
		 }); 

$('#button_login').live("click",function(event){


if($(this).attr('class')=='loggedin'){
	
	window.location="/account";
}else{
	$dlg.load('/ajax/ajax_login.php', {},
                function (responseText, textStatus, XMLHttpRequest) {
                		$dlg.dialog( "option", "width", 600 );
                		$dlg.dialog( "option", "height", 300 );
                		$dlg.dialog( "option", "modal", true );
                		$dlg.dialog( "option", "resizable", false );
                		$dlg.dialog( "option", "title", "sign in to All Star Coaching" );
                		$dlg.dialog('option', 'buttons',false);
                		
                   	$dlg.dialog( "open");
                });
                
                

                
}


});

                $('#login_password').live("keyup",function(e) {
                	
    if (e.keyCode == 13) {
       
        signin();
    }
});



$('#button_account').live("click",function(event){

		window.location ='/account';



});

	function signin(){
			formParams = $("#FORM_SIGNIN").serialize();
			
			$('#SPINNER_SIGNIN').html('<img src="/images/ajax-loader-small.gif">'); 
			
			$('#SPINNER_SIGNIN').load("/ajax/ajax_doLogin.php",formParams);
		
}


$('#modal_button_sign_in').live("click",function(event){
	
	
	signin();
	

	
	 });

		
		$('#modal_button_register').live("click",function(event){
		
		window.location ='/register';
	 });
	
			

 

































		
		   	   
   
         
         
            
        var formoptions = { 
        target:        '#dlg',    // target element(s) to be updated with server response 
        beforeSubmit: validate ,
       
       
        success:       function() { 
           		var title = $dlg.dialog('option', 'title');
							$dlg.dialog('option', 'title', 'Thankyou!');
							
  
           		
           		

							$dlg.dialog('open');
    					
        } ,
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        	clearForm: true        // clear all form fields after successful submit 
       // resetForm: true       // reset the form after successful submit 
				
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
    
    
    
     
function validate(formData, jqForm) { 
    // jqForm is a jQuery object which wraps the form DOM element 
    // 
    // To validate, we can access the DOM elements directly and return true 
    // only if the values of both the username and password fields evaluate 
    // to true 

    var form = jqForm[0]; 
    if (!form.fullname.value || !form.email.value || !form.telephone.value  || !form.refer.value) { 
        alert('please note all fields are required'); 
        return false; 
      
    } 
   
}
    
 
     // bind form using ajaxForm 
    $('#form_getintouch').ajaxForm(formoptions); 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    // bind to the form's submit event 
    $('#form_getintouch').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
} 
            
            
    
	$('.datepicker').live('click', function() { 

$(this).datepicker({
	showOn: "focus",
	buttonText:"click to edit date",
	changeYear: true,
	changeMonth: true,
	yearRange:"-80:-3",

	dateFormat: 'yy-mm-dd',
	onClose: function(dateText, inst) {
		
		var refstring = this.getAttribute('id');
		
		var explode = refstring.split("_"); 
		var tablename = explode[0];
		var fieldname = explode[1];
		var refid = explode[2];
		var dbaction = explode[3];
		//alert(dbaction)
		if(dbaction =='UPDATE'){
		var spinner = $("#"+refstring+"_SPINNER");
		
		spinner.html('<img src="/images/ajax-loader-small.gif">'); 	
   // if(dateText.length > 0){   

    	spinner.load('/ajax/ajax_updateField.php',{'TABLENAME':tablename,'FIELDNAME':fieldname,'REFID':refid,'UPDATETO':dateText});
  	//}
  	
 		 }
  	
		}
}).focus(); ;
}); 
	

	
$('.datepicker_reset').live("click",function(event){
	var refstring = this.getAttribute('id');
	var explode = refstring.split("_"); 
	var tablename = explode[0];
	var fieldname = explode[1];
	var refid = explode[2];
	var datePickerInput = tablename +"_" + fieldname +"_" +refid
	var datePickerSpinner = tablename +"_" + fieldname +"_" +refid+"_SPINNER"
			//alert('datePickerInput:' + datePickerInput);
			
	//	var spinner = $("#"+refstring+"_SPINNER");

		 $("#"+datePickerSpinner).html('<img src="/images/ajax-loader-small.gif">'); 	
	   $("#"+datePickerSpinner).load('/ajax/ajax_updateField.php',{'TABLENAME':tablename,'FIELDNAME':fieldname,'REFID':refid,'UPDATETO':null});
  	
  	
  	$("#"+datePickerInput).val('not set');
  		

  		event.preventDefault();
});
	
	

	
	
	
			   
$(".DELETE_CRATEITEM").live("click",function(event){
    	
     	var CRATEITEMID = this.getAttribute('name');
    	var isminicrate = this.getAttribute('rel');

    	//alert(CRATEITEMID);
    	$dlg.html('<center>are you sure you want to delete this booking?</center>');
   		$dlg.dialog('option', 'title','delete booking?');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',140);  
			$dlg.dialog('option', 'width',300);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'buttons', ({

				'Delete this booking...': function() {
				//alert("delete"+EVENTSESSID +' '+EVENTID);
					$(this).load('/ajax/ajax_delCrateItem.php',{'CRATEITEMID':CRATEITEMID},function(){
						reloadMiniCrate();
					$(this).dialog('close');
					if(isminicrate!='minicrate'){
					window.location ='/crate';
					}
					});
				
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
    		
      });
    
	
	
	$(".DELETE_SUBACCOUNT").live("click",function(event){
    		$dlg.dialog('close');
     	var SUBUSERID = this.getAttribute('name');
  

    	//alert(CRATEITEMID);
    	$dlg.html('<center>are you sure you want to delete this account?</center>');
   		$dlg.dialog('option', 'title','delete account?');  	
  		$dlg.dialog('option', 'resizable',false);  
			$dlg.dialog('option', 'height',130);  
			$dlg.dialog('option', 'width',300);  
			$dlg.dialog('option', 'modal', true);
			$dlg.dialog('option', 'buttons', ({

				'Delete this account...': function() {
				//alert("delete"+SUBUSERID +' '+SUBUSERID);
					$(this).load('/ajax/ajax_delSubaccount.php',{'SUBUSERID':SUBUSERID},function(){
					
				
					
					});
				
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}));
			   
			$dlg.dialog('open');

   		event.preventDefault();
    		
      });
 
	$(".TOOLTIP").tipsy({title: 'rel'});

	
/////////////////////////////    
});
