$(document).ready(function () {
    setNavigation();
    //setDateRange();
});
function setNavigation() {
    var path = window.location.href;
    path = path.replace(/\/$/, "");
    path = decodeURIComponent(path);

    $("#sidebar a").each(function () {

        var href = $(this).attr('href');

        if (path == href) {

            $(this).addClass('active');
            $(this).closest('ul').parent("li").children('a').addClass('subOpened');
            $(this).closest('ul').show();
            $(this).closest('ul').children('a').removeClass('subClosed');
            $(this).closest('li').addClass('active');
            $(this).attr('id', 'current');
            $(this).closest('li').children('a').attr('id', 'current');
            $(this).closest('li').children('a').attr('id', 'current');
            $(this).closest('li').children('a').addClass('subOpened');
            $(this).closest('li').children('a').addClass('active')
            return;
        }
    });
}

function formSubmit(formid, rid, target) {
    $(".overflow").show();
    $("#" + formid).validate();
    if ($("#" + formid).valid()) {
        var oOutput = document.getElementById(rid);
        oData = new FormData(document.forms.namedItem(formid));
        
        var resultid = rid;
        var oReq = new XMLHttpRequest();
        oReq.open("POST", target, true);
        oReq.onload = function (oEvent) {
            if (oReq.status == 200) {
                var result = oReq.responseText;
                if (result.indexOf("Success :") != -1) {
                    oOutput.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>' + result + '</div>';
                    if (result.indexOf("update") < 0) {
                        $("#" + formid + " input[type=text]").val("");
                        $("#" + formid + " textarea").val("");
                        $("#" + formid + " input[type=email]").val("");
                        $("#" + formid + " input[type=password]").val("");
                        $('#' + formid).each(function () {
                            this.reset();
                        });
                    }
                    setTimeout("$('#" + rid + "')html('')", 3000);
                }
                else if (result.indexOf("Reload :") != -1) {
                    oOutput.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>' + result.replace('Reload :', 'Success :') + '</div>';
                    setTimeout('window.location.reload()', 2000);
                }
                else if (result.indexOf("Redirect :") != -1) {
                    var rs = result.replace('Redirect :', 'Success :');
                    var res = rs.split('URL')
                    var url = res[1];
                    oOutput.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>' + res[0] + '</div>';
                    window.location.href = url;
                }
                else if (result.indexOf("Print :") != -1) {
                    var rs = result.replace('Print :', 'Success :');
                    var res = rs.split('URL')
                    var url = res[1];
                    oOutput.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>' + res[0] + '</div>';
                    window.open(url);
                    window.location.reload();
                }
                else {
                    oOutput.innerHTML = '<div class="alert alert-danger"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>' + result + '</div>';
                }
                $(".overflow").hide();
            }
            else {
                oOutput.innerHTML = '<div class="alert alert-danger"><button class="close" data-dismiss="alert" type="button"><span aria-hidden="true">×</span></button>Error ' + oReq.status + ' occurred uploading your file.</div>';
                $(".overflow").hide();
            }
        };
        oReq.send(oData);

    }
    else {
        $(".overflow").hide();
    }

}

function deleteRow(ths) {
    $(".overflow").show();
    $(ths).closest("tr").remove();
    $(".overflow").hide();
}

function unsetSearchDataTable(tableId, columnID) {
    $.each(columnID, function (key, value) {
        var txt = $('#' + tableId + ' thead th:nth-child(' + value + ') input[type="text"] ').attr('placeholder');
        $('#' + tableId + ' thead th:nth-child(' + value + ')').html(txt);
        //alert(txt);
    });

}
function setDateTableManual(tableId) {
    // Setup - add a text input to each footer cell
    $('#' + tableId + ' thead th').each(function () {
        var title = $('#' + tableId + ' thead th').eq($(this).index()).text();
        $(this).html('<input type="text" placeholder="' + title + '" />');
    });
    //setup - dataTable
    var table = $('#' + tableId).DataTable({
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": '../js/swf/copy_csv_xls_pdf.swf'
        },
        'iDisplayLength': 12,
        deferRender: true
    });
    // Apply the search
    table.columns().eq(0).each(function (colIdx) {
        $('input', table.column(colIdx).header()).on('keyup change', function () {
            table
                    .column(colIdx)
                    .search(this.value)
                    .draw();
        });
    });
}
function setDateTableAjax(tableId, pageUrl) {
    // Setup - add a text input to each footer cell
    $('#' + tableId + ' thead th').each(function () {
        var title = $('#' + tableId + ' thead th').eq($(this).index()).text();
        $(this).html('<input type="text" placeholder="' + title + '" />');
    });
    //setup - dataTable
    var table = $('#' + tableId).DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": pageUrl,
        "dom": 'T<"clear">lfrtip',
        
        "tableTools": {
            "sSwfPath": '../js/swf/copy_csv_xls_pdf.swf'
        },
        'iDisplayLength': 12,
        "fnDrawCallback": function () {
            /*ready();*/
        }

    });
    // Apply the search
    table.columns().eq(0).each(function (colIdx) {
        $('input', table.column(colIdx).header()).on('keyup change', function () {
            table
                    .column(colIdx)
                    .search(this.value)
                    .draw();
        });
    });
}

function setDateTableAjax1(tableId, pageUrl) {
    // Setup - add a text input to each footer cell
    $('#' + tableId + ' thead th').each(function () {
        var title = $('#' + tableId + ' thead th').eq($(this).index()).text();
        $(this).html('<input type="text" placeholder="' + title + '" />');
    });
    //setup - dataTable
    var table = $('#' + tableId).DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": pageUrl,
        "dom": 'T<"clear">lfrtip',
        "order": [[0, 'desc']],
        "tableTools": {
            "sSwfPath": '../js/swf/copy_csv_xls_pdf.swf'
        },
        'iDisplayLength': 12,
        "fnDrawCallback": function () {
            /*ready();*/
        }

    });
    // Apply the search
    table.columns().eq(0).each(function (colIdx) {
        $('input', table.column(colIdx).header()).on('keyup change', function () {
            table
                    .column(colIdx)
                    .search(this.value)
                    .draw();
        });
    });
}

function confirmBox() {
    var conf = confirm("Sure You Want To Do This.");
    if (conf) {
        return true;
    } else {
        return false;
    }
}
function getAddress(address) {
    var result;
    $.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address, function (data) {
        result = data.results[0].geometry.location.lat + "|" + data.results[0].geometry.location.lng;
        alert(result);
        return result;
    });
}