﻿////////////////////////////////////////////////////////////////
//--------------------------------------------------------------
// BEGIN COMMON METHODS
//--------------------------------------------------------------
function QUERY_STRING(key) {
    query = window.location.search.substring(1);
    pairs = query.split("&");
    for (i = 0; i < pairs.length; i++) {
        pair = pairs[i].split("=");
        if (pair[0] == key) {
            return urlDecode(pair[1]);
        }
    }
}

function urlDecode(value) {
    if (value)
        return unescape(value.replace(/\+/g, " "));
    return value;
}

function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}

function EscapeSpecialChar(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_-~]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            x++;
        }
    }
    return output;
}
function toTitleCase(s) {
    if ("[BMW] [GMC]".indexOf(s) >= 0)
        return s;

    var words = s.split(" ");

    for (var i = 0; i < words.length; i++) {
        var w = words[i];

        if (w != " ") {
            w = w.toLowerCase();
            w = w.substr(0, 1).toUpperCase() + w.substr(1);
        }

        for (var j = 1; j < w.length; j++)
            if (w[j - 1] == '-' || (w[j - 1] >= '0' && w[j - 1] <= '9'))
            w = w.replace(w[j], w[j].toUpperCase());

        words[i] = w;
    }

    return words.join(" ");
}

//check if str2 is a sub-string of str1.
function InStr(str1, str2) {
    var n = str1.toString().indexOf(str2, 0);
    return n >= 0;
}

function ToggleElementDisplay(element) {
    element = document.getElementById(element);
    if (element.style.display == "") {
        element.style.display = "none";
    }
    else {
        element.style.display = "";
    }
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}
//--------------------------------------------------------------
// END COMMON METHODS
