﻿$.setContext = function(/* fn, context, arg1, arg2, ... */) {
    var args = $.makeArray(arguments),
        fn = args.shift(),
        context = args.shift();
    return function() { fn.apply(context, $.makeArray(arguments).concat([this].concat(args))); };
};

function restripeTable($rows) {
    $rows.removeClass('alt-row');
    $.each($rows, function(i) {
        if (i % 2 != 0) {
            $(this).addClass('alt-row');
        }
    });
}

function formatCurrentDate() {
    var date = new Date();
    var day = date.getDate();
    var month = "" + (date.getMonth() + 1);
    var year = "" + date.getFullYear();

    if (month.length < 2) {
        month = "0" + month;
    }

    return day + "/" + month + "/" + year.substring(2, 4);
}

function charLimit(e, el, element) {
    var characters = element.value; // when using live() pass in the element clicked
    var charLen = characters.length;
    var maxLen = $(element).closest('.section').find('.section-head .total-char').text();
    maxLen = Number(maxLen);
    var threeQtrLen = Math.floor(maxLen * .75);

    if (charLen > maxLen) {
        $(element).closest('form').addClass('limit-reached');
        $(element).closest('.section').find('.section-head .limit-reached').removeClass('hidden');
        $(element).closest('.section').find('.section-controls .current-char').addClass('char-limit-reached');
        $(element).closest('.section').find('.section-controls .save').addClass('disabled');
    }

    if (charLen >= threeQtrLen) {
        $(element).closest('.section').find('.section-head .current-char').addClass('three-qtr-len');
    } else {
        $(element).closest('.section').find('.section-head .current-char').removeClass('three-qtr-len');
    }

    if (charLen <= maxLen) {
        $(element).closest('form').removeClass('limit-reached');
        $(element).closest('.section').find('.section-head .limit-reached').addClass('hidden');
        $(element).closest('.section').find('.section-head .current-char').removeClass('char-limit-reached');
        $(element).closest('.section').find('.section-controls .save').removeClass('disabled');
    }

    getCountChar(element, charLen);
}

function getCountChar(el, charLen) {
    var currentCharCount = $(el).closest('.section').find('.current-char');
    currentCharCount.html(charLen);
   }

   //from date.js

   function jsToMsDate(jsdate) {
   	var timezoneOffset = jsdate.getTimezoneOffset() / (60 * 24);
   	var msDateObj = (jsdate.getTime() / 86400000) + (25569 - timezoneOffset);
   	return msDateObj;
   }
