function ForNumerOnly(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
$('.PageAlias').bind('keyup blur', function () {
    $(this).val(function (i, val) {
        return val.toLowerCase().replace(/ /g, '-').replace(/--/g, '-').replace(/[^a-z0-9-]+/g, '');
    });
});

$('.PageAliasTitle').bind('keyup blur', function () {

    var value = $(this).val();
    value = value.toLowerCase().replace(/[^[a-z][0-9][०-९][\u0900-\u097F]\s]/gi, '').replace(/[_\s]/g, '-').replace(/[&*+\\/;"'?/~`#%,]/g, '');
    $("#PageAlias").val(value);
});
function Alias() {
    $('.PageAlias').bind('keyup blur', function () {
        $(this).val(function (i, val) {
            return val.toLowerCase().replace(/ /g, '-').replace(/--/g, '-').replace(/[^a-z0-9-]+/g, '');
        });
    });

    $('.PageAliasTitle').bind('keyup blur', function () {

        var value = $(this).val();
        value = value.toLowerCase().replace(/[^[a-z][0-9][०-९][\u0900-\u097F]\s]/gi, '').replace(/[_\s]/g, '-').replace(/[&*+\\/;"'?/~`#%,]/g, '');
        $("#PageAlias").val(value);
    });
}

$('.textboxrestriction').bind('keyup blur', function () {
    $(this).val(function (i, val) {
        return val.replace(/[^a-zA-Z0-9- ()=+*#$]+/, '');
    });
});

$(".JQsearch").keyup(function () {

    var search = "";
    var result = true;
    var tr = $("#datatable").find("tr");
    for (i = 0; i < tr.length; i++) {
        result = true;
        $(".JQsearch").each(function () {
            
            var filter = $(this).val();
            var index = $(this).attr("data-index");
            if (filter != undefined && filter.length > 0 && $(tr[i]).find("td")[index] != undefined) {
                var tdval = $(tr[i]).find("td")[index].innerHTML.toUpperCase().indexOf(filter.toUpperCase());
                //if (tdval != undefined && tdval.length > 0) {
                if (result == true) {
                    result = (tdval > -1)
                }
                //}
                //
                //if (tdval != undefined && tdval.length > 0) {
                //    search += "(" + tdval + "> -1 ) && ";
                //}
            }
        });
        //if (search.length > 0) {
        //    search = search.substring(0, search.length - 3);
        //}
        if (result==true) {
            tr[i].style.display = "";
        }
        else {
            tr[i].style.display = "none";
        }
    }

});

function mySearchFunction(id, postion) {
    // Declare variables 
    var input, filter, table, tr, td, i;
    input = document.getElementById(id);
    filter = input.value.toUpperCase();
    tr = $("#datatable").find("tr");

    // Loop through all table rows, and hide those who don't match the search query
    for (i = 0; i < tr.length; i++) {
        td = $(tr[i]).find("td")[postion]
        if (td) {
            if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}

function mySearchDropdownFunction(id, postion) {
    // Declare variables 
    var input, filter, table, tr, td, i;
    input = document.getElementById(id);
    filter = input.value.toUpperCase();
    filter = (filter == "ALL") ? "" : filter;

    tr = $("#datatable").find("tr");
    

    // Loop through all table rows, and hide those who don't match the search query
    for (i = 1; i < tr.length; i++) {
        td = $(tr[i]).find("td")[postion];
        var value = "";
        if (td != undefined)
        {
            value = $(td).find(".hdnCType").val();
        }
        if (filter == "Participated".toUpperCase()) {
            value = (value == "Approve" || value == "Reject" || value == "Selected") ? "Participated" : value;
        }
        value = value.toUpperCase();
        if (filter != "") {
            value = (value == "") ? "Select" : value;
        }
        if (value!=undefined) {
            if (value.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}

function mergeQueryString(newquerystring) {
    var NQSArray = newquerystring.split('&');
    var url = window.location.href;
    for (i = 0; i < NQSArray.length; i++) {
        var QSArray = NQSArray[i].split('=');
        url = mergeUrlParameter(url, QSArray[0], QSArray[1]);
    }
    return url;
}
function mergeUrlParameter(mainurl, parameter, paravalue) {
    var url = "";

    var vars = [], hash;
    var len = mainurl.indexOf('?');
    var hashes = mainurl;
    var query = "";
    var newQS = "";
    if (len > 1) {
        //len = len - 1;
        hashes = hashes.substring(0, len);
        query = mainurl.substring(len + 1, mainurl.length);


        var hashesparameter = query.split('&');
        var update = 0;
        for (var j = 0; j < hashesparameter.length; j++) {
            hash = hashesparameter[j].split('=');
            if (hash[0] == parameter) {
                update = 1;
                //hash[1] = paravalue;
                newQS += "&" + parameter + "=" + paravalue;
            }
            else {
                newQS += "&" + hash[0] + "=" + hash[1];
            }

        }
        if (update == 0) {
            url = hashes + "?" + newQS.substring(1, newQS.length) + "&" + parameter + "=" + paravalue;
        }
        else {
            url = hashes + "?" + newQS.substring(1, newQS.length);
        }
    }
    else {
        url = mainurl + "?" + parameter + "=" + paravalue;
    }

    return url;

}
//$(document).ready(function () {
//    if ($("#MessageType").val() == "success") {
//        $("#btninfo2").click();
//    }
//    else if ($("#MessageType").val() == "error") {
//        $("#btninfo1").click();
//    }
//});
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function updateQueryStringParameter(uri, key, value) {
    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    }
    else {
        return uri + separator + key + "=" + value;
    }
}

function removeURLParameter(url, parameter) {
    //prefer to use l.search if you have a location/link object
    var urlparts = url.split('?');
    if (urlparts.length >= 2) {

        var prefix = encodeURIComponent(parameter) + '=';
        var pars = urlparts[1].split(/[&;]/g);

        //reverse iteration as may be destructive
        for (var i = pars.length; i-- > 0;) {
            //idiom for string.startsWith
            if (pars[i].lastIndexOf(prefix, 0) !== -1) {
                pars.splice(i, 1);
            }
        }

        url = urlparts[0] + '?' + pars.join('&');
        return url;
    } else {
        return url;
    }
}
function ValidateCheckbox()
{
    // extend jquery range validator to work for required checkboxes
    var defaultRangeValidator = $.validator.methods.range;
    $.validator.methods.range = function (value, element, param) {
        if (element.type === 'checkbox') {
            // if it's a checkbox return true if it is checked
            return element.checked;
        } else {
            // otherwise run the default validation function
            return defaultRangeValidator.call(this, value, element, param);
        }
    }
}

$(".btnsubmit").click(function () {
    $("[data-valmsg-summary=true]").find("ul").find("li").remove();
    $("[data-valmsg-summary=true]").addClass("alert alert-danger").addClass("in").removeClass("alert-success");
});
$("button.close").click(function () {
    $("[data-valmsg-summary=true]").find("ul").find("li").remove();
    $("[data-valmsg-summary=true]").removeClass("alert");
});
