YAHOO.namespace("configurator.demo");

var hws 	 = YAHOO.configurator.demo.hws;
var oss 	 = YAHOO.configurator.demo.oss;
var cards 	 = YAHOO.configurator.demo.cards;
var icards 	 = YAHOO.configurator.demo.icards;
var bris 	 = YAHOO.configurator.demo.bris;
var hss 	 = YAHOO.configurator.demo.hss;
var phones 	 = YAHOO.configurator.demo.phones;
var sips 	 = YAHOO.configurator.demo.sips;
var services = YAHOO.configurator.demo.services;

function setCardListListner() {
	for (i=1;i<cards.length;i++) {
		YAHOO.util.Event.addListener('cardCb'+i	 ,   'click', osClick);
		YAHOO.util.Event.addListener('cardInfo'+i,   'click', showOSInfo);
		YAHOO.util.Event.addListener('cardAmount'+i, 'blur',  setAmount);
		YAHOO.util.Dom.get('cardCb'+i).checked 		= false;
		YAHOO.util.Dom.get('cardAmount'+i).value	= 1;
	}
	function osClick() 	  { cards[this.id.charAt(6)].selected(); setLinesCb(); }
	function showOSInfo() { cards[this.id.charAt(8)].showInfo(); }
	function setAmount()  { cbPrilines.value = "-1"; cards[this.id.charAt(10)].save(); setLinesCb(); }

	function setLinesCb() {
		var totLines = 0;
		
		for (i=1; i<cards.length; i++)
			if (cards[i].sel)
				totLines += cards[i].lines * cards[i].getAmount();
		
		if (totLines != 0 && totLines <= 180)
			cbPrilines.value = totLines;
		else
			cbPrilines.value = '-1';
	}
}

function setICardListListner() {
	for (i=1;i<icards.length;i++) {
		YAHOO.util.Event.addListener('icardCb'+i  	, 'click', osClick);
		YAHOO.util.Event.addListener('icardInfo'+i	, 'click', showOSInfo);
		YAHOO.util.Event.addListener('icardAmount'+i, 'blur' , setAmount);
		YAHOO.util.Dom.get('icardCb'+i).checked = false;
		YAHOO.util.Dom.get('icardAmount'+i).value = 1;		
	}
	function osClick() 	  { 
		icards[this.id.charAt(7)].selected(); 
		setLinesCb();		
	}
	function showOSInfo() { icards[this.id.charAt(9)].showInfo(); }
	function setAmount()  {
		icards[this.id.charAt(11)].save();
		setLinesCb();
		lineeDisp.innerHTML	= 0;
	}
	function setLinesCb() {
		var totLines = 0;
		
		for (i=1; i<icards.length; i++)
			if (icards[i].sel)
				totLines += icards[i].lines * icards[i].getAmount();
		
		if (totLines != 0 && totLines <= 32)
			cbAnalines.value = totLines;
		else
			cbAnalines.value = '-1';
	}
}

function setOSListListner() {
	for (i=1;i<oss.length;i++) {
		YAHOO.util.Event.addListener('osCb'+i, 'click', osClick);
		YAHOO.util.Event.addListener('osInfo'+i, 'click', showOSInfo);
		YAHOO.util.Dom.get('osCb'+i).checked = false;
	}	
	function osClick() 	  {	oss[this.id.charAt(4)].selected(); }
	function showOSInfo() { oss[this.id.charAt(6)].showInfo(); }	
}

function setServicesListListner() {
	for (i=1;i<services.length;i++) {
		YAHOO.util.Event.addListener('serviceCb'+i  , 'click', osClick);
		YAHOO.util.Event.addListener('serviceInfo'+i, 'click', showOSInfo);
		YAHOO.util.Dom.get('serviceCb'+i).checked = false;
	}
	function osClick() 	  {	services[this.id.charAt(9)].selected(); }
	function showOSInfo() { services[this.id.charAt(11)].showInfo(); }	
}

function setHSListListener() {
	for (i=1;i<hss.length;i++) {
		YAHOO.util.Event.addListener('hsCb'+i    , 'click', hsClick);
		YAHOO.util.Event.addListener('hsInfo'+i  , 'click', showHsInfo);
		YAHOO.util.Event.addListener('hsAmount'+i, 'blur' , setAmount);
		YAHOO.util.Dom.get('hsCb'+i).checked 	= false;
		YAHOO.util.Dom.get('hsAmount'+i).value 	= 1;
	}
	function showHsInfo() {	hss[this.id.charAt(6)].showInfo(); }
	function hsClick() 	  {	hss[this.id.charAt(4)].selected();	}
	function setAmount()  { hss[this.id.charAt(8)].save();}	
}

function setPhonesListListener() {
	for (i=1;i<phones.length;i++) {
		YAHOO.util.Event.addListener('phoneCb'+i    , 'click', hsClick);
		YAHOO.util.Event.addListener('phoneInfo'+i  , 'click', showHsInfo);
		YAHOO.util.Event.addListener('phoneAmount'+i, 'blur' , setAmount);
		YAHOO.util.Dom.get('phoneCb'+i).checked 	= false;
		YAHOO.util.Dom.get('phoneAmount'+i).value 	= 1;
	}
	function showHsInfo() { phones[this.id.charAt(9)].showInfo(); }	
	function hsClick() 	  {	phones[this.id.charAt(7)].selected(); checkAnalogLines();}
	function setAmount()  { phones[this.id.charAt(11)].save(); checkAnalogLines();}
}

function setSIPListListener() {
	for (i=1;i<sips.length;i++) {
		YAHOO.util.Event.addListener('sipCb'+i  ,   'click', hsClick);
		YAHOO.util.Event.addListener('sipInfo'+i,   'click', showHsInfo);
		YAHOO.util.Event.addListener('sipAmount'+i, 'blur' , setAmount);
		YAHOO.util.Dom.get('sipCb'+i).checked 	= false;
		YAHOO.util.Dom.get('sipAmount'+i).value = 1;
	}
	function showHsInfo() {	sips[this.id.charAt(7)].showInfo(); }
	function hsClick() 	  {	sips[this.id.charAt(5)].selected(); }
	function setAmount()  { sips[this.id.charAt(9)].save();}
}

function setBRIListListener() {
	for (i=1;i<sips.length;i++) {
		YAHOO.util.Event.addListener('briCb'+i    , 'click', hsClick);
		YAHOO.util.Event.addListener('briInfo'+i  , 'click', showHsInfo);
		YAHOO.util.Event.addListener('briAmount'+i, 'blur' , setAmount);
		YAHOO.util.Dom.get('briCb'+i).checked 	= false;
		YAHOO.util.Dom.get('briAmount'+i).value = 1;
	}
	function showHsInfo() {	bris[this.id.charAt(7)].showInfo(); }
	function hsClick() 	  {	bris[this.id.charAt(5)].selected(); setLinesCb(); }
	function setAmount()  { cbBrilines.value = "-1"; bris[this.id.charAt(9)].save(); setLinesCb(); }
	function setLinesCb() {
		var totLines = 0;
		
		for (i=1; i<bris.length; i++)
			if (bris[i].sel)
				totLines += bris[i].lines * bris[i].getAmount();

		if (totLines != 0 && totLines <= 16)
			cbBrilines.value = totLines;
		else
			cbBrilines.value = '-1';
	}	
}


function checkAnalogLines() {
	var sellines = 0;
	
	for (i=0; i<phones.length;i++)
		if(phones[i].sel) 
			sellines += parseInt(phones[i].getAmount());

	if(dispAnalogLines < sellines) {
		lineeDispErr.innerHTML = 'Le linee dispobili non sono sufficienti per i telefoni selezionati';			
	} else {
		lineeDispErr.innerHTML = '';
	}
}

function setLinesCbsListener() {
	cbPrilines = YAHOO.util.Dom.get	('priLinesCb');
	cbBrilines = YAHOO.util.Dom.get	('briLinesCb');
	cbAnalines = YAHOO.util.Dom.get	('analLinesCb');	
	
	cbPrilines.value = "-1";
	cbBrilines.value = "-1";
	cbAnalines.value = "-1";

	cbPrilines.disabled = true;
	cbBrilines.disabled = true;
	cbAnalines.disabled = true;	

	YAHOO.util.Event.addListener(cbAnalines, 'change', function dbChange() {
		
		var effLines = 0;
		icards[1].deSelected();
		icards[2].deSelected();
		icards[3].deSelected();
		
		if (this.value == '') return;
		var val = this.value;

		if (val <= 4) {
			icards[1].selectedWithoutFocus();
			icards[1].setAmount(1);
			effLines = 4;
		} else if (val > 4  && val <= 8) {
			icards[2].selectedWithoutFocus();
			icards[2].setAmount(1);
			effLines = 8;
		} else if (val > 8  && val <= 12) {
			icards[1].selectedWithoutFocus();
			icards[1].setAmount(1);
			icards[2].selectedWithoutFocus();
			icards[2].setAmount(1);
			effLines = 12;
		} else if (val > 12 && val <= 16) {
			icards[2].selectedWithoutFocus();
			icards[2].setAmount(2);
			effLines = 16;
		} else if (val > 16 && val <= 24) {
			icards[3].selectedWithoutFocus();
			icards[3].setAmount(1);
			effLines = 24;
		} else if (val > 24 && val <= 32) {
			icards[2].selectedWithoutFocus();
			icards[2].setAmount(1);
			icards[3].selectedWithoutFocus();
			icards[3].setAmount(1);
			effLines = 32;
		}

		dispAnalogLines 	= effLines - val;
		lineeDisp.innerHTML	= dispAnalogLines;
		checkAnalogLines();

	});
	
	YAHOO.util.Event.addListener(cbBrilines, 'change', function dbChange() {

		bris[1].deSelected();
		bris[2].deSelected();
		bris[3].deSelected();
		
		switch(this.value) {
			case '4':
				bris[1].selectedWithoutFocus();
				bris[1].setAmount(1);
			break;
			case '8':
				bris[2].selectedWithoutFocus();
				bris[2].setAmount(1);
			break;
			case '12':
				bris[1].selectedWithoutFocus();
				bris[1].setAmount(1);
				bris[2].selectedWithoutFocus();
				bris[2].setAmount(1);
			break;
			case '16':
				bris[2].selectedWithoutFocus();
				bris[2].setAmount(2);
			break;
		}
	});
	
	YAHOO.util.Event.addListener(cbPrilines, 'change', function dbChange() {

		cards[1].deSelected();
		cards[2].deSelected();
		cards[3].deSelected();
		cards[4].deSelected();
		cards[5].deSelected();
		cards[6].deSelected();		
		cards[7].deSelected();
		cards[8].deSelected();
		
		var card30, card60, card120;
		
		if (hws[3].sel || hws[4].sel) {
			card30  = cards[3];
			card60  = cards[6];
			card120 = cards[8];						
		} else {
			card30  = cards[1];
			card60  = cards[4];
			card120 = cards[7];
		}

		switch(this.value) {
			case '15':
				card30.selectedWithoutFocus();
				card30.setAmount(1);
			break;
			case '30':
				card30.selectedWithoutFocus();
				card30.setAmount(1);
			break;
			case '45':
				card60.selectedWithoutFocus();
				card60.setAmount(1);
			break;
			case '60':
				card60.selectedWithoutFocus();
				card60.setAmount(1);
			break;
			case '75':
				card30.selectedWithoutFocus();
				card30.setAmount(1);			
				card60.selectedWithoutFocus();
				card60.setAmount(1);				
			break;
			case '90':
				card30.selectedWithoutFocus();
				card30.setAmount(1);
				card60.selectedWithoutFocus();
				card60.setAmount(1);
			break;
			case '105':
				card60.selectedWithoutFocus();
				card60.setAmount(2);
			break;
			case '120':
				card120.selectedWithoutFocus();
				card120.setAmount(1);
			break;
			case '135':
				card30.selectedWithoutFocus();
				card30.setAmount(1);			
				card120.selectedWithoutFocus();
				card120.setAmount(1);				
			break;
			case '150':
				card30.selectedWithoutFocus();
				card30.setAmount(1);
				card120.selectedWithoutFocus();
				card120.setAmount(1);
			break;
			case '165':
				card60.selectedWithoutFocus();
				card60.setAmount(1);			
				card120.selectedWithoutFocus();
				card120.setAmount(1);
			break;
			case '180':
				card60.selectedWithoutFocus();
				card60.setAmount(1);			
				card120.selectedWithoutFocus();
				card120.setAmount(1);
			break;			
		}
	});		
}

function setHWListListner() {
	
	for (i=1;i<hws.length;i++) {
		YAHOO.util.Event.addListener('hwCb'+i  , 'click', hwClick);
		YAHOO.util.Event.addListener('hwInfo'+i, 'click', showHWInfo);
		YAHOO.util.Dom.get('hwCb'+i).checked = false;
	}

	function showHWInfo() { hws[this.id.charAt(6)].showInfo(); }

	function hwClick() {
		var id = this.id.charAt(4);
		hws[id].selected();

		if (id==4 || id==3 ) {
			// I Rac hanno solo Debian come possibile OS
			oss[1].hide();
			oss[2].show();
			oss[3].hide();

			// I Rac hanno solo le schede Rac
			cards[1].hide();
			cards[2].hide();
			cards[3].show();
			cards[4].hide();
			cards[5].hide();
			cards[6].show();
			cards[7].hide();
			cards[8].show();
			// BRI
			bris[1].show();
			bris[2].hide();
			bris[3].hide();
			
			cbPrilines.disabled = false;
			cbPrilines.value = "-1";
			cbBrilines.disabled = true;
			cbBrilines.value = "-1";
			cbAnalines.disabled = true;
			cbAnalines.value = "-1";
			//Hide
			icards[1].hide();
			icards[2].hide();
			icards[3].hide();
			
		} else {
			//Os
			oss[1].show();
			oss[2].show();			
			oss[3].show();
			//Schede PRI
			cards[1].show();
			cards[2].show();
			cards[3].hide();
			cards[4].show();
			cards[5].show();
			cards[6].hide();
			cards[7].show();
			cards[8].hide();
			//Schede BRI
			bris[1].show();
			bris[2].show();
			bris[3].show();
			//Analogici
			icards[1].show();
			icards[2].show();
			icards[3].show();
					
			cbPrilines.disabled = false;
			cbPrilines.value = "-1";			
			cbBrilines.disabled = false;
			cbBrilines.value = "-1";
			cbAnalines.disabled = false;
			cbAnalines.value = "-1";
			
		}
	}

}

function showInfoDialog(infos, picture) {
	var text = '';

	if (picture != null)
		text  = '<img src="images/' + picture + '" />';

	for (i=0; i<infos.length; i++)
		text += infos[i] + '<br/>';
		
	YAHOO.util.Dom.get('infoBody').innerHTML = '<p>' + text + '</p>';
	YAHOO.configurator.demo.info.show();
}

function setInfoDialog() {
	YAHOO.configurator.demo.info = new YAHOO.widget.Dialog('infoDialog',
							{ fixedcenter		  : true,
							  visible		      : false,
							  width				  : "300px",
							  height			  : "300px",
							  modal				  : true,
							  hideaftersubmit	  : false,
							  constraintoviewport : true,
							  effect  : [{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.4 }]
							});
	YAHOO.configurator.demo.info.render();
}

function setAboutDialog() {
	YAHOO.configurator.demo.about = new YAHOO.widget.Dialog('aboutDialog',
							{ fixedcenter		  : true,
							  visible		      : false,
							  width				  : "300px",
							  height			  : "300px",
							  modal				  : true,
							  hideaftersubmit	  : false,
							  constraintoviewport : true,
							  effect  : [{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.4 }]
							});
	YAHOO.configurator.demo.about.render();
	//YAHOO.util.Event.addListener('aboutButton', 'click', YAHOO.configurator.demo.about.show);
}

function startDemo() {

	result		 = YAHOO.util.Dom.get('result');
	resultIva	 = YAHOO.util.Dom.get('resultIva');
	resultTime   = YAHOO.util.Dom.get('resultTime');
	lineeDisp	 = YAHOO.util.Dom.get('lineeDisp');
	lineeDispErr = YAHOO.util.Dom.get('lineeDispErr');

	setHWListListner();
	setOSListListner();
	setCardListListner();
	setHSListListener();
	setSIPListListener();
	setPhonesListListener();
	setICardListListner();
	setBRIListListener();
	setLinesCbsListener();
	setServicesListListner();
	/*
	(function() { var tabView = new YAHOO.widget.TabView('extLinesTab');})();
	(function() { var tabView = new YAHOO.widget.TabView('interniTab');	})();
	(function() { var tabView = new YAHOO.widget.TabView('demoTabView');})();
	setInfoDialog();
	*/
	
	oss[1].hide();
	oss[2].hide();
	oss[3].hide();	
	
	cards[1].hide();
	cards[2].hide();
	cards[3].hide();
	cards[4].hide();	
	cards[5].hide();
	cards[6].hide();
	cards[7].hide();
	cards[8].hide();	

	bris[1].hide();
	bris[2].hide();
	bris[3].hide();

	icards[1].hide();
	icards[2].hide();
	icards[3].hide();

	result.innerHTML 	 = '';
	resultIva.innerHTML  = '';
	resultTime.innerHTML = '';	
}


YAHOO.util.Event.onDOMReady(startDemo);
