﻿// JScript File
// Contains common functions that are not specific to any site/page.

//Toggles a list of controls to enabled/disabled based on condition
function Common_ToggleControls(idList, disabledCondition, clearOnDisable)
{
    var ctrlIds = idList.split('|');

    var ele;
    for (var i=0; i < ctrlIds.length; i++) 
    {
        //alert(ctrlIds[i]);
        ele = document.getElementById(ctrlIds[i]);
        
        if (ele)
        {
            ele.disabled = disabledCondition;
            
            if (clearOnDisable && ele.disabled)
            {
                switch (ele.type)
                {
                    case "textarea":
                    case "text": 
                        ele.value = "";
                        break;
                    case "checkbox": 
                        ele.checked = false;
                        break;
                    case "select-one":
                        ele.selectedIndex = 0;
                        break;
                    default: 
                        //Do not remove. For debugging purpose. Should only be showing up when coding. Not after code complete
                        alert("This control type is not handled in Common_ClearElements: "+ ele.type);
                        break;
                }
            }
        }
        else
        {
            //Do not remove. For debugging purpose. Should only be showing up when coding. Not after code complete
            alert("Element not found - id: " + ctrlIds[i]);
        }
    }
    return true;
}

//Clears a list of elements.
function Common_ClearElements(idList)
{
    var ctrlIds = idList.split('|');

    var ele;
    for (var i=0; i < ctrlIds.length; i++) 
    {
        //alert(ctrlIds[i]);
        ele = document.getElementById(ctrlIds[i]);
        
        if (ele)
        {
            Common_ClearElement(ele);
        }
        else
        {
            //Do not remove. For debugging purpose. Should only be showing up when coding. Not after code complete
            alert("Element not found - id: " + ctrlIds[i]);
        }
    }
    return true;
}

function Common_ClearElement(ele)
{
    switch (ele.type)
    {
        case "textarea":
        case "text": 
            ele.value = "";
            break;
        case "checkbox": 
            ele.checked = false;
            break;
        case "select-one":
            ele.selectedIndex = 0;
            break;
        default: 
            //Do not remove. For debugging purpose. Should only be showing up when coding. Not after code complete
            alert("This control type is not handled in Common_ClearElement: "+ ele.type);
            break;
    }
}

//Clears/hides a list of warnings.
function Common_HideElements(idList)
{
    var ctrlIds = idList.split('|');

    var ele;
    for (var i=0; i < ctrlIds.length; i++) 
    {
        //alert(ctrlIds[i]);
        ele = document.getElementById(ctrlIds[i]);
        
        if (ele)
        {
            ele.style.display = "none";
        }
        else
        {
            //Do not remove. For debugging purpose. Should only be showing up when coding. Not after code complete
            alert("Element not found - id: " + ctrlIds[i]);
        }
    }
    return true;
}

// Removes leading whitespaces
function Common_LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function Common_RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function Common_Trim( value ) {
	
	return Common_LTrim(Common_RTrim(value));
	
}

