/*
 * Prototypes
 */

// Extends 'String' with a non-alphanumeric cleanup.
String.prototype.removeIllegalChars = function () {
  return this.replace(/[^a-zA-Z 0-9]+/g, '')
};

// Extends 'String' allowing simple whitespace cleanup (ie: replaces multiple whitepsace chars with a single space).
String.prototype.cleanInternalWhitespace = function () {
  return this.replace(/ +(?= )/g, '')
};

// Extends 'String' with a version of a trim() function. Trims leading and trailing spaces.
String.prototype.trim = function () {
  return this.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
};



/*
 * Search
 */

//if (typeof(Search) === "undefined") {
//    Search = { }
//}
 
var Search = {
    DefaultString: 'search me',
    
    
    Clear: function(el) {
        // Convert to lower and then check. Clears cases where user enters 'SEARCH ME', for example.
        if (el.value.toLowerCase() == this.DefaultString.toLowerCase()) {
            el.value = '';
        }
    },
    
    Reset: function(el) {
        // Clean and trim the incoming string.
        var s = el.value.removeIllegalChars().cleanInternalWhitespace().trim();
        
        if (s.length == 0) {
            // Cleaned & trimmed string is empty, set to default string.
            el.value = this.DefaultString;
        }
        else {
            // Set search string. Maintain casing, as used by CSS.
            el.value = s.toLowerCase();
        }
    },
    
    Clean: function(eln) {	
        var el = document.getElementById(eln);
        var s = el.value.removeIllegalChars().cleanInternalWhitespace().trim().toLowerCase();
           
        if (s.length == 0 || s == this.DefaultString) {
            return false;
        }
        
        el.value = s;
        
        return true;
    }
};



/*
 * General
 */
/*
  --------------------------------------------------------------------------------------------
  Handles the opening of new windows.
  --------------------------------------------------------------------------------------------
  We are using a strict xhtml doctype definition which has depreciated the target attribute
  of the anchor tag. Therefore we will implement a client-side solution to ensure that our
  markup remains valid. (The DOM allows us to do this and still keep everyting valid).

  NB: This method has been adopted in preference to using window.open() as there are issues
      with some browsers in that they do not correctly report the referring URL in the request
      for the new page, which could lead to additional problems in the future.
  --------------------------------------------------------------------------------------------
*/
function externalLinks() { // v1.0
    if (!document.getElementsByTagName) {
        return;
    }

    var anchors = document.getElementsByTagName('a'); 

    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i]; 

        if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
            anchor.target = '_blank'; 
    } 
}
window.onload = externalLinks;

