/***********************************************************************\
*                              MAIN SETUP                               *
\***********************************************************************/

//---------------------------------------------------------------------\\
//-----     ATTACH ALL STARTUP ROUTINES TO WINDOW.ONLOAD EVENT    -----\\

window.onload=function(){
	// ----- process all custom tags and attributes (e.g. expando)
	if ( window.tagScanner ) { tagScanner(); }

	// ----- add Help Feedback Survey to the bottom
	if ( window.HELPFILE && !HELPFILE.isExpando && !HELPFILE.isPopup ) {
		if ( window.feedback_makeSurvey ) { feedback_makeSurvey(); }
	}

	// ----- required for External Expando module
	if ( window.HELPFILE && !HELPFILE.isExpando && !HELPFILE.isPopup ) {
		if ( window.expando_resizeWindow ) { document.body.onresize = expando_resizeWindow; }
	}
}


/***********************************************************************\
*      DETECT PRODUCT STATUS, HELP STATUS, AND SET GLOBAL VALUES        *
\***********************************************************************/

HELPFILE	= new createHelpObject();
PRODUCT		= new createProFileObject();

function createProFileObject() {
	this.Error		= "";
	this.Object		= null;
	this.isDesktopEdition	= ( true );
	this.isOnlineEdition	= ( (getTopicProtocol().toLowerCase() == "http://") || (getTopicProtocol().toLowerCase() == "https://") );
	this.Product		= "ProFile";
	this.Edition		= "";
	this.isReturnOpen	= false;
	this.isEasyStep		= false;
	this.isExpress		= false;
	this.isForms		= true;
	this.isEnglish		= true;
	this.isFrench		= false;
	this.Language		= "EN";
	this.isDevelopment	= ( (getTopicProtocol().toLowerCase() == "file://") && (!getTopicCHM() || !obj) );
}


//---------------------------------------------------------------------\\

function createHelpObject() {

	this.isExpando		= ( window.name.indexOf("_expando_") > -1 );
	this.isPopup		= ( window.name.indexOf("popup__") > -1 );

	this.Protocol		= getTopicProtocol();		// --  file://, http://, intu-res://
	this.Domain		= getTopicDomain();		// --  www.intuit.com/, helpcache/
	this.ExternalPath	= getTopicExternalPath();	// --  C:/Program Files/QuickTax/
	this.CHM		= getTopicCHM();		// --  helpfile.chm::/
	this.TopicPath		= getTopicPath();		// --  topicpath/
	this.TopicFilename	= getTopicFilename();		// --  topicname.html
	this.TopicBookmark	= getTopicBookmark();		// --  H7_LIMIT

	this.Paths		= new Array();
	this.Paths["css"]	= getPaths_CSS();
	this.Paths["images"]	= getPaths_Images();
	this.Paths["debug"]	= getPaths_Debug();
}


function displayHelpObject( obj ) {
	var txt = "";
	txt += "[Help Object]:";
	txt += "\n  isExpando     = ";
	try { txt += (obj.isExpando ? "TRUE" : "FALSE"); }		catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isPopup       = ";
	try { txt += (obj.isPopup ? "TRUE" : "FALSE"); }		catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  -----";
	txt += "\n  Protocol      = ";
	try { txt += obj.Protocol; }					catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  Domain        = ";
	try { txt += obj.Domain; }					catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  ExternalPath  = ";
	try { txt += obj.ExternalPath; }				catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  CHM           = ";
	try { txt += obj.CHM; }						catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  TopicPath     = ";
	try { txt += obj.TopicPath; }					catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  TopicFilename = ";
	try { txt += obj.TopicFilename; }				catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  TopicBookmark = ";
	try { txt += obj.TopicBookmark; }				catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  -----";
	txt += "\n  [Paths]";
	txt += "\n    'css'       = ";
	try { txt += obj.Paths["css"]; }				catch (e) { txt += "{READ FAILED}"; }
	txt += "\n    'images'    = ";
	try { txt += obj.Paths["images"]; }				catch (e) { txt += "{READ FAILED}"; }
	txt += "\n    'debug'     = ";
	try { txt += obj.Paths["debug"]; }				catch (e) { txt += "{READ FAILED}"; }

	return txt;
}

//---------------------------------------------------------------------\\

function getTopicProtocol() {
	var loci = document.location.toString().match(/^\w*\:\/\//);
	if (!loci) return "";
	return loci[0];
}

function getTopicDomain() {
	var loci = document.location.toString().match(/^\w*\:\/\/[^\/\\]*[\/\\]/);
	if (!loci) return "";
	loci = loci[0];
	loci = loci.substr(loci.indexOf("://")+3,loci.length);
	return loci;
}

function getTopicExternalPath() {
	if (window.TOPIC_EXTPATH && window.QUICKPATCH_codeVersion) {
		// ----- "_updated.js" has already calculated TOPIC_EXTPATH; just use it ----- \\
		var tPath = window.TOPIC_EXTPATH;
	} else {
		// ----- figure out path on our own ----- \\
		var tPath = "";
		var loci = document.location.toString().replace(/\\/g, "/");
		switch ( loci.search(":") ) {
			case 2:		// mk:@MSITStore:
				var X = 14;
				break;
			case 6:		// ms-its:
				var X = 7;
				break;
			case 4:		// file:///
				var X = 8;
				break;
		}

		if (X==8) {	// "file:///"
			// ----- C:/Development/QuickBooks/Help/ (Y2) internalpath/ (Y3) topicfile.html (Y4) #bookmarkdata
			var Y4 = loci.indexOf("#");		Y4 = (Y4 < 0) ? loci.length : Y4;
			tPath = loci.substring(X,Y4);
			var Y3 = tPath.lastIndexOf("/")+1;
			tPath = tPath.substring(0,Y3);
			var Y2 = tPath.lastIndexOf("/",tPath.length-2)+1;
			tPath = tPath.substring(0,Y2);
		} else {
			// ----- do the calc ----- \\
			// ----- C:/ProgramFiles/IntuitProgram/ (Y1) help.chm::/ (Y2) internalpath/ (Y3) topicfile.html (Y4) #bookmarkdata
			var Y2 = loci.indexOf("::/");
			tPath = loci.substring(X,Y2);
			var Y1 = tPath.lastIndexOf("/")+1;
			tPath = tPath.substring(0,Y1);
		}
	}
	return tPath;
}

function getTopicCHM() {
	var loci = document.location.toString().match(/[^\/\\]*\.chm\:\:\//i);
	if (!loci) return "";
	return loci[0];
}

function getTopicPath() {
	var loci = document.location.toString();
	if (getTopicCHM()) {
		// ----- there is a CHM ----- \\
		var Y = loci.lastIndexOf("/");
		var t = loci.substring(0,Y);
		var Y2 = loci.indexOf("::/");
		var t = loci.substring(Y2+3,Y+1);
	} else {
		// ----- there is no CHM ----- \\
		if (window.TOPIC_UPDATEPATH && window.QUICKPATCH_codeVersion) {
			// ----- we're a patched topic ----- \\
			var Y = loci.indexOf(TOPIC_UPDATEPATH) + TOPIC_UPDATEPATH.length;
			var t = loci.substring(Y,loci.length);
			var Y2 = t.lastIndexOf("/");
			var t = t.substring(0,Y2+1);
		} else {
			// ----- development enviroment, or unknown ----- \\
			// ----- just assume one dir up ----- \\
			var Y = loci.lastIndexOf("/");
			var t = loci.substring(0,Y);
			var Y2 = t.lastIndexOf("/");
			var t = loci.substring(Y2+1,Y+1);
		}
	}
	return t;
}

function getTopicFilename() {
	var loci = document.location.toString().match(/[^\/\\]*\.html?/);
	if (!loci) return "";
	return loci[0];
}

function getTopicBookmark() {
	var bookmark = document.location.hash.substring(1);
	if (!bookmark) return "";
	return bookmark;
}

//---------------------------------------------------------------------\\

function getPaths_CSS() {
}

function getPaths_Images() {
	var isCHM = ( getTopicCHM() );
	var isOnline = ( (getTopicProtocol().toLowerCase() == "http://") || (getTopicProtocol().toLowerCase() == "https://") );

	if ( isCHM ) {		// = Desktop
		return "/";
	}
	if ( isOnline ) {	// = Web edition
		return "/";
	}
	return "./";
}

function getPaths_Debug() {
}


/***********************************************************************/

function displayProFileObject( obj ) {
	var txt = "";
	txt += "[QuickTax Object]:";
	txt += "\n  isDesktopEdition  = ";
	try { txt += (obj.isDesktopEdition ? "TRUE" : "FALSE"); }		catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isOnlineEdition   = ";
	try { txt += (obj.isOnlineEdition ? "TRUE" : "FALSE"); }		catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  Product          = ";
	try { txt += obj.Product; }						catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  Edition          = ";
	try { txt += obj.Edition; }						catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  -----";
	txt += "\n  isReturnOpen      = ";
	try { txt += (obj.isReturnOpen ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isEasyStep        = ";
	try { txt += (obj.isEasyStep ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isExpress         = ";
	try { txt += (obj.isExpress ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isForms           = ";
	try { txt += (obj.isForms ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  -----";
	txt += "\n  isEnglish         = ";
	try { txt += (obj.isEnglish ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  isFrench          = ";
	try { txt += (obj.isFrench ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  Language          = ";
	try { txt += obj.Language; }						catch (e) { txt += "{READ FAILED}"; }
	txt += "\n  -----";
	txt += "\n  isDevelopment     = ";
	try { txt += (obj.isDevelopment ? "TRUE" : "FALSE"); }			catch (e) { txt += "{READ FAILED}"; }
	txt += "\n";
	txt += "\n  Error?\n";
	try { txt += obj.Error; }						catch (e) { txt += "{READ FAILED}"; }

	return txt;
}

/***********************************************************************/

function QTEZLink( pLink ) {}

function QTFormLink( pLink ) {}



