﻿function OverButton(id, className)
{
     var div = document.getElementById(id);
     if(null != div)
     {
         var isEnabled = IsAllChildsEnabledLimited(div);
         if (isEnabled)
         {
             div.className = className;
             div.style.cursor = "pointer";
         }
     }
}
    
function OutButton(id, className)
{
     var div = document.getElementById(id);
     if(null != div)
     {
         div.className = className;
         div.style.cursor = "default";
     }
}

function IsAllChildsEnabledLimited(object)
{
    function IsAllChildsEnabled(object, depth)
    {
        var result = true;
        if (object.children != null && object.children.length > 0 && depth > 0)
        {
            var newObject = object.children[0];
            if (newObject.disabled)
            {
                result = false;
            }
            else
            {
                result = IsAllChildsEnabled(newObject, depth - 1);
            }
        }
        return result;
    }

    var depth = 32;
    return IsAllChildsEnabled(object, depth);
}

function OverTabMenu(id)
{
    var div = document.getElementById(id);
    if (null != div)
    {
        var tabEnabled = true;
        if (div.children != null && div.children.length > 0)
        {
            if (div.children[0].disabled != null && div.children[0].disabled == true)
            {
                tabEnabled = false;
            }
        }
        if (tabEnabled && div.className == 'tabMenu tabMenuInactive')
        {
            div.className = 'tabMenu tabMenuInactiveMouse';
            if (!div.disabled)
            {
                div.style.cursor = "pointer";
            }
        }
    }
}

function OutTabMenu(id)
{
    var div = document.getElementById(id);
    if (null != div)
    {
        if (div.className == 'tabMenu tabMenuInactiveMouse')
        {
            div.className = 'tabMenu tabMenuInactive';
            div.style.cursor = "default";
        }
    }
}

function OverToolBoxButton(id)
{
     var div = document.getElementById(id);
     if(null != div)
	 {
	     div.className = 'toolBoxBackgroundMouse toolBoxButtonFrame';
	     if (!div.disabled)
	     {
	         div.style.cursor = "pointer";
	     }
     }
}
    
function OutToolBoxButton(id)
{
     var div = document.getElementById(id);
     if(null != div)
     {
         div.className = 'toolBoxBackground toolBoxButtonFrame';
         div.style.cursor = "default";
     }
}

function ToggleBrowse(enabled, fileUploadID, validatorUploadID, validationDivId)
{
    var browseButton = document.getElementById(fileUploadID);
    if (browseButton != null) {
        browseButton.disabled = !enabled;
    }
    var validator = document.getElementById(validatorUploadID);
    if (validator != null) {
        validator.disabled = !enabled;
    }

    var vDiv = document.getElementById(validationDivId);
    if (vDiv != null && !validator.isvalid)
    {
        if (enabled)
        {
            vDiv.style.border = "solid 1px red";
        }
        else
        {
            vDiv.style.border = "solid 1px grey";
        }
    }
}

function ValidateUploadFile(sender, args, fileUploadClientID, validationDivId) {
    var fup = document.getElementById(fileUploadClientID);
    var vDiv = document.getElementById(validationDivId);
    vDiv.style.border = "solid 0px white";
    
    if (!fup.disabled) {
        var fileName = fup.value;
        var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
        if (ext != "") {
            ext = ext.toUpperCase()
            if (ext == "GIF" || ext == "JPEG" || ext == "JPG" || ext == "PNG") {
                args.IsValid = true;
            }
            else {
                sender.innerHTML = "This format is not supported. Supported formats: .jpg, .png, .gif.";
                args.IsValid = false;
                vDiv.style.border = "solid 1px red";
            }
        }
        else {
            sender.innerHTML = "Please select file to upload";
            args.IsValid = false;
        }
    }
    return;
}

function GetGridSelectedItemsCount(gridId) {
    var dataGrid = igtbl_getGridById(gridId);
    var count = 0;
    if (dataGrid != null) {
        dataRows = dataGrid.Rows;
        for (var i = 0; i < dataRows.length; i++) {
            var isSelected = dataRows.getRow(i).getCell(0).getValue();
            if (isSelected) {
                count++;
            }
        }
    }
    else 
    {
        count = GetHiddenFieldSelectedItemsCount();
    }
    return count;
}

function GetHiddenFieldSelectedItemsCount() 
{
    var count = 0;
    var hiddenField = document.getElementById(hiddenFieldToStoreSelectedItemsCount);
    if (hiddenField != null) {
        count = eval(hiddenField.value);
    }
    return count;
}

function GetWidth()
{
        var x = 0;
        if (self.innerHeight)
        {
                x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                x = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
                x = document.body.clientWidth;
        }
        return x;
}
 
function GetHeight()
{
        var y = 0;
        if (self.innerHeight)
        {
                y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                y = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                y = document.body.clientHeight;
        }
        return y;
}

function SetTopLeft(divLayer)
{
    // Get the dialogbox height
    var divHeightPer = divLayer.style.height.split('px')[0];

    // Set the top variable 
    var top = (GetHeight()/2) - (divHeightPer / 2);
    //var top = (parseInt(document.body.offsetHeight) / 2) - (divHeightPer / 2)
    // Get the dialog box width
    var divWidthPix = divLayer.style.width.split('px')[0];

    // Get the left variable
    var left = (GetWidth() / 2) - (parseInt(divWidthPix) / 2);
    //var left = (parseInt(document.body.offsetWidth) / 2) - (parseInt(divWidthPix) / 2);
    // set the dialogbox position to abosulute
    divLayer.style.position = 'absolute';

    // Set the div top to the height 
    divLayer.style.top = top+'px';

    // Set the div Left to the height 
    divLayer.style.left = left+'px';
}

function SetHeightWidth(divLayer, divWidth, divHeight)
{
    // Set the dialogbox width
    divLayer.style.width = divWidth + 'px';
    // Set the dialogbox Height
    divLayer.style.height = divHeight + 'px'
}

function ClearNoteText(clientId)
{
    var textBox = document.getElementById(clientId);
    if (textBox != null && !textBox.disabled)
    {
        textBox.value = '';
    }
}

function DisableObject(id, value)
{
    var object = document.getElementById(id);
    if (object != null)
    {
        object.disabled = value;
        object.style.cursor = (value == true) ? "default" : "pointer";
    }
}

function UpdateCommandMenuState()
{
    var _disabled = false;

    var selectedItemsCount = 0;
    if (typeof (patientGridClientID) != 'undefined')
    {
        selectedItemsCount = GetGridSelectedItemsCount(patientGridClientID);
    }
    else
    {
        selectedItemsCount = GetHiddenFieldSelectedItemsCount();
    }
     _disabled = !(selectedItemsCount > 0);

    DisableObjectAndChild('divRejectOrdersFrame', _disabled);
    DisableObjectAndChild('divRejectOrderFrame', _disabled);
    DisableObjectAndChild('divRejectPatientFrame', _disabled);
    DisableObjectAndChild('divRejectPatientsFrame', _disabled);
    
    if (window.ValidateControls) //if function is defined
    {
        ValidateControls();
    }
}

function DisableObjectAndChild(id, value) 
{
    var rejectionModeDiv = document.getElementById(id);
    if (rejectionModeDiv != null) 
    {
        rejectionModeDiv.disabled = value;
        rejectionModeDiv.style.cursor = (value == true) ? "default" : "pointer";
        if (rejectionModeDiv.childNodes != null && rejectionModeDiv.childNodes.length > 0) 
        {
            var childElement = rejectionModeDiv.children[0];
            childElement.disabled = value;
            childElement.style.cursor = (value == true) ? "default" : "pointer";
            if (childElement.href_backup == "") 
            {
                childElement.href_backup = childElement.href;
            }
            if (value) 
            {
                childElement.href = "javascript:void(0);"
            }
            else 
            {
                childElement.href = childElement.href_backup;
            }
        }
    }
}

function doTabClick(id)
{
    var button = document.getElementById(id);
    if (button != null)
    {
        button.click();
    }
}

function doChildLinkClick(button) 
{
    if (button != null)
    {
        if (typeof (button.href) != 'undefined') {
            button.click();
        }
        else {
            if (button.childNodes != null)
            {
                var childsCount = button.childNodes.length;
                if (childsCount > 0)
                {
                    var i; 
                    for (i = 0; i < childsCount; i++)
                    {
                        var childButton = button.children[i];
                        if (childButton != null) {
                            doChildLinkClick(childButton);
                        }   
                    }
                }
            }
        }
    }
}


/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
} 
