﻿///This is a place to put your general purpose functions that are NOT SITE SPECIFIC.
///Think of this as a toolbelt that can be used on any site.

///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;
}

///Trims leading and trailing whitespace from the string.
///Returns string
///Usage - string.trim()
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

///Trims leading whitespace from the string.
///Returns string
///Usage - string.lTrim()
String.prototype.lTrim = function() {
    return this.replace(/^\s+/,"");
}

///Trims trailing whitespace from the string.
///Returns string
///Usage - string.rTrim()
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);
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)
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);
}

///Indicates whether a specified string is null, or empty.
///Returns Boolean
///Usage String.IsNullOrEmpty(stringToEvaluate)
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)
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>");
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; }
}
