function loadGarmin() {
	if (findPlugin()) {
		unlockPluginAndStartFindingDevices(garminKeyCodeArray);
	}
}

/*
 * Creates an instance of the plugin and and instance of the GarminGpsPlugin.js object. Then we attempt 
 * to get the Version and by examining that we can determine if the plugin exists.
 */	
function findPlugin() {

	var MyControl;

	if( window.ActiveXObject ){
		MyControl =  document.getElementById('GarminActiveXControl');
	}else {
		MyControl =  document.getElementById('GarminNetscapePlugin');
	}
		    
	garminGpsPlugin = new Garmin.DevicePlugin(MyControl);
	
	// Test the plugin to make sure that we can script it.
	var thePluginVersionString;
	try {
		thePluginVersionString = garminGpsPlugin.getVersionXml();
	} catch(e) {
		//alert(e);
	}
	
	if( typeof thePluginVersionString == "undefined" ) {
		return false;
	} else {
		return true;
	}		
}

/*
 * Unlocks the plugin. If that is successful then we initate the startFindDevices().
 * If all is well we return true;
 */	
function unlockPluginAndStartFindingDevices() {
	if(garminGpsPlugin.unlock(garminKeyCodeArray) != true) {
		return false;
	}

	try {
		// cancel any existing calls
		garminGpsPlugin.cancelFindDevices();
		garminGpsPlugin.cancelReadFitnessData();
		
		garminGpsPlugin.startFindDevices();
	} catch(e) {
		alert(e);
		return false;
	}
	
	return true;
}

/*
 * Because the plugin operates in an asynchronous manner, we must poll devices to see if the 
 * initating command has completed. This returns true if the find is complete otherwise false.
 */
function finishFindingDevices(){
	return garminGpsPlugin.finishFindDevices();	
}

/*
 * Displays the xml description of all devices connected to this machine. If a device is actually 
 * connected, there will be a node in the xml with the value : 'Number' for each device enumerated
 * from 0 to the total number of devices. If the xml string contains the 'Number' string then return 
 * the whole xml chunk otherwise return null.
 * 
 */
function getDescriptionOfDevices(){
	var deviceDescription =  garminGpsPlugin.getDevicesXml();
		
	//Possibly replace this with some kind of DOM node search...	
	if(deviceDescription.indexOf("Number", 0) > -1){
		return ieGarminFix(deviceDescription);
	}else{
		return null;
	}
}


/* Initiates a history read from the fitness device. Poll using the function 'finishReadAndGetDataForFitness()' method below to 
 * extract the xml data from the device. The device will continue to complete the task and indicate 
 * it's status with the 'completionState'. See the  'finishReadAndGetDataForFitness()' function.
 */
function startReadHistoryFromDevice(device) {
	// cancel any existing calls	
	garminGpsPlugin.cancelReadFitnessData();
	
	garminGpsPlugin.startReadFitnessData(device, "FitnessHistory");
}

/*
 * Poll with this method waiting for theCompletionState to show a value of 3. Here are all of the 
 * possible completion states:
 * 
 * 	0: idle
 * 	1: working
 * 	2: waiting
 * 	3: finished
 * 
 */
function finishReadAndGetDataForFitness(){

	var theCompletionState = garminGpsPlugin.finishReadFitnessData();
		
	if(theCompletionState != null){
			
		//If the completion state is 2 or 'waiting' then get the progress message...
		if(theCompletionState == "2"){
			//alert(garminGpsPlugin.getProgressXml());
			return;
		}
	}
		
	return ieGarminFix(garminGpsPlugin.getTcdXml());
}


// There appears to be a problem with the garmin plugin / javascript and flash when used in combo.
// This hack appears to fix the issue by stripping characters from the XML.
function ieGarminFix(pluginString) {
	if (Prototype.Browser.IE) {
		pluginString = pluginString.replace(/[^a-zA-Z 0-9<//>()_,!?="/.:/-]+/g,'');
	}
	return pluginString;
}

