var type_val = "";

$(document).ready(function(){
  debug: true,

  $('#giftmbr').hide(); //initially hide the Gift Membership fields
  
  // Move to membership number if Renewal is selected. Select Renewal if user leaves Membership Number and field is not blank
$('#chooseType input:text').each(function(){
	 //these are both used twice, 
	 // the text input
	 var $inputTxt = $(this);
	 // the associated radio button
	 var $radioBtn = $inputTxt.siblings('input:radio');
	 
	 //listen for the blur event on the text input
	 $inputTxt.blur(function(){
		// if text input has text
		if ($inputTxt.val() != '' ) {
			//select radio button
			$radioBtn.attr('checked',true);
		}
	 });
	 
	 //listen for the change event on the radio button
	 $radioBtn.change(function(){
		// if it is checked, focus on text input
		if ( this.checked ) {$inputTxt.focus(); }
	 });
	 
  }); //close each()  
$("#membership").validate({							
	onkeyup:false, // do not validate after every keystroke //
	rules:{
		names: {
			required: true,	
		},
		giftnames: {
			required: '#type_2:checked',
			/* The following format didn't work to require only when a particular radio option was chosen. It made the field required when any button in the group was selected because depends did not work.
			{
			    depends: function(element) {
				    return $('#type_2:checked')
				},
			} */
		},
		giftaddress: {
			required: '#type_2:checked',
		},
		giftcity: {
			required: '#type_2:checked',
		},
		giftstate: {
			required: '#type_2:checked',
		},
		giftzip: {
			required: '#type_2:checked',
		},
		giftemail: {
			email:
			{
			    depends: function(element) {
				    return $('#type_2:checked')
				},
			}
		},
		// make membership number required if renewal is selected.
/*		mbr_number: {
			required:
			{
			    depends: function(element) {
				    return $('#renew:checked')
				},
			}
		}
*/
	}, // end rules
	messages: {
		giftnames: {
			required: 'Required for Gift Memberships'
		},
		giftaddress: {
			required: 'Required for Gift Memberships'
		},
		giftcity: {
			required: 'Required for Gift Memberships'
		},
		giftstate: {
			required: 'Required for Gift Memberships'
		},
		giftzip: {
			required: 'Required for Gift Memberships'
		},
		giftemail: {
			email: 'Not recognized as an e-mail address'
		}
	}, // end messages
	
  
	// once form is validated, assign Gift Membship & checkbox info to custom hidden field
	submitHandler: function(form) {
		// do stuff here
  
		var customfield = "";
		var mbr_names = $('#names').val();
		var mbr_number = $('#mbr_number').val();
		// customfield = "This is the text of customfield";
		// #name references the id of the element, not it
		if (type_val == "gift"){ //remember that testing equality uses '==' while setting a value uses '=' !!
			customfield = "\n" + $('#giftnames').val() + "; \n" + $('#giftaddress').val() + "; \n" + $('#giftcity').val() + ", " + $('#giftstate').val() + " " + $('#giftzip').val() + ";\n";
			// alert(customfield);
			customfield += "E-mail: " + $('#giftemail').val() + "; \n";
			// alert(customfield);
			customfield += "Phone: " + $('#giftphone').val() + "; \n";
			// alert(customfield);
		}
		var match_check = $('#matching').attr('checked');
		// customfield += "Matching Gift: " + match_check + "; \n";
		// alert(customfield);
		var info_check = $('#sendinfo').attr('checked');
		// customfield += "Send Info: " + info_check;
		// alert(customfield);		
		// alert('The value of customfield is: ' + customfield);
		// $('input[name=custom]').val(customfield); // set the hidden control named "custom" to the value text stored in customfield
		$('input[name=os0]').val(mbr_names); // set the hidden control named "os0" to the text stored in mbr_names for Member Names
		$('input[name=os1]').val(customfield); // set the hidden control named "os1" to the text stored in customfield for Gift Membership info
		$('input[name=os2]').val(match_check); // set the hidden control named "os2" to the text stored in match_check Matching Gift Program
		$('input[name=os3]').val(info_check); // set the hidden control named "os3" to the text stored in info_check for Send Volunteer Info
		$('input[name=os4]').val(type_val); // set the hidden control named "os4" to the text stored in info_check for Send Volunteer Info
		$('input[name=os5]').val(mbr_number); // set the hidden control named "os5" to the text stored in info_check for Send Volunteer Info
		form.submit();
	}
	
}); // end Validate
  
  // Toggle visibility of Gift Membership fields
  $('#type_0').click(function() {
	if ($('#giftmbr').is(':visible')) {
		$('#giftmbr').hide();
		mbr_type = "New";
		}
	}
	); // end type_0
    $('#type_1').click(function() {
	if ($('#giftmbr').is(':visible')) {
		$('#giftmbr').hide();		
		mbr_type = "Renew";
		}
	}
	); // end type_1
	
// Make gift membership fields visible when Gift Membership radio button is clicked	
  $('#type_2').click(function() {
	$('#giftmbr').show();
	mbr_type = "Gift";
	}
	);

  
  $(':radio[name=type]').change(function(){
    type_val = ($(this).val());
  });
			  
  $(':radio[name=level]').change(function(){ //select any radio button in grouped named "level" and react to a select action (change)
    var mem_id = $(this).val();
	switch (mem_id) {
		case "stdnt1":
		$('input[name=amount]').val("25.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Student Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("STDNT1");
		break;
		case "pda1":
		$('input[name=amount]').val("60.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Painted Desert Affiliate Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("PDA1");
		break;
		case "pda2":
		$('input[name=amount]').val("48.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Painted Desert Affiliate Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("PDA2");
		break;
		case "scc1":
		$('input[name=amount]').val("100.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Sunset Crater Colleague Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("SCC1");
		break;
		case "scc2":
		$('input[name=amount]').val("80.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Sunset Crater Colleague Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("SCC2");
		break;
		case "cc1":
		$('input[name=amount]').val("150.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Canyonlands Contributor Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("CC1");
		break;
		case "cc2":
		$('input[name=amount]').val("120.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Canyonlands Contributor Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("CC2");
		break;
		case "cs1":
		$('input[name=amount]').val("275.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Chaco Sponsor Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("CS1");
		break;
		case "cs2":
		$('input[name=amount]').val("220.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Chaco Sponsor Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("CS2");
		break;
		case "bma1":
		$('input[name=amount]').val("500.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Black Mesa Associate Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("BMA1");
		break;
		case "bma2":
		$('input[name=amount]').val("400.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Black Mesa Associate Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("BMA2");
		break;
		case "kf1":
		$('input[name=amount]').val("1000.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Kaibab Fellow Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("KF1");
		break;
		case "kf2":
		$('input[name=amount]').val("800.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Kaibab Fellow Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("KF2");
		break;
		case "mvc1":
		$('input[name=amount]').val("2500.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Mesa Verde Circle Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("MVC1");
		break;
		case "mvc2":
		$('input[name=amount]').val("2000.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Mesa Verde Circle Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("MVC2");
		break;
		case "gcc1":
		$('input[name=amount]').val("5000.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Grand Canyon Associate Membership [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("GCC1");
		break;
		case "gcc2":
		$('input[name=amount]').val("4000.00"); //set the hidden control named "amount" to the value inside val()
		$('input[name=item_name]').val("Grand Canyon Associate Membership - Senior/Educator [Membership materials will arrive via mail in approx 7 days]");
		$('input[name=item_number]').val("GCC2");
		break;
	    }
// alert($(this).val());
//   $('input[name=amount]').val("165.00"); //set the hidden control named "amount" to the value inside val()
//   });
   });

});
