Garmin.DeviceControl.MESSAGES = {
	deviceControlMissing: langComm['deviceControlMissing'],
	missingPluginTag: langComm['missingPluginTag'],
	browserNotSupported: langComm['browserNotSupported'],
	pluginRequired: "The Garmin Communicator Plugin is required to register the device for the serial number entered. <a href='http://www8.garmin.com/products/communicator/'>Download and install this free Internet browser plugin now.</a> After installing the plugin, you must restart your browser and return to product registration to continue. If you're still having problems and need additional support, see our <a href='http://www.garmin.com/products/communicator/faq.jsp'>Communicator FAQs</a>.",
	pluginNotInstalled: langComm['pluginNotInstalled'],
	outOfDatePlugin1: langComm['outOfDatePlugin1'],
	outOfDatePlugin2: langComm['outOfDatePlugin2'],
	pluginNotUnlocked: langComm['pluginNotUnlocked'],
	noDevicesConnected: langComm['noDevicesConnected'],
	invalidFileType: langComm['invalidFileType'],
	incompleteRead: langComm['incompleteRead'],
	unsupportedDataType: langComm['unsupportedDataType']
	};

var DeviceInfo = Class.create();
DeviceInfo.prototype = {
	initialize: function(commDiv, errorDiv, keysArray) {
		this.keys = keysArray;
		this.unitDiv = $(commDiv);
		this.unitDiv.style.visibility= 'visible';
		this.errDiv = $(errorDiv);
		clearErrors();
		this.sendBtn = $('send_btn');
		this.garminController = null;
		this.intializeController();
	},

	intializeController: function() {
		try {
			this.garminController = new Garmin.DeviceControl();
			this.garminController.register(this);
			this.garminController.setPluginRequiredVersion([2,5,2,0]); 
			this.garminController.unlock(this.keys);
		} catch (e) { 
			//this.handleException(e); 
			showErrors(pluginRequired)
		}
	},

	handleException: function(error) {
		var msg = error.name + ": " + error.message;
		//var msg = error.toString;
		
		if (Garmin.PluginUtils.isDeviceErrorXml(error)) {
			msg = 'pu'+Garmin.PluginUtils.getDeviceErrorMessage(error);	
		}
		// catch plugin related messages		

		if (error.name == "pluginRequired"){
			msg =  'pluginRequired';//zmienna
		} else if (error.name == "PluginNotInstalledException"){
			msg =  'pluginRequired';//zmienna
		} else if (error.name == "OutOfDatePluginException") {
			msg = 'pluginOutOfDate';//zmienna
		} else if (error.name == "BrowserNotSupportedException") {
			msg = "<strong>" + 'browserNotSupportedDetailText' + "</strong> " + 'compatibleBrowsersText' ; //zmienne
			
		}
		
		try{
			showErrors(msg);
		}catch(e){
			alert(msg);
		}
	},

	onStartFindDevices: function(json){
		this.unitDiv.innerHTML = langComm['search_connected_dev'];
	},

	onFinishFindDevices: function(json){
		if(json.controller.numDevices > 0) {
			var devices = json.controller.getDevices();
			var selDev = new Array();
			var device;
			var k=0;
			for (var i=0; i<devices.length; i++){
				device = devices[i];
				if (device.getSoftwareVersion()!="0" ) {
					selDev[k++] = i;
					device1 = device;
				}
			}

			if (selDev.length > 1) {
				try{
				//this.unitDiv.innerHTML = langComm['found_too_much_dev'] + langComm['connect_one_and_again'];
				var dSel = document.createElement('select');
				dSel.id = "unitSelect";
				dSel.onchange = function(){setSelectedUnit();} 

				var firstUnit = true;
				for (var i=0; i<selDev.length; i++){
					device = devices[selDev[i]];
					var model = device.getDescription();
					var id = device.getId();
					dSel.options[i] = new Option(model + ', ' + langComm['unit_id'] + id, id, firstUnit);
					if(firstUnit){ 
						$('i_unitID').value = id;
						this.sendBtn.disabled = false;
						firstUnit = false;
					}
				}

				this.unitDiv.innerHTML = langComm['found_too_much_dev'];

				this.unitDiv.appendChild(dSel);
				}catch(e){
					this.handleException(e);
				}
			} else {
				device = device1;
				var model = device.getDescription()
				var id = device.getId()
				var sv = device.getSoftwareVersion().substring(0,device.getSoftwareVersion().length-2) + "." + device.getSoftwareVersion().substring(device.getSoftwareVersion().length-2);

				var firstFix = '';
/*
				var deviceDescriptionXml = unl.garminController.garminPlugin.getDeviceDescriptionXml(device.getNumber() );	
				var deviceDescriptionDoc = Garmin.XmlConverter.toDocument(deviceDescriptionXml);

				try {
					// get the first fix
					var firstFix = deviceDescriptionDoc.getElementsByTagName("Ifix")[0].firstChild.nodeValue;
				} catch(err) {
					// XML parsing failed
					var firstFix = '';
				}
*/
				this.unitDiv.innerHTML = langComm['founded_unit'] + '<b>'+model+'</b>' + ' ' + langComm['unit_id'] + '<b>'+id+'</b>' + ' ' + langComm['unit_sv'] + '<b>'+sv+'</b>';
				if(firstFix != ''){
					var ffDate = new Date();
					var today = new Date();
					ffDate.setTime(today.getTime()-firstFix);
					var ffTxt = ffDate.getDate()+'.'+ffDate.getMonth()+'.'+ffDate.getFullYear();
					this.unitDiv.innerHTML += ' firstfix: '+ffTxt;
				}
				$('i_unitID').value = id;
				this.sendBtn.disabled = false;
			}
		}else{
			this.unitDiv.innerHTML = langComm['none_found'] + langComm['connect_one_and_again'];
		}
	},

	onCancelFindDevices: function(json) {
		this.unitDiv.innerHTML = langComm['find_cancel'];
    },

	findDevices: function() {
			this.garminController.findDevices();
	}


};

function loadGarminApi() {
	display = new DeviceInfo('unitForm', 'errors', siteKeys);

	try {
		$('send_btn').disabled = true;
		display.findDevices();
	}catch(err){
		$('send_btn').disabled = true;
		display.handleException(err);
		//eat the error, it's probably because the communicator is not installed.
	}
}


function regbyGarminApi() {
	display = new DeviceInfo('unitForm', 'errors', siteKeys);

	try {
		$('send_btn').disabled = true;
		display.findDevices();
	}catch(err){
		$('send_btn').disabled = false;
		display.handleException(err);
		//eat the error, it's probably because the communicator is not installed.
	}
}


function unlbyGarminApi(){
	display = new DeviceInfo('unitForm', 'errors', siteKeys);

	try {
		$('send_btn').disabled = true;
		display.findDevices();
	}catch(err){
		$('send_btn').disabled = false;
		display.handleException(err);
		//eat the error, it's probably because the communicator is not installed.
	}
}


function setSelectedUnit(){
	$('i_unitID').value = $('unitSelect').options[$('unitSelect').selectedIndex].value;
	try{showFID();}catch(e){};
}


PluginTest = Class.create();
PluginTest.prototype = {
	initialize: function() {
		this.result = false;
		this.err = '';
		this.msg = '';
		this.version = '';
		var system = true;
		switch(BrowserDetect.OS) {
			case "Windows":
	       		break;
	       	case "Mac":
	        	break;
	       	default:
				system = false;
	       		break;
		}
		if(system){
			if(window.opera){
				this.err = 'opera';
				this.msg = langComm['pluginRequiredUnsup'];
			}else if (PluginDetect.detectGarminCommunicatorPlugin()) {
				try {
					// Create Controller
					this.controller = new Garmin.DeviceControl();
					// Set the latest plugin version available for download
					this.controller.setPluginLatestVersion([2,6,1,0]);
					this.version = this.controller.getPluginVersion();
					// Check latest version
					this.controller.checkForUpdates();
					this.result = true;
				} catch(error) {
					if (error.name == "OutOfDatePluginException" || error.name == "UpdatePluginException") {
						this.err = 'outofdate';
			       		this.msg = langComm['outOfDatePlugin1'];
					}else  {
						this.err = 'noinstall';
			       		this.msg = langComm['pluginRequired'];
					}
				}
			} else {
				this.err = 'noinstall';
	       		this.msg = langComm['pluginRequired'];
			}
		}else{
			this.err = 'system';
       		this.msg = langComm['os_wrong'];
		}
	}
};
function checkPlugin() {
	var plugInTest = new PluginTest();
	return plugInTest;
}

function getPluginVer(){
	var t = checkPlugin();
	if(t.result){
		document.write(langComm['gc_shortmsg']['pluginInstalled']+t.version[0]+'.'+t.version[1]+'.'+t.version[2]+'.'+t.version[3]);
	}else{
		if(t.err=='outofdate'){
			document.write(langComm['gc_shortmsg']['outofdate1']+t.version[0]+'.'+t.version[1]+'.'+t.version[2]+'.'+t.version[3]+langComm['gc_shortmsg']['outofdate2']);
		}else 
			document.write(langComm['gc_shortmsg'][t.err]);
	}
}

function checkForGC(){
	var t = checkPlugin();
	if(t.result){
		$('unitForm').style.display = 'block';
		$('formForGC').style.display = 'block';
		$('formForManual').style.display = 'none';

		$('i_unitID').disabled = false;
		$('i_unitModel').disabled = false;
		$('i_unitSN').disabled = false;

		$('i_man_unitID').disabled = true;
		$('i_man_unitModel').disabled = true;
		$('i_man_newModel').disabled = true;
		$('i_man_unitSN').disabled = true;
	}else{
		$('unitForm').style.display = 'none';
		$('formForGC').style.display = 'none';
		$('formForManual').style.display = 'block';

		$('i_type').value = 'manual';

		$('i_unitID').disabled = true;
		$('i_unitModel').disabled = true;
		$('i_unitSN').disabled = true;

		$('i_man_unitID').disabled = false;
		$('i_man_unitModel').disabled = false;
		$('i_man_newModel').disabled = false;
		$('i_man_unitSN').disabled = false;
		$('send_btn').disabled = false;
	}
}
