// All code copyright (c) 2001-2002 Intuit Canada Ltd. Any unauthorized duplication or reuse of this code
// is a violation of applicable laws.
// Coded by Docs Canada.
// Version 2.7 - Dec 15, 2007
//	Copy the IE7 workaround code from QuickBooks, to work around Microsoft's latest IE7 patch
//	which pops the "Page Not Found" error to the top of the frameset, rendering standard patching impossible.
// Version 2.62 - Dec 5, 2005
//	Move the TaxWiz/IM versus QuickTax/IR detection into _allframes.html and tracking_start.html files
// Version 2.6 - Dec 15, 2004
//	Make it work with "expando" blocks
// Version 2.5 - Jun 16, 2003
//	Rewrite to work with 2004 Help Platform JavaScript v.2.0.
// Version 2.4 - Nov 4, 2002
//	Disable patching for filenames that start with a tilde (~).
// Version 2.3 - Jul 22, 2002
//	Add search for frame names to allow Shown to appear in nested subframes.
//	Rewrite HiddenPopup code to work with our new popup code, as opposed to eHelp's popups.
// Version 2.2a - Jun 24, 2002
//	Added "ms-its:" substitution to allow for persistence
//	Changed path for updates folder
// Version 2.1 - Mar 04, 2002

TOPIC_UPDATEPATH =	"updates/";

var XML_FILE_NOT_FOUND = -2146697211;

var QUICKPATCH_codeVersion = "2.51";
var loci = document.location.toString().replace(/\\/gi, "/");
var ver = navigator.appVersion;
var IEVersion = parseFloat( ver.substring(ver.indexOf("MSIE ")+5, ver.indexOf(";",ver.indexOf("MSIE "))) );

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:/ProgramFiles/IntuitProgram/ (Y1) updatedpath/ (Y2) internalpath/ (Y3) topicfile.html (Y4) #bookmarkdata
	var Y1 = loci.lastIndexOf(TOPIC_UPDATEPATH);
	var Y2 = Y1 + TOPIC_UPDATEPATH.length;
	var tInternal = loci.substring(Y2,loci.length);
	var Y4 = tInternal.indexOf("#");		Y4 = (Y4 < 0) ? loci.length : Y4;
	var tFileInternal = tInternal.substring(0,Y4);
	var Y3 = tFileInternal.lastIndexOf("/")+1;	Y3 = (Y3 < 0) ? 0 : Y3;

	TOPIC_EXTPATH = loci.substring(X,Y1);
	TOPIC_CHMNAME = "";
	TOPIC_PATH = tFileInternal.substring(0,Y3);
	TOPIC_FILENAME = tFileInternal.substring(Y3,tFileInternal.length);
	TOPIC_BOOKMARK = tInternal.substring(Y4,tInternal.length);
} else {	// "*.chm::/"
	// ----- C:/ProgramFiles/IntuitProgram/ (Y1) help.chm::/ (Y2) internalpath/ (Y3) topicfile.html (Y4) #bookmarkdata
	var Y2 = loci.lastIndexOf("::/");
	var tExternal = loci.substring(X,Y2);
	var Y1 = tExternal.lastIndexOf("/")+1;
	var tInternal = loci.substring(Y2+3,loci.length);
	var Y4 = tInternal.indexOf("#");		Y4 = (Y4 < 0) ? loci.length : Y4;
	var tFileInternal = tInternal.substring(0,Y4);
	var Y3 = tFileInternal.lastIndexOf("/")+1;	Y3 = (Y3 < 0) ? 0 : Y3;

	TOPIC_EXTPATH = tExternal.substring(0,Y1);
	TOPIC_CHMNAME = tExternal.substring(Y1,tExternal.length) + "::/";
	TOPIC_PATH = tFileInternal.substring(0,Y3);
	TOPIC_FILENAME = tFileInternal.substring(Y3,tFileInternal.length);
	TOPIC_BOOKMARK = tInternal.substring(Y4,tInternal.length);
}


// count the "/"s in TOPIC_UPDATEPATH + TOPIC_PATH
var look = 0;
var lookin = TOPIC_UPDATEPATH + TOPIC_PATH;
TOPIC_REVERSEPATH = "";
do {
	var look = lookin.indexOf("/",look+1);
	if (look > -1) TOPIC_REVERSEPATH += "../";
} while (look > -1);

// look for a filename beginning with a tilde (~)
if (TOPIC_FILENAME.substring(0,1) != "~") {
// ========== NOT A TEMP FILE BEGINNING WITH "~" ========== \\

if ( IEVersion < 5.5 ) {
// ========== IE 5.01 OR LESS ========== \\

if (X != 8) {
	var oldIEcode = TOPIC_EXTPATH + TOPIC_UPDATEPATH + "_updatedlist.js";
	document.writeln("<script src=\"" + oldIEcode + "\" language=\"JavaScript\"></script>");
}

// ========== IE 5.01 OR LESS ========== \\
} else {
// ========== IE 5.5 OR GREATER ========== \\

if (window.self == window.top) {
	window.top.location.replace( "ms-its:" + TOPIC_EXTPATH + TOPIC_CHMNAME + "_allframes.html#" + TOPIC_PATH + TOPIC_FILENAME );
} else {
//	frameShown = findFrame(window.top, "Shown");
	frameShown = top.Shown;
	if (frameShown == null) { frameShown = window.self; }

	if (window.self.name == "Hidden") {
		top.IntuitHelpCheck = "TRUE";
		window.top.fileHiddenString = window.self.location.toString();
		frameShown.location.replace( window.top.Hidden.location );
	}
	if (window.self.name == "Shown") {
		if (!top.IntuitHelpCheck) {
			window.top.fileHiddenString = "";
			// ========== IE 7 OR GREATER ========== \\
			if (IEVersion >= 7)
			{
				var sPushTopic = TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME;
				var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.load(sPushTopic);
				if (xmlDoc.parseError.errorCode != XML_FILE_NOT_FOUND)
				{
					window.top.Hidden.location.replace( "file:///" + sPushTopic );
				}
			}
			else
			{
				window.top.Hidden.location.replace( "file:///" + TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME );
			}
		} else {
			top.IntuitHelpCheck = "";
		}
	}
	if (window.self.name.substr(0,7) == "popup__") {
		if (!top.IntuitHelpCheck) {
			window.top.fileHiddenString = "";
			window.top.fileHiddenFrameObj = window.self;
			// ========== IE 7 OR GREATER ========== \\
			if (IEVersion >= 7)
			{
				var sPushTopic = TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME;
				var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.load(sPushTopic);
				if (xmlDoc.parseError.errorCode != XML_FILE_NOT_FOUND)
				{
					window.top.HiddenPopup.location.replace( "file:///" + sPushTopic );
				}
			}
			else
			{
				window.top.HiddenPopup.location.replace( "file:///" + TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME );
			}
		} else {
			top.IntuitHelpCheck = "";
		}
	}
	if (window.self.name.substr(0,8) == "_expando") {
		if (!top.IntuitHelpCheck) {
			window.top.fileHiddenString = "";
			window.top.fileHiddenFrameObj = window.self;
			// ========== IE 7 OR GREATER ========== \\
			if (IEVersion >= 7)
			{
				var sPushTopic = TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME;
				var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.load(sPushTopic);
				if (xmlDoc.parseError.errorCode != XML_FILE_NOT_FOUND)
				{
					window.top.HiddenPopup.location.replace( "file:///" + sPushTopic );
				}
			}
			else
			{
				window.top.HiddenPopup.location.replace( "file:///" + TOPIC_EXTPATH + TOPIC_UPDATEPATH + TOPIC_PATH + TOPIC_FILENAME );
			}
		} else {
			top.IntuitHelpCheck = "";
		}
	}
	if (window.self.name == "HiddenPopup") {
		top.IntuitHelpCheck = "POPUP";
		window.top.fileHiddenString = window.self.location.toString();
		window.top.fileHiddenFrameObj.location.replace( window.top.HiddenPopup.location );
	}
}

// ========== IE 5.5 OR GREATER ========== \\
}

// ========== NOT A TEMP FILE BEGINNING WITH "~" ========== \\
}

function findFrame( pMasterFrame, pFrameName ) {
	for (var i=0; i<pMasterFrame.document.frames.length; i++) {
		var myFrame = pMasterFrame.document.frames[i];
		if (myFrame.name == pFrameName) {
			return myFrame;
		}
		if (myFrame.document.frames.length > 0) {
			var mySub = findFrame( myFrame, pFrameName );
			if (mySub != null) {
				return mySub;
			}
		}
	}
}
