﻿// JScript File

// global variable
var external_refer = "";
var isRepair = false;

function ottohandler_UpdateVariables(xmlstring) {
    sendconnectrequest("connect_otto.ashx?connect=true", xmlstring, processotto);
}

function ottohandler_UpdateLogInfo(xmlstring) {
    sendconnectrequest("post_ottolog_info.ashx", xmlstring, processlog);
}

function ottohandler_OperationFailed(xmlstring) {
    window.location.href="device_operationfailed.aspx";
}

function ottohandler_RequiresRepair(xmlstring) {
    window.location.href="device_repair.aspx";
}

function sendconnectrequest(url, data, handler)
{
    //document.getElementById('communicating').style.display='';

    if (typeof window.ActiveXObject != 'undefined' ) {
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        xmlDoc.onreadystatechange = handler ;
    }
    else {
        xmlDoc = new XMLHttpRequest();
        xmlDoc.onload = handler ;
    }				    
    xmlDoc.open("POST", url, true);				   
	xmlDoc.send(data); //Send the stream
	window.focus();
}
  
function processotto() {
	if ( xmlDoc.readyState != 4 ) return ;
	
	if (xmlDoc.status == 200) { }
	//document.getElementById('communicating').style.display='none';
	if (!isRepair) window.location.reload(true);
	else window.location.href="device_overview.aspx";
}

function processlog() {
	if ( xmlDoc.readyState != 4 ) return ;
	
	if (xmlDoc.status == 200) { }
	//document.getElementById('communicating').style.display='none';
	window.location.href="ottolog_overview.aspx";		
}

function connectdevice(connect)
{
    if (connect) document.ottohandler.ConnectOtto();
    else         sendconnectrequest("connect_otto.ashx?connect=false", null, processotto); 
}

function clearlogging()
{
	var ret = confirm("Clearing your memory will erase all log files on your device.\nPlease ensure you have saved your log before proceeding.\n\nContinue?");
	if (ret) document.ottohandler.ClearMemory();
}


function uploadfrompc(fileuploadid)
{
	document.ottohandler.UploadLogFromPC(fileuploadid);	
}
		
function uploadfromotto(fileuploadid)
{
	document.ottohandler.UploadLogFromOtto(fileuploadid);	
}

// configuration setting handlers
function setalertsconfiguration(configstring)
{
	document.ottohandler.SetAlertsConfiguration(configstring);
}

function setgeneralconfiguration(configstring)
{
	document.ottohandler.SetGeneralConfiguration(configstring);	
}

function setloggingconfiguration(configstring)
{
	document.ottohandler.SetLoggingConfiguration(configstring);
}

function addcoveragemap(configstring)
{
	document.ottohandler.AddCoverageMaps(configstring);
}

function removecoveragemap(configstring)
{
	document.ottohandler.RemoveCoverageMaps(configstring);
}

function updatecoveragemaps(configstring)
{
	document.ottohandler.UpdateCoverageMaps(configstring);
}

function setsecuritylevel()
{
    document.ottohandler.SetSecurityConfiguration();
}
				
function clearlogging()
{
	var ret = confirm("Clearing your memory will erase all log files on your device.\nPlease ensure you have saved your log before proceeding.\n\nContinue?");
	if (ret) document.ottohandler.ClearLoggingMemory();	
}

function installlogging()
{
    var ret = confirm("Switching to OttoLog will change your device from Road Safety mode to OttoLog trip recording mode.\n\nWARNING: The installation may take several minutes. Do not unplug Otto while the installation is taking place.\n\nContinue?");
	if (ret) document.ottohandler.InstallLogging();	
}

function installroadsafety()
{
    var ret = confirm("Switching to Road Safety mode will change your device from  OttoLog trip recording mode to Road Safety mode.\nAll current log files will be erased from your device.\n\nWARNING: The installation may take several minutes. Do not unplug Otto while the installation is taking place.\n\nContinue?");
	if (ret) document.ottohandler.InstallRoadSafety();	
}

function upgradeotto()
{
    var ret = confirm("Upgrading Otto may enable new features that will erase your current logs and settings.\nYou can reconfigure your device when the upgrade is finished.\n\nWARNING: The upgrade may take several minutes. Do not unplug Otto while the upgrade is taking place.\n\nContinue?");
	if (ret) document.ottohandler.UpgradeOtto();	
}

function repairotto()
{
    var ret = confirm("Repairing Otto will apply the default firmware to your device, and may reconfigure some settings.\nYou can reconfigure your device when the repair is finished.\n\nWARNING: The process may take several minutes. Do not unplug Otto while the repair is taking place.\n\nContinue?");
	if (ret) 
	{
	    isRepair = true;
	    document.ottohandler.RepairOtto();	
	}
}