﻿/*
Browser-independent GetElementById method
*/

function getElement(ID) 
{
    return document.getElementById ? document.getElementById(ID) : document.all[ID];
}

/*
Browser-independent event.srcElement property
*/
function getEventSrcElement(e) 
{
    var trigger='';

    if(e.srcElement) 
    {
        trigger= event.srcElement;
    }
    else
    {    
        if (e.target) trigger= e.target;
    }
    
    return trigger;
}

function openWindow(url)
{
    window.open(url);
    return void(0);
}

function disableBack() 
{
    history.go(1);
}

/*
status  The status bar at the bottom of the window. 
toolbar  The standard browser toolbar, with buttons such as Back and Forward. 
location  The Location entry field where you enter the URL. 
menubar  The menu bar of the window 
directories  The standard browser directory buttons, such as What's New and What's Cool 
resizable Allow/Disallow the user to resize the window. 
scrollbars  Enable the scrollbars if the document is bigger than the window 
height Specifies the height of the window in pixels. (example: height='350') 
width  Specifies the width of the window in pixels. 
*/

function printEntityDetails(url, height, width) 
{
    bV = parseInt(navigator.appVersion);
    if (bV >= 4) 
    {
        var offset = 50;
        var w = window.open(url + '&format=print', "Print", "status=0,toolbar=0,location=0,resizable=1,scrollbars=1,left=" + offset + ",top=" + offset + ",height=" + (parseInt(height) - (offset * 4)) + ",width=" + (parseInt(width) - (offset * 2)));
        //w.print(); 
    } 
    else 
    {
        alert("Sorry, can't print from your web browser");
    }
    return void(0);        
}

function showHelpWindow(helpID, height, width) 
{
    bV = parseInt(navigator.appVersion);
    if (bV >= 4) 
    {
        var offset = 50;
        var w = window.open("Help.aspx?ID=" + helpID, "Help", "status=0,toolbar=0,location=0,resizable=1,scrollbars=1,left=" + offset + ",top=" + offset + ",height=" + (parseInt(height) - (offset * 4)) + ",width=" + (parseInt(width) - (offset * 2)));
    } 
    else 
    {
        alert("Wrong browser version");
    }
    return void(0);        
}

function changeFontSize(flag) 
{
    var sizeArray = ['xx-small','x-small','small','medium','large','x-large','xx-large']
    
    var span = getElement("spanFontSize");
    
    if (span.style.fontSize != null) 
    {
        for (var i=0; i<sizeArray.length; i++) 
        {
            if (sizeArray[i]==span.style.fontSize) 
            {
                var newIdx = i + flag;
                if (newIdx > -1 && newIdx < sizeArray.length) 
                {
                    span.style.fontSize = sizeArray[newIdx];             
                }                         
                break;           
            }
        }        
    } 
    else 
    {
        span.style.fontSize = 'small';
    }
}

function checkAllCheckboxes(e, lblCountID) 
{
    //alert(e);
    var allCb = document.getElementsByTagName('input');
    var checkedFlag = false;
    var senderID = getEventSrcElement(e).id;
    
    var cnt = parseInt(getElement(lblCountID).innerHTML);
    
    //Check if any of the items is checked already
    for (var i=0;i<allCb.length;i++) 
    {
        if (allCb[i].type == 'checkbox') 
        {
            if (allCb[i].id != senderID)//event.srcElement.id) 
            {
	            if (allCb[i].checked == true) 
	            {
                    checkedFlag = true;
                    break;
	            } 
	        }
        }    
    }
    
    //alert('Checked flag=' + checkedFlag ? 'true' : 'false');
    
    for (var i=0;i<allCb.length;i++) 
    {
        if (allCb[i].type == "checkbox") 
        {
	        if (allCb[i].id == senderID)// event.srcElement.id) 
	        {
                allCb[i].checked = checkedFlag;
	        } 
	        else 
	        {
	            if (allCb[i].checked == !checkedFlag) 
	            {
	                //we don't need to change the checked
	            } else 
	            {	        	            
                    allCb[i].checked = !checkedFlag;  
                    if (checkedFlag) cnt--; else cnt++;
                }              
	        }
        } 
        else 
        {
            if (allCb[i].className == "noDisplay") 
            {
                if (checkedFlag) allCb[i].value = '0'; else allCb[i].value = '1';
            }
        }   
    }    
       
    //alert('Count=' + cnt);
       
    //document.getElementById(lblCountID).innerHTML = cnt;
    getElement(lblCountID).innerHTML = cnt < 0 ? 0 : cnt;
}

function copyStatusFlag(e, txt, lblNoSelected) 
{ 
        var cnt = parseInt(getElement(lblNoSelected).innerHTML); 
        
        if (getEventSrcElement(e).checked == true) 
        { 
            getElement(txt).value = '1'; cnt++; 
        } 
        else 
        { 
            getElement(txt).value = '0'; 
            if (cnt > 0) cnt--; else cnt = 0;
        } 
        
        getElement(lblNoSelected).innerHTML = cnt;
}

function hasNoSelectedParents(e, lblNoSelected, message) 
{ 
    if (getElement(lblNoSelected) != null) {
        if (getElement(lblNoSelected).innerHTML == '0') 
        {
            alert(message); 
            return false;
        }      
    } else {
            return false;  
    }
}

function hasNoSelectedParentsOrEmpty(e, lblNoSelected, txtValueId, message) 
{ 
    var result = true;
    var el = getEventSrcElement(e);
    var txtValue = getElement(txtValueId);
    
    if (getElement(lblNoSelected) != null) 
    {
        if (getElement(lblNoSelected).innerHTML == '0') 
        {
            alert(message); 
            result = false;           
        }
        else 
        {
            if (txtValue != null && txtValue.value == '') 
            {
                alert('Field cant be empty'); 
                result = false;              
            }         
        }             
    } 
    else 
    {
        result = false;  
    }
    
    if (result == true) { showLongSearch(el); };
    
    return result;
}

function copyLabel(e, ID) 
{ 
    getElement(ID).value = getEventSrcElement(e).options[getEventSrcElement(e).selectedIndex].text;
}

//Part of the Simple Search result help screen - highlights some of the labels when the user moves mouse over 
//specific terms
function highlightElements(flag,tagName,pattern) 
{
    var allCb = document.getElementsByTagName(tagName);
    for (var i=0;i<allCb.length;i++) 
    {
        if (allCb[i].id.indexOf(pattern) > -1)
        {
            if (flag == 1) 
            {
                if (allCb[i].style.backgroundColor == null) allCb[i].style.add("background-color", "yellow");
                else allCb[i].style.backgroundColor = "yellow";
            }
            else allCb[i].style.backgroundColor = "transparent";
        }
    }
}

//*****Functions related to LongProcessSubmitButton
function showLongSearch(button)
{
    button.disabled = true;
    showSearchingProgress(button.id, 0, 100);
}

function showLongSearchSort(button)
{
    button.disabled = true;
    showSearchingProgressSort(button.id, 0, 100);
}

function showSearchingProgress(controlID, progress, maxProgress) 
{
    var control =  getElement(controlID);
    var progressInt = parseInt(progress) + 1;
    var maxProgressInt = parseInt(maxProgress);
           
    if (progressInt > 2) 
    {
        if (progressInt < 10) control.innerText = 'Searching (' + (progressInt - 2) + ' sec.)';
        else control.innerText = 'Compiling Results (' + (progressInt - 9) + ' sec.)';
    }
    
    if (progressInt < maxProgressInt) 
    {    
        setTimeout("showSearchingProgress('" + control.id + "'," + progressInt + "," + maxProgressInt + ")", 1000);           
    }
}

//Identical to the one above, just shorter messages - used by the sort buttton
function showSearchingProgressSort(controlID, progress, maxProgress) 
{
    var control =  getElement(controlID);
    var progressInt = parseInt(progress) + 1;
    var maxProgressInt = parseInt(maxProgress);
           
    if (progressInt > 2) 
    {
        if (progressInt < 10) control.innerText = 'Searching';
        else control.innerText = 'Compiling Results';
    }
    
    if (progressInt < maxProgressInt) 
    {    
        setTimeout("showSearchingProgress('" + control.id + "'," + progressInt + "," + maxProgressInt + ")", 1000);           
    }
}

//This function works with SearchResultPager custom control
function showLongSearchPager(control)
{
    for (var i=0;i<control.children.length;i++) 
    {
        var c = control.children[i];
        if (c.tagName == 'A') 
        {
            c.disabled = true;
        } else 
        {
            if (c.id.indexOf("lblProcessing") > -1) 
            {
                c.style.display = 'inline';
                showSearchingProgress(c.id, 0, 100);
            }
        }
    }
}

//*****END Functions related to LongProcessSubmitButton
