var sisp;
sisp = (function() {
	var toggle, showElement, hideElement, trim, amountValue, merchantRef, taxRate, qtyValue, subValue, taxValue, /*
	 */
	totalValue, quantity, costDiv, displayAmount, displaySub, displayTax, displayTotal, subTotal, tax, total, /*
	 */
	amount, courseSelector, setOrder, roundUp, displayCurrency, updateAmount, updateOrder, goDeposit, goDPS, validate;
	
	// Registration order processing
	amountValue = 0.0;
	merchantRef = "[NO ORDER]";
	taxRate = 15.00;
	qtyValue = null;
	subValue = 0;
	taxValue = 0;
	totalValue = 0;
	quantity = null;
	costDiv = null;
	displayAmount = null;
	displaySub = null;
	displayTax = null;
	displayTotal = null;
	subTotal = null;
	tax = null;
	total = null;
	amount = null;
	courseSelector = null;
	
	// Show/Hide an element
	toggle = function(element) {
		var obj = document.getElementById(element);
		if (obj.style.display !== 'none') {
			obj.style.display = 'none';
		}
		else {
			obj.style.display = '';
		}
	};
	
	// Show an element on the page
	showElement = function(element) {
		var obj = document.getElementById(element);
		obj.className = 'visible';
	};
	
	// Hide an element on the page 
	hideElement = function(element) {
		var obj = document.getElementById(element);
		obj.className = 'hidden';
	};
	
	// Remove leading and training spaces from a string
	trim = function(str) {
		str = str.replace(/(^\s+)|(\s+$)/g, '');
		return str;
	};
	
	setOrder = function() {
		quantity = document.getElementById('quantity');
		displayAmount = document.getElementById('displayAmount');
		displaySub = document.getElementById('displaySub');
		displayTax = document.getElementById('displayTax');
		merchantRef = document.getElementById('merchantreference');
		displayTotal = document.getElementById('displayTotal');
		subTotal = document.getElementById('subTotal');
		tax = document.getElementById('tax');
		total = document.getElementById('total');
		amount = document.getElementById('amount');
		quantity.value = '';
		displayAmount.innerHTML = '$' + displayCurrency(amountValue);
		amount.value = amountValue;
		courseSelector = document.getElementById('courseTypeList');
		
		updateOrder(quantity);
	};
	
	roundUp = function(data, points) {
		var factor = Math.pow(10, points);
		return Math.round(data * factor) / factor;
	};
	
	displayCurrency = function(data) {
		var dt = data.toString(10);
		var point = dt.indexOf('.');
		if (point < 0) {
			dt += '.00';
		}
		else {
			var integer = dt.substring(0, point);
			var decimal = dt.substring(point + 1);
			decimal = (decimal.length < 2) ? decimal + '0' : decimal;
			dt = integer + '.' + decimal;
		}
		return dt;
	};
	
	updateAmount = function(input) {
		switch (input.value) {
			case "si":
				amountValue = 63.0;
				amount.value = 63.0;
				merchantRef.value = "Store Induction";
				break;
			case "fh":
				amountValue = 45.0;
				amount.value = 45.0;
				merchantRef.value = "Food Handling";
				break;
			case "si_fh":
				amountValue = 100.0;
				amount.value = 100.0;
				merchantRef.value = "Store Induction + Food Handling";
				break;
			default:
				amountValue = 0;
				amount.value = 0;
				merchantRef.value = "[NO ORDER]";
				break;
		}
		
		displayAmount.innerHTML = '$' + displayCurrency(amountValue);
		updateOrder(quantity);
	};
	
	updateOrder = function(input) {
		qty = input.value;
		if (!isNaN(qty) && qty.indexOf('.') < 0 && qty.length > 0 && qty > 0) {
			qtyValue = qty;
		}
		else if (qty.length > 0) {
			qtyValue = (qty > 0 || isNaN(qty)) ? qtyValue : 1;
			quantity.value = qtyValue;
		}
		else {
			qtyValue = 0;
		}
		
		subValue = qtyValue * amountValue;
		taxValue = roundUp((subValue * taxRate / 100), 2);
		totalValue = roundUp(subValue + taxValue, 2);
		displaySub.innerHTML = '$' + displayCurrency(subValue);
		displayTax.innerHTML = '$' + displayCurrency(taxValue);
		displayTotal.innerHTML = '$' + displayCurrency(totalValue);
		subTotal.value = displayCurrency(subValue);
		tax.value = displayCurrency(taxValue);
		total.value = displayCurrency(totalValue);
		amount.value = displayCurrency(amountValue);
	};
	
	goDeposit = function(form) {
		var frm = form;
		if (validate(frm)) {
			frm.action = '/public/include/scripts/processDeposit.asp';
			frm.submit();
		}
		else {
			return false;
		}
	};
	
	goDPS = function(form) {
		var frm = form;
		if (validate(frm)) {
			frm.action = '/public/include/scripts/processDPS.asp';
			form.submit();
		}
		else {
			return false;
		}
	};
	
	// Form validation script v2.12
	// Validate function scans for input fields with matching [fieldname].validate hidden fields
	// Value argument of the hidden field is used for the error feedback message
	// Alt argument is used for [text/numeric/email/password]; [focus/select/none];
	//                          [predecessor]; [predecessor type]
	// Size argument used to set the minimum characters required
	//      *Set as 1 for default any size
	
	validate = function(form) {
		for (var i = 0; i < form.length; i += 1) {
			var obj = form.elements[i];
			if (obj.type != 'hidden') {
				var objName = obj.name;
				var vObj = form[objName + '.validate'];
				// Does validation data exist
				if (vObj) {
					var isError = false;
					var message = vObj.value.replace(/\\n/g, '\n');
					var minSize = vObj.size;
					if (minSize < 2) {
						minSize = 0;
					}
					var alt = vObj.alt;
					var actions = [];
					var count = 0;
					var index1 = 0;
					var index2 = alt.indexOf('\;');
					while (index2 >= 0) {
						actions[count] = alt.substring(index1, index2);
						index1 = index2 + 1;
						index2 = alt.indexOf('\;', index1);
						count++;
					}
					actions[count] = alt.substring(index1, alt.length);
					vType = (actions[0]) ? actions[0] : 'text';
					sType = (actions[1]) ? actions[1] : 'focus';
					pred = (actions[2]) ? actions[2] : '';
					pType = (actions[3]) ? actions[3] : '';
					var doCheck = true;
					if (pred !== '') {
						if (pType === 'display') { // Check object have display set to visible
							var styleProperty;
							if (document.getElementById) {
								styleProperty = document.getElementById(pred).style;
							}
							else if (document.all) {
								styleProperty = document.all[pred].style;
							}
							else if (document.layers) {
								styleProperty = document.layers[pred];
							}
							if (styleProperty.display === 'none') {
								doCheck = false;
							}
						}
						if (pType === 'compare') { // Compare fields
							if (obj.value !== form[pred].value) {
								isError = true;
								if (vType === 'password') {
									obj.value = '';
									form[pred].value = '';
									obj = form[pred];
								}
							}
						}
						if (pType.substr(0, 1) === '=') { // Check variable equal to value
							var testValue = pType.substr(1);
							if (this[pred] !== testValue) {
								doCheck = false;
							}
						}
					}
					if (doCheck) {
						// Trim leading/trailing spaces
						var value = obj.value.replace(/(^\s+)|(\s+$)/g, '');
						// Check if any value has been entered
						if (value === '') {
							isError = true;
						}
						// Check if value is numeric only > 0
						if (vType === 'numeric' && isNaN(value)) {
							isError = true;
							message += '\nThis is a numeric field.';
						}
						else if (vType === 'numeric' && value < 1) {
							isError = true;
							message += '\nYou must supply a value greater than zero.';
						}
						if (value.length < minSize && minSize > 0) {
							isError = true;
							message += '\nYou must enter a minimum of ' + minSize + ' characters.';
						}
						// Check if value is a valid email address
						var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						if (vType === 'email' && !pattern.test(value)) {
							isError = true;
						}
						// Check if checkbox is checked
						if (vType === 'checkbox' && !obj.checked) {
							isError = true;
						}
						// Check if is valid radio selection
						if (obj.type === 'radio') {
							sType = 'none';
							obj = form[obj.name];
							isError = true;
							for (var j = 0; j < obj.length; j += 1) {
								if (obj[j].checked) {
									isError = false;
								}
							}
						}
						// Process errors
						if (isError) {
							alert(message);
							if (sType === 'select') {
								obj.select();
							}
							else if (sType !== 'none') {
								obj.focus();
							}
							return false;
						}
					}
				}
			}
		}
		return true;
	};
	
	return {
		setOrder: setOrder,
		updateAmount: updateAmount,
		updateOrder: updateOrder,
		goDeposit: goDeposit,
		goDPS: goDPS
	};
})();

