﻿$(document).ready(function() {
    /* Focus and Exit Form Fields  */
    $("input[example]").live("focus", function() {
        var ex = $(this).attr("example");
        if (ex == $(this).val()) {
            $(this).val("");
        }
    });
    $("input[example]").live("blur", function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("example"));
        }
    });

    /* TextArea MaxLength  */
    $("textarea[maxlength]").live("keyup", function() {
        var maxLength = $(this).attr("maxlength");
        $(this).val($(this).val().substring(0, maxLength));
    });

    $(".dt").datepicker({ duration: 100 });
    $(".num").keypress(function(e) { return numbersOnly(e); });
    
});






function salt() {
    var r = Math.floor(Math.random() * 5001);
    return "&salt=" + r;
}


function isArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1)
        return false;
    else
        return true;
}

//========== input masks =================
function noNumbers(e) {
    var keynum
    var keychar
    var numcheck
    if (window.event) // IE
    {
        keynum = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    keychar = String.fromCharCode(keynum);
    numcheck = /\D/;
    return numcheck.test(keychar);
}


function numbersOnly(e) {
    
    var key
    var keychar
    var numcheck
    if (window.event) // IE
    {
        key = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        key = e.which
    }
    else
        return true;

    keychar = String.fromCharCode(key);
    
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    else
        return false;

}

function floatpt(e) {

    var key
    var keychar
    var numcheck
    if (window.event) // IE
    {
        key = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        key = e.which
    }
    else
        return true;

    keychar = String.fromCharCode(key);

    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    else if ((("-.0123456789").indexOf(keychar) > -1))
        return true;

    else
        return false;

}
function phoneOnly(e) {
    var key
    var keychar
    var numcheck
    if (window.event) // IE
    {
        key = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        key = e.which
    }
    else
        return true;

    keychar = String.fromCharCode(key);
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    else if ((("0123456789-()").indexOf(keychar) > -1))
        return true;

    else
        return false;

}

function noSpaces(e) {
    var keynum
    var keychar
    var numcheck
    if (window.event) // IE
    {
        keynum = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    keychar = String.fromCharCode(keynum);
    numcheck = /\ /;
    return !numcheck.test(keychar);
}

function getBrowserPosition() {
    var intH = 0, intW = 0;

    if (self.innerHeight) {
        intH = window.innerHeight;
        intW = window.innerWidth;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            intH = document.documentElement.clientHeight;
            intW = document.documentElement.clientWidth;
        }
        else {
            if (document.body) {
                intH = document.body.clientHeight;
                intW = document.body.clientWidth;
            }
        }
    }

    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {
        offx: scrOfX,
        offy: scrOfY
    };
}
function centerElement(elem) {
    var viewport = getBrowserPosition();
    var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
    var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);

    elem.style.left = left + 'px';
    elem.style.top = top + 'px';

    viewport, left, top, elem = null;
}

function urlencode(str) {
    var hexStr = function(dec) { return '%' + dec.toString(16).toUpperCase(); };
    var ret = '', unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str + '').toString();
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) { ret += ch; }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) { ret += '+'; } // %20 in rawurlencode
            else if (code < 128) { ret += hexStr(code); } // 1 byte
            else if (code >= 128 && code < 2048) { ret += hexStr((code >> 6) | 0xC0); ret += hexStr((code & 0x3F) | 0x80); } // 2 bytes
            else if (code >= 2048 && code < 65536) { ret += hexStr((code >> 12) | 0xE0); ret += hexStr(((code >> 6) & 0x3F) | 0x80); ret += hexStr((code & 0x3F) | 0x80); } // 3 bytes
            else if (code >= 65536) { ret += hexStr((code >> 18) | 0xF0); ret += hexStr(((code >> 12) & 0x3F) | 0x80); ret += hexStr(((code >> 6) & 0x3F) | 0x80); ret += hexStr((code & 0x3F) | 0x80); } // 4 bytes
        }
    }
    return ret;
}


location.querystring = (function() {
    // The return is a collection of key/value pairs
    var queryStringDictionary = {};
    // Gets the query string, starts with '?'
    var querystring = decodeURI(location.search);
    // document.location.search is empty if no query string
    if (!querystring) {
        return {};
    }
    // Remove the '?' via substring(1)
    querystring = querystring.substring(1);
    // '&' seperates key/value pairs
    var pairs = querystring.split("&");
    // Load the key/values of the return collection
    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]]
                = keyValuePair[1];
    }
    // toString() returns the key/value pairs concatenated
    queryStringDictionary.toString = function() {
        if (queryStringDictionary.length == 0) {
            return "";
        }
        var toString = "?";
        for (var key in queryStringDictionary) {
            toString += key + "=" +
                queryStringDictionary[key];
        }
        return toString;
    };
    // Return the key/value dictionary
    return queryStringDictionary;
})();

// This is how to call it in your script
// (If "q" isn't there, val is undefined):
//var val = location.querystring["q"];

// This is how to roll out all of them:
//for (var key in location.querystring) {
//    alert(key + "=" + location.querystring[key]);
//}

// this is how to get the whole thing (incl. the ?):
//alert(window.location.querystring.toString());



