<!--
// Routines for CCMA online membership

// ====================================
// Define some global variables
// ====================================
var valAdultPrice = 90.00;
var valSeniorPrice = 75.00;
var valStudentPrice = 55.00;
var valFamilyPrice = 215.00;

var PriceFmt = new Array ("F", 7, 2);	// Floating, total width: 7, decimals: 2

var txtActPreferSeatPrice;		// Actual prefered seat price - it's free for higher patron levels
var valActPreferSeatPrice;
var valStdPreferSeatPrice = 35.00;
var txtStdPreferSeatPrice = "$35";
var txtFreePreferSeatPrice = "no charge";

var PatronTotal;	// Total Patronage amount
var PreferSeatTotal;	// Total prefered seating amount

// Indices into patron arrays
var PatronContributingNdx = 0;
var PatronSustainingNdx = 1;
var PatronSupportingNdx = 2;
var PatronBenefactorNdx = 3;
var PatronConservatorNdx = 4;
var PatronDirectorNdx = 5;

// PatronParms array
// PatronParms[PatronLevelNdx,0] = Name of patron level
// PatronParms[PatronLevelNdx,1] = Minimum contribution for this patron level
// PatronParms[PatronLevelNdx,2] = Maximum contribution for this patron level
// PatronParms[PatronLevelNdx,3] = Flag indicates if preferred seating included for this level

var PatronNameNdx = 0;
var PatronMinAmtNdx = 1;
var PatronMaxAmtNdx = 2;
var PatronPreferSeatNdx = 3;

var PatronParms = new Array;
PatronParms[PatronContributingNdx] = ["Contributing", 50, 99, false];
PatronParms[PatronSustainingNdx] = ["Sustaining", 100, 149, false];
PatronParms[PatronSupportingNdx] = ["Supporting", 150, 299, false];
PatronParms[PatronBenefactorNdx] = ["Benefactor", 300, 499, false];
PatronParms[PatronConservatorNdx] = ["Conservator", 500, 999, true];
PatronParms[PatronDirectorNdx] = ["Director", 1000, 3000, true];

var PatronActAmt = new Array;	// actual patron contributions
clearPatronActAmt();

// ====================================
// Initialization
// ====================================
function StartUp()
{
  document.frmMembership.txtAdultCt.onchange = updtMemberTicketFld;
  document.frmMembership.txtAdultCt.DfltVal = "";	// Default value
  document.frmMembership.txtAdultCt.FmtIn = new Array ("I", 1, 0);	// Integer, total width: 1, decimals: 0

  document.frmMembership.txtSeniorCt.onchange = updtMemberTicketFld;
  document.frmMembership.txtSeniorCt.DfltVal = "";	// Default value
  document.frmMembership.txtSeniorCt.FmtIn = new Array ("I", 1, 0);	// Integer, total width: 1, decimals: 0
  
  document.frmMembership.txtStudentCt.onchange = updtMemberTicketFld;
  document.frmMembership.txtStudentCt.DfltVal = "";	// Default value
  document.frmMembership.txtStudentCt.FmtIn = new Array ("I", 1, 0);	// Integer, total width: 1, decimals: 0
  
  document.frmMembership.chkFamily.onchange = updtFamilyFields;
  document.frmMembership.chkFamily.onclick = updtFamilyFields;
  
  document.frmMembership.txtFamilyAdults.onchange = updtMemberTicketFld;
  document.frmMembership.txtFamilyAdults.DfltVal = "";	// Default value
  document.frmMembership.txtFamilyAdults.FmtIn = new Array ("I", 1, 0);	// Integer, total width: 1, decimals: 0
  
  document.frmMembership.txtFamilyChildren.onchange = updtMemberTicketFld;
  document.frmMembership.txtFamilyChildren.DfltVal = "";	// Default value
  document.frmMembership.txtFamilyChildren.FmtIn = new Array ("I", 1, 0);	// Integer, total width: 1, decimals: 0
  
  document.frmMembership.chkContributing.onchange = chkContributingOnClick;
  document.frmMembership.chkContributing.onclick = chkContributingOnClick;
  document.frmMembership.txtContributingActAmt.onchange = updtContributingActAmt;
  InitContributingLine();
  
  document.frmMembership.chkSustaining.onchange = chkSustainingOnClick;
  document.frmMembership.chkSustaining.onclick = chkSustainingOnClick;
  document.frmMembership.txtSustainingActAmt.onchange = updtSustainingActAmt;
  InitSustainingLine();
  
  document.frmMembership.chkSupporting.onchange = chkSupportingOnClick;
  document.frmMembership.chkSupporting.onclick = chkSupportingOnClick;
  document.frmMembership.txtSupportingActAmt.onchange = updtSupportingActAmt;
  InitSupportingLine();
  
  document.frmMembership.chkBenefactor.onchange = chkBenefactorOnClick;
  document.frmMembership.chkBenefactor.onclick = chkBenefactorOnClick;
  document.frmMembership.txtBenefactorActAmt.onchange = updtBenefactorActAmt;
  InitBenefactorLine();
  
  document.frmMembership.chkConservator.onchange = chkConservatorOnClick;
  document.frmMembership.chkConservator.onclick = chkConservatorOnClick;
  document.frmMembership.txtConservatorActAmt.onchange = updtConservatorActAmt;
  InitConservatorLine();
  
  document.frmMembership.chkDirector.onchange = chkDirectorOnClick;
  document.frmMembership.chkDirector.onclick = chkDirectorOnClick;
  document.frmMembership.txtDirectorActAmt.onchange = updtDirectorActAmt;
  InitDirectorLine();
  
//  document.frmMembership.btnSubmit.onclick = SendToPayPal;	// Default value
  
  InitAdultLine();
  InitSeniorLine();
  InitStudentLine();
  InitFamilyLine();
  InitMembershipSubtotalLine();
  InitPatronFields();
  
  RefreshAllCalcedFields();
}

// ====================================
// Common routines
// ====================================

// Data entry for number of members field
function updtMemberTicketFld()
{
  if (this.value > 9)
  {
    this.value = this.DfltVal;
  }

  if (IsNotNegative(this.value))
  {
    this.value = FormatStringOut(this.value, this.FmtIn);
  }
  else
  {
    this.value = this.DfltVal;
  }

  RefreshAllCalcedFields();
  
  if (document.frmMembership.chkPreferSeat.checked)  // Is preferred seating checked?
  {
    updtPreferSeatCt();
    calcPreferSeatTotal();
    RefreshAllCalcedFields();
  }
}

function updtFamilyFields()
{
  updtPreferSeatCt();
  calcPreferSeatTotal();
  RefreshAllCalcedFields();
}

function FormatStringOut(ValueIn, Format)
{
    var a = new ToFmt(ValueIn);
	switch (Format[0])
	{
      case "I":		// Integer
        return a.fmtI(Format[1]);
		break;

      case "F":		// Floating
        return a.fmtF(Format[1], Format[2]);
		break;

     default:
        alert ("Bad Format: |" + Format + "| in FormatStringOut.");
		return "#";
		break;
	}
}

//  check for valid non-negative numeric strings	
function IsNotNegative(strString)
{
  if (!IsNumeric(strString))
  {
    return false;
  }
  else if (strString >= 0)
  {
    return true;
  }
  else
  {
    return false;
  }
}

//  check for valid numeric strings	
function IsNumeric(strString)
{
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
      blnResult = false;
    }
  }
  return blnResult;
}

// ====================================
// Process Adult membership line
// ====================================

function InitAdultLine()
{
    document.frmMembership.txtAdultCt.value = document.frmMembership.txtAdultCt.DfltVal;
    document.getElementsByTagName('span')['txtAdultPrice'].innerHTML = valAdultPrice + "";
    document.getElementsByTagName('span')['txtAdultTotal'].innerHTML = FormatStringOut(0, PriceFmt);
}

// ====================================
// Process Senior membership line
// ====================================

function InitSeniorLine()
{
  document.frmMembership.txtSeniorCt.value = document.frmMembership.txtSeniorCt.DfltVal;
  document.getElementsByTagName('span')['txtSeniorPrice'].innerHTML = valSeniorPrice + "";
  document.getElementsByTagName('span')['txtSeniorTotal'].innerHTML = FormatStringOut(0, PriceFmt);
}

// ====================================
// Process Student membership line
// ====================================

function InitStudentLine()
{
  document.frmMembership.txtStudentCt.value = document.frmMembership.txtStudentCt.DfltVal;
  document.getElementsByTagName('span')['txtStudentPrice'].innerHTML = valStudentPrice + "";
  document.getElementsByTagName('span')['txtStudentTotal'].innerHTML = FormatStringOut(0, PriceFmt);
}

// ====================================
// Process Family membership line
// ====================================

function InitFamilyLine()
{
  document.getElementsByTagName('span')['txtStudentTotal'].innerHTML = "0";

  document.frmMembership.chkFamily.checked = false;
  document.getElementsByTagName('span')['txtFamilyPrice'].innerHTML = valFamilyPrice + "";
  document.getElementsByTagName('span')['txtFamilyTotal'].innerHTML = "0";
  document.frmMembership.txtFamilyAdults.value = document.frmMembership.txtFamilyAdults.DfltVal;
  document.frmMembership.txtFamilyAdults.disabled = true;
  document.frmMembership.txtFamilyChildren.value = document.frmMembership.txtFamilyChildren.DfltVal;
  document.frmMembership.txtFamilyChildren.disabled = true;
  document.getElementsByTagName('span')['txtFamilyCounts'].style.display='none';
}


// ====================================
// Init Membership subtotals line
// ====================================
function InitMembershipSubtotalLine()
{
  document.getElementsByTagName('span')['txtTicketTotal'].innerHTML = "0";
  document.getElementsByTagName('span')['txtMembershipSubtotal'].innerHTML = FormatStringOut(0, PriceFmt);
}

// ====================================
// Refresh all calculated fields on screen
// ====================================

function RefreshAllCalcedFields()
{
  document.getElementsByTagName('span')['txtAdultTotal'].innerHTML = FormatStringOut(MyParseInt(document.frmMembership.txtAdultCt.value) * valAdultPrice, PriceFmt);
  document.getElementsByTagName('span')['txtSeniorTotal'].innerHTML = FormatStringOut(document.frmMembership.txtSeniorCt.value * valSeniorPrice, PriceFmt);
  document.getElementsByTagName('span')['txtStudentTotal'].innerHTML = FormatStringOut(document.frmMembership.txtStudentCt.value * valStudentPrice, PriceFmt);
  
  if (document.frmMembership.txtFamilyAdults.value > 2)			// Adults in 1 family cannot be greater than 2
  {
    document.frmMembership.txtFamilyAdults.value = document.frmMembership.txtFamilyAdults.DfltVal;
  }
  
  if (document.frmMembership.chkFamily.checked)
  {
    document.frmMembership.txtFamilyAdults.disabled = false;
    document.frmMembership.txtFamilyChildren.disabled = false;
    document.getElementsByTagName('span')['txtFamilyCounts'].style.display='inline';
    document.getElementsByTagName('span')['txtFamilyTotal'].innerHTML = FormatStringOut(valFamilyPrice, PriceFmt);
  }
  else
  {
    document.frmMembership.txtFamilyAdults.value = document.frmMembership.txtFamilyAdults.DfltVal;
    document.frmMembership.txtFamilyAdults.disabled = true;
    document.frmMembership.txtFamilyChildren.value = document.frmMembership.txtFamilyChildren.DfltVal;
    document.frmMembership.txtFamilyChildren.disabled = true;
    document.getElementsByTagName('span')['txtFamilyCounts'].style.display='none';
    document.getElementsByTagName('span')['txtFamilyTotal'].innerHTML = FormatStringOut(0, PriceFmt);
  }
  
  RefreshMembershipSubtotals();
  calcPreferSeatTotal()

  PatronTotal = 0;
  PreferSeatTotal = 0;
  for (var PatronNdx=0; PatronNdx < PatronActAmt.length; PatronNdx++)
  {
    PatronTotal += PatronActAmt[PatronNdx];
  }
  
  if (isNaN(document.getElementsByTagName('span')['txtPreferSeatTotal'].innerHTML))
  {
    PreferSeatTotal = 0;
  }
  else
  {
    PreferSeatTotal = parseFloat(document.getElementsByTagName('span')['txtPreferSeatTotal'].innerHTML);
  }

  var GrandTotal = parseFloat(document.getElementsByTagName('span')['txtMembershipSubtotal'].innerHTML);
  GrandTotal += PatronTotal;
  GrandTotal += PreferSeatTotal;
  document.getElementsByTagName('span')['txtGrandTotal'].innerHTML = FormatStringOut(GrandTotal, PriceFmt);
}

// ====================================
// Refresh Membership subtotals line
// ====================================
function RefreshMembershipSubtotals()
{
  var AdultTixCt = MyParseInt(document.frmMembership.txtAdultCt.value);
  var AdultTixAmt = parseFloat(document.getElementsByTagName('span')['txtAdultTotal'].innerHTML);
  
  var SeniorTixCt = MyParseInt(document.frmMembership.txtSeniorCt.value);
  var SeniorTixAmt = parseFloat(document.getElementsByTagName('span')['txtSeniorTotal'].innerHTML);

  var StudentTixCt = MyParseInt(document.frmMembership.txtStudentCt.value);
  var StudentTixAmt = parseFloat(document.getElementsByTagName('span')['txtStudentTotal'].innerHTML);

  var FamilyTixCt = MyParseInt(document.frmMembership.txtFamilyAdults.value) + MyParseInt(document.frmMembership.txtFamilyChildren.value);
  var FamilyTixAmt = parseFloat(document.getElementsByTagName('span')['txtFamilyTotal'].innerHTML);
  
  document.getElementsByTagName('span')['txtTicketTotal'].innerHTML = AdultTixCt + SeniorTixCt + StudentTixCt + FamilyTixCt;
  document.getElementsByTagName('span')['txtMembershipSubtotal'].innerHTML = FormatStringOut(AdultTixAmt + SeniorTixAmt + StudentTixAmt + FamilyTixAmt, PriceFmt);
}

// ====================================
// Common Patron routines
// ====================================

function InitPatronFields()
{
  ClearAllPatronLines();
  InitMatchingGiftsLine();
  InitPreferSeatLine();
  WritePatronAmtMsg('txtContributingRange', PatronContributingNdx);
  WritePatronAmtMsg('txtSustainingRange', PatronSustainingNdx);
  WritePatronAmtMsg('txtSupportingRange', PatronSupportingNdx);
  WritePatronAmtMsg('txtBenefactorRange', PatronBenefactorNdx);
  WritePatronAmtMsg('txtConservatorRange', PatronConservatorNdx);
  WritePatronAmtMsg('txtDirectorRange', PatronDirectorNdx);
}

function InitPatronLine(ChkBox, ActAmtFld)
{
  ChkBox.checked = false;
  ActAmtFld.value = " ";	
  ActAmtFld.disabled = true;	
}

function WritePatronAmtMsg(txtFld, PatronNdx)
{
  var strAmtMsg = "$" + PatronParms[PatronNdx][PatronMinAmtNdx];
  strAmtMsg += " - $" + PatronParms[PatronNdx][PatronMaxAmtNdx];
  document.getElementsByTagName('span')[txtFld].innerHTML = strAmtMsg;
}

function ClearAllPatronLines()
{
  InitContributingLine()
  InitSustainingLine()
  InitSupportingLine()
  InitBenefactorLine()
  InitConservatorLine()
  InitDirectorLine()
}

function updtPatronActAmt(txtFld, PatronNdx)
{
  clearPatronActAmt();
  if (isNaN(parseFloat(txtFld.value)))
  {
    PatronActAmt[PatronNdx] = 0;
  }
  else
  {
    PatronActAmt[PatronNdx] = parseFloat(txtFld.value);
	var PatronAmtOutOfRange = false;
	if (PatronActAmt[PatronNdx] < PatronParms[PatronNdx][PatronMinAmtNdx])
      PatronAmtOutOfRange = true;
//	  txtFld.value = FormatStringOut(PatronParms[PatronNdx][PatronMinAmtNdx], PriceFmt);
	else if (PatronActAmt[PatronNdx] > PatronParms[PatronNdx][PatronMaxAmtNdx])
      PatronAmtOutOfRange = true;
//	  txtFld.value = FormatStringOut(PatronParms[PatronNdx][PatronMaxAmtNdx], PriceFmt);
	else
	  txtFld.value = FormatStringOut(txtFld.value, PriceFmt);
  }
  
  if (PatronAmtOutOfRange)
  {
    var PatronAmtOutOfRangeMsg = "Valid amounts for the " + PatronParms[PatronNdx][PatronNameNdx] + " level are from $" +
                                 PatronParms[PatronNdx][PatronMinAmtNdx] + " to $" +
                                 PatronParms[PatronNdx][PatronMaxAmtNdx] + "." +
								 "\nPlease enter a valid amount or select a different level.\nThank you.";
    alert (PatronAmtOutOfRangeMsg);
    txtFld.value = "";
  }
  
  RefreshAllCalcedFields()
}

function clearPatronActAmt()
{
  for (var PatronNdx=0; PatronNdx < PatronParms.length; PatronNdx++)
  {
    PatronActAmt[PatronNdx] = 0;
  }
}
// ====================================
// Matching Gifts checkbox routines
// ====================================

function InitMatchingGiftsLine()
{
  CloseMatchingGiftsLine()
}

function OpenMatchingGiftsLine()
{
  document.frmMembership.chkMatchingGifts.disabled = false;
  document.frmMembership.chkMatchingGifts.style.display = 'inline';
  document.getElementsByTagName('span')['txtMatchingGifts'].style.display='inline';
}

function CloseMatchingGiftsLine()
{
  document.frmMembership.chkMatchingGifts.style.display = 'none';
  document.getElementsByTagName('span')['txtMatchingGifts'].style.display='none';
  document.frmMembership.chkMatchingGifts.disabled = true;
  document.frmMembership.chkMatchingGifts.checked = false;
}

// ====================================
// Prefered Seating routines
// ====================================

function InitPreferSeatLine()
{
  ClosePreferSeatLine()
  document.frmMembership.chkPreferSeat.onclick = chkPreferSeatOnClick;
  document.frmMembership.chkPreferSeat.onchange = chkPreferSeatOnClick;
  document.frmMembership.txtPreferSeatCt.onchange = TestPreferSeatCt;
  document.frmMembership.chkAnonymous.checked = false;
  document.frmMembership.txtProgramNameValue.value = "";
}

function OpenPreferSeatLine(PatronNdx)
{
  if (PatronParms[PatronNdx][PatronPreferSeatNdx])	// Prefered seating included for this level?
  {
    txtActPreferSeatPrice = " @ " + txtFreePreferSeatPrice;
	valActPreferSeatPrice = 0;
    document.frmMembership.txtPreferSeatCt.value = document.getElementsByTagName('span')['txtTicketTotal'].innerHTML
    document.frmMembership.chkPreferSeat.checked = true;
    document.frmMembership.chkPreferSeat.disabled = true;
    document.frmMembership.txtPreferSeatCt.disabled = true;
  }
  else
  {
    txtActPreferSeatPrice = " @ " + txtStdPreferSeatPrice;
	valActPreferSeatPrice = valStdPreferSeatPrice;
    document.frmMembership.chkPreferSeat.disabled = false;
    if (document.frmMembership.chkPreferSeat.checked)
	{
      document.frmMembership.txtPreferSeatCt.disabled = false;
	}
	else
	{
      document.frmMembership.txtPreferSeatCt.disabled = true;
	}
  }
  document.frmMembership.chkPreferSeat.style.display = 'inline';
  document.getElementsByTagName('span')['txtPreferSeat'].style.display='inline';
  document.frmMembership.txtPreferSeatCt.style.display = 'inline';
  document.getElementsByTagName('span')['txtPreferSeatPrice'].innerHTML=txtActPreferSeatPrice;
  document.getElementsByTagName('span')['txtPreferSeatPrice'].style.display='inline';
  document.getElementsByTagName('span')['txtPreferSeatTotal'].style.display='inline';
  calcPreferSeatTotal();

// Patron name program listing is handled here too
  document.getElementsByTagName('span')['txtProgramNameLabel'].style.display='inline';
  document.frmMembership.txtProgramNameValue.style.display = 'inline';
  document.frmMembership.chkAnonymous.style.display = 'inline';
  document.getElementsByTagName('span')['txtAnonymousLabel'].style.display='inline';
}

function ClosePreferSeatLine()
{
  document.frmMembership.chkPreferSeat.style.display = 'none';
  document.getElementsByTagName('span')['txtPreferSeat'].style.display='none';
  document.frmMembership.txtPreferSeatCt.style.display = 'none';
  document.getElementsByTagName('span')['txtPreferSeatPrice'].style.display='none';
  document.getElementsByTagName('span')['txtPreferSeatTotal'].style.display='none';
  document.frmMembership.chkPreferSeat.disabled = true;
  document.frmMembership.chkPreferSeat.checked = false;
  document.frmMembership.txtPreferSeatCt.value = 0;

// Patron name program listing is handled here too
  document.frmMembership.chkAnonymous.checked = false;
  document.frmMembership.txtProgramNameValue.value = "";
  document.getElementsByTagName('span')['txtProgramNameLabel'].style.display='none';
  document.frmMembership.txtProgramNameValue.style.display = 'none';
  document.frmMembership.chkAnonymous.style.display = 'none';
  document.getElementsByTagName('span')['txtAnonymousLabel'].style.display='none';
}

function chkPreferSeatOnClick()
{
  if (document.frmMembership.chkPreferSeat.checked)
  // Open the line
  {
    document.frmMembership.txtPreferSeatCt.disabled = false;	
    updtPreferSeatCt();
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtPreferSeatCt.disabled = true;	
    document.frmMembership.txtPreferSeatCt.value = 0;
  }
  calcPreferSeatTotal();
  RefreshAllCalcedFields();
}

function updtPreferSeatCt()
{
  document.frmMembership.txtPreferSeatCt.value = document.getElementsByTagName('span')['txtTicketTotal'].innerHTML;
}

function calcPreferSeatTotal()
{
  document.getElementsByTagName('span')['txtPreferSeatTotal'].innerHTML=FormatStringOut(document.frmMembership.txtPreferSeatCt.value * valActPreferSeatPrice, PriceFmt);
}

function TestPreferSeatCt()
{
  if (document.frmMembership.txtPreferSeatCt.value > document.getElementsByTagName('span')['txtTicketTotal'].innerHTML)
  {
    document.frmMembership.txtPreferSeatCt.value = document.getElementsByTagName('span')['txtTicketTotal'].innerHTML;
  }
  RefreshAllCalcedFields()
}

// ====================================
// Contributing Patron
// ====================================

function InitContributingLine()
{
  InitPatronLine(document.frmMembership.chkContributing, document.frmMembership.txtContributingActAmt);
}

function chkContributingOnClick()
{
  if (document.frmMembership.chkContributing.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkContributing.checked = true;	// reset this one
    document.frmMembership.txtContributingActAmt.disabled = false;	
//    document.frmMembership.txtContributingActAmt.value = FormatStringOut(PatronParms[PatronContributingNdx][PatronMinAmtNdx], PriceFmt);
    OpenPreferSeatLine(PatronContributingNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtContributingActAmt.value = " ";	
    document.frmMembership.txtContributingActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtContributingActAmt();
}

function updtContributingActAmt()
{
  updtPatronActAmt(document.frmMembership.txtContributingActAmt, PatronContributingNdx);
}

// ====================================
// Sustaining Patron
// ====================================

function InitSustainingLine()
{
  InitPatronLine(document.frmMembership.chkSustaining, document.frmMembership.txtSustainingActAmt);
}

function chkSustainingOnClick()
{
  if (document.frmMembership.chkSustaining.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkSustaining.checked = true;	// reset this one
    document.frmMembership.txtSustainingActAmt.disabled = false;	
//    document.frmMembership.txtSustainingActAmt.value = FormatStringOut(PatronParms[PatronSustainingNdx][PatronMinAmtNdx], PriceFmt);
	OpenPreferSeatLine(PatronSustainingNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtSustainingActAmt.value = " ";	
    document.frmMembership.txtSustainingActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtSustainingActAmt();
}

function updtSustainingActAmt()
{
  updtPatronActAmt(document.frmMembership.txtSustainingActAmt, PatronSustainingNdx);
}

// ====================================
// Supporting Patron
// ====================================

function InitSupportingLine()
{
  InitPatronLine(document.frmMembership.chkSupporting, document.frmMembership.txtSupportingActAmt);
}

function chkSupportingOnClick()
{
  if (document.frmMembership.chkSupporting.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkSupporting.checked = true;	// reset this one
    document.frmMembership.txtSupportingActAmt.disabled = false;	
//    document.frmMembership.txtSupportingActAmt.value = FormatStringOut(PatronParms[PatronSupportingNdx][PatronMinAmtNdx], PriceFmt);
	OpenPreferSeatLine(PatronSupportingNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtSupportingActAmt.value = " ";	
    document.frmMembership.txtSupportingActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtSupportingActAmt();
}

function updtSupportingActAmt()
{
  updtPatronActAmt(document.frmMembership.txtSupportingActAmt, PatronSupportingNdx);
}

// ====================================
// Benefactor Patron
// ====================================

function InitBenefactorLine()
{
  InitPatronLine(document.frmMembership.chkBenefactor, document.frmMembership.txtBenefactorActAmt);
}

function chkBenefactorOnClick()
{
  if (document.frmMembership.chkBenefactor.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkBenefactor.checked = true;	// reset this one
    document.frmMembership.txtBenefactorActAmt.disabled = false;	
//    document.frmMembership.txtBenefactorActAmt.value = FormatStringOut(PatronParms[PatronBenefactorNdx][PatronMinAmtNdx], PriceFmt);
	OpenPreferSeatLine(PatronBenefactorNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtBenefactorActAmt.value = " ";	
    document.frmMembership.txtBenefactorActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtBenefactorActAmt();
}

function updtBenefactorActAmt()
{
  updtPatronActAmt(document.frmMembership.txtBenefactorActAmt, PatronBenefactorNdx);
}

// ====================================
// Conservator Patron
// ====================================

function InitConservatorLine()
{
  InitPatronLine(document.frmMembership.chkConservator, document.frmMembership.txtConservatorActAmt);
}

function chkConservatorOnClick()
{
  if (document.frmMembership.chkConservator.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkConservator.checked = true;	// reset this one
    document.frmMembership.txtConservatorActAmt.disabled = false;	
//    document.frmMembership.txtConservatorActAmt.value = FormatStringOut(PatronParms[PatronConservatorNdx][PatronMinAmtNdx], PriceFmt);
	OpenPreferSeatLine(PatronConservatorNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtConservatorActAmt.value = " ";	
    document.frmMembership.txtConservatorActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtConservatorActAmt();
}

function updtConservatorActAmt()
{
  updtPatronActAmt(document.frmMembership.txtConservatorActAmt, PatronConservatorNdx);
}

// ====================================
// Director Patron
// ====================================

function InitDirectorLine()
{
  InitPatronLine(document.frmMembership.chkDirector, document.frmMembership.txtDirectorActAmt);
}

function chkDirectorOnClick()
{
  if (document.frmMembership.chkDirector.checked)
  // Open the line
  {
    ClearAllPatronLines();		// reset all patron lines
	document.frmMembership.chkDirector.checked = true;	// reset this one
    document.frmMembership.txtDirectorActAmt.disabled = false;	
//    document.frmMembership.txtDirectorActAmt.value = FormatStringOut(PatronParms[PatronDirectorNdx][PatronMinAmtNdx], PriceFmt);
	OpenPreferSeatLine(PatronDirectorNdx);	
    OpenMatchingGiftsLine();	
  }
  else
  // Close the line and reset its values
  {
    document.frmMembership.txtDirectorActAmt.value = " ";	
    document.frmMembership.txtDirectorActAmt.disabled = true;
	ClosePreferSeatLine();	
	CloseMatchingGiftsLine();
  }
  updtDirectorActAmt();
}

function updtDirectorActAmt()
{
  updtPatronActAmt(document.frmMembership.txtDirectorActAmt, PatronDirectorNdx);
}

// ====================================
// Generate variables to pass to Paypal in the form "frmMembership"
// ====================================

function GeneratePaypalCode()
{
  var newElement;				// Use this var to create elements (i.e. fields) for the form to pass to Paypal
  var item_ct = 0;				// Number of line items in the order
  var cart_total_amount = 0;	// Order total amount
  var item_name = new Array();
  var item_quantity = new Array();
  var item_amount = new Array();

// ====================================
// First check if nothing is checked and do not proceed if so.
// IE presents a problem in the following situation ....
// 1. User selects one or more items and goes to PayPal
// 2. Uses browser back button to return to this page.
// 3. Clicks the submit button without changing product selection.
// In this case, IE leaves the boxes checked in the screen, but their "checked" property is false.
// This means we see no boxes checked internally and therefore send an empty cart to PayPal.
// Solution is to cancel submit and issue a warning.
// ====================================

  var cart_empty = true;
  
  if (document.getElementsByTagName('span')['txtTicketTotal'].innerHTML != 0) cart_empty = false;
  if (document.frmMembership.txtContributingActAmt.value != 0) cart_empty = false;
  if (document.frmMembership.txtSustainingActAmt.value != 0) cart_empty = false;
  if (document.frmMembership.txtSupportingActAmt.value != 0) cart_empty = false;
  if (document.frmMembership.txtBenefactorActAmt.value != 0) cart_empty = false;
  if (document.frmMembership.txtConservatorActAmt.value != 0) cart_empty = false;
  if (document.frmMembership.txtDirectorActAmt.value != 0) cart_empty = false;
	
  if (cart_empty)
  {
    StartUp();			// start fresh
    alert ("There are no items selected.\nPlease check your items and try again.\nThank you.");
    return false;					// this cancels the submit
  }

// ====================================
// Test that patron contribution has been specified if patron box has been checked.
// ====================================

  var PatronContribMissing = false;
  var PatronContribMsg = "";
  
  if (document.frmMembership.chkContributing.checked && document.frmMembership.txtContributingActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Contributing";
  }
  if (document.frmMembership.chkSustaining.checked && document.frmMembership.txtSustainingActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Sustaining";
  }
  if (document.frmMembership.chkSupporting.checked && document.frmMembership.txtSupportingActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Supporting";
  }
  if (document.frmMembership.chkBenefactor.checked && document.frmMembership.txtBenefactorActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Benefactor";
  }
  if (document.frmMembership.chkConservator.checked && document.frmMembership.txtConservatorActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Conservator";
  }
  if (document.frmMembership.chkDirector.checked && document.frmMembership.txtDirectorActAmt.value.trim() == "")
  {
    PatronContribMissing = true;
	PatronContribMsg = "Director";
  }
  
  if (PatronContribMissing)
  {
//    StartUp();			// start fresh
    alert ("You have selected patronage at the " + PatronContribMsg + " level, but have not entered your contribution.\nPlease enter your contribution amount and then click the Buy Now button again.\nThank you.");
    return false;					// this cancels the submit
  }

// ====================================
// Show summary and confirm user wants to proceed to PayPal
// ====================================

  var ConfirmMsg = "Your total order is $" + document.getElementsByTagName('span')['txtGrandTotal'].innerHTML + "\nProceed with payment?";
  if (!confirm(ConfirmMsg))
  {
    return false;					// this cancels the submit
  }

// ====================================
// Test if patron has left program name blank AND anonymous box is unchecked
// ====================================

  if (PatronTotal > 0)
  {
	if (document.frmMembership.txtProgramNameValue.value.trim() == "" && !document.frmMembership.chkAnonymous.checked)
	{
      var ConfirmMsg = "You have not entered a name to appear in the program.\n Please click \"OK\" to remain anonymous, or \"Cancel\" to go back.";
      if (confirm(ConfirmMsg))
      {
        document.frmMembership.chkAnonymous.checked = true;
      }
	  else
      {
        return false;					// this cancels the submit
      }
	}
  }

// ====================================
// cmd = "_cart"
// ====================================

  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="cmd";
  newElement.value="_cart";
  newElement.id="PP_cmd";
  document.frmMembership.appendChild(newElement);

// ====================================
// Paypal vendor account
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="business";
  newElement.value="paypal@corningcivicmusic.org";
  newElement.id="PP_business";
  document.frmMembership.appendChild(newElement);

// ====================================
// URL for normal return from PayPal after transaction is complete
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="return";
  newElement.value="http://www.corningcivicmusic.org";
  newElement.id="return";
  document.frmMembership.appendChild(newElement);

// ====================================
// URL for return from PayPal after transaction cancel
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="cancel_return";
  newElement.value="http://www.corningcivicmusic.org";
  newElement.id="cancel_return";
  document.frmMembership.appendChild(newElement);

// ====================================
// URL for PayPal's "continue shopping" button
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="shopping_URL";
  newElement.value="http://corningcivicmusic.org";
  newElement.id="shopping_URL";
  document.frmMembership.appendChild(newElement);

// ====================================
// "upload" var is the trigger to Paypal that we're passing entire shopping cart
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="upload";
  newElement.value="1";
  newElement.id="upload";
  document.frmMembership.appendChild(newElement);

// ====================================
// Build arrays corresponding to line items
// ====================================
  if (MyParseInt(document.frmMembership.txtAdultCt.value) > 0)  // Adult tickets?
  {
    item_ct++;
	item_name[item_ct] = "Adult membership";
    item_quantity[item_ct] = document.frmMembership.txtAdultCt.value;
    item_amount[item_ct] = valAdultPrice;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtSeniorCt.value > 0)  // Senior tickets?
  {
    item_ct++;
	item_name[item_ct] = "Senior membership";
    item_quantity[item_ct] = document.frmMembership.txtSeniorCt.value;
    item_amount[item_ct] = valSeniorPrice;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtStudentCt.value > 0)  // Student tickets?
  {
    item_ct++;
	item_name[item_ct] = "Student membership";
    item_quantity[item_ct] = document.frmMembership.txtStudentCt.value;
    item_amount[item_ct] = valStudentPrice;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.chkFamily.checked)  // Family tickets?
  {
    item_ct++;
	item_name[item_ct] = "Family membership, " + document.frmMembership.txtFamilyAdults.value + " adults, " + document.frmMembership.txtFamilyChildren.value + " children";
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = valFamilyPrice;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtContributingActAmt.value > 0)  // Contributing patron?
  {
    item_ct++;
	item_name[item_ct] = "Contributing patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtContributingActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtSustainingActAmt.value > 0)  // Sustaining patron?
  {
    item_ct++;
	item_name[item_ct] = "Sustaining patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtSustainingActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtSupportingActAmt.value > 0)  // Supporting patron?
  {
    item_ct++;
	item_name[item_ct] = "Supporting patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtSupportingActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtBenefactorActAmt.value > 0)  // Benefactor patron?
  {
    item_ct++;
	item_name[item_ct] = "Benefactor patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtBenefactorActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtConservatorActAmt.value > 0)  // Conservator patron?
  {
    item_ct++;
	item_name[item_ct] = "Conservator patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtConservatorActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtDirectorActAmt.value > 0)  // Director patron?
  {
    item_ct++;
	item_name[item_ct] = "Director patron";
	item_name[item_ct] += ProgramName() + MatchingGifts();
    item_quantity[item_ct] = 1;
    item_amount[item_ct] = document.frmMembership.txtDirectorActAmt.value;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

  if (document.frmMembership.txtPreferSeatCt.value > 0)  // Student tickets?
  {
    item_ct++;
	item_name[item_ct] = "Prefered seats";
    item_quantity[item_ct] = document.frmMembership.txtPreferSeatCt.value;
    item_amount[item_ct] = valActPreferSeatPrice;
	cart_total_amount += item_quantity[item_ct] * item_amount[item_ct];
  }

// ====================================
// Convert line items array elements to actual Paypal variables
// Each item contains (I from 1 to N)
//   item_name_I
//   quantity_I
//   amount_I
// ====================================
  for (i=1; i <= item_ct; i++)
  {
    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="item_name_" + i
    newElement.value=item_name[i];
    newElement.id="item_name_" + i;
    document.frmMembership.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="quantity_" + i;
    newElement.value=item_quantity[i];
    newElement.id="quantity_" + i;
    document.frmMembership.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="amount_" + i;
    newElement.value=item_amount[i];
    newElement.id="amount_" + i;
    document.frmMembership.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="tax_" + i;
    newElement.value="0";
    newElement.id="tax_" + i;
    document.frmMembership.appendChild(newElement);
  }
  return true;
}

function ProgramName()
{
  if (document.frmMembership.chkAnonymous.checked)
  {
    return ", Anonymous";
  }
  else
  {
    return ", " + document.frmMembership.txtProgramNameValue.value;
  }
}

function MatchingGifts()
{
  if (document.frmMembership.chkMatchingGifts.checked)
  {
    return ", MG";
  }
  else
  {
    return "";
  }
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function MyParseInt(strIn)
{
  if (strIn.trim() == "")
    return 0;
  else
    return parseInt(strIn);
}
-->