Utils =
{
    pageAnchors: [],
    
    validateEmailAddress : function(emailAddress)
    {
        var regex = /^[\w'_-]+(?:\.[\w'_-]+)*?@(?:(?:\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(?:(?:[\w-]+\.)+))(?:[a-zA-Z]{2,4}|\d{1,3})(?:\]?)$/;
        return emailAddress.match(regex);
    },
    
    validateTelephoneNumber : function(telephoneNumber)
    {
        var regex = /^(?:\+)?[\d\s()-]+(?:(ex|ext|extension|x|xt)[\d\s]+)?$/;
        return telephoneNumber.match(regex);
    },
    
    getAnchors: function (baseElement, ignoreCache)
    {
        var cacheIndex;
        var _baseElement;
        
        if (Object.isUndefined(baseElement)) {
            _baseElement = document;
            cacheIndex = 'doc';
        } else {
            _baseElement = $(baseElement);
            cacheIndex = _baseElement.identify();
        }
        
        var anchors = (((null != cacheIndex) && ! ignoreCache) ? Utils.pageAnchors[cacheIndex] : null);
      
        if (anchors) { return anchors; }
          
        anchors = Element.select(_baseElement, 'a');
        if (cacheIndex && ! ignoreCache) {
            Utils.pageAnchors[cacheIndex] = anchors;
        }
        
        return anchors;
    },

    // target any external links to a blank window (necessary because target attribute within content is illegal in xhtml-strict)
    targetExternalLinks : function()
    {
        var anchors = Utils.getAnchors();
        // var relAnchors = document.getElementsBySelector('a[rel="external"]');
        for (var i = 0, count = anchors.length; i < count; ++i) {
            var anchor = anchors[i];
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') {
                anchor.target = '_blank';
                /* anchor.className += ' external_link_icon'; -- just for future development this is worth having here */
            }
        }
    },
    
    setStaticLinkHrefs : function(href, staticLinkSelector, baseElement)
    {
        if (Object.isUndefined(href)) { href = ''; }
        
        var anchors = [];
        isLessConciseLinksCollection = true;
        
        if (! baseElement || ! $(baseElement)) {
            anchors = Utils.getAnchors(baseElement);    
        } else {
            anchors = Element.select($(baseElement), staticLinkSelector);
            isLessConciseLinksCollection = false;
        }
        
        anchors.each(function(anchor){
            if (isLessConciseLinksCollection && ! anchor.match(staticLinkSelector)) { return; }
            if (! anchor.getAttribute('href')) {
                anchor.setAttribute('href', href);
            }
        });
    },
    
    removeChildren : function(element)
    {
        element.immediateDescendants().each(function(node){node.parentNode.removeChild(node);});
    },

    inArray : function(needle, haystack, strictComparison)
    {
        var inHaystack = false;
        for (var i = 0, count = haystack.length; i < count; ++i) {
            if (strictComparison === true) {
                if (needle === haystack[i]) {
                    inHaystack = true;
                    break;
                }
            } else {
                if (needle == haystack[i]) {
                    inHaystack = true;
                    break;
                }
            }
        }
        return inHaystack;
    },
    
     // Scan for a numeric ID, the set of digits after the last underscore in elementId (a string).
    scanNumericId : function(elementId)
    {
        var numericIds = [];
        var  pattern = /_(\d+)$/;
        elementId.scan(pattern, function(match){numericIds.push(match[1]);});
        var numericId = numericIds.last();
        return numericId;
    },
    
    evalJSON : function(jsonString)
    {
        try {
            return eval('(' + jsonString + ')');
        } catch (e) {}
    },
    
     setAllCheckBoxes : function(formName, fieldName, switchId)
    {
        //Exit if the form doesn't exist
        if(!document.forms[formName])
            return;
            
        //Gets checkboxes and 'check/uncheck all' link
        var objCheckBoxes = document.forms[formName].elements[fieldName];
        var switchLink = document.getElementById(switchId);
        
        //Exit if either checkboxes or link don't exist
        if(! objCheckBoxes || ! switchLink)
            return;
    
        //Determines length of array
        var countCheckBoxes = objCheckBoxes.length;
        //Determines value of checked property
        var checked;
        
        //Probably not the best way of doing it, but there will be time for this later on...
        if (switchLink.innerHTML == 'uncheck all') {
            checked = false;
            switchLink.innerHTML = 'check all';
        } else {
            checked = true;
            switchLink.innerHTML = 'uncheck all';
        }
        
        if(!countCheckBoxes) {
            objCheckBoxes.checked = checked;
        }
        else {
            // set the check value for all check boxes
            for(var i = 0; i < countCheckBoxes; i++) {
                objCheckBoxes[i].checked = checked;
            }
        }
    }
}

/* A utility Behaviour ruleset for form input elements.
 * Assumption: Behaviour is loaded into the page before this script file.
*/
var formsRuleset = 
{
    '.click-select' : function (el) 
    {
        el.onclick = function(){ 
            el.focus();
            el.select();
        };
    }
}
Behaviour.register(formsRuleset);

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}
    

