

function Site
(
  opts
)
		{
		this.opts = ( opts  ?  opts  :  new Object() );

		this.opts.alert = ( (typeof this.opts.alert) != 'undefined'  ?  this.opts.alert  :  true );
		this.opts.domain = ( (typeof this.opts.domain) != 'undefined'  ?  this.opts.domain  :  document.domain );
		this.opts.linkWithDoc = ( (typeof this.opts.linkWithDoc) != 'undefined'  ?  this.opts.linkWithDoc  :  false );
		this.opts.masterDoc = ( (typeof this.opts.masterDoc) != 'undefined'  ?  this.opts.masterDoc  :  '' );
		this.opts.titleSuffix = ( (typeof this.opts.titleSuffix) != 'undefined'  ?  this.opts.titleSuffix  :  this.opts.domain );

		if ( this.opts.linkWithDoc && (typeof document.site) == 'undefined' )	document.site = this;

		this.rXPrefix = this.COMMENT_START + this.opts.domain + this.opts.masterDoc.replace( /\//g, '\\/' ) + '\\s*\\<';


		this.url = window.location.search;
		if ( (typeof this.url) == 'undefined'  ||  this.url.length < 1 )	
			this.url = this.opts.defaultURL;
		else
			this.url = unescape( this.url.substring( 1 ) );	// drop ?

		this.status = null;
		this.statusText = null;


		var xreq = ( window.XMLHttpRequest  ?  new XMLHttpRequest()  :  ( window.ActiveXObject  ?  new ActiveXObject( 'Microsoft.XMLHTTP' )  :  null ) );
		if ( !xreq )
			{
			this.status = this.FAILED;
			this.statusText = 'XMLHttpRequest not supported by this browser';
			if ( this.opts.alert )	alert( this.statusText );
			return;	// should probably just pass off to URL and leave it at that.
			}
		try {
				xreq.open( 'GET', this.url, false );	// non-asynch request
				}
		catch ( ex )
				{
				this.status = this.FAILED;
				this.statusText = 'XMLHttpRequest.open( ' + this.url + ' ) failed: ' + ex.toString();
				if ( this.opts.alert )	alert( this.statusText );
				return;
				}

		try {
				xreq.send( null );
				}
		catch ( ex )
				{
				this.status = this.FAILED;
				this.statusText = 'XMLHttpRequest.send() failed: ' + ex.toString();
				if ( this.opts.alert )	alert( this.statusText );
				return;
				}
		if ( xreq.readyState != 4 )
			{
			this.status = xreq.readyState;
			this.statusText = 'XMLHttpRequest not ready: ' + xreq.readyState;
			if ( this.opts.alert )	alert( this.statusText );
			return;
			}
		if ( xreq.status < 200  ||  xreq.status > 300 )
			{
			this.status = xreq.status;
			this.statusText = 'XMLHttpRequest returned http status: ' + xreq.status;
			if ( this.opts.alert )	alert( this.statusText );
			return;
			}

		this.html = ( (typeof xreq.responseText) != 'undefined'  ?  new String( xreq.responseText )  :  '' );
		if ( !this.html )	alert( this.html.length );
		}	// End constructor; Site()


Site.prototype.FAILED = -1;
Site.prototype.COMMENT_END = '\\>\\s*-->';
Site.prototype.COMMENT_START = '<!--\\s*';





Site.prototype.include = function
(
  tag
)
		{
		var isBody = tag.search( /body/i ) > -1;
		if ( isBody )
			{
			if ( this.status )
				{
				this.writeException( this.status, this.statusText );
				return;
				}
			else if ( !this.html )
				{
				this.writeException( 0, 'No HTML available' );
				return;
				}
			}
		else if ( !this.html )	return;


		var pS = this.html.search( new RegExp( this.rXPrefix + tag + this.COMMENT_END, 'i' ) );
		var pE = this.html.search( new RegExp( this.rXPrefix + '\\/' + tag + this.COMMENT_END, 'i' ) );

		if ( pS < 0 )
			{
			if ( isBody )	this.writeException( null, 'not found: &lt;!-- ' + this.opts.domain + this.opts.masterDoc + ' &lt;' + tag + '&gt; --&gt;' );
			return;
			}
		if ( pE < 0 )
			{
			if ( isBody )	this.writeException( null, 'not found: &lt;!-- ' + this.opts.domain + this.opts.masterDoc + ' &lt;/' + tag + '&gt; --&gt;' );
			return;
			}

		document.write( this.html.substring( pS, pE ) );
		}	// End method; include



Site.prototype.setTitle = function ()
		{
		if ( (typeof document.title) == 'undefined' )		return;
		
		try {
				window.top.document.title = document.title + ' - ' + this.opts.titleSuffix;
				}
		catch ( ex )
				{
					alert( ex );
				}

		document.write( document.title );
		}	// End method: setTitle()



Site.prototype.writeException = function
(
  sts
, diagnostic
)
		{
		var loc = ( window.location.search  ?  window.location.search.substring( 1 )  :  window.location.href );
		document.writeln( 
			'<div class="siteException">'
		+ loc
		+ '\n\nException: ' + diagnostic
		+ ( sts  ?  '\nstatus code: ' + sts  :  '' )
		+ '</div>' );

//		document.writeln( 
//		  '<' + 'script type="text/javascript" src="http://data.najf.org/error-report?loc=' + escape( loc ) 
//		+	( sts  ?  '&sts=' + sts  : '' )
//		+ '&diagnostic=' + escape( diagnostic )
//		+ '" >' );
//		document.writeln( '</' + 'script>' );
		}	// End method; writeException

