/**************************************************************************\

Filename: storage_solutions.js
Version: 1.2
Author: Ian Nielsen
Date: 25 June 2010

The file contains the JS functions for the Storage Solutions section.

Dependancies
------------

This script is dependant on the following JS files / libraries

common.js - $fh library functions
slider_controls.js - slider functions

/**************************************************************************/
var storageSolution = function() {
	
	// function-wide variables
	var arPriceList = [[1,1]];
	var country = 'UK';
	var cSymbol = '&pound;';
	var cMonthly = 'pm';
	var cYearly = 'pa';
	
	this.setPricing = function(ar) {
		// Sets the pricing used when calculating prices
		arPriceList = ar;
	};
	
	this.location = function(c,s,m,y) {
		country = c;
		cSymbol = s;
		cMontly = m;
		cYearly = y;
	};

	this.addStorage = function(steps,unitSize,currentQuota,unitType) {
		// JS for adding a storage solution
		
		// show the price box
		$fh.fetch('outputPrice').style.display = 'block';
	
		// setup a blank function to prevent errors caused by the slider controls error handler
		var debug = function() { $fh.fetch('testOutput').innerHTML = val; };
		
		var Methods = {
			
			updatePrice : function() {
				// outputs the price
		
				// local variables
				var val = parseInt($fh.fetch('hdd_val').value);
				var o = $fh.fetch('outputPrice');
				var rdPeriod = document.getElementsByName('contract');
				var dN = 0;
				var fN = 0;
				var pN = '';
				
				// find the selected value
				for(i = 0; i < arPriceList.length; i++) {
					
					if(val == parseFloat(arPriceList[i][2])) {
						// loop through the radio buttons
						
						for(n = 0; n < rdPeriod.length; n++) {
							
							// which one of the radio buttons is checked?
							if(rdPeriod[n].checked) {
								switch(rdPeriod[n].value) {
									case 'MONTHLY':
										dN = arPriceList[i][1];
										pN = cMonthly;
										break;
									case 'YEARLY':
										dN = arPriceList[i][0];
										pN = cYearly;
										break;
								};			
							};
							
						};
						
					};
					
				};
				
				// re-assign on click events
				for(i = 0; i < rdPeriod.length; i++) {
					rdPeriod[i].onclick = function() {
						Methods.updatePrice();
					};
				};
				
				// prep the price for display
				fN = ((dN - Math.floor(dN)).toFixed(2));
				dN -= fN;
						
				// output the price
				o.innerHTML = cSymbol + dN+ '<span>' + fN.substr(1) + '<em>' + pN + '</em>' + '</span>';
			},
			
			submitForm : function() {
				var oVal = document.getElementById('hdd_val');
				var arValues = [['ONLINESTORAGE_50GB',50],['ONLINESTORAGE_100GB',100],['ONLINESTORAGE_250GB',250],['ONLINESTORAGE_500GB',500],['ONLINESTORAGE_1TB',1024],['ONLINESTORAGE_2TB',2048]];
				var isOK = false;
				
				// add the appropriate form element 
				var oForm = $fh.fetch('storageForm');
				var oOutput = document.createElement('input');
				oOutput.setAttribute('id', 'package');
				oOutput.setAttribute('name', 'package');
				oOutput.setAttribute('type', 'hidden');
				oForm.appendChild(oOutput);
		
				// select the appropriate value				
				for(i = 0; i < arValues.length; i++) {
					if(oVal.value == arValues[i][1]) {
						oVal.value = arValues[i][0];
						oOutput.value =  arValues[i][0];
						isOK = true;
					};
				};
				return isOK;
			}
		};
		
		// set up the slider
		oSliders = new sliderControls([ ['hdd',steps, unitSize, currentQuota, unitType, 'storageSlider', currentQuota,1] ]);
		oSliders.config.setCursorAdjust(45);
		oSliders.config.setContainer('storageForm');
		oSliders.config.setOffsetRelativeTo('storageconfig');	
		oSliders.config.setPadding(6);
		oSliders.config.setSliderClass('vpsSliderScale');
		oSliders.config.setCursorClass('vpsSliderCursor');
		oSliders.config.setIndicateCurrentClass('vpsSliderNew');
		oSliders.config.setGhostClass('vpsSliderGhost');
		oSliders.config.setIndicateExistingClass('vpsSliderExisting');
		oSliders.config.setIndicateUpgradeClass('vpsSliderTemp');
		oSliders.config.setIndicateWarningClass('vpsSliderWarning');
		oSliders.config.setGhosts(false);
		oSliders.config.setIndicateExisting(false);
		oSliders.config.setIndicateUpgrade(false);
		oSliders.config.setIndicateWarning(false);
		oSliders.config.setAllowDowngrade(true);
		oSliders.config.setAnimatePreset(true);
		oSliders.onUpdate( Methods.updatePrice );
		oSliders.init();
		
		// setup the pricing for the initial state
		Methods.updatePrice();
		document.getElementById('storageForm').onsubmit = Methods.submitForm;
	};
	
};
