jQuery(document).ready(function($) {
	/**
	 * Setup the form to validate
	 */
	$("#multiregistrationform").validate();
	
	/**
	 * Reroute the form based on how the user wants to pay
	 */
	$("#cft-srf-paymenttype").change(function(){
		switch ($(this).val()) {
			case 'CC' :
				$("#multiregistrationform").attr('action', 'https://www.paypal.com/cgi-bin/webscr').attr('method', 'get');
				break;
			case 'Check' :
				$("#multiregistrationform").attr('action', '/processmultiregistration/').attr('method', 'get');
				break;
		}
	});
	
	/**
	 * Update the price based on the
	 * delivery method for webinars only
	 */
	$("#cft-srf-deliveryoption").change(function() {
		
		switch ($('#web_switch').val()) {
			case 'normal' :
				// Switch the values around
				if ($(this).val()=='both') {
					 $("#amount").attr('name', 'amount_backup').attr('id', 'amount_backup');
					 $("#amount_web_both").attr('name', 'amount').attr('id', 'amount');
					 $("#web_switch").val('both');
				}
				break;
			case 'both' :
				$("#amount").attr('name', 'amount_web_both').attr('id', 'amount_web_both');
				$("#amount_backup").attr('name', 'amount').attr('id', 'amount');
				$("#web_switch").val('normal');
				break;
		}
	});
	
	/**
	 * Control the flow of the submission process
	 * Make sure that they can't submit the form
	 * multiple times and build a return URL based
	 * on the fields filled out.
	 **/
	$("#multiregistrationform").submit(function(){
		if ($("#multiregistrationform").valid()) {

			$('#coursesubmissionbutton').attr('disabled', 'disabled').val('Please wait...');
			/**
			 * Build Return URL and AJAX Data
			 */
			$pageAddress = 'http://cftncs.org/processmultiregistration/?';
 
			/**
			 * Add Students
			 */
			$returnURL = 'cft-crf-students=' + escape($('#cft-crf-students').val());

			/**
			 * Add Course ID
			 */
			$returnURL+='&cft-crf-courseid=' + escape($('#cft-crf-courseid').val());

			/**
			 * Add Course Type
			 */
			$returnURL+='&cft-crf-coursetype=' + escape($('#cft-crf-coursetype').val());

			/**
			 * Add Course Name
			 */
			$returnURL+='&cft-crf-coursename=' + escape($('#cft-crf-coursename').val());

			/**
			 * Add Course Code
			 */
			$returnURL+='&cft-crf-coursecode=' + escape($('#cft-crf-coursecode').val());

			/**
			 * Add Course Date
			 */
			$returnURL+='&cft-crf-coursedate=' + escape($('#cft-crf-coursedate').val());
			
			/**
			 * Add Course Price
			 */
			$returnURL+='&cft-crf-courseprice=' + escape($('#cft-crf-courseprice').val());
			
			/**
			 * Add Course Location
			 */
			$returnURL+='&cft-crf-courselocation=' + escape($('#cft-crf-courselocation').val());
			
			/**
			 * Add Bank Name
			 */
			$returnURL+='&cft-srf-bankname=' + escape($('#cft-srf-bankname').val());
			
			/**
			 * Add Bank Address 1
			 */
			$returnURL+='&cft-srf-bankaddress1=' + escape($('#cft-srf-bankaddress1').val());
			
			/**
			 * Add Bank Address 2
			 */
			$returnURL+='&cft-srf-bankaddress2=' + escape($('#cft-srf-bankaddress2').val());
			
			/**
			 * Add Bank City
			 */
			$returnURL+='&cft-srf-bankcity=' + escape($('#cft-srf-bankcity').val());
			
			/**
			 * Add Bank State
			 */
			$returnURL+='&cft-srf-bankstate=' + escape($('#cft-srf-bankstate').val());
			
			/**
			 * Add Bank Zip
			 */
			$returnURL+='&cft-srf-bankzip=' + escape($('#cft-srf-bankzip').val());
			
			/**
			 * Add Supervisor Name
			 */
			$returnURL+='&cft-srf-supervisorname=' + escape($('#cft-srf-supervisorname').val());
			
			/**
			 * Add Supervisor Email
			 */
			$returnURL+='&cft-srf-supervisoremail=' + escape($('#cft-srf-supervisoremail').val());
			
			/**
			 * Add Supervisor Phone
			 */
			$returnURL+='&cft-srf-supervisorphonenumber=' + escape($('#cft-srf-supervisorphonenumber').val());
			
			/**
			 * Add Webinar Delivery Type
			 */
			$returnURL+='&cft-crf-delivery-type=' + escape($('#cft-crf-delivery-type').val());
			
			/**
			 * Add Students
			 */
			for ($i=1; $i<=$('#cft-crf-students').val(); $i++) {
				/**
				 * Add Student $i Name
				 */
				$returnURL+='&cft-srf-studentname-' + $i + '=' + escape($('#cft-srf-studentname-' + $i).val());
				
				/**
				 * Add Student $i Email
				 */
				$returnURL+='&cft-srf-studentemail-' + $i + '=' + escape($('#cft-srf-studentemail-' + $i).val());
				
				/**
				 * Add Student $i Phone
				 */
				$returnURL+='&cft-srf-studentphone-' + $i + '=' + escape($('#cft-srf-studentphone-' + $i).val());
			}
			
			/**
			 * Add Payment Type
			 */
			$returnURL+='&cft-srf-paymenttype=' + escape($('#cft-srf-paymenttype').val());
			
			/**
			 * Add Additional Option
			 */
			$returnURL+='&cft-srf-deliveryoption=' + escape($('#cft-crf-students').val());
			
			$('#return').val($pageAddress + $returnURL);
			
			/**
			 * Add information to DB via AJAX
			 */
			$.ajax({
				url: "/wp-content/plugins/CFTNCS/ajax/registration.php",
				type: "POST",
				data: $returnURL,
				dataType: 'html',
				complete : function() {
					if ($("#multiregistrationform").attr('action') == 'https://www.paypal.com/cgi-bin/webscr') {
						$("#paypalsubmitform").html($("#multiregistrationform").html()).submit();
						return true;
						// TODO: For some reason this isn't working right...
					}
					else {
						window.location = $("#multiregistrationform").attr('action') + '?' + $returnURL;
						return false;
					}
				}
			});
			return false;
		}
		else {
			return false;
		}
	});
});
