
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_on( id ) 
{
	var object 				= document.getElementById( id );
	object.style.display 	= 'block';
}


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


function set_display( id, state ) 
{
	var object 				= document.getElementById( id );
	object.style.display 	= state;
}


function checkmaster( value, master ) 
{
	if ( value.checked == false ) 
		master.checked = false;
}


function checkallboxes( form ) 
{
	field = document.form;
	for ( i = 0; i < field.length; i++ )
		field[ i ].checked = true ;
}


function encode_url( str ) 
{
    return escape( str ).replace( /\+/g, '%2C' ).replace( /\"/g,'%22' ).replace( /\'/g, '%27' );
}


function clear_select( id, button ) 
{
	var s = document.getElementById( id );
	var x = s.length;
	for ( i = 0; i < x; i++ )
		if ( s[ i ].selected ) 
			s[ i ].selected = false;
			
	button.disabled = true;
}


function enable_button( button ) 
{
	document.getElementById( button ).disabled = false;
}


function select_all( id ) 
{
	var s = document.getElementById( id );
	var x = s.length;
	for ( i = 0; i < x; i++ )
		s[ i ].selected = true;
}


function preview( text, div ) 
{
	document.getElementById( div ).innerHTML = "<p>" + text + "</p>";
}


function translate( text, div ) 
{
	var restore 		= "restore";
	var submit 			= "submit";
	document.getElementById( restore ).disabled = false;
	document.getElementById( submit ).disabled = false;
	text = text.replace( /\[B\]/g, "<b>" );
	text = text.replace( /\[\/B\]/g, "</b>" );
	text = text.replace( /\[I\]/g, "<em>" );
	text = text.replace( /\[\/I\]/g, "</em>" );
	text = text.replace( /\n\n/g, "</p><p>" );
	text = text.replace( /\n/g, "<br>" );
	preview( text, div );
}


function restore_text( original, textarea, div ) 
{
	document.getElementById( textarea ).value = document.getElementById( original ).value;
	translate( document.getElementById( original ).value, div, textarea );
}


function loading( img, div )
{
	document.getElementById( div ).innerHTML = "<img src='" + img + "' alt='Loading' />";
}


function activate( id ) 
{
	if ( document.getElementById( "button_" + id ).value == "Yes" ) 
	{
		document.getElementById( "button_" + id ).value = "No";
		document.getElementById( "active_" + id ).value = 0;
	} 
	else 
	{
		document.getElementById( "button_" + id ).value = "Yes";
		document.getElementById( "active_" + id ).value = 1;
	}
}


function popup( url, name, height, width ) 
{
	newwindow = window.open( url, name, 'height=' + height + ',width=' + width + ',scrollbars=yes' );
	if ( window.focus ) 
		newwindow.focus();
}


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;
}

var state_name;
function num_dealers( state ) 
{
	state_name = state;
	x_num_dealers( state, num_dealers_cb );
}


function num_dealers_cb( ret ) 
{
	
	if ( state_name.indexOf( '-' ) < 0 )
		var start = 0;
	else
		var start 	= state_name.indexOf( '-' ) + 2;	// we need to remove the prefixed country
	var end		= state_name.length;
	
	state_name 	= state_name.substring( start, end );
	
	document.getElementById( "descr" ).innerHTML = "<table border=1 width='150' height='50' ><tr><td bgcolor='#B4CFE4'><div align='center'><font color='#000000'><strong>" 
												 + state_name + " (" + ret + ")</strong></font></div></td></tr></table>";
}




function getMouseXY( e ) 
{
	if ( IE ) 
	{ // grab the x-y pos.s if browser is IE
		x = event.clientX;
		y = event.y;
	}
	else 
	{  // grab the x-y pos.s if browser is NS
		x = e.pageX;
		y = e.pageY;
	}
	  
	if ( x < 0 )
		x = 0;
	if ( y < 0 )
		y = 0; 
	
	if ( is_nav5up )
	{
		document.getElementById( "descr" ).style.left 	= x - 120 + "px";
		document.getElementById( "descr" ).style.top 	= y + 20 + "px";
	}
	
	if ( !is_nav5up && is_nav )
	{	
		document.layers[ "descr" ].left = x - 120 + "px";
		document.layers[ "descr" ].top 	= y + 20 + "px";
	}
	if ( is_ie )
	{
		document.getElementById( "descr" ).style.left	= x - 100 + "px";
		document.getElementById( "descr" ).style.top	= y + 20 + "px";
	} 
	
	return true;
}

var descStr = "";

function desc( ix )
{
	num = 0;
	if ( ix == "-1" )
	{
		descStr = "";
		num 	= 0;
	}
	else
	{
		if ( ix != index )
			descStr = "<table border=1 width='120'><tr><td bgcolor='#B4CFE4'><div align='center'><strong>" 
					+ english[ix] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></div></td></tr></table>";
		num_dealers( english[ ix ] );
	}
	
	doit( 'descr' );	
	index = ix;
}


function doit( x )
{
	if ( ( !is_nav5up ) && is_nav )
	{
		if ( x == 'subroot' )
			eval( "with (document.layers['root'].document.layers['" + x + "'].document){write(newsStr);close();}" );
		else
			eval( "with (document.layers['" + x + "'].document){write(descStr);close();}" );
	}

	if ( is_nav5up )
	{	
		if ( x == 'subroot' )
			eval( "document.getElementById('" + x + "').innerHTML=newsStr;" );
		else
			eval( "document.getElementById('" + x + "').innerHTML=descStr;" );
	}
		
	if ( is_ie )
	{
		if ( x == 'subroot' )
			eval( "document.all['" + x + "'].innerHTML = newsStr;" );
		else
			eval( "document.all['" + x + "'].innerHTML = descStr;" );
	}
}


function toggle_tour( file )
{
	var URL_IPIX 	= 'http://www.fairmontserver.com/shared/builderd/ipix/';
	var IPIX_HEIGHT	= '240';
	var IPIX_WIDTH	= '350';
	var overlay		= null;
	var page_size 	= getPageSize();
	
	// 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 ] - IPIX_WIDTH ) / 2 ) + 'px';
		virtual_tour.style.top			= ( ( page_size[ 1 ] - IPIX_HEIGHT ) / 2 ) + 'px';
		virtual_tour.style.padding		= '5px';
		
		applet							= document.createElement( "applet" );
		applet.setAttribute( 'id', 'vt_applet' );
		applet.code						= 'panoapplet';
		applet.archive					= URL_IPIX + '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					= URL_IPIX + '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					= URL_IPIX + '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;
		}
	}
	
	toggle_display( "vt_overlay" );
	toggle_display( "right-column" );
}

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 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 			= '200';
		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" );	
	toggle_display( "right-column" );
}

