﻿//holds the Virtual Earth map object
var displaymap = null;

//Contains the base url to get the location of images, etc.
var url = null;

window.onload=GetDisplayMap;
window.onresize=resizeMap;

//Setup the capture of the mouse movement
var isIE = document.all?true:false;
if (!isIE) 
	document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMousePosition;

//True means that a popup balloon is being shown, false means it should not be shown
var isBalloonShown = false;
//True means the balloon should be to the right of the numbered pushpin
var isLeft = false;
//The store id of the pushpin being hovered over
var storeID = -1;

//Method getMousePosition
//keeps track of where the mouse is currently located
//if a popup balloon is shown determine if the mouse is still within the bounds of the 
//popup and if it is not then make the popup disapear
function getMousePosition(e) 
{
	try
	{
		var _x;
		var _y;
		
		//Get the current X and Y pixel position of the mouse
		if (!isIE) 
		{
			_x = e.pageX;
			_y = e.pageY;
		}
		if (isIE) 
		{
			_x = event.clientX + document.body.scrollLeft;
			_y = event.clientY + document.body.scrollTop;
		}
		
		if(isBalloonShown)
		//If the popup balloon is shown determine if it should be hidden
		{
			//Get the top and bottom image coords of the popup
			var bottom = document.getElementById('balloon_bottom');
			var bot_coords = findPos(bottom);
			var top = document.getElementById('balloon_top');
			var top_coords = findPos(top);
			
			var leftPointPos = document.getElementById('left_point_pos');
			var rightPointPos = document.getElementById('right_point_pos');
			
			//Variables for the four sides of the popup
			var top_pos = top_coords[1];
			var bottom_pos = bot_coords[1] + 21;
			var left_pos;
			var right_pos;
			
			//Variables for the four sides of the pushpin box
			var box_top;
			var box_left;
			var box_bottom;
			var box_right;
			
			//get the coords for the pushpin and the popup
			if(isLeft)
			{
				left_pos = top_coords[0];
				right_pos = bot_coords[0] + 333;
				box_top = top_pos + rightPointPos.height + 7;
				box_left = right_pos;
			}
			else
			{
				left_pos = top_coords[0] - 22;
				right_pos = bot_coords[0] + 311;
				box_top = top_pos + leftPointPos.height + 7;
				box_left = left_pos - 36;
			}
			box_bottom = box_top + 27;
			box_right = box_left + 36;
			
			//Determine if the mouse is inside of the pushpin
			var inIcon = true;
			if(_x < box_left || _x > box_right || _y < box_top || _y > box_bottom)
				inIcon = false;
			
			//Determine if the mouse is not inside of the popup and not inside of the pushpin
			if(!inIcon && (_x < left_pos || _x > right_pos || _y < top_pos || _y > bottom_pos))
			{
				//Hide the popup and clear the highlight of the pushpins
				clearPopup();
			}
		}
		return true;
	}
	catch(e)
	{
		setProgError('an error occured tracking the mouse.', 'getMousePosition', e.message);
		return true;
	}
}

//Method browserAdjustment
//IE returns a slightly different position for the highlighting of the pushpin
//This function returns an adjustment to the location if it is IE.
function browserAdjustment()
{
	if(BrowserDetect.browser == 'Explorer')
		return [-1,-1];
	else
		return [0,0];
}

//Method pinDetails
//this method creates and displays the popup when the mouse moves over a pushpin
//Parameters:
//x - the x position of the machine.  Depending on how the pushpin is sent to Virtual Earth
//			it can be different parts of an icon
//y - the y position of the machine. (see x from above)
//title - the title parameter of the pushpin, in this case only used to determine if it is the
//				home location as opposed to a machine location
//details - the details of the pushpin.  For the home location the address inputted by the user,
//					for a machine location all the machine details.
function pinDetails(x, y, title, details)
{
	try
	{
		//Get the browser adjustment
		var adjustment = browserAdjustment();
		x += adjustment[0];
		y += adjustment[1];
		
		var hover = null;
		var count = 1;
		//Clear all highlighted pushpins in the left content bar
		while(hover = document.getElementById('hover' + count))
		{
			hover.src = 'images/pushpins/map_pin_inactv_' + count + '.png';
			count++;
		}
		var positionicon = document.getElementById('positionIcon');
		if(title == 'Start')
		//If the location is the home location display the home popup
		{
			document.getElementById('machineLink').href = '#Machine1';
			positionicon.src = 'images/pushpins/map_pin_here.png';
			//get the address and split it into it's lines
			var start = document.getElementById('_ctl0_cphCenterContent_txtStartAddress').value;
			var splitStart = start.split(';');
			if(splitStart.length > 1)
				start = splitStart[0] + '<br />' + splitStart[1];
			else
				start = splitStart[0];
			//add the address to the popup
			var address = '<strong>Start</strong><br />' + start;
			document.getElementById('storeData').innerHTML = address;
			document.getElementById('machineIcons').innerHTML = "";
			document.getElementById('machineProducts').innerHTML = "";
			
			//determine the optimal location of the popup
			var balloon = document.getElementById('popup');
			winCoords = windowSize();
			var scrollCoords = getScrollXY();
			winCoords[0] += scrollCoords[0];
			winCoords[1] += scrollCoords[1];
			if((x + 353) > winCoords[0] && (x - 353) > 0)
			//Make the popup to the left of the pushpin because there is more room
			{
				document.getElementById('popupleft').style.display = 'none';
				document.getElementById('popupright').style.display = 'inline';
				balloon.style.left = (x - 353) + 'px';
				isLeft = true;
			}
			else
			//Make the popup to the right of the pushpin by default
			{
				document.getElementById('popupleft').style.display = 'inline';
				document.getElementById('popupright').style.display = 'none';
				balloon.style.left = (x + 20) + 'px';
				isLeft = false;
			}
			balloon.style.top = (y - 55) + 'px';
			
			//Display the popup
			balloon.style.display = 'block';
			
			//Don't hightlight for the home image
			var hover = document.getElementById('hoverImg');
			hover.style.display = 'none';
			isBalloonShown = true;
		}
		else
		{
			//Add the machine details to the popup
			addMachineDetail(details);
			
			var balloon = document.getElementById('popup');
			//Display the popup (this prevents errors in calculating size, some browsers
			//		return zeros for location, etc when the element is set to display of none)
			balloon.style.display = 'block';
			
			//Get the coords for the top and bottom image of the popup
			var bottom = document.getElementById('balloon_bottom');
			var bottomCoords = findPos(bottom);
			var top = document.getElementById('balloon_top');
			var topCoords = findPos(top);

			//Get the size of the window
			winCoords = windowSize();
			var scrollCoords = getScrollXY();
			winCoords[0] += scrollCoords[0];
			winCoords[1] += scrollCoords[1];
			
			//If a popup to the right of the pushpin would go off the screen and there is enough
			//room to put it on the left of the pushpin then do so, otherwise put it to the right
			//of the pushpin.
			if((x + 353) > winCoords[0] && (x - 353) > 0)
			{
				document.getElementById('popupleft').style.display = 'none';
				document.getElementById('popupright').style.display = 'inline';
				balloon.style.left = (x - 353) + 'px';
				isLeft = true;
			}
			else
			{
				document.getElementById('popupleft').style.display = 'inline';
				document.getElementById('popupright').style.display = 'none';
				balloon.style.left = (x + 20) + 'px';
				isLeft = false;
			}
			
			//Objects for placing the point of the popup next to the pushpin
			var leftPointPos = document.getElementById('left_point_pos');
			var rightPointPos = document.getElementById('right_point_pos');
			
			var base_top = y - 55; //the top of the popup if it were not to move vertically
			var normal_bottom = (bottomCoords[1] - topCoords[1]) + base_top + 21; //the bottom of the popup if it were not to move vertically
			var extra_height = normal_bottom - winCoords[1]; //the position of the bottom of the popup with out movement relative to the bottom of the screen
			var pointAdjust; //the adjustment from normal of the popup points
			
			if(extra_height > 0)
			//the bottom of the popup is below the visible screen so move up if possible
			{
				if(extra_height > base_top)
				//Moving the popup as far up as is needed would go beyond the top of the screen
				//so just move it to the top
				{
					balloon.style.top = '0px';
					pointAdjust = 34;
					leftPointPos.height = (y - pointAdjust);
					rightPointPos.height = (y - pointAdjust);
				}
				else
				//There is room to move the popup entirely into the visible screen so do so
				{
					pointAdjust = (base_top - extra_height) + 34;
					balloon.style.top = (base_top - extra_height) + 'px';
					leftPointPos.height = (y - pointAdjust);
					rightPointPos.height = (y - pointAdjust);
				}
			}
			else
			//The bottom of the popup is not below the visible screen so treat as normal
			{
				pointAdjust = base_top;
				balloon.style.top = base_top + 'px';
				leftPointPos.height = "21";
				rightPointPos.height = "21";
			}
			
			//The popup is now being shown
			isBalloonShown = true;
			
			//Set the position of the highlight picture over the selected pushpin and make it visible
			var hover = document.getElementById('hoverImg');
			var hoverImg = document.getElementById('hoverIcon');
			hover.style.left = (x - 15) + 'px';
			hover.style.top = (y - 26) + 'px';
			hover.style.display = 'block';
			
			//Split the machine data to get the machine number
			var parts = details.split('~~');
			if(parts.length >= 7) //This is a valid entry
			{
				//set the hover image to the appropriately numbered highlight image
				positionicon.src = 'images/pushpins/map_pin_actv_' + parts[6] + '.png';
				document.getElementById('hover' + parts[6]).src = 'images/pushpins/map_pin_actv_' + parts[6] + '.png';
				hoverImg.src = 'images/pushpins/map_pin_actv_' + parts[6] + '.png';
				storeID = parts[6];
			}
		}
	}
	catch(e)
	{
		setProgError('an error occured creating the popup.', 'pinDetails', e.message);
	}
}

//Method updateVertical
//when the size of the popup changes this gets called to recalculate the vetical position
//of the popup if needed
function updateVertical()
{
	try
	{
		//Get the hover image in order to place the popup and pushpin
		var hoverIcon = document.getElementById('hoverIcon');
		var hoverCoords = findPos(hoverIcon);
		
		//Get the popup
		var balloon = document.getElementById('popup');
		
		//Set the y position based off the hover image
		var y = hoverCoords[1] + 26;
		
		//Get the bottom and top image coords of the popup
		var bottom = document.getElementById('balloon_bottom');
		var bottomCoords = findPos(bottom);
		var top = document.getElementById('balloon_top');
		var topCoords = findPos(top);

		//Get the window size
		var winCoords = windowSize();
		var scrollCoords = getScrollXY();
		winCoords[0] += scrollCoords[0];
		winCoords[1] += scrollCoords[1];
		
		//The popup point objects
		var leftPointPos = document.getElementById('left_point_pos');
		var rightPointPos = document.getElementById('right_point_pos');
		
		var base_top = y - 55; //the top of the popup if it were not to move vertically
		var normal_bottom = (bottomCoords[1] - topCoords[1]) + base_top + 21; //the bottom of the popup if it were not to move vertically
		var extra_height = normal_bottom - winCoords[1]; //the position of the bottom of the popup with out movement relative to the bottom of the screen
		var pointAdjust; //the adjustment from normal of the popup points
		
		if(extra_height > 0)
		//the bottom of the popup is below the visible screen so move up if possible
		{
			if(extra_height > base_top)
			//Moving the popup as far up as is needed would go beyond the top of the screen
			//so just move it to the top
			{
				balloon.style.top = '0px';
				pointAdjust = 34;
				leftPointPos.height = (y - pointAdjust);
				rightPointPos.height = (y - pointAdjust);
			}
			else
			//There is room to move the popup entirely into the visible screen so do so
			{
				pointAdjust = (base_top - extra_height) + 34;
				balloon.style.top = (base_top - extra_height) + 'px';
				leftPointPos.height = (y - pointAdjust);
				rightPointPos.height = (y - pointAdjust);
			}
		}
		else
		//The bottom of the popup is not below the visible screen so treat as normal
		{
			pointAdjust = base_top;
			balloon.style.top = base_top + 'px';
			leftPointPos.height = "21";
			rightPointPos.height = "21";
		}
	}
	catch(e)
	{
		setProgError('an error occured updating the vertial spacing for the popup.', 'updateVertical', e.message);
	}
}

//Method clearPopup
//clears the popup from the screen and resets some variables for the next popup
function clearPopup()
{
	try
	{
		//Get the popup
		var balloon = document.getElementById('popup');
		//Make it invisible
		balloon.style.display = 'none';
		//Get the hover images
		var hover = document.getElementById('hoverImg');
		//Make it invisible
		hover.style.display = 'none';
		//Get the point objects
		var leftPointPos = document.getElementById('left_point_pos');
		var rightPointPos = document.getElementById('right_point_pos');
		//reset their position
		leftPointPos.height = '21';
		rightPointPos.height = '21';
		//reset the popup's position
		balloon.style.top = '0px';
		balloon.style.left = '0px';
		var lefthover = null; //to hold the left list icons
		var count = 1;
		while(lefthover = document.getElementById('hover' + count))
		//For each # image on the left machines list clear the hover
		{
			lefthover.src = 'images/pushpins/map_pin_inactv_' + count + '.png';
			count++;
		}
		//The popup is no longer being shown
		isBalloonShown = false;
	}
	catch(e)
	{
		setProgError('an error occured clearing the popup.', 'clearPopup', e.message);
	}
}

//method GetDisplayMap
//Loads the map and the machine pushpins on the screen
//The entry function of the map page
function GetDisplayMap()
{
	try
	{
		clearProgError(); //clear all errors
		setMasterOptions(); //Set up the page based on language and country (portal) selection
		setCountryOptions(); //Set up the page based on the searched country
		setMenuLoc();
		
		//Get the base url of the page
		url = window.location.href;
		var index = url.lastIndexOf('/') + 1;
		url = url.substring(0, index);
		
		// If the browser is Firefox get the version number
		// If we're using Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
		if(BrowserDetect.browser == 'Firefox')
		{
			if (BrowserDetect.version >= 1.5) 
			{
				Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
			}
		}

		//Display the quick locate
		var quick = document.getElementById('quickSearch');
		var link = document.getElementById('lnkAdvSearch');
		
		quick.style.display = "inline";
		link.style.display = "inline";
		
		var country = document.getElementById('_ctl0_txtMasterCountry').value;
		
		var countries = document.getElementsByName('country');
		
		if(country != 'IE')
		{
			countries[0].checked = true;
		}
		
		//Set version display to MapPoint version
		var version = document.getElementById('version');
		var versionMP = document.getElementById('versionMP');
		version.style.display = 'inline';
		versionMP.style.display = 'none';
		
		//Resize the map based on the size of the window
		resizeMap();
				
		//Get the lat, lon values of the search position from the textboxes set by the server
		var lat = document.getElementById('_ctl0_cphCenterContent_txtLatitude').value;
		var lon = document.getElementById('_ctl0_cphCenterContent_txtLongitude').value;
		var loc = new VELatLong(lat, lon);
		
		//setup and display the map
		displaymap = new VEMap('displayMap');
		var units = document.getElementById('_ctl0_cphCenterContent_txtUnits').value;
		
//		var token = "<%=clienttoken %>";
//		displaymap.SetClientToken(token);
		displaymap.SetDashboardSize(VEDashboardSize.Tiny);
		
		displaymap.LoadMap(null, null, VEMapStyle.Road, false, VEMapMode.Mode2D, false);
		
		if(units == 'Miles')
			displaymap.SetScaleBarDistanceUnit(VEDistanceUnit.Miles);
		else
			displaymap.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
		
		//Add the machine points
		addPoints();
	
		//Add the search location
		var address = document.getElementById('_ctl0_cphCenterContent_txtStartAddress').value;
		var pin = new VEPushpin(
			0, 
			loc, 
			url + 'images/pushpins/map_pin_here.png', 
			'Start', 
			address,
			'iconStyle');
		displaymap.AddPushpin(pin);
	}
	catch(e)
	{
		setProgError('an error occured loading the map.', 'GetDisplayMap', e.message);
	}
}

//Method replace
//Replaces a given string with another within a main string
//Parameters:
//		value - the main string to search
//		search - the string value to search for
//		change - the value to change all search values to
//Returns:
//		the value string with all instances of search changed to change
function replace(value, search, change)
{
	var parts = value.split(search);
	var retVal = "";
	for(var times = 0; times < parts.length - 1; times++)
	{
		retVal += parts[times] + change;
	}
	retVal += parts[parts.length - 1];
	return retVal;
}

//Method addMachineDetail
//adds the machine details to the popup balloon
//Parameters:
//		details - the '~' seperated string containing all the machine details
function addMachineDetail(details)
{
	try
	{
		//split the parts string
		var parts = details.split('~~');
		//get the language
		var language = document.getElementById('_ctl0_txtMasterLanguage').value;
		//if it is a valid details string
		if(parts.length >= 6)
		{
			//Set the link to jump to the machine in the machine list on the left
			document.getElementById('machineLink').href = '#Machine' + parts[6];
			
			//set the store info values
			var storedata = '<strong>' + parts[2] + '</strong><br />';
			storedata += parts[3] + '<br />';
			storedata += parts[4] + ', ' + parts[7] + '<br />';
			storedata += parts[5] + '<br />';
			//Add directions link
			var drivDir = document.getElementById('_ctl0_cphCenterContent_txtDrivDirs').value;
			if(drivDir == 'True')
			{
				storedata += '<span style="cursor:pointer; text-decoration: underline;" onclick="directionsClick(' + parts[6] + ');">';
				if(language == 'FR')
					storedata += 'Directions D\'Entraînement';
				else if(language == 'ES')
					storedata += 'Direcciones Que conducen';
				else
					storedata += 'Driving Directions';
				storedata += '</span>';
			}
			document.getElementById('storeData').innerHTML = storedata;
			
			//Set the icons to display
			setIconData(parts);
			
			var machineInfo = '';
			var endDiv = '';
			//Set the list of products on the machine
			var productdata = '<div id="div ' + parts[6] + ' balloon details" style="display: none;">';
			for(var catPos = 9; catPos < parts.length; catPos++)
			{
				//Split the product type into the products
				var values = parts[catPos].split('~');
				machineInfo += endDiv;
				machineInfo += '<img src="images/_clear.gif" alt="" style="width:20px; height:0px;" />';
				machineInfo += '<img id="img ' + parts[6] + ' balloon ' + values[0] + '" src="images/twistUp.gif" alt="" style="cursor:pointer;" onclick="expand(\'' + parts[6] + ' balloon ' + replace(values[0], "'", "\\'") + '\');" />';
				machineInfo += '<span style="color: maroon; cursor:pointer; text-decoration: underline;" onclick="expand(\'' + parts[6] + ' balloon ' + replace(values[0], "'", "\\'") + '\');">' + values[0] + '</span><br />';
				machineInfo += '<div id="div ' + parts[6] + ' balloon ' + values[0] + '" style="display:none;">';
				endDiv = '</div>';
				for(var provPos = 1; provPos < values.length; provPos++)
				{
					machineInfo += '<img src="images/_clear.gif" alt="" style="width:40px; height:0px;" />' + 
						values[provPos] + '<br />';
				}
			}
			productdata += machineInfo;
			document.getElementById('machineProducts').innerHTML = productdata;
		}
	}
	catch(e)
	{
		setProgError('an error occured setting the popup machine data.', 'addMachineDetail', e.message);
	}
}

//Method setIconData
//sets the icons to display in the popup
//Parameters:
//		parts - the machine data split into parts.  For this method the machine number and the icon data is needed
function setIconData(parts)
{
	try
	{
		//Get the language setting
		var language = document.getElementById('_ctl0_txtMasterLanguage').value;
		
		//Get the list of the the icon named types in the appropriate language
		var types = new Array();
		if(language == 'FR')
			types = new Array('Cartes De Cadeau', 'eCertificate', 
				'Comptage de pièces de monnaie', 'Organisations à but non lucratif', 
				'Enquête de livre de paie et rapport de salaire', 
				'Cartes D\\\'Argent comptant', 
				'Temps d\\\'antenne', 'Longue distance');
		else if(language == 'ES')
			types = new Array('Tarjeta de Regalo', 'eCertificate', 'Conteo de monedas', 
				'Organizaciones sin fines de lucro', 'Saldo de Nomina y Resumen de Sueldo', 
				'Tarjeta de Efectivo', 'Tiempo de conexión', 'Larga distancia');
		else
			types = new Array('Gift Cards', 'eCertificate', 'Coin Counting', 
				'Non-Profit Organizations', 'Payroll Balance Inquiry and Wage Statement', 
				'Cash Cards', 'Wireless Airtime', 'Long Distance');
		
		//Get the method list to collapse when an icon is clicked
		var collapses = new Array(0);
		collapses = getCollapses(parts);
		
		//Build the icon data html code
		var icondata = '<span style="cursor:pointer; text-decoration: underline;" onclick="expand(\'' + parts[6] + ' balloon details\');';
		for(var times = 0; times < collapses.length; times++)
			icondata += collapses[times];
		icondata += '" class="headert">';
		icondata += '<img id="img ' + parts[6] + ' balloon details" style="display:none;" alt="" src="images/twistUp.gif" />'
		if(language == 'FR')
			icondata += 'Produits'
		else if(language == 'ES')
			icondata += 'Productos'
		else
			icondata += 'Products'
		icondata += ':';
		icondata += '</span>&nbsp;';
		if((parts[8] & 2) == 2) //Coin Counting
		{
			icondata += '<img src="images/legend/service_icon_coin.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[2] + '\');" />';
		}
		if((parts[8] & 8) == 8) //eCertificate
		{
			icondata += '<img src="images/legend/service_icon_eCert.png" style="cursor:pointer;" height="17" width="20"  alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[1] + '\');" />';
		}
		if((parts[8] & 16) == 16) //Gift Cards
		{
			icondata += '<img src="images/legend/service_icon_giftc.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[0] + '\');" />';
		}
		if((parts[8] & 4) == 4) //NPOs
		{
			icondata += '<img src="images/legend/service_icon_donate.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[3] + '\');" />';
		}
		if((parts[8] & 1) == 1) //Cash Card
		{
			icondata += '<img src="images/legend/service_icon_cashc.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[5] + '\');" />';
		}
		if((parts[8] & 64) == 64) //Wireless cards
		{
			icondata += '<img src="images/legend/service_icon_wireless.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[6] + '\');" />';
		}
		if((parts[8] & 128) == 128) //Long Distance Cards
		{
			icondata += '<img src="images/legend/service_icon_longdist.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[7] + '\');" />';
		}
		if((parts[8] & 32) == 32) //All Payroll Services
		{
			icondata += '<img src="images/legend/service_icon_payroll.png" style="cursor:pointer;" height="17" width="20" alt="" onclick="expandIcon(\'' + parts[6] + ' balloon details\');';
			for(var times = 0; times < collapses.length; times++)
				icondata += collapses[times];
			icondata += 'expandIcon(\'' + parts[6] + ' balloon ' + types[4] + '\');" />';
		}
		document.getElementById('machineIcons').innerHTML = icondata;
	}
	catch(e)
	{
		setProgError('an error occured setting the popup icons.', 'setIconData', e.message);
	}
}

//Method getCollapses
//retrieves a list of method calls to collapse when an icon is clicked
function getCollapses(parts)
{
	var collapses = new Array(parts.length - 9);
	for(var catPos = 9; catPos < parts.length; catPos++)
	{
		var values = parts[catPos].split('~');
		collapses[catPos-9] =  'collapse(\'' + parts[6] + ' balloon ' + replace(values[0], "'", "\\'") + '\');';
	}
	return collapses;
}

//Method directionsClick
//sets the driveMachine value and submits the form to go to the directions pages
function directionsClick(machine)
{
	try
	{
		var drive = document.getElementById('_ctl0_cphCenterContent_driveMachine');
		drive.value = machine;
		var frmMain;
		for(var times = 0; times < document.forms.length; times++)
		{
			if(document.forms[times].id == "aspnetForm")
				frmMain = document.forms[times];
		}
		
		frmMain.submit();
	}
	catch(e)
	{
		setProgError('an error occured trying to load the driving directions.', 'dirrectionsClick', e.message);
	}
}

//Method addPoints
//add the machine pushpins to the map
function addPoints()
{
	try
	{
		//Set the map to allow for personalized popup
		VEPushpin.ShowDetailOnMouseOver = false;
		VEPushpin.OnMouseOverCallback = pinDetails;

		//Get the data for the points
		var data = document.getElementById('_ctl0_cphCenterContent_txtMachineData').value;
		var lat = document.getElementById('_ctl0_cphCenterContent_txtLatitude').value;
		var lon = document.getElementById('_ctl0_cphCenterContent_txtLongitude').value;
		var units = document.getElementById('_ctl0_cphCenterContent_txtUnits').value;
		//Split the point data into the individual points
		var temp = data.split(';');
		var locs = new Array(); //array to contail all points to get the map view
		//Add the search location to the array
		locs.push(new VELatLong(lat, lon));
		for(var times = temp.length - 1; times >= 0; times--)
		//Add the points to the map and add them to the array
		{
			var parts = temp[times].split('~~');
			if(parts.length >= 7)
			{
				var loc = new VELatLong(parts[0], parts[1]);
				locs.push(loc);

				var id = parts[6];
				var pin = new VEPushpin(
					id,
					loc,
					url + 'images/pushpins/map_pin_inactv_' + id + '.png',
					"",
					temp[times],
					'iconStyle');
				displaymap.AddPushpin(pin);
			}
		}
		//Set the map to display the best view containing all the points
		displaymap.SetMapView(locs);
	}
	catch(e)
	{
		setProgError('an error occured adding the pushpins.', 'addPoints', e.message);
	}
}

//Method ZoomPoint
//Zooms the map to the given point
//Used when the # image on the machine list is collected
//Parameters:
//		latitude - the latitude of the point to zoom too
//		longitude - the longitude of the point to zoom too
function ZoomPoint(latitude, longitude)
{
	try
	{
		var point = new VELatLong(latitude, longitude);
		//Center on the point
		displaymap.SetCenter(point);
		//Zoom to the point at zoom level 15
		displaymap.SetZoomLevel(15);
	}
	catch(e)
	{
		setProgError('an error occured trying to zoom to the store location.', 'ZoomPoint', e.message);
	}
}

//Method unhighLight
//unhighlights the given machine pushpin
//Parameters:
//		machine - the machine number
function unhighLight(machine)
{
	try
	{
		//Get the hover image and hide it
		document.getElementById('hover' + machine).src = 'images/pushpins/map_pin_inactv_' + machine + '.png';
		var hover = document.getElementById('hoverImg');
		hover.style.display = 'none';
	}
	catch(e)
	{
		setProgError('an error occured trying to Unhighlight the numbered balloon.', 'unhighLight', e.message);
	}
}

//Method highLight
//highlights the given machine pushpin at the given latitude
//Parameters:
//		latitude - the latitude of the machine
//		longitude - the longitude of the machine
//		machine - the machine number
function highLight(latitude, longitude, machine)
{
	try
	{
		document.getElementById('hover' + machine).src = 'images/pushpins/map_pin_actv_' + machine + '.png';
		//Convert the latitude and longitude to pixel points
		var point = new VELatLong(latitude, longitude);
		var loc = displaymap.LatLongToPixel(point);
		//Get the coords of the map
		var mapDiv = document.getElementById('displayMap');
		var mapCoords = findPos(mapDiv);
		//Find the width and height of the map
		var map_width = mapDiv.style.width;
		var map_height = mapDiv.style.height;
		if(loc.x >= 0 && loc.y >= 0 && loc.x <= map_width.substring(0, (map_width.length - 2)) 
			&& loc.y <= map_height.substring(0, (map_height.length - 2)))
		//If the point is inside the visible map then place the highlight on the machine
		{
			var x = loc.x + mapCoords[0];
			var y = loc.y + mapCoords[1];
			var adjustment = browserAdjustment();
			//x += adjustment[0];
			y += adjustment[1];
			
			var hover = document.getElementById('hoverImg');
			var hoverImg = document.getElementById('hoverIcon');
			hover.style.left = (x - 15) + 'px';
			hover.style.top = (y - 26) + 'px';
			hover.style.display = 'block';
			hoverImg.src = 'images/pushpins/map_pin_actv_' + machine + '.png';
		}
		
	}
	catch(e)
	{
		setProgError('an error occured trying to highlight the numbered balloon.', 'highLight', e.message);
	}
}

//Method jumpToStore
//when you click on the # image in the popup or the pushpin on the map jump to the 
//machine in the machine list
function jumpToStore()
{
	try
	{
		if(storeID != -1)
			window.location = "#Machine" + storeID;
	}
	catch(e)
	{
		setProgError('an error occured jumpong to the store.', 'jumpToStore', e.message);
	}
}

//method setCountryOptions
//toggles the correct items to reflect the country searching in
function setCountryOptions()
{
	try
	{
		//Get the selected country
		var country = getKey('country');
		
		if(country == 'CE' || country == 'CF')
		{
			toggle('CA');
			toggleOff('US');
			toggleOff('UK');
			toggleOff('IE');
			toggle('notIE');
		}
		else if(country == 'IN')
		{
			toggle('UK');
			toggleOff('CA');
			toggleOff('US');
			toggleOff('IE');
			toggle('notIE');
		}
		else if(country == 'IE')
		{
			toggle('IE');
			toggleOff('CA');
			toggleOff('US');
			toggleOff('UK');
			toggleOff('notIE');
		}
		else
		{
			toggle('US');
			toggleOff('CA');
			toggleOff('UK');
			toggleOff('IE');
			toggle('notIE');
		}
	}
	catch(e)
	{
		setProgError('an error occured changing the country.', 'setCountryOptions', e.message);
	}
}

//Method setMasterOptions
//Set the options specific to the language and the country.
function setMasterOptions()
{
	try
	{
		var language = document.getElementById('_ctl0_txtMasterLanguage').value;
		var country = document.getElementById('_ctl0_txtMasterCountry').value;
		if(language == 'EN')
		{
			toggle('masterEN');
			toggleOff('masterES');
			toggleOff('masterFR');
		}
		else if(language == 'ES')
		{
			toggle('masterES');
			toggleOff('masterEN');
			toggleOff('masterFR');
		}
		else
		{
			toggle('masterFR');
			toggleOff('masterES');
			toggleOff('masterEN');
		}
		
		if(country == 'EN' || country == 'ES')
		{
			toggle('masterUS');
			toggleOff('masterCA');
			toggleOff('masterIN');
			toggleOff('masterIE');
		}
		else if(country == 'CE' || country == 'CF')
		{
			toggle('masterCA');
			toggleOff('masterUS');
			toggleOff('masterIN');
			toggleOff('masterIE');
		}
		else if(country == 'IN')
		{
			toggle('masterIN');
			toggleOff('masterCA');
			toggleOff('masterUS');
			toggleOff('masterIE');
		}
		else
		{
			toggle('masterIE');
			toggleOff('masterCA');
			toggleOff('masterUS');
			toggleOff('masterIN');
		}
	}
	catch(e)
	{
		setProgError('an error occured setting the language and country options.', 'setMasterOptions', e.message);
	}
}

//Method expand
//expand/contract the given product type, etc
//Parameters:
//		id - the id of the item to expand or contract
function expand(id)
{
	try
	{
		//Get the twisty and the sub-div
		var image = document.getElementById('img ' + id);
		var divide = document.getElementById('div ' + id);
		if(image.src == url + 'images/twistUp.gif')
		//If the item is contracted then expand it
		{
			image.src = url + 'images/twistDown.gif';
			divide.style.display = 'inline';
		}
		else
		//If the item is expanded then contract it
		{
			image.src = url + 'images/twistUp.gif';
			divide.style.display = 'none';
		}
		//Update the popup's position
		updateVertical();
	}
	catch(e)
	{
		setProgError('an error occured expanding the details.', 'expand', e.message);
	}
}

//Method collapse
//only collapses the given item
//Parameters:
//		id - the id of the item to collapse
function collapse(id)
{
	try
	{
		var image = document.getElementById('img ' + id);
		var divide = document.getElementById('div ' + id);
		if(image.src != url + 'images/twistUp.gif')
		//if the item is not already collapsed then collapse it
		{
			image.src = url + 'images/twistUp.gif';
			divide.style.display = 'none';
		}
		//Update the popup's position
		updateVertical();
	}
	catch(e)
	{
		setProgError('an error occured expanding the details.', 'collapse', e.message);
	}
}

//Method expandIcon
//expand the single icon type when clicked
//Parameters:
//		id - the id of the item to expand alone
function expandIcon(id)
{
	try
	{
		var image = document.getElementById('img ' + id);
		var divide = document.getElementById('div ' + id);
		if(image.src == url + 'images/twistUp.gif')
		{
			image.src = url + 'images/twistDown.gif';
			divide.style.display = 'inline';
		}
		//Update the popup's position
		updateVertical();
	}
	catch(e)
	{
		setProgError('an error occured expanding the details.', 'expandIcon', e.message);
	}
}

//Method resizeMap
//resizes the map according to the size of the window
function resizeMap()
{
	try
	{
		//Determine map height and width here
		var mapDiv = document.getElementById('displayMap');
		mapCoords = findPos(mapDiv);
		size_window = windowSize();
		var locations = document.getElementById('_ctl0_cphLeftNav_pnlMapLeft');
		var width = (size_window[0] - mapCoords[0] - 60);
		var height = (size_window[1] - mapCoords[1] - 150);
		if(width < 300)
			width = 300;
		if(height < 300)
			height = 300;
		//if(width > (height * 1.5))
		//	width = height * 1.5;
		mapDiv.style.width = width + "px";
		mapDiv.style.height = height + "px";
		locations.style.height = height + "px";
	}
	catch(e)
	{
		setProgError('an error occured resizing the map and side bar.', 'resizeMap', e.message);
	}
}