
changelanguage();

// Check for Web Browser API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
} else {
  alert("File APIs are not fully supported in this browser.");
}
 
function saveTextAsFile()
{
    var textToSave = document.getElementById("inputTextToSave").value;
    var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
    var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
	fileNameToSaveAs = fileNameToSaveAs + ".csv";
 
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = textToSaveAsURL;
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
 
    downloadLink.click();
}
 
function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}
 
 function parseCSV()
{
    var iconName;
    var funcName;
	var iconSel;
    var textToSave = document.getElementById("inputTextToSave");
	var textCSV = textToSave.value;
	var lines = textCSV.split("\n");
    var information = lines[1].split(";");
	var field = document.getElementById("NameLoco");
	field.value = information[0];
    field = document.getElementById("NumImage");
	field.value = information[1];
	field = document.getElementById("SpeedMax");
	field.value = information[2];
	for (let i = 0; i < 29; i++) {
	    iconName = "iconID" + information[3 + i];
        funcName = "F" + i;
		iconSel = document.getElementById(funcName);
	    iconSel.className = iconName;
	}
	var imgSrc = document.getElementById("imageToShow");
    imgSrc.src = "image/" + information[1] + ".bmp";
}  
 
 
function loadFileAsText()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];
	var loco = document.getElementById("NumLoco");
	var locoFileName = fileToLoad.name.split(".");
	loco.value = locoFileName[0];
 
    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
	fileReader.onloadend = function(progressEvent) 
	{
	    parseCSV();
	}
    fileReader.readAsText(fileToLoad, "UTF-8");
	hideInstrucctions();
}

function loadFileAsImage()
{
    var imgSrc = document.getElementById("imageToShow");
	var imgNum = document.getElementById("NumImage");
    var fileToLoad = document.getElementById("imageToLoad").files[0];
	var imgFileName = fileToLoad.name.split(".");
	imgNum.value = imgFileName[0];
    imgSrc.src = "image/" + imgFileName[0] + ".bmp";
	
	hideInstrucctions();
}

function changeImageLoco() 
{
    var imgNum = document.getElementById("NumImage");
	var imgSrc = document.getElementById("imageToShow");
    imgSrc.src = "image/" + imgNum.value + ".bmp";
}

 
function changelanguage() 
{
    const languageSelect = document.getElementById('language-select');
    elements = document.querySelectorAll(`span[lang]`);
    for (let element of elements) {
      element.style.display = 'none';
	}
	var x = languageSelect.selectedIndex;
	if (x==0) {elements = document.querySelectorAll(`span[lang="en"]`);}
	if (x==1) {elements = document.querySelectorAll(`span[lang="es"]`);}
	if (x==2) {elements = document.querySelectorAll(`span[lang="de"]`);}
	if (x==3) {elements = document.querySelectorAll(`span[lang="ca"]`);	}
	for (let i = 0; i < elements.length; i++) {
		elements[i].style.display = 'inline-block';
	}
};
  
function selectIcon(value) 
{
    var allIcon = document.getElementById('iconAll');
	allIcon.style.display = 'block';
	var funcNum = document.getElementById('NumFunc');
	funcNum.value = value;
	var iconFunc = document.getElementById('Fx');
	const iconText = "F" + value;
	iconFunc.innerHTML = iconText;
}  


function updateIcon(value) 
{
    var funcNum = document.getElementById('NumFunc');
    const iconName = "iconID" + value;
    const funcName = "F" + funcNum.value;
	var iconSel = document.getElementById(funcName);
	iconSel.className = iconName;
	var allIcon = document.getElementById('iconAll');
	allIcon.style.display = 'none';
	hideInstrucctions();
}

function hideInstrucctions() 
{
	var showIns = document.getElementById('instruc');
	showIns.style.display = 'none';
}

function createTextCSV() 
{
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs")
    var textToSave = document.getElementById("inputTextToSave");
    var textCSV = "Name;Image;Vmax;F0;F1;F2;F3;F4;F5;F6;F7;F8;F9;F10;F11;F12;F13;F14;F15;F16;F17;F18;F19;F20;F21;F22;F23;F24;F25;F26;F27;F28\n";
	var loco = document.getElementById("NumLoco");
    var field = document.getElementById("NameLoco");
	var funcName;
	var iconSel;
	if (loco.value != "") {
        textCSV = textCSV + field.value;
    	field = document.getElementById("NumImage");
		if (field.value == "")
		    field.value = "0";
	    textCSV = textCSV + ";" + field.value;
    	field = document.getElementById("SpeedMax");
		if (field.value == "")
		    field.value = "100";
    	textCSV = textCSV + ";" + field.value ;

        for (let i = 0; i < 29; i++) {
	        funcName = "F" + i;
    		iconSel = document.getElementById(funcName).className.split("iconID");
		    iconSel = iconSel[1];
	        textCSV = textCSV + ";" + iconSel ;
	    }
        textCSV = textCSV + "\n"
        textToSave.value = textCSV;
    	fileNameToSaveAs.value = loco.value;
    	saveTextAsFile();
	    field = document.getElementById('instruc');
    	field.style.display = 'block';
	}
}
  

  