// Author: Sergi Diaz Planas
/*
    These are the functions whose tasks do not belong to a specific category.
    Most of them perform tasks involving view's appearance manipulation.
*/

// Client status regarding the communication with server
var dniNum = "unknown";      // dni number sent by server

// Send the given image in reference request to the server directed by given server address.
function sendReference(server, image, canvas)
{
    // Pack additional data, comprising (x,y) origin, width and height of the rectangles
    // containing the dni photo, the number and the names (name + surname/s)
    var add_data = {"PX" : pho_x, "PY" : pho_y, "PW" : pho_w, "PH" : pho_h,
                    "DX" : dni_x, "DY" : dni_y, "DW" : dni_w, "DH" : dni_h,
                    "NX" : nms_x, "NY" : nms_y, "NW" : nms_w, "NH" : nms_h,
                    "SX" : snm_x, "SY" : snm_y, "SW" : snm_w, "SH" : snm_h };
    
    // Parameters of the request                
    var params = {"REQ" : "REF", "IMG" : image.toDataURL("image/jpg", ""), "ADD" : add_data};

    // Use ajax to post the request
    $.ajax({
		type: "POST",
		url: server,
		data: params,
		dataType: "json",
        success: function(json) 
        {
            // If the request succeeded, retrieve the information sent by server
            // and update status
            if (json["ANS"] == "TRUE") 
            {
                drawRects(canvas, "rgb(0, 255, 0)");  // Green
                setForm(json["DNI"], json["Name1"], json["Name2"]);

                var video = document.getElementById("video");
                video.pause();

                // Enable continue button
                document.getElementById("btnContinue").disabled = false;
            }
            // Reference could not be set, update status
            else
            {
                drawRects(canvas, "rgb(255, 0, 0)");  // Red
            }          
        },
        failure: function() 
        { 
            // Reference cannot be set if client fails at communicating with server.
            drawRects(canvas, "rgb(255, 0, 0)");  // Red
        }
    });	
}

// Send the given image in validation request to the server directed by given server address.
function sendForValidation(server, image)
{
    // Parameters of the request                
    var params = {"REQ" : "VAL", "IMG" : image.toDataURL("image/jpg", "")};
    
        // Use ajax to post the request
        $.ajax({
            type: "POST",
            url: server,
            data: params,
            dataType: "json",
            success: function(json) 
            {
                var video = document.getElementById("video");
                video.pause();

                if (json["ANS"] == "TRUE") 
                {
                    //window.location.href = "./private.php";
                    alert("Successful validation!");
                }   
                else
                {   
                    alert("Not a valid document!");
                }          

                // Enable finish button
                document.getElementById("btnFinish").disabled = false;
            },
            failure: function() 
            { 
                // Reference cannot be set if client fails at communicating with server.
                validDocument =  false;
            }
        });	
}