// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC 	= navigator.userAgent.toLowerCase(); 	// Get client info
var clientVer 	= parseInt( navigator.appVersion ); 	// Get browser version

var is_ie 		= ( ( clientPC.indexOf( "msie" ) != -1) && ( clientPC.indexOf( "opera" ) == -1 ) );
var is_nav 		= ( ( clientPC.indexOf( 'mozilla' ) != -1 ) && ( clientPC.indexOf( 'spoofer' ) == -1 )
					&& ( clientPC.indexOf( 'compatible' ) == -1 ) && ( clientPC.indexOf( 'opera' ) == -1 )
					&& ( clientPC.indexOf( 'webtv' ) == -1 ) && ( clientPC.indexOf( 'hotjava' ) == -1 ) );
var is_moz = 0;

var is_win 		= ( ( clientPC.indexOf( "win" ) != -1 ) || ( clientPC.indexOf( "16bit" ) != -1 ) );
var is_mac 		= ( clientPC.indexOf( "mac" ) != -1 );
	
var activeTab = 0;
function activate_tab( index )
{
	// initialization
	var curTab 		= document.getElementById( "tab_" + index );
	var i, divs		= curTab.getElementsByTagName( "div" );
	var oldTab 		= document.getElementById( "tab_" + activeTab );
	var oldDivs		= oldTab.getElementsByTagName( "div" );
	
	// make the current tab the active one
	divs[ 0 ].className 	= "west";
	divs[ 1 ].className 	= "";
	divs[ 2 ].className 	= "east";
	
	if ( activeTab != index )
	{
		// deactivate the old one
		oldDivs[ 0 ].className 	= "inactive_west";
		oldDivs[ 1 ].className 	= "inactive";
		oldDivs[ 2 ].className 	= "inactive_east";
	
		activeTab	= index;
	}
}


function switch_tabs( tab, page, vendor, url )
{
	save_changes(); 
	activate_tab( tab ); 
	
	document.getElementById( "iframe" ).src = "browser.php?section=" + document.getElementById( 'sid' ).value + "&vendor=" + vendor;
	
	load_content( page, vendor, url )
}


function load_details( state, vendors )
{
	document.getElementsByTagName( "body" )[ 0 ].style.cursor = "wait";
	loading();
	x_load_details( state, vendors, load_details_cb );
}

var details_html;
function load_details_cb( arguments )
{
	var divFiles 	= document.getElementById( "details_files" );
	var args		= arguments.split( ";" );
	var files		= args[ 0 ].split( "|" );
	var sizes		= args[ 1 ].split( "|" );
	var code		= args[ 2 ].split( "|" );
	var vendor		= args[ 3 ].split( "|" );
	var readonly	= args[ 4 ].split( "|" );
	var assoc		= args[ 5 ].split( "|" );
	var state		= args[ 6 ].split( "|" );
	var n			= ( files != "" ) ? files.length : 0;
	
	if ( !details_html )
	{
		details_html = document.getElementById( "details_box" ).innerHTML;
		document.getElementById( "details_box" ).innerHTML = "";
	}
	
	var table 	= '<table width="100%" cellspacing="0" cellpadding="4"><thead><tr><th>#</th><th>File</th><th>Size</th>';
	table		+= '<th>Associations</th><th align="right" colspan="2">' + details_html + '</th></tr></thead><tbody>';
	
	
	if ( n == 0 )
		table += "<tr><td colspan='8'>There are currently no downloads in this category.</td></tr>";
	else
	{
		for ( var i = 0; i < n; i++ )
		{
			switch ( vendor[ i ] )
			{
				case "Fairmont":	var location = "http://www.fairmontserver.com/shared/fhomes/downloads/details/"; break;
				case "Century":		var location = "http://www.fairmontserver.com/shared/century/downloads/details/"; break;
				case "Frienship":	var location = "http://www.fairmontserver.com/shared/friendship/downloads/details/"; break;
				default: 			var location = "http://www.fairmontserver.com/shared/global/downloads/details/"; break;
			}
			
			var row_bg = ( i % 2 ) ? "even" : "odd";
			
			table += "<tr class='" + row_bg + "'><td valign='top'>" + ( i + 1 ) + ". </td>";
			table += "<td valign='top'><a href='" + location + files[ i ] + "' target='_blank'>" + files[ i ] + "</a></td>";
			table += "<td valign='top'>" + sizes[ i ] + "</td>";
			table += "<td valign='top'><div id='" + files[ i ] + "'>";
			table += "<table width='100%'><tbody>";
			
			var cur_assoc 	= assoc[ i ].split( "," );
			var cur_file 	= code[ i ].split( "," );
			
			for ( var j = 0; j < cur_assoc.length; j++ )


				if ( cur_assoc[ j ] != "none" )
					table += "<tr><td align='right'><div id='label_" + files[ i ] + "_" + cur_file[ j ] + "'><label for='assoc_" + files[ i ] + "_" + cur_file[ j ] + "'>" + cur_assoc[ j ] + "</label></div></td><td align='right'><div id='check_" + files[ i ] + "_" + cur_file[ j ] + "'><input type='checkbox' name='" + files[ i ] + "[]' id='assoc_" + files[ i ] + "_" + cur_file[ j ] + "' value='" + cur_assoc[ j ] + "' /></div></td></tr>";
				else
					table += "<tr><td colspan='2'><span class='req'>none</span></td></tr>";
			
			table += "</tbody><tfoot><tr><td colspan='2'></td></tr></tfoot></table>";
			table += "</div></td>";
			
			if ( readonly[ i ] == false )
			{
				var disabled = cur_assoc.length == 1 ? " disabled='disabled'" : "";
				
				table += "<td valign='top'><input type='checkbox' id='assoc_all_" + files[ i ] + "' name='assoc_all[]' value='1' /> <label for='assoc_all_" + files[ i ] + "'>Unlink All</label></td>";
				table += "<td valign='top'><input type='button' value='Unlink'" + disabled + " onclick='unlink_files( \"" + files[ i ] + "\" );' /></td>";
			}
			else
				table += "<td colspan='2'><em>Read-Only</em></td>";
			table += "</tr>";
		}
	}
	table += "</tbody><tfoot><tr><td colspan='8'></td></tr></tfoot></table>";
	divFiles.innerHTML = table;
	
	var st_options	= document.getElementById( "detail_state" );
	var st_n		= st_options.options.length;
	
	for ( var i = 0; i < st_n; i++ )
		if ( st_options.options[ i ].value == state )
			st_options.selectedIndex = i;
			
	loading();
	document.getElementsByTagName( "body" )[ 0 ].style.cursor = "default";
}


function unlink_files( detail_file )
{
	loading();
	var assoc 		= document.getElementsByName( detail_file + "[]" );
	var assoc_all 	= document.getElementById( "assoc_all_" + detail_file );
	var main_files	= new Array();
	
	for ( var i = 0; i < assoc.length; i++ )
		if ( assoc_all.checked == true || assoc[ i ].checked == true )
			main_files[ main_files.length ] = assoc[ i ].value;
			
	x_unlink_files( main_files, detail_file, unlink_files_cb );
}


function unlink_files_cb( arguments )
{	
	var args 	= arguments.split( "|" );
	var files 	= args[ 0 ].split( "," );
	var detail	= args[ 1 ];
	var n		= files.length;
	
	for( var i = 0; i < n; i++ )
	{
		document.getElementById( "label_" + detail + "_" + files[ i ] ).innerHTML = "";
		document.getElementById( "check_" + detail + "_" + files[ i ] ).innerHTML = "";
	}
	
	loading();
}

function loading()
{
	var throbber = document.getElementById( "throbber" );
	
	throbber.className = throbber.className == "no_progress" ? "progress" : "no_progress"; 
}


function load_files( name, vendors )
{
	document.getElementsByTagName( "body" )[ 0 ].style.cursor = "wait";
	loading();
	x_load_files( name, vendors, load_files_cb );
}


function load_files_cb( arguments )
{
	var divFiles 	= document.getElementById( "files" );
	var args		= arguments.split( ";" );
	var files		= args[ 0 ].split( "|" );
	var sizes		= args[ 1 ].split( "|" );
	var modified	= args[ 2 ].split( "|" );
	var vendor		= args[ 3 ].split( "|" );
	var vendor_id	= args[ 4 ].split( "|" );
	var readonly	= args[ 5 ].split( "|" );
	var assoc		= args[ 6 ].split( "|" );
	var category	= args[ 7 ];
	var n			= ( files != "" ) ? files.length : 0;
	var table 		= '<table width="100%" cellspacing="0" cellpadding="4">';
	table			+= '<thead><tr><th>#</th><th>File</th>';
	table			+= '<th>Size</th>';
	table			+= '<th>Vendor</th>';
	table			+= '<th>Associations</th>'
	table			+= '<th colspan="2">Actions</th></tr></thead><tbody>';
	
	if ( n == 0 )
		table += "<tr><td colspan='8'>There are currently no downloads in this category.</td></tr>";
	else
	{
		for ( var i = 0; i < n; i++ )
		{
			switch ( vendor[ i ] )
			{
				case "Fairmont":	var location = "http://www.fairmontserver.com/shared/fhomes/downloads/"; break;
				case "Century":		var location = "http://www.fairmontserver.com/shared/century/downloads/"; break;
				case "Frienship":	var location = "http://www.fairmontserver.com/shared/friendship/downloads/"; break;
				default: 			var location = "http://www.fairmontserver.com/shared/global/downloads/"; break;
			}
			
			var row_bg = ( i % 2 ) ? "even" : "odd";
			
			table += "<tr class='" + row_bg + "'><td valign='top'>" + ( i + 1 ) + ". </td>";
			table += "<td valign='top'><a href='" + location + files[ i ] + "' target='_blank'>" + files[ i ] + "</a></td>";
			table += "<td valign='top'>" + sizes[ i ] + "</td>";
			table += "<td valign='top'>" + vendor[ i ] + "</td>";
			table += "<td valign='top'><table width='225'><tbody>";
			
			var cur_assoc = assoc[ i ].split( "," );
			for ( var j = 0; j < cur_assoc.length; j++ )
				if ( cur_assoc[ j ] != "none" )
					table += "<tr><td>" + cur_assoc[ j ] + "</td></tr>";
				else
					table += "<tr><td><span class='req'>none</span></td></tr>";
			
			table += "</tbody><tfoot><tr><td></td></tr></tfoot></table></td>";
			if ( readonly[ i ] == false )
			{
				var safe_cat = category;
				//while( safe_cat.indexOf( " " ) != -1 )
				//	safe_cat = safe_cat.replace( " ", "_" );
				
				table += "<td valign='top'>[<a href='associations.php?section=12&file=" + files[ i ] + "&cat=" + safe_cat + "'>edit</a>]" + "</td>";
				table += "<td valign='top'>[<a href='#' onclick='if ( confirm( \"Are you sure you want to delete this?\" ) ) ";
				table += "window.location=\"downloads.php?section=10&action=delete&file=" + files[ i ] + "&cat=" + category + "&vendor=" + vendor_id[ i ];
				table += "\"; return false;'>delete</a>]";
			}
			else
				table += "<td colspan='2'><em>Read-Only</em></td>";
			table += "</td></tr>";
		}
	}
	table += "</tbody><tfoot><tr><td colspan='8'></td></tr></tfoot></table>";
	divFiles.innerHTML = table;
	loading();
	document.getElementsByTagName( "body" )[ 0 ].style.cursor = "default";
}


var originalCount	= 1;
var recipientCount	= 1;
function add_recipient()
{
	// get the container of all of the recipients
	var recipient 		= document.getElementById( "recipients" );
	var	recipientHTML 	= document.getElementById( "row_" + recipientCount ).innerHTML;
	var j;
	
	// add a new recipient by copying the first one
	recipientCount++;
	recipient.innerHTML += '<div id="row_' + recipientCount + '">' + recipientHTML + "</div>";
	
	
	for( j = ( originalCount + 1 ); j <= recipientCount; j++ )
	{
		var newRecipient 	= document.getElementById( "row_" + j );
		var inputs			= newRecipient.getElementsByTagName( "input" );
		var labels			= newRecipient.getElementsByTagName( "label" );
		var i, n 			= inputs.length;
		
		// now change the id's and names of all of the little states so that they can be differentiated when the form is posted
		for ( i = 0; i < n; i++ )
		{
			if ( inputs[ i ].type == "checkbox" )
			{
				inputs[ i ].name = j + inputs[ i ].name.substr( 1 );
				inputs[ i ].id = j + inputs[ i ].id.substr( 1 );
				inputs[ i ].checked = false;
			}
			else
				inputs[ i ].value = "";
		}
		
		n = labels.length;
		
		// don't forget those labels	
		for ( i = 0; i < n; i++ )
		{
			var label_for = labels[ i ].getAttribute( "for" );
			if ( label_for.indexOf( "state" ) != -1 )
			{
				labels[ i ].setAttribute( "for", j + label_for.substr( 1 ) )
			}
		}
	}
}


function set_num_recipients( value )
{
	recipientCount = value;
	originalCount = value;
}


function check_errors( indices, prefix, permissions )
{
	var ids 		= indices.split( "|" );
	var n			= ids.length;
	var disabled 	= true;
	
	for ( var i = 0; i < n; i++ )
		if ( document.getElementById( prefix + ids[ i ] ).checked )
			disabled = false;
	
	if ( permissions >= 2 )
		document.getElementById( 'button_read' ).disabled = disabled;
	if ( permissions >= 3 )
		document.getElementById( 'button_delete' ).disabled = disabled;
}


function disable( id ) 
{
	var object 			= document.getElementById( id );
	object.disabled 	= true;
}


function enable( id ) 
{
	var object 			= document.getElementById( id );
	object.disabled 	= false;
}


function export_csv()
{
	x_export_csv( export_csv_cb );
}


function export_csv_cb( file )
{
	window.location = file;
}


function load_content( page, vendor, url )
{
	x_load_content( page, vendor, url, load_content_cb );
}


function load_content_cb( arguments )
{
	arguments 		= arguments.split( "|" );
	var page		= document.getElementById( "page_id" ).value;
	
	var editor = 	'<form name="form_content" id="form_content" method="post" action="' + arguments[ 0 ] + '" onsubmit="save_changes();">';
	editor +=		'<input type="hidden" id="page_vendor" name="page_vendor" value="' + arguments[ 1 ] + '" />';
	editor +=		'<table cellspacing="0" cellpadding="0" width="100%">';
	editor +=		'<tbody><tr><td colspan="2"><textarea id="page_content" name="page_content" class="widgEditor nothing">' + arguments[ 2 ] + '</textarea></td></tr>';
	editor +=		'</tbody>';
	editor +=		'<tfoot><tr><td align="left">';
	editor +=		'<strong>Note: </strong>Do not paste formatted content from a word processor directly into this window.';
	editor +=		'</td><td align="right">';
	editor +=		'<input type="submit" value="Publish" style="margin:4px;" />';
	editor +=		'</td></tr></tfoot>';
	editor +=		'</table>';
	editor +=		'</form>';
	
	document.getElementById( "editor" ).innerHTML = editor;
	widgInit();
}


function save_changes()
{
	widg.updateWidgInput();
	x_save_changes( document.getElementById( "page_content" ).value, document.getElementById( "page_id" ).value, document.getElementById( "page_vendor" ).value, save_changes_cb );
}


function save_changes_cb( arguments )
{
}


function load_order( number )
{
	x_load_order( number, load_order_cb );
}


function load_order_cb( values )
{
	if ( values != "" )
	{
		var row 		= values.split( "|" );
		var name 		= document.getElementById( "plan_name" );
		var series 		= document.getElementById( "plan_series" );
		var short_desc 	= document.getElementById( "plan_short_desc" );
		var long_desc 	= document.getElementById( "plan_long_desc" );
		var sq_foot		= document.getElementById( "plan_sq_foot" );
		var bedrooms	= document.getElementById( "plan_bedrooms" );
		var bathrooms	= document.getElementById( "plan_bathrooms" );
		var single_sec	= document.getElementById( "plan_sections_single" );
		var multi_sec	= document.getElementById( "plan_sections_multi" );
		var modular		= document.getElementById( "plan_modular" );
		var i			= 0;
		
		// Load the series
		for ( i = 0; i < series.length; i++ )
			if ( series.options[ i ].value == row[ 0 ] )
				series.options[ i ].selected = true;
				
		// Load the bedrooms
		for ( i = 0; i < bedrooms.length; i++ )
			if ( bedrooms.options[ i ].value == row[ 5 ] )
				bedrooms.options[ i ].selected = true;
				
		// Load the bathrooms
		for ( i = 0; i < bathrooms.length; i++ )
			if ( bathrooms.options[ i ].value == row[ 6 ] )
				bathrooms.options[ i ].selected = true;
				
		name.value 			= row[ 1 ];		// Name
		short_desc.value 	= row[ 2 ];		// Short Description
		long_desc.value		= row[ 3 ];		// Long Description
		sq_foot.value		= row[ 4 ];		// Square Footage
		
		if ( row[ 7 ] == "1" )				// Single or Multiple Sectioning
			multi_sec.checked = true;
		else
			single_sec.checked = true;
			
		modular.checked = row[ 8 ];			// Modular Home
	}
}


function set_focus( id )
{
	object = document.getElementById( id );
	object.focus();
}


function select_all( indices, prefix, button, permissions )
{
	var ids = indices.split( "|" );
	var n	= ids.length;
	var i;
			
	if ( button.value == "Select All" )
	{
		for ( i = 0; i < n; i++ )
			document.getElementById( prefix + ids[ i ] ).checked = true;
		button.value = "Deselect All";
		if ( permissions >= 2 )
			document.getElementById( 'button_read' ).disabled = false;
		if ( permissions >= 3 )
			document.getElementById( 'button_delete' ).disabled = false;
	}
	else
	{
		for ( i = 0; i < n; i++ )
			document.getElementById( prefix + ids[ i ] ).checked = false;
		button.value = "Select All";
		
		if ( permissions >= 2 )
			document.getElementById( 'button_read' ).disabled = true;
		if ( permissions >= 3 )
			document.getElementById( 'button_delete' ).disabled = true;
	}
}


function toggle_display( id ) 
{
	var object 	= document.getElementById( id );
	var state 	= object.style.display;
	if ( state == 'none' )
		object.style.display = 'block';
	else if ( state != 'none' )
		object.style.display = 'none'; 
}


function toggle_enable( id ) 
{
	var object 	= document.getElementById( id );
	var state 	= object.disabled;
	if ( state == false )
		object.disabled = true;
	else if ( state != 'none' )
		object.disabled = false; 
}


function toggle_off( id ) 
{
	var object 				= document.getElementById( id );
	object.style.display 	= 'none';
}


function toggle_on( id ) 
{
	var object 				= document.getElementById( id );
	object.style.display 	= 'block';
}

var pseudo 		= new Array( "[b]", "[/b]", "[i]", "[/i]", "[u]", "[/u]", "[list]", "[/list]", "[item]", "[/item]", "[link]", "[/link]", 
							 "[h2]", "[/h2]", "[h3]", "[/h3]", "\n" );
var tag			= new Array( "<strong>", "</strong>", "<em>", "</em>", "<u>", "</u>", "<ul>", "</ul>", "<li>", "</li>", "<a href='", "'></a>", 
							 "<h2>", "</h2>", "<h3>", "</h3>", "<br />" );

function toggle_blinds( id, length )
{
	var object 	= document.getElementById( id );
	var state 	= object.style.display;
	if ( state == 'none' )
		new Effect.BlindDown( id, {duration:length} );
	else if ( state != 'none' )
		new Effect.BlindUp( id, {duration:length} );
}


function create_overlay( id )
{
	var overlay			= null;
	
	// first check to see if the overlay exists - if it doesn't then this is where it will be created
	if ( !( overlay = document.getElementById( id ) ) )
	{
		var page_size 					= getPageSize();
		var body_tag					= document.getElementsByTagName( "body" ).item( 0 );
		
		overlay 						= document.createElement( "div" );
		overlay.setAttribute( 'id', id );
		overlay.style.display 			= 'none';
		overlay.style.position 			= 'absolute';
		overlay.style.top 				= '0';
		overlay.style.left 				= '0';
		overlay.style.zIndex 			= '90';
		overlay.style.width 			= '100%';
		overlay.style.height			= ( page_size[ 1 ] + 'px' );
		body_tag.insertBefore( overlay, body_tag.firstChild );
	}
	
	return overlay;
}

var BUILDER_HEIGHT	= 440;
var BUILDER_WIDTH	= 720;
var EXTERIOR		= "interactive_builder.swf";
var INTERIOR		= "interactive_builder2.swf";
var builder_type	= EXTERIOR;
var builder_dir		= "";

function interior_builder()
{
	builder_type = INTERIOR;
	build_interactive();
}

function exterior_builder()
{
	builder_type = EXTERIOR;
	build_interactive();
}


function toggle_window( id, title )
{
	var page_size 		= getPageSize();
	var page_scroll 	= getPageScroll();
	var overlay 		= create_overlay( "win_overlay" );
	
	if ( !document.getElementById( "win_container" ) )
	{
		var container 					= document.createElement( "div" );
		container.setAttribute( 'id', 'win_container' );
		container.style.display			= 'none';
		container.style.background		= '#eeeeee';
		container.style.border 			= '1px solid #666666';
		container.style.position		= 'absolute';
		container.style.width			= '525px';
		container.style.zIndex 			= '100';
		container.style.left			= ( ( page_size[ 0 ] - 10 - 525 ) / 2 ) + 'px';
		container.style.top				= ( page_scroll[ 1 ] + ( page_size[ 3 ] - 35 - 350 ) / 2 ) + 'px';
		container.style.padding			= '5px';
		//overlay.onclick 				= function () { toggle_display( "win_overlay" ); return false; }
		
		var body_tag					= document.getElementsByTagName( "body" ).item( 0 );
		body_tag.insertBefore( container, overlay.nextSibling );
	}

	x_get_html( id, title, toggle_window_cb );
}


function toggle_window_cb( arguments )
{
	var args 		= arguments.split( "|" );
	var content		= args[ 0 ];
	var id			= args[ 1 ];
	var title		= args[ 2 ];
	
	document.getElementById( "win_container" ).innerHTML = "<center><div id='win_close'><a href='#' onclick='toggle_window(\"" + id + "\",\"" + title + "\")'><img src='" + closeButton + "' alt='Click here to close' /></a></div><h2>" + title + "</h2><br /><div style='width:525px; height:300px; overflow:scroll; background-color:#fff; border:1px solid #ccc;'>" + content + "</div></center>";
	toggle_display( "win_overlay" );
	toggle_display( "win_container" );		
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for ( i = 0; i != selects.length; i++ ) 
		selects[ i ].style.display = ( selects[ i ].style.display == "none" ) ? "block" : "none";
}


function build_interactive()
{
	var container 		= document.getElementById( 'bd_container' );
	
	
	var html 			= "<div id='bd_interior'><a href='javascript:interior_builder();'>Kitchen</a></div><div id='bd_exterior'><a href='javascript:exterior_builder();'>Exterior</a></div>";
	html 				+= "<div id='bd_close'><a href='javascript:toggle_builder( \"" + builder_dir + "\" );'><img src='" + closeButton + "' alt='Click here to close' /></a></div><br clear='all' />";
	html				+= '<object id="builder" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ';
	html				+= 'width="' + BUILDER_WIDTH + '" height="' + BUILDER_HEIGHT + '">';
	html				+= '<param id="bd_movie" name="movie" value="' + builder_dir + builder_type + '">';
	html				+= '<param id="bd_quality" name="quality" value="height">';
	html				+= '<embed id="bd_embed" src="' + builder_dir + builder_type + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + BUILDER_WIDTH + '" height="' + BUILDER_HEIGHT + '">';
	
	container.innerHTML = html;
}

function toggle_builder( dir )
{
	var page_size 		= getPageSize();
	var page_scroll 	= getPageScroll();
	builder_dir			= dir;
	
	var overlay = create_overlay( "bd_overlay" );
	
	if ( !document.getElementById( "bd_container" ) )
	{
		var container 					= document.createElement( "div" );
		container.setAttribute( 'id', 'bd_container' );
		container.style.display			= 'none';
		container.style.background		= '#eeeeee';
		container.style.border 			= '1px solid #666666';
		container.style.position		= 'absolute';
		container.style.zIndex 			= '100';
		container.style.left			= ( ( page_size[ 0 ] - 10 - BUILDER_WIDTH ) / 2 ) + 'px';
		container.style.top				= ( page_scroll[ 1 ] + ( page_size[ 3 ] - 35 - BUILDER_HEIGHT ) / 2 ) + 'px';
		container.style.padding			= '5px';
		
		var body_tag					= document.getElementsByTagName( "body" ).item( 0 );
		body_tag.insertBefore( container, overlay.nextSibling );
		
		exterior_builder();
	}
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for ( i = 0; i != selects.length; i++ ) 
		selects[ i ].style.display = ( selects[ i ].style.display == "none" ) ? "block" : "none";
		
	toggle_display( "bd_overlay" );	
	toggle_display( "bd_container" );	
}


function toggle_tour( file )
{
	if ( file.indexOf( "/friendship/" ) != -1 )
		var dir = 'http://75.125.60.16/~anderso6/images/vtour/';
	else
		var dir = 'http://75.125.60.16/~anderso6/images/vtour/';
	
	var IPIX_HEIGHT	= '240';
	var IPIX_WIDTH	= '350';
	var overlay		= null;
	var page_size 	= getPageSize();
	var page_scroll = getPageScroll();
	
	// first check to see if the overlay exists - if it doesn't then this is where it will be created
	if ( !( overlay = document.getElementById( "vt_overlay" ) ) )
	{
		var body_tag					= document.getElementsByTagName( "body" ).item( 0 );
		
		overlay 						= document.createElement( "div" );
		overlay.setAttribute( 'id', 'vt_overlay' );
		overlay.onclick 				= function () { toggle_tour( file ); return false;}
		overlay.style.display 			= 'none';
		overlay.style.position 			= 'absolute';
		overlay.style.top 				= '0';
		overlay.style.left 				= '0';
		overlay.style.zIndex 			= '90';
		overlay.style.width 			= '100%';
		overlay.style.height			= ( page_size[ 1 ] + 'px' );
		body_tag.insertBefore( overlay, body_tag.firstChild );
		
		virtual_tour 					= document.createElement( "div" );
		virtual_tour.setAttribute( 'id', 'virtual_tour' );
		virtual_tour.style.display		= 'block';
		virtual_tour.style.background	= '#eeeeee';
		virtual_tour.style.border 		= '1px solid #666666';
		virtual_tour.style.position		= 'absolute';
		virtual_tour.style.left			= ( ( page_size[ 0 ] - 10 - IPIX_WIDTH ) / 2 ) + 'px';
		virtual_tour.style.top			= ( page_scroll[ 1 ] + ( page_size[ 3 ] - 20 - IPIX_HEIGHT ) / 2 ) + 'px';
		virtual_tour.style.padding		= '5px';
		
		applet							= document.createElement( "applet" );
		applet.setAttribute( 'id', 'vt_applet' );
		applet.code						= 'panoapplet';
		applet.archive					= dir + 'panoapplet.jar';
		applet.width					= IPIX_WIDTH;
		applet.height					= IPIX_HEIGHT;
		
		vt_file							= document.createElement( "param" );
		vt_file.setAttribute( 'id', 'file' );
		vt_file.name					= "file";
		vt_file.value					= file;
			
		splash							= document.createElement( "param" );
		splash.setAttribute( 'id', 'altSplash' );
		splash.name						= "altSplash";
		splash.value					= dir + 'rrlogo.jpg';
		
		autospin						= document.createElement( "param" );
		autospin.setAttribute( 'id', 'autospin' );
		autospin.name					= "autospin";
		autospin.value					= '-30';
		
		infoPage						= document.createElement( "param" );
		infoPage.setAttribute( 'id', 'infoPage' );
		infoPage.name					= "infoPage";
		infoPage.value					= dir + 'rrlogo.jpg';
		
		applet.appendChild( vt_file );
		applet.appendChild( splash );
		applet.appendChild( autospin );
		applet.appendChild( infoPage );
		
		virtual_tour.appendChild( applet );
		virtual_tour.innerHTML += "<br /><center><a href='#' onclick='function () {toggle_display( \"vt_overlay\" ); return false; }'>Click here to close</a></center>";
		overlay.appendChild( virtual_tour );
	}
	else
	{
		if ( vt_file = document.getElementById( "file" ) )
		{
			vt_file.value	= file;
		}
	}
	
	var object 	= document.getElementById( "vt_overlay" );
	var state 	= object.style.display;
	if ( state == 'none' )
		object.style.display = 'block';
	else if ( state != 'none' )
	{
		var body_tag		= document.getElementsByTagName( "body" ).item( 0 );
		body_tag.removeChild( object );
	}
}


function insert( openIndex, closeIndex )
{
	var textarea = document.getElementById( "post" );

	if ( ( clientVer >= 4 ) && is_ie && is_win ) 
	{
		theSelection = document.selection.createRange().text;
		if ( !theSelection ) 
		{
			textarea.value += pseudo[ openIndex ] + pseudo[ closeIndex ];
			textarea.focus();
			preview( textarea.value );
			return;
		}
		document.selection.createRange().text = pseudo[ openIndex ] + theSelection + pseudo[ closeIndex ];
		textarea.focus();
		preview( textarea.value );
		return;
	}
	else if ( textarea.selectionEnd && ( textarea.selectionEnd - textarea.selectionStart > 0 ) )
	{
		mozWrap( textarea, pseudo[ openIndex ], pseudo[ closeIndex ] );
		preview( textarea.value );
		return;
	}
	else
	{
		textarea.value += pseudo[ openIndex ] + pseudo[ closeIndex ];
		textarea.focus();
	}
	storeCaret( textarea );
	preview( textarea.value );
}

function payment_calc()
{
  loan			= document.calc.loan.value;
  down_payment	= document.calc.down_payment.value;
  interest 		= document.calc.interest.value / 100.0;
  years			= document.calc.years.value;
  
  if ( down_payment == "" )  
  	down_payment = "0";
	
  monthly		= interest / 12;
  payments		= years * 12;
  prin			= loan - down_payment;
  monthly		= Math.floor( ( prin * monthly ) / ( 1 - Math.pow( ( 1 + monthly ), ( -1 * payments ) ) ) * 100 ) / 100;
  
  document.calc.payments.value	= payments;
  document.calc.monthly.value	= monthly;
}

// From http://www.massless.org/mozedit/
function mozWrap( textarea, open, close )
{
	var selLength 	= textarea.textLength;
	var selStart 	= textarea.selectionStart;
	var selEnd 		= textarea.selectionEnd;
	if ( selEnd == 1 || selEnd == 2 )
		selEnd = selLength;

	var s1 = ( textarea.value ).substring( 0, selStart );
	var s2 = ( textarea.value ).substring( selStart, selEnd )
	var s3 = ( textarea.value ).substring( selEnd, selLength );
	textarea.value = s1 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret( textEl ) 
{
	if ( textEl.createTextRange ) 
		textEl.caretPos = document.selection.createRange().duplicate();
}

function fixFlash(){
	theObjects = document.getElementsByTagName("object");
	for(var i = 0; i < theObjects.length; i++){
			theObjects[i].outerHTML = theObjects[i].outerHTML;
	}
}

function checkPlan(){
	if(document.form_search.canada.checked == true) {
		document.getElementById('us_choice').style.display = 'none';
		document.getElementById('canada_choice').style.display = 'inline';
		if(document.getElementById('us_label'))
			document.getElementById('us_label').style.display = 'none';
		if(document.getElementById('canada_label'))
			document.getElementById('canada_label').style.display = 'inline';
	}
	else {
		document.getElementById('canada_choice').style.display = 'none';
		document.getElementById('us_choice').style.display = 'inline';
		if(document.getElementById('canada_label'))
			document.getElementById('canada_label').style.display = 'none';
		if(document.getElementById('us_label'))
			document.getElementById('us_label').style.display = 'inline';
	}
}