﻿/* Protos */

String.prototype.trimStart = function (c) {
    if (this.length == 0) return this;
    c = c ? c : ' ';
    var i = 0;
    var val = 0;
    for (; this.charAt(i) == c && i < this.length; i++);
    return this.substring(i);
}

String.prototype.trimEnd = function (c) {
    c = c ? c : ' ';
    var i = this.length - 1;
    for (; i >= 0 && this.charAt(i) == c; i--);
    return this.substring(0, i + 1);
}

String.prototype.trim = function (c) {
    return this.trimStart(c).trimEnd(c);
}

String.prototype.leadingUpper = function () {
    var result = "";
    if (this.length > 0) {
        result += this[0].toUpperCase();
        if (this.length > 1) result += this.substring(1, this.length);
    }
    return result;
}

/* Functions */

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 _CreateOneDayCookie(key, val) {
    var cdate = new Date();  // current date & time
    cdate.setDate(cdate.getDate() + 1);
    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 _FBModal(src) {
    $.fancybox(src, {
        'autoScale': true
        , 'autoDimensions': true
        , 'padding': 10
        , 'modal' : true
    });
    $.fancybox.resize();
}

function _FBOpen(src) {
    $.fancybox(src, {
        'autoScale': true
        , 'autoDimensions' : true
        , 'padding' : 10
    });
    $.fancybox.resize();
}

function _FBScroll(src) {
    $.fancybox(src, {
        'autoScale': true
        , 'autoDimensions': false
        , 'width' : 660
        , 'height' : 340
        , 'padding': 10
        , 'scrolling' : 'yes'
    });
    $.fancybox.resize();
}

function _FBClose() {
    $.fancybox.close();
}

function _GetRootUrl() {
    var url = window.location.href;
    var qmIndex = url.indexOf("?");
    if (qmIndex >= 0) {
        url = url.substring(0, qmIndex);
    }
    return url;
}

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://www.redips.net/javascript/date-validation/ */
function _IsDate(txtDate) {
    var objDate,  // date object initialized from the txtDate string
    mSeconds, // txtDate in milliseconds
    day,      // day
    month,    // month
    year;     // year

    // date length should be 10 characters (no more no less)
    if (txtDate.length !== 10) {
        return false;
    }
    // third and sixth character should be '/'
    if (txtDate.substring(2, 3) !== '/' || txtDate.substring(5, 6) !== '/') {
        return false;
    }
    // extract month, day and year from the txtDate (expected format is mm/dd/yyyy)
    // subtraction will cast variables to integer implicitly (needed
    // for !== comparing)
    month = txtDate.substring(0, 2) - 1; // because months in JS start from 0
    day = txtDate.substring(3, 5) - 0;
    year = txtDate.substring(6, 10) - 0;
    // test year range
    if (year < 1000 || year > 3000) {
        return false;
    }
    // convert txtDate to milliseconds
    mSeconds = (new Date(year, month, day)).getTime();
    // initialize Date() object from calculated milliseconds
    objDate = new Date();
    objDate.setTime(mSeconds);
    // compare input date and parts from Date() object
    // if difference exists then date isn't valid
    if (objDate.getFullYear() !== year ||
objDate.getMonth() !== month ||
objDate.getDate() !== day) {
        return false;
    }
    // otherwise return true
    return true;
}

/* 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));
}

/* http://www.quirksmode.org/js/cookies.html */
function _ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

function _SetUrlEncodedKey(key, value, query) {
    var q = new String();
    q = query + "&";
    var re = new RegExp("[?|&]" + key + "=.*?&");
    if (!re.test(q)) {
        q += key + "=" + encodeURI(value);
    }
    else {
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    }

    // trim ending apos
    var i = q.length - 1;
    for (; i >= 0 && q.charAt(i) == '&'; i--);
    q = q.substring(0, i + 1);

    return q[0] == "?" ? q : q = "?" + q;
}

function _ShowHide(o) {
    var elem = $("#" + o);
    if (elem) {
        if (elem.is(":visible")) {
            elem.slideUp("normal", function () {
                // Animation complete.
            });
        }
        else {
            elem.slideDown("normal", function () {
                // Animation complete.
            });
        }
    }
}

function CheckNull(str) {
    return ((str == null) ? "" : str);
}

