﻿function _inDown(sender, eventArgs) {
    bhf = false;
    if (bna) {
        _zbDM = true;
        var P1 = (inp - 2);
        var P2 = (inp - 1);

        var solIP = pcc.FindName("page" + P1 + "ip");
        var sagIP = pcc.FindName("page" + P2 + "ip");
        if (sender.name == "oddPageCanvas")
            _inStk = solIP;
        else
            _inStk = sagIP;
        if (_inbErase) {
            _inErase = pcc.createFromXaml('<StylusPointCollection/>');
            _inErase.addStylusPoints(eventArgs.getStylusPoints(sender));
        } else {
            _insID++;
            var uniqueName;
            if (sender.name == "oddPageCanvas")
                uniqueName = "st" + P1 + "_" + _insID;
            else
                uniqueName = "st" + P2 + "_" + _insID;
            var strokeXaml = "<Stroke xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='" + uniqueName + "'/>";
            _inStoke = pcc.createFromXAML(strokeXaml);
            _inStk.Strokes.Add(_inStoke);
            _inStoke.DrawingAttributes = dda;
            var stylusPoints = eventArgs.GetStylusPoints(sender);
            _inStoke.StylusPoints.AddStylusPoints(stylusPoints);
        }

        sender.releaseMouseCapture();
        sender.captureMouse();
    }
}
function _inMove(sender, eventArgs) {
    if (bna) {
        if (_inbErase && _inErase != null) {
            _inErase.addStylusPoints(eventArgs.getStylusPoints(sender));
            var hitStrokes = _inStk.strokes.hitTest(_inErase);
            for (i = 0; i < hitStrokes.count; i++)
                _inStk.strokes.remove(hitStrokes.getItem(i));

            if (sender.name == "oddPageCanvas")
                var pageNo = (inp - 2);
            else
                var pageNo = (inp - 1);

            var cID = _inGetArray(pageNo);
            _insCol[cID] = "";
            addLines(pageNo, "");            
        }
        else if (_inStoke != null) {
            var stylusPoints = eventArgs.GetStylusPoints(sender);
            _inStoke.StylusPoints.AddStylusPoints(stylusPoints);
        }
    }
}
function _inUp(sender, eventArgs) {
    if (bna) {
        if (_inStoke != null || _inbErase)
            _inbSave = true;
        sender.releaseMouseCapture();
        _zbDM = false;
        _inStoke = null;
        _inErase = null;
    }
}
function _inGetArray(ia) {
    var mPageN = (ia);
    for (m = 0; m < _insCol.length; m++) {
        var mText = _insCol[m];
        var mSayi = mText.split("_", 1);
        mText = null;
        if (mSayi == mPageN)
            return m;
    }
    return -1;
}
function _inInit(IN) {
    var mID = _inGetArray(IN);
    var mINK = pcc.FindName("page" + IN + "ip");
    if (mINK)
        mINK.strokes.clear();
    if (mID == -1) return;
    var data = _insCol[mID];
    _inLoad(data.slice(data.indexOf("_") + 1), mINK);
    data = null;
}
function _inLoad(str, addInk) {
    var strokeStrings = str.split(";");
    for (var i = 0; i < strokeStrings.length - 1; i++) {
        var stroke = pcc.createFromXaml("<Stroke/>");
        stroke.drawingAttributes = dda;
        stylusPoints = strokeStrings[i].split(",");
        for (var j = 0; j < stylusPoints.length / 2; j++) {
            var stylusPoint = pcc.createFromXaml("<StylusPoint/>");
            stylusPoint.x = stylusPoints[2 * j];
            stylusPoint.y = stylusPoints[2 * j + 1];
            stroke.stylusPoints.add(stylusPoint);
        }
        addInk.strokes.add(stroke);
        stroke = null;
    }
    strokeStrings = null;
}
function _inSave(IN) {
    var serializedStylusPoints = "";
    var strokes = null;
    strokes = pcc.FindName("page" + IN + "ip").strokes;
    if (strokes != null) {
        serializedStylusPoints = IN + "_";
        var strokeCount = strokes.Count;
        if (strokeCount > 0) {
            for (i = 0; i < strokeCount; i++) {
                var stroke = strokes.GetItem(i);
                var packetCount = stroke.stylusPoints.count;
                for (j = 0; j < packetCount; j++) {
                    var stylusPoint = stroke.stylusPoints.getItem(j);
                    serializedStylusPoints += stylusPoint.x.toString();
                    serializedStylusPoints += ",";
                    serializedStylusPoints += stylusPoint.y.toString();
                    if (j != packetCount - 1)
                        serializedStylusPoints += ",";
                    else
                        serializedStylusPoints += ";";
                }
            }
            var mFree = _inGetArray(IN);
            if (mFree == -1)
                _insCol.push(serializedStylusPoints);
            else
                _insCol[mFree] = serializedStylusPoints;
        }
        serializedStylusPoints = null;
    }
   
}

function addLines(number, text) {
    var http = new XMLHttpRequest();
    var url = "an.aspx";
    var params = "pnum=" + number + "&mUser=" + mUser + "&text=" + text + "&type=1";
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = function() {//Call a function when the state changes.
        if (http.readyState == 4 && http.status == 200) {
       
            var RT = http.responseText;
            return RT;
        }
    }
    http.send(params);
}


function getLines() {
    if (DigiCheck()) {
        var mDown = new XMLHttpRequest();
        mDown.open("GET", "nc.aspx?username=" + mUser + "&type=1", false);
        mDown.send(null);
        if (mDown.readyState == 4 && mDown.status == 200)
            var RT = mDown.responseText;
        var temp = RT.split("#");
        for (i = 0; i < temp.length - 1; i++)
            _insCol.push(temp[i]);
    }
}