/*
 * JavaScript for the Dobson "Build Your Plan" app
 *
 * Requires MooTools 1.2
 */

var svcType = 'res';
var slBasic;
var slAdv;
var slInet;
var slMulti;
var slRepair;
var slSecurity;

window.addEvent('domready', function(){
	$$('#svcItems div.svc').each(function(o){
		o.setProperty('sel', '0');

		o.addEvent('click', function(){
			if (o.getProperty(svcType + 'price') != 'NA'){
				var mystate = this.getProperty('sel');
				if (mystate == 0){
					this.setProperty('sel', '1');
					this.addClass('selected');
					// get any prerequisites
					var req = this.getProperty('req');
					if (req && req.length > 0){
						var reqArr = req.split(' ');
						for (i=0; i<reqArr.length; i++){
							var obj = $(reqArr[i]);
							if (obj.getProperty('sel') == 0){
								obj.fireEvent('click');
							}
						}
					}
					// remove any checked packages
					$$('#svcPkgs div.svc').each(function(pk){
						pk.setProperty('sel', '0');
						pk.removeClass('selected');
					});
				} else {
					var reqExists = false;
					var myid = this.getProperty('id');
					// find any items that i'm a prereq of
					$$('#svcItems div.selected').each(function(ob){
						var req = ob.getProperty('req');
						if (req && req.length > 0){
							var pos = req.search(myid);
							if (pos != -1){
								reqExists = true;
							}
						}
					});
	
					if (reqExists == false){
						this.setProperty('sel', '0');
						this.removeClass('selected');
					}
				}
				calcTotal();
			} else {
				if (svcType == 'res') alert('This item is not available for residential customers.');
				else alert('This items is not available for business customers.');
			}
		});
	});
	
	$$('#svcPkgs div.svc').each(function(o){
		o.setProperty('sel', '0');
		o.addEvent('click', function(){
			if (o.getProperty(svcType + 'price') != 'NA'){
				$$('#svcPkgs div.svc').each(function(obj){
					if (this != obj && obj.getProperty('sel') != '0'){
						obj.removeClass('selected');
						obj.setProperty('sel', '0');
					}
				});
				var mystate = this.getProperty('sel');
				if (mystate == 0){
					this.setProperty('sel', '1');
					this.addClass('selected');
					var price = this.getProperty(svcType + 'price');
					setTotal(price);
					$('svcSummary').innerHTML = '<h3>Your current selection includes:</h3>' + this.getProperty('title');
					// remove any checked a la carte items
					$$('#svcItems div.svc').each(function(pk){
						pk.setProperty('sel', '0');
						pk.removeClass('selected');
					});
				}
			} else {
				if (svcType == 'res') alert('This item is not available for residential customers.');
				else alert('This items is not available for business customers.');
			}
		});
	});


	$('svcRes').addEvent('click', function(){
		if (svcType != 'res'){
			this.addClass('selected');
			$('svcBiz').removeClass('selected');
			svcType = 'res';
			calcTotal();
		}
	});
	$('svcBiz').addEvent('click', function(){
		if (svcType != 'biz'){
			this.addClass('selected');
			$('svcRes').removeClass('selected');
			svcType = 'biz';
			calcTotal();
		}
	});

	
	slBasic = new Fx.Slide('basic').hide();
	slAdv = new Fx.Slide('advphone').hide();
	slInet = new Fx.Slide('inet').hide();
	//slMulti = new Fx.Slide('multiline').hide();
	slRepair = new Fx.Slide('repair').hide();
	slSecurity = new Fx.Slide('security').hide();
	
	$('basicHdr').addEvent('click', function(){
		slBasic.toggle();
	});
	$('advphoneHdr').addEvent('click', function(){
		slAdv.toggle();
	});
	$('inetHdr').addEvent('click', function(){
		slInet.toggle();
	});
	/*$('multilineHdr').addEvent('click', function(){
		slMulti.toggle();
	});*/
	$('repairHdr').addEvent('click', function(){
		slRepair.toggle();
	});
	$('securityHdr').addEvent('click', function(){
		slSecurity.toggle();
	});
	
	$('scrollToSummary').addEvent('click', function(){
		winscroller = new Fx.Scroll(window).toElement('summaryarea');
		winscroller.addEvent('complete', function(){
			$('summaryarea').highlight();
		});
	});
	
	$('ordernewbiz').addEvent('click', function(){
		document.forms.passedServicesForm.action = '/order/business';
		passForm();
	});
	
	$('ordernewres').addEvent('click', function(){
		document.forms.passedServicesForm.action = '/order/residential';
		passForm();
	});
	
	$('orderaddbutton').addEvent('click', function(){
		passForm();
	});
});


function passForm(){
	$('passedServices').value = $('svcSummary').innerHTML.replace(summaryIntro, '');
	document.forms.passedServicesForm.submit();
}

var summaryIntro = '<h3>Your current selection includes:</h3>';
function calcTotal(){
	var total = 0;
	var summary = '';
	
	// determine price breaks
	var advCnt = 0;
	$$('.advphone').each(function(o){
		if (o.hasClass('selected')){
			advCnt++;
		}
	});
	var varPrice = 3;
	switch (true){
		case advCnt==1:
			varPrice = 3;
			break;
		case advCnt==2:
			varPrice = 2.25;
			break;
		case advCnt>2:
			varPrice = 2;
			break;
	}
	$$('.advphone').each(function(o){
		o.setProperty('resprice', varPrice);
		o.setProperty('bizprice', varPrice);
	});
	
	// tally up selected svcs
	$$('#svcItems div.svc').each(function(o){
		if (o.getProperty('sel') == '1') {
			total += (o.getProperty(svcType + 'Price') * 1);
			if (summary.length > 0) summary += ', ';
			summary += o.innerHTML;
		}
	});
	$('svcTotal').innerHTML = 'Total $' + total.toFixed(2);
	$('smAmt').innerHTML = total.toFixed(2);
	$('svcSummary').innerHTML = summaryIntro + summary;
	
	
	// set the service number counts for each type
	var svcTypes = ['basic', 'inet', 'advphone', 'security', 'repair'];
	for (i=0; i<svcTypes.length; i++){
		var cnt = 0;
		$$('#' + svcTypes[i] + ' div.svc').each(function(o){
			if (o.hasClass('selected')) cnt++;
		});
		$(svcTypes[i] + 'Hdr').innerHTML = $(svcTypes[i]+'Hdr').innerHTML.replace(/\([0-9]+\)/, '(' + cnt + ')');
	}
}


function setTotal(amt){
	amt = amt * 1;
	$('svcTotal').innerHTML = 'Total $' + amt.toFixed(2);
	$('smAmt').innerHTML = amt.toFixed(2);
}