// JavaScript Document
$(document).ready(function(){
	_origionalForumNoInput = $('form[name="frmAddPost"] input[name="forumNo"]');
	//_aJax = $.ajax();
	$('form[name="frmSelectForum"] input[name="btnSelect"]').hide();
	$('div.header.forum h4').hide();
	$('form[name="frmSelectForum"] select[name="manufacturerNo"]').change( function(e) {
		_this = $(this);
		_equipSelectBox = $('form[name="frmSelectForum"] select[name="equipmentNo"]');
		$('form[name="frmAddPost"] input[name="equipmentNo"]').remove();
		$('form[name="frmAddPost"]').prepend(_origionalForumNoInput);		
		$('form[name="frmSelectForum"] select[name="equipmentNo"] option').remove();		
		if (_this.val() != 0) {
			_equipSelectBox.append('<option val="0">Loading...</option>');
			if(typeof _aJax != 'undefined') { _aJax.abort(); } 
			_aJax = $.ajax({
				type: "post",
				url: "/?home.getModels",
				data: 'ajaxRequest=true&manufacturerNo=' + _this.val(),
				complete: function(msg) {
					var thisText = msg.responseText.split("</td></td>");
					thisText = thisText[0];
					var models = thisText.split("~~");
					var splitModels;
					selectBoxHTML = "";
					for(var i=0; i < models.length; i++) {
						splitModels = models[i].split("``");
						if(splitModels.length == 2) {
							selectBoxHTML = selectBoxHTML + '<option value="' + splitModels[1].toString() + '">' + splitModels[0].toString() + '</option>';
						}
					}
					_equipSelectBox.html(selectBoxHTML);
					
					if (_equipSelectBox.find('option').size() > 1) {
						$('form[name="frmSelectForum"] input[name="btnView"]').val("View All Manufacturers Equipment");
						_equipSelectBox.prepend('<option val="0">View All</option>');
						_equipSelectBox.val("0");
					} else {
						$('form[name="frmSelectForum"] input[name="btnView"]').val("View This Equipment");
						$('form[name="frmAddPost"] input[name="forumNo"]').remove();							
						$('form[name="frmAddPost"]').prepend('<input type="hidden" name="equipmentNo" value="' + _equipSelectBox.val() + '" />');						
					}
				} // Close Complete
			});	// Close $Ajax
		} else {
			_equipSelectBox.append('<option val="0">View All</option>');
			$('form[name="frmSelectForum"] input[name="btnView"]').val("View All Equipment");		
		}
	}); // close select[name="manufacturerNo"]').change
	$('form[name="frmSelectForum"] select[name="equipmentNo"]').change( function(e) {
		_this = $(this);
		$('form[name="frmAddPost"] input[name="equipmentNo"]').remove();		
		if (_this.val() > 0) {
			$('form[name="frmSelectForum"] input[name="btnView"]').val("View This Equipment");
			$('form[name="frmAddPost"] input[name="forumNo"]').remove();			
			$('form[name="frmAddPost"]').prepend('<input type="hidden" name="equipmentNo" value="' + _this.val() + '" />');
		}
		else {
			$('form[name="frmSelectForum"] input[name="btnView"]').val("View All Manufacturers Equipment");		
			$('form[name="frmAddPost"]').prepend(_origionalForumNoInput);
		}
	}); // close select[name="equipmentNo"]').change
	
	
	
	$('#chooseMMForm select[name="manufacturerNo"]').change( function() {
		var manufacturerNo = $(this).val(); 
		var EquipSelectBox = $(this).parents('form').find('#modelSelect');
		if (manufacturerNo.length == 0) {
			EquipSelectBox.html('<option value="">View All</option>');
			$('input[name="newManufacturerName"]').remove();
			$('input[name="newEquipmentName"]').remove();
		}
		else if (manufacturerNo == "NEW") {
			$(this).after("<input type='text' name='newManufacturerName' value='' class='txtAddNewEquipInput' />");
		}
		else {
			EquipSelectBox.html('<option value="">Loading...</option>');
			$('input[name="newManufacturerName"]').remove();
			$('input[name="newEquipmentName"]').remove();
			$.ajax({
				type: "post",
				url: "/?home.getModels",
				contentType: "application/x-www-form-urlencoded; charset=UTF-8",
				data: 'ajaxRequest=true&manufacturerNo=' + manufacturerNo,
				dataType: "html",
				async: false,
				complete: function(msg) {
					var thisText = msg.responseText.split("</td></td>");
					thisText = thisText[0];
					var models = thisText.split("~~");
					var splitModels;
					if (EquipSelectBox.attr("name").toLowerCase().indexOf("equipmentno") == '0') {
						var selectBoxHTML = '<option value="0">Choose your equipment model</option><option value="0">---</option><option value="0">Other model not listed</option><option value="0">---</option>';
					} else {
						var selectBoxHTML = '<option value="">View All Models</option>';
					}
					for(var i=0; i < models.length; i++) {
						splitModels = models[i].split("``");
						if(splitModels.length == 2) {
							selectBoxHTML = selectBoxHTML + '<option value="' + splitModels[1].toString() + '">' + splitModels[0].toString() + '</option>';
						}
					}
					EquipSelectBox.html(selectBoxHTML);
				} // Close Complete
			});	// Close $Ajax
			
			var btn = $(this).parents('form').find('#modelSelectBtn');
			if (btn.is(':visible')) {
				EquipSelectBox.after(btn.clone());
				btn.remove();
			}
		} // Close IF THEN ELSE
		if (EquipSelectBox.attr('name') == "equipmentNo") {
			//$('#frmAskAQuestions').find('#modelSelect').append("<option value=''>----------------------</option><option value='NEW'>Not Listed</option>");
		}
	});
	$('#chooseMMForm select[name="equipmentNo"]').change( function() {
		if ($(this).val() == "NEW") {
			$(this).after("<input type='text' name='newEquipmentName' value='' class='txtAddNewEquipInput' />");
		}
		else {
			$('input[name="newEquipmentName"]').remove();
		}
	});
	
	$('#chooseMMForm').submit( function() {
		var ManufacturerSelectBox = $(this).find('select[name="manufacturerNo"]');
		var EquipmentSelectBox = $(this).find('#modelSelect');
		//alert("manufacturer: '" + ManufacturerSelectBox.val() + "'\nequip: '"+EquipmentSelectBox.val()+"'");
		if (EquipmentSelectBox.val().length > 0) {
			//$('#chooseMMForm').attr("action","/?equipment.view/equipmentNo/" + $('#modelSelect option:selected').val());
			window.location.href = "/?equipment.view/equipmentNo/" + EquipmentSelectBox.val();
			return false;
		}
	});
});
