$(document).ready(function(){
	
	//var base_url = 'http://dev.damondelgado.com/ga/ahs/';
	var base_url = 'https://www.americanherniasociety.org/';
	
	//$("input#phone").mask("(999) 999-9999");
	//$("input#fax").mask("(999) 999-9999");
	//$("input#zip_code").mask("99999");
	//$("input#cc_zip_code").mask("99999");	
	//$("input#cc_number_long").mask("9999999999999999");
	$("input#cc_exp_date").mask("99/99");	
	//$("input#cc_cvv").mask("999");
	
	$("input#Birthdate").mask("99/99/99");
	
	//  REGISTER NEW MEMBER FORM
			
	$("#register_member_form").validate({
	   rules: {
		     username: {
		       minlength: 4			
		     },
		     password: {
		       minlength: 5			
		     },
		     Gender: "required",
		     Preferred_Mail_Address: "required"
	   },	   
	   submitHandler: function(form) {
	      //debug: true;
 		  //alert('click');
 		  
 		  var userPassDifferent = false;
 		  var passwordConfirmSame = false;
 		  var officeComplete = false;
 		  var homeComplete = false;
 		  
 		  if($('#username').val() == $('#password').val()) {
 		  		userPassDifferent = false;
 		  } else {
 		  		userPassDifferent = true;
 		  }
 		  
 		  if($('#password').val() != $('#password_confirm').val()) {
 		  		passwordConfirmSame = false;
 		  } else {
 		  		passwordConfirmSame = true;
 		  }
 		   		  
 		  if(($('#Office_Address').val() == '') || ($('#Office_City').val()  == '') || ($('#Office_State').val() == '') || ($('#Office_Postal_Code').val() == '') || ($('#Office_Country').val() == '')) {
 		  		officeComplete = false;
 		  } else {
 		  		officeComplete = true;
 		  }
 		  
 		  if(($('#Home_Address').val()  == '') || ($('#Home_City').val() == '') || ($('#Home_State').val() == '') || ($('#Home_Postal_Code').val()  == '') || ($('#Home_Country').val() == '')) {
 		  		homeComplete = false;
	   	  } else {
 		  		homeComplete = true;
 		  }
 		  
 		  
 		  if (userPassDifferent) {
 		  		if (passwordConfirmSame) {
 		  			if ((officeComplete) || (homeComplete)) { 
 		   		  	
					      var str = $('#cc_exp_date').val();
					      var exp_date = str.replace("/", "");
					      $.ajax({
				                  url: base_url + "adm1nAHS/aim/process.php", 
				                  global: false, 
				                  async: false, 
				                  type: "POST", 
				                  data: ({
					                  		cc_number : $('#cc_number_long').val(), 
					                  		cc_exp_date : exp_date, 
					                  		cc_cvv : $('#cc_cvv').val(), 
					                  		total : $('.type_total:checked').val(), 
					                  		x_description : $('#x_description').val(), 
					                  		cc_first_name : $('#cc_first_name').val(), 
					                  		cc_last_name : $('#cc_last_name').val(), 
					                  		cc_address : $('#cc_address').val(), 
					                  		cc_state : $('#cc_state').val(), 
					                  		cc_zip_code : $('#cc_zip_code').val(), 
					                  		cc_country : $('#cc_country').val(), 
					                  		cc_phone : $('#cc_phone').val()	                  		
				                  		}),
				                  dataType: "xml",
				                  success: function(xml){
				        		       var code = $(xml).find("code").text();
				        		       var reason_code = $(xml).find("reasoncode").text();
				        		       var reason_text = $(xml).find("text").text();
				        		       if(code == 1) {
				        		       		ccstring = $('#cc_number_long').val();
				                            var ccLength = ccstring.length;
				                            $('#m_field_id_2').attr('value', ccstring.substr(ccLength-4,4));                                                    
				                            $('#m_field_id_3').attr('value', $(xml).find("authcode").text());
				                            $('#m_field_id_4').attr('value', $(xml).find("transactionid").text());                            
				                            $('#m_field_id_5').attr('value', $('.type_total:checked').val());
				                            
				                            $('#m_field_id_6').attr('value', code);
				                            $('#m_field_id_7').attr('value', reason_code);
				                            $('#m_field_id_8').attr('value', reason_text);
				                            
				                            //alert('code: '+code);
					        		       	//alert('reasoncode: '+reason_code);
					        		        //alert('reasontext: '+reason_text);
					        		        //alert('transactionid: '+$(xml).find("transactionid").text());
				                                                     
				                            add_to_filemaker(form);                       
				                            						
				        		       } else {
				        		       		alert("Error: "+reason_text+" Code "+code+"-"+reason_code);
				        		       }
				        		       
				        		       
				                  },
				                  error:function (xhr, ajaxOptions, thrownError){
				                  		alert('Error: '+xhr.status+' '+xhr.statusText);
				                  }  
				               }
				            ).responseText;
			            
			 		  	} else {
			 		  		alert('You must enter either a complete Office or Home Address:\nRequired Fields: Street, City, State, Postal Code and Country.');
			 		  	}
 		  		} else {
 		  			alert('The password and password confirmation do not match.');
 		  		} 
 		  } else {
 		  		alert('Your password cannot be based on your username.')
 		  }

            return false;                 
	   }   
   	});
	
	function add_to_filemaker(form)  {
		$.ajax({
                  url: base_url + "filemaker/new_member_response.php", 
                  global: false, 
                  async: false, 
                  type: "POST", 
                  data: ({
	                  		First_Name : $('#First_Name').val(), 
	                  		Middle_Name : $('#Middle_Name').val(), 
	                  		Last_Name : $('#Last_Name').val(), 
	                  		Suffix : $('#Suffix').val(), 
	                  		//Degree : $('#Degree').val(),
	                  		Degree_0 : $('#Degree_0').val(),
	                  		Degree_1 : $('#Degree_1').val(),
	                  		Degree_2 : $('#Degree_2').val(),
	                  		Degree_3 : $('#Degree_3').val(), 
	                  		Gender : $('.Gender:checked').val(), 
	                  		Birthdate : $('#Birthdate').val(), 
	                  		Office_Address : $('#Office_Address').val(), 
	                  		Office_City : $('#Office_City').val(), 
	                  		Office_State : $('#Office_State').val(), 
	                  		Office_Postal_Code : $('#Office_Postal_Code').val(), 
	                  		Office_Country : $('#Office_Country').val(), 
	                  		Office_Phone : $('#Office_Phone').val(), 
	                  		Office_Fax : $('#Office_Fax').val(), 
	                  		Home_Address : $('#Home_Address').val(), 
	                  		Home_City : $('#Home_City').val(), 
	                  		Home_State : $('#Home_State').val(), 
	                  		Home_Postal_Code : $('#Home_Postal_Code').val(), 
	                  		Home_Country : $('#Home_Country').val(), 
	                  		Home_Phone : $('#Home_Phone').val(), 
	                  		Home_Fax : $('#Home_Fax').val(), 
	                  		Preferred_Mail_Address : $('.Preferred_Mail_Address:checked').val(), 
	                  		Preferred_Publish_Address : $('.Preferred_Publish_Address:checked').val(), 	                  		 
	                  		'Preferred_Publish_Phones[]' : [$('#Preferred_Publish_Phones0:checked').val(), $('#Preferred_Publish_Phones1:checked').val()],	                  			                  		 
	                  		Website_URL : $('#Website_URL').val(), 
							Email_Address : $('#email').val(), 
							Classification_Serial_No : '163'														                		
                  		}),                  	
                  dataType: "xml",
                  success: function(xml){
        		       var serialno = $(xml).find("serialno").text();
        		       if(serialno) {
                            //$('#serialno').attr('value', serialno);                            
                            //alert(serialno);
                            $('#m_field_id_1').attr('value', serialno);
                            form.submit();                       
        		       }        		       
        		       //alert('serailno: '+$(xml).find("serialno").text());
                  },
                  error:function (xhr, ajaxOptions, thrownError){
                  		alert('Error: '+xhr.status+' '+xhr.statusText);
                  }  
               }
            ).responseText;
            
            return false;
	}
			
	$("#member").validate({
	   submitHandler: function(form) {
	   	  if(($('#Office_Address').val() == '') || ($('#Office_City').val()  == '') || ($('#Office_State').val() == '') || ($('#Office_Postal_Code').val() == '') || ($('#Office_Country').val() == '')) {
 		  		officeComplete = false;
 		  } else {
 		  		officeComplete = true;
 		  }
 		  
 		  if(($('#Home_Address').val()  == '') || ($('#Home_City').val() == '') || ($('#Home_State').val() == '') || ($('#Home_Postal_Code').val()  == '') || ($('#Home_Country').val() == '')) {
 		  		homeComplete = false;
	   	  } else {
 		  		homeComplete = true;
 		  }
 		  
 		  if ((officeComplete) || (homeComplete)) {
 		  		form.submit();
 		  } else {
 		  		alert('You must enter either a complete Office or Home Address:\nRequired Fields: Street, City, State, Postal Code and Country.');
 		  }
 		  
 		  return false;
	   }
	});	
	
// MEMBERSHIP RENEWAL FORM
	
	//$("#total_donation").jStepper({minValue:0, maxValue:99999, minDecimals:2, maxDecimals:2, disableNonNumeric:true, allowDecimals:false});
	$("#total_donation").numeric(false);
	
	$('a#test').click(function() {
	  renew_calculateTotals();
	return false;
	});
	
	function renew_calculateTotals()  {
		var membership_subtotal;
		var donation_subtotal;
		var total;
		
		membership_subtotal = parseInt($('input[name=membership_total]:checked').val());
		//alert(membership_subtotal);
		donation_subtotal = parseInt($('#total_donation').val());
		//alert(donation_subtotal);
		total = membership_subtotal + donation_subtotal;
		//alert(total);
		
		$('input#membership_subtotal').val(membership_subtotal.toFixed(2));
		$('input#donate_subtotal').val(donation_subtotal.toFixed(2));
		$("input#total").val(total.toFixed(2));
		
		var answer = confirm('Your Membership Renewal total is: $'+total.toFixed(2)+'.  Click "OK" to process your payment.')
		if(answer) {
			return true;
		} else {
			return false;
		}
		
	}
	
	$("#membership_renewal").validate({
	   submitHandler: function(form) {
	      //debug: true;
      	  var confirmation = renew_calculateTotals();
		  	
	      if(confirmation) {
			  var str = $('#cc_exp_date').val();
		      var exp_date = str.replace("/", "");
		      $.ajax({
	                  url: base_url + "adm1nAHS/aim/process.php", 
	                  global: false, 
	                  async: false, 
	                  type: "POST", 
	                  data: ({
		                  		cc_number : $('#cc_number_long').val(), 
		                  		cc_exp_date : exp_date, 
		                  		cc_cvv : $('#cc_cvv').val(), 
		                  		total : $('#total').val(), 
		                  		x_description : $('#x_description').val(), 
		                  		cc_first_name : $('#cc_first_name').val(), 
		                  		cc_last_name : $('#cc_last_name').val(), 
		                  		cc_address : $('#cc_address').val(), 
		                  		cc_state : $('#cc_state').val(), 
		                  		cc_zip_code : $('#cc_zip_code').val(), 
		                  		cc_country : $('#cc_country').val(), 
		                  		cc_phone : $('#cc_phone').val()	                  		
	                  		}),
	                  dataType: "xml",
	                  success: function(xml){
	        		       var code = $(xml).find("code").text();
	        		       var reason_code = $(xml).find("reasoncode").text();
	        		       var reason_text = $(xml).find("text").text();
	        		       if(code == 1) {
	        		       		ccstring = $('#cc_number_long').val();
	                            var ccLength = ccstring.length;
	                            $('#cc_number').attr('value', ccstring.substr(ccLength-4,4));
	                            $('#auth_code').attr('value', $(xml).find("authcode").text());
	                            $('#transaction_id').attr('value', $(xml).find("transactionid").text());
	                            $('#code').attr('value', code);
	                            $('#reason_code').attr('value', reason_code);
	                            $('#reason_text').attr('value', reason_text);
	                            form.submit();						
	        		       } else {
	        		       		alert("Error: "+reason_text+" Code "+code+"-"+reason_code);
	        		       }
	        		       //alert($('#total:checked').val());
	        		       //alert($(xml).find("code").text());
	        		       //alert($(xml).find("reasoncode").text());
	        		       //alert($(xml).find("text").text());
	        		       //alert($(xml).find("authcode").text());
	        		      // alert($(xml).find("transactionid").text());
	                  },
	                  error:function (xhr, ajaxOptions, thrownError){
	                  		alert('Error: '+xhr.status+' '+xhr.statusText);
	                  }  
	               }
	            ).responseText;
            
             }
             return false;   	
	   }   
   	});
	
	// HEADSET REGISTRATION FORM

		$("#headset_registration").validate({
		    //alert('test');
			submitHandler: function(form) {

				  var str = $('#cc_exp_date').val();
			      var exp_date = str.replace("/", "");
			      $.ajax({
		                  url: base_url + "adm1nAHS/aim/process.php", 
		                  global: false, 
		                  async: false, 
		                  type: "POST", 
		                  data: ({
			                  		cc_number : $('#cc_number_long').val(), 
			                  		cc_exp_date : exp_date, 
			                  		cc_cvv : $('#cc_cvv').val(), 
			                  		total : $('#total').val(), 
			                  		x_description : $('#x_description').val(), 
			                  		cc_first_name : $('#cc_first_name').val(), 
			                  		cc_last_name : $('#cc_last_name').val(), 
			                  		cc_address : $('#cc_address').val(), 
			                  		cc_state : $('#cc_state').val(), 
			                  		cc_zip_code : $('#cc_zip_code').val(), 
			                  		cc_country : $('#cc_country').val(), 
			                  		cc_phone : $('#cc_phone').val()	                  		
		                  		}),
		                  dataType: "xml",
		                  success: function(xml){
		        		       var code = $(xml).find("code").text();
		        		       var reason_code = $(xml).find("reasoncode").text();
		        		       var reason_text = $(xml).find("text").text();
		        		       if(code == 1) {
		        		       		ccstring = $('#cc_number_long').val();
		                            var ccLength = ccstring.length;
		                            $('#cc_number').attr('value', ccstring.substr(ccLength-4,4));
		                            $('#auth_code').attr('value', $(xml).find("authcode").text());
		                            $('#transaction_id').attr('value', $(xml).find("transactionid").text());
		                            $('#code').attr('value', code);
		                            $('#reason_code').attr('value', reason_code);
		                            $('#reason_text').attr('value', reason_text);
		                            form.submit();						
		        		       } else {
		        		       		alert("Error: "+reason_text+" Code "+code+"-"+reason_code);
		        		       }
		        		       //alert($(xml).find("code").text());
		        		       //alert($(xml).find("reasoncode").text());
		        		       //alert($(xml).find("text").text());
		        		       //alert($(xml).find("authcode").text());
		        		       //alert($(xml).find("transactionid").text());
		                  },
		                  error:function (xhr, ajaxOptions, thrownError){
		                  		alert('Error: '+xhr.status+' '+xhr.statusText);
		                  }  
		               }
		            
					).responseText;

	             return false;
	 		}
	   	});
	
	// ATTENDEE REGISTRATION FORM
	
	$("#register_form").validate({
	   submitHandler: function(form) {
	      //debug: true;
	      var submit = 0;
	      
	      var total = $('#total').val();
	      
	      if((total != 0) || (total != 0.00)) {
	      
		      var member_list = '';
		      var member_ehs_list = '';
		      var nonmember_list = '';
		      var resident_list = '';
		      var guest_list = '';
		      
		      $("tr#member td span.name_field input[type='text']").each(function (i) {	        
		         member_list += $(this).val() + ' - ';
		         var member_name_input_id = $(this).attr('id');
		         if($('input[name="post_grad_'+member_name_input_id+'"]:checked').val() != undefined){
		        	 member_list += "Post Grad Course Selection: " + $('input[name="post_grad_'+member_name_input_id+'"]:checked').val();
		         } else {
		        	 member_list += "Post Grad Course Selection: None";
		         }
		         member_list += '<br/>';
		      });
		      $("tr#member_ehs td span.name_field input[type='text']").each(function (i) {	        
		    	 member_ehs_list += $(this).val() + ' - ';
		         var member_ehs_name_input_id = $(this).attr('id');
		         if($('input[name="post_grad_'+member_ehs_name_input_id+'"]:checked').val() != undefined){
		        	 member_ehs_list += "Post Grad Course Selection: " + $('input[name="post_grad_'+member_ehs_name_input_id+'"]:checked').val();
		         } else {
		        	 member_ehs_list += "Post Grad Course Selection: None";
		         }
		         member_ehs_list += '<br/>';
		      });
		      $("tr#nonmember td span.name_field input[type='text']").each(function (i) {	        
		    	 nonmember_list += $(this).val() + ' - ';
		         var nonmember_name_input_id = $(this).attr('id');
		         if($('input[name="post_grad_'+nonmember_name_input_id+'"]:checked').val() != undefined){
		        	 nonmember_list += "Post Grad Course Selection: " + $('input[name="post_grad_'+nonmember_name_input_id+'"]:checked').val();
		         } else {
		        	 nonmember_list += "Post Grad Course Selection: None";
		         }
		         nonmember_list += '<br/>';
		      });
		      $("tr#resident td span.name_field input[type='text']").each(function (i) {	        
		    	 resident_list += $(this).val() + ' - ';
		         var resident_name_input_id = $(this).attr('id');
		         if($('input[name="post_grad_'+resident_name_input_id+'"]:checked').val() != undefined){
		        	 resident_list += "Post Grad Course Selection: " + $('input[name="post_grad_'+resident_name_input_id+'"]:checked').val();
		         } else {
		        	 resident_list += "Post Grad Course Selection: None";
		         }
		         resident_list += '<br/>';
		      });
		      $("tr#guest td span.name_field input[type='text']").each(function (i) {	        
		         guest_list += $(this).val() + '<br/>';     
		      });
		      
		      var member;
		      var member_ehs;
		      var nonmember;
		      var resident;
		      var guest;
		      var attendees = '';
		      
		      if(member_list != '') {
		      	member = '<strong style="color:#004F93">AHS Member Attendees:</strong><br/>' + member_list;
		      	attendees += member;
		      }
		      if(member_ehs_list != '') {
		      	member_ehs = '<strong style="color:#004F93">EHS Member Attendees:</strong><br/>' + member_ehs_list;
		      	attendees += member_ehs;
		      }
		      if(nonmember_list != '') {
		      	nonmember = '<strong style="color:#004F93">Non-Member Attendees:</strong><br/>' + nonmember_list;
		      	attendees += nonmember;
		      }
		      if(resident_list != '') {
		      	resident = '<strong style="color:#004F93">Resident/Military Attendees:</strong><br/>' + resident_list;
		      	attendees += resident;
		      }
		      if(guest_list != '') {
		      	guest = '<strong style="color:#004F93">Spouse/Guest Attendees:</strong><br/>' + guest_list;
		      	attendees += guest;
		      }
		      	      
		      $("input#attendee_list").val(attendees);	      
		      //alert(attendees);
		      //return false;	      
		      var str = $('#cc_exp_date').val();
		      var exp_date = str.replace("/", "");
		      $.ajax({
	                  url: base_url + "adm1nAHS/aim/process.php", 
	                  global: false, 
	                  async: false, 
	                  type: "POST", 
	                  data: ({
		                  		cc_number : $('#cc_number_long').val(), 
		                  		cc_exp_date : exp_date, 
		                  		cc_cvv : $('#cc_cvv').val(), 
		                  		total : $('#total').val(), 
		                  		x_description : $('#x_description').val(), 
		                  		cc_first_name : $('#cc_first_name').val(), 
		                  		cc_last_name : $('#cc_last_name').val(), 
		                  		cc_address : $('#cc_address').val(), 
		                  		cc_state : $('#cc_state').val(), 
		                  		cc_zip_code : $('#cc_zip_code').val(), 
		                  		cc_country : $('#cc_country').val(), 
		                  		cc_phone : $('#cc_phone').val()	                  		
	                  		}),
	                  dataType: "xml",
	                  success: function(xml){
	        		       var code = $(xml).find("code").text();
	        		       var reason_code = $(xml).find("reasoncode").text();
	        		       var reason_text = $(xml).find("text").text();
	        		       if(code == 1) {
	        		       		ccstring = $('#cc_number_long').val();
	                            var ccLength = ccstring.length;
	                            $('#cc_number').attr('value', ccstring.substr(ccLength-4,4));
	                            $('#auth_code').attr('value', $(xml).find("authcode").text());
	                            $('#transaction_id').attr('value', $(xml).find("transactionid").text());
	                            $('#code').attr('value', code);
	                            $('#reason_code').attr('value', reason_code);
	                            $('#reason_text').attr('value', reason_text);
	                            form.submit();						
	        		       } else {
	        		       		alert("Error: "+reason_text+" Code "+code+"-"+reason_code);
	        		       }
	        		       
	        		       //alert($(xml).find("code").text());
	        		       //alert($(xml).find("reasoncode").text());
	        		       //alert($(xml).find("text").text());
	        		       //alert($(xml).find("authcode").text());
	        		       //alert($(xml).find("transactionid").text());
	                  },
	                  error:function (xhr, ajaxOptions, thrownError){
	                  		alert('Error: '+xhr.status+' '+xhr.statusText);
	                  }  
	               }
	            ).responseText;
            
            
            } else {
	   			alert('Your registration total is $0.00.   Please make a selection in Step 2: Attendees and click Submit again.');
	   		}
            
            return false;                 
	   }   
   	});
	
	$("select.attendees").change(function() {
		//alert($(this).attr('rel'));
		slideDownList($(this).attr('rel'), $(this).val());			
	});

	$('input[type="radio"].post_grad').live('change', function() {
		calculateTotals();
	});
		
	function slideDownList(list, count)  {
		 var i=0;
		 var tdInner = '';
		 while (i<count) {
		 	i++;
		 	tdInner += '<span class="name_field"><label>Name '+i+'</label><input id="'+list+i+'" name="'+list+i+'" type="text" class="required" /><br/><label class="long" style="padding-top: 10px;"><strong>Post Grad Courses*</strong> - Registration is limited. Please pick one per attendee.</label><br/><label class="long">Chronic Pain</label>&nbsp;<label class="radio"><input type="radio" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Chronic Pain" class="type_total">$175</label>&nbsp;&nbsp;&nbsp;<label class="long">Component Separation</label>&nbsp;<label class="radio"><input type="radio" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Component Separation" class="type_total">$175</label>&nbsp;&nbsp;&nbsp;<label class="long">Optimizing Laparoscopic Inguinal</label>&nbsp;<label class="radio"><input type="radio" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Optimizing Laparoscopic Inguinal" class="type_total">$175</label></span>';
		}
		 $('#'+list+' td').html(tdInner);
		 if(tdInner != '') {
			$('tr#'+list+'_row').addClass('active');
			$('#'+list).show();
		 } else {
		 	$('#'+list).hide();
		 	$('tr#'+list+'_row').removeClass('active'); 
		 }
		 calculateTotals();       
	}

	function calculateTotals()  {
		
		//EARLY SIGN-UP
		//var member_price = 499;
		//var member_ehs_price = 499;
		//var nonmember_price = 699;
		//var resident_price = 249;
		//var guest_price = 40;
		
		//LATE SIGN-UP
		var member_price = 549;
		var member_ehs_price = 549;
		var nonmember_price = 749;
		var resident_price = 299;
		var guest_price = 40;
		
		var member_subtotal;
		var member_ehs_subtotal;
		var nonmember_subtotal;
		var resident_subtotal;
		var guest_subtotal;
		var post_grad_subtotal;
		
		var total;
		
		member_subtotal = $('select#attendees_member').val() * member_price;
		member_ehs_subtotal = $('select#attendees_member_ehs').val() * member_ehs_price;
		nonmember_subtotal = $('select#attendees_nonmember').val() * nonmember_price;
		resident_subtotal = $('select#attendees_resident').val() * resident_price;
		//guest_subtotal = $('select#attendees_guest').val() * guest_price;
		//total = member_subtotal + member_ehs_subtotal + nonmember_subtotal + resident_subtotal + guest_subtotal;
		
		var post_grad_subtotal = 0;
		
		$("input.post_grad:checked").each(function (i) {	
		
			//var post_grad_choice = $("input[name=post_grad]:checked").val();
			var post_grad_choice = $(this).val();
			//alert(post_grad_choice);
			switch(post_grad_choice) {
				case 'Chronic Pain':
					 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	case 'Component Separation':
			  		 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	case 'Optimizing Laparoscopic Inguinal':
			  		 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	default:
			  		 post_grad_subtotal = post_grad_subtotal + 0;
			  		 //post_grad_choice = 'None';
			  		 break;
			}
		
		});	
		
		//alert(post_grad_subtotal);
		total = member_subtotal + member_ehs_subtotal + nonmember_subtotal + resident_subtotal + post_grad_subtotal;
		//alert(total);
		$('tr#member_row td.subtotal').text('$'+member_subtotal.toFixed(2));
		$('tr#member_ehs_row td.subtotal').text('$'+member_ehs_subtotal.toFixed(2));
		$('tr#nonmember_row td.subtotal').text('$'+nonmember_subtotal.toFixed(2));
		$('tr#resident_row td.subtotal').text('$'+resident_subtotal.toFixed(2));
		
		//$('tr#guest_row td.subtotal').text('$'+guest_subtotal.toFixed(2));
		
		$('td.post_grad_subtotal').text('$'+post_grad_subtotal.toFixed(2));
		
		$('td.total').text('$'+total.toFixed(2));
		$("input#total").val(total.toFixed(2));
		
		$("input#member_subtotal").val(member_subtotal.toFixed(2));
		$("input#member_ehs_subtotal").val(member_ehs_subtotal.toFixed(2));
		$("input#nonmember_subtotal").val(nonmember_subtotal.toFixed(2));
		$("input#resident_subtotal").val(resident_subtotal.toFixed(2));
		$("input#post_grad_subtotal").val(post_grad_subtotal.toFixed(2));	
		//$("input#guest_subtotal").val(guest_subtotal.toFixed(2));
		
		$("input#member_price").val(member_price);
		$("input#member_ehs_price").val(member_price);
		$("input#nonmember_price").val(nonmember_price);
		$("input#resident_price").val(resident_price);
		//$("input#guest_price").val(guest_price);
	}
	
	// POST GRAD COURSE REGISTRATION FORM
	
	$("#post_grad").validate({
	   submitHandler: function(form) {
	      //debug: true;
	      var submit = 0;
	      
	      var total = $('#total').val();
	      
	      if((total != 0) || (total != 0.00)) {
	      
		      var member_list = '';
		      
		      $("tr#member td span.name_field input[type='text']").each(function (i) {	        
		         member_list += $(this).val() + ' - ';
		         var member_name_input_id = $(this).attr('id');
		         if($('input[name="post_grad_'+member_name_input_id+'"]:checked').val() != undefined){
		        	 member_list += "Post Grad Course Selection: " + $('input[name="post_grad_'+member_name_input_id+'"]:checked').val();
		         } else {
		        	 member_list += "Post Grad Course Selection: None";
		         }
		         member_list += '<br/>';
		      });

		      var member;
		      var attendees = '';
		      
		      if(member_list != '') {
		      	//member = member_list;
		      	attendees += member_list;
		      }
		      $("input#attendee_list").val(attendees);	      
		      //alert(attendees);
		      //return false;	      
		      var str = $('#cc_exp_date').val();
		      var exp_date = str.replace("/", "");
		      $.ajax({
	                  url: base_url + "adm1nAHS/aim/process.php", 
	                  global: false, 
	                  async: false, 
	                  type: "POST", 
	                  data: ({
		                  		cc_number : $('#cc_number_long').val(), 
		                  		cc_exp_date : exp_date, 
		                  		cc_cvv : $('#cc_cvv').val(), 
		                  		total : $('#total').val(), 
		                  		x_description : $('#x_description').val(), 
		                  		cc_first_name : $('#cc_first_name').val(), 
		                  		cc_last_name : $('#cc_last_name').val(), 
		                  		cc_address : $('#cc_address').val(), 
		                  		cc_state : $('#cc_state').val(), 
		                  		cc_zip_code : $('#cc_zip_code').val(), 
		                  		cc_country : $('#cc_country').val(), 
		                  		cc_phone : $('#cc_phone').val()	                  		
	                  		}),
	                  dataType: "xml",
	                  success: function(xml){
	        		       var code = $(xml).find("code").text();
	        		       var reason_code = $(xml).find("reasoncode").text();
	        		       var reason_text = $(xml).find("text").text();
	        		       if(code == 1) {
	        		       		ccstring = $('#cc_number_long').val();
	                            var ccLength = ccstring.length;
	                            $('#cc_number').attr('value', ccstring.substr(ccLength-4,4));
	                            $('#auth_code').attr('value', $(xml).find("authcode").text());
	                            $('#transaction_id').attr('value', $(xml).find("transactionid").text());
	                            $('#code').attr('value', code);
	                            $('#reason_code').attr('value', reason_code);
	                            $('#reason_text').attr('value', reason_text);
	                            form.submit();						
	        		       } else {
	        		       		alert("Error: "+reason_text+" Code "+code+"-"+reason_code);
	        		       }
	        		       
	        		       //alert($(xml).find("code").text());
	        		       //alert($(xml).find("reasoncode").text());
	        		       //alert($(xml).find("text").text());
	        		       //alert($(xml).find("authcode").text());
	        		       //alert($(xml).find("transactionid").text());
	                  },
	                  error:function (xhr, ajaxOptions, thrownError){
	                  		alert('Error: '+xhr.status+' '+xhr.statusText);
	                  }  
	               }
	            ).responseText;
            
            
            } else {
	   			alert('Your registration total is $0.00.   Please make a selection in Step 2: Attendees and click Submit again.');
	   		}
            
            return false;                 
	   }   
   	});
	
	$("#post_grad select.attendees").change(function() {
		//alert($(this).attr('rel'));
		slideDownPGList($(this).attr('rel'), $(this).val());			
	});

	$('#post_grad input[type="radio"].post_grad').live('change', function() {
		//alert('totals');
		calculatePGTotals();
	});
		
	function slideDownPGList(list, count)  {
		 var i=0;
		 var tdInner = '';
		 while (i<count) {
		 	i++;
		 	tdInner += '<span class="name_field"><label>Name '+i+'</label><input id="'+list+i+'" name="'+list+i+'" type="text" class="required" /><br/><label class="long" style="padding-top: 10px;"><strong>Post Grad Courses*</strong> - Registration is limited. Please pick one per attendee.</label><br/><label class="long">Chronic Pain</label>&nbsp;<label class="radio"><input type="radio" checked="checked" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Chronic Pain" class="type_total">$175</label>&nbsp;&nbsp;&nbsp;<label class="long">Component Separation</label>&nbsp;<label class="radio"><input type="radio" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Component Separation" class="type_total">$175</label>&nbsp;&nbsp;&nbsp;<label class="long">Optimizing Laparoscopic Inguinal</label>&nbsp;<label class="radio"><input type="radio" id="post_grad_'+list+i+'" name="post_grad_'+list+i+'" class="post_grad" value="Optimizing Laparoscopic Inguinal" class="type_total">$175</label></span>';
		}
		 $('#'+list+' td').html(tdInner);
		 if(tdInner != '') {		 		
		 	$('tr#'+list+'_row').addClass('active');
		 	$('#'+list).fadeIn(500);
		 	
		 } else {
		 	$('#'+list).fadeOut(300);
		 	$('tr#'+list+'_row').removeClass('active'); 
		 }
		 calculatePGTotals();       
	}

	function calculatePGTotals()  {
				
		var total;
		var post_grad_subtotal = 0;
		
		$("input.post_grad:checked").each(function (i) {	
		
			//var post_grad_choice = $("input[name=post_grad]:checked").val();
			var post_grad_choice = $(this).val();
			//alert(post_grad_choice);
			
			switch(post_grad_choice) {
				case 'Chronic Pain':
					 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	case 'Component Separation':
			  		 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	case 'Optimizing Laparoscopic Inguinal':
			  		 post_grad_subtotal = post_grad_subtotal + 175;
			  		 break;
			  	default:
			  		 post_grad_subtotal = post_grad_subtotal + 0;
			  		 //post_grad_choice = 'None';
			  		 break;
			}
		
		});	
		
		total = post_grad_subtotal;
		
		$('td.total').text('$'+total.toFixed(2));
		$("input#total").val(total.toFixed(2));
		
	}
	
	// ABSTRACTS - EDIT PRESENTATION FORM
	
	$("form#presentation_edit").validate();
	
	
	// ABSTRACTS - NEW PRESENTATION FORM
	
	var options = {
			beforeSubmit: function() {
        		//alert($('#upload_disclosure').validate().form());
				var np = $('#new_presentation').validate().form()
				if(np) {
					return $('#upload_disclosure').validate().form();
				}
				//if(($('#upload_disclosure').validate().form()) && $('#new_presentation').validate().form()) {
					//return true;
				//}
    		},
			success: submitPresentation,  // post-submit callback 
		    dataType:  'xml' // 'xml', 'script', or 'json' (expected server response type)
		};
	
	//$("#upload_disclosure").validate({
		//submitHandler: function(form) {
			//alert('submitted');
			//$('#title').val($('#First_Name').val()+' '+$('#Last_Name').val());
			//form.ajaxSubmit(options); 
		    //return false;
		//}
	//});

	// bind to the form's submit event 
	$('#upload_disclosure').submit(function() { 
		$('#title').val($('#First_Name').val()+' '+$('#Last_Name').val());
		$(this).ajaxSubmit(options); 
	    return false; 
	}); 
	
	function submitPresentation(responseText, statusText, xhr, $form) {
		var file = $(responseText).find("file").text();
		$('#Disclosure_Form_Name').val(file);
		//alert(file);
		//alert($('#Disclosure_Form_Name').val());
		$('#new_presentation').submit();
	}
		
	// ABSTRACTS - EDIT PRESENTATION
		
	$("form#edit_presentation").validate({
		   rules: {
			     Resident: "required", 
			     Type: "required" 
		   }
	});
		
	$("#Description").counter({
	    type: 'word',
	    goal: 300
	});
	
	
});
