








var B2Widget = {

	/* Private */

	getBaseUrl : function() {
		return 'http://www.bredbandsbolaget.se/portlet-b2-orderflow/'; 
	},

	sendRequest : function(url) {
		var baseUrl = B2Widget.getBaseUrl() + 'widget';
		var script = document.createElement('script');
    	script.src = baseUrl + url;
    	document.getElementsByTagName('head')[0].appendChild(script);
	},

	setupCss : function(url) {
		var baseUrl = B2Widget.getBaseUrl() + 'widget-css/';
	    var cssElement = document.createElement('link');
	    cssElement.href = baseUrl + url;
		cssElement.rel = 'styleSheet';
		cssElement.type = 'text/css';
	
	    document.getElementsByTagName('head')[0].appendChild(cssElement);
	},

	/* Constructor */

	initialize : function() {
		B2Widget.setupCss('telenorbrand_styles.css');

	
	
		B2Widget.sendRequest('/init');
	

	},

	/* Public */

	addLoadEvent: function(func) { 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') { 
	    window.onload = func; 
	  } else { 
	    window.onload = function() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    } 
	  } 
	},

	renderView: function(content) {
		document.getElementById('b2_placeholder').innerHTML = content;
	},
 
	sendForm: function(theForm, isProgressBar) {
		var submitUrl = '/flow?';
		for (i = 0; i < theForm.length; i++) {
			if ((theForm.elements[i].nodeName.toLowerCase() == 'input' || theForm.elements[i].nodeName.toLowerCase() =='select') && theForm.elements[i].type != 'submit' && theForm.elements[i].value != '') {
				if (theForm.elements[i].nodeName.toLowerCase() == 'select') {
					var theSelect=theForm.elements[i];
					submitUrl += '&' + theSelect.name + '=' + theSelect.options[theSelect.selectedIndex].value;
				}
				else if (theForm.elements[i].type== 'checkbox' &&theForm.elements[i].checked!=true) {
					/* Do nothing if the element is an unchecked checkbox */	
				}
				else {
					submitUrl += '&' + theForm.elements[i].name + '=' + escape(theForm.elements[i].value);
				}
			}
		}
		if (isProgressBar) {
			B2Widget.renderView('<div class="b2_widget_searchinfo">Sökning pågår</div>');
		}
		B2Widget.sendRequest(submitUrl);
		return false;
	},

	toggleTabs: function(activeNumber) {
	    for (i=1; i<=2; i++) {
	        var theTab = document.getElementById('b2_widget_tab_'+i);
	        var theFieldset = document.getElementById('b2_widget_fieldset_'+i);
	        if (i == activeNumber) {
	            if (theTab!=null) {
	                theTab.style.display = '';
	            } 
	            if (theFieldset!=null) {
	                theFieldset.style.display = '';
	            }
	        } else {
	            if (theTab!=null) {
	                theTab.style.display = 'none';
	            }
	            if (theFieldset!=null) {
	                theFieldset.style.display = 'none';
	            }
	        }
	    }
	    var theInputField = document.getElementById('b2_widget_checkboxIHaveNoPhoneLine');
	    if (theInputField!=null) {
		    if (activeNumber==1) {
		        theInputField.value='false';
		   	} else {
		   		theInputField.value='true';
		   	}
		}
	},

	B2_Widget_Entrance: function(entranceNameIn) {    
	    this.entranceName = entranceNameIn; 
	    this.floors = new Array(); 
	    this.elementCounter = 1;    
	    
		this.addFloor = function(floor) {        
		    this.floors[this.elementCounter]=floor;        
		    this.elementCounter++;    
		};
	   
		this.getFloors = function() {
	    	return this.floors
		};
	   	
		this.getName = function() {
		    return this.entranceName;
		};
	},

	B2_Widget_Floor: function(floorNameIn) {
	    this.floorName = floorNameIn; 
	    this.apartmentNumbers = [];
	    this.elementCounter = 1;

	    this.addApartmentNumber = function(aptNo) {
	        this.apartmentNumbers[this.elementCounter]=aptNo;
	        this.elementCounter++;
	    };
    
	    this.getName = function() {
	        return this.floorName;
	    };
 
	    this.getApartmentNumbers = function() {
	        return this.apartmentNumbers;    
	    };
	},	

	updateFloorFromSelected: function() {
	    selectedValue = B2Widget.getSelectedValue(document.getElementById('b2_widget_floor_sel'));
	    B2Widget.updateFloorFromName(selectedValue);
	},

	updateFloorFromName: function(floorName) {
	    for (i=1; i < currentEntrance.getFloors().length;i++) {
	        if (currentEntrance.getFloors()[i].getName()==floorName) {
	            B2Widget.updateFloor(currentEntrance.getFloors()[i]);
	            break;
	        }
	    }
	},

	updateFloor: function(newFloor) {
	    aptNoOptions = '';
	    aptNos = newFloor.getApartmentNumbers();
	    var aptNoSel = document.getElementById('b2_widget_aptno_sel');
	    if (aptNoSel!=null) {
	        aptNoSel.innerHTML='';
	    }
	    for (i=1; i < aptNos.length; i++) {
	        if (aptNos[i]!='' && aptNoSel!=null) {
	            var newAptNoOpt = document.createElement('option');
	            newAptNoOpt.value = aptNos[i];
	            newAptNoOpt.innerHTML=aptNos[i];
	            aptNoSel.appendChild(newAptNoOpt);
	        }
	    }
	    currentFloor = newFloor;
	},

	updateEntranceFromSelected : function() {
	    selectedValue = B2Widget.getSelectedValue(document.getElementById('b2_widget_entrance_sel'));
	    B2Widget.updateEntranceFromName(selectedValue);
	},

	updateEntranceFromName : function(entranceName) {
	    for (i=1; i < entrances.length;i++) {
	        if (entrances[i].getName()==entranceName) {
	            return B2Widget.updateEntrance(entrances[i]);
	        }
	    }
	},

	updateEntrance : function(newEntrance) {
		floorOptions = '';
	    floors = newEntrance.getFloors();
	    var theFloorSelect = document.getElementById('b2_widget_floor_sel');
	    if (theFloorSelect!=null) {
	        theFloorSelect.innerHTML='';
	    }
	    for (i=1; i < floors.length; i++) {
	        if (floors[i].getName()!='') {
	            var currentFloor = document.createElement('option');
	            currentFloor.value = floors[i].getName();
	            currentFloor.innerHTML=floors[i].getName();
	            if (theFloorSelect!=null) {
	                theFloorSelect.appendChild(currentFloor);
	            }                
	        }
	    }
	    B2Widget.updateFloor(newEntrance.getFloors()[1]);
	    currentEntrance = newEntrance;    
	},	

	getSelectedValue : function(theSelect) {
	    if (theSelect!=null) {
	        return theSelect.options[theSelect.selectedIndex].value;
	    } else {
	        return '';
	    }
	},

	toggleVoip : function(theCheckbox) {
		if (theCheckbox.value == '0') { 
			theCheckbox.value = '1'; 
			document.getElementById('b2_widget_all_price').style.display=''; 
			document.getElementById('b2_widget_broadband_price').style.display='none';
		} 
		else { 
			theCheckbox.value = '0'; 
			document.getElementById('b2_widget_all_price').style.display='none'; 
			document.getElementById('b2_widget_broadband_price').style.display='';
		}
	}
};


B2Widget.addLoadEvent(function() { 
    B2Widget.initialize(); 
});