 $(document).ready(function(){ 
    
    //Defaults
    $(overseas1yearID).attr("disabled", "disabled");
    //--------------
    
    membershipSubTotal();
    grandTotal();

    var isOverseasPostCardChecked = document.getElementById('isOverseasPostCardChecked');    
    if ($(overseasPostCardID).attr("checked"))
        isOverseasPostCardChecked.setAttribute("value", "true");
    else
        isOverseasPostCardChecked.setAttribute("value", "false");

    $(countryPD1ID).change(function() { 
		if(!$(countryGD1ID).val() && !$(countryGMS1ID).val()) {							 
			if($(countryPD1ID).val() == "New Zealand")  {
				setNzPost();
			} else {  
				setOverseasPost();
			}
		}
	} );


    $(countryGD1ID).change(function() { 
			if($(countryGD1ID).val() == "New Zealand")  {
				setNzPost();
			} else {  
				setOverseasPost();
			}
	    
	} );
	
	$(countryGMS1ID).change(function() { 
	    if($(countryGMS1ID).val() == "New Zealand")  {
			setNzPost();
		} else {  
			setOverseasPost();
		}
	} );
	
	var country;
	country = $(countryPD1ID).val();
	if (country && country != "New Zealand")
	    $(countryPD1ID).change();
	    
	country = $(countryGD1ID).val();
	if (country && country != "New Zealand")
	    $(countryGD1ID).change();
	   
	country = $(countryGMS1ID).val();
	if (country && country != "New Zealand")
	    $(countryGMS1ID).change();

    $(familyID).click(function() { $(membershipCategoryCostID).attr("value","69.00"); membershipSubTotal();} );
    $(individualID).click(function() { $(membershipCategoryCostID).attr("value","64.00"); membershipSubTotal();} );   
    $(jointSSID).click(function() { $(membershipCategoryCostID).attr("value","50.00"); membershipSubTotal(); } );
    $(individualSSID).click(function() { $(membershipCategoryCostID).attr("value","45.00"); membershipSubTotal(); } );
    $(studentUnwagedID).click(function() { $(membershipCategoryCostID).attr("value","45.00"); membershipSubTotal(); } );
	$(lifeMemberID).click(function() { $(membershipCategoryCostID).attr("value","995.00"); membershipSubTotal(); } );
	$(jointLifeMemberID).click(function() { $(membershipCategoryCostID).attr("value","1295.00"); membershipSubTotal(); } );
	
	$(family5ID).click(function() { $(membershipCategoryCostID).attr("value","345.00"); membershipSubTotal();} );
    $(individual5ID).click(function() { $(membershipCategoryCostID).attr("value","320.00"); membershipSubTotal();} );
    $(jointSS5ID).click(function() { $(membershipCategoryCostID).attr("value","250.00"); membershipSubTotal(); } );
    $(individualSS5ID).click(function() { $(membershipCategoryCostID).attr("value","225.00"); membershipSubTotal(); } );
    $(studentUnwaged5ID).click(function() { $(membershipCategoryCostID).attr("value","225.00"); membershipSubTotal(); } );

    $(overseasPostMagazineID).click(function() { 
      if($(overseasPostMagazineID).attr("checked")) {
         $(overseasPostMagazineCostID).attr("value","30.00");   
      } else {
         $(overseasPostMagazineCostID).attr("value","");           
      } 
      membershipSubTotal();    
    }); 
	
	$(overseasPostCardID).click(function() { 
      if($(overseasPostCardID).attr("checked")) {
         $(overseasPostCardCostID).attr("value","10.00"); 
         isOverseasPostCardChecked.setAttribute("value", "true");
      } else {
         $(overseasPostCardCostID).attr("value","");          
         isOverseasPostCardChecked.setAttribute("value", "false");
      } 
      membershipSubTotal();    
    }); 

    $(extraCardID).click(function() { 
      if($(extraCardID).attr("checked")) {
         $(extraCardCostID).attr("value","5.00");   
      } else {
         $(extraCardCostID).attr("value","");           
      }  
      membershipSubTotal();
    });
    
    $(expressServiceID).click(function() { 
      if($(expressServiceID).attr("checked")) {
         $(expressServiceCostID).attr("value","20.00");   
      } else {
         $(expressServiceCostID).attr("value","");           
      }  
      membershipSubTotal();
    });  


    //subscription

    $(NZ1yearID).click(function() { $(subscriptionCostID).attr("value","35.00"); grandTotal();} );
    $(NZ2yearsID).click(function() { $(subscriptionCostID).attr("value","70.00"); grandTotal();} );
    $(overseas1yearID).click(function() { $(subscriptionCostID).attr("value","65.00"); grandTotal(); } );


    //donation
    $(donationCostID).focus(function() { grandTotal(); } );
	$(donationMainCostID).focus(function() { grandTotal(); } );
    $(donationCostID).keyup(function() {  grandTotal(); } );
	$(donationMainCostID).keyup(function() {  grandTotal(); } );

    


});
	 
	 
function membershipSubTotal() {
  subTotal1 = 0;
  if($(extraCardCostID).attr("value")) subTotal1 += parseFloat($(extraCardCostID).attr("value"));
  if($(expressServiceCostID).attr("value")) subTotal1 += parseFloat($(expressServiceCostID).attr("value"));
  if($(overseasPostMagazineCostID).attr("value")) subTotal1 += parseFloat($(overseasPostMagazineCostID).attr("value"));
  if($(overseasPostCardCostID).attr("value")) subTotal1 += parseFloat($(overseasPostCardCostID).attr("value"));
  if($(membershipCategoryCostID).attr("value")) subTotal1 += parseFloat($(membershipCategoryCostID).attr("value"));  
  $(subTotal1ID).attr("value", subTotal1.toFixed(2));
  grandTotal();
}	 



function grandTotal() {
  total = 0;
  if($(subTotal1ID).attr("value")) total += parseFloat($(subTotal1ID).attr("value"));  
  if($(subscriptionCostID).attr("value")) total += parseFloat($(subscriptionCostID).attr("value"));
  if($(donationCostID).attr("value") && parseFloat($(donationCostID).attr("value"))) total += parseFloat($(donationCostID).attr("value"));
  if($(donationMainCostID).attr("value") && parseFloat($(donationMainCostID).attr("value"))) total += parseFloat($(donationMainCostID).attr("value"));
  $(grandTotalID).attr("value", total.toFixed(2));
}


function setNzPost() {
	$(overseasPostMagazineID).attr("checked", ""); 
	$(overseasPostMagazineID).attr("disabled", "disabled"); 
	$(overseasPostMagazineCostID).attr("value",""); membershipSubTotal();
	$(overseasPostCardID).attr("checked", ""); 
	$(overseasPostCardID).attr("disabled", "disabled"); 
	$(overseasPostCardCostID).attr("value",""); membershipSubTotal(); 
	$(expressServiceID).removeAttr("disabled"); 
	$(NZ1yearID).attr("checked", "checked"); 
	$(NZ1yearID).removeAttr("disabled"); 
	$(NZ2yearsID).removeAttr("disabled"); 
	$(overseas1yearID).attr("checked", ""); 
	$(overseas1yearID).attr("disabled", "disabled"); 
	$(subscriptionCostID).attr("value","35.00");  
	document.getElementById('isOverseasPostCardChecked').setAttribute("value", "false");
	grandTotal();	
}


function setOverseasPost() {
	$(overseasPostMagazineID).removeAttr("disabled"); 
	$(overseasPostCardID).attr("checked", "checked"); 
	$(overseasPostCardID).attr("disabled"); 
	$(overseasPostCardCostID).attr("value","10.00"); membershipSubTotal(); 
	$(NZ1yearID).attr("checked", ""); 
	$(NZ1yearID).attr("disabled", "disabled"); 
	$(NZ2yearsID).attr("checked", ""); 
	$(NZ2yearsID).attr("disabled", "disabled"); 
	$(overseas1yearID).attr("checked", "checked");
	$(overseas1yearID).removeAttr("disabled"); 
	$(subscriptionCostID).attr("value","65.00");  
	$(expressServiceID).attr("checked", ""); 
	$(expressServiceID).attr("disabled", "disabled");
	$(expressServiceCostID).attr("value", ""); membershipSubTotal();
	document.getElementById('isOverseasPostCardChecked').setAttribute("value", "true");
	grandTotal();
}