$(document).ready(function(){
		
	
	// email function					   
	$('.sendlnk').click(function(){
	var email = $(this).html();
	document.location.href =  "mailto:" + email.replace('&#64;', '@');
	});
	
	
	// paypal form
	if($('#joincsa').length == 1){
		//console.log('form exists');
		var csacost = 180.00;
		var csashipping = 19.00;
		var annualShipments = 3;
		var shiprate1 = 25.26; //6.08 * annualShipments;
		var shiprate2 = 25.26 //8.21 * annualShipments;
		var shiprate3 = 31.88 // 9.57 * annualShipments;
		var shiprate4 = 38.04 //10.85 * annualShipments;
		var shiprate5 = 38.04 //14.85 * annualShipments;
		var shiprate6 = 0;
		var shipdistance1 = ["NY", "NJ", "CT"];
		var shipdistance2 = ["ME", "VT", "NH", "MA", "RI", "DE", "DC"]
		var shipdistance3 = ["AR", "AL", "AZ", "PA", "MD", "OH", "MI", "IL", "MO", "IN", "OH", "VA", "NC", "SC", "WV", "KY", "GA", "FL", "DC","AK", "HI"];
		var csatotal;
		
		
		$('#state').change(function(){
			var selectedState = $('#state').val();
			if( selectedState == "NY" || selectedState == "NJ"){
				doNy();
				doTotal();
			} else {
				hideNy();
				doTotal();	
			}
			
		});
		
		$('input[type=image]').mouseover(function(){
			var selectedState = $('#state').val();
		    if(selectedState != ""){
				if( selectedState == "NY" || selectedState == "NJ"){
					doNy();
					doTotal();
				} else {
					hideNy();
					doTotal();	
				}	
			}
		});
		$('input[type=radio]').click(function(){
					 doTotal();
		});
		
		$('input[type=image]').click(function(){
					//alert($('#pickuplocation').attr("selectedIndex"));
					if($("input[@name='pickup']:checked").val() == "delivery" && $('#pickuplocation').get(0).selectedIndex != 0){
						alert("You have selected both 'delivery' and a pickup location. Please select either delivery OR a pick up location. Not both.")
						return false;
					}
					if($("input[@name='pickup']:checked").val() == "pickup" && $('#pickuplocation').get(0).selectedIndex == 0){
						alert("Please select a pickup location");
						return false;
					}
					return true;
		});
		
		
		$("input[name='pickup']").change(function(){
				doTotal();
		});
		
		function doNy(){
			$('#newyorkonly').css('display','block');
			
		}
		
		function hideNy(){
			$('#newyorkonly').css('display','none');	
		}
		
		function doShipping(){
			var selectedState = $('#state').val();

			if((selectedState == "NY" || selectedState == "NJ") && $("input[type=radio]:checked", '#joincsa').val()){
			   if($("input[type=radio]:checked").val() == "pickup") {
				   
				   $('#mailingregion').get(0).selectedIndex = 5;
				   return "0.00";
			   }
			   else if($("input[type=radio]:checked").val() == "delivery") {
				  $('#mailingregion').get(0).selectedIndex = 0;
				   return shiprate1;
			   }
			}
			else if($.inArray(selectedState, shipdistance1) >= 0){
				$('#mailingregion').get(0).selectedIndex = 0;
				return shiprate1;
			}
			else if($.inArray(selectedState, shipdistance2) >= 0) {
				$('#mailingregion').get(0).selectedIndex = 1;
				return shiprate2;
			}
			else if($.inArray(selectedState, shipdistance3) >= 0){
				$('#mailingregion').get(0).selectedIndex = 2;
				return shiprate3;
			}
			else if(selectedState != "NY" && selectedState != "NJ"){
				$('#mailingregion').get(0).selectedIndex = 3;
				return shiprate4;
			}
			else alert("we're working on the page at the moment. please come back in 5 minutes");
			
			$('#mailingregion').get(0).selectedIndex = 3;
			return shiprate4;	
		}
		
		function doTotal(){
			var csashipping = doShipping();
			$('#csashipping').html('$ ' + parseFloat(csashipping).toFixed(2));
			var csatotal = (180*1)  + (csashipping*1);
			$('#csatotal').html('$ ' + parseFloat(csatotal).toFixed(2));
		}
		
	}
		
});
