/*
* Sitewide functions
*/

// Get array of elements based on class name
function getElementsByClassName(tName, cName){
    var elements = document.getElementsByTagName(tName);
    var returnArray = new Array();
    for(var i=0; i<elements.length; i++){
        elem = elements[i];
        if(hasClass(elem,cName)) {
            returnArray.push(elem);
        }
    }
    return (returnArray)
}

// Gets an ancestor of elem with specified tag-name
function getAncestor(elem,nName) {
    var parent = elem.parentNode;
    var parName = parent.tagName;
    var nameMatch = new RegExp(nName,'i');
    if(parent.nodeName && nameMatch.exec(parent.tagName)) return parent;
    else return getAncestor(parent,nName);
}


// change the font size of the document
function changeSize(fontSize) {
    var sheets = document.styleSheets; // Array of all stylesheets
    
    for(var i=0; i < sheets.length; i++) { // For each stylesheet
        var sheet = sheets[i];
        
        if(sheet.title == 'textSize') { // find the one called 'textSize'
            var rules = sheet.cssRules || sheet.rules; // Array of rules
            var rule = rules[0]; // The first rule
            
            // Should only be one rule, and it should be a one line font-size declaration
            if(rules.length == 1 && rule.selectorText.match(/body/i) && rule.style.fontSize) {
                rule.style.fontSize = fontSize;
                return true;
            }
            else return false; // This is a bad stylesheet, return false
        }
    }
    
    return false; // Haven't found the stylesheet - return false
}

// changes the style display type of the specified element
function styleDisplay(displayType,elemId) {
    var elem = document.getElementById(elemId);
    elem.style.display = displayType;
    
    return;
}

// Delete an element from the current document
function removeElem(elemId) {
    var thisElem = document.getElementById(elemId);
    
    thisElem.parentNode.removeChild(thisElem);
    if(document.getElementById(elemId)) return false;
    else return true;
}

// Adds a class to an element
function addClass(elementOrId,thisClass) {
    // find the element
    var element;
    if(elementOrId.nodeType && elementOrId.nodeType == 1) element = elementOrId; // If it is an element, use it as such
    else if(document.getElementById(elementOrId)) element = document.getElementById(elementOrId); // Otherwise try to use it as an element id
    else return false; // or return false
    
    var classString = element.className;
    // Analyse current class string to decide what operation to perform
    if(hasClass(element,thisClass)) { // If contains thisClass (whether either end of line or spaces on either side)
        return false; // Do nothing, return false
    } else if(classString.match('[^ ]+')) { // If it has non-space characters in it - i.e. a class already exists
        element.className = classString + ' ' + thisClass; // append this class
        return true;
    } else if(classString.match('^ *$')) { // If only contains spaces (or nothing) then no class alreadt defined...
        element.className = thisClass; // Has no class, so make this the class
        return true;
    } else { // Otherwise, we're confused - show error
        alert("Error adding class:\nElement: "+element.id+"\nClass to add: "+thisClass+"\nClass name: "+element.className);
        return false;
    }
}

// Check if element has class
function hasClass(elementOrId,thisClass) {
    // Find the element
    var element;
    if(elementOrId.nodeType && elementOrId.nodeType == 1) element = elementOrId; // If it is an element, use it as such
    else if(document.getElementById(elementOrId)) element = document.getElementById(elementOrId); // Otherwise try to use it as an element id
    else return false; // or return false
    
    var classString = element.className;
    var matches = classString.match('(?:^| )'+thisClass+'(?:$| )');
    return matches;
}

// Removes a class from an element
function removeClass(elementOrId,thisClass) {
    // find the element
    var element;
    if(elementOrId.nodeType && elementOrId.nodeType == 1) element = elementOrId; // If it is an element, use it as such
    else if(document.getElementById(elementOrId)) element = document.getElementById(elementOrId); // Otherwise try to use it as an element id
    else return false; // or return false
    
    var classString = element.className;
    // Analyse class string
    var matches = hasClass(element,thisClass);
    if(matches) { // If contains thisClass
        if(classString.match('^'+thisClass+' ')) { // Starts with this class
            var replaceMatch = new RegExp(thisClass + ' '); // Create regexp object so that it will work if a match is passed
            element.className = classString.replace(replaceMatch,''); // Remove this class and the following space
            return matches;
        } else if(classString.match(' '+thisClass)) { // Space preceeds this class
            var replaceMatch = new RegExp(' ' + thisClass); // Create regexp object so that it will work if a match is passed
            element.className = classString.replace(replaceMatch,''); // Remove the preceeding space and this class
            return matches;
        } else if(classString.match('^ *'+thisClass+' *$')) { // only contains this class and spaces
            element.className = ''; //  So remove all classes
            return true;
        } else { // Otherwise, we're confused - show error
            alert("Error removing class:\nElement: "+element.id+"\nClass to remove: "+thisClass+"\nClass name: "+element.className);
            return false;
        }
    } else {
        return false;
    }
}

/* Cookie Control Functions
***************************/

// Creates a Regexp object to match the named cookie
function cookieMatch(cookieName) {
    var cookieRgx = new RegExp('(;|,|^)( *'+cookieName+' *)=([^;]*)(;|$)');
    return cookieRgx;
}

/* Gets an array containing the parts of the cookie
* 0 = the full string
* 1 = the seperator before the cookie
* 2 = the name of the cookie, including spaces
* 3 = the value of the cookie
* 4 = the seperator following the cookie
*/
function getWholeCookie(cookieName) {
    var cookie = document.cookie;
    var cookieRgx = cookieMatch(cookieName);
    var cookieParts = cookie.match(cookieRgx);
    
    /*
    // Debugging - test cookie parts
    alert("0: "+cookieParts[0]
    +"\n"+"1: "+cookieParts[1]
    +"\n"+"2: "+cookieParts[2]
    +"\n"+"3: "+cookieParts[3]
    +"\n"+"4: "+cookieParts[4]);
    */
    
    return cookieParts || false;
}

// Returns the value of cookie <cookieName>
function getCookieVal(cookieName) {
    var cookieParts = getWholeCookie(cookieName);
    return cookieParts[3] || cookieParts;
}

// Clears the current elements of all child nodes
function clearElem(elemOrId) {
    // find the elem
    var elem;
    if(elemOrId.nodeType && elemOrId.nodeType == 1) elem = elemOrId; // If it is an element, use it as such
    else if(document.getElementById(elemOrId)) elem = document.getElementById(elemOrId); // Otherwise try to use it as an element id
    else return false; // or return false
    
    var children = elem.childNodes;
    
    for(var i=0; i < children.length; i++) {
        elem.removeChild(children[i]);
    }
    
    if(elem.childNodes.length) return false;
    else return true;
}

// Opens pop up on the homepage
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
