var Number_of_Members=0;

function totalcost()
{
    document.getElementById("Total_Cost").innerHTML=parseInt(document.getElementById("Report_Membership_Cost").innerHTML) + parseInt(document.getElementById("Total_Book_Cost").innerHTML);
	document.getElementById("Amount_Charged").value=document.getElementById("Total_Cost").innerHTML;
}

function countbooks(Counter)
{
	// Check all the books for sale.  Make sure the number requested is less than or equal to the number of members.
	var Number_of_Members=document.getElementById("Report_Memberships").innerHTML;

	Form_Value=document.getElementById("Books_Wanted" + Counter).value;
    // Replace anything but a digit with "0", then pick up the final value
    Form_Value=Form_Value.replace(new RegExp(/[^0-9]/g),"0");
    if (Form_Value=="") { Form_Value="0"; }

    document.getElementById("Books_Wanted" + Counter).value=Form_Value;
  
    if (parseInt(Form_Value)>parseInt(Number_of_Members)) 
      {
	// Replace the value with the Number_of_Members 
	    Form_Value=Number_of_Members;
	// Warn the user by setting a message in the span next to the value.
        document.getElementById("Books_Wanted" + Counter).value=Number_of_Members;
	    document.getElementById("Excess_Books").innerHTML="You can not pre-order more copies than members registering.";
	  }
	  else
	  {
   // Eliminate the warning message (assuming there is one).
	    document.getElementById("Excess_Books").innerHTML="&nbsp;";
	  }
// Total up all books (not just this one) and update the Book Cost Field.
// Then add to the member and update that field.

   var Total_Book_Cost=0;
   var Total_Books_Purchased=0;
   var This_Book=0;
   var Book_Info="";
   
   for (var book_counter=0; book_counter<Books_for_Sale.length; book_counter++)
     {
	    This_Book=parseInt(document.getElementById("Books_Wanted" + book_counter).value);
	    Total_Books_Purchased+=This_Book;
	    Total_Price=This_Book*Book_Prices[book_counter];
	    Total_Book_Cost+=Total_Price;
	    Book_Info=Book_Info + This_Book + "|" + Books_Short[book_counter] + "|";
     }	  
    document.getElementById("Books_Purchased").innerHTML=Total_Books_Purchased;
    document.getElementById("Total_Book_Cost").innerHTML=Total_Book_Cost;
    document.getElementById("Book_Info").value=Book_Info;
         
    totalcost();
	  
}

function countmembers()
{
	// Determine how many people are on the entry form.  Combine First, Middle, Last, and Badge.
	// Eliminate all not A-Z0-9 characters.  There must be at least 2 left.
    Number_of_Members=0;
	for (var Counter=1; Counter<6; Counter++)
	{
      Form_Value=document.getElementById("First_Name" + Counter).value + document.getElementById("Middle_Initial" + Counter).value + document.getElementById("Last_Name" + Counter).value + document.getElementById("Badge_Name" + Counter).value;
      Form_Value=Form_Value.replace(new RegExp(/[^a-zA-z0-9]/g),"");
      if (Form_Value.length>1) { Number_of_Members++; }
    }
    document.getElementById("Report_Memberships").innerHTML=Number_of_Members;
	document.getElementById("Number_of_Memberships").value=Number_of_Members;
    document.getElementById("Report_Membership_Cost").innerHTML=(Number_of_Members*Price_of_Membership);
      
    totalcost();
    
// Make sure the number of books doesn't exceed the new number of members
   for (var book_counter=0; book_counter<Books_for_Sale.length; book_counter++)
     {
          Form_Value=parseInt(document.getElementById("Books_Wanted" + book_counter).value);
	      Form_Value+=0;
          if (Form_Value>Number_of_Members) 
            {
              document.getElementById("Books_Wanted" + book_counter).value=Number_of_Members;
	        }
     }

   var Total_Book_Cost=0;
   var Total_Books_Purchased=0;
   var This_Book=0;
   var Book_Info="";
   
   for (var book_counter=0; book_counter<Books_for_Sale.length; book_counter++)
     {
	    This_Book=parseInt(document.getElementById("Books_Wanted" + book_counter).value);
	    This_Book+=0;
	    Total_Books_Purchased+=This_Book;
	    Total_Price=This_Book*Book_Prices[book_counter];
	    Total_Book_Cost+=Total_Price;
   	    Book_Info=Book_Info + This_Book + "|" + Books_Short[book_counter] + "|";
     }	  
    document.getElementById("Books_Purchased").innerHTML=Total_Books_Purchased;
    document.getElementById("Total_Book_Cost").innerHTML=Total_Book_Cost;
    document.getElementById("Book_Info").value=Book_Info;
 
    totalcost();
          
}

function Show_Book_Info(Book_Index)
{
    document.getElementById("Book_Info_Box").innerHTML=Book_Blurbs[Book_Index];
	document.getElementById("Book_Info_Box").style.display = "inline";
}

function Hide_Book_Info()
{
	  document.getElementById("Book_Info_Box").style.display = "none";
}


function hide_if_no_javascript()
{
  document.getElementById("hide_if_no_javascript").style.display = "inline";
}

function ShowHideCCFields(SH_Choice)
{
  if (SH_Choice == "show") { document.getElementById("CreditCardSection").style.display = "inline"; }
  else { document.getElementById("CreditCardSection").style.display = "none"; }	
}

function CheckSubmission()
{

// Check the fields that we can check.  It's very basic, but it's better than nothing.

// Check to make sure we have at least one member registering
    if (document.getElementById("Report_Memberships").innerHTML==0)
    {
	      alert("You need to provide at least one member's information");
	      document.CapclaveRegistrationForm.First_Name1.focus();
	      return false;
    }

// Make sure the basic Address Fields have something in them.
    if (document.CapclaveRegistrationForm.Address1.value=="")
      {
	      alert("You need to provide us with a mailing address");
	      document.CapclaveRegistrationForm.Address1.focus();
	      return false;
      }
    if (document.CapclaveRegistrationForm.City.value=="")
      {
	      alert("You need to provide us with your City");
	      document.CapclaveRegistrationForm.City.focus();
	      return false;
      }
    if (document.CapclaveRegistrationForm.State.value=="")
      {
	      alert("You need to provide us with your State/Province");
	      document.CapclaveRegistrationForm.State.focus();
	      return false;
      }
    if (document.CapclaveRegistrationForm.Postal_Code.value=="")
      {
	      alert("You need to provide us with your Zip/Postal Code");
	      document.CapclaveRegistrationForm.Postal_Code.focus();
	      return false;
      }

   
    var button_index;
// If "add to mailing list is selected, which it is by default.  Make sure we have an email address to send to.
    for (var index_counter=0; index_counter<document.CapclaveRegistrationForm.Email_List.length; index_counter++)
     {
	    if (document.CapclaveRegistrationForm.Email_List[index_counter].checked)
	      {
		    button_index=document.CapclaveRegistrationForm.Email_List[index_counter].value;
	      }
     }

    if (button_index=="Y")
      {
        if (document.CapclaveRegistrationForm.Email_Address.value=="")
          {
	        alert("You've asked to be added to our Announcements list.  Please provide an email address that we can use.");
	        document.CapclaveRegistrationForm.Email_Address.focus();
	        return false;
          }
        with (document.CapclaveRegistrationForm.Email_Address)
          {
            apos=value.indexOf("@");
            dotpos=value.lastIndexOf(".");
            if (apos<1||dotpos-apos<2)
              {
                alert("Your email address is not valid.");
                document.CapclaveRegistrationForm.Email_Address.focus();
                return false;
              }  
          }
      }

var payment_selected=false;
var payment_type;
// On-line registration must be done via paypal or manual credit card.  They must choose one or the other.
    for (var index_counter=0; index_counter<document.CapclaveRegistrationForm.PaymentMethod.length; index_counter++)
     {
	    if (document.CapclaveRegistrationForm.PaymentMethod[index_counter].checked)
	      {
		    payment_selected=true;
		    payment_type=document.CapclaveRegistrationForm.PaymentMethod[index_counter].value;
	      }
     }
//      
    if (payment_selected==false)
    {
      alert("Please choose a payment method.  Either Paypal or Manual Credit Card");
      return false;
    }    
    
          
// Vet the Credit Card information if Manual Credit Card was selected
if (payment_type=="Manual")
{
	
// Credit Card Type

var card_selected=false;
var card_type;
// If they've indicated they want to use a manual credit card, they have to tell us what kind it is.
    for (var index_counter=0; index_counter<document.CapclaveRegistrationForm.Card_Type.length; index_counter++)
     {
	    if (document.CapclaveRegistrationForm.Card_Type[index_counter].checked)
	      {
		    card_selected=true;
		    card_type=document.CapclaveRegistrationForm.Card_Type[index_counter].value;
	      }
     }
      
    if (card_selected==false)
    {
      alert("Please indicate what kind of credit card you will be using");
      return false;
    }      

// Credit Card Number:
    if (document.CapclaveRegistrationForm.Card_Number.value=="")
      {
	      alert("You need to provide us with Credit Card Number");
	      document.CapclaveRegistrationForm.Card_Number.focus();
	      return false;
      }
      
// Make sure the Credit Card number is 16 characters in length.

   var workingvariable=document.CapclaveRegistrationForm.Card_Number.value;
   workingvariable=workingvariable.replace(new RegExp(/[^0-9]/g),"");
    if (workingvariable.length!=16)
      {
	      alert("There appears to be a problem with your Credit Card Number");
	      document.CapclaveRegistrationForm.Card_Number.focus();
	      return false;
      }
      
// Expiration Date (Day, Month, Year)      

    if (document.CapclaveRegistrationForm.Expiration_Day.value=="Day")
      {
	      alert("You need to provide us with Day of Expiration");
	      document.CapclaveRegistrationForm.Expiration_Day.focus();
	      return false;
      }
    if (document.CapclaveRegistrationForm.Expiration_Month.value=="Month")
      {
	      alert("You need to provide us with Month of Expiration");
	      document.CapclaveRegistrationForm.Expiration_Month.focus();
	      return false;
      }
    if (document.CapclaveRegistrationForm.Expiration_Year.value=="Year")
      {
	      alert("You need to provide us with Year of Expiration");
	      document.CapclaveRegistrationForm.Expiration_Year.focus();
	      return false;
      }
      
// Name on Card

    if (document.CapclaveRegistrationForm.Name_On_Card.value=="")
      {
	      alert("You need to provide us with Name to which the card has been issued");
	      document.CapclaveRegistrationForm.Name_On_Card.focus();
	      return false;
      }
} // If manual creditcard  
                 
	return true;
}
// Testing Code
//		    document.CapclaveRegistrationForm.Address2.value=card_type;
