
//band event
$("input").click(function () {
    $.ajaxSetup({
        cache: false
    });
    var tbody = "";
    $("table").find("tbody").remove();
    $.ajax({
        //post type
        type: "GET",
        //json location
        url: "info.json",
        //return type
        dataType: "json",
        //process the function
        success: function (data) {

            for (var n = 0; n < data.length; n++) {
                var table = document.getElementById("tableView");

                var num = n + 1;
                if (data[n].type == "99") {
                    //add Content
                    var id = "text" + num;
                    tbody = document.createElement("tbody");
                    tbody.innerHTML = "<tr>" + "<th scope='row'>" + num + "</th>" + "<td id='" + id + "'>" + data[n].content + "</td>" + "</tr>";
                    table.appendChild(tbody);
                }
                else if (data[n].type == "98") {
                    //add Content
                    var id = "text" + num;
                    tbody = document.createElement("tbody");
                    tbody.innerHTML = "<tr>" + "<th scope='row'>" + num + "</th>" + "<td id='" + id + "'>" + "<pre>" + "<xmp>" + data[n].content + "</xmp>" + "</pre>" + "</td>" + "</tr>";
                    table.appendChild(tbody);
                }
                else if (data[n].type == "88") {
                    //add Content
                    var id = "image" + num;
                    var imagePath = data[n].content.trim().replace(/\s/g,"");
                    tbody = document.createElement("tbody");
                    tbody.innerHTML = "<tr>" + "<th scope='row'>" + num + "</th>" + "<td id='" + id + "'>" + "<div >" + "<img src= /" + imagePath + ">" + "</div>" + "</td>" + "</tr>";
                    table.appendChild(tbody);
                }
            }
        }
    });
});


//jquery function  each loop

$("ul li").each(function () {
    $(this).click(function () {
        alert($(this).text());
    })
})


$('li').click(function () {
    alert($(this).text());
})

$("table").on("click", "td", function () {
    //alert($(this).text());

    var val = $(this).attr("id");
    var u = navigator.userAgent;
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios
    //if is iOS device
    if (isIOS) {
        //var content = document.getElementById(val);
        //var text = content.textContent || content.innerText;
        //var input = document.getElementById("testCopy");
        //input.value = text;
        //input.focus();
        //input.selectionStart = 0;
        //input.selectionEnd = input.value.length;
        var content = document.getElementById(val);
        var text = content.textContent || content.innerText;

        var btn = document.getElementById("myBtn");
        var textContent = document.getElementById("textContent");
        if (text != "") {
            textContent.value = text;
            btn.onclick();
        }
        $('#myModal').on('change keyup keydown paste cut', 'textarea', function () {
            $(this).height(0).height(this.scrollHeight);
        }).find('textarea').change();

    }
    else {
        //var text = $(this).text();
        //var input = document.getElementById("testCopy");
        //input.value = text;
        //input.select();
        //document.execCommand("copy");
        var content = document.getElementById(val);
        var text = content.textContent || content.innerText;

        var btn = document.getElementById("myBtn");
        var textContent = document.getElementById("textContent");
        if (text != "") {
            textContent.value = text;
            btn.onclick();
        }

        $('#myModal').on('change keyup keydown paste cut', 'textarea', function () {
            $(this).height(0).height(this.scrollHeight);
        }).find('textarea').change();

        //$('document').ready(function () {
        //    $("#myModal").modal('show');
        //});
    }
})

function myCopy() {

    if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
        //for iOS
        var $input = $('#textContent');
        $input.val();
        var el = $input.get(0);
        var editable = el.contentEditable;
        var readOnly = el.readOnly;
        el.contentEditable = true;
        el.readOnly = false;
        var range = document.createRange();
        range.selectNodeContents(el);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        el.setSelectionRange(0, 999999);
        el.contentEditable = editable;
        el.readOnly = readOnly;

        var successful = document.execCommand('copy');
        $input.blur();
        alert("Text copied: " + $input.val());
        myClose();

    }
    else {
        // for Andorid

        /* Get the text field */
        var copyText = document.getElementById("textContent");

        /* Select the text field */
        copyText.select();

        /* Copy the text inside the text field */
        document.execCommand("copy");

        /* Alert the copied text */
        alert("Text copied: " + copyText.value);
        myClose();
    }
}

function myClose() {
    modal.style.display = "none";
}