﻿// General JScript File of utilities

        function OnEnter(e,codeToRun)
        {
            if (e.keyCode == 0) keyCode = e.which;
            else if (e.keyCode != 0) keyCode = e.keyCode;
            else return;
            if (keyCode == 13) { 
                  eval(codeToRun);
            }
        } 

// image resizing - TODO - const value should be in configurations         
         var  phone_preview_height = 200;
         var  phone_preview_width = 200;
         var  search_height = 90; //used also for ExpertsViewer
         var  search_width = 120;
        
          function ImageResize(imageCID, limit_width, limit_height)
          {                
                var imgElm = document.getElementById(imageCID);                
          
                //for IE
                var width = imgElm.getAttribute("clientWidth"); 
                var height = imgElm.getAttribute("clientHeight"); 
               
                //For Firefox
                if (width == null)
                {
                    width = imgElm.naturalWidth;
                }
                if(height == null)
                {
                    height = imgElm.naturalHeight;
                }               
                                
                ImageResizeLimit(imgElm, width, height, limit_width, limit_height);                                
                
                // center it
                if (imgElm.width < limit_width)
                {                                            
                    if (parent.document.body.dir == 'rtl')
                    {
                        imgElm.style.marginRight = Number((limit_width - imgElm.width) / 2) + 'px';                        
                    }
                    else                    
                    {
                        imgElm.style.marginLeft = Number((limit_width - imgElm.width) / 2) + 'px';
                    }
                }
                if (imgElm.height < limit_height)                
                {
                    imgElm.style.marginTop = Number((limit_height - imgElm.height) / 2) + 'px';
                }
                
                imgElm.style.visibility="Visible";
          }
          function ImageResizeLimit(image, imageWidth, imageHeight, limit_width, limit_height)
          {               
                var retWidth = limit_width;
                          
                if(imageWidth > limit_width || imageHeight > limit_height)
                {                                                    
                    var ratio = imageWidth / imageHeight;
                   
                    var candHeight = (1/ratio) * limit_width;
                    if(candHeight <= limit_height)
                    {                                                
                        image.width = retWidth = limit_width;                        
                        image.height = candHeight;                    
                    }
                    else
                    {
                        var candWidth =  ratio * limit_height;                        
                        image.width = retWidth = candWidth;                        
                        image.height = limit_height;
                    }  
                }                   
                return retWidth;
          }
          function ImageResizeFillFrame(imageCID, divWrapperCID)
          {
                var imgElm = document.getElementById(imageCID);
                var divWrapperElm = document.getElementById(divWrapperCID);
          
                //for IE
                var width = imgElm.getAttribute("clientWidth"); 
                var height = imgElm.getAttribute("clientHeight"); 
               
                //For Firefox
                if (width == null)
                {
                    width = imgElm.naturalWidth;
                }
                if(height == null)
                {
                    height = imgElm.naturalHeight;
                }               
                
                var divWidth = divWrapperElm.style.width.replace('px','');
                var divHeight = divWrapperElm.style.height.replace('px','');
                
                var imageRatio = width / height;
                var divRatio = divWidth / divHeight;
                              
                if (imageRatio < divRatio)
                {
                    imgElm.width = divWidth;
                    imgElm.height = Number(divWidth / imageRatio);
                    imgElm.style.position = 'absolute';
                    imgElm.style.top = '-' + Number((imgElm.height - divHeight) /2) + 'px';                    
                }
                else
                {
                    imgElm.height = divHeight;
                    imgElm.width = Number(divHeight * imageRatio);
                    imgElm.style.position = 'absolute';
                    imgElm.style.left = '-' + Number((imgElm.width - divWidth) /2) + 'px';
                }
                imgElm.style.visibility="Visible";                                                
          }
          function ImageStretch(imageCID,height,width)
          {
                var elm = document.getElementById(imageCID);
                elm.width = width;
                elm.height = height;   
                elm.style.visibility="Visible";         
          }
         function ImageShow(imageCID)
          {                
                var elm = document.getElementById(imageCID);
                elm.style.visibility="Visible";         
          }         
         function ImageResizePhonePreview(imageCID)
         {   
            ImageResize(imageCID, phone_preview_width, phone_preview_height);     
         }
         function ImageResizeSearch(imageCID)
         {  
            ImageResize(imageCID, search_width, search_height);   
         }
         
//Toggle Panel Visibility           
    function TogglePanelVisibility(hiddenFieldClientId)
    {
        var hidden = document.getElementById(hiddenFieldClientId);
        if(hidden != null)
        {
            TogglePanelVisibilityByClientID(hidden.value);
        }
    }
    function TogglePanelVisibilityByClientID(clientID)
    {
           var panel = document.getElementById(clientID);
            if(panel != null)
            {            
                if(panel.style.display == 'none')
                {
                    panel.style.display = 'block';  
                }
                else
                {
                    panel.style.display = 'none';  
                }
            }
    }
    function TogglePanelVisibilityByClientIDWithIndicator(clientID, imageCid, collapsedPath, expandedPath)
    {
           var panel = document.getElementById(clientID);
            if(panel != null)
            {            
                var image = $get(imageCid);
                if(panel.style.display == 'none')
                {
                    panel.style.display = 'block';
                    image.src = collapsedPath;
                }
                else
                {
                    panel.style.display = 'none'; 
                    image.src = expandedPath; 
                }
            }
    }
    
    function ShowFileUpload(panel1ID, panel2ID, hiddenFieldCID)
    {
        TogglePanelVisibilityByClientID(panel1ID);
        TogglePanelVisibilityByClientID(panel2ID);
        document.getElementById(hiddenFieldCID).value = "false";
    }
    
//Get Client Cookie    
    function GetClientCookieValue(cookieName,hiddenFieldValueCID)
    {
           if (document.cookie.length > 0)
            {
                var c_start = document.cookie.indexOf( cookieName + '=');
                if (c_start != -1)
                {
                    c_start = c_start + cookieName.length + 1; 
                    var c_end = document.cookie.indexOf(";",c_start);
                    if (c_end==-1) c_end=document.cookie.length;  
                    var val = document.cookie.substring(c_start,c_end);
                    var valHF = document.getElementById(hiddenFieldCID);
                    if(valHF!= null)
                    {
                        valHF.value = val;
                    }
                }
            } 
    } 
    function SetClientCookieValue(cookieName, cookieValue, ttlInDays)
    {
          var exdate = new Date()
          exdate.setDate(exdate.getDate()+ttlInDays)
          document.cookie = cookieName+ '=' + cookieValue + ';expires=' + exdate.toGMTString() + ';path=/';     
    }  

        
// **************************************************************        
        
        function ellipsis_old(elmID, widthLimit, ellipsisCtrlID) {                    
                               
                    var e = document.getElementById(elmID);         
                                        
                    if (widthLimit < 20) widthLimit = 20;
                    
                    if (e.offsetWidth > widthLimit)                                   
                    {
                        t = e.innerHTML;
                    
                        widthLimit -= 15; // place for the elipsis
                        
                    
                        var low = 0;   
                        var high = t.length - 1;                    
                        var next = high;
                        var isFound = false;
                        var itr = 0;
                        while (!isFound)
                        {                                                    
                            
                            if (e.offsetWidth >= widthLimit)
                            {                            
                                high = next;
                            }
                            else
                            {
                                low = next;
                            }
                            next = parseInt((low + high) / 2);
                            
                            
                            if (low + 1 >= high) isFound = true;
                            
                            var t2 = t.substr(0, next);
                            e.innerHTML = t2;
                            itr ++;
                        }
                 
                        var elpsStr = '<acronym title="' + t + '">…</acronym>' + itr;   
                        var elps = null;
                        if (ellipsisCtrlID != null)
                        {
                            elps = document.getElementById(ellipsisCtrlID);
                        }                        
                        if (elps != null)
                        {
                            elps.innerHTML = elpsStr;
                        }
                        else
                        {
                            e.innerHTML += elpsStr;
                        }
                    }                                                                                                  
                  
                }
                
                function ellipsis(elmID, widthLimit) {                                                   
                    var e = document.getElementById(elmID);  
                    if (e == null)
                    {
                        return;  
                    }                         
                    if (e.offsetWidth > widthLimit)                                   
                    {
                        t = e.innerHTML;                        
                        var next = t.length - 1;                                            
                        var isFound = false;                        
                        var w = e.offsetWidth;
                        while (w + 10 > widthLimit)
                        {                                            
                            var prev = next;        
                            var w = e.offsetWidth;
                            if (w > widthLimit)
                            {                   
                                next = parseInt(next * widthLimit / w);
                            }                                                        
                            if (next + 1 >= prev) break;
                            
                            var t2 = t.substr(0, next);
                            e.innerHTML = t2 + '…';
                        }                        
                    }                                                                                                                    
                }


