<?php
include("serverConnect.php");
include("functions.php");

session_start();

?>
<!DOCTYPE html>
<html>
  <head>
    <title>Map svg</title>
    <script src="clipper/clipper.js"></script>
    <script type="text/javascript" src="clipper/svg_todataurl.js"></script>
  </head>
  <body>
    <div id="svgcontainer">
        <svg id="svg" version="1.1" xmlns="http://www.w3.org/2000/svg" style="background: rgba(0, 0, 0, 0)" width="<? echo getHexaWidth() * getMapWidth() * 3/4; ?>" height="<? echo getHexaHeight() * getMapHeight(); ?>"><desc></desc><defs></defs>
        <?
        $cou = count(getTerrainsList()) - 1;
        for($i = 1; $i <=$cou; $i++) {
            echo file_get_contents("resources/map/" . $i);
        }
        ?>
        </svg>
    </div>
    <script>
    var svg = document.getElementById("svg");
				svg.toDataURL("image/png", {
					callback: function(data) {
                        post("addMapSvg.php", {txt : data});
					}
				})
    
    function post(path, params, method) {
        method = method || "post"; // Set method to post by default if not specified.

        // The rest of this code assumes you are not using a library.
        // It can be made less wordy if you use one.
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("action", path);

        for(var key in params) {
            if(params.hasOwnProperty(key)) {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", key);
                hiddenField.setAttribute("value", params[key]);

                form.appendChild(hiddenField);
             }
        }

        document.body.appendChild(form);
        form.submit();
    }
    </script>
  </body>
</html>