﻿///This is a place to put your general purpose functions that are NOT SITE SPECIFIC and NOT FRAMEWORK SPECIFIC.
///Think of this as a toolbelt that can be used on any site.
///THESE FUNCTIONS SHOULD NOT RELY ON A SPECIFIC FRAMEWORK (I.E. JQUERY).
///PUT FRAMEWORK SPECIFIC FUNCTIONS IN general-[framework].js

///Gets the value of the query string specified by name or null if not found.
///Returns string or null if not found.
///Usage - var blah = location.getQueryString("queryStringName");
window.location.getQueryString = function(queryStringName) {
    if (typeof (queryStringName) != 'string') return null;
    var qStrings = this.search.substring(1).split("&");
    for (var i = 0; i < qStrings.length; i++) {
        var pair = qStrings[i].split("=");
        if (pair[0] == queryStringName) return decodeURIComponent(pair[1].replace(/\+/g, " "));
    }
    return null;
}

///Adds the specified querystring to the end of a string.
///Returns string.
///Usage - var blah = getElementByID('myLink').href.addQueryString("queryStringName", "queryStringValue");
///if 'myLink' had href of 'www.blah.com/boing' this would return 'www.blah.com/boing?queryStringName=queryStringValue'
///if 'myLink' had href of 'www.blah.com/boing?someqs=blah' this would return 'www.blah.com/boing?someqs=blah&queryStringName=queryStringValue'
String.prototype.addQueryString = function(queryStringName, queryStringValue) {
    var href = this;
    if (typeof (queryStringName) == 'string' && typeof (queryStringValue) == 'string') {
        var delim = this.indexOf('?') > -1 ? '&' : '?';
        href += delim + queryStringName + "=" + queryStringValue;
    }
    return href;
}

///Gets the value of the query string specified by name or null if not found.
///Useful for parsing hrefs in jQuery.
///Returns string or null if not found.
///Usage - var blah = "/blah/blah/blah.aspx?blah=1&queryStringName=2".getQueryString("queryStringName");
String.prototype.getQueryString = function(queryStringName) {
    var queryBlock = this.split("?")[1];
    if (queryBlock == undefined || queryBlock == null) return null;
    var qStrings = queryBlock.split("&");
    for (var i = 0; i < qStrings.length; i++) {
        var pair = qStrings[i].split("=");
        if (pair[0] == queryStringName) return decodeURIComponent(pair[1].replace(/\+/g, " "));
    }
    return null;
}

///Trims leading and trailing whitespace from the string.
///Returns string
///Usage - string.trim()
///Author - found on web. Added by BWM.
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

///Trims leading whitespace from the string.
///Returns string
///Usage - string.lTrim()
///Author - found on web. Added by BWM.
String.prototype.lTrim = function() {
    return this.replace(/^\s+/, "");
}

///Trims trailing whitespace from the string.
///Returns string
///Usage - string.rTrim()
///Author - found on web. Added by BWM.
String.prototype.rTrim = function() {
    return this.replace(/\s+$/, "");
}

///Returns the index of the specified item within the array or -1 if not found.
///Returns Int
///Usage - var index = array.indexOf(item);
///Author - BWM
Array.prototype.indexOf = function(item) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === item) return i;
    }
    return -1;
}

///Replaces all instances of specified substring within the string.
///Returns String
///Usage - string.replaceAll("subString", "newString")
///or to ignore case - string.replaceAll("subString", "newString", true)
///Author - BWM
String.prototype.replaceAll = function(substr, newstr, caseInsensitive) {
    var regexMeta = ['\\', '[', '^', '$', '.', '|', '?', '*', '+', '(', ')']; //Regex meta characters must be escaped.
    var regexMod = caseInsensitive ? 'gi' : 'g';
    for (var i in regexMeta) substr = substr.replace(regexMeta[i], "\\" + regexMeta[i]);
    return this.replace(new RegExp(substr, regexMod), newstr);
}

///Returns true if the string is "true" or "True" otherwise returns false.
///Returns Bool
///Usage - string.toBool()
///Author - BWM
String.prototype.toBool = function() {
    return (/^true$/i).test(this);
};

///Indicates whether a specified string is null, or empty.
///Returns Boolean
///Usage String.IsNullOrEmpty(stringToEvaluate)
///Author - BWM
String.IsNullOrEmpty = function(value) {
    if (typeof (value) == 'string' && value.length > 0) return false;
    return true;
}

///Indicates whether a specified string is null, empty, or consists only of white-space characters.
///Returns Boolean
///Usage String.IsNullOrWhiteSpace(stringToEvaluate)
///Author - BWM
String.IsNullOrWhiteSpace = function(value) {
    if (typeof (value) == 'string' && value.replace(/^\s+|\s+$/g, "").length > 0) return false;
    return true;
}

///Parses XML from string.
///Returns XML Document
///Usage var xml = parseXml("<foo>Stuff</foo>");
///Author - found on web. Added by BWM.
var parseXml;

if (window.DOMParser) {
    parseXml = function(xmlStr) {
        return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    parseXml = function() { return null; }
}
