window.FSBOManager = (function()
{
	var prospectListNumber = 0;
	var currentTab = 'eb';
	var fsboManager = {};
	var businessTypeRowIsHighlighted = false;
	
	base64 = function(){};
	base64.e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_|";
	base64.encode = function(input) 
	{
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		
		do 
		{
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
		  
			if (isNaN(chr2)) 
			{
				enc3 = enc4 = 64;
			} 
			else if (isNaN(chr3)) 
			{
				enc4 = 64;
			}
		  
			output = (
				output + 
				base64.e.charAt(enc1) + 
				base64.e.charAt(enc2) + 
				base64.e.charAt(enc3) + 
				base64.e.charAt(enc4));
				
	   } 
	   while (i < input.length);
	   
	   return output;
	};
	
	function queryStringDataBuilder()
	{
		var queryStringData = [];
		
		this.length = function()
		{
			return queryStringData.length;
		}
		
		this.append = function(
			key,
			value)
		{
			queryStringData.push(
				encodeURIComponent(
					key) +
				'=' +
				encodeURIComponent(
					value));	
		};
		
		this.toString = function()
		{
			return (
				queryStringData.join('&'));
		};
	}
	
	if (typeof(ContentProviderUrl) == 'undefined')
	{
		ContentProviderUrl = (
			document.location.protocol +
			'//www.bizbuysell.com');
			
		if (document.location.protocol.toLowerCase() == 'https:')
		{
			ContentProviderUrl = (
				document.location.protocol +
				'//secure.bizbuysell.com');
		}
	}
	
	var contentProviderUrl = ContentProviderUrl;
	
	// Moved the call to FSBOManager.InitializeFields to fsbomgr.cpp to an inline
	// script to ensure it's ran before BizO takes it's time executing -BT 07/10/09 (WI #4172)
	/*
	var oldOnLoad = window.onload;
	
	window.onload = function()
	{
		if (oldOnLoad)
		{
			oldOnLoad();
		}
		
		if ((typeof(FSBOManager) != 'undefined') &&
			(typeof(FSBOManager.InitializeFields) == 'function'))
		{
			FSBOManager.InitializeFields();	
		}
	};
	*/
	
	fsboManager.ShowAdPreview = function()
	{
		if ((currentTab != '') &&
			(prospectListNumber > 0))
		{
			window.open(
				('http://www.bizbuysell.com/cgi-bin/addetail?J=bbs&nosearch=1&tab=' +
					currentTab +
					'&prospect=1&q=' +
					prospectListNumber),
				'',
				'resizable=yes,toolbar=no,scrollbars=yes,menubar=no,status=no,directories=no,width=690,height=800');
		}
		
		return;
	};
	
	fsboManager.InitializeProspectData = function(
		tab,
		listNumber)
	{
		currentTab = tab;
		prospectListNumber = listNumber;
	};
	
	fsboManager.InitializeFields = function()
	{
		var frmFSBOManager = document.forms['fsbomgr'];
		
		if (frmFSBOManager)
		{
			if (prospectListNumber > 0)
			{
				// Get "input" fields
				var inputFields = frmFSBOManager.getElementsByTagName(
					'input');
					
				if (inputFields.length > 0)
				{
					for (var index = 0; index < inputFields.length; index++)
					{
						if (typeof(inputFields[index].type) != 'undefined')
						{
							// TextBoxes
							if (inputFields[index].type == 'text')
							{
								inputFields[index].onblur = (function(currentOnBlur)
								{
									return function()
									{
										if (currentOnBlur)
										{
											currentOnBlur.apply(
												this, 
												arguments);
										}
										
										if ((typeof(FSBOManager) != 'undefined') &&
											(typeof(FSBOManager.UpdateBizopProspect) == 'function'))
										{
											FSBOManager.UpdateBizopProspect.call(
												this);
										}
									};
								})(inputFields[index].onblur);
							}
							// CheckBoxes
							else if (inputFields[index].type == 'checkbox')
							{
								inputFields[index].onchange = (function(currentOnChange)
								{
									return function()
									{
										if (currentOnChange)
										{
											currentOnChange.apply(
												this, 
												arguments);
										}
										
										if ((typeof(FSBOManager) != 'undefined') &&
											(typeof(FSBOManager.UpdateBizopProspect) == 'function'))
										{
											FSBOManager.UpdateBizopProspect.call(
												this);
										}
									};
								})(inputFields[index].onchange);
							}
							// Radio Buttons
							else if (inputFields[index].type == 'radio')
							{
								inputFields[index].onchange = (function(currentOnChange)
								{
									return function()
									{
										if (currentOnChange)
										{
											currentOnChange.apply(
												this, 
												arguments);
										}
										
										if ((typeof(this.checked) != 'undefined') &&
											(this.checked) &&
											(typeof(FSBOManager) != 'undefined') &&
											(typeof(FSBOManager.UpdateBizopProspect) == 'function'))
										{
											FSBOManager.UpdateBizopProspect.call(
												this);
										}
									};
								})(inputFields[index].onchange);
							}
						}
					}
				}
				
				// Get DropDownLists
				var selectFields = frmFSBOManager.getElementsByTagName(
					'select');
					
				if (selectFields.length > 0)
				{
					for (var index = 0; index < selectFields.length; index++)
					{
						selectFields[index].onchange = (function(currentOnChange)
						{
							return function()
							{
								if (currentOnChange)
								{
									currentOnChange.apply(
										this, 
										arguments);
								}
								
								if ((typeof(FSBOManager) != 'undefined') &&
									(typeof(FSBOManager.UpdateBizopProspect) == 'function'))
								{
									FSBOManager.UpdateBizopProspect.call(
										this);
								}
							};
						})(selectFields[index].onchange);
					}
				}
				
				// Get TextAreas
				var textAreaFields = frmFSBOManager.getElementsByTagName(
					'textarea');
					
				if (textAreaFields.length > 0)
				{
					for (var index = 0; index < textAreaFields.length; index++)
					{
						textAreaFields[index].onblur = (function(currentOnBlur)
						{
							return function()
							{
								if (currentOnBlur)
								{
									currentOnBlur.apply(
										this, 
										arguments);
								}
										
								if ((typeof(FSBOManager) != 'undefined') &&
									(typeof(FSBOManager.UpdateBizopProspect) == 'function'))
								{
									FSBOManager.UpdateBizopProspect.call(
										this);
								}
							};
						})(inputFields[index].onblur);
					}
				}
			}
		}
	};
	
	fsboManager.UpdateBizopProspect = function()
	{
		if ((prospectListNumber > 0) &&
			(typeof(this.type) != 'undefined') &&
			(this.type != '') &&
			(typeof(this.name) != 'undefined') &&
			(this.name != '') &&
			(typeof(this.value) != 'undefined'))
		{
			var type = this.type.toLowerCase();
			var name = this.name.toUpperCase();
			var value = this.value;
			
			var bizopQueryStringDataBuilder = new queryStringDataBuilder();
			
			bizopQueryStringDataBuilder.append(
				'ListNumber',
				prospectListNumber);
				
			bizopQueryStringDataBuilder.append(
                'HiddenFieldName',
                name);
                
            var fieldType = 0;
                
            switch (type)
            {
				case 'text':
				{
					fieldType = 10;
					
					break;
				}
				
				case 'textarea':
				{
					fieldType = 20;
					
					break;
				}
				
				case 'checkbox':
				{
					fieldType = 30;
					
					if (typeof(this.checked) != 'undefined')
					{
						bizopQueryStringDataBuilder.append(
							'CheckBoxChecked',
							this.checked);
					}
					
					// If the field is the "state confidential" checkbox,
					// pass along the selected state
					if (name == 'F8')
					{
						var ddlState = document.getElementsByName(
							'LO')[0];
							
						if ((ddlState) &&
							(typeof(ddlState.value) != 'undefined') &&
							(ddlState.value != ''))
						{
							bizopQueryStringDataBuilder.append(
								'SelectedState',
								ddlState.value);
						}
					}
					
					break;
				}
				
				case 'radio':
				{
					fieldType = 40;
					
					break;
				}
				
				case 'select-one':
				{
					fieldType = 50;
					
					// If the field is the state drop down list,
					// pass along if the "state confidential" check box
					// is checked
					if (name == 'LO')
					{
						var isStateConfidential = false;
						
						var chkIsStateConfidential = document.getElementsByName(
							'F8')[0];
							
						if ((chkIsStateConfidential) &&
							(typeof(chkIsStateConfidential.checked) != 'undefined'))
						{
							isStateConfidential = chkIsStateConfidential.checked;
						}
							
						bizopQueryStringDataBuilder.append(
							'IsStateConfidential',
							isStateConfidential);
					}
					
					break;
				}
				
				case 'select-multiple':
				{
					fieldType = 60;
					
					break;
				}
            }
            
			bizopQueryStringDataBuilder.append(
				'FieldType',
				fieldType);
				
			bizopQueryStringDataBuilder.append(
				'FieldValue',
                base64.encode(
					value));
			
			updateBizopProspect(
				bizopQueryStringDataBuilder);
		}
	};
	
	fsboManager.HighlightBusinessType = function(
		highlight)
	{
		if ((highlight) &&
			(!businessTypeRowIsHighlighted))
		{
			this.className += ' highlighted';
			
			businessTypeRowIsHighlighted = true;
		}
		else
		{
			this.className = this.className.replace(
				'highlighted',
				'');
				
			businessTypeRowIsHighlighted = false;
		}
	};
	
	fsboManager.SelectBusinessType = function(
		rowID)
	{
		var isRadioButton = false;
		
		var businessTypesTable = document.getElementById(
			'businessTypes');
			
		if (businessTypesTable)
		{
			if ((typeof(rowID) == 'undefined') ||
				(rowID < 0))
			{
				if ((typeof(this.type) != 'undefined') &&
					(this.type == 'radio') &&
					(typeof(this.value) != 'undefined') &&
					(parseInt(this.value)))
				{
					rowID = parseInt(
						this.value);
						
					isRadioButton = true;
				}
				else
				{
					rowID = 0;
				}
			}
		}
		
		businessTypesTable.className = businessTypesTable.className.replace(
			/pfRow\d+_selected/g,
			'');
			
		businessTypesTable.className += (
			'pfRow' +
			rowID +
			'_selected');
			
		if (!isRadioButton)
		{
			var businessTypeRadioButton = document.getElementById(
				'PF' +
				rowID);
				
			if (businessTypeRadioButton)
			{
				businessTypeRadioButton.checked = 'checked';
			}
		}
	};
	
	function updateBizopProspect(
        bizopQueryStringDataBuilder)
    {
		if (bizopQueryStringDataBuilder.length() > 0)
		{
			var fsboUtilitiesHandlerImage = document.createElement(
				'img');
				
			fsboUtilitiesHandlerImage.onload = function()
			{
				document.body.removeChild(
					fsboUtilitiesHandlerImage);
			};
			
			document.body.appendChild(
				fsboUtilitiesHandlerImage);
				
			fsboUtilitiesHandlerImage.src = (
				contentProviderUrl +
				'/fsbo/handlers/FSBOUtilities.ashx?' +
				bizopQueryStringDataBuilder.toString());
		}
    }
	
	return fsboManager;
})();