[].indexOf || (Array.prototype.indexOf = function(v,n){
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
});

var Quotes = function(){
	return {
		lastTxt: null,
		
		init: function(){
			Quotes.updateTypeSection(true);
		},
		
		onSelect: function(cmb) {
			switch (cmb.name) {
			case 'type':
			case 'size':
			case 'weight':
			case 'cmyk':
			case 'quantity':
			case 'location':
			case 'design':
			case 'Name':
			case 'Email':
				Quotes.updateTypeSection(false, cmb.name);
				break;
			}
		},
		
		updateTypeSection: function(init, changedId) {

			var frm = document.quote;

			var fieldOrder = ['type', 'size', 'weight', 'quantity', 'cmyk'];
			var current = fieldOrder.indexOf(changedId);
			if (current >= 0) {
				for (var i=(current+1); i<fieldOrder.length; i++) {
					frm.elements[fieldOrder[i]].selectedIndex = 0;
				}
			}

			//Set up the combos
			var selected = {
				type: Quotes.fillCombo('type', quoteConfig.Type, null, (init ? quoteData.type : null)),
				size: Quotes.fillCombo('size', quoteConfig.Size, 'type', (init ? quoteData.size : null)),
				weight: Quotes.fillCombo('weight', quoteConfig.Weight, 'size', (init ? quoteData.weight : null)),
				quantity: Quotes.fillCombo('quantity', quoteConfig.Quantity, 'weight', (init ? quoteData.quantity : null)),
				cmyk: Quotes.fillCombo('cmyk', quoteConfig.CMYK, 'quantity', (init ? quoteData.cmyk : null)),
				includeDesign: (frm.elements['design'].options[ frm.elements['design'].selectedIndex ].value == 'Yes' ? true  : false)
			}
			
			//Set the locations
			selected.location = Quotes.setLocation(selected.quantity, (init ? quoteData.location : null));
			
			//Get the 'design' state
			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
			Quotes.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 txt = '';
			if (selected.type == null) {
				txt = '<img src="/images/Quotes-Type.jpg" border="0" alt="Select Product Type" />';
			} else if (selected.size == null) {
				txt = '<img src="/images/Quotes-Size.jpg" border="0" alt="Select Size"/>';
			} else if (selected.weight == null) {
				txt = '<img src="/images/Quotes-Stock.jpg" border="0" alt="Select Stock"/>';
			} else if (selected.quantity == null) {
				txt = '<img src="/images/Quotes-Qty.jpg" border="0" alt="Select Quantity"/>';
			} else if (selected.cmyk == null) {
				txt = '<img src="/images/Quotes-Ink.jpg" border="0" alt="Select Ink"/>';
			} else if (selected.location == null) {
				txt = '<img src="/images/Quotes-Location.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/Quotes-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 {
				
				//Include the job type text
				txt = selected.type.description + '<br /><br />'; 
				
				//Include total and unit print price
				//console.log([selected, Number(selected.cmyk.print), Number(selected.quantity.text), Number(selected.cmyk.print) / Number(selected.quantity.text)]);
				var total = Number(selected.cmyk.print);
				txt += '<table border="0" cellpadding="0" cellspacing="0" width="100%">'
					 + '<tr><td nowrap="true"><b>Print price</b>:</td><td align="right">$' + Number(selected.cmyk.print).toFixed(2) + '</td></tr>'
					 + '<tr><td nowrap="true"><b>Unit price</b>:</td><td align="right">$' + (Number(selected.cmyk.print) / Number(selected.quantity.text)).toFixed(2) + '</td></tr>';
				
				//Include graphic design
				if (selected.includeDesign) {
					total += Number(selected.cmyk.design);
					txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						 + '<tr><td nowrap="true"><b>Graphic design</b>:</td><td align="right">$' + Number(selected.cmyk.design).toFixed(2) + '</td></tr>'; //$x.xx + gst
				}
				
				//Use the specified or generic delivery cost
				var deliveryPrice = Number(selected.quantity.locations[selected.location]);
				if (deliveryPrice <= 0)
					deliveryPrice = Number(quoteLocations[selected.location].quote_price);
				
				total += deliveryPrice;
				txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						+ '<tr><td nowrap="true"><b>Delivery</b>:</td><td align="right">$' + deliveryPrice.toFixed(2) + '</td></tr>';
				
				//Set up the total price
				txt += '<tr><td colspan="2">&nbsp;</td></tr>'
					 + '<tr><td nowrap="true"><b>TOTAL PRICE</b>:</td><td align="right"><b>$' + Number(total).toFixed(2) + '</b><br /> + gst</td></tr>';
				
				//Add the 'turnaround' info
				if (selected.quantity.turnaround != '') {
					txt += '<tr><td colspan="2">&nbsp;</td></tr>'
						  + '<tr><td colspan="2"><b>Printing turnaround time</b>:</td></tr>'
						  + '<tr><td colspan="2">' + selected.quantity.turnaround + '</td></tr>';
				}
				txt += '</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="height: 295px; padding: 10px;">' + txt + '</div>';
				
			}
			
			if (txt != this.lastTxt) {
				prices.innerHTML = txt;
				this.lastTxt = 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 = Quotes.init;