﻿var nlsUtil = new Object();

//--------------------------------------BROWSER-------------------------------

//Get client's browser type (IE, Safari, FireFox etc') 
nlsUtil.BrowserType = (function x() {
})[-5] == 'x' ? 'FF3' : (function x() {
})[-6] == 'x' ? 'FF2' : '\v' == 'v' ? 'IE' : /a/.__proto__ == '//' ? 'Saf' : /s/.test(/a/.toString) ? 'Chr' : 'Op';

//Geting browser workspace height
nlsUtil.GetWorkspaceHeight = function() {

    var workspaceHeight = (window.innerHeight ? window.innerHeight :
                                 (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
    return workspaceHeight;
}

//Geting browser workspace width if need it
nlsUtil.GetWorkspaceWidth = function() {

    var workspaceWidth = (window.innerWidth ? window.innerWidth :
         (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
    return workspaceWidth;
}


//-----------------------------------------COOKIES---------------------------

//Write Cookie
nlsUtil.WriteCookie = function WriteCookie(name, value, days) {

    var expires = "";

    if (days) {
        var date = new Date();
        date.setTime(date.getTime) + (days * 24 * 60 * 60 * 1000);
        expires = "; expires" + date.toUTCString();
    }

    document.cookie = name + "=" + value + expires + "; path=/";
}

// Read Cookie
nlsUtil.ReadCookie = function ReadCookie(name) {

    var searchName = name + "=";
    var cookies = document.cookie.split(';');

    for (var i = 0; i < cookies.length; i++) {
        var c = cookies[i];
        while (c.charATt(0) == ' ')
            c = c.substring(1, c.lenght);
        if (c.indexOf(searchName) == 0)
            return c.substring(searchName.length, c.length);
    }
    return null;
}

// Erase Cookie
nlsUtil.EraseCookie = function(name) {
    this.WriteCookie(name, "", -1);
}

//---------------------------------JavaScript Tools-----------------------------------------------

// Get elements by class name
nlsUtil.getElementsByClass = function(className) {
    var all = document.all ? document.all :
    document.getElementsByTagName('*');
    var elements = new Array();
    for (var e = 0; e < all.length; e++)
        if (all[e].className.indexOf(className) > -1)
        elements[elements.length] = all[e];
    return elements;
}

//--------------------------------StringValidation-----------------------------------------------

//use with customValidators
nlsUtil.ValidateString = function(ctl, args) {

    var isValid = false;
    isValid = nlsUtil.ValidateStringSQL(args.Value);
    args.IsValid = isValid;
}

//use to validate strings 
nlsUtil.ValidateStringSQL = function(str) {
    if (str.indexOf("<") >= 0)
        return false;

    if (str.indexOf(">") >= 0)
        return false;

    if (str.indexOf("//") >= 0)
        return false;

    if (str.indexOf("--") >= 0)
        return false;

    return true;
}

nlsUtil.StringIsEmpty = function(str) {
    var s = str.replace(" ", "");
    if (s == "")
        return true;

    return false;
}

//----------------CheckBox Validation-------------
nlsUtil.CheckBoxValidatorDisableButton = function(chkId, mustBeChecked, btnId) {
    var button = document.getElementById(btnId);
    var chkbox = document.getElementById(chkId);

    if (button && chkbox) {
        button.disabled = (chkbox.checked != mustBeChecked);
    }
}

nlsUtil.CheckBoxValidatorEvaluateIsValid = function(val) {
    var control = document.getElementById(val.controltovalidate);
    var mustBeChecked = Boolean(val.mustBeChecked == 'true');

    return control.checked == mustBeChecked;
}

nlsUtil.CheckBoxListValidatorEvaluateIsValid = function(val) {
    var control = document.getElementById(val.controltovalidate);
    var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);

    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    while (currentListItem != null) {
        if (currentListItem.checked) selectedItemCount++;
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }

    return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}

//--------------------------------Alert window-----------------------------------------------

nlsUtil.Alert = function(alertText) {
    alert(alertText);
}

//--------------------------------Confirm window-----------------------------------------------

nlsUtil.Confirm = function(confirmText) {
    if (confirm(confirmText))
        return true;
    return false;
}

//------------------------------Set IFrame Height---------------------------------------------

nlsUtil.SetIframeHeight = function(iFrameId) {

    // Find the height of the current page (the condition is for browser compatability)
    var docHeight = document.body.clientHeight;

    if (docHeight == 0) {
        docHeight = document.documentElement.clientHeight;
    }
    // Get the iFrame element in which this page is displayed
    var currentFrame = parent.document.getElementById(iFrameId);

    // Set its height
    currentFrame.setAttribute("height", docHeight + 40);
}

nlsUtil.SetIframeHeightExtra = function(iFrameId, extraHeight) {

    // condition for browser compatability: find the height of the current page
    var docHeight = document.body.clientHeight;

    if (docHeight == 0) {
        docHeight = document.documentElement.clientHeight;
    }

    var currentFrame = parent.document.getElementById(iFrameId);

    currentFrame.setAttribute("height", docHeight + extraHeight);
}

//-----------------------------------------Drop Down Lists Helpers-------------------------------------
var ddlUtil = new Object();

ddlUtil.selectIndexByValue = function(ddlId, value) {
    var ddl = document.getElementById(ddlId);

    if (ddl) {
        ddl.selectedIndex = 0
        try {
            for (var i = 0; i < ddl.length; i++) {
                if (ddl.options[i].value == value) {
                    ddl.selectedIndex = i;
                    break;
                }
            }
        }
        catch (e) {

        }
    }
}

ddlUtil.getValueByText = function(ddlId, Text) {
    var ddl = document.getElementById(ddlId);

    if (ddl) {
        try {
            for (var i = 0; i < ddl.length; i++) {
                if (ddl.options[i].text == Text) {
                    return ddl.options[i].value;
                }
            }
        }
        catch (e) {

        }
    }
    return "";
}

ddlUtil.getTextByValue = function(ddlId, value) {
    var ddl = document.getElementById(ddlId);

    if (ddl) {
        try {
            for (var i = 0; i < ddl.length; i++) {
                if (ddl.options[i].value == value) {
                    return ddl.options[i].text;
                }
            }
        }
        catch (e) {
        }
    }
    return "";
}

// Builds ddl items based on values in oList
ddlUtil.builddlFromTextValueArray = function(oDdl, oList) {

    oDdl.innerHTML = "";

    for (var i = 0; i < oList.length; i++) {

        // Create the list item.
        var option = document.createElement("option");
        option.innerHTML = oList[i].Text;
        option.value = oList[i].Value;
        oDdl.appendChild(option);
    }

    oDdl.style.visibility = "visible";
    oDdl.disabled = false;
}
//-----------------------------------------Event on deleting text in TextBoxes-------------------------------------

nlsUtil.SetDeletedValue = function(ContainerId) {
    var jInput = $("#" + ContainerId + " input");
    jInput.change(function(objEvent) {
        nlsUtil.OnTextChange($(this));
    });
}

nlsUtil.OnTextChange = function(textBox) {
    if (textBox[0].value == "") {
        textBox[0].style.color = "Red";
        textBox[0].value = "DELETED";
    }
    else
        textBox[0].style.color = "Black";
}


//---------------------------------------Loading animation functions----------------------------

nlsUtil.StartAnimation = function(loading) {

    var animation = document.getElementById("waitingAnimation");
    if (animation == null) {
        window.parent.nlsUtil.StartAnimation(loading);
    }
    else {
        if (loading) {
            var top = (document.body.offsetHeight / 2 - 50) + "px";
            var left = (document.body.offsetWidth / 2 - 50) + "px";
            animation.style.position = 'absolute';
            animation.style.left = left;
            animation.style.top = top;
            animation.style.zIndex = 100;
            animation.style.display = 'block';

            //            $(animation).css({ position: 'absolute', left: left, top: top, zIndex: 100 });
            //            $(animation).removeClass("hidden");
        }
        else {
            animation.style.display = 'none';
            //            $(animation).addClass("hidden");
        }
    }
}

//--------------------------------------------------------------------------------------------------------
// When re-activating a tab, need to make sure the iFrame inside its active div is set to its right height
nlsUtil.SetHeight = function(TabIFrame) {

    var divs = TabIFrame.contentWindow.document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == "visiblePanel") {
            var arr = divs[i].getElementsByTagName("iFrame");
            for (var j = 0; j < arr.length; j++) {
                if (arr[j].contentWindow.SetIFrameHeight) {
                    arr[j].contentWindow.SetIFrameHeight();
                }
            }
            return;
        }
    }
}

nlsUtil.IsValidEmailPerfix = function(string) {

    var emailPattern = /^\w+((-\w+)|(\.\w+))*$/;
    return emailPattern.test(string);

}

