﻿// set span text value
function CrossbrowserSetSpanValue(label, value)
{
    if (label)
    {
        label.textContent = value;
        label.innerText = value;
    }
}

// assigns text value to the label and sets it display property, if the value is empty hides it
function AssignListingTextInput(sender, labelObj)
{
    if (sender != null && labelObj != null)
    {
        CrossbrowserSetSpanValue(labelObj, sender.value);
    }        
}

// replaces all occurencies of a value in a string
function MultiReplace(str, regExpValue, valToReplace)
{
    while (str.indexOf(regExpValue) > -1)
    {
        str = str.replace(regExpValue, valToReplace);
    }
            
    return str;
}

// shows as passed display style/hides object with passed ID, depending on checkbox status
function ShowHideItem(itemID, sender, dispStyle)
{
    var item = getRef(itemID);

    if (item != null)
    {
        if (sender != null)
        { 
            if (sender.checked) item.style.display = dispStyle;
            else item.style.display = 'none';
        }
    }
}

// showes/hides object found by reference
function SetItemVisibility(item, dispStyle)
{
    if (item != null)
    {
        item.style.display = dispStyle;
    }
}

// shows as inline/hides object with passed reference, depending on checkbox status
function ShowHideFacilityIcon(itemObj, sender)
{        
    if (itemObj != null)
    {
        if (sender != null)
        { 
            if (sender.checked) itemObj.style.display = "inline";
            else itemObj.style.display = "none";
        }
    }
}

// Controls input data to the input field (restricts maximum allowed length) 
// and display information about remaining chars in the specified label
function ControlInputField(obj, maxChars, countInfoObjID, labelTextObjID)
{
    var remCh;
    var ltxt;
    var countLabelObj = getRef(countInfoObjID);
    var textObj = getRef(labelTextObjID);
    
    if (obj != null && countLabelObj != null && textObj != null)
    {
        obj.value = obj.value.substring(0, maxChars);
        
        remCh = maxChars - obj.value.length;
        
        ltxt = '(' + remCh + ' ';                
        ltxt += (remCh > 1 || remCh == 0) ? 'chars.' : 'char.';
        ltxt += ' remaining)';          
         
        CrossbrowserSetSpanValue(countLabelObj, ltxt);
        
        // update text object
        CrossbrowserSetSpanValue(textObj, obj.value);
        
        if (obj.value.length > 0) SetItemVisibility(textObj, 'block');
        else SetItemVisibility(textObj, 'none');
    }
}

// Limits postal code (trims the second part)
function LimitPostalCode(postalcode)
{
    if (postalcode.indexOf(' ') > -1) postalcode = postalcode.substring(0, postalcode.indexOf(' '));        
    return postalcode;
}

function IsVisible(obj)
{
    result = false;
    if (obj && (obj.style.display.length > 0 && obj.style.display != 'none')) result = true;
    return result;
}

function SetItemVisibilityByRef(obj, visible)
{
    if (obj != null)
    {
        if (visible) obj.style.display = 'block';
        else obj.style.display = 'none';
    }
}