/* 
 * Added String Methods v1.1.0
 * created circa 2007 by A. Lewis
 * last updated 2010-01-14 by A. Lewis
 */

// ECMAScript 5 (Javascript 1.8.1) Compliant
// return a copy of the string without leading or trailing spaces
if(!String.prototype.trim)
    String.prototype.trim = function(){
        var re = /^\s+|\s+$/g;
        return this.replace(re,"");
    } 
               
// ECMAScript 5 (Javascript 1.8.1) Compliant
// return a copy of the string without leading spaces
if(!String.prototype.trimLeft)
    String.prototype.trimLeft = function(){
        var re = /^\s+/;
        return this.replace(re,"");
    }   
               
// ECMAScript 5 (Javascript 1.8.1) Compliant
// return a copy of the string without trailing spaces
if(!String.prototype.trimRight)
    String.prototype.trimRight = function(){
        var re = /\s+$/;
        return this.replace(re,"");
    } 
    
// return a copy of a string's leftmost n number of characters 
if(!String.prototype.left)
    String.prototype.left = function(n){
        if(isNaN(n))n = this.length;
        if(!n) n = 0;
        n = parseInt(n);
        if(n > this.length)n = this.length;
        return this.substring(0, this.length - n)
    }

// return a copy of a string's rightmost n number of characters 
if(!String.prototype.right)
    String.prototype.right = function(n){
        if(isNaN(n))n = this.length;
        if(!n) n = 0;
        n = parseInt(n);
        if(n < 0)n = 0;
        return this.substring(this.length - n, this.length)
    }

/*
 * END Added String Methods
 */


/*
 *  Cookie Utilities v1.0.0
 *  by A. Lewis (written forever ago)
 */

// set a cookie
function setCookie(sName, sValue, sExpires, sPath, sDomain, secure) 
{ 
    document.cookie = sName + "=" + escape(sValue)
        + (sExpires == null ? "" : ";expires=" + sExpires.toGMTString())
        + (sPath == null ? "" : ";path=" + sPath)
        + (sDomain == null ? "" : ";domain=" + sDomain)
        + (secure == null ? "" : ";secure")
}

// get a cookie (returns string)
function getCookie(sName) 
{ 
    var search = sName + "=";
    // if there are any cookies
    if (document.cookie.length > 0) 
    { 
        offset = document.cookie.indexOf(search);
        // if cookie exists 
        if (offset != -1) 
        {  
            // set index of beginning of value
            offset += search.length; 
            // set index of end of cookie value
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(offset, end));
        }
        else 
        {
            return "";
        } 
    }
}

// delete a cookie (if path and/or domain were set to other than default
// when the cookie was written, matching arguments must be passed here.)
function deleteCookie(sName, sPath, sDomain)
{
    if(document.cookie.length > 0 && document.cookie.indexOf(sName) != -1)
    {
        var oldDate = new Date();
            oldDate.setYear(oldDate.getFullYear() - 20);
        document.cookie = sName
            + "=;expires=" + oldDate.toGMTString()
            + (sPath == null ? "" : ";path=" + sPath)
            + (sDomain == null ? "" : ";domain=" + sDomain);
        eval("var re = /" + sName + "\=[^;]*(;|$)/gi;");
        document.cookie = document.cookie.replace(re,"");
    }
}

/*
 * END Cookie Utilities
 */

/*
 * HTML Utilities v1.0.0
 * added 2010-01-14 by A. Lewis
 */

// escapeHTML v0.9.0
// characters to HTML entities
// NOTE: common symbols supported, no alpha character support
function escapeHTML(s)
{
    if(!s) s = "";
    s = s.replace(/\</g, "&lt;");
    s = s.replace(/\>/g, "&gt;");
    s = s.replace(/&(?!\b|#)/g, "&amp;");
    s = s.replace(/\ª|(<|&lt;)[Ss][Uu][Pp](>|&gt;)TM(<|&lt;)\/[Ss][Uu][Pp](>|&gt;)|\(TM\)/g, "&#8482;");         
    s = s.replace(/©/g, "&copy;");
    s = s.replace(/¨/g, "&reg;");  
    s = s.replace(/@/g, "&#64;");
    //quotes
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\'/g, "&#39;");   //"&apos;" does not work in IE
    s = s.replace(/Ü/g,  "&#8249;"); //left angle quote
    s = s.replace(/Ý/g,  "&#8250;"); //rght angle quote
    s = s.replace(/Ç/g,  "&#171;");  //left dbl angle quote
    s = s.replace(/È/g,  "&#187;");  //rght dbl angle quote
    s = s.replace(/Ô/g,  "&#8216;"); //left (single high-6)
    s = s.replace(/Õ/g,  "&#8217;"); //rght (single high-9)
    s = s.replace(/Ò/g,  "&#8220;"); //left (double high-6)
    s = s.replace(/Ó/g,  "&#8221;"); //rght (double high-9)
    s = s.replace(/ã/g,  "&#8222;"); //rght (double low-9)
    //currency
    s = s.replace(/Û/g, "&euro;");
    s = s.replace(/¢/g, "&cent;");  
    s = s.replace(/£/g, "&pound;");  
    s = s.replace(/´/g, "&yen;");  
    s = s.replace(/¤/g, "&sect;"); 
    
    return s;
}

// unescapeHTML v0.9.0
// characters from HTML entities
// NOTE: common symbols supported, no alpha character support
function unescapeHTML(s)
{
    if(!s) s = "";
    s = s.replace(/&lt;|&#60;/g,    "<");
    s = s.replace(/&gt;|&#62;/g,    ">");
    s = s.replace(/&amp;|&#38;/g,   "&");
    s = s.replace(/&#64;/g,         "@");
    s = s.replace(/&#8482;/g,       "ª");           
    s = s.replace(/&copy;|&#169;/g, "©");
    s = s.replace(/&reg;|&#174;/g,  "¨");
    //quotes
    s = s.replace(/&quot;|&#34;/g,  '"');
    s = s.replace(/&apos;|&#39;/g,  "'");
    s = s.replace(/&#8249;/g,  "Ü"); //left angle quote
    s = s.replace(/&#8250;/g,  "Ý"); //rght angle quote
    s = s.replace(/&#171;/g,   "Ç"); //left dbl angle quote
    s = s.replace(/&#187;/g,   "È"); //rght dbl angle quote
    s = s.replace(/&#8216;/g,  "Ô"); //left (single high-6)
    s = s.replace(/&#8217;/g,  "Õ"); //rght (single high-9)
    s = s.replace(/&#8220;/g,  'Ò'); //left (double high-6)
    s = s.replace(/&#8221;/g,  'Ó'); //rght (double high-9)
    s = s.replace(/&#8222;/g,  'ã'); //rght (double low-9)
    //currency
    s = s.replace(/&euro;/g,         "Û");
    s = s.replace(/&cent;|&#162;/g,  "¢");  
    s = s.replace(/&pound;|&#163;/g, "£");  
    s = s.replace(/&yen;|&#165;/g,   "´");  
    s = s.replace(/&sect;|&#167;/g,  "¤");             
    
    return s;
}

// unescapeHTMLTag v1.0.0
// restores HTML tags that have had delimiting characters converted into 
// HTML entites. It additionally by default forces the tag to use single
// quoted attributes since the source argument string may be enclosed in
// double quotes itself as an attribute of another HTML element.
function unescapeHTMLTag(s)
{
    if(!s) s = "";
    s = s.replace(/&lt;|&#60;/g, "<");
    s = s.replace(/&gt;|&#62;/g, ">");
    
    // handle nested single quotes used as apostrophes via &#39; apostrophe entity
    s = s.replace(/(\w+)\s*=\s*'/gi, "$1='"); // normalize spacing
    s = s.replace(/(?!\B)\'(t|re|ve|ll)(?!\B)/gi, "&#39;$1"); // contractions ("He'll be here") 
    s = s.replace(/(?!=)(\s)'(\w+)/gi, "$1&#39;$2"); // leading apostrophes ("I was there in '08...")
    
    // force single quotes in tag attributes by default
    if(!arguments[1]) s = s.replace(/\"/g, "'");
    
    return s;
}

/*
 * END HTML Utilities
 */
