// The membership levels (this order matches the order on the form):
var membershiplevels = [ 45.00, 65.00, 100.00, 250.00 ];

// The shipping costs (this order matches the order on the form):
var shipping = [ 0, 5.00, 15.00 ];

// E-mail validation variables
var email = document.getElementById('email');
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	
// Show a DHTML layer
function showLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='block';
  }
}

// Hide a DHTML layer
function hideLayer(id) {
  if (element = document.getElementById(id)) {
    element.style.display='none';
  }
}

// Set or clear mailing address
function setMailing(which) {
  if ( which.checked == true ) {
    which.form.personalmailingaddress1.value=which.form.baddr1.value;
    which.form.personalmailingaddress2.value=which.form.baddr2.value;
    which.form.personalmailingcity.value=which.form.bcity.value;
    which.form.personalmailingstate.selectedIndex=which.form.bstate.selectedIndex;
    which.form.personalmailingprovince.value=which.form.bstate2.value;
    which.form.personalmailingcountry.selectedIndex=which.form.bcountry.selectedIndex;
    which.form.personalmailingzip.value=which.form.bzip.value;
  }

  else {
    which.form.personalmailingaddress1.value='';
    which.form.personalmailingaddress2.value='';
    which.form.personalmailingcity.value='';
    which.form.personalmailingstate.selectedIndex=0;
    which.form.personalmailingprovince.value='';
    which.form.personalmailingcountry.selectedIndex=0;
    which.form.personalmailingzip.value='';
  }
}

// Check to form for valid values and submit
function checkSubmit(theform) {
  // if donation is selected
  if ( theform.type.value == "donate" ) {
    if ( theform.donationamount.options[theform.donationamount.selectedIndex].value == "" ) {
      alert("Please select an amount for your donation!");
      theform.donationamount.focus();
      return;
    }

    else if ( theform.donationamount.options[theform.donationamount.selectedIndex].value == "other" && ( theform.donationother.value == "" || isNaN(theform.donationother.value) ) ) {
      alert("Please enter a number for your 'other' donation!");
      theform.donationother.focus();
      return;
    }
  }


  // if membership is selected
  else {
    if ( theform.membershipgift.checked == true ) {
      if (( theform.membershipgiftfirstname.value == "" ) || ( theform.membershipgiftlastname.value == "" )){
        alert("Please enter a first and last name for your membership gift!");
	theform.membershipgiftname.focus();
	return;
      }

      if ( theform.membershipgiftaddress.value == "" ) {
        alert("Please enter a street address for your membership gift!");
        theform.membershipgiftaddress.focus();
        return;
      }

      if ( theform.membershipgiftcity.value == "" ) {
        alert("Please enter a city for your membership gift!");
        theform.membershipgiftcity.focus();
        return;
      }

      if ( theform.membershipgiftstate.options[theform.membershipgiftstate.selectedIndex].value == "" ) {
        alert("Please enter a state for your membership gift!");
	theform.membershipgiftstate.focus();
        return;
      }

      if ( theform.membershipgiftzip.value == "" ) {
        alert("Please enter a ZIP code for your membership gift!");
	theform.membershipgiftzip.focus();
        return;
      }

      if ( theform.membershiptype[3].checked == true && ( theform.membershiptypeother.value == "" || isNaN(theform.membershiptypeother.value) ) ) {
        alert("Please enter a value for your 'other' membership!");
        theform.membershiptypeother.focus();
        return;
      }
    }
  }

  // Check the personal information
  if ( theform.personalfirstname.value == "" ) {
    alert("Please enter a first name under 'Personal Information'!");
    theform.personalfirstname.focus();
    return;
  }

  if ( theform.personallastname.value == "" ) {
    alert("Please enter a last name under 'Personal Information'!");
    theform.personallastname.focus();
    return;
  }

  if ( theform.baddr1.value == "" && theform.baddr2.value == "" ) {
    alert("Please enter a street for your billing address!");
    theform.baddr1.focus();
    return false;
  }

  if ( theform.baddr1.value == "" ) {
    alert("The first line of the business street address can not be blank!");
    thefomr.baddr.focus();
    return false;
  }

  if ( theform.bcity.value == "" ) {
    alert("Please enter a city for your billing address!");
    theform.bcity.focus();
    return false;
  }

  if ( theform.bstate.options[theform.bstate.selectedIndex].value == "" && theform.bstate2.value == "" ) {
    alert("Please enter a state or province for your billing address!");
    theform.bstate.focus();
    return false;
  }

  if ( theform.bzip.value == "" ) {
    alert("Please enter a ZIP code for your billing address!");
    theform.bzip.focus();
    return;
  }

  if ( theform.bcountry.options[theform.bcountry.selectedIndex].value == "" )  {
    alert("Please select a country for your billing address!");
    theform.bcountry.focus();
    return;
  }

//  if ( theform.email.value == "" ) {
//    alert("Please enter an email address where a confirmation of this transaction will be sent.");
//    theform.email.focus();
//    return;
//  }
	if (!filter.test(theform.email.value)) {
	  alert('Please provide a valid email address.');
	  email.focus
	  return false;
  }
  
  if ( theform.personalmailingaddress1.value == "" && theform.personalmailingaddress2.value == "" ) {
    alert("Please enter a street for your mailing address!");
    theform.personalmailingaddress1.focus();
    return false;
  }

  if ( theform.personalmailingcity.value == "" ) {
    alert("Please enter a city for your mailing address!");
    theform.personalmailingcity.focus();
    return false;
  }

  if ( theform.personalmailingstate.options[theform.personalmailingstate.selectedIndex].value == "" && theform.personalmailingprovince.value == "" ) {
    alert("Please enter a state or province for your mailing address!");
    theform.personalmailingstate.focus();
    return false;
  }

  if ( theform.personalmailingzip.value == "" ) {
    alert("Please enter a ZIP code for your mailing address!");
    theform.personalmailingzip.focus();
    return;
  }

  if ( theform.personalmailingcountry.options[theform.personalmailingcountry.selectedIndex].value == "" ) {
    alert("Please select a country for your mailing address!");
    theform.personalmailingcountry.focus();
    return;
  }

  // Payment Information checks
  if ( theform.cardnumber.value == "" ) {
    alert("Please enter a credit card number for your transaction!");
    theform.cardnumber.focus();
    return;
  }

  if ( theform.bname.value == "" ) {
    alert("Please enter the name of the account holder for this credit card!");
    theform.cardnumber.focus();
    return;
  }

  if ( theform.expmonth.options[theform.expmonth.selectedIndex].value == "" ) {
    alert("Please select an expiration month for your credit card!");
    theform.expmonth.focus();
    return;
  }

  if ( theform.expyear.options[theform.expyear.selectedIndex].value == "" ) {
    alert("Please select an expiration year for your credit card!");
    theform.expyear.focus();
    return;
  }

  if ( theform.cvm.value == "" && theform.cvmnotpres.checked == false ) {
    alert("Please enter a verification code (if your card does not have a verification code please check the 'not present' checkbox)!");
    theform.cvm.focus();
    return;
  }

  // All values are good, calculate the total charge:
  // Donation:
  if ( theform.type.value == "donate" ) {
    if ( theform.donationamount.options[theform.donationamount.selectedIndex].value != 'other' ) {
      theform.chargetotal.value=theform.donationamount.options[theform.donationamount.selectedIndex].value; 
    }
   
    else {
      theform.chargetotal.value=theform.donationother.value;
    } 
  }

  // Membership:
  else {
    if ( theform.membershiptype[0].checked == true ) {
      theform.chargetotal.value = membershiplevels[0];
    }

    if ( theform.membershiptype[1].checked == true ) {
      theform.chargetotal.value = membershiplevels[1];
    }

    if ( theform.membershiptype[2].checked == true ) {
      theform.chargetotal.value = membershiplevels[2];
    }

    if ( theform.membershiptype[3].checked == true ) {
      theform.chargetotal.value = membershiplevels[3];
    }

    if ( theform.membershiptype[4].checked == true ) {
      theform.chargetotal.value = theform.membershiptypeother.value;
    }

    // Shipping:
    if ( theform.membershipshipping[1].checked == true ) {
      theform.chargetotal.value = parseFloat(theform.chargetotal.value) + shipping[1];
    }

    if ( theform.membershipshipping[2].checked == true ) {
      theform.chargetotal.value = parseFloat(theform.chargetotal.value) + shipping[2];
    }
  }

  // Get a confirmation from the user that it is ok to charge the total to this credit card
  // if ( theform.type.value == "donate" && theform.submode[1].checked == true )
  if ( theform.type.value == "donate" && theform.submode.value != "" ) {
    periodicity = theform.periodicity.options[theform.periodicity.selectedIndex].value == 'm1' ? ' months' : ' years';
    msg = "Do you want to proceed with charging $" + theform.chargetotal.value + " to your " + theform.cctype.options[theform.cctype.selectedIndex].text + " card every " + theform.installments.value + periodicity + " starting on " + theform.recurringmonth.options[theform.recurringmonth.selectedIndex].value+'/'+theform.recurringday.options[theform.recurringday.options.selectedIndex].value+'/'+theform.recurringyear.options[theform.recurringyear.selectedIndex].value+'?';
  }

  else {
    msg = "Do you want to proceed with charging $" + theform.chargetotal.value + " to your " + theform.cctype.options[theform.cctype.selectedIndex].text + " card?";
  }

  if ( window.confirm(msg) ) {
    //alert("Card processing is not yet implemented!");
    theform.submit();
  }
}