//*****************************************************************************
//
// DESCRIPTION: General JavaScript Modules for System-Wide Usage.
//
//*****************************************************************************

//*****************************************************************************
//* FUNCTION
//*****************************************************************************
function squeezespaces ()
{
    return this.replace(/ +/g, " ");
}

String.prototype.squeezespaces = squeezespaces;

//*****************************************************************************
//* FUNCTION
//*****************************************************************************
function trim ()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.trim = trim;

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function replaceChar(toReplace, toReplaceWith)
{
	var reg = eval('/' + toReplace + '/gi');
	return this.replace(reg, toReplaceWith);
}

String.prototype.replaceChar = replaceChar;

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function getFieldValue (strFieldName)
{
    var strFieldValue;
    var objRegExp = new RegExp(strFieldName + "=([^&]+)","gi");

    if (objRegExp.test(location.search))
    {
        strFieldValue = unescape(RegExp.$1);
        strFieldValue = strFieldValue.replaceChar('\\+', ' ');
    }
    else 
    {
        strFieldValue="";
    }
    
    return strFieldValue;
}

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function getSelectedRadio(buttonGroup) 
{
    // returns the array number of the selected radio button or -1 if no button is selected
    
    // if the button group is an array (one button is not an array)
    if (buttonGroup[0]) 
    { 
        for (var i=0; i<buttonGroup.length; i++) 
        {
            if (buttonGroup[i].checked) 
            {
                return i
            }
        }
    } 
    else 
    {
        // if the one button is checked, return zero
        if (buttonGroup.checked) 
        { 
            return 0; 
        } 
    }

    // if we get to this point, no radio button is selected
    return -1;
    
} // Ends the "getSelectedRadio" function

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function setSelectedRadioValue(buttonGroup, value) 
{
    // if the button group is an array (one button is not an array)
    if (buttonGroup[0]) 
    { 
        for (var i=0; i<buttonGroup.length; i++) 
        {
            if (buttonGroup[i].value == value) 
            {
                buttonGroup[i].checked = true;
            }
        }
    } 
    else 
    {
        buttonGroup.checked = true;
    }

} // Ends the "getSelectedRadioValue" function

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function getSelectedRadioValue(buttonGroup) 
{
    // returns the value of the selected radio button or "" if no button is selected
    var i = getSelectedRadio(buttonGroup);
    
    if (i == -1) 
    {
        return "";
    } 
    else 
    {
        // Make sure the button group is an array (not just one button)
        if (buttonGroup[i]) 
        { 
            return buttonGroup[i].value;
        } 
        // The button group is just the one button, and it is checked
        else 
        { 
            return buttonGroup.value;
        }
   } // else
   
} // Ends the "getSelectedRadioValue" function

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function getSelectedCheckbox(buttonGroup) 
{
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   
    var retArr = new Array();
    var lastElement = 0;
   
    // if the button group is an array (one check box is not an array)
    if (buttonGroup[0]) 
    { 
        for (var i=0; i<buttonGroup.length; i++) 
        {
            if (buttonGroup[i].checked) 
            {
                retArr.length = lastElement;
                retArr[lastElement] = i;
                lastElement++;
                
            } // if
            
        } // for
    } 
    // There is only one check box (it's not an array)
    else 
    { 
        // if the one check box is checked
        if (buttonGroup.checked) 
        { 
            retArr.length = lastElement;
            retArr[lastElement] = 0; // return zero as the only array value
        }
    }
    
    return retArr;
    
} // Ends the "getSelectedCheckbox" function

//*****************************************************************************
// FUNCTION
//*****************************************************************************
function getSelectedCheckboxValue(buttonGroup) 
{
    // return an array of values selected in the check box group. if no boxes
    // were checked, returned array will be empty (length will be zero)
    
    var retArr = new Array(); // set up empty array for the return values
    var selectedItems = getSelectedCheckbox(buttonGroup);
    
    // if there was something selected
    if (selectedItems.length != 0) 
    { 
        retArr.length = selectedItems.length;
        for (var i=0; i<selectedItems.length; i++) 
        {
            // Make sure it's an array
            if (buttonGroup[selectedItems[i]]) 
            { 
                retArr[i] = buttonGroup[selectedItems[i]].value;
            } 
            // It's not an array (there's just one check box and it's selected)
            else 
            { 
                retArr[i] = buttonGroup.value; // return that value
                
            } // else
            
        } // for
    } // if
    
    return retArr;
    
} // Ends the "getSelectedCheckBoxValue" function

//*****************************************************************************
//*****************************************************************************
// COOKIE FUNCTIONS
//*****************************************************************************
//*****************************************************************************
var caution = false 

//*****************************************************************************
// FUNCTION
//*****************************************************************************
// name - name of the cookie 
// value - value of the cookie 
// [expires] - expiration date of the cookie 
// (defaults to end of current session) 
// [path] - path for which the cookie is valid 
// (defaults to path of calling document) 
// [domain] - domain for which the cookie is valid 
// (defaults to domain of calling document) 
// [secure] - Boolean value indicating if 
// the cookie transmission requires a secure transmission 
// * an argument defaults when it is assigned null as a placeholder 
// * a null placeholder is not required for trailing omitted arguments 
//*****************************************************************************
function setCookie(name, value, expires, path, domain, secure) 
{ 
    var curCookie = name + "=" + escape(value) + 
    ((expires) ? "; expires=" + expires.toGMTString() : "") + 
    ((path) ? "; path=" + path : "") + 
    ((domain) ? "; domain=" + domain : "") + 
    ((secure) ? "; secure" : "") 

    if (!caution || (name + "=" + escape(value)).length <= 4000) 
        document.cookie = curCookie 
    else 
        if (confirm("Cookie exceeds 4KB and will be cut!")) 
            document.cookie = curCookie 
} 

//*****************************************************************************
// FUNCTION
//*****************************************************************************
// name - name of the cookie 
// * return string containing value 
// of specified cookie or null if cookie does not exist 
//*****************************************************************************
function getCookie(name) 
{ 
    var prefix = name + "=" 
    var cookieStartIndex = document.cookie.indexOf(prefix) 
    
    if (cookieStartIndex == -1) 
        return null 

    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + 
    prefix.length) 

    if (cookieEndIndex == -1) 
        cookieEndIndex = document.cookie.length 
        
        return unescape(document.cookie.substring(cookieStartIndex + 
            prefix.length, 
            cookieEndIndex)) 
} 

//*****************************************************************************
// FUNCTION
//*****************************************************************************
// name - name of the cookie 
// [path] - path of the cookie 
// (must be same as path used to create cookie) 
// [domain] - domain of the cookie 
// (must be same as domain used to create cookie) 
// * path and domain default if assigned 
// null or omitted if no explicit argument proceeds 
//*****************************************************************************
function deleteCookie(name, path, domain) 
{ 
    if (getCookie(name)) 
    { 
        document.cookie = name + "=" + 
        ((path) ? "; path=" + path : "") + 
        ((domain) ? "; domain=" + domain : "") + 
        "; expires=Thu, 01-Jan-70 00:00:01 GMT" 
    } 
}