	var mapviewer, searcher, loading, markers, search; //search_address;
	var filter1, filter2, filter3, boolean1, filter4, filter5, filter6, return_fields, submitbtn;
	var funcRef = resultsLoaded;
	
	// Including the 'client_id' ensures that clicking on a result logs it as such within the reports:
	var rfs = ['client_id', 'name', 'street', 'town', 'pc', 'state', 'phone','fax', 'lat', 'lon'];
	
	function onLoad() {
		// Construct Map Viewer and display default location:
		mapviewer = new MultimapViewer( document.getElementById( 'mapviewer' ) );
		mapviewer.goToPosition( new MMLocation( new MMLatLon( 45.501747, - 73.721298 ) ,3 ) );
	
		search_address = new MMAddress({ qs : 'CA' });
		//Add widgets
		mapviewer.addWidget( new MMMapTypeWidget() );
		mapviewer.addWidget( new MMPanZoomWidget() );		
		// Make form elements accessible as variables:
		filter6 = document.getElementById( 'filter6' );   
		qsValue = document.getElementById( 'qsearch' ); 
	   // boolean1 = document.getElementById( 'boolean1' );      
		submitbtn = document.getElementById( 'submitbtn' ); 
		loading = document.getElementById( 'loading' );
		printp= document.getElementById( 'printpage' );
		loadingStatus( false );
	}
	
	
	function openInfoBox( type, target) {
		if( target.infoBoxOpened() ) {
			target.closeInfoBox();
		}
		else {
			target.openInfoBox( );
		}
	}
	
	function loadingStatus ( bool ) {
		// If we're loading values we want to disable the form elements
		// and display a spinning icon to show activity
		
		submitbtn.disabled = bool;
		loading.style.display = bool ? 'block' : 'none';
	}
	
	function createMarker(location, display_name, num) {
		var icon = new MMIcon( 'http://reebokhockey.com/corpo/images/locator.png' );
		icon.iconSize = new MMDimensions( 32, 32 );
		icon.iconAnchor = new MMPoint( 16, 15 );
		icon.textAnchor = new MMPoint( 7, 5 );
		var marker = mapviewer.createMarker(location, {'text' : num, 'icon':icon});
		
		marker.setInfoBoxContent('<p>' + display_name + '<' + '/p>');
		return marker;
	}
	  
	function initSearch () {
		cleanUp();
		searcher = new MMSearchRequester( funcRef );
		search_address = new MMAddress({ qs : qsValue.value+", "+ filter6.value });
		// Set return fields, radius units, and maximum number of records to return from search:
		search = new MMSearch(); 
		search.return_fields = rfs;
		search.radius_units = 'km';
		search.nin_distance = 0; 
		search.max_distance = 50; 
		search.count = 10; 
		search.address = search_address;    
	// Activate the next line of code if you wish to configure the API to use data
	// from one of your existing 'classic' Multimap services (such as XML Storefinder), 
	// as described in the topic "Integration with Classic Multimap XML Products".  
	// When doing so, please replace "[api_key]" with the appropriate service client name:
	// search.data_source = 'mm.clients.[reebok_hk]'
	
	   /* // If an attribute has been selected for the first filter item, apply the filter:
		if ( filter1.value != '') {
			search.filters = new Array();
			search.filters.push( new MMSearchFilter(filter1.value, filter2.value, filter3.value) );
		}
		
		search.logic = 'AND';
	
	  if ( filter6.value != '') {
			search.filters = new Array();
			search.filters.push( new MMSearchFilter(filter4.value, filter5.value, filter6.value) );
		} */
	   
			
		loadingStatus( true );
		searcher.search( search );
	}  
	
	function cleanUp () {
		// Clean up the HTML containers
		var message = document.getElementById('message');
		while (message.firstChild) {
			message.removeChild(message.firstChild);
		}
		printp.style.display = 'none';
		message.style.display = 'none';
		var record_list = document.getElementById('recordListDiv');
		while (record_list.firstChild) {
			record_list.removeChild(record_list.firstChild);
		}            
		mapviewer.removeAllOverlays();
	   
	   markers = new Array(); 
	}
			
	function resultsLoaded ( ) {
		var container = document.getElementById('recordListDiv');
		
		// Results are now loaded, so re-enable form elements, and remove spinning icon:
		loadingStatus( false );
	
		// If an error code has been produced, display the explanation:
		if ( searcher.error_code ) {
			var err =  '';
			if ( searcher.error_explanation ) {
				err="No matches found. Please add more details to your search and try again.";
				//err =  searcher.error_explanation;
			} else {
				err="No matches found. Please add more details to your search and try again.";
				//err =  'Your request failed. Error code: ' + searcher.error_code;
			}
			var errm= document.getElementById('message') ;
				errm.innerHTML = err;
				errm.style.display = 'block';
			return;
		} 
	
		var results_returned = 1;
		
		var tbl = document.createElement('table');
		tbl.id = 'recordListt';
		var tbody = document.createElement('tbody');
		
		
		//var el = document.createElement ( 'ol' );
		//el.id = 'recordList';
		var  start_index_value = 1;     
		 
		// Loop through each record set:
		for ( var count=0, l = searcher.record_sets.length; count < l; count++ ) {
			
			// If an error was returned for the record set, display details and return:
			if ( searcher.record_sets[count].error ) {
				var err =  '';
				if ( searcher.record_sets[count].error.error_explanation ) {
					//err =  searcher.record_sets[count].error.error_explanation;
					err="No matches found. Please add more details to your search and try again.";
				} else {
					//err =  'Your request failed. Error code: ' + searcher.record_sets[count].error.error_code;
						err="No matches found. Please add more details to your search and try again.";
				}
				var errm= document.getElementById('message') ;
				errm.innerHTML = err;
				errm.style.display = 'block';
				return;  
			} 
			
			/*  var tbl_th  = document.createElement('tr'); 
			 
			  var tbl_td1 = document.createElement('td');
			  tbl_td1.innerHTML= '<b>Map</b>'; 
			  tbl_th.appendChild(tbl_td1);
			  var tbl_td2 = document.createElement('td');
			  tbl_td2.innerHTML= '<b>Store Name</b>'; 
			  tbl_th.appendChild(tbl_td2);	
			  var tbl_td3 = document.createElement('td');
			  tbl_td3.innerHTML= '<b>Phone</b>'; 
			  tbl_th.appendChild(tbl_td3);
			  var tbl_td4 = document.createElement('td');
			  tbl_td4.innerHTML= '<b>Distance</b>' ; 
			  tbl_th.appendChild(tbl_td4);
			  
			  tbody.appendChild(tbl_th);*/
								 
			  var tbl_trh = document.createElement('tr');
			  
			  var tbl_td1 = document.createElement('th');
			  tbl_td1.appendChild( document.createTextNode( 'Map') ); 
			  tbl_trh.appendChild(tbl_td1);
			  var tbl_td2 = document.createElement('th');
			  tbl_td2.appendChild( document.createTextNode( 'Store Name') ); 
			  tbl_trh.appendChild(tbl_td2);	
			  var tbl_td3 = document.createElement('th');
			  tbl_td3.appendChild( document.createTextNode( 'Contact Information') ); 
			  tbl_trh.appendChild(tbl_td3);
			  var tbl_td4 = document.createElement('th');
			  tbl_td4.appendChild( document.createTextNode( 'Distance') ); 
			  tbl_trh.appendChild(tbl_td4); 					 
								 
					  
			  tbody.appendChild(tbl_trh);
			
			// If not, check to see if individual records have been returned:
			if ( searcher.record_sets[count].records ) {
								  
			  
				// Loop through each record in the record set, and add it to the list below the map,
				//  and populate the infobox text:
				for (var record_count = 0, rl = searcher.record_sets[count].records.length; record_count < rl; record_count++ ) {

					var tbl_tr1  = document.createElement('tr');					
					var record = searcher.record_sets[count].records[record_count];  
							
					var anchor = handleRecord(record, start_index_value + record_count);
					//var li = document.createElement('li');
					//li.appendChild(anchor);
					//el.appendChild ( li );
				
					var tbl_td5 = document.createElement('td');
					tbl_td5.innerHTML= record_count+1; 
					tbl_tr1.appendChild(tbl_td5); 
					
					var tbl_td6 = document.createElement('td');
					tbl_td6.appendChild(anchor);
					tbl_tr1.appendChild(tbl_td6); 

					var tbl_td7 = document.createElement('td');
					tbl_td7.innerHTML= record[rfs[6]]; 
					tbl_tr1.appendChild(tbl_td7);

					var tbl_td8 = document.createElement('td');
					tbl_td8.innerHTML= record.distance.km + ' km / ' + record.distance.miles + ' mi' ; 
					tbl_tr1.appendChild(tbl_td8);					 
					
					tbody.appendChild(tbl_tr1); 
					
				}
				printp.style.display = 'block';
				
			}else {
				// No records have been returned. If a record count has been returned, display it,
				// otherwise display a message noting that no records were returned by the search:
				var total = searcher.record_sets[count].totalRecordCount; 
				el = document.createElement ('p');
				if ( total  > 0) {
					el.appendChild ( document.createTextNode( 'Total results found: ' + total ) );
				} else {
					el.appendChild ( document.createTextNode( 'No results returned' ) ); 
				}  
				results_returned = 0;
			}
			
		}
		
		tbl.appendChild(tbody);	
		//container.appendChild(el); 
		container.appendChild(tbl); 							                  
		if ( results_returned == 1 ) {
			mapviewer.goToPosition ( mapviewer.getAutoScaleLocation( markers ) ); 
		 }  
			
	}
	
	function handleRecord ( record, num ) {
		// Create marker text for the infobox for this record:    
		var markerText = '<p class=\'boxTitle\'>'+record[rfs[1]]+ '<' + '/p>'; 
		markerText += '<p class=\'boxDesc\'>';  
		for ( i = 2, j=0; i < rfs.length; i++ ) {
			if ( record[rfs[i]] ) {
				 markerText += record[rfs[i]];
				 if (j==1){ markerText += ', ';}
				else { markerText += '<br>';}
				j++;
			}
		}
		markerText += '<' + '/p>';    
		if ( record.distance ) {
			markerText += '<p>Distance: ' + record.distance.km + ' km / ' + record.distance.miles + ' mi<' + '/p>'
		}
		if ( record.point ) {
			var marker = createMarker( record.point, markerText, num);
			markers.push(marker);
		} 
		// Show the name in our records list. When the name is clicked, open
		//  our info box:    
		var anchor = document.createElement ( 'a' );
		anchor.href = '#';
		anchor.record_id = record.id;
		anchor.appendChild(document.createTextNode(record.name));
		anchor.onclick = function () { openInfoBox ( 'click', marker ); return false; };
		return anchor;
	
	}
	  
