/***********************************************************************\
*                           CONDITIONAL TEXT                            *
\***********************************************************************/

function validateSKUs ( str ) {
	if (!str) str = "";
	var tLists = new flattenSKUs( str );
	var tValid = false;
	if (tLists.valid) {
		if ( PRODUCT.Product == "QuickTax" || PRODUCT.Product == "ImpôtRapide" ) {

			// ----- Conditions based on Platform (Desktop/Web) should have no use in DITA;
			// ----- these should be determined at DITA build time.
			if ( PRODUCT.isDesktopEdition && tLists.valid.indexOf(",DESKTOP,") >= 0 ) {
				tValid = true;
			}
			if ( PRODUCT.isOnlineEdition && tLists.valid.indexOf(",WEB,") >= 0 ) {
				tValid = true;
			}

			// ----- Conditions based on Product (Student, Basic, Standard, Platinum, Business).
			if ( PRODUCT.Edition ) {
				if ( PRODUCT.Edition == "BASIC" ) {
					if ( tLists.valid.indexOf(",BASIC,") >= 0 || tLists.valid.indexOf(",TAXWIZ,") >= 0 ) {
						tValid = true;
					}
				}
				if ( PRODUCT.Edition == "STUDENT" ) {
					if ( tLists.valid.indexOf(",STUDENT,") >= 0 ) {
						tValid = true;
					}
				}
				if ( PRODUCT.Edition == "STANDARD" ) {
					if ( tLists.valid.indexOf(",STD,") >= 0 || tLists.valid.indexOf(",STANDARD,") >= 0 ) {
						tValid = true;
					}
				}
				if ( PRODUCT.Edition == "PLATINUM" ) {
					if ( tLists.valid.indexOf(",PLAT,") >= 0 || tLists.valid.indexOf(",PLATINUM,") >= 0 ) {
						tValid = true;
					}
				}
				if ( PRODUCT.Edition == "BUSINESS" ) {
					if ( tLists.valid.indexOf(",BIZ,") >= 0 || tLists.valid.indexOf(",BUSINESS,") >= 0 ) {
						tValid = true;
					}
				}
			}

			// ----- Conditions based on run-time information.
			if ( PRODUCT.Edition != "BASIC" && PRODUCT.isEasyStep && tLists.valid.indexOf(",INTERVIEW,") >= 0 ) {
				tValid = true;
			}
			if ( PRODUCT.Edition == "BASIC" && PRODUCT.isEasyStep && tLists.valid.indexOf(",SIMPLE,") >= 0 ) {
				tValid = true;
			}
			if ( PRODUCT.isForms && tLists.valid.indexOf(",FORMS,") >= 0 ) {
				tValid = true;
			}
			if ( PRODUCT.isExpress && tLists.valid.indexOf(",EXPRESS,") >= 0 ) {
				tValid = true;
			}
			if ( PRODUCT.isFree && tLists.valid.indexOf(",FREE,") >= 0 ) {
				tValid = true;
			}

			if ( tLists.valid.indexOf(",DESKTOP:INTERVIEW,") >= 0
			  && ( PRODUCT.isEasyStep && PRODUCT.isDesktopEdition ) ) {
				tValid = true;
			}
		}
	} else {
		// ----- if there is no Valid string of conditions, then it's assumed valid until proven otherwise.
		tValid = true;
	}

	if (tLists.invalid) {
		// ----- NOT() terms only affect the existing positive conditions; i.e.
		// ----- "DESKTOP,NOT(STANDARD)" would be false for the WEB+BIZ product, because it failed
		// ----- the DESKTOP check, and the fact that NOT(STANDARD) is true is irrelevant.

		if ( PRODUCT.Product == "QuickTax" || PRODUCT.Product == "ImpôtRapide" ) {

			// ----- Conditions based on Platform (Desktop/Web) should have no use in DITA;
			// ----- these should be determined at DITA build time.
			if ( PRODUCT.isDesktopEdition && tLists.invalid.indexOf(",DESKTOP,") >= 0 ) {
				tValid = false;
			}
			if ( PRODUCT.isOnlineEdition && tLists.invalid.indexOf(",WEB,") >= 0 ) {
				tValid = false;
			}

			// ----- Conditions based on Product (Basic, Standard, Platinum, Business).
			if ( PRODUCT.Edition ) {
				if ( PRODUCT.Edition == "BASIC" ) {
					if ( tLists.invalid.indexOf(",BASIC,") >= 0 || tLists.invalid.indexOf(",TAXWIZ,") >= 0 ) {
						tValid = false;
					}
				}
				if ( PRODUCT.Edition == "STUDENT" ) {
					if ( tLists.invalid.indexOf(",STUDENT,") >= 0 ) {
						tValid = false;
					}
				}
				if ( PRODUCT.Edition == "STANDARD" ) {
					if ( tLists.invalid.indexOf(",STD,") >= 0 || tLists.invalid.indexOf(",STANDARD,") >= 0 ) {
						tValid = false;
					}
				}
				if ( PRODUCT.Edition == "PLATINUM" ) {
					if ( tLists.invalid.indexOf(",PLAT,") >= 0 || tLists.invalid.indexOf(",PLATINUM,") >= 0 ) {
						tValid = false;
					}
				}
				if ( PRODUCT.Edition == "BUSINESS" ) {
					if ( tLists.invalid.indexOf(",BIZ,") >= 0 || tLists.invalid.indexOf(",BUSINESS,") >= 0 ) {
						tValid = false;
					}
				}
			}

			// ----- Conditions based on run-time information.
			if ( PRODUCT.Edition != "BASIC" && PRODUCT.isEasyStep && tLists.invalid.indexOf(",INTERVIEW,") >= 0 ) {
				tValid = false;
			}
			if ( PRODUCT.Edition == "BASIC" && PRODUCT.isEasyStep && tLists.invalid.indexOf(",SIMPLE,") >= 0 ) {
				tValid = false;
			}
			if ( PRODUCT.isForms && tLists.invalid.indexOf(",FORMS,") >= 0 ) {
				tValid = false;
			}
			if ( PRODUCT.isExpress && tLists.invalid.indexOf(",EXPRESS,") >= 0 ) {
				tValid = false;
			}
			if ( PRODUCT.isFree && tLists.invalid.indexOf(",FREE,") >= 0 ) {
				tValid = false;
			}

			if ( tLists.invalid.indexOf(",DESKTOP:INTERVIEW,") >= 0
			  && ( PRODUCT.isEasyStep || PRODUCT.isDesktopEdition ) ) {
				tValid = false;
			}
		}
	}

	return tValid;
}

function flattenSKUs ( source ) {
	// ----- expand expression into two flat, complete lists of SKUs  ----- \\
	// ----- and flavours, separated and bookended by commas; i.e.    ----- \\
	// ----- list = ",BASIC,PRO,PREMIER,CONTRACTOR,ACCOUNTANT,".      ----- \\
	// ----- there is a ".valid" list string and an ".invalid" one.   ----- \\

	// ---  separate the NOT() list from the regular one  --- \\
	var tNots = source.match( /NOT\(([^\)]*)\)/gi );
	var tValid = source.replace( /NOT\([^\)]*\)/gi , "" );
	var tInvalid = "";
	if (tNots) {
		for (var j=0; j < tNots.length; j++) {
			if (tInvalid) { tInvalid += ","; }
			tInvalid += tNots[j].substring(4,tNots[j].length-1);
		}
	}

	this.valid =	atomizeSKUs( tValid ).toString();
	this.invalid =	atomizeSKUs( tInvalid ).toString();
}

function atomizeSKUs ( str ) {
	// ----- expand any "shorthand" elements to a full list of tokens ----- \\

	var tTokens = str.split(",");
	var tOutputList = new Array();

	for (var j=0; j < tTokens.length; j++) {
		tAtoms = string_trim(tTokens[j].toUpperCase());
		tAtoms = tAtoms.split(":");

		var tList = new Array();
		// -----  loop through the subelements of each list item    ----- \\
		// -----  and expand the subelements, then reassemble them. ----- \\
		var tSubElements = new Array();
		for (var k=0; k < tAtoms.length; k++) {
			var tOut = tAtoms[k];
			tList[k] = new Array();

			// ----- convert synonyms to proper code ----- \\
			var tSynOut = tOut.replace( /^(\w*)[+-]?$/i, "$1" );
			if (tSynOut == "IR") { tSynOut = "IMPÔTRAPIDE"; }
			if (tSynOut == "QT") { tSynOut = "QUICKTAX"; }
			tOut = tOut.replace( /^(\w*)([+-]?)$/, tSynOut+"$2" );


			// ----- if all else fails, just add the code you were passed ----- \\
			if ( tOut ) {
				tList[k][tList[k].length] = tOut;
			}
		}

		// -----  assemble collections of subelements into colon-separated compound elements  ----- \\
		// -----  i.e. "CB:PRO-" becomes "CB-CBK:BASIC,CB-CBK:PRO,CB-BMC:BASIC,CB-BMC:PRO"    ----- \\
		var tGrid = new Array();
		for (var k=tList.length-1; k >= 0; k--) {
			var tOldGrid = tGrid;
			tGrid = new Array();
			for (m=0; m < tList[k].length; m++) {
				// --- for each entry of existing data, append the [k] element --- \\
				if ( tOldGrid.length == 0 ) {
					tGrid[tGrid.length] = tList[k][m];
				} else {
					for (n=0; n < tOldGrid.length; n++) {
						tGrid[tGrid.length] = tList[k][m] + ":" + tOldGrid[n];
					}
				}
			}
		}

		// -----  add the expanded compound elements to the list, if they're new ----- \\
		for (var k=0; k < tGrid.length; k++) {
			if ( (tOutputList.toString()+",").indexOf(tGrid[k]+",") < 0 ) {
				tOutputList[tOutputList.length] = tGrid[k];
			}
		}
	// ----- loop to next token
	}

	if (tOutputList.length > 0) { tOutputList = "," + tOutputList + ","; }
	return tOutputList;
}

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

function string_trim ( str ) {
	str = str.replace( /^\s*/ , "" );
	str = str.replace( /\s*$/ , "" );
	return str;
}
