main();

 //----------------------------------------------------------------------------
 //Primary operations
 function main() {
   document.applicationForm.newMember[0].onclick = clear1Click;
   document.applicationForm.newMember[1].onclick = resetLease;
   document.applicationForm.newMember[2].onclick = resetLease;
   document.applicationForm.newMember[3].onclick = prepareDonation;
   document.applicationForm.family1.onchange = totalUp;
   document.applicationForm.family2.onchange = totalUp;
   document.applicationForm.family3.onchange = totalUp;
   document.applicationForm.family4.onchange = totalUp;
   document.applicationForm.family5.onchange = totalUp;
   document.applicationForm.lease.onchange = leaseChange;
   document.applicationForm.clear1.onclick = clear1Click;
   //document.applicationForm.spectrum[0].onclick = needEmail;
   //document.applicationForm.spectrum[1].onclick = needEmail; //Verify email address for spectrum
   //document.applicationForm.spectrum[2].onclick = needEmail;
   //document.applicationForm.email.onchange = needEmail;
   document.applicationForm.astroMag.onclick = totalUp; //Add Astronomy Magazine subscription
   document.applicationForm.skyTelMag.onclick = totalUp; //Add Sky and Telescope subscription
   //document.applicationForm.email.onmouseover = submitPress;
   document.applicationForm.donationBox.onchange = toggleDonationBox; 
   document.applicationForm.subtotalDonation.onkeyup = checkDonationAmt;
   document.applicationForm.subtotalDonation.onchange = totalUp;
   totalUp();
  } //main
  
 //----------------------------------------------------------------------------
	function submitPress() {
	//Assemble GET string and save in hidden field 'custom'
	 getString = new String("http://www.texasastro.org/cgi-bin/view.cgi?a=");
	 temp = new String("");
	 
	 //First, update all current totals
	 totalUp();
	 
	 //New member?
	 if(document.applicationForm.newMember[0].checked == true) {
	   getString += "1"; //new member
	    document.applicationForm.item_name.value = "New Annual Membership";
         } //if 
	 if(document.applicationForm.newMember[1].checked == true) {
	   getString += "0"; //existing member
	    document.applicationForm.item_name.value = "Existing Member Renewal";
         } //if 
	 if(document.applicationForm.newMember[2].checked == true) {
	   getString += "2"; //New Life member
	    document.applicationForm.item_name.value = "New Life Membership";
         } //if 
	 if(document.applicationForm.newMember[3].checked == true) {
	   getString += "3"; //Donation
	    document.applicationForm.item_name.value = "Donation";
         } //if 
	 
	 //OK to release office phone?
	 if(document.applicationForm.Day_phone_aOK.checked == true) {
	   getString += "1"; //OK
	 } //if
	 else {
	   getString += "0"; //Not OK
	 } //else
	 
	 //OK to release mobil phone?
	 if(document.applicationForm.mobilPhoneOK.checked == true) {
	   getString += "1"; //OK
	 } //if
	 else {
	   getString += "0"; //Not OK
	 } //else
	 
	 //OK to release pager?
//	 if(document.applicationForm.Day_phone_bOK.checked == true) {
//	   getString += "1"; //OK
//	 } //if
//	 else {
	   getString += "0"; //Not OK
//	 } //else
	  
	 //OK to release email address?
	 if(document.applicationForm.emailOK.checked == true) {
	   getString += "1"; //OK
	 } //if
	 else {
	   getString += "0"; //Not OK
	 } //else
	 
	 //Spectrum delivery method
	 //if(document.applicationForm.spectrum[0].checked == true) {
	 //  getString += "0"; //snail mail
	 //} //if
	 //else if(document.applicationForm.spectrum[1].checked == true) {
	   getString += "1"; //email only
	 //} //else if
	 //else if(document.applicationForm.spectrum[2].checked == true) {
	 //  getString += "2"; //both
	 //} //else if
	 
	 //Sky and Telescope magazine?
	 if(document.applicationForm.skyTelMag.checked == true) {
	   getString += "1"; //Yes
	 } //if
	 else {
	   getString += "0"; //No
	 } //else
	 
	 //Astronomy Magazine?
	 if(document.applicationForm.astroMag.checked == true) {
	   getString += "1"; //Yes
	 } //if
	 else {
	   getString += "0"; //No
	 } //else
	 
	 //Primary member dues
	 switch(document.applicationForm.subtotalPrimary.value) {
	   case "20.00" : 
	     getString += "0";
	     break;
	   case "30.00" :
	     getString += "1"; 
	     break;
	   case "40.00" :
	     getString += "2";
	     break;
	   case "600.00" :
	     getString += "3";
	     break;		 
	   case "0.00" :
	     getString += "4";
	     break;
	   default : getString += "2";
	 } //switch
	 
	 //Family member dues
	 switch(document.applicationForm.subtotalFamily.value) {
	   case "0.00" : 
	     getString += "0";
	     break;
	   case "4.00" :
	     getString += "1"; 
	     break;
	   case "8.00" :
	     getString += "2";
	     break;
	   case "12.00" :
	     getString += "3"; 
	     break;
	   default : getString += "0";
	 } //switch
	 
	 //Home phone
	 temp = document.applicationForm.night_phone_a.value;
	 if(temp.length > 0) {
  	   getString += "&b=" + strip(document.applicationForm.night_phone_a.value);
	 } //if
	 
	 //Ham Radio
	 temp = document.applicationForm.ham.value;
	 if(temp.length > 0) {
	   getString += "&c=" + strip(document.applicationForm.ham.value);
	 } //if
	 
	 //Lease sites
	 temp = document.applicationForm.selectedSites.value;
	 if(temp.length > 0) {
  	   getString += "&d=" + encode(document.applicationForm.selectedSites.value);
	 } //if
	 
	 //Office phone
	 temp = document.applicationForm.Day_phone_a.value;
	 if(temp.length > 0) {
	   getString += "&e=" + strip(document.applicationForm.Day_phone_a.value);
	 } //if
	 
	 //Mobil phone
	 temp = document.applicationForm.mobilPhone.value;
	 if(temp.length > 0) {
	   getString += "&f=" + strip(document.applicationForm.mobilPhone.value);
	 } //if
	 
	 //Pager
//	 temp = document.applicationForm.Day_phone_b.value;
//	 if(temp.length > 0) {
//	   getString += "&g=" + strip(document.applicationForm.Day_phone_b.value);
//	 } //if
	 
	 //Email address
	 temp = document.applicationForm.email.value;
	 if(temp.length > 0) {
	   getString += "&h=" + encode(document.applicationForm.email.value);
	 } //if
	 
	 //Family member #1
	 temp = document.applicationForm.family1.value;
	 if(temp.length > 0) {
	   getString += "&i=" + encode(document.applicationForm.family1.value);
	 } //if
	 
	 //Family member #2
	 temp = document.applicationForm.family2.value;
	 if(temp.length > 0) {
	   getString += "&j=" + encode(document.applicationForm.family2.value);
	 } //if
	 
	 //Family member #3
	 temp = document.applicationForm.family3.value;
	 if(temp.length > 0) {
	   getString += "&k=" + encode(document.applicationForm.family3.value);
	 } //if
	  
	 //Family member #4
	 temp = document.applicationForm.family4.value;
	 if(temp.length > 0) {
	   getString += "&l=" + encode(document.applicationForm.family4.value);
	 } //if
	 
	 //Family member #5    
	 temp = document.applicationForm.family5.value;
	 if(temp.length > 0) {
	   getString += "&m=" + encode(document.applicationForm.family5.value);
	 } //if

	 //Donation Amt    
         if ( document.applicationForm.donationBox.checked == true ) {
  	   temp = document.applicationForm.subtotalDonation.value;
	   if(temp.length > 0) {
	     getString += "&n=" + encode(document.applicationForm.subtotalDonation.value);
	   } //if
  	 } // if

	 //Primary Member Name    
	 temp = document.applicationForm.first_name.value;
	 if(temp.length > 0) {
	   getString += "&o=" + encode(document.applicationForm.first_name.value);
	 } //if
	 
	 //Primary Member Last Name    
	 temp = document.applicationForm.last_name.value;
	 if(temp.length > 0) {
	   getString += "&p=" + encode(document.applicationForm.last_name.value);
	 } //if

	 //Member Address    
	 temp = document.applicationForm.address1.value;
	 if(temp.length > 0) {
	   getString += "&q=" + encode(document.applicationForm.address1.value);
	 } //if
	 temp = document.applicationForm.city.value;
	 if(temp.length > 0) {
	   getString += "&r=" + encode(document.applicationForm.city.value);
	 } //if
	 temp = document.applicationForm.state.value;
	 if(temp.length > 0) {
	   getString += "&s=" + encode(document.applicationForm.state.value);
	 } //if
	 temp = document.applicationForm.Zip.value;
	 if(temp.length > 0) {
	   getString += "&t=" + encode(document.applicationForm.Zip.value);
	 } //if

/*
	 //Sky and Tel renewal account number
	 temp = document.applicationForm.skyTelMagAcct.value;
	 if(temp.length > 0) {
	   getString += "&n=" + encode(document.applicationForm.skyTelMagAcct.value);
	 } //if
	 
	 //Astronomy Magazine renewal account number
	 temp = document.applicationForm.astroMagAcct.value;
	 if(temp.length > 0) {
	   getString += "&o=" + encode(document.applicationForm.astroMagAcct.value);
	 } //if
*/	
	 //Save GET string to hidden field
	 document.applicationForm.custom.value = getString; 

//         alert(getString);
} //submitPress

 //----------------------------------------------------------------------------
 function needEmail() { 
 //Verify email address for spectrum
   if(document.applicationForm.email.value == "" 
       && (document.applicationForm.spectrum[1].checked == true 
	   || document.applicationForm.spectrum[2].checked == true) ) {
	 alert("You must provide an email address above\n" + "to receive the Spectrum via email");
	 document.applicationForm.spectrum[0].checked = true;
	} //if
 } //needEmail
 
 //----------------------------------------------------------------------------
 function decimals(f, p) {
  // Returns a string representing a fp number rounded to a specified number of decimal places
  // f = fp number to be rounded
  // p = number of decimal places to return
  // Example: decimals(3.14159,2) returns "3.14"
  var IntPart;
  var Composite;
  var sDecPart;
  var i;
  IntPart = parseInt(f.toString());
  f -= IntPart;
  f *= Math.pow(10,p);
  f = Math.round(f);
  f /= Math.pow(10,p);
  sDecPart = f.toString();
  for(i = 1; i <= p; i++) sDecPart += "0";
  Composite = IntPart + "." + sDecPart.substr(sDecPart.indexOf(".") + 1, p);
  return Composite;
 }

 //----------------------------------------------------------------------------
 function verify(f) {
 //Be sure all required fields are filled out when user clicks Submit
 var okay = true;
  msg = "_______________________________";
  msg += "\n\nThe form was not submitted because of the following error(s).";
  msg += "\nPlease correct these error(s) and resubmit."
  msg += "\n______________________________";

   if(f.first_name.value == "") {
     msg += "\n -- Primary Name missing";
	 okay = false;
	} //if
   if(f.email.value == "") {
     msg += "\n -- Email Address missing";
	 okay = false;
	} //if
   if(f.address1.value == "") {
     msg += "\n -- Address missing";
	 okay = false;
	} //if
   if(f.city.value == "") {
     msg += "\n -- City missing";
	 okay = false;
	} //if
   if(f.state.value == "") {
     msg += "\n -- State Name missing";
	 okay = false;
	} //if
   if(f.Zip.value == "") {
     msg += "\n -- Zip Code missing";
	 okay = false;
	} //if
   if(f.night_phone_a.value == "") {
     msg += "\n -- Home Phone missing";
	 okay = false;
	} //if
   if ( f.donationBox.checked == true &&
        (f.subtotalDonation.value == "" || f.subtotalDonation.value == "0.00") ) {
      msg += "\n -- Please enter donation amount or uncheck donation box"; 
      okay = false;
      } //if 
   if ( parseFloat(f.amount.value) < 1 ) { 
      msg += "\n -- Invalid options selected. Please verify form"; 
      okay = false;
      } //if 
   if(!okay) { //information is missing
	  alert(msg);
	} //if
	else {
	  submitPress();
	} //else
   return okay;
 } //verify
 
 //----------------------------------------------------------------------------
 function totalUp() {
 //Total up all subtotals into the grand total box
   //Declarations
   var subtotalPrimary = document.applicationForm.subtotalPrimary, subtotalFamily;
   var subtotalSkyTelMag, subtotalAstroMag, total, subtotalLease, paypal, subTotal;
   var subtotalDonation; 
   
   //subtotalPrimary
   switch(document.applicationForm.month.value) {
         case "1" :
	   document.applicationForm.subtotalPrimary.value = "20.00";
	   break;
	 case "2" :
	   document.applicationForm.subtotalPrimary.value = "20.00";
	   break;
	 case "3" :
	   document.applicationForm.subtotalPrimary.value = "20.00";
	   break;
	 case "0" :
	   document.applicationForm.subtotalPrimary.value = "30.00";
	   break;
	 case "10" :
	   document.applicationForm.subtotalPrimary.value = "30.00";
	   break;
	 case "11" :
	   document.applicationForm.subtotalPrimary.value = "30.00";
	   break;
	 default :
	   document.applicationForm.subtotalPrimary.value = "40.00";
	   break;
   } //switch
      
   //Override primary dues if Life Membership
   if(document.applicationForm.newMember[2].checked == true) {
     document.applicationForm.subtotalPrimary.value = "600.00";
   } //if
   if(document.applicationForm.newMember[3].checked == true) {
     document.applicationForm.subtotalPrimary.value = "0.00";
   } //if
   
   subtotalPrimary = document.applicationForm.subtotalPrimary.value;
   
   //subtotalFamily
   subtotalFamily = 0;
   if(!document.applicationForm.newMember[2].checked && !document.applicationForm.newMember[3].checked) {
     if(document.applicationForm.family1.value != "" && subtotalFamily < 12) subtotalFamily += 4;
     if(document.applicationForm.family2.value != "" && subtotalFamily < 12) subtotalFamily += 4;
     if(document.applicationForm.family3.value != "" && subtotalFamily < 12) subtotalFamily += 4;
     if(document.applicationForm.family4.value != "" && subtotalFamily < 12) subtotalFamily += 4;
     if(document.applicationForm.family5.value != "" && subtotalFamily < 12) subtotalFamily += 4;
   } //if
   document.applicationForm.subtotalFamily.value = decimals(subtotalFamily, 2);
   
   //subtotalSkyTelMag
   if(document.applicationForm.skyTelMag.checked == true) document.applicationForm.subtotalSkyTelMag.value = "32.95";
   else document.applicationForm.subtotalSkyTelMag.value = "0.00";
   subtotalSkyTelMag = document.applicationForm.subtotalSkyTelMag.value;
   
   //subtotalAstroMag
   if(document.applicationForm.astroMag.checked) document.applicationForm.subtotalAstroMag.value = "34.00";
   else document.applicationForm.subtotalAstroMag.value = "0.00";
   subtotalAstroMag = document.applicationForm.subtotalAstroMag.value;
   
   //subtotalLease
   subtotalLease = document.applicationForm.leaseTotal.value;

   // subtotalDonation
   if(document.applicationForm.donationBox.checked == false) 
       document.applicationForm.subtotalDonation.value = "0.00";
   subtotalDonation = document.applicationForm.subtotalDonation.value;
   
   //Paypal fee
   subTotal = parseFloat(subtotalPrimary)
   + parseFloat(subtotalFamily)
   + parseFloat(subtotalSkyTelMag)
   + parseFloat(subtotalAstroMag)
   + parseFloat(subtotalLease)
   + parseFloat(subtotalDonation);
   total = (subTotal + .30)/.971;
   paypal = total - subTotal;
   document.applicationForm.paypalFee.value = decimals(paypal,2);
   
   //Grand total
   document.applicationForm.amount.value = decimals(total, 2); 
 } //totalUp
 
 //----------------------------------------------------------------------------
 function leaseChange() {
 //Track total cost of all selected lease sites
 //Declarations
   var s1, s2, newTotal;
   
 //Initializations
   newTotal = 0;
   
 //This function available to existing members only
   if(document.applicationForm.newMember[0].checked == true || 
      document.applicationForm.newMember[3].checked == true )
     return;
	 
 //Processing
   if(document.applicationForm.lease.value != "NONE") {
     document.applicationForm.selectedSites.value += document.applicationForm.lease.value + " ";
	 s1 = document.applicationForm.selectedSites.value;
	 s2 = s1.split(" ");
	 l = s2.length - 1;
	 for(i = 0; i < l; i++) {
	   switch(s2[i].charAt(0)) {
	     case 'A' :
		   newTotal += 50;
		   break;
		 case 'B' :
		   newTotal += 40;
		   break;
		 case 'C' :
		   newTotal += 40;
		   break;
		 case 'D' :
		   newTotal += 40;
		   break;
		 case 'E' :
		   newTotal += 40;
		   break;
		 case 'F' :
		   newTotal += 20;
		   break;				 
		 case 'G' :
		   newTotal += 20;
		   break;				 
		 case 'J' :
		   newTotal += 20;
		   break;
		 default :
		   break; //do nothing				 
	   } //switch
	 } //for
	 document.applicationForm.leaseTotal.value = decimals(newTotal, 2);
	 totalUp();
	} //if
	else {
	  clear1Click();
	}
 } //leaseChange
 
 //----------------------------------------------------------------------------
 function clear1Click() {
 //Clear selected lease sites box and reset lease total to zero
   document.applicationForm.selectedSites.value = "";
   document.applicationForm.leaseTotal.value = "0.00";
   document.applicationForm.donationBox.checked = false; 
   toggleDonationBox();
   resetLease();
   totalUp();
 } //clear1Click
 
 //----------------------------------------------------------------------------
 function resetLease() {
 //Sets the lease site selector back to "NONE"
   document.applicationForm.lease.value = "NONE";
   totalUp();
 } //resetLease

 //----------------------------------------------------------------------------
 function strip(s) {
 //Remove all non-alphanumeric characters
   len = s.length;
   retVal = new String("");

   for(i = 0; i < len; i++) {
     c = s.charCodeAt(i);
     if( (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) ) {
       retVal += s.charAt(i); //is a character; otherwise skip it     
     } //if
   } //for

   return retVal;
 } //strip

 //----------------------------------------------------------------------------
 function encode(s) {
 //Encode all non-alphanumeric characters into a url-friendly format
   len = s.length;
   retVal = new String("");

   for(i = 0; i < len; i++) {
     c = s.charCodeAt(i);
     if( c < 48 || (c > 57 && c < 65) || (c > 90 && c < 97) || c > 122 ) {
       if(c == 32) { //space
         retVal += '+';
       }
       else { //not a space and not url-friendly
         retVal += "%" + c.toString(16);
       }
     } //if
     else { //already a url-friendly character
       retVal += s.charAt(i);
     } //else
   } //for

   return retVal;
 } //encode

//----------------------------------------------------------------------------
function CalcKeyCode(aChar) {
// calculate the ASCII code of the given character
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
} // CalcKeyCode
//----------------------------------------------------------------------------
function checkNumber(val) {
// Check if the keyed-in character is a number 
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
} // checkNumber

//----------------------------------------------------------------------------
function checkDonationAmt() {
//Checks the donation amount for numeric value as it is entered
  var val = document.applicationForm.subtotalDonation; 
  checkNumber(val); 
  document.applicationForm.subtotalDonation.value = val.value; 
  return false; 
} // checkDonationAmt

//----------------------------------------------------------------------------
function toggleDonationBox() { 
// Enables/disables the donation amount field
   if ( document.applicationForm.donationBox.checked == true ) { 
     document.applicationForm.subtotalDonation.readOnly = false; 
   } else 
   { 
     document.applicationForm.subtotalDonation.value = "0.00"; 
     document.applicationForm.subtotalDonation.readOnly = true; 
   } 
   totalUp();
   return false;
} // toggleDonationBox

//----------------------------------------------------------------------------
function prepareDonation() { 
// Enables/disables the donation amount field
  resetLease();
  document.applicationForm.donationBox.checked = true; 
  toggleDonationBox();
} // prepareDonation
  
