//---------------------->
//Define two arrays, StoresArray and CategoryArray, to hold the information

var numItemsCol1;
var numItemsCol2;
var numItemsCol3;
var PopulateCategoryKey;
var PopulateunitNumFloorPlan;

function PopulateDropDown(CategoryKey, unitNumFloorPlan)
{
	PopulateCategoryKey = CategoryKey;
	PopulateunitNumFloorPlan = unitNumFloorPlan;
}

function PopulateDropDown2(CategoryKey, unitNumFloorPlan)
{
	/*
	* Order of data in the array is as follows:Store Name, Unit Number, Store Page URL, Phone number, Promotion flag, Category.
	* Assuming that promotion flag is a 1 or 0, where 1 means there is a promotion.
	* Category is pipe delimited
	*/
	// Populate the categories drop down list--------------->
	var insertCategory = document.getElementById("CategorySelect").options;
	
	for (x in CategoryArray)
	{
		var newCategory = document.createElement('OPTION');
		newCategory.text = CategoryArray[x][0];
		newCategory.value = CategoryArray[x][1];
		insertCategory.add(newCategory);
	}
	//-----------------------------------------------------------> 
	// Populate Stores drop down list
	var insertStore = document.getElementById("StoreSelect").options;

	for (i in StoresArray)
	{
		var StoresDropDownList = document.getElementById("StoreSelect").options;
		var StoresDropDownListSize = StoresDropDownList.length;
		var newStore = document.createElement('OPTION');
		newStore.text = StoresArray[i][0];
		newStore.value = StoresArray[i][1];
		
		for (var c = 0; c < StoresDropDownListSize; c++) 
		{
			// If you go through the whole list and the store doesn't exist, then insert it.
			if (c == (StoresDropDownListSize - 1) && StoresDropDownList[c].text != newStore.text)
			{
				insertStore.add(newStore);
			}
			if (StoresArray[i][0] == StoresDropDownList[c].text)
			{
				c = StoresDropDownListSize;
			}
		}
	}

	if (PopulateunitNumFloorPlan != "")
	{
		for (var z = 0; z < StoresArray.length; z++)
		{
			if (StoresArray[z][1] == PopulateunitNumFloorPlan)
			{
				var StoreName = StoresArray[z][0];
				z = StoresArray.length;
			}
		} 

		for (var z = 0; z < StoresArray.length; z++)
		{
			if (StoresArray[z][0] == StoreName)
			{
				document.aspnetForm.StoreSelect.value = StoresArray[z][1]; 
				DropDownSelection(document.getElementById('StoreSelect'));
				z = StoresArray.length;
			}
		} 
	}
	else if (PopulateunitNumFloorPlan == "" && PopulateCategoryKey !=  0)
	{
		document.aspnetForm.CategorySelect.value = PopulateCategoryKey; 
		DropDownSelection(document.getElementById('CategorySelect'));
	}
}


//------------------------------------------------------------->
function DropDownSelection(sender)
{
	var SelectedStoreIndices = new Array();
   	var counter = 0;
 	var numStoresPerCol = 0;

	ClearStoreSelection(); //clear all selections (if any) on the map
	
	if (sender.name == "StoreSelect")
	{
		document.aspnetForm.CategorySelect.selectedIndex=0; //Reset the selection of the category drop down list

		var w = document.aspnetForm.StoreSelect.selectedIndex;
		var selected_text = document.aspnetForm.StoreSelect.options[w].text;
		// first get the number of stores that match the selection, and store it in variable "counter"
		for (var z = 0; z < StoresArray.length; z++)
		{
			if (StoresArray[z][0] == selected_text)
			{
				HighlightUnitNumber(StoresArray[z][1]);
				SelectedStoreIndices[counter] = z;
				counter++;
			}
		}
	}
	else 
	{
		document.aspnetForm.StoreSelect.selectedIndex=0; //Reset the selection of the store drop down list

		for (var z = 0; z < StoresArray.length; z++)
		{
			var CategoryDelimiter = StoresArray[z][5].split("|");
			for (var jj = 0; jj < CategoryDelimiter.length; jj++)
			{
				if (CategoryDelimiter [jj] == sender.value && document.aspnetForm.CategorySelect.selectedIndex != 0)
				{
					HighlightUnitNumber(StoresArray[z][1]);
					SelectedStoreIndices[counter] = z;
					counter++;
					jj = CategoryDelimiter.length;
				}
			}
		}
	}	

	setNumberStoresPerCol(counter);

	var TableHTML;
	
	TableHTML = '<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">'
	for (var z = 0; z < numItemsCol1; z++)
	{
		TableHTML += '<tr>'
		TableHTML += CalcStoreHTML(SelectedStoreIndices[z]);
		if (z < numItemsCol2)
			{
			TableHTML += CalcStoreHTML(SelectedStoreIndices[z + numItemsCol1]);
			}
		else
			{
			TableHTML += '<td></td><td></td>';
			}
		if (z < numItemsCol3)
			{
			TableHTML += CalcStoreHTML(SelectedStoreIndices[z + numItemsCol1 + numItemsCol2]);
			}
		else
			{
			TableHTML += '<td></td><td></td>';
			}
		TableHTML += '</tr>'
	}
	TableHTML += '</table>'
	//alert(TableHTML);

	document.getElementById('StoreSelectionResult').innerHTML = TableHTML;

	LoadZoom();
}

function CalcStoreHTML(z)
{
	var ReturnHTML;
    var url;
	url = location.href.toLowerCase();
	var altText;
	var titleText;
	
	if (url.indexOf('/fr/') > -1) {
		//french interactive map needs french tooltips
		altText = 'Une promotion est en cours &agrave; ce magasin';
		titleText = 'Le magasin est indiqu&eacute; sur le plan d&#39;&eacute;tage';
	}
	else
	{
		//english interactive map needs english tooltips
		altText = 'This store has a current promotion';
		titleText = 'The store is highlighted on the map below';
	}

	//check if the store has a promotion
	if (StoresArray[z][4] == 1)
	{
		ReturnHTML = '<td class="PromoImg"><img src="/_layouts/cfretail/Images/icon_star.gif" border="0" alt="' + altText +'" /></td>';
	}
	else
	{
		ReturnHTML = '<td class="PromoImg"><img src="/_layouts/cfretail/Images/icon_nostar.gif" width="15px" height="15px" border="0" alt="" /></td>';
	}
	
	ReturnHTML += '<td class="StoreLink"><a title="' + titleText + '" onmouseover="hyperLinkHoverIn(\''+StoresArray[z][1]+'\')" onmouseout ="hyperLinkHoverOut(\''+StoresArray[z][1]+'\')" href='+StoresArray[z][2]+'>'+StoresArray[z][0]+'</a></td>';		
	return(ReturnHTML);
}

function setNumberStoresPerCol(counter)
{
	// Determine the number of stores that fill all three columns (divide with no remainder)
	var storesPerCol = Math.floor(counter/3);
	// Get the remainder to determine how much is left over from that
	var numStoresPerCol = (counter % 3);
	
	// If there is no remainder then everything fits exactly
	if (numStoresPerCol == 0)
	{
		numItemsCol1 = storesPerCol;
		numItemsCol2 = storesPerCol;
		numItemsCol3 = storesPerCol;
	}
	// If the remainder is one put the extra store in the first column
	else if (numStoresPerCol == 1)
	{
		numItemsCol1 = storesPerCol + 1;
		numItemsCol2 = storesPerCol;
		numItemsCol3 = storesPerCol;
	}
	// If the remainder is two put the extra stores in the first two columns
	else if (numStoresPerCol == 2)
	{
		numItemsCol1 = storesPerCol + 1;
		numItemsCol2 = storesPerCol + 1;
		numItemsCol3 = storesPerCol;
	}
}