/*
 * edb 13 feb 2011
 * updated 27 nov 2011 for 2012 camp
 * functions to validate registration form data
 */

function toDollars( nValue, nWide ) {
    var s= nValue.toFixed(2);
    for( j=6; j<15; j+=4 ) {  // insert commas
        if ( s.length > j ) { 
            s= s.substr(0,s.length-j)+','+ s.substr(s.length-j);
        }
    }
    s= '$'+s;  // leading dollar sign
    if ( !isNaN(nWide) ) { // parameter is present
        while( nWide > s.length ) {  // pad with leading spaces
            s=' '+s;
        }
    }
    return(s);
}


function ComputeTotal(TheForm)
{
  /* array order: FullFee, Camper26, Camper23, Camper17, Camper13,
   *              Underemployed, 4for3, Staff, Committee, Spice */
  var Fees = new Array( 625.00, 300.00, 250.00, 200.00, 150.00,
	                300.00,   0.00,   0.00, 300.00, 300.00 );
  var NumFees = Fees.length;
  var Total = 0;
  var Fee = 0.0;
  var Deposit = 0.0;
  var Payment = 0.0;
  var Balance = 0.0;

  /* TheType = typeof(document.getElementById('Total'))); */

  if((typeof(document.getElementById('Total')) != 'undefined') &&
           (document.getElementById('Total') != null))
  {

      for(FeeChoice=0; FeeChoice<TheForm.CamperFee.length; FeeChoice++) {
	if (TheForm.CamperFee[FeeChoice].checked) {
	  CamperFee = TheForm.CamperFee[FeeChoice].value;
	  break;
	}
      }

      Deposit = 0.00;
      if ((FeeChoice < 0) || (FeeChoice > NumFees-1))
      {
	// shenanigans...
	Fee = 625.00;
      } else {
	Fee = Fees[FeeChoice];
      }
      DonationAmount = TheForm.Donation.value;
      DonationAmount = DonationAmount.replace(/\$/, "");
      DonationAmount = DonationAmount.replace(/ */, "");
      DonationAmount = DonationAmount.replace(/,/, "");

      Donation = DonationAmount * 1.0;

      DonationAmount = toDollars(Donation, 2);
      if (Donation > 0) {
	TheForm.Donation.value = DonationAmount;
      } else {
	TheForm.Donation.value = "";
      }


      if (TheForm.DepositOnly.checked) {
	if(Fee >= 150.0) {
	  Deposit = 150.00;
	} else {
	  Deposit = Fee;
	}
      }

      Total = Fee + Donation;
      if (TheForm.SemiPrivate.checked)
      {
	Total += 100.00;
      }
      TotalString = toDollars(Total, 2);
      document.getElementById('Total').innerHTML = TotalString;

      if (Deposit > 0)
      {
	Payment = Deposit + Donation;
      } else {
	Payment = Fee + Donation;
      }
      if (TheForm.SemiPrivate.checked)
      {
	Payment += 100.00;
      }
      TotalString = toDollars(Payment, 2);
      document.getElementById('Payment').innerHTML = TotalString;

      Balance = Total - Payment;
      BalanceString = toDollars(Balance, 2);
      document.getElementById('Balance').innerHTML = BalanceString;

  }

}


function ReminderPrivateRoom(TheForm)
{
  if (TheForm.SemiPrivate.checked && TheForm.CamperFee[0].checked == 0) {
    alert("To request a semi-private room both campers must register as full-fee campers).");
    TheForm.CamperFee.className = "errorinput";
    TheForm.SemiPrivate.checked = 0;
    TheForm.CamperFee[0].focus();
    return false;
  }
  TheForm.CamperFee.className = "";

  if (TheForm.SemiPrivate.checked && TheForm.ShareWith.value == "") {
    alert("To request a semi-private room you must specify a roommate above in \"I would like to room with\" (both of you must register as full-fee campers).");
    TheForm.ShareWith.className = "errorinput";
    TheForm.ShareWith.focus();
    return false;
  }  
  TheForm.ShareWith.className = "";
  return true;
}

function ReminderReducedFee(TheForm)
{
  CamperFee = "";

  for(FeeChoice=0; FeeChoice<TheForm.CamperFee.length; FeeChoice++) {
    if (TheForm.CamperFee[FeeChoice].checked) {
      CamperFee = TheForm.CamperFee[FeeChoice].value;
      break;
    }
  }

  if(FeeChoice > 8) {
    alert("Please select a Camper Fee");
    TheForm.CamperFee.className = "errorinput";
    TheForm.CamperFee[0].focus();
    return false;
  }

  if(TheForm.Age.value == "" && (FeeChoice > 0 && FeeChoice <= 4))
  {
    alert("Please enter your age as of the start of camp on July 3, 2011.");
    TheForm.Age.className = "errorinput";
    TheForm.Age.focus();
    return false;
  }
  TheForm.Age.className = "";

  CamperAge = parseInt(TheForm.Age.value);
  if(CamperAge < 13)
  {
    alert("I'm sorry, all campers must be at least 13 years of age.");
    TheForm.Age.className = "errorinput";
    TheForm.Age.focus();
    return false;
  }

  if(CamperFee == "Camper26-300" && CamperAge > 30)
  {
    alert("You indicated you will be older than 30 at camp.\nPlease choose the Full Camper Fee and support the camp.  Thank you!");
    TheForm.Age.className = "errorinput";
    TheForm.Age.focus();
    return false;
  }
  if(CamperFee == "Camper23-250" && CamperAge > 25) {
    if(CamperAge > 25) {
      alert("You indicated you will be older than 25 at camp.\nPlease choose the age 26-30 option or the Full Camper Fee option.  Thank you!");
      TheForm.Age.className = "errorinput";
      TheForm.Age.focus();
      return false;
    }
  } else if (CamperFee == "Camper17-200" && CamperAge > 22) {
    alert("You indicated you will be older than 22 at camp.\nPlease choose the age-appropriate reduced-fee option or the Full Camper Fee option.  Thank you!");
    TheForm.Age.className = "errorinput";
    TheForm.Age.focus();
    return false;
  } else if (CamperFee == "Camper13-150" && CamperAge > 16) {
    alert("You indicated you will be older than 16 at camp.\nPlease choose the age-appropriate reduced-fee option or the Full Camper Fee option.  Thank you!");
    TheForm.Age.className = "errorinput";
    TheForm.Age.focus();
    return false;
  } else {
    TheForm.Age.className = "";
  }
  if ((TheForm.Referral.value != "") && (CamperFee != "FullCamperFee-625")) {
    alert("To sponsor a camper for \"Hey for Four for Three\" all three sponsors must register as Full Fee campers.");
    TheForm.CamperFee.className = "errorinput";
    TheForm.Referral.className = "errorinput";
    TheForm.Referral.focus();
    return false;
  } else {
    TheForm.Referral.className = "";
  }
  return true;
}

function ReminderUnderEmployed(TheForm)
{
  alert("We're glad you can join us at camp!  If you can pay more than this reduced amount, please do so\nto help defray the cost of helping other campers afford camp!");
  return true;
}

function Reminder4For3Names(TheForm)
{
  alert("Please list the three full-fee campers who are naming you as their Hey for Four for Three referral.\n\nYour application is not complete until all four of you have registered, and your three friends have paid.");
  return true;
}

function Reminder4For3(TheForm)
{
  alert("Be sure that all three of your full-fee friends register and name you as their referral.\n\nYour application is not accepted until all four of you have registered, and your three friends have paid the full camper fee.");
  return true;
}

function ValidateEmail(TheForm)
{
  if(TheForm.Email.value == "")
  {
    alert("We need your email address if you register online.\nPlease enter your email address and submit again.");
    TheForm.Email.className = "errorinput";
    TheForm.Email.focus();
    return false;
  }
  TheForm.Email.className = "";
  TheForm.email.value = TheForm.Email.value;
  return true;
}

function ValidateCamperContact(TheForm)
{
  if ((TheForm.FirstName.value == "") || (TheForm.LastName.value == ""))
  {
    alert("We really want to know your name!");
    TheForm.FirstName.className = "errorinput";
    TheForm.LastName.className = "errorinput";
    TheForm.FirstName.focus();
    return false;
  }
  TheForm.FirstName.className = "";
  TheForm.LastName.className = "";
  TheForm.realname.value = TheForm.FirstName.value + " " + TheForm.LastName.value;

  if (TheForm.Address1.value == "")
  {
    alert("Please enter your complete mailing address");
    TheForm.Address1.className = "errorinput";
    TheForm.Address2.className = "errorinput";
    TheForm.City.className = "errorinput";
    TheForm.ZIP.className = "errorinput";
    TheForm.Address1.focus();
    return false;
  }
  TheForm.Address1.className = "";
  TheForm.Address2.className = "";
  TheForm.City.className = "";
  TheForm.ZIP.className = "";

  if (TheForm.ButtonName.value == "")
  {
    alert("Please check the name for your name button\n(we took a guess)");
    TheForm.ButtonName.value = TheForm.FirstName.value
    TheForm.ButtonName.className = "errorinput";
    TheForm.ButtonName.focus();
    return false;
  }
  TheForm.ButtonName.className = "";

  return true;
}

function ValidateGender(TheForm)
{
  var Sex, SexChoice;

  for(SexChoice=0; SexChoice<TheForm.Sex.length; SexChoice++) {
    if (TheForm.Sex[SexChoice].checked) {
      Sex = TheForm.Sex[SexChoice].value;
      break;
    }
  }
  var box;
  if (SexChoice > 1)
  {
    alert("Please indicate your gender/sex");
    box = document.getElementById("SexBox");
    box.className = "errorinput";
    box.focus();
    return false;
  } else {
      if (((TheForm.HousingGenderPref[0].checked == 1) && (SexChoice == 0)) ||
           ((TheForm.HousingGenderPref[1].checked == 1) && (SexChoice == 1)))
      {
	  alert("Your gender and housing preference are not compatible");
	  box = document.getElementById("SexBox");
	  box.className = "errorinput";
	  box = document.getElementById("HousingPref");
	  box.className = "errorinput";
	  box.focus();
	  return false;
      }
  }
  box = document.getElementById("SexBox");
  box.className = "";
  box = document.getElementById("HousingPref");
  box.className = "";
  return true;
}

function ValidateForm(TheForm)
{
  if (!ValidateCamperContact(TheForm)) {
    return false;
  }
  if (!ReminderReducedFee(TheForm)) {
    return false;
  }
  if (!ReminderPrivateRoom(TheForm)) {
    return false;
  }
  if (!ValidateEmail(TheForm)) {
    return false;
  }
  if (!ValidateGender(TheForm)) {
    return false;
  }
  return true;
}

/*
 * 	Developed by Robert Nyman, http://www.robertnyman.com
 * 		Code/licensing: http://code.google.com/p/getelementsbyclassname/
 * 		*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};


function ToggleHiddenFields(TheForm)
{
    var ToggleFields = getElementsByClassName('StaffCommittee');

    for (var i=0; i< ToggleFields.length; i++) {
	var ThisOne = ToggleFields[i];
	if (ThisOne.style.display == "none") {
	    ThisOne.style.display = "";
	} else {
	    ThisOne.style.display = "none";
	}
    }
}

