
function LoadCanvas()
{
  var c = document.getElementById("canvas");
  var ctx = getCtx();
  c.addEventListener("mousedown", on_mousedown, false);
  c.addEventListener("mousemove", on_mousemove, false);
  c.addEventListener("mouseup", on_mouseup, false);
}

function Path()
{
  this.xs = new Array();
  this.ys = new Array();
  var ls = new Array();

  this.init = function(inX, inY)
  {
    for (var i = 0; i < inX.length; i++)
    {
      this.add(inX[i], inY[i]);
    }
  }

  this.add = function(x, y)
  {
    var i = ls.length;
    this.xs.push(x);
    this.ys.push(y);
  }
}

function getCtx()
{
  var c = document.getElementById("canvas");
  var ctx = c.getContext("2d");
  return ctx;
}

var drawing = false;
var lastpos = {x:-1, y:-1};
var path;

function on_mousedown(e)
{
//  getCtx().clear();

  var c = document.getElementById("canvas");

  drawing = true;

  lastpos.x = e.clientX - c.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft);
  lastpos.y = e.clientY - c.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);

  path = new Path();
  path.add(lastpos.x, lastpos.y);
}

function on_mousemove(e)
{
  if (!drawing)
  {
    return;
  }

  var c = document.getElementById("canvas");
  var ctx = getCtx();

  var pos = {x:-1, y:-1};

  pos.x = e.clientX - c.offsetLeft + (window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft);
  pos.y = e.clientY - c.offsetTop + (window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop);

  path.add(pos.x, pos.y);

  ctx.strokeStyle = "rgba(0,0,255,0.5)";
  ctx.lineWidth = 4.0;

  ctx.beginPath();
   ctx.moveTo(lastpos.x, lastpos.y);
   ctx.lineTo(pos.x, pos.y);
  ctx.closePath();

  ctx.stroke();

  lastpos = pos;
}

function on_mouseup(e)
{
  drawing = false;
}

function Email()
{
    var email = document.getElementById ("email").value;

    alert ("Email to:" + email);
}

function SubmitEmail()
{
    var canvas = document.getElementById("canvas");
    
    document.emailform.File.value = canvas.toDataURL();
}

function Send()
{
    var email = document.getElementById ("user").value;

    alert ("Email to:" + email);
}
