///////////////////////////////////////////////////////////////////////////////
// ========== Javascript for Top Producer ==========
///////////////////////////////////////////////////////////////////////////////

		// Ensure the user of this form has entered the required fields.
		function validateData()
		{
			var txtFirstName		= getElementById( "FIRST_NAME"		);
			var txtLastName			= getElementById( "LAST_NAME"		);
			var txtEmail			= getElementById( "EMAIL"		);
			var txtPhoneAreaCode		= getElementById( "PHONE_AREA_CODE"	);
			var txtPhoneLocalCode		= getElementById( "PHONE_LOCAL_CODE"	);
			var txtPhoneNumber		= getElementById( "PHONE_NUMBER"	);
			var txtComments			= getElementById( "COMMENTS"		);
			var sPhoneValue			= txtPhoneAreaCode.value + txtPhoneLocalCode.value + txtPhoneNumber.value;
			var bRequirementsMet		= true;
			var txtFocusField;
			
			if( txtFirstName.value.length == 0 && txtLastName.value.length == 0 ) {
				txtFocusField = txtFirstName;
				bRequirementsMet = false;
			}
			if( bRequirementsMet && ( txtEmail.value.length == 0 && sPhoneValue.length == 0 ) ) {
				txtFocusField = txtEmail;
				bRequirementsMet = false;
			}
			
			if( !bRequirementsMet ) {
				window.alert( "\nPlease enter your FIRST NAME or LAST NAME\nand\nPHONE NUMBER or EMAIL ADDRESS.\n" );
				txtFocusField.focus();
			} else {
				if( sPhoneValue.length > 0 && ( txtPhoneAreaCode.value.length != 3 || txtPhoneLocalCode.value.length != 3 || txtPhoneNumber.value.length != 4 ) ) {
					bRequirementsMet = false;
					window.alert( "\nYou have entered an invalid phone number. Please enter a correct phone number.\n" );
					txtPhoneAreaCode.focus();
				}
				if( bRequirementsMet && txtEmail.value.length > 0 ) {
					var nAtSymbolIndex = txtEmail.value.indexOf( "@" );
					var sUserName      = "";
					var sServerName    = "";
					
					if( nAtSymbolIndex > 0 && nAtSymbolIndex < txtEmail.value.length - 1 ) {
						sUserName = txtEmail.value.substring( 0, nAtSymbolIndex );
						sServerName = txtEmail.value.substring( nAtSymbolIndex + 1 );
						nAtSymbolIndex = sServerName.indexOf( "." );
					}
					if( nAtSymbolIndex == -1 || sUserName.length == 0 || sServerName.length == 0 ) {
						window.alert( "You have entered an invalid email address. Please enter a correct email address." );
						txtEmail.focus();
						bRequirementsMet = false;
					}
				}
				if( bRequirementsMet && txtComments.value.length > 2900 ) {
					bRequirementsMet = false;
					window.alert( "\nYour comments are too long.  Please try to limit the length of your comments to 2900 characters.\n" );
					txtComments.focus();
				}
			}

			if( bRequirementsMet ) {
				var sMinPrice = getElementById( "BUYER_MIN_PRICE" ).value;
				var sMaxPrice = getElementById( "BUYER_MAX_PRICE" ).value;
				// Pre-parse the string to remove any non digit characters
				sMinPrice = sMinPrice.replace( /\$|\+|,/g,			"" );
				sMaxPrice = sMaxPrice.replace( /\$|\+|,/g,			"" );
				
				// Convert from a string to a number.
				var nMinPrice = new Number( sMinPrice );
				var nMaxPrice = new Number( sMaxPrice );
				
				if( nMinPrice != Number.NaN && nMaxPrice != Number.NaN && nMinPrice > nMaxPrice ) {
					window.alert( "\nYou have specified an invalid minimum or maximum price.  Please\nensure that the minimum price is not greater than the maximum price\nin the price range section.\n" )
					getElementById( "BUYER_MAX_PRICE" ).focus();
					bRequirementsMet = false;
				}
			}
			
			return bRequirementsMet;
		}
		

		// Retrieve an element by it's ID attribute
		// NOTE: This function is used in place of
		//  'document.getElementById() to support
		//  Internet Explorer 4.01.
		function getElementById( id ) {
			if( typeof( document.getElementById ) == "undefined" ) {
				for( var i = 0; i < document.all.length; i++ ) {
					var el = document.all( i );
					if( el.id == id ) {
						return el;
					}
				}
			}
			else {
				return document.getElementById( id );
			}
		}

		function init() {
			var statusSeller	= getElementById( "STATUS_SELLER"	);
			var statusBoth		= getElementById( "STATUS_BOTH"		);
			
			var status = "Buyer";
			if( statusSeller.checked ) {
				status = "Seller";
			} else if ( statusBoth.checked ) {
				status = "Both";
			}
			
			enableSection( status );
			size( getElementById( "TABLE_PICTURES" ) );
			var backgroundImage = getElementById( "BACKGROUND_IMAGE" );
			if( typeof( backgroundImage ) != "undefined" && backgroundImage != null ) {
				backgroundImage.style.visibility = "visible";
			}
			
			window.moveTo( 0, 0 );
			window.resizeTo( screen.availWidth, screen.availHeight );

			getElementById( "FIRST_NAME" ).onkeypress = Capitalize;
			getElementById( "LAST_NAME" ).onkeypress = Capitalize;
		}

		function size( objTable ) {
			var backgroundImage = getElementById( "BACKGROUND_IMAGE" );
			
			if( typeof( backgroundImage ) != "undefined" && typeof( objTable ) != "undefined" && backgroundImage != null ) {
				backgroundImage.style.left   = objTable.offsetLeft;
				backgroundImage.style.top    = objTable.offsetTop;
				backgroundImage.style.width  = objTable.offsetWidth;
				backgroundImage.style.height = objTable.offsetHeight;
			}
		}

		function enableSection( section ) {
			setControl( getElementById( "SELLER_SQUAREFEET"		), section == "Buyer" );
			setControl( getElementById( "SELLER_BEDROOMS"		), section == "Buyer" );
			setControl( getElementById( "SELLER_BATHROOMS"		), section == "Buyer" );
			setControl( getElementById( "TXT_SELLER"		), section == "Buyer" );
			setControl( getElementById( "TXT_SELLER_SQUAREFEET"	), section == "Buyer" );
			setControl( getElementById( "TXT_SELLER_BEDROOMS"	), section == "Buyer" );
			setControl( getElementById( "TXT_SELLER_BATHROOMS"	), section == "Buyer" );
			
			setControl( getElementById( "BUYER_MIN_PRICE"		), section == "Seller", "$0" );
			setControl( getElementById( "BUYER_MAX_PRICE"		), section == "Seller", "no maximum" );
			setControl( getElementById( "BUYER_SQUAREFEET"		), section == "Seller", "0-999"	);
			setControl( getElementById( "BUYER_BEDROOMS"		), section == "Seller", "1"		);
			setControl( getElementById( "BUYER_BATHROOMS"		), section == "Seller", "1"		);
			setControl( getElementById( "TXT_BUYER"			), section == "Seller" );
			setControl( getElementById( "TXT_BUYER_SQUAREFEET"	), section == "Seller" );
			setControl( getElementById( "TXT_BUYER_BEDROOMS"	), section == "Seller" );
			setControl( getElementById( "TXT_BUYER_BATHROOMS"	), section == "Seller" );
			setControl( getElementById( "TXT_BUYER_MIN_PRICE"	), section == "Seller" );
			setControl( getElementById( "TXT_BUYER_MAX_PRICE"	), section == "Seller" );
		}

		function setControl( control, disable, value ) {
			control.disabled = disable;
			if( disable ) {
				control.value = typeof( value ) == "undefined" ? "" : value;
				if( control.checked ) {
					control.checked = false;
				}
			}
		}

		function moveToNextField( id ) {
			if( window.event.keyCode != 9 ) {
				if( getElementById( id ).value.length == 3 ) {
					if( id == "PHONE_AREA_CODE" ) {
						getElementById( "PHONE_LOCAL_CODE" ).focus();
					}
					else if( id == "PHONE_LOCAL_CODE" ) {
						getElementById( "PHONE_NUMBER" ).focus();
					}
				}
			}
		}
		
		function allowKeys( obj, keys ) {
			var bAllowKey = false;
			for( var i = 0; i < keys.length; i++ ) {
				if( keys.charAt( i ) == String.fromCharCode( window.event.keyCode ) ) {
					bAllowKey = true;
					break;
				}
			}
			if( !bAllowKey ) {
				window.event.cancelBubble = true;
				window.event.keyCode = 0;
			}
		}
		
		
		function validateInput( obj, keys ) {
			if( typeof( window.clipboardData ) != "undefined" ) {
				var sData = window.clipboardData.getData( "Text" )
				var sTemp = "";
				if( typeof( sData ) != "undefined" ) {
					for( var i = sData.length; --i >= 0; ) {
						for( var j = keys.length; --j >= 0; ) {
							if( sData.charAt( i ) == keys.charAt( j ) ) {
								sTemp += sData.charAt( i );
							}
						}
					}
				}
			
				window.clipboardData.setData( "Text", sTemp );
			}
		}
		
		function enableBestTime() {
			var txtPhoneArea	= getElementById( "PHONE_AREA_CODE"		);
			var txtPhoneLocal	= getElementById( "PHONE_LOCAL_CODE"	);
			var txtPhoneNumber	= getElementById( "PHONE_NUMBER"		);
			var obgBestTime		= getElementById( "BEST_TIME"			);
			
			if( typeof( txtPhoneArea ) != "undefined" && txtPhoneArea != null && typeof( obgBestTime ) != "undefined" && obgBestTime != null ) {
				obgBestTime.disabled = !( txtPhoneArea.value.length != 0 || txtPhoneLocal.value.length != 0 || txtPhoneNumber.value.length != 0 );
			}
		}
		
		function Capitalize()
		{
			var el = window.event.srcElement;
			if( el == null ) return;
			
			if( el.getAttribute( "CAP" ) == null )
			{
				el.setAttribute( "CAP", "1" );
				if( el.value == "" ) {
					var nUCharCode = String.fromCharCode( window.event.keyCode ).toUpperCase().charCodeAt(0);
					window.event.keyCode = nUCharCode;
				}
			}
		}
		
		
		function Submit()
		{
			if( validateData() ) {
				getElementById( "SUBMIT" ).disabled = true;
				var pForm = getElementById( "frmMain" );
				pForm.submit();
			}
		}
