﻿function Trim(str) {
    try {
        if (!str || str == 'undefined' || str == null) {
            return '';
        } else {
            str = String(str);
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }
    } catch (e) {
        return '';
    }
}

function isValidEmail(address) {
    return (address.indexOf('.') > 2) && (address.indexOf('@') > 0);
}

function Request() {
    var _this = this;
    var search = window.location.search;

    this._PageQuery = function (q) {
        if (String(this.q) == 'undefined') { this.q = ''; }

        if (q.length > 1) {
            this.q = q.substring(1, q.length);
        }
        else {
            this.q = null;
        }

        this.keyValuePairs = new Array();

        if (q) {
            if (this.q) {
                for (var i = 0; i < this.q.split("&").length; i++) {
                    this.keyValuePairs[i] = this.q.split("&")[i];
                }
            }
        }

        this.getKeyValuePairs = function () { return this.keyValuePairs; }

        this.getValue = function (s) {
            for (var j = 0; j < this.keyValuePairs.length; j++) {
                if (this.keyValuePairs[j].split("=")[0] == s)
                    return this.keyValuePairs[j].split("=")[1];
            }
            return '';
        }

        this.getParameters = function () {
            var a = new Array(this.getLength());
            for (var j = 0; j < this.keyValuePairs.length; j++) {
                a[j] = this.keyValuePairs[j].split("=")[0];
            }
            return a;
        }

        this.getLength = function () { return this.keyValuePairs.length; }
    }

    this.QueryString = function (key) {
        if (search == null) search = '';
        if (key == null) key = '';
        var page = new _this._PageQuery(search);
        return unescape(page.getValue(key));
    }
}

function getElementByPartialId(tag, id) {
    var items = document.getElementsByTagName(tag);

    for (var i = 0; i < items.length; i++) {
        if (items[i].id.indexOf(id) != -1)
            return items[i];
    }
    return null;
}

