var productSubs = {
	"Carpet": ["Broadloom", "Modular"],
	"Casegoods": ["Desks & Credenzas", "Conference Room"],
	"Flooring": ["Raised", "Resilient", "Stone and Tile"],
	"Furniture Systems": ["Enhancements"],
	"Seating": ["Benches", "Conference", "Ergonomic Desk/Task", "Guest", "Sofas & Lounge", "Stacking"],
	"Tables": ["Conference Tables", "Occasional Tables", "Training Tables"],
	"Textiles": ["Drapery", "Panel", "Upholstery", "Healthcare"]
};

function MannersCats() {
	var subCats = $('subcategory');

	setSub = function() {
		if (productSubs[this.value]) {
			$('subcategoryLabel').setStyle({color: '#000000'});
			subCats.enable();
			subCats.descendants().each(function(kid) {
				if (kid.id != "mSelector")
					kid.remove();
			});
			productSubs[this.value].each(function(option) {
				subCats.insert({"bottom": "<option value='" + option + "'>" + option + "</option>"});
			});
			document["contract.Contract.form"].subcategory.options[0].selected = true;
		}
		else {
			$('subcategoryLabel').setStyle({color: '#CCCCCC'});
			subCats.disable();
		}
	}

	$('category').observe('change', setSub.bindAsEventListener($('category')));
}

function MultiSelector(list_target, yTags) {
	this.mTags;
	if (yTags == 5)
		this.mTags = ["imageFile1", "imageFile2", "imageFile3", "imageFile4", "imageFile5"];
	else
		this.mTags = ["imageFile1", "imageFile2", "imageFile3", "imageFile4", "imageFile5", "imageFile6", "imageFile7", "imageFile8", "imageFile9", "imageFile10"];

	this.mTags.each(function(tag) {$(tag).value = "";});
	
	this.list_target = list_target;

	this.mCheckImage = function(mFile) {
		var gErrors = '';
		var gFocus = '';
		var gForm = document["contract.Contract.form"];

		if (!mFile.match(/.+(\.jpg|\.jpeg|\.gif|\.png)$/i)) {
			alert("Please upload only images of 'jpg', 'gif' or 'png' formats");
			return true;
		}

		if (yTags == 5) {
			if ($F(gForm["product.productName"]) == "") {
				gFocus = "product.productName";
				gErrors += "Product name<br>";
				$('dName').addClassName('gRed');
			} else {$('dName').removeClassName('gRed');}

			if ($F(gForm["product.manufacturer"]) == "") {
				if (gFocus == "")
					gFocus = "product.manufacturer";
				gErrors += "Manufacturer<br>";
				$('dMan').addClassName('gRed');
			} else {$('dMan').removeClassName('gRed');}

			if ($F(gForm["product.manufacturerWebsite"]) == "") {
				if (gFocus == "")
					gFocus = "product.manufacturerWebsite";
				gErrors += "Manufacturer website<br>";
				$('dManWebsite').addClassName('gRed');
			} else {$('dManWebsite').removeClassName('gRed');}

			if ($F(gForm["product.productCategory"]) == "") {
				if (gFocus == "")
					gFocus = "product.productCategory";
				gErrors += "Product category<br>";
				$('dCategory').addClassName('gRed');
			} else {$('dName').removeClassName('dCategory');}

			if (productSubs[$F(gForm["product.productCategory"])] != undefined && $F(gForm["product.productSubcategory"]) == "Select Subcategory") {
				if (gFocus == "")
					gFocus = "product.productCategory";
				gErrors += "Product subcategory<br>";
				$('subcategoryLabel').addClassName('gRed');
			} else {$('subcategoryLabel').removeClassName('gRed');}
		
			if ($F(gForm["product.productDescription"]) == "") {
				if (gFocus == "")
					gFocus = "product.productDescription";
				gErrors += "Product description<br>";
				$('dDescription').addClassName('gRed');
			} else {$('dDescription').removeClassName('gRed');}

			if ($F(gForm["product.name"]) == "") {
				if (gFocus == "")
					gFocus = "product.name";
				gErrors += "Name<br>";
				$('dYourName').addClassName('gRed');
			} else {$('dYourName').removeClassName('gRed');}

			if (!$F(gForm["product.email"]).match(/[a-zA-Z0-9_\-.]+@{1,1}[a-zA-Z0-9_\-]+\.{1,1}[a-zA-Z]{2,}/)) {
				if (gFocus == "")
					gFocus = "product.email";
				gErrors += "Valid email address<br>";
				$('dEmail').addClassName('gRed');
			} else {$('dEmail').removeClassName('gRed');}

			if ($F(gForm["product.phone"]) == "") {
				if (gFocus == "")
					gFocus = "product.phone";
				gErrors += "Name<br>";
				$('dPhone').addClassName('gRed');
			} else {$('dPhone').removeClassName('gRed');}
		} else {
			if ($F(gForm["project.firmName"]) == "") {
				gFocus = "project.firmName";
				gErrors += "Firm name<br>";
				$('jFirm').addClassName('gRed');
			} else {$('jFirm').removeClassName('gRed');}

			if ($F(gForm["project.name"]) == "") {
				if (gFocus == "")
					gFocus = "project.name";
				gErrors += "Name<br>";
				$('jName').addClassName('gRed');
			} else {$('jName').removeClassName('gRed');}

			if (!$F(gForm["project.email"]).match(/[a-zA-Z0-9_\-.]+@{1,1}[a-zA-Z0-9_\-]+\.{1,1}[a-zA-Z]{2,}/)) {
				if (gFocus == "")
					gFocus = "project.email";
				gErrors += "Valid email address<br>";
				$('jEmail').addClassName('gRed');
			} else {$('jEmail').removeClassName('gRed');}

			if ($F(gForm["project.phone"]) == "") {
				if (gFocus == "")
					gFocus = "project.phone";
				gErrors += "Phone number<br>";
				$('jPhone').addClassName('gRed');
			} else {$('jPhone').removeClassName('gRed');}

			if ($F(gForm["project.clientName"]) == "") {
				if (gFocus == "")
					gFocus = "project.clientName";
				gErrors += "Client name<br>";
				$('jClient').addClassName('gRed');
			} else {$('jClient').removeClassName('gRed');}
		
			if ($F(gForm["project.completionDate"]) == "") {
				if (gFocus == "")
					gFocus = "project.completionDate";
				gErrors += "Completion date<br>";
				$('jDate').addClassName('gRed');
			} else {$('jDate').removeClassName('gRed');}

			if ($F(gForm["project.projectLocation"]) == "") {
				if (gFocus == "")
					gFocus = "project.projectLocation";
				gErrors += "Project location<br>";
				$('jLocation').addClassName('gRed');
			} else {$('jLocation').removeClassName('gRed');}

			if ($F(gForm["project.projectDescription"]) == "") {
				if (gFocus == "")
					gFocus = "project.projectDescription";
				gErrors += "Project description<br>";
				$('jDescription').addClassName('gRed');
			} else {$('jDescription').removeClassName('gRed');}
		}
		
		if (gErrors != "") {
			$('errors').update('You must complete all required fields (highlighted in red) before uploading images.<br><br>');
			if (location.href.indexOf("#pTop") == -1)
				location.href = location.href + "#pTop";
			else
				location.href = location.href;
			return true;
		} else {
			$('errors').update("");
			return false;
		}
	}
	
	this.addElement = function(element) {
		if (element.tagName == 'INPUT' && element.type == 'file') {
			element.multi_selector = this;

			element.onchange = function(){
				if (element.multi_selector.mCheckImage(this.value)) {
					this.remove();
					var yInput = new Element('input', {type: 'file', id: this.id, name: this.id});
					element.multi_selector.addElement(yInput);
					$('iParent').insertBefore(yInput, list_target);
					return;
				}
				
				this.multi_selector.addListRow(this);

				$(this.id).setStyle({display: 'none'});
				for (yI = 0; yI < element.multi_selector.mTags.length; yI++) {
					var tag = element.multi_selector.mTags[yI];
					if (this.id != tag && $(tag).value == "") {
						$(tag).setStyle({display: 'block'});
						this.multi_selector.addElement($(tag));
						break;
					}
				}
			};
		} else {
			alert( 'Error: not a file input element' );
		};

	};
	
	this.mChanger = function() {
		alert("Hi Manners");
	}

	this.addListRow = function(element){
		var new_row = document.createElement('div');
		var new_row_button = document.createElement('input');
		
		new_row_button.multi_selector = this;

		new_row_button.type = 'button';
		new_row_button.value = 'Delete';
		new_row_button.name = element.id;
		new_row.element = element;

		new_row_button.onclick = function(){
			for (yI = 0; yI < element.multi_selector.mTags.length; yI++) {
				var tag = element.multi_selector.mTags[yI];
				if (this.name == tag) {
					$(tag).remove();
					var mInput = new Element('input', {type: 'file', id: tag, name: tag});
					mInput.setStyle({display: 'block'});
					new_row_button.multi_selector.addElement(mInput);
					$('iParent').insertBefore(mInput, list_target);
				} else
					$(tag).setStyle({display: 'none'});
			}

			this.parentNode.parentNode.removeChild(this.parentNode);
			
			return false;
		};

		new_row.innerHTML = element.value.substr(0, 50) + "<br>" + element.value.substr(50);
		new_row.appendChild(new_row_button);
		this.list_target.appendChild(new_row);
	};
	
	this.mSubmitter = function() {
		for (yI = 0; yI < this.mTags.length; yI++) {
			alert(this.mTags[yI] + " = " + $(this.mTags[yI]).value);
		}
		return false;
	};
};