﻿//// thx to http://www.west-wind.com/weblog/posts/884279.aspx

//String.prototype.zTrimEnd = function (c) {
//    if (c)
//        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
//    return this.replace(/\s+$/, '');
//}
//String.prototype.zTrimStart = function (c) {
//    if (c)
//        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
//    return this.replace(/^\s+/, '');
//}

//String.prototype.escapeRegExp = function () {
//    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
//};

function CreateCookie(key, val) {
    var cdate = new Date();  // current date & time
    cdate.setDate(cdate.getDate() + 7);
    var c = key + "=" + escape(val) + "; path=/; expires=" + cdate.toGMTString();
    document.cookie = c;
}

function DeleteCookie(key) {
    var cdate = new Date();  // current date & time
    cdate.setDate(cdate.getDate() - 3);
    document.cookie = key + "=; path=/; expires=" + cdate.toGMTString();
}

function getUrlEncodedKey (key, query) {
    if (!query)
        query = window.location.search;
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
}

/* http://stackoverflow.com/questions/487073/jquery-check-if-element-is-visible-after-scroling*/
function IsScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
    && (elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}


function setUrlEncodedKey (key, value, query) {
query = query || window.location.search;
var q = query + "&";
var re = new RegExp("[?|&]" + key + "=.*?&");
if (!re.test(q))
q += key + "=" + encodeURI(value);
else
q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
//q = q.zTrimStart("&").zTrimEnd("&");
return q[0] == "?" ? q : q = "?" + q;
}

/*
hs = HideStatus
Hides the window status
*/
function hs() {
    window.status = "";
    return true;
}
