var BWSubBuilder = {
	
	perUserFlatPrice: 495,
	
	setup: function() {
		this.allUpgrades = new Array();
		
		$$('.builder-upgrade-skeleton').each(function(a){
			this.allUpgrades.push(a);
		}.bind(this));
		
		this.allUpgrades.each(function(x){
			$(x).observe('click', function(){
				// inputTags = $(x).select('input[type=checkbox]');
				// if(inputTags.length > 0) {
				// 	inputTags[0].checked = true;
				// 	if(inputTags[0].onclick) {
				// 		inputTags[0].onclick();
				// 	}
				// }
				this.replaceAndInclude(x);	
				this.calculateTotal();
			}.bind(this));
			
		}.bind(this));
		
		$('builder-people-addmore-do').onclick = function(){			
			this.addPersonRow();
			this.addPersonRow();
			this.addPersonRow();
			
			if(typeof ClickTaleTag == "function") ClickTaleTag("Clicked Add People");
			
			if($$('.builder-people-skeleton').length > 11) {
				$('builder-people-addmore-do').hide();
				
				warning = $('too-many-people-warning');
				warning.setOpacity(0.0);
				warning.show();				

				f = new fx.Opacity(warning);
				f.now = 0;
				f.toggle();
				
			}
			
			return false;
			
		}.bind(this);		

		this.addPersonRow();
		this.addPersonRow();
		this.addPersonRow();
		this.calculateTotal();
		
		$('add-to-cart').observe('click', this.submitForm.bind(this));
		
		$('builder-form').onsubmit = this.verifyForm.bind(this);

	},
	
	killCustomChoices: function() {
		checkBoxes = $('builder-upgade-custom').select('input[type=checkbox]');
		if(checkBoxes.length > 0) {
			checkBoxes.each(function(a){
				a.checked = false;
			});
		}
		checkBoxLabels = $('builder-upgade-custom').select('label.checkbox_checked');
		if(checkBoxLabels.length > 0) {
			checkBoxLabels.each(function(a){
				$(a).removeClassName('checkbox_checked');
				$(a).addClassName('checkbox_unchecked');
			});
		}		
	},
	
	verifyForm: function() {
		if(this.countValidPeople() == 0) {
			alert("You need to specify at least one user's name and e-mail address.");
			return false;
		}
		if(this.countInvalidPeople() != 0) {
			alert("You need to specify both the name and e-mail address for each user.");
			return false;			
		}
		return true;
	},
	
	submitForm: function() {
		if(this.verifyForm()) {
			$('builder-form').submit();
		}
	},
	
	countValidPeople: function() {
		people = 0;
		$$('.builder-people-skeleton').each(function(skr){
			if(this.doesThisPersonRowNeedAPrice(skr)) {
				people++;
			}
		}.bind(this));
		return people;
	},
	
	countInvalidPeople: function() {
		people = 0;
		$$('.builder-people-skeleton').each(function(skr){
			if(this.doesThisPersonRowHavePartialData(skr)) {
				people++;
			}
		}.bind(this));
		return people;		
	},
	
	calculateTotal: function() {
		total = this.countValidPeople() * this.perUserFlatPrice;
		this.allUpgrades.each(function(x){
			total = total + this.doesThisPackageRowNeedAPrice($(x));
		}.bind(this));
		$('builder-grand-total').update("$" + total);
	},
	
	doesThisPersonRowHavePartialData: function(r) {
		inputTags = r.getElementsByTagName('input');
	
		haveName = false;
		haveEmail = false;
		
		$A(inputTags).each(function(m){
			if(m.className == "email" && m.value != "") {
				haveEmail = true;
			}
			if(m.className == "name" && m.value != "") {
				haveName = true;
			}
		}.bind(this));
		
		return (haveName && !haveEmail) || (!haveName && haveEmail);
	},
	
	doesThisPersonRowNeedAPrice: function(r) {
		inputTags = r.getElementsByTagName('input');
		doWe = true;
		$A(inputTags).each(function(m){
			if(m.value == "") {
				doWe = false;
			} 
		}.bind(this));
		
		priceVal = r.select('td.calc-column');
		if(priceVal.length != 0) {
			$(priceVal[0]).update((doWe) ? "$" + this.perUserFlatPrice : "");
		}
		return doWe;
	},
	
	doesThisPackageRowNeedAPrice: function(r) {
		price = 0;
		doWe = false;
		
		checkBoxes = r.select('input[type=checkbox]');
		if(checkBoxes.length > 0) {
			checkBoxes.each(function(a){
				// alert(a.id + " - " + a.checked);
				if(a.checked) {
					price = price + parseInt(a.getAttribute('price'));
					doWe = true;
				}
			}.bind(this));
		}
		// alert(price);
		
		priceVal = r.select('td.calc-column');
		if(priceVal.length != 0) {
			$(priceVal[0]).update((doWe && price > 0) ? "$" + price : "");
		}
		
		if(!doWe) price = 0;
		
		return price;		
 	},

	replaceAndInclude: function(r) {
		checkBoxes = r.select('input[type=checkbox]');
		if(checkBoxes.length > 0) {
			checkBoxes.each(function(a){
				if(a.checked) {
					replaces = $w(a.getAttribute('replaces'));
					replaces.each(function(e){
						this.killChoice($('builder-upgade-' + e), false);
						this.removeExistingIncludes($('builder-upgade-' + e));
					}.bind(this));
					
					
					specials = $w(a.getAttribute('specials'));
					specials.each(function(e){
						$('builder-upgade-' + e).show();
					});			
				} else {
					
					specials = $w(a.getAttribute('specials'));
					specials.each(function(e){
						$('builder-upgade-' + e).hide();
					});
				}
				
				includes = $w(a.getAttribute('includes'));
				includes.each(function(e){
					this.killChoice($('builder-upgade-' + e), a.checked);
				}.bind(this));
			
				
			}.bind(this));
		}
	},

	removeExistingIncludes: function(r) {
		if(!r) return;
		inCheckBoxes = r.select('input[type=checkbox]');
		inCheckBoxes.each(function(a){
			includes = $w(a.getAttribute('includes'));
			includes.each(function(e){
					this.killChoice($('builder-upgade-' + e), false);
			}.bind(this));
			specials = $w(a.getAttribute('specials'));
			specials.each(function(e){
				this.killChoice($('builder-upgade-' + e), false);
				$('builder-upgade-'+e).hide();
			}.bind(this));
		}.bind(this));
		
	},

	killChoice: function(r, included) {
		if (r == undefined) return;
		checkBoxes = r.select('input[type=checkbox]');
		if(checkBoxes.length > 0) {
			checkBoxes.each(function(a){
				a.checked = false;
				a.disabled = included;
				a.enabled = !included;
			});
		}
		checkBoxLabels = r.select('label');
		if(checkBoxLabels.length > 0) {
			checkBoxLabels.each(function(a){
				$(a).removeClassName('checkbox_checked');
				if(included) {
					$(a).addClassName('included_in_package');			
				} else {
					$(a).removeClassName('included_in_package');
					$(a).addClassName('checkbox_unchecked');					
				}
			});
		}		
	},

	addPersonRow: function() {
		
		if(typeof ClickTaleIsIn == "function" && typeof ClickTaleExec == "function") {
			
			if(ClickTaleIsIn("recording")) {
				ClickTaleExec("BWSubBuilder.addPersonRow()");
			}
			
		}
		
		newPersonRow = $('builder-people-skeleton').cloneNode(true);
		addPersonRow = $('builder-people-addmore').remove();

		bpTable = $('builder-people');

		if(bpTable.firstChild.nodeName == "TBODY") {
			out = bpTable.firstChild;
		} else {
			out = bpTable;
		}
		
		out.appendChild(newPersonRow);
		out.appendChild(addPersonRow);
		
		$(newPersonRow).show();
		
		myIndex = $$('.builder-people-skeleton').length - 1;
		
		xm = newPersonRow.getElementsByTagName('span');
		if(xm.length > 0) {
			$(xm[0]).update(myIndex);			
		}

		inputTags = newPersonRow.getElementsByTagName('input');
		$A(inputTags).each(function(m){
			m.name = "people[" + (myIndex - 1) + "]["+m.className+"]";
			m.myRow = newPersonRow;
			$(m).observe('change', function(){
				this.doesThisPersonRowNeedAPrice(m.myRow);
				this.calculateTotal();
			}.bind(this));
		}.bind(this));

		this.doesThisPersonRowNeedAPrice(newPersonRow);
		
		return false;
	}
	
};