﻿/**/
// calculate the current window width //
function pageWidth() {
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
    return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function Slope(id, c)
{
    BlockAll(true);
    var ws;
    if (c.innerHTML=='+')
        ws='AddSlope';
    else
        ws='DeleteSlope';
    var pl = new SOAPClientParameters();
    pl.add("id", id);
    SOAPClient.invoke(url, ws, pl, true, sendslope_callBack);
    if (c.innerHTML=='+')
        c.innerHTML='-'
    else
        c.innerHTML='+'
    return false;
}
function sendslope_callBack(r)
{
    BlockAll(false);
}

function BlockAll(how) {
    if (how)
        $.blockUI(
        {
            message: null,
            overlayCSS: { backgroundColor: '#555'},
            opacity: 0.6
        }
        );
    else
        $.unblockUI();
}

function validate(informa) {
if (informa.filtra.value.length<4)
{
   informa.filtra.focus();
   return (false);
}
return (true);
}


if (window != top) top.location = location;

var dialogid = 'dialog';
var dialog;
var dialogtimeid = 'dialogtime';
var dialogtime;
var lockdialog = false;
var actualIdSalita;

function CreateDivComment() {
    dialog = window.document.getElementById(dialogid);
    dialogtime = window.document.getElementById(dialogtimeid);
}
function CloseComment() {
    if (lockdialog) return false;
    CreateDivComment();
    dialog.style.display = 'none';
    $.unblockUI();
    return false;
}
function CloseTimeComment() {
    if (lockdialog) return false;
    CreateDivComment();
    dialogtime.style.display = 'none';
    $.unblockUI();
    return false;
}
function ShowComment(idsalita) {
    CreateDivComment();

    var dialog_viewdiv = window.document.getElementById('dialog_view');
    var dialogtextcontentdiv = window.document.getElementById('dialogtextcontent');
    if (dialog_viewdiv == null || dialogtextcontentdiv == null) return false;
    dialog_viewdiv.style.display = 'block';
    dialogtextcontentdiv.innerHTML = 'Loading...';
    $.blockUI({
        message: null
    });

    actualIdSalita = idsalita;
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    dialog.style.display = 'block';
    var dialogwidth = dialog.offsetWidth;
    var dialogheight = dialog.offsetHeight;
    var topposition = top + (height / 2) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialog.style.top = topposition + "px";
    dialog.style.left = leftposition + "px";
    dialog.style.zIndex = 1002;

    LoadComments();
    return false;
}
function LoadComments() {
    lockdialog = true;
    var pl = new SOAPClientParameters();
    pl.add('idsalita', actualIdSalita);
    SOAPClient.invoke(url, 'GetCommentsSlopes', pl, true, LoadingComment_callBack);
    return;
}
function LoadingComment_callBack(r) {
    window.document.getElementById('dialogtextcontent').innerHTML = '';
    var i = 0;
    for (i = 0; i < r.length; i++) {
        var curr_date = r[i].DataCommento.getDate();
        var curr_month = r[i].DataCommento.getMonth()+1;
        var curr_year = r[i].DataCommento.getFullYear();
        var curr_hour = r[i].DataCommento.getHours();
        var curr_min = r[i].DataCommento.getMinutes();
        var divtitle = document.createElement('div');
        window.document.getElementById('dialogtextcontent').appendChild(divtitle);
        divtitle.className = 'titlecomment';
        divtitle.innerHTML += curr_date + '/' + curr_month + '/' + curr_year + ' ' + curr_hour + '.' + curr_min + ' - ';
        divtitle.innerHTML += '<a href="community/profile.aspx?ProfileId=' + r[i].UserKey + '">'+r[i].Da+'</a>';
        if (r[i].TimeUser != null && r[i].TimeUser.length == 6) {
            divtitle.innerHTML += ' (' + ConvertTime(r[i].TimeUser) + ')';
        }
        var divcontent = document.createElement('div');
        divcontent.className = 'textcomment';
        window.document.getElementById('dialogtextcontent').appendChild(divcontent);
        var commento = r[i].Commento;
        while (commento.indexOf('\n') != -1) commento = commento.replace('\n', '<br />');
        divcontent.innerHTML += commento;
        var divseparator = document.createElement('hr');
        window.document.getElementById('dialogtextcontent').appendChild(divseparator);
    }

    lockdialog = false;
    return false;
}

/**********************************************************************************/
function ShowTime(idsalita) {
    CreateDivComment();

    var dialog_viewdiv = window.document.getElementById('dialogtime_view');
    var dialogtextcontentdiv = window.document.getElementById('dialogtimetextcontent');
    if (dialog_viewdiv == null || dialogtextcontentdiv == null) return false;
    dialog_viewdiv.style.display = 'block';
    dialogtextcontentdiv.innerHTML = 'Loading...';
    $.blockUI({
        message: null
    });

    actualIdSalita = idsalita;
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    dialogtime.style.display = 'block';
    var dialogwidth = dialogtime.offsetWidth;
    var dialogheight = dialogtime.offsetHeight;
    var topposition = top + (height / 2) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialogtime.style.top = topposition + "px";
    dialogtime.style.left = leftposition + "px";
    dialogtime.style.zIndex = 1002;
    LoadTimes();
    return false;
}
function LoadTimes() {
    lockdialog = true;
    var pl = new SOAPClientParameters();
    pl.add('idsalita', actualIdSalita);
    SOAPClient.invoke(url, 'GetTimesSlopes', pl, true, LoadingTime_callBack);
    return;
}
function LoadingTime_callBack(r) {
    var result = window.document.getElementById('dialogtimetextcontent');
    result.innerHTML = '';
    if (r != null) {
        var table = document.createElement('table');
        result.appendChild(table);
        table.id = 'tableslopes';
        table.border = '1px';
        table.setAttribute('cellpadding', 3);
        table.setAttribute('cellspacing', 0);
        table.style.borderStyle = 'solid';
        table.style.borderWidth = '1px';
        table.style.bordercoborderColor = 'black';
        table.style.width = '100%';

        var tbhe = document.createElement('thead');
        table.appendChild(tbhe);
        var tbo = document.createElement('tbody');
        table.appendChild(tbo);
        // Header
        var trh = document.createElement('tr');
        tbhe.appendChild(trh);
        var tdh1 = document.createElement('th');
        trh.appendChild(tdh1);
        var tdh1b = document.createElement('b');
        tdh1.appendChild(tdh1b);
        tdh1b.appendChild(document.createTextNode('User'));
        tdh1 = document.createElement('th');
        trh.appendChild(tdh1);
        tdh1b = document.createElement('b');
        tdh1.appendChild(tdh1b);
        tdh1b.appendChild(document.createTextNode('Time'));
        trh.style.backgroundColor = '#c2c2c2';
        trh.style.color = '#000000';

        var i = 0;
        for (i = 0; i < r.length; i++) {
                var tr = document.createElement('tr');
                tr.id = 'tr' + r[i].Id;
                if (i % 2 == 0) {
                    tr.style.backgroundColor = '#f2f2f2';
                }
                else {
                    tr.style.backgroundColor = '#c2c2c2';
                }
                tbo.appendChild(tr);
                var td1 = document.createElement('td');
                var link = document.createElement('a');
                link.setAttribute('href', 'community/profile.aspx?ProfileId=' + r[i].UserKey);
                link.appendChild(document.createTextNode(r[i].Da))
                td1.appendChild(link);
                tr.appendChild(td1);

                var td2 = document.createElement('td');
                td2.appendChild(document.createTextNode(ConvertTime(r[i].TimeUser)));
                tr.appendChild(td2);
        }
    }
    lockdialog = false;
    return false;
}
function ConvertTime(time) {
    return time.substring(0, 2) + 'h ' + time.substring(2, 4) + 'm ' + time.substring(4, 6) + 's';
}
