// ******************************************
// * $Id$
// *
// *   sendSFTP()
// *
// * Verstuur gegevens via SFTP
// *
// ******************************************
function sendSFTP(p_connect, p_file, p_data, p_path)
{
    var objFso = new ActiveXObject("Scripting.FileSystemObject");

    Log2File(2, "*> sendSFTP");
    var SFTPsend = false;
    if (p_connect.Connected)
    {
        try
        {
            var handle = p_connect.sftpconnection.OpenFile(p_connect.directory + "/" + p_file, "writeOnly", "createTruncate");
            if (p_connect.sftpconnection.LastMethodSuccess != 1)
                throw { description: p_connect.sftpconnection.LastErrorText };

            if (p_path)
            {
                objFile = objFso.GetFile(p_path);
                Log2File(1, "Transfer file: " + p_file + " (size: " + objFile.Size + ")");
                var success = p_connect.sftpconnection.UploadFile(handle, p_path);
                if (success != 1)
                    throw { description: p_connect.sftpconnection.LastErrorText };
            }
            else
            {   // Forceer p_data altijd naar utf-8
                var tempfile = "../../../temp/puo_" + objFso.GetTempName();
                p_data.SaveToFile(tempfile);
                Log2File(1, "Transfer data as file: " + p_file + " (size: " + p_data.Size + ")");
                var success = p_connect.sftpconnection.UploadFile(handle, tempfile);
                if (success != 1)
                    throw { description: p_connect.sftpconnection.LastErrorText };
                if (S("puo_loglevel") < 4)
                    objFso.DeleteFile(tempfile);
            }
            SFTPsend = true;

            // Close the file.
            success = p_connect.sftpconnection.CloseHandle(handle)
            if (success != 1)
                throw { description: p_connect.sftpconnection.LastErrorText };
        }
        catch(e)
        {
            Log2File(0, "Error sending " + p_file + ": " + e.description, "W");
            SFTPsend = false;
        }
    }
    Log2File(2, "*< sendSFTP");

    return SFTPsend;
}
