
// Check for the File API support.
function fileApiSupport() {
    if (window.File && window.FileReader && window.FileList) {
        //alert('The File APIs are supported in this browser.');
        return true;
    } else {
        alert('The File APIs are not fully supported in this browser.');
        return false;
    }
}

function startTextRead(file, onProgress, onLoad, onError, fileType) {
    try {
        reader = new FileReader();
    } catch (e) {
        alert("FileReader API is not supported by this browser!");
        return;
    }
    if (typeof fileType !== 'undefined') {
        if (!file.name.match(fileType)) {
            alert("Invalid file type");
            return;
        }
    }
    
    reader.file = file;
    reader.onprogress  = onProgress;
    reader.onload      = onLoad;
    reader.onerror     = onError;
    reader.readAsText(file, "UTF-8");
}

function startBinaryRead(file, onProgress, onLoad, onError, fileType) {
    try {
        reader = new FileReader();
    } catch (e) {
        alert("FileReader API is not supported by this browser!");
        return;
    }
    if (typeof fileType !== 'undefined') {
        if (!file.name.match(fileType)) {
            alert("Invalid file type");
            return;
        }
    }
    
    reader.file = file;
    reader.onprogress  = onProgress;
    reader.onload      = onLoad;
    reader.onerror     = onError;
    reader.readAsBinaryString(file, "UTF-8");
}

// Check to see if the new Blob Constructor is supported
function blobSupport() {
   return (typeof Blob == 'function');
}

//saving files locally on the PC
function setupSave(a, data, filename)
{
   if(blobSupport())
   {
      var blob = new Blob([data], {"type":"text/plain"});
      a.href = window.URL.createObjectURL(blob);
      a.target = "_blank";
      a.download = (filename) ? filename : null;
   }
}

function clearSave(a)
{
   a.href = "#";
   a.target = "_self";
   a.download = null;
}
