/**
 * These are "utility" functions, used to help out some
 * of the presentation features of the Floor Designer but
 * not necessarily part of the mainstream behavior.
 */
function fdSetUserAgent()
{
	var pos = navigator.userAgent.indexOf('MSIE');
	if (pos > -1) {
		hiddenValue = document.getElementById('strUserAgent');
		if (hiddenValue) {
			hiddenValue.value = 'MSIE';
		}
	}
	return;
}
function fdSetDimensions(layout, wa, ha, wb, hb)
{
	// Clear out any currently entered dimensions
	for (i = 1; i < 3; i++) {
		document.getElementById('w' + i + 'a').value = '';
		document.getElementById('w' + i + 'ai').value = '';
		document.getElementById('h' + i + 'a').value = '';
		document.getElementById('h' + i + 'ai').value = '';
		if (i > 1) {
			document.getElementById('w' + i + 'b').value = '';
			document.getElementById('w' + i + 'bi').value = '';
			document.getElementById('h' + i + 'b').value = '';
			document.getElementById('h' + i + 'bi').value = '';
		}
	}
	
	// Now show the specified preset size
	document.getElementById('layout' + layout).checked = true;
	document.getElementById('w' + layout + 'a').value = wa;
	document.getElementById('h' + layout + 'a').value = ha;
	if (layout > 1) {
		document.getElementById('w' + layout + 'b').value = wb;
		document.getElementById('h' + layout + 'b').value = hb;
	}
	return;	
}
/**
 * These functions are used during the pattern selection phase (step 2), and
 * partially in the custom editor phase (step 3).
 */
function fdSetPatternStyleWarning(className)
{
	var patternName = className.substring(0, className.length - 1).replace('Pattern', '');
	
	// See if all of the selected tile styles for this pattern
	// are of the same size; if so, then allow submission
	// otherwise show a warning message
	var blnIsMismatch = false;
	var strTileSize = '';
	
	if (typeof(g_fdTileStyles) != 'undefined' && typeof(g_fdTileStyleSizes) != 'undefined') {
		if (g_fdTileStyles.length == g_fdTileStyleSizes.length && g_fdTileStyles.length > 0) {

			arrSelects = document.getElementsByTagName('select');
			intSelectCount = arrSelects.length;
			for (i = 0; i < intSelectCount; i++) {
				
				theSelect = arrSelects[i];
				if (theSelect.id.indexOf(patternName) == 0) {
					
					testStyle = theSelect.options[theSelect.selectedIndex].value;
					
					intStyleCount = g_fdTileStyles.length;
					for (j = 0; j < intStyleCount; j++) {
						if (g_fdTileStyles[j] == testStyle) {
							if (strTileSize == '') {
								strTileSize = g_fdTileStyleSizes[j];
							}
							else {
								if (strTileSize != g_fdTileStyleSizes[j]) {
								
									// Try to force this option to the same tile size as the previous style
									blnFoundSizeMatch = false;
									for (k = 0; k < theSelect.options.length; k++) {
										testStyle = theSelect.options[k].value;
										for (m = 0; m < intStyleCount; m++) {
											if (g_fdTileStyles[m] == testStyle) {
												if (strTileSize == g_fdTileStyleSizes[m]) {
													theSelect.options[k].selected = true;
													blnFoundSizeMatch = true;
													break;
												}
											}
										}
										if (blnFoundSizeMatch) {
											break;
										}
									}
								
									blnIsMismatch = true;
								}
							}
							break;
						}
						
					}
					
				}
				
			}

		}
	}
	
	if (blnIsMismatch) {
		theMsg = document.getElementById('fdTileSizeMismatchMessage');
		if (theMsg) {
			theMsg.style.display = 'block';
			theMsg.style.visibility = 'visible';
		}
		theButton = document.getElementById('submit');
		if (theButton) {
			theButton.disabled = true;
		}
	}
	else {
		theMsg = document.getElementById('fdTileSizeMismatchMessage');
		if (theMsg) {
			theMsg.style.display = 'none';
			theMsg.style.visibility = 'hidden';
		}
		theButton = document.getElementById('submit');
		if (theButton) {
			theButton.disabled = false;
		}
	}
	return;
}
function fdSetPatternStyle(pattern, className, theSelect)
{
	theCheck = document.getElementById('pattern' + pattern);
	if (theCheck) {
		theCheck.checked = true;
	}
	
	// Show the appropriate color selections for this style
	theDiv = document.getElementById(className + '_' + theSelect.options[theSelect.selectedIndex].value);
	if (theDiv) {
		arrDivs = document.getElementsByTagName('div');
		intDivCount = arrDivs.length;
		for (i = 0; i < intDivCount; i++) {
			testDiv = arrDivs[i];
			if (testDiv.id.indexOf(className + '_') == 0) {
				testDiv.style.display = 'none';
				testDiv.style.visibility = 'hidden';
			}
		}
		
		theDiv.style.display = 'block';
		theDiv.style.visibility = 'visible';

		// Try to show the default color for this style
		var defaultColor = '';
		if (typeof(g_fdTileStyles) != 'undefined' && typeof(g_fdTileStyleColors) != 'undefined') {
			intStyleCount = g_fdTileStyles.length;
			for (j = 0; j < intStyleCount; j++) {
				if (g_fdTileStyles[j] == theSelect.options[theSelect.selectedIndex].value) {
					defaultColor = g_fdTileStyleColors[j];
					break;
				}
			}
		}
		if (defaultColor != '') {
			fdSetPatternColor(pattern, className, defaultColor);
		}
		
		// Verify that this pattern configuration is safe for submission
		fdSetPatternStyleWarning(className);
	}
	return;
}
function fdSetPatternColor(pattern, className, colorCode)
{
	theCheck = document.getElementById('pattern' + pattern);
	if (theCheck) {
		theCheck.checked = true;
	}
	
	theSelect = document.getElementById(className.replace('Pattern', ''));
	if (theSelect) {
		tileStyle = theSelect.options[theSelect.selectedIndex].value;
	}
	
	// Check to see if there is an image to associate with this tile color
	g_fdTileImageSingle = '';
	var strTileColorStyle = colorCode.toUpperCase() + '|' + tileStyle;
	
	var intTileStyleColorTitleCount = g_fdTileStyleColorTitles.length;
	for (i = 0; i < intTileStyleColorTitleCount; i++) {
		if (g_fdTileStyleColorTitles[i][0] == strTileColorStyle) {
			g_fdTileImageSingle = g_fdTileStyleColorTitles[i][3];
			break;
		}
	}
		
	// Show this new color in the preview by updating
	// all of the appropriate tiles
	arrDivs = document.getElementsByTagName('div');
	intDivCount = arrDivs.length;
	blnUpdateSummary = false;
	for (i = 0; i < intDivCount; i++) {
		testDiv = arrDivs[i];
		if (testDiv.className == className) {
			if (g_fdTileImageSingle != '') {
				testDiv.style.background = 'url(' + g_fdTileImageSingle + ') center';
			}
			else {
				testDiv.style.background = '';
				testDiv.style.backgroundColor = colorCode;
			}

			if (fdSetTileStyleColor(testDiv, tileStyle, colorCode)) {
				blnUpdateSummary = true;
			}
		}
	}
	if (blnUpdateSummary) {
		fdSetStyleColorSummary();
	}	
	// Update a hidden form field to track the color selection
	hiddenValue = document.getElementById(className + '_color');
	if (hiddenValue) {
		hiddenValue.value = colorCode;
		
		// Verify that this pattern configuration is safe for submission
		fdSetPatternStyleWarning(className);
	}
	return;
}
/**
 * These functions are used during the custom editor phase. 
 */
function fdEnableFillTile()
{
	g_blnColorCells = true;
}
function fdDisableFillTile()
{
	g_blnColorCells = false;
	fdSetStyleColorSummary();
}
function fdFillHover(theCell)
{
	if (g_blnColorCells) {
		if (g_fdTileImageSingle != '') {
			theCell.style.background = 'url(' + g_fdTileImageSingle + ') center';
		}
		else {
			theCell.style.background = '';
			theCell.style.backgroundColor = g_fdTileColorSingle;
		}
		fdSetTileStyleColor(theCell, g_fdTileStyleSingle, g_fdTileColorSingle);
	}
}
function fdFillTile(theCell)
{
	if (g_fdTileImageSingle != '') {
		theCell.style.background = 'url(' + g_fdTileImageSingle + ') center';
	}
	else {
		theCell.style.background = '';
		theCell.style.backgroundColor = g_fdTileColorSingle;
	}
	fdSetTileStyleColor(theCell, g_fdTileStyleSingle, g_fdTileColorSingle);
	fdSetStyleColorSummary();
}
function fdFillLine(lineId)
{
	if (g_fdTileStyleSingle && g_fdTileStyleSingle != '' && g_fdTileColorSingle && g_fdTileColorSingle != '') {
		arrDivs = document.getElementsByTagName('div');
		intDivCount = arrDivs.length;
		for (i = 0; i < intDivCount; i++) {
			testDiv = arrDivs[i];
			if (testDiv.id.indexOf('fdTile') == 0 && testDiv.id.indexOf(lineId) > 0) {
				if (g_fdTileImageSingle != '') {
					testDiv.style.background = 'url(' + g_fdTileImageSingle + ') center';
				}
				else {
					testDiv.style.background = '';
					testDiv.style.backgroundColor = g_fdTileColorSingle;
				}
				fdSetTileStyleColor(testDiv, g_fdTileStyleSingle, g_fdTileColorSingle);
			}
		}
	}
	else if (!(g_fdTileStyleSingle) || g_fdTileStyleSingle != '') {
		alert('Please select a line fill tile style.');
	}
	else if (!(g_fdTileColorSingle) || g_fdTileColorSingle != '') {
		alert('Please select a line fill tile color.');
	}
	fdSetStyleColorSummary();
	return;
}
function fdFillRow(intRow)
{
	fdFillLine('_r' + intRow + '_');
	return;
}
function fdFillCol(intCol)
{
	fdFillLine('_c' + intCol + '_');
}
function fdSetTileStyleColor(theCell, tileStyle, tileColor)
{
	cellId = theCell.id.substring(0, theCell.id.indexOf('_') + 1);
	hiddenValue = document.getElementById(cellId);
	if (hiddenValue) {
		hiddenValue.value = tileColor.toUpperCase() + '|' + tileStyle;
		return(true);
	}
	
	return(false);
}
function fdSetStyleColorSummary()
{
	intSummaryCount = 0;
	arrSummaries = new Array();
	arrSummaryCounts = new Array();

	intTempSummaryCount = 0;
	arrTempSummaries = new Array();
	arrTempSummaryCounts = new Array();
	
	arrInputs = document.getElementsByTagName('input');
	intInputCount = arrInputs.length;
	for (i = 0; i < intInputCount; i++) {
		testInput = arrInputs[i];
		if (testInput.id != '') {
			if (testInput.id.indexOf('fdTile') == 0) {
				
				blnFoundStyleColor = false;
				for (j = 0; j < intTempSummaryCount; j++) {
					if (arrTempSummaries[j] == testInput.value) {
						arrTempSummaryCounts[j] += 1;
						blnFoundStyleColor = true;
						break;
					}
				}
				
				if (!(blnFoundStyleColor)) {
					arrTempSummaries[intTempSummaryCount] = testInput.value;
					arrTempSummaryCounts[intTempSummaryCount] = 1;
					intTempSummaryCount += 1;
				}
			}
		}
	}
	
	intTileStyleColorTitleCount = g_fdTileStyleColorTitles.length;
	for (i = 0; i < intTempSummaryCount; i++) {
		for (j = 0; j < intTileStyleColorTitleCount; j++) {
			if (g_fdTileStyleColorTitles[j][0] == arrTempSummaries[i]) {
			
				if (g_fdTileStyleColorTitles[j][1] == g_fdTileStyleColorTitles[j][2]) {
					// Some tile styles have only a single color (named after the tile
					// itself), so in these cases don't repeat the color AND style
					strTileColorStyle = g_fdTileStyleColorTitles[j][2];
				}
				else {
					strTileColorStyle = g_fdTileStyleColorTitles[j][1] + ' ' + g_fdTileStyleColorTitles[j][2];
				}
				
				if (g_fdTileStyleColorTitles[j][0].indexOf(g_fdTileStyleBlank) < 0) {
					blnFoundStyleColor = false;
					for (k = 0; k < intSummaryCount; k++) {
						if (arrSummaries[k] == strTileColorStyle) {
							arrSummaryCounts[k] += arrTempSummaryCounts[i];
							blnFoundStyleColor = true;
							break;
						}
					}
					
					if (!(blnFoundStyleColor)) {
						arrSummaries[intSummaryCount] = strTileColorStyle;
						arrSummaryCounts[intSummaryCount] = arrTempSummaryCounts[i];
						intSummaryCount += 1;
					}
				}
			}
		}
	}
	
	intTileCount = 0;
	strSummary = '';
	for (i = 0; i < intSummaryCount; i++) {
		intTileCount += arrSummaryCounts[i];
		strSummary += arrSummaryCounts[i] + ' - ' + arrSummaries[i] + ' Tile(s)<br />';
	}
	
	document.getElementById('fdTileStyleColorSummary').innerHTML = strSummary;
	document.getElementById('fdTileTotalCount').innerHTML = intTileCount;
	return;
}
function fdSetEditorTileColors(colorSet, theSelect)
{
	// Show the appropriate color selections for this style
	theDiv = document.getElementById(colorSet + theSelect.options[theSelect.selectedIndex].value);
	if (theDiv) {
		arrDivs = document.getElementsByTagName('div');
		intDivCount = arrDivs.length;
		for (i = 0; i < intDivCount; i++) {
			testDiv = arrDivs[i];
			if (testDiv.id.indexOf(colorSet) == 0) {
				testDiv.style.display = 'none';
				testDiv.style.visibility = 'hidden';
			}
		}
		
		theDiv.style.display = 'block';
		theDiv.style.visibility = 'visible';
	}
	return;
}
function fdSetEditorTileColorsSingle(theSelect)
{
	g_fdTileStyleSingle = theSelect.options[theSelect.selectedIndex].value;
	g_fdTileColorSingle = '';

	// Try to select the default color for this style
	if (typeof(g_fdTileStyles) != 'undefined' && typeof(g_fdTileStyleColors) != 'undefined') {
		intStyleCount = g_fdTileStyles.length;
		for (j = 0; j < intStyleCount; j++) {
			if (g_fdTileStyles[j] == theSelect.options[theSelect.selectedIndex].value) {
				g_fdTileColorSingle = g_fdTileStyleColors[j];
				fdSetEditorTileColorTitleSingle(g_fdTileStyleSingle, g_fdTileColorSingle); 
				break;
			}
		}
	}
	
	fdSetEditorTileColors('fdTileColorsSingle_', theSelect);
	return;
}
function fdSetEditorTileColorOptionSingle(colorCode)
{
	g_fdTileColorSingle = colorCode;
	fdSetEditorTileColorTitleSingle(g_fdTileStyleSingle, g_fdTileColorSingle); 
	return;
}
function fdSetEditorTileColorTitleSingle(tileStyle, colorCode)
{
	g_fdTileImageSingle = '';
	colorCode = colorCode.toUpperCase();
	strTileColorStyle = colorCode + '|' + tileStyle;
	
	intTileStyleColorTitleCount = g_fdTileStyleColorTitles.length;
	for (i = 0; i < intTileStyleColorTitleCount; i++) {
		if (g_fdTileStyleColorTitles[i][0] == strTileColorStyle) {
			g_fdTileImageSingle = g_fdTileStyleColorTitles[i][3];
			document.getElementById('fdTileColorTitleSingle').innerHTML = 'Selected Color: ' + g_fdTileStyleColorTitles[i][1];
			break;
		}
	}

	return;				
}