﻿// 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;
}

var dialogid = 'dialog';
var dialog;
var dialogmessageid = 'dialogmessage';
var dialogmessage;
var dialogalertid = 'dialogalert';
var dialogalert;
var iseditmode;
var profileuserId;
function CreateDivComment() {
    dialog = window.document.getElementById(dialogid);
    dialogmessage = window.document.getElementById(dialogmessageid);
    dialogalert = window.document.getElementById(dialogalertid);
}
function EditComment(id, editmode, isempty) {
    CreateDivComment();

    iseditmode = editmode;
    var dialog_editdiv = window.document.getElementById('dialog_edit');
    var dialog_viewdiv = window.document.getElementById('dialog_view');
    var dialogtextcontentdiv = window.document.getElementById('dialogtextcontent');
    var dialogsavebuttondiv = window.document.getElementById('dialogsavebutton');
    if (dialog_editdiv == null || dialog_viewdiv == null || dialogtextcontentdiv == null || dialogsavebuttondiv == null) return false;
    if (editmode) {
        dialog_editdiv.style.display = 'block';
        dialog_viewdiv.style.display = 'none';
        dialogsavebuttondiv.style.visibility = 'visible';
        if (!isempty)
            window.document.getElementById('txcomment').value = lbcaricamento;
        else
            window.document.getElementById('txcomment').value = '';
    }
    else {
        dialog_editdiv.style.display = 'none';
        dialog_viewdiv.style.display = 'block';
        dialogtextcontentdiv.innerHTML = lbcaricamento;
        dialogsavebuttondiv.style.visibility = 'hidden';
    }

    actualId = id;
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    dialog.style.display = 'block';
    dialog.style.zIndex = 1002;
    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";

    $.blockUI({
        message: null,
        bindEvents: false
    });

    LoadingComment(id);
    return false;
}
function CloseComment() {
    if (lockdialog) return false;
    CreateDivComment();
    dialog.style.display = 'none';
    $.unblockUI();
    return false;
}
function CloseMessage() {
    if (lockdialog) return false;
    CreateDivComment();
    dialogmessage.style.display = 'none';
    $.unblockUI();
    return false;
}
function ConterChar(idtxt, idcont, limit) {
    var id1 = window.document.getElementById(idtxt);
    var id2 = window.document.getElementById(idcont);
    if (id1 == null || id2 == null) return;
    var valuelength = limit - id1.value.length;
    if (valuelength < 0) {
        valuelength = 0;
        id1.value = id1.value.substring(0, limit);
    }
    id2.innerHTML = valuelength;
}
function GetSlope(profileId) {
    profileuserId = profileId;
    var result = window.document.getElementById('tableslopes');
    result.innerHTML = lbcaricamento;
    var pl = new SOAPClientParameters();
    pl.add("profileId", profileId);
    SOAPClient.invoke(url, 'GetSlopesForUser', pl, true, GetSlope_callBack);
}
function GetSlope_callBack(r) {
    var result = window.document.getElementById('tableslopes');
    result.innerHTML = '';
    if (r == null || r.length == 0) {
        result.appendChild(document.createTextNode(lbnessunasalita));
        return;
    }
    var table = document.createElement('table');
    result.appendChild(table);
    table.id = 'tableslopes';
    table.border = '1';
    table.setAttribute('cellpadding', 3);
    table.setAttribute('cellspacing', 0);
    table.style.borderStyle = 'solid';
    table.style.borderWidth = '1px';
    table.style.bordercoborderColor = 'black';
    table.style.width = '540px';
    table.style["float"] = 'left';

    // Header
    var trh = document.createElement('tr');
    table.appendChild(trh);
    if (showEdit) {
        var tdh1x = document.createElement('td');
        trh.appendChild(tdh1x);
        var tdh1bx = document.createElement('b');
        tdh1x.appendChild(tdh1bx);
        tdh1bx.appendChild(document.createTextNode(' '));
    }
    var tdh1 = document.createElement('td');
    trh.appendChild(tdh1);
    var tdh1b = document.createElement('b');
    tdh1.appendChild(tdh1b);
    tdh1b.appendChild(document.createTextNode(lbsalita));
    tdh1 = document.createElement('td');
    trh.appendChild(tdh1);
    tdh1b = document.createElement('b');
    tdh1.appendChild(tdh1b);
    tdh1b.appendChild(document.createTextNode(lbda));
    tdh1 = document.createElement('td');
    trh.appendChild(tdh1);
    tdh1b = document.createElement('b');
    tdh1.appendChild(tdh1b);
    tdh1b.appendChild(document.createTextNode(lbmax));
    tdh1 = document.createElement('td');
    trh.appendChild(tdh1);
    tdh1b = document.createElement('b');
    tdh1.appendChild(tdh1b);
    tdh1b.appendChild(document.createTextNode(lbtempo));
    tdh1 = document.createElement('td');
    trh.appendChild(tdh1);
    tdh1b = document.createElement('b');
    tdh1.appendChild(tdh1b);
    tdh1b.appendChild(document.createTextNode(lbcommento));

    for (var i = 0; i < r.length; i++) {
        var tr = document.createElement('tr');
        tr.id = 'tr' + r[i].Id;
        if (r[i].InTwoUser != null && r[i].InTwoUser) {
            tr.style.backgroundColor = '#f2f2f2';
        }
        table.appendChild(tr);
        if (showEdit) {
            var td0 = document.createElement('td');
            var link0 = document.createElement('a');
            link0.setAttribute('href', '#');
            link0.setAttribute('onclick', 'return DeleteSlope("' + r[i].Id + '");');
            link0.appendChild(document.createTextNode("x"))
            td0.appendChild(link0);
            tr.appendChild(td0);
        }
        var td1 = document.createElement('td');
        var link = document.createElement('a');
        link.setAttribute('target', '_blank');
        link.setAttribute('href', '../altimetria/' + r[i].Id + '/' + escape(r[i].Stato) + '/salita_' + escape(r[i].Nome) + '+' + escape(r[i].Da) + '.aspx');
        link.appendChild(document.createTextNode(r[i].Nome))
        td1.appendChild(link);
        tr.appendChild(td1);
        var td2 = document.createElement('td');
        td2.appendChild(document.createTextNode(r[i].Da));
        tr.appendChild(td2);
        var td3 = document.createElement('td');
        td3.appendChild(document.createTextNode(r[i].QuotaF));
        tr.appendChild(td3);
        var td4 = document.createElement('td');
        var valx = r[i].TimeUser;
        var valeedit = r[i].TimeUser;
        if (valx == null) {
            valx = '-';
            valeedit = 'hh.mm.ss'
        }
        else {
            valx = valx.substring(0, 2) + 'h ' + valx.substring(2, 4) + 'm ' + valx.substring(4, 6) + 's';
            valeedit = valeedit.substring(0, 2) + '.' + valeedit.substring(2, 4) + '.' + valeedit.substring(4, 6);
        }
        var spanshow = document.createElement('span');
        spanshow.id = 'spanshow' + r[i].Id;
        spanshow.style.display = 'inline';
        spanshow.appendChild(document.createTextNode(valx));
        td4.appendChild(spanshow);
        if (showEdit) {
            spanshow.appendChild(document.createTextNode(' '));
            var linkedit = document.createElement('a');
            linkedit.setAttribute('href', '#');
            linkedit.setAttribute('onclick', 'return ShowTime("' + r[i].Id + '");');
            linkedit.appendChild(document.createTextNode('e'));
            spanshow.appendChild(linkedit);
            spanshow.appendChild(document.createTextNode(' '));
            td4.appendChild(spanshow);

            var spanedit = document.createElement('span');
            spanedit.id = 'spanedit' + r[i].Id;
            spanedit.style.display = 'none';
            var textedit = document.createElement('input');
            textedit.setAttribute('type', 'text');
            textedit.id = 'textedit' + r[i].Id;
            textedit.setAttribute('value', valeedit);
            textedit.style.width = '60px';
            spanedit.appendChild(textedit);

            var buttonedit = document.createElement('input');
            buttonedit.setAttribute('type', 'button');
            buttonedit.setAttribute('value', ' S ');
            buttonedit.setAttribute('onclick', 'SaveTime("textedit' + r[i].Id + '");');
            buttonedit.style.padding = '1px';
            buttonedit.style.margin = '0px';
            spanedit.appendChild(buttonedit);
            var buttoncancel = document.createElement('input');
            buttoncancel.setAttribute('type', 'button');
            buttoncancel.setAttribute('value', ' C ');
            buttoncancel.setAttribute('onclick', 'ShowTime("' + r[i].Id + '");');
            buttoncancel.style.padding = '1px';
            buttoncancel.style.margin = '0px';
            spanedit.appendChild(buttoncancel);

            td4.appendChild(spanedit);
        }
        tr.appendChild(td4);
        var td5 = document.createElement('td');
        tr.appendChild(td5);
        var spaneditcomment = document.createElement('span');
        spaneditcomment.id = 'spaneditcomment' + r[i].Id;
        td5.appendChild(spaneditcomment);
        CreateCellComment(r[i].Id, r[i].HaveComment, showEdit);
        if (showEdit) {
            var linkeditcomment = document.createElement('a');
            linkeditcomment.setAttribute('href', '#');
            linkeditcomment.setAttribute('onclick', 'return EditComment('+r[i].Id+',true);');
            linkeditcomment.appendChild(document.createTextNode(lbedit));
            td5.appendChild(linkeditcomment);
        }
    }
}

function CreateCellComment(id, havecomment, showEdit) {
    var spaneditcomment = window.document.getElementById('spaneditcomment' + id);
    if (spaneditcomment == null) return;
    spaneditcomment.innerHTML = '';
    if (havecomment) {
        var linkshowcomment = document.createElement('a');
        linkshowcomment.setAttribute('href', '#');
        linkshowcomment.setAttribute('onclick', 'return EditComment(' + id + ',false);');
        linkshowcomment.appendChild(document.createTextNode(lbshow));
        spaneditcomment.appendChild(linkshowcomment);
        if (showEdit) spaneditcomment.appendChild(document.createTextNode(' | '));
    }
    else {
        if (!showEdit) spaneditcomment.appendChild(document.createTextNode(' - '));
    }
}

var lockedsave = false;
var idedittextbox = null;
var idx = null;
function SaveTime(id) {
    if (lockedsave) return;
    lockedsave = true;
    idx = id.substring(8);
    var valueid = window.document.getElementById(id);
    if (valueid == null) return;
    idedittextbox = valueid;
    var value = valueid.value;
    while (value.indexOf(" ") != -1) value = value.replace(" ", "");
    if (value != '') {
        if (window.RegExp) {
            var reg2str = "^[0-9]{1,2}\\.\\d\\d\\.\\d\\d$";
            var reg2 = new RegExp(reg2str);
            if (reg2.test(value) == false) {
                valueid.style.color = '#f00';
                lockedsave = false;
                return;
            }
        }
    }
    var pl = new SOAPClientParameters();
    pl.add("idsalita", idx);
    pl.add("time", value);
    SOAPClient.invoke(url, 'AddTime', pl, true, SaveTime_callBack);
}
function SaveTime_callBack(r) {
    lockedsave = false;
    if (r == false) {
        idedittextbox.style.color = '#f00';
    }
    else {
        idedittextbox.style.color = '#000';
        var time = idedittextbox.value.replace(".", "").replace(".", "");
        if (time != '' && time.length < 6) time = '0' + time;
        var spantx = window.document.getElementById('spanshow' + idx);
        if (spantx != null) {
            var buf = spantx.innerHTML;
            var f = buf.indexOf("s");
            if (f == -1) f = buf.indexOf("-");
            if (time == '') {
                spantx.innerHTML = '- ' + spantx.innerHTML.substring(f + 1);
            }
            else {
                spantx.innerHTML = time.substring(0, 2) + 'h ' + time.substring(2, 4) + 'm ' + time.substring(4.6) + 's' +
                        spantx.innerHTML.substring(f + 1);
            }
        }
        ShowTime(idx);
    }
}
function ShowTime(id) {
    var elemshow = window.document.getElementById('spanshow' + id);
    var elemhide = window.document.getElementById('spanedit' + id);
    var elemtxt = window.document.getElementById('textedit' + id);

    if (elemtxt != null) elemtxt.style.color = '#000';
    if (elemshow != null && elemhide != null) {
        if (elemshow.style.display == 'none') {
            elemshow.style.display = 'inline';
            elemhide.style.display = 'none';
        }
        else {
            elemshow.style.display = 'none';
            elemhide.style.display = 'inline';
        }
    }
    return false;
}
function DeleteSlope(id) {
    if (locked) return false;
    actualId = id;
    $.blockUI({
        message: null,
        bindEvents: false
    });
    CreateDivComment();

    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    dialogalert.style.display = 'block';
    dialogalert.style.zIndex = 1002;
    var dialogwidth = dialogalert.offsetWidth;
    var dialogheight = dialogalert.offsetHeight;
    var topposition = top + (height / 2) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialogalert.style.top = topposition + "px";
    dialogalert.style.left = leftposition + "px";
    return false;
}
function CloseDeleteSlope() {
    $.unblockUI();
    CreateDivComment();
    dialogalert.style.display = 'none';
    locked = false;
    return false;
}
function DeleteRealSlope() {
    if (locked) return false;
    locked = true;
    var pl = new SOAPClientParameters();
    pl.add('id', actualId);
    SOAPClient.invoke(url, 'DeleteSlope', pl, true, DeleteSlope_callBack);
    return false;
}
function DeleteSlope_callBack(r) {
    var tr = window.document.getElementById('tr' + actualId);
    tr.parentNode.removeChild(tr);
    return CloseDeleteSlope();
}
var txteditorid;
var lockdialog=false;
function SaveComment(tx) {
    if (lockdialog) return false;
    txteditorid = tx;
    var txteditor=window.document.getElementById(tx);
    if (txteditor==null) return false;
    lockdialog=true;
    txteditor.disabled=true;
    var pl = new SOAPClientParameters();
    pl.add('idsalita', actualId);
    pl.add('comment', txteditor.value);
    SOAPClient.invoke(url, 'AddComment', pl, true, SaveComment_callBack);
    return false;
}
function SaveComment_callBack(r) {
    var txteditor = window.document.getElementById(txteditorid);
    if (txteditor == null) return false;
    txteditor.disabled = false;
    lockdialog = false;
    if (r == false) {
        alert("Error!!!");
    }
    else {
        var HaveComment = false;
        if (txteditor.value.length > 0)
            HaveComment = true;
        CreateCellComment(actualId, HaveComment, true);
        CloseComment();
    }
    return false;
}
function LoadingComment(id) {
    lockdialog = true;
    lockedsave = true;
    actualId = id;
    var pl = new SOAPClientParameters();
    pl.add('profileid', profileuserId);
    pl.add('idsalita', actualId);
    SOAPClient.invoke(url, 'GetComment', pl, true, LoadingComment_callBack);
    return false;
}
function LoadingComment_callBack(r) {
    if (iseditmode) {
        if (r == null)
            window.document.getElementById('txcomment').value = '';
        else
            window.document.getElementById('txcomment').value = r.toString();
        ConterChar('txcomment', 'lblchar', 2000);
    }
    else {
        if (r == null)
            window.document.getElementById('txcomment').value = '';
        else {
            while (r.indexOf('\n') != -1) r = r.replace('\n', '<br />');

            var strtotal = '';
            var counter;
            var islink = false;
            var textlink = '';
            for (counter = 0; counter < r.length; counter++) {
                var substr = r[counter];
                if (islink == true) {
                    if (substr == '*') {
                        islink = false;
                        if (textlink.length>0)
                        {
                            while (textlink[0] == ' ') textlink = textlink.substring(1);
                            while (textlink[textlink.length - 1] == ' ') textlink = textlink.substring(0, textlink.length - 2);
                            if (textlink.length<5 || textlink.toLowerCase().substring(0,4)!='http') textlink='http://'+textlink;
                            strtotal += '<a href="' + textlink + '" title="' + textlink + '" target="_blank">[link]</a>';
                        }
                        textlink = '';
                    }
                    else {
                        textlink += substr;
                    }
                }
                else {
                    if (substr == '*') {
                       islink = true;
                    }
                    else {
                        strtotal += substr;
                    }
                }
            }
            if (textlink.length > 0) strtotal += '*' + textlink;
            window.document.getElementById('dialogtextcontent').innerHTML = strtotal;
        }
    }
    lockdialog = false;
    lockedsave = false;
    return false;
}

function SendMessage() {
    $.blockUI({
        message: null,
        bindEvents: false
    });
    CreateDivComment();

    var dialog_editdiv = window.document.getElementById('messageinputtext');
    var dialog_viewdiv = window.document.getElementById('messageshowtext');
    var dialog_messagesavebutton = window.document.getElementById('messagesavebutton');
    dialog_editdiv.style.display = 'block';
    dialog_viewdiv.style.display = 'none';
    dialog_messagesavebutton.style.display = 'inline';

    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    window.document.getElementById('txmessage').value = '';
    dialogmessage.style.display = 'block';
    dialogmessage.style.zIndex = 1002;
    var dialogwidth = dialogmessage.offsetWidth;
    var dialogheight = dialogmessage.offsetHeight;
    var topposition = top + (height / 2) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialogmessage.style.top = topposition + "px";
    dialogmessage.style.left = leftposition + "px";

    return false;
}

function Sendmessage(txmessage) {
    var content = window.document.getElementById(txmessage).value;
    if (content.length == 0) return;
    lockdialog = true;
    var dialogtextmessage_text = window.document.getElementById('dialogtextmessage');
    dialogtextmessage_text.innerHTML = lbProfile_MessageSending;
    var dialog_editdiv = window.document.getElementById('messageinputtext');
    var dialog_viewdiv = window.document.getElementById('messageshowtext');
    var dialog_messagesavebutton = window.document.getElementById('messagesavebutton');
    dialog_editdiv.style.display = 'none';
    dialog_viewdiv.style.display = 'block';
    dialog_messagesavebutton.style.display = 'none';

    var pl = new SOAPClientParameters();
    pl.add('message', content);
    SOAPClient.invoke(url, 'SemdMessage', pl, true, Sendmessage_callBack);
    return false;
}
function Sendmessage_callBack(r) {
    lockdialog = false;
    var dialogtextmessage_text = window.document.getElementById('dialogtextmessage');
    dialogtextmessage_text.innerHTML = lbProfile_MessageSent;
}

function ListSlope(quale) {
    var finalurl = '../motore2.aspx?pagina=1&lista=true&da=url' + lbdaurl + '&lingua='+lblang +'&quanti=30&url=' + escape(quale);
    window.document.location.href = finalurl;
    return false;
}

function SearchUserByButton() {
    $('#txusersearch').unautocomplete();
    $("#txusersearch").autocomplete("AutoSearchUser.ashx", { matchCase: false, cacheLength: 0, selectFirst: false });
    SearchUserByEmail();
}

function SearchUserByEmail() {
    $("#menulist").html('<li>Loading...</li>');
    var filter = $("#txusersearch").val();
    var pl = new SOAPClientParameters();
    pl.add('filter', filter);
    SOAPClient.invoke(url, 'SearchUserByEmail', pl, true, SearchUserByEmail_callBack);
    return false;
}
function SearchUserByEmail_callBack(r) {
    if (r == null) {
        $("#menulist").html('');
    }
    else {
        if (r.length == 1) {
            window.location.href = 'Profile.aspx?ProfileId=' + r[0].ActiveKey + '&language=' + lblang;
        }
        else {
            $("#menulist").html('');
            var i = 0;
            for (i = 0; i < r.length; i++) {
                var li = $("<li><a></a></li>");

                $("#menulist").append(li);

                $("a", li).text(r[i].UserName);
                $("a", li).attr("href", "Profile.aspx?ProfileId=" + r[i].ActiveKey);
            }
        }
    }
}
function SearchSlope() {
    var text = $("#filtra").val();
    var finalurl = '../motore2.aspx?pagina=1&lista=false&da=url' + lbdaurl + '&lingua=' + lblang + '&quanti=30&filtra=' + escape(text);
    window.document.location.href = finalurl;
}
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}

