﻿/*
* Utils for jQuery - arunes
*
* Copyright (c) 2010 Ali Riza Unes (arunes.com/jqueryutils)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* $Date: 2010-03-03 00:16:33 +0200 (Wed, 03 Mar 2010) $
*/

function arunesJSLib($) {
    this.isInFrame = function () {
        return !(top === self);
    }

    this.queryString = function (key) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (i = 0; i < vars.length; i++) {
            var cVar = vars[i].split('=');
            if (cVar[0] == key) {
                return cVar[1];
            }
        }
    }

    this.isEnterKey = function (e) {
        var k = e.keyCode || e.which;
        return k == 13;
    }

    this.getPageUrl = function (withQuery) {
        var fullUrl = document.location.toString();
        var lastPart = fullUrl.split('/')[fullUrl.split('/').length - 1];
        var plainUrl = lastPart.split('?')[0];
        return withQuery ? lastPart : plainUrl;
    }

    this.addToQueryString = function (key, value, queryString) {
        if (queryString == null) {
            if (document.location.href.split('?')[1] != null) {
                queryString = document.location.href.split('?')[1];
            }
        }

        var isExists = 0; var iFound = 0;
        if (queryString != "" && queryString != null) {
            isExists = queryString.indexOf(key);
            if (isExists > -1) {
                var splitUrl = queryString.split('&');

                if (splitUrl.length > 0) {
                    for (i = 0; i < splitUrl.length; i++) {
                        if (splitUrl[i].substring(0, key.length) == key)
                            iFound = i;
                    }
                    return "?" + queryString.replace(splitUrl[iFound], key + "=" + value);
                } else {
                    return "?" + key + "=" + value;
                }
            } else {
                return "?" + queryString + "&" + key + "=" + value;
            }
        } else {
            return "?" + key + "=" + value;
        }
    }

    this.openPopupWindow = function (url, windowName, width, height, properties) {
        var w = screen.width;
        var h = screen.height - 50;
        var popW = width, popH = height;
        var leftPos = (w - popW) / 2, topPos = (h - popH) / 2;
        var wnd = window.open(url, windowName, 'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',' + properties);
        return wnd;
    }

    this.trim = function (str, chr) {
        var trimChar = chr ? chr : "\s";
        var regex = '/^' + trimChar + '+|' + trimChar + '+$/g';
        return eval('str.replace(' + regex + ', "")');
    }

    this.ltrim = function (str, chr) {
        var trimChar = chr ? chr : "\s";
        var regex = '/^' + trimChar + '+/';
        return eval('str.replace(' + regex + ', "")');
    }

    this.rtrim = function (str, chr) {
        var trimChar = chr ? chr : "\s";
        var regex = '/' + trimChar + '+$/';
        return eval('str.replace(' + regex + ', "")');
    }

}

(function ($) { $.utils = new arunesJSLib($); })(jQuery);

$.fn.clearForm = function () {
    return this.each(function () {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};
