document.write('<script type="text/javascript" src="/js/getHTTPObject.js"></script>');
APP = function () {
	// INITIAL SETUP FUNCTION
		this.init = function() {
			this.setText('c-s-count', sizes.selected.length);
			this.setText('c-mm-count', maincanopy.selected.length);
			this.setText('c-mmo-count', this.totalModels('maincanopy'));
			this.setText('c-mmin-num', maincanopy.minSize);
			this.setText('c-mmax-num', maincanopy.maxSize);
			this.setText('c-rm-count', reservecanopy.selected.length);
			this.setText('c-rmo-count', this.totalModels('reservecanopy'));
			this.setText('c-rmin-num', reservecanopy.minSize);
			this.setText('c-rmax-num', reservecanopy.maxSize);
			this.selectBox = this.getItem('c-select-box');
			this.selectBoxOpen = false;
			this.contentBox = this.getItem('c-select-box-content');
			this.submitButton = this.getItem('c-submit');
		}
	// SIZES FUCTIONS
		this.showSizes = function() {
			if(this.selectBoxOpen) this.closeSelectBox();
			var t = '';
			var title, id, opened;
			this.openSelectBox('c-s');
			t += '<h3>Container Sizes</h3>';
			for(var i=0; i < sizes.list.length; i++) {
				id = sizes.list[i];
				title = sizes['size' + id].title;
				opened = sizes['size' + id].opened;
				t += '<p><label for="c-id-' + id + '"><input onclick="app.changeSize(this, ' + id + ');" type="checkbox" id="c-id-' + id + '" name="id" value="' + id + '"' + ((opened) ? ' checked="checked"' : '') + ' /> ' + title + '</label></p>';
			}
			t += this.getSelectOptions('Size', '');
			this.contentBox.innerHTML = this.createForm(t);
		}
		this.selectSizeToggle = function(_type, _on) {
			var f = document.ids.id;
			for(var i=0; i < sizes.list.length; i++) {
				f[i].checked = _on;
				sizes['size' + sizes.list[i]].opened = _on;
			}
			this.updateSizeInfo();
		}
		this.changeSize = function(_obj, _id) {
			sizes['size' + _id].opened = _obj.checked;
			this.updateSizeInfo();
		}
		this.updateSizeInfo = function() {
			sizes.selected = [];
			for(var i=0; i < sizes.list.length; i++) if(sizes['size' + sizes.list[i]].opened) sizes.selected[sizes.selected.length] = sizes.list[i];
			this.setText('c-s-count', sizes.selected.length);
			this.pushUpdate();
		}
	// MANUFACTURER FUCTIONS
		this.showManufacturers = function(_obj, _type) {
			if(this.selectBoxOpen) this.closeSelectBox();
			var o = eval(_type);
			var t = '';
			var title, id, opened;
			this.openSelectBox(_obj);
			t += '<h3>' + ((_type == 'maincanopy') ? 'Main': 'Reserve') + ' Manufacturers</h3>';
			for(var i=0; i < o.list.length; i++) {
				id = o.list[i];
				title = o['manufacturer' + id].title;
				opened = o['manufacturer' + id].opened;
				t += '<p><label for="c-id-' + id + '"><input onclick="app.changeManufacturer(this, ' + id + ', \'' + _type + '\');" type="checkbox" id="c-id-' + id + '" name="id" value="' + id + '"' + ((opened) ? ' checked="checked"' : '') + ' /> ' + title + '</label></p>';
			}
			t += this.getSelectOptions('Manufacturer', _type);
			this.contentBox.innerHTML = this.createForm(t);
		}
		this.selectManufacturerToggle = function(_type, _on) {
			var o = eval(_type);
			var f = document.ids.id;
			for(var i=0; i < o.list.length; i++) {
				f[i].checked = _on;
				o['manufacturer' + o.list[i]].opened = _on;
			}
			this.updateManufacturerInfo(_type);
		}
		this.changeManufacturer = function(_obj, _id, _type) {
			var o = eval(_type);
			o['manufacturer' + _id].opened = _obj.checked;
			this.updateManufacturerInfo(_type);
		}
		this.updateManufacturerInfo = function(_type) {
			var o = eval(_type);
			var l = _type.substr(0, 1);
			o.selected = [];
			for(var i=0; i < o.list.length; i++) if(o['manufacturer' + o.list[i]].opened) o.selected[o.selected.length] = o.list[i];
			this.setText('c-' + l + 'm-count', o.selected.length);
			this.setText('c-' + l + 'mo-count', this.totalModels(_type));
			this.pushUpdate();
		}
	// MODEL FUCTIONS
		this.showModels = function(_obj, _type) {
			if(this.selectBoxOpen) this.closeSelectBox();
			var o = eval(_type);
			var t = '';
			var i, m, s, title, id, mods, opened, mtitle, mid, mopened;
			this.openSelectBox(_obj);
			t += '<h3>' + ((_type == 'maincanopy') ? 'Main': 'Reserve') + ' Models</h3>';
			for(i=0; i < o.list.length; i++) {
				id = o.list[i];
				opened = o['manufacturer' + id].opened;
				if(opened) {
					s = '';
					title = o['manufacturer' + id].title;
					mods = o['manufacturer' + id].models;
					for(m=0; m<mods.length; m++) {
						mid = mods[m];
						mtitle = models['model' + mid].title;
						mopened = models['model' + mid].opened;
						s += '<p><label for="item' + mid + '"><input id="item' + mid + '" onclick="app.changeModel(this, ' + mid + ', \'' + _type + '\');" type="checkbox" name="id" value="' + mid + '"' + ((mopened) ? ' checked="checked"' : '') + ' /> ' + mtitle + '</label></p>';
					}
					if(s.length) t += '<h4>' + title + '</h4>' + s;
				}
			}
			t += this.getSelectOptions('Model', _type);
			this.contentBox.innerHTML = this.createForm(t);
		}
		this.selectModelToggle = function(_type, _on) {
			var o = eval(_type);
			var f = document.ids.id;
			var i, m, id, opened, mods, mid;
			for(i=0; i < o.list.length; i++) {
				id = o.list[i];
				opened = o['manufacturer' + id].opened;
				if(opened) {
					mods = o['manufacturer' + id].models;
					for(m=0; m<mods.length; m++) {
						mid = mods[m];
						this.getItem('item' + mid).checked = _on;
						models['model' + mid].opened = _on;
					}
				}
			}
			this.updateModelInfo(_type);
		}
		this.changeModel = function(_obj, _id, _type) {
			models['model' + _id].opened = _obj.checked;
			this.updateModelInfo(_type);
		}
		this.updateModelInfo = function(_type) {
			var o = eval(_type);
			var l = _type.substr(0, 1);
			var i, m, id, opened, mods, mid;
			for(i=0; i < o.list.length; i++) {
				id = o.list[i];
				opened = o['manufacturer' + id].opened;
				if(opened) {
					mods = o['manufacturer' + id].models;
					o['manufacturer' + id].selected = [];
					for(m=0; m<mods.length; m++) {
						mid = mods[m];
						if(models['model' + mid].opened) o['manufacturer' + id].selected[o['manufacturer' + id].selected.length] = mid;
					}
				}
			}
			this.setText('c-' + l + 'm-count', o.selected.length);
			this.setText('c-' + l + 'mo-count', this.totalModels(_type));
			this.pushUpdate();
		}
		this.totalModels = function(_type) {
			var n = 0;
			var o = eval(_type);
			for(var i=0; i < o.selected.length; i++) n += o['manufacturer' + o.selected[i]].selected.length;
			return n;
		}
		this.getModelsList = function(_type) {
			var n = [];
			var o = eval(_type);
			var i, id;
			for(i=0; i < o.list.length; i++) {
				id = o.list[i];
				if(o['manufacturer' + id].opened) n = n.concat(o['manufacturer' + id].selected);
			}
			return n.toString();
		}
	// SIZE
		this.showSize = function(_obj, _type, _option) {
			if(this.selectBoxOpen) this.closeSelectBox();
			var o = eval(_type);
			var t = '';
			var c = 0;
			var title, id, opened;
			var v = o[_option + 'Size'];
			this.openSelectBox(_obj);
			t += '<h3>' + ((_type == 'maincanopy') ? 'Main': 'Reserve') + ' ' + ((_option == 'min') ? 'Minimum': 'Maximum') + ' Size</h3>';
			t += '<p>';
				t += '<select name="size" class="forminput" onchange="app.updateSize(this.value, \'' + _obj + '\', \'' + _type + '\', \'' + _option + '\')">';
					while(c <= 300) {
						t += '<option value="' + c + '"' + ((c == v) ? ' selected="selected"' : '') + '>' + c + '</value>';
						c += 5;
					}
				t += '</select>';
			t += '</p>';
			t += '<p class="backbutton"><a href="javascript:app.v();" onclick="app.closeSelectBox();">Close</a></p>';
			this.contentBox.innerHTML = this.createForm(t);
		}
		this.updateSize = function(_value, _obj, _type, _option) {
			var o = eval(_type);
			var e = '';
			if(_option == 'min') {
				if(Number(_value) > o.maxSize) e = 'The minimum size must be less than the maximum.';
			} else {
				if(Number(_value) < o.minSize) e = 'The maximum size must be larger than the minimum.';
			}
			if(e.length) {
				document.ids.size.value = o[_option + 'Size'];
				alert(e);
			} else {
				o[_option + 'Size'] = _value;
				this.setText(_obj + '-num', _value);
				this.pushUpdate();
			}
		}
	// SELECT BOX
		this.openSelectBox = function(_obj) {
			var i = this.getItem(_obj);
			this.contentBox.innerHTML = '&nbsp;';
			this.selectBox.className = 'c-select-box-open';
			this.selectBox.style.top = this.getTop(i)-3 + 'px';
			this.selectBox.style.left = this.getLeft(i)-235 + 'px';
			this.selectBoxOpen = true;
		}
		this.closeSelectBox = function() {
			this.selectBox.className = 'c-select-box-closed';
			this.selectBoxOpen = false;
		}
		this.getSelectOptions = function(_typeTitle, _type) {
			return '<p class="backbutton"><a href="javascript:app.v();" onclick="app.select' + _typeTitle + 'Toggle(\'' + _type + '\', true);">All</a> | <a href="javascript:app.v();" onclick="app.select' + _typeTitle + 'Toggle(\'' + _type + '\', false);">None</a> | <a href="javascript:app.v();" onclick="app.closeSelectBox();">Close</a></p>';
		}
	// SUBMIT INFO
		this.updateInformation = function() {
			var req = 'action=update';
			this.closeSelectBox();
			req += '&containersizes=' + sizes.selected;
			req += '&mainmanufacturers=' + maincanopy.selected;
			req += '&mainmodels=' + this.getModelsList('maincanopy');
			req += '&mainminsize=' + maincanopy.minSize;
			req += '&mainmaxsize=' + maincanopy.maxSize;
			req += '&reservemanufacturers=' + reservecanopy.selected;
			req += '&reservemodels=' + this.getModelsList('reservecanopy');
			req += '&reserveminsize=' + reservecanopy.minSize;
			req += '&reservemaxsize=' + reservecanopy.maxSize;
			this.submitButton.className = 'formbutton';
			httpobj.makeRequest('/sizing-guide/controls/update.cfm', req, this.handleUpdateInformation);
			return false;
		}
		this.handleUpdateInformation = function(re) {
			processXML(re);
		}
	// LINK INFO
		this.getLink = function() {
			var req = '/sizing-guide/?';
			req += 'containersizes=' + sizes.selected;
			req += '&mainmanufacturers=' + maincanopy.selected;
			req += '&mainmodels=' + this.getModelsList('maincanopy');
			req += '&mainminsize=' + maincanopy.minSize;
			req += '&mainmaxsize=' + maincanopy.maxSize;
			req += '&reservemanufacturers=' + reservecanopy.selected;
			req += '&reservemodels=' + this.getModelsList('reservecanopy');
			req += '&reserveminsize=' + reservecanopy.minSize;
			req += '&reservemaxsize=' + reservecanopy.maxSize;
			self.location = req;
		}
	// HELPER FUNCTIONS
		this.pushUpdate = function() {
			this.submitButton.className = 'formbutton-highlight';
		}
		this.createForm = function(_txt) {
			return '<form name="ids" action="##" onsubmit="return app.updateInformation();">' + _txt + '</form>';
		}
		this.getItem = function(_obj) {
			var obj = (document.getElementById(_obj) != undefined) ? document.getElementById(_obj) : false;
			if (!obj && !obj.style) obj.style = obj;
			return obj;
		}
		this.setText = function(_obj, _txt) {
			var i = this.getItem(_obj);
			if (i) i.innerHTML = _txt;
		}
		this.getLeft = function(_obj) {
			var leftPos = _obj.offsetLeft;
			var ep = _obj.offsetParent;
			while (ep != null) {
				leftPos += ep.offsetLeft;
				ep = ep.offsetParent;
			}
			return leftPos;
		}
		this.getTop = function(_obj) {
			var topPos = _obj.offsetTop;
			var ep = _obj.offsetParent;
			while (ep != null) {
				topPos += ep.offsetTop;
				ep = ep.offsetParent;
			}
			return topPos;
		}
	// VOID
		this.v = function() {
			var v = 0;
		}
}
var sg_page = '/sizing-guide/controls/updateform.cfm';
var sg_items = ['mainmanufacturer','mainmodel','mainsize','reservemanufacturer','reservemodel','reservesize'];
function sg_getValues() {
	var v = '';
	for(var i=0; i < sg_items.length; i++) v += '&' + sg_items[i] + '=' + document.feedbackform[sg_items[i]].value;
	return v;
}
function sg_changeManufacturer(type) {
	if(!bground_inaction) {
		bgStartAction();
		var req = 'doaction=models';
		req += sg_getValues();
		req += '&type=' + type;
		httpobj.makeRequest(sg_page, req, sg_handle);
	}
}
function sg_changeModel(type) {
	if(!bground_inaction) {
		bgStartAction();
		var req = 'doaction=sizes';
		req += sg_getValues();
		req += '&type=' + type;
		httpobj.makeRequest(sg_page, req, sg_handle);
	}
}
function sg_handle(re) {
	processXML(re);
	bgEndAction();
}

