/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//
// Please read scanner.js developer's guide at: http://asprise.com/document-scan-upload-image-browser/ie-chrome-firefox-scanner-docs.html
//
/** Scan: output PDF original and JPG thumbnails */
function scanToPdfWithThumbnails() {
    scanner.scan(displayImagesOnPage,
            {
                "output_settings": [
                    {
                        "type": "return-base64",
                        "format": "pdf",
                        "pdf_text_line": "By ${USERNAME} on ${DATETIME}"
                    },
                    {
                        "type": "return-base64-thumbnail",
                        "format": "jpg",
                        "thumbnail_height": 200
                    }
                ]
            }
    );
}
/** Processes the scan result */
function displayImagesOnPage(successful, mesg, response) {
    if (!successful) { // On error
        console.error('Falló: ' + mesg);
        return;
    }
    if (successful && mesg != null && mesg.toLowerCase().indexOf('user cancel') >= 0) { // User cancelled.
        console.info('Cancelado por el usuario');
        return;
    }
    var scannedImages = scanner.getScannedImages(response, true, false); // returns an array of ScannedImage
    for (var i = 0; (scannedImages instanceof Array) && i < scannedImages.length; i++) {
        var scannedImage = scannedImages[i];
        processOriginal(scannedImage);
    }
    var thumbnails = scanner.getScannedImages(response, false, true); // returns an array of ScannedImage
    for (var i = 0; (thumbnails instanceof Array) && i < thumbnails.length; i++) {
        var thumbnail = thumbnails[i];
        processThumbnail(thumbnail);
    }
}
/** Images scanned so far. */
var imagesScanned = [];
/** Processes an original */
function processOriginal(scannedImage) {
    imagesScanned.push(scannedImage);
}
/** Processes a thumbnail */
function processThumbnail(scannedImage) {
    var elementImg = scanner.createDomElementFromModel({
        'name': 'img',
        'attributes': {
            'class': 'scanned',
            'src': scannedImage.src
        }
    });
    document.getElementById('images').appendChild(elementImg);
}
/** Upload scanned images by submitting the form */
function submitFormWithScannedImages() {
    if (scanner.submitFormWithImages('form1', imagesScanned, function (xhr) {
        if (xhr.readyState == 4) { // 4: request finished and response is ready
            document.getElementById('server_response').innerHTML = "<h4>Respuesta del Servidor: </h4><b>" + xhr.responseText + "</b>";
            document.getElementById('images').innerHTML = ''; // clear images
            imagesScanned = [];
        }
    })) {
        document.getElementById('server_response').innerHTML = "ENVIANDO... ESPERE POR FAVOR..!!";
    } else {
        document.getElementById('server_response').innerHTML = "HA CANCELADO LA OPERACIÓN. PRIMERO DEBE ESCANEAR UN DOCUMENTO.";
    }
}
