﻿//----------------------------------------------------------------------------------------------------
//Error 
function showError(divID, message) {
    $("#" + divID).show('slow');
    $("#" + divID).empty();
    $("#" + divID).removeClass('information').removeClass('success').removeClass('process');
    $("#" + divID).addClass('error');
    $("#" + divID).html(message);
}
//Success
function showSuccess(divID, message) {
    $("#" + divID).show('slow');
    $("#" + divID).empty();
    $("#" + divID).removeClass('information').removeClass('error').removeClass('process');
    $("#" + divID).addClass('success');
    $("#" + divID).html(message);
}
//Processing
function showProcess(divID, message) {
    $("#" + divID).show('slow');
    $("#" + divID).empty();
    $("#" + divID).removeClass('information').removeClass('success').removeClass('error');
    $("#" + divID).addClass('process');
    $("#" + divID).html(message);
}

//hide error div
function hideErrorDiv(divID) {
    $("#" + divID).hide('slow');
    $("#" + divID).empty();
    $("#" + divID).removeClass('information').removeClass('success').removeClass('process');
    $("#" + divID).addClass('error');

}
//Error in input field
function showErrorInput(inputID) {
    $("#" + inputID).addClass('error');
    $("#" + inputID).focus();
}
function removeErrorInput(inputID) {
    $("#" + inputID).removeClass('error');
    $("#" + inputID).hide('slow');
}

function clearHtmlContent(controlid) {    
    $('#' + controlid ).html(' ');
}

//----------------------------------------------------------------------------------------------------

function IsImageOk(img) {
    // During the onload event, IE correctly identifies
    //any images that
    // weren’t downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties:
    //naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it’s ok.
    return true;
}

//Call this function onLoad of body tag
function checkImages() {
    for (var i = 0; i < document.images.length; i++) {
        if (!IsImageOk(document.images[i])) {

            document.images[i].src = "/images/loaderror.jpg";
        }
    }
}

//Dynamically remove an external JavaScript or CSS file
function removejscssfile(filename, filetype) {
    var targetelement = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none" //determine element type to create nodelist from
    var targetattr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none" //determine corresponding attribute to test for
    var allsuspects = document.getElementsByTagName(targetelement)
    for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
        if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
            allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
    }
}

//Dynamically loading an external JavaScript or CSS file
function loadjscssfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}
//----------------------------------------------------------------------------------------------------
//Page.ResolveUrl Treatment in Javascript
function ResolveUrl(url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
}

//-----------Textbox Placeholder ------------------------------------------
function placeholder() {
    $("input[type=text]").each(function () {
        var phvalue = $(this).attr("placeholder");
        $(this).val(phvalue);
    });
}
placeholder();
$("input[type=text]").focusin(function () {
    var phvalue = $(this).attr("placeholder");
    if (phvalue == $(this).val()) {
        $(this).val("");
    }
});
$("input[type=text]").focusout(function () {
    var phvalue = $(this).attr("placeholder");
    if ($(this).val() == "") {
        $(this).val(phvalue);
    }
});


//-----------Textbox Placeholder ------------------------------------------

function stringIsBlank(val) {
    if (val == null || val == undefined)
        return false;

    if ($.trim(val).length > 0)
        return false;
    else
        return true;
}

function PageAftrDelay(dTime, url) {
    window.setTimeout(function(){
        window.location = url;
    }, dTime);
}

function JsonDdlBind(obj) {
    ddlObj = $("[id$='" + obj.DdlId + "']")
    ddlObj.empty();
    ddlObj.append($("<option></option>").val('0').html(globalSelect));
    $.each(obj.JsonObject, function (index, domEle) {
        ddlObj.append($("<option></option>").val(this[obj.DdlValue]).html(this[obj.DdlText]));
    });
}

function getDDLText(control) {
    return $('[id$="' + control.attr('id') + '"] option:selected').text();
}

function getRDOValue(control){
    return $('[id$="' + control.attr('id') + '"] input:radio:checked').val();
}

function getCurrentDate() {
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    return day + "/" + month + "/" + year;
}

//-----------Convert To Title Case ------------------------------------------
function toTitleCase(str) {
    return str.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
}
//-----------Convert To Title Case ------------------------------------------

function scrollSetup(id) {
    $('#' + id).jScrollPane({ showArrows: true, scrollbarWidth: 13, verticalGutter: 10, arrowSize: 16 });
    return $('#' + id).data('jsp');
}

//----------- AJAX Call ------------------------------------------
function callAjax(obj) {

    switch (obj.Type.toUpperCase()) {
        case "JSON":
            $.ajax({
                type: "POST",
                url: obj.AjaxUrl,
                data: JSON.stringify(obj.DataObject),
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                async: true,
                success: function (data) {
                    if (obj.ReturnJson)
                        var returnObj = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
                    else
                        var returnObj = data.d;

                    obj.CallBack(returnObj);
                },
                error: function (result) {
                    
                }
            });
            break;
    }
}
//----------- AJAX Call ------------------------------------------

/*-------------------------------Pagination Section---------------------------------------*/
function getListings(sp) {

    $.ajax({
        type: 'GET',
        url: sp.AjaxCall,
        data: sp.CallData,
        dataType: 'html',
        success: function (data) {
            if (data.length === 0) {
                $('#NoRecordFound').show();

                $('#' + sp.RenderInTable).html(' ');
            }
            else {
                $('#NoRecordFound').hide();
                $('#' + sp.RenderInControl).html(' ').append(data);

            }
            if (sp.CallBack != undefined || sp.CallBack != null) {
                sp.CallBack();
            }
        }
    });
}

function InitializePagination() {
    optInit = getOptionsFromForm();
    if (Number(ITEMPERPAGE) < Number(totalDataCount)) {
        $("#Pagination").pagination(totalDataCount, optInit);
    } else {
        $("#Pagination").hide();
        ListCallBack(0);
    }
}

function getOptionsFromForm() {
    var opt = { callback: pageselectCallback };

    opt["items_per_page"] = ITEMPERPAGE;
    opt["num_display_entries"] = ITEMPERPAGE;

    return opt;
}

function pageselectCallback(page_index, jq) {
    var items_per_page = ITEMPERPAGE;
    var max_elem = Math.min((page_index + 1) * items_per_page, totalDataCount);
    var page_number = Math.min((page_index) * items_per_page, totalDataCount);
    ListCallBack(page_number);
    return false;
}

/*-------------------------------Pagination Section---------------------------------------*/

