﻿
function resizeProfileImageCore(objid, maxWidth, maxHeight)
{
    var obj = document.getElementById(objid);
    var oldResize = obj.onresize;
    obj.onresize = null;
    if (obj != null)
    {
        try
        {
            var ratiomax = maxHeight / maxWidth;
            var ratioobj = obj.height / obj.width;

            if (ratiomax > ratioobj)
            { // too wide
                obj.width = maxWidth;
            }
            else
            { // too tall
                obj.height = maxHeight;
            }
        }
        catch (e)
        {
        }
    }
    obj.onresize = oldResize;
}

function resizeProfileImage(objid)
{
    resizeProfileImageCore(objid, 75, 75);
}

// This will add selected item to the destination listbox and removes from source listbox
function AddItemToFilterDestination(sourceID, destinationID, hdnSelectedFiltersId)
{
    //    debugger;
    try
    {
        var listBoxSource = document.getElementById(sourceID);
        var selectedFilters = "";

        if (listBoxSource.selectedIndex >= 0)
        {
            var listBoxDestination = document.getElementById(destinationID);
            var count = listBoxSource.length;

            for (var i = 0; i < count; i++)
            {
                var item = listBoxSource.options[i];

                if (item.selected)
                {
                    item.selected = false;
                    listBoxSource.remove(i);
                    count--;
                    i--;

                    listBoxDestination.add(item);
                }
            }
        }
        else
        {
            alert("Please select an Available Filter to add.");
        }

        for (var j = 0; j < listBoxDestination.length; j++)
        {
            if (selectedFilters == "")
                selectedFilters = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
            else
                selectedFilters = selectedFilters + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
        }

        var hdnSelectedFilters = document.getElementById(hdnSelectedFiltersId);
        if (hdnSelectedFilters != null)
            hdnSelectedFilters.value = selectedFilters;
    }
    catch (err)
    {

    }

    return false;
}

// This will remove from destination ListBox and Add to Source List Box
function RemoveItemFromFilterDestination(sourceID, destinationID, hdnSelectedFiltersId)
{
    //    debugger;
    try
    {
        var listBoxDestination = document.getElementById(destinationID);
        var selectedFilters = "";

        if (listBoxDestination.selectedIndex >= 0)
        {
            var listBoxSource = document.getElementById(sourceID);
            var count = listBoxDestination.length;
            var isFixedFilterType = false;

            for (var i = 0; i < count; i++)
            {
                var item = listBoxDestination.options[i];
                var lowerValue = item.value.toLowerCase();
                var lowerText = item.text.toLowerCase();

                if (item.selected && !(lowerValue == 'firstname' || lowerValue == 'lastname' || lowerText == 'first name' || lowerText == 'lase name'))
                {
                    listBoxDestination.remove(i);
                    count--;
                    i--;
                    item.selected = false;
                    listBoxSource.add(item);
                }

                if (item.selected && (lowerValue == 'firstname' || lowerValue == 'lastname' || lowerText == 'first name' || lowerText == 'lase name'))
                {
                    alert("Cannot remove the fixed filter type.");
                    break;
                }
            }
        }
        else
        {
            alert("Please select a Selected Filter to remove.");
        }

        for (var j = 0; j < listBoxDestination.length; j++)
        {
            if (selectedFilters == "")
                selectedFilters = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
            else
                selectedFilters = selectedFilters + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
        }

        var hdnSelectedFilters = document.getElementById(hdnSelectedFiltersId);
        if (hdnSelectedFilters != null)
            hdnSelectedFilters.value = selectedFilters;
    }
    catch (err)
    {

    }

    return false;
}

// This will add selected item to the destination listbox and removes from source listbox
function AddItemToDisplayDestination(sourceID, destinationID, sortDllId, hdnSelectedColumnsId, hdnSortFieldId, hdnExcludeSortId)
{
    try
    {
        var listBoxSource = document.getElementById(sourceID);
        var selectedColumns = "";

        var hdnExcludeSort = document.getElementById(hdnExcludeSortId);
        var excludeFromSortFields = "";

        if (hdnExcludeSort != null && hdnExcludeSort != undefined)
            excludeFromSortFields = ";" + hdnExcludeSort.value.toLowerCase() + ";";

        if (listBoxSource.selectedIndex >= 0)
        {
            var listBoxDestination = document.getElementById(destinationID);

            if (sortDllId != null)
            {
                var sortDropDown = document.getElementById(sortDllId);
            }
            var count = listBoxSource.length;

            for (var i = 0; i < count; i++)
            {
                var item = listBoxSource.options[i];

                if (item.selected)
                {
                    item.selected = false;

                    listBoxSource.remove(i);
                    count--;
                    i--;

                    listBoxDestination.add(item);

                    if (sortDropDown != null)
                    {
                        var value = ";" + item.value + ";";

                        if (excludeFromSortFields.indexOf(value, 0) < 0)
                        {
                            var option = document.createElement("option");

                            sortDropDown.options.add(option);
                            option.text = item.text;
                            option.value = item.value;
                        }
                    }
                }
            }
        }
        else
        {
            alert("Please select an Available Property to add.");
        }

        for (var j = 0; j < listBoxDestination.length; j++)
        {
            if (selectedColumns == "")
                selectedColumns = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
            else
                selectedColumns = selectedColumns + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
        }

        var hdnSelectedColumns = document.getElementById(hdnSelectedColumnsId);
        if (hdnSelectedColumns != null)
            hdnSelectedColumns.value = selectedColumns;

        var hdnSortField = document.getElementById(hdnSortFieldId);
        if (hdnSortField != null && sortDropDown != null)
        {
            if (sortDropDown.selectedIndex >= 0)
            {
                hdnSortField.value = sortDropDown.options[sortDropDown.selectedIndex].value;
            }
            else
            {
                hdnSortField.value = "";
            }
        }
    }
    catch (err)
    {

    }

    return false;
}
// This will remove from destination ListBox and Add to Source List Box
function RemoveItemFromDisplayDestination(sourceID, destinationID, sortDllId, hdnSelectedColumnsId, hdnSortFieldId)
{
    //    debugger;
    try
    {
        var listBoxDestination = document.getElementById(destinationID);
        var selectedColumns = "";

        if (listBoxDestination.selectedIndex >= 0)
        {
            var listBoxSource = document.getElementById(sourceID);

            if (sortDllId != null)
            {
                var sortDropDown = document.getElementById(sortDllId);
                var hdnSortField = document.getElementById(hdnSortFieldId);
            }

            var count = listBoxDestination.length;

            for (var i = 0; i < count; i++)
            {
                var item = listBoxDestination.options[i];

                var lowerValue = item.value.toLowerCase();
                var lowerText = item.text.toLowerCase();

                if (item.selected && !(lowerValue == 'preferredname' || lowerText == 'Name*'))
                {
                    item.selected = false;
                    listBoxDestination.remove(i);
                    count--;
                    i--;
                    listBoxSource.add(item);

                    if (sortDropDown != null)
                    {
                        var Sortcount = sortDropDown.options.length;
                        for (var j = 0; j < Sortcount; j++)
                        {
                            var option = sortDropDown.options[j];
                            if (option != null && (option.value == item.value || option.text == item.text))
                            {
                                var flag = option.selected;
                                sortDropDown.remove(j);
                                Sortcount--;
                                j--;
                                if (flag)
                                {
                                    if (sortDropDown.options.length == 0)
                                        hdnSortField.value = "";
                                    else
                                        hdnSortField.value = sortDropDown.options[0].value;
                                }
                                //                            else
                                //                            {
                                //                                if(sortDropDown.options.length == 0)
                                //                                    hdnSortField.value = "";
                                //                            }

                                break;
                            }
                        }
                    }
                }

                if (item.selected && (lowerValue == 'preferredname' || lowerText == 'Name*'))
                {
                    alert("Cannot remove the fixed property type.");
                    break;
                }
            }
        }
        else
        {
            alert("Please select a Selected Property to remove.");
        }

        for (var j = 0; j < listBoxDestination.length; j++)
        {
            if (selectedColumns == "")
                selectedColumns = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
            else
                selectedColumns = selectedColumns + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
        }

        var hdnSelectedColumns = document.getElementById(hdnSelectedColumnsId);
        if (hdnSelectedColumns != null)
            hdnSelectedColumns.value = selectedColumns;
    }
    catch (err)
    {

    }
    return false;
}

function SortSelectionChange(sortDropDown, hdnSortFieldId)
{
    //    debugger;
    var hdnSortField = document.getElementById(hdnSortFieldId);
    if (hdnSortField != null)
    {
        if (sortDropDown.selectedIndex >= 0)
        {
            hdnSortField.value = sortDropDown.options[sortDropDown.selectedIndex].value;
        }
        else
        {
            hdnSortField.value = "";
        }
    }
}

// this method to move item Up by one item in the ListBox
function MoveItemUp(destinationID, hdnSelectedColumnsId)
{
    //    debugger;
    try
    {
        var listBoxDestination = document.getElementById(destinationID);
        if (listBoxDestination != null && listBoxDestination.selectedIndex != null && listBoxDestination.selectedIndex > 1)
        {
            if (listBoxDestination.options.length)
            {
                var itemText = listBoxDestination.options[listBoxDestination.selectedIndex].text;
                var itemValue = listBoxDestination.options[listBoxDestination.selectedIndex].value;

                var itemPrevious = listBoxDestination.options[listBoxDestination.selectedIndex - 1];

                listBoxDestination.options[listBoxDestination.selectedIndex].text = itemPrevious.text;
                listBoxDestination.options[listBoxDestination.selectedIndex].value = itemPrevious.value;

                listBoxDestination.options[listBoxDestination.selectedIndex - 1].text = itemText;
                listBoxDestination.options[listBoxDestination.selectedIndex - 1].value = itemValue;

                listBoxDestination.selectedIndex = listBoxDestination.selectedIndex - 1;

                var selectedColumns = "";
                for (var j = 0; j < listBoxDestination.length; j++)
                {
                    if (selectedColumns == "")
                        selectedColumns = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
                    else
                        selectedColumns = selectedColumns + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
                }

                var hdnSelectedColumns = document.getElementById(hdnSelectedColumnsId);
                if (hdnSelectedColumns != null)
                    hdnSelectedColumns.value = selectedColumns;
            }
        }
        else
        {
            if (listBoxDestination == null || listBoxDestination.selectedIndex == null || listBoxDestination.selectedIndex < 0)
            {
                alert("Please select a Selected Property to move up.");
            }
            else if (listBoxDestination.selectedIndex == 1)
            {
                alert("This property cannot be moved above the mandatory property.");
            }
            else if (listBoxDestination.selectedIndex == 0)
            {
                alert("First property cannot be moved up.");
            }
        }
    }
    catch (err)
    {

    }

    return false;
}

// this method to move item Down by one item in the ListBox
function MoveItemDown(destinationID, hdnSelectedColumnsId)
{
    //    debugger;
    try
    {
        var listBoxDestination = document.getElementById(destinationID);
        var count = listBoxDestination.length;
        if (listBoxDestination != null && listBoxDestination.selectedIndex >= 1 && listBoxDestination.selectedIndex != count - 1)
        {
            if (listBoxDestination.options.length > 0 && listBoxDestination.selectedIndex > -1)
            {
                var itemText = listBoxDestination.options[listBoxDestination.selectedIndex].text;
                var itemValue = listBoxDestination.options[listBoxDestination.selectedIndex].value;

                var itemNext = listBoxDestination.options[listBoxDestination.selectedIndex + 1];

                listBoxDestination.options[listBoxDestination.selectedIndex].text = itemNext.text;
                listBoxDestination.options[listBoxDestination.selectedIndex].value = itemNext.value;

                listBoxDestination.options[listBoxDestination.selectedIndex + 1].text = itemText;
                listBoxDestination.options[listBoxDestination.selectedIndex + 1].value = itemValue;

                listBoxDestination.selectedIndex = listBoxDestination.selectedIndex + 1;

                var selectedColumns = "";
                for (var j = 0; j < listBoxDestination.length; j++)
                {
                    if (selectedColumns == "")
                        selectedColumns = listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
                    else
                        selectedColumns = selectedColumns + ";" + listBoxDestination.options[j].value.replace(";", "!@#EscapeQM#@!");
                }

                var hdnSelectedColumns = document.getElementById(hdnSelectedColumnsId);
                if (hdnSelectedColumns != null)
                    hdnSelectedColumns.value = selectedColumns;
            }
        }
        else
        {
            if (listBoxDestination == null || listBoxDestination.selectedIndex == null || listBoxDestination.selectedIndex < 0)
            {
                alert("Please select a Selected Property to move down.");
            }
            else if (listBoxDestination.selectedIndex != null && listBoxDestination.selectedIndex == 0)
            {
                alert("Mandatory property cannot be moved down.");
            }
            else if (listBoxDestination.selectedIndex != null && listBoxDestination.selectedIndex == count - 1)
            {
                alert("Last property cannot be moved down.");
            }
        }
    }
    catch (err)
    {

    }

    return false;
}

function validateControls(idViewName, idChbDefaultView, idlistBoxDisplayDestination, idTxtImageDimension, idTxtbxPageSize)
{
    //    debugger;
    var objViewName;
    var objChbDefaultView;
    var objListBoxDisplay;

    objViewName = document.getElementById(idViewName);

    if (objViewName != null)
    {
        objViewName.value = Trim(objViewName.value);

        if (objViewName.value == "")
        {
            alert("Please enter a name for the View.");
            objViewName.focus();
            return false;
        }
        if (objViewName.value.length > 255)
        {
            alert("View Name should be less than 255 characters.");
            objViewName.focus();
            return false;
        }
    }

    // checking for display columns count should be >0 
    objListBoxDisplay = document.getElementById(idlistBoxDisplayDestination);
    if (objListBoxDisplay != null && objListBoxDisplay.options != null && objListBoxDisplay.options.length > 0)
    {
        //        return true;
    }
    else
    {
        alert("Please select at least one column for display.");
        objListBoxDisplay.focus();
        return false;
    }

    var objTxtImageDimension = document.getElementById(idTxtImageDimension);
    if (objTxtImageDimension != null)
    {
        if (objTxtImageDimension.value < 40 || objTxtImageDimension.value > 100)
        {
            alert("Please enter image dimension between 40 and 100.");
            objTxtImageDimension.focus();
            return false;
        }
    }

    //     Default view Check
    objChbDefaultView = document.getElementById(idChbDefaultView);
    if (objChbDefaultView != null)
    {
        if (objChbDefaultView.checked == true)
        {
            if (confirm("You want to set this as default?"))
            {
                //                return true;
            }
            else
            {
                return false;
            }
        }
    }

    var objTxtbxPageSize = document.getElementById(idTxtbxPageSize);
    if (objTxtbxPageSize != null)
    {
        if (IsInteger(objTxtbxPageSize.value))
        {
            var pageSize = parseInt(objTxtbxPageSize.value);
            if (pageSize <= 0 || pageSize > 1000)
            {
                alert("Please enter page size between 1 and 1000.");
                return false;
            }
        }
        else
        {
            alert("Please enter an integer between 1 and 1000 for Page Size.");
            return false;
        }
    }

    return true;
}

function IsInteger(strString)
{
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;

    if (strString.length == 0)
        return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }

    return blnResult;
}

function Trim(str)
{
    while (str.charAt(0) == (" "))
    {
        str = str.substring(1);
    }
    while (str.charAt(str.length - 1) == " ")
    {
        str = str.substring(0, str.length - 1);
    }
    return str;
}

function ConfirmForDelete()
{
    if (confirm("Are you sure you want to delete this view?"))
    {
        return true;
    }
    else
    {
        return false;
    }
}