var Postcards = function(){
	return {
		init: function(){
			Postcards.updateTypeSection(true);
		},
		
		onSelect: function(cmb) {
			switch (cmb.name) {
			case 'size':
			case 'quantity':
			case 'distribution':
			case 'location':
			case 'design':
			case 'Name':
			case 'Email':
				Postcards.updateTypeSection(false, (cmb.name == 'size' ? true : false));
				break;
			}
		},
		
		updateTypeSection: function(init, reset) {
			//Selected object
			var selected = {
						size: null,
						quantity: null,
						distribution: null,
						location: null
					};

			//Set up the combos
			var frm = document.quote;
			var cmbSize = frm.elements['size'];
			var cmbQuantity = frm.elements['quantity'];
			var cmbDistribution = frm.elements['distribution'];
			var cmbLocation = frm.elements['location'];

			//Set up the size
			var selectedSize = cmbSize.options.selectedIndex;
			cmbSize.options.length = 0; var i = 1;
			cmbSize.options[0] = new Option("< Select >", "");
			for (var key in quoteConfig) {
				cmbSize.options[i] = new Option(quoteConfig[key].size, key);
				if ((i == selectedSize) || ((init == true) && (quoteData['size'] == Number(key)))) {
					selected.size = quoteConfig[key];
					selectedSize = i;
				}
				i++;
			}
			if (selectedSize != -1) cmbSize.selectedIndex = selectedSize;
			
			//Set up the quantity, distribution and location
			var selectedQuantity = (reset ? 0 : cmbQuantity.selectedIndex);
			var selectedDistribution = (reset ? 0 : cmbDistribution.selectedIndex);

			//Make sure the quantity is not less than the distribution amount
			if (cmbDistribution.selectedIndex >= 0) {
				distribution = Number(cmbDistribution.options[cmbDistribution.selectedIndex].value);
				quantity = Number(cmbQuantity.options[cmbQuantity.selectedIndex].value);
				if (distribution > quantity) {
					selectedQuantity = cmbDistribution.selectedIndex-1;
				}
			}

			//Reset the combos
			cmbQuantity.options.length = cmbDistribution.options.length  = 0;
			cmbQuantity.options[0] = new Option("< Select >", "");
			cmbDistribution.options[0] = new Option("< Select >", "");

			if (selected.size != null) {
				//Set up the quantities
				for (var j=0; j<selected.size.quantities.length; j++) {
					cmbQuantity.options[j+1] = new Option(selected.size.quantities[j].quantity, selected.size.quantities[j].quantity);
					if ((j == (selectedQuantity-1)) || ((init == true) && (quoteData['quantity'] == Number(selected.size.quantities[j].quantity)))) {
						selectedQuantity = j+1;
						selected.quantity = {quantity: Number(selected.size.quantities[j].quantity), price: Number(selected.size.quantities[j].print) };
					}
				}

				//Set up the distributions
				cmbDistribution.options[1] = new Option(0, 0);
				for (var j=0; j<selected.size.quantities.length; j++) {
					cmbDistribution.options[j+2] = new Option(selected.size.quantities[j].quantity, selected.size.quantities[j].quantity);

					if ((selectedDistribution == 1) || ((init == true) && (quoteData['distribution'] == 0))) { //Zero distribution
						selectedDistribution = 1;
						selected.distribution = {quantity: 0, price: 0 };
					} else if ((j == (selectedDistribution-2)) || ((init == true) && (quoteData['distribution'] == Number(selected.size.quantities[j].quantity)))) {
						selectedDistribution = j+2;
						selected.distribution = {quantity: Number(selected.size.quantities[j].quantity), price: Number(selected.size.quantities[j].distribution) };
					}
				}
			}

			if (selectedQuantity >= 0) cmbQuantity.selectedIndex = selectedQuantity;
			if (selectedDistribution >= 0) cmbDistribution.selectedIndex = selectedDistribution;

			//Set up the locations
			var selectedLocation = cmbLocation.options.selectedIndex;
			cmbLocation.options.length = 0; var i = 1;
			cmbLocation.options[0] = new Option("< Select >", "");
			for (var key in quoteLocations) {
				cmbLocation.options[i] = new Option(quoteLocations[key], key);
				if ((i == selectedLocation) || ((init == true) && (quoteData['location'] == Number(key)))) {
					selected.location = Number(key);//quoteLocations[key];
					selectedLocation = i;
				}
				i++;
			}
			if (selectedLocation != -1) cmbLocation.selectedIndex = selectedLocation;
			
			//Set the design required yes/no
			switch (frm.elements['design'].options[ frm.elements['design'].selectedIndex ].value) {
			case '':
				selected.design = null;
				break;
			case 'Yes':
				selected.design = true;
				break;
			case 'No':
				selected.design = false;
				break;
			}
			
			//Set the name/email
			selected.name = (frm.Name ? frm.Name.value : null);
			selected.email = (frm.Email ? frm.Email.value : null);

			//Set the price
			Postcards.setPrice(selected);
			
			//Set the help field
			/*
			var el = document.getElementById('helpWeight');
			if (selected.weight == null) {
				el.innerHTML = '';
			} else {
				el.innerHTML = '<a href="#" class="tt"><img src="/images/help.gif" border="0" /><span class="tooltip"><span class="top"></span><span class="middle">' + selected.weight.description + '</span><span class="bottom"></span></span></a>';
			}
			*/
		},
		setPrice: function(selected) {
			//Set the appropriate text
			var prices = document.getElementById('prices');
			var frm = document.quote;
			var total = 0, unitPrice = 0, unitQuantity = 0;
			
			var txt = '';
			if (selected.size == null) {
				txt = '<img src="/images/Postcards-Size.jpg" border="0" alt="Select Size"/>';
			} else if (selected.quantity == null) {
				txt = '<img src="/images/Web-Postcard-QTY.jpg" border="0" alt="Select Quantity"/>';
			} else if (selected.distribution == null) {
				txt = '<img src="/images/Web-Postcard-disto.jpg" border="0" alt="Select Distribution"/>';
			} else if (selected.location == null) {
				txt = '<img src="/images/Postcards-Details.jpg" border="0" alt="Select Location"/>';
			} else if ((selected.name != null) && (selected.name == '')) {
				txt = '<img src="/images/Web-Name.jpg" border="0" alt="Enter Member Name"/>';
			} else if ((selected.email != null) && ((selected.email == '') || (selected.email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi) == null))) {
				txt = '<img src="/images/Web-Email.jpg" border="0" alt="Enter Member Email Address"/>';
			} else if (selected.design == null) {
				txt = '<img src="/images/Postcards-Design.jpg" border="0" alt="Select Design"/>';
			} else if (frm.submitted.value != 'Y') {
				txt = '<img src="/images/Web-submit.jpg" border="0" alt="Submit your details"/>';
			} else {
				
				txt = '<table border="0" cellpadding="0" cellspacing="0" width="100%">'
					 + '<tr><td colspan="2">' + selected.size.size + '</td></tr>'
					 + '<tr><td colspan="2">&nbsp;</td></tr>'

				//Include print cost
				if (selected.quantity) {
					unitPrice += Number(selected.quantity.price);
					unitQuantity += Number(selected.quantity.quantity);
					total += Number(selected.quantity.price);
					txt += '<tr><td><b>Print Price</b>:</td><td>$' + Number(selected.quantity.price).toFixed(2) + '</td></tr>'; //$x.xx + gst
				}

				//Include distribution cost
				if (selected.distribution) {
					unitPrice += Number(selected.distribution.price);
					unitQuantity += Number(selected.distribution.quantity);
					total += Number(selected.distribution.price);
					txt += '<tr><td><b>Distribution Price</b>:</td><td>$' + Number(selected.distribution.price).toFixed(2) + '</td></tr>'; //$x.xx + gst
				}
				
				/*
				txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						+ '<tr><td><b>Unit Price</b>:</td><td>$' + Number(unitPrice / unitQuantity).toFixed(2) + '</td></tr>'; //$x.xx + gst
				*/
				
				//Include delivery cost
				if ((selected.distribution ==null) || (Number(selected.quantity.quantity) > Number(selected.distribution.quantity))) {
					var location = quoteLocations[selected.location];
					var delivery = selected.size.locations[selected.location];

					total += Number(delivery);
					txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						 + '<tr><td><b>Delivery Price</b>:</td><td>$' + Number(delivery).toFixed(2) + '</td></tr>'; //$x.xx + gst
				}

				//Include graphic design
				if (selected.design) {
					total += Number(selected.size.design);
					txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						 + '<tr><td><b>Graphic design price</b>:</td><td>$' + Number(selected.size.design).toFixed(2) + '</td></tr>'; //$x.xx + gst
				}

				//Set up the total price
				txt += '<tr><td colspan="2">&nbsp;</td></tr>'
					 + '<tr><td><b>TOTAL PRICE</b>:</td><td><b>$' + Number(total).toFixed(2) + '</b></td></tr>'
					 + '<tr><td></td><td align="right"> + gst</td></tr>'
					 + '</table>';
				
				txt += '<br /><div align="center"><input type="submit" name="submit_order" value="Order and Pay Online" /></div></div>';
				
				txt = '<div class="EntryForm" align="left" style="padding: 10px; width:180px; height:270px;">' + txt + '</div>';
				
			}
			prices.innerHTML = txt;
			
		},
		setLocation: function(quantity, selected) {
			var result = null;
			
			var frm = document.quote;
			var cmb = frm.elements['location'];
			var selectedIndex = cmb.selectedIndex;
			cmb.options.length = 0;

			if (quantity != null) {
				cmb.options[0] = new Option('< select >', '');
				var index = 1;
				for (var k in quantity.locations) {
					if (quoteLocations[k]) {
						cmb.options[index] = new Option(quoteLocations[k].name, k);
						if ((index == selectedIndex) || ((selectedIndex == -1) && (Number(k) == selected))) {
							cmb.selectedIndex = index;
							result = k;
						}
						index++;
					}
				}
			}
			
		return result;
		},
		fillCombo: function(cmbName, data, parent, selected) {
			var result = null;
			var frm = document.quote;
			var cmb = frm.elements[cmbName];			
			var selectedIndex = cmb.selectedIndex;

			//Determine the parent filter
			var parentId = null;
			var cmbParent = (parent != null ? frm.elements[parent] : null);			
			if (cmbParent != null) {
				if (cmbParent.selectedIndex <= 0) {
					cmb.options.length = 0;
					return null;
				} else {
					parentId = Number(cmbParent.options[cmbParent.selectedIndex].value);
				}
			}

			//Fill the combo
			cmb.options.length = 0;
			cmb.options[0] = new Option('< select >', '');
			var index = 1;
			for (var k in data) {
				if ((parentId == null) || (Number(data[k].parent) == parentId)) {					
					cmb.options[index] = new Option(data[k].text, Number(k));
					
					if ((index == selectedIndex) || ((selectedIndex == -1) && (k == selected))) {
						cmb.selectedIndex = index;
						result = data[k];
					}
					
					index++;
				}
			}
			
		return result;
		}
		
	};
}();

window.onload = Postcards.init;