﻿/*
  
  This file contains generic functions that are used throughout the LebTurf website.
  They may utilize jQuery, and as such should follow the jQuery include file wherever
  it is referenced.
  
*/
// Numeric only control handler
jQuery.fn.ForceNumericOnly =
    function() {
        return this.each(function() {
            $(this).keydown(function(e) {
                var key = e.charCode || e.keyCode || 0;
                // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
                return (
                    key == 8 ||
                    key == 9 ||
                    key == 46 ||
                    (key >= 37 && key <= 40) ||
                    (key >= 48 && key <= 57) ||
                    (key >= 96 && key <= 105) ||
                // allow or deny Ctrl+V (paste), Shift+Ins
                    ((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86)
                /* opera */
         || (e.shiftKey && key == 45)));
            })
        })
    };

// Strip html from the contents
    jQuery.fn.stripTags = function() { return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, '')); };

    function removeHTMLTags(htmlString) {
        if (htmlString) {
            var mydiv = document.createElement("div");
            mydiv.innerHTML = htmlString;

            if (document.all) // IE Stuff
            {
                return mydiv.innerText;

            }
            else // Mozilla does not work with innerText
            {
                return mydiv.textContent;
            }
        }
    }


    /* Client-side access to querystring name=value pairs
    Version 1.3
    28 May 2008
	
	License (Simplified BSD):
    http://adamv.com/dev/javascript/qslicense.txt
    */
    function Querystring(qs) { // optionally pass a querystring to parse
        this.params = {};

        if (qs == null) qs = location.search.substring(1, location.search.length);
        if (qs.length == 0) return;

        // Turn <plus> back to <space>
        // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
        qs = qs.replace(/\+/g, ' ');
        var args = qs.split('&'); // parse out name/value pairs separated via &

        // split out each name=value pair
        for (var i = 0; i < args.length; i++) {
            var pair = args[i].split('=');
            var name = decodeURIComponent(pair[0]);

            var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

            this.params[name] = value;
        }
    }

    Querystring.prototype.get = function(key, default_) {
        var value = this.params[key];
        return (value != null) ? value : default_;
    }

    Querystring.prototype.contains = function(key) {
        var value = this.params[key];
        return (value != null);
    }

    var autoPrint = false;
    
    function PrintUrl(href) {
        w = window.open(href, 'printwin', 'width=600,height=600');

        //if (window.print) w.print();

        w.onload = function() {
            
            this.focus();
            //if (window.print) w.print(); //w.print();
            // use window.onfocus because of issues in earlier versions of IE.
            //w.onfocus = function() { w.close(); };
            //this.close();
        }
        return false;
    }

    function PrintHtml(html) {
        w = window.open('', 'printwin', 'width=600,height=600');
        w.document.write(html);
        w.document.close();
        w.focus();
        if (window.print) w.print();
        // use window.onfocus because of issues in earlier versions of IE.
        w.onfocus = function() { w.close(); };
        w.close();
    }

    function PrintContentSection(a, head) {
        var sHtml = '<h1>' + head + '</h1>';
        var $c = $(a).parents('div.printable:first');
        //alert($c.html());
        h = $c.html();
        //alert(h);
        //var tag = 'p';
        //var re = new RegExp('(<a class="print">)(.*)(</a>)', 'g');
        //sHtml += h.replace(/(<a class="print">)(.*)(</a>)/g, '');
        PrintHtml(sHtml + h);
    }

