/* hyne2SupplierSearch.js */ 

var storeLocations = new Array()
var stateIndex = -1
var blueSearch	


storeLocations.length = 5;
for (var i=0; i<storeLocations.length; i++)
storeLocations[i] = new Array();

storeLocations[0][0] = 'Australian Capital Territory';
storeLocations[1][0] = 'New South Wales';
storeLocations[2][0] = 'Queensland';
storeLocations[3][0] = 'South Australia';
storeLocations[4][0] = 'Victoria';

	
/*
e.g.
storeLocations[0][1] = new Array()
storeLocations[0][1][0] = 'Fyshwick'
storeLocations[0][1][1] = new Array()
storeLocations[0][1][1][0] = 'Builders Trading Co-Op'
storeLocations[0][1][1][1] = 'Phone: 02 6280 5595'

storeLocations[0][2] = new Array()
storeLocations[0][2][0] = 'Griffith'
storeLocations[0][2][1] = new Array()
storeLocations[0][2][1][0] = 'Bunnings'
storeLocations[0][2][1][1] = 'Phone: 02 6962 1499'
storeLocations[0][2][2] = new Array()
storeLocations[0][2][2][0] = 'M I A Trade Timber & Hardware'
storeLocations[0][2][2][1] = 'Phone: 02 6962 9688'*/


function hideShowStoresButton()
{
	var e = document.getElementById('showStores');
	e.style.visibility='hidden';
	MM_showHideLayers('searchResultsBoxOuter','','hide')
}

function unhideShowStoresButton()
{
	var e = document.getElementById('showStores');
	e.style.visibility='visible';
}


function setLocationList(selObj)
{
	var eHeading
	var eSelect
	if (blueSearch)
		stateIndex = selObj.options[selObj.selectedIndex].value - 1
	else
		stateIndex = 0;
	eHeading = document.getElementById('stateLocationsHeading');
	eSelect = document.getElementById('locationSelect');
	if (eHeading)
		eHeading.innerHTML = 'Store Locations';
	eSelect.options.length = 1;
	eSelect.options[0] = new Option('--Please Select--', 0, true, false);
	if (stateIndex > -1) 
	{
		if (eHeading)
			eHeading.innerHTML = storeLocations[stateIndex][0] + ' Store Locations';
		eSelect.options.length = storeLocations[stateIndex].length;
		for (var i=1; i<storeLocations[stateIndex].length; i++)
		{
			eSelect.options[i] = new Option(storeLocations[stateIndex][i][0], i, false, false);
		}
	}
	if (eSelect.selectedIndex <= 0)
		hideShowStoresButton()
	else
		unhideShowStoresButton();
}

function setStoreList(selObj)
{
	var storeIndex
	var eHeading
	var eResults1
	var eResults2
	var locationCount
	var Results1Count
	var Results2Count
	var s

	if (selObj.selectedIndex <= 0)
		hideShowStoresButton()
	else
		unhideShowStoresButton();

	storeIndex = selObj.options[selObj.selectedIndex].value;
	if (storeIndex >= 1)
	{
		eHeading = document.getElementById('storeLocationsHeading');
		eResults1 = document.getElementById('results1');
		eResults2 = document.getElementById('results2');
		locationCount = storeLocations[stateIndex][storeIndex].length - 1;
		s = locationCount + ' search result'
		if (locationCount > 1) s += 's'
		s+= ' for ' + selObj.options[selObj.selectedIndex].innerHTML
		eHeading.innerHTML =  s; 

		Results2Count = parseInt(locationCount/2)
		Results1Count = locationCount - Results2Count
		s =  '';
		for (var i=1; i <= Results1Count; i++)
		{
			s+= '<p><strong>' + storeLocations[stateIndex][storeIndex][i][0] + '</strong><br />'
			for (var j=1; j<storeLocations[stateIndex][storeIndex][i].length;j++)
				s+= storeLocations[stateIndex][storeIndex][i][j]
			s+= '</p>';
		}
		eResults1.innerHTML = s;

		s =  '';
		for (var i=Results1Count+1; i <= locationCount; i++)
		{
			s+= '<p><strong>' + storeLocations[stateIndex][storeIndex][i][0] + '</strong><br />'
			s+= storeLocations[stateIndex][storeIndex][i][1] + '</p>';
		}
		eResults2.innerHTML = s;
	}
}

function ltrim(s)
{
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}


function getStoreLocations()
{
	var e 
	var index
	var states = '';

	function processStore(sList, index, stateIndex, locationIndex, storeIndex)
	{
		var exhausted = false
		var s
		var addressIndex = 0
		var re = new RegExp(/\<[pP]\>\s*(.+)\s*\<[bB][rR]\s*\/?\>/);
		var re1 = new RegExp(/(.+)\s*\<[bB][rR]\s*\/?\>/);
		var re2 = new RegExp(/(.+)\s*\<\/[pP]\>/);
		if (sList[index].search(re) != -1)
		{
			s = RegExp.$1;
			storeLocations[stateIndex][locationIndex][storeIndex] = new Array()
			storeLocations[stateIndex][locationIndex][storeIndex][addressIndex] = s;
			
			addressIndex++
			index++																										 
			while (exhausted == false)
			{
				if (index >= sList.length)
					exhausted = true
				else
				{
					s = ltrim(sList[index]).toLowerCase();																										 
					if (s == '')
						exhausted = true
					else
					if (s.substr(0,18)=='<p><a href="#top">')
					{
						exhausted = true
						index = index-1
					}
					else
					if (s.substr(0,3)=='<p>')
					{
						exhausted = true
						index = index-2
					}
					else
					{
						if (sList[index].search(re1) != -1)
						{
							s = RegExp.$1;
							storeLocations[stateIndex][locationIndex][storeIndex][addressIndex] = s;
							addressIndex++
							index++
						}
						else
						if (sList[index].search(re2) != -1)
						{
							s = RegExp.$1;
							storeLocations[stateIndex][locationIndex][storeIndex][addressIndex] = s;
							addressIndex++
							index++
						}
						else
						{
							exhausted = true
						}
					}
				}
			}
		}
		return index
	}

	function processStores(sList, index, stateIndex, locationIndex)
	{
		var exhausted = false
		var s
		var storeIndex = 1
		//extract location name from sList[index] <h4>Fyshwick Store Locations</h4>
		var re = new RegExp(/\<[hH]4\>\s*(.+)\s+Store Locations\s*\<\/[hH]4\>/);
		if (sList[index].search(re) != -1)
		{
			s = RegExp.$1;
			storeLocations[stateIndex][locationIndex] = new Array()
			storeLocations[stateIndex][locationIndex][0] = s;
			// extract stores until one of the following is found
			// <p><a href="#top">#Back to top</a></p>
			// or <a name...
			// or <h4>...
			index++
			while (exhausted == false)
			{
				if (index >= sList.length)
					exhausted = true
				else
				{
					s = ltrim(sList[index]).toLowerCase();
					if (s.substr(0,4)=='<h4>')
					{
						index = index-2
						exhausted = true
					}
					else
					if ((s.substr(0,18)=='<p><a href="#top">') || (s.substr(0,7)=='<a name'))
					{
						index = index-1
						exhausted = true
					}
					else
					if (s.substr(0,3)=='<p>')
					{
						index = processStore(sList, index, stateIndex, locationIndex, storeIndex);
						storeIndex++
					}
					index++;
				}
			}
		}
		return index
	}

	function processLocations(sList, index, stateIndex)
	{
		var exhausted = false
		var s
		var locationIndex = 1
		while (exhausted == false)
		{
			if (index >= sList.length)
				exhausted = true
			else
			{
				s = ltrim(sList[index]).toLowerCase();
				if (s.substr(0,4)=='<h3>')
				{
					index = index - 2
					exhausted = true
				}
				else
				if (s.substr(0,4)=='<h4>')
				{
					index = processStores(sList, index, stateIndex, locationIndex);
					locationIndex++
				}
				index++;
			}
		}
		return index
		
	}

	function processState(sList, index)
	{
		//extract state name from sList[index] <h3> South Australia Store Locations</h3>
		var re = new RegExp(/\<[hH]3\>\s*(.+)\s+Store Locations\s*\<\/[hH]3\>/);
		var stateIndex = 0
		if (sList[index].search(re) != -1)
		{
			var s = RegExp.$1;
			if (blueSearch)
			{
				if (s == 'Australian Capital Territory') stateIndex = 0
				else
				if (s == 'New South Wales') stateIndex = 1
				else
				if (s == 'Queensland') stateIndex = 2
				else
				if (s == 'South Australia') stateIndex = 3
				else
				if (s == 'Victoria') stateIndex = 4
			}
			else
				stateIndex = 0;
			index++
			index = processLocations(sList, index, stateIndex)
		}
		return index
	}


	/*  main code for getStoreLocations starts here*/

	if (document.getElementById('spacerT2Red'))
		blueSearch = false
	else
		blueSearch = true;
	if (blueSearch == false)
	{
		storeLocations.length = 1;
		storeLocations[0][0] = 'Queensland';
		stateIndex = 0;
	}
			
	e = document.getElementById('searchT2ContentContainer');
	if (e)
	{
		var s = e.innerHTML
		//var sList = s.split('\n'); IE7 removes empty lines, capitilizes tag names 
		//and changes <br />CRLF to just <BR> so a workaround is required
		sListTemp = s.split('\n');
		var sList = new Array()
		var sListIndex = 0
		for (var j=0; j<sListTemp.length; j++)
		{
			sListTempLineSplit = sListTemp[j].split('<BR>');
			for (var k=0; k<sListTempLineSplit.length; k++)
			{
				s = sListTempLineSplit[k];
				if (k < sListTempLineSplit.length-1)
					s+= '<br />'
				sList[sListIndex] = s
				sListIndex++
			}
		}
		index = 0;
		while (index < sList.length)
		{
			s = ltrim(sList[index]).toLowerCase();
			if (s.substr(0,4)=='<h3>')
			{
				index = processState(sList, index);
			}
			index++;
		}


		/* debug		
		s= ''
		for (var i=0; i < storeLocations.length;i++)
		{
			s+=  '\n' + storeLocations[i][0] + '\n  '
			
			for (j=1; j<storeLocations[i].length;j++)
			{
				s+= storeLocations[i][j][0] + '('
				for (k=1; k<storeLocations[i][j].length;k++)
				{
					if (storeLocations[i][j][k]==null) 
						s+= ' \n***error for ' + storeLocations[i][j][0] + '***\n'
					else
					{
						for (m=0; m<storeLocations[i][j][k].length;m++)
						s+= storeLocations[i][j][k][m] + '|'
					}
				}
				s+=')\n'	
			}	
		}
		alert(s) 
		*/
			
		e = document.getElementById('individualProductContentContainer');
		if (e)
		{			
			s = ''
			if (blueSearch)
			{
				s+= '<div class="selectionBoxOuter">'
				s+= '<h2>State</h2>'
				s+= '<p class="boldText">Step 1 - Select your state</p>'
				s+= '<div id="selectState" class="selectionDiv">'
				s+= '<form action="" method="get">'
				s+= '<select name="jumpMenu" id="jumpMenu" onchange="setLocationList(this)">'
				s+= '<option value="0" selected="selected">---Please Select---</option>'
				s+= '<option value="1">Australian Capital Territory</option>'
				s+= '<option value="2">New South Wales</option>'
				s+= '<option value="3">Queensland</option>'
				s+= '<option value="4">South Australia</option>'
				s+= '<option value="5">Victoria</option>'
				s+= '</select>'
				s+= '</form>'
				s+= '</div>'
				s+= '</div>'
				s+= '<div class="selectionBoxOuter2">'
				s+= '<h2 id="stateLocationsHeading">Store Locations</h2>'
				s+= '<p class="boldText">Step 2 - Select your locality</p>'
				s+= '<div id="locationsForState" class="selectionDiv">'
				s+= '<form action="" method="get" >'
				s+= '<select name="jumpMenu" id="locationSelect" onclick="setStoreList(this)">'
				s+= '<option value="">---Please Select---</option>'
				s+= '</select>'
				s+= '<span class="selectionChoice" id="showStores"><a href="#" onclick="MM_showHideLayers(' + "'searchResultsBoxOuter','','show'" + ')">Show Stores</a></span>'
				s+= '</form>'
				s+= '</div>'
				s+= '</div>'
			}
			else
			{
				s+= '<div class="selectionBoxOuter3">'
				s+= '<p class="boldText">Please select your locality</p>'
				s+= '<div id="locationsForState" class="selectionDiv">'
				s+= '<form action="" method="get" >'
				s+= '<select name="jumpMenux" id="locationSelect" onclick="setStoreList(this)">'
				s+= '<option value="0">---Please Select---</option>'
				s+= '</select>'
				s+= '<span class="selectionChoice" id="showStores"><a href="#" onclick="MM_showHideLayers(' + "'searchResultsBoxOuter','','show'" + ')">Show Stores</a></span>'
				s+= '</form>'
				s+= '</div>'

			}
			e.innerHTML = s
		}
		
		e = document.getElementById('jumpMenu');
		if (e)
		{
			e.selectedIndex = 0;
			hideShowStoresButton();
		}
		else
		{
			e = document.getElementById('locationSelect');
			e.selectedIndex = 0;
			setLocationList(e);  // ok to pass this dropdown as selectedIndex = 0
			hideShowStoresButton();
		}
	}
}

onload = getStoreLocations


/* end */
