// =====================================
// Module: GTC Web Site
// Description: Javascript utility functions
// $Archive: /PublicSite/Web/includes/js/utility_fnc.js $
// $Author: Petert $
// $Date: 24/02/03 14:40 $
// $Revision: 4 $
// =====================================
// $NoKeywords: $
// =====================================

function trim(strValue)
// returns the supplied value with leading and trailing spaces removed
{
    var lngFirstNonSpace=-1;
    var lngLastNonSpace=-1;
    var lngIndex;
    for (lngIndex = 0 ; lngIndex < strValue.length ; lngIndex++)
    {
        if (' ' != strValue.charAt(lngIndex))
        {
            lngLastNonSpace = lngIndex;
            if (-1 == lngFirstNonSpace) lngFirstNonSpace = lngIndex;
        }
    }
    if (-1 == lngLastNonSpace)
    {
        return "";
    }
    else
    {
        return strValue.substring(lngFirstNonSpace,lngLastNonSpace+1);
    }
}

function isEmail(strEmail)
// returns true if the supplied string is likely to be an email address
{
    strEmail = trim(strEmail);
    if ( "" == strEmail )
    {
        return false;
    }
    else
    {
        var lngAtPos = strEmail.indexOf('@');
        var lngDotPos = strEmail.lastIndexOf('.');

        if ((-1 == lngAtPos) || (-1 == lngDotPos) || (lngAtPos<1) || (lngDotPos>=(strEmail.length-1)) || (lngDotPos<=lngAtPos))
        {
            return false;
        }
        else
        {
            return (-1==strEmail.toLowerCase().search(/[^0123456789abcdefghijklmnopqrstuvwxyz@\._-]/));
        }
    } 
}

function addOptionToSelect
    // add an option to a select control
    (
        ctlSelect,
            // select control to be added to
        strValue,
            // value to be added
        strText,
            // text to be added
        blnIsSelected
			// true if this item is selected
    )
{
    var objOption = new Option();
        // option to be added
objOption.text=strText;
    objOption.value=strValue;
    ctlSelect.options[ctlSelect.options.length] = objOption;
    objOption.selected=blnIsSelected;
}


