﻿jQuery.ajaxSettings.traditional = true;
(function (jQuery) {
	jQuery.fn.createValidationMessage = function (msg) {
		jQuery(this).after('<span class="validation">' + msg + '</span>');
		jQuery(this).next().show('fast');
	}

	jQuery.fn.destroyValidationMessage = function () {
		jQuery(this).siblings('span.validation').remove();
	}

	jQuery.fn.check = function (doCheck) {
		var input = $(this);
		if (input.is(':checkbox')) {
			if (doCheck) {
				input.attr('checked', 'checked');
			} else {
				input.removeAttr('checked');
			}
		}
	}

	jQuery.readCookie = function (cookieName) {
		var value = null;
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = jQuery.trim(cookies[i]);
			if (cookie.substring(0, cookieName.length + 1) == cookieName + '=') {
				value = decodeURIComponent(cookie.substring(cookieName.length + 1));
				break;
			}
		}
		return value;
	}

	jQuery.addCookie = function (cookieName, cookieValue, days) {
		var expires = '';
		if (days) {
			var expiryDate = new Date();
			expiryDate.setTime(expiryDate.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = ';expires=' + expiryDate.toUTCString();
		}
		document.cookie = cookieName + '=' + encodeURIComponent(cookieValue) + expires + '; path=/';
	}

	jQuery.removeCookie = function (cookieName) {
		var date = new Date();
		date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
		var expires = 'expires=' + date.toUTCString();
		document.cookie = cookieName + '=;' + expires + ';path=/';
	}

})(jQuery);

Date.prototype.formatDate = function (yearDigits) {
	var day = this.getDate();
	var month = this.getMonth();
	var year = this.getFullYear();
	var yearDigitsToShow = yearDigits ? 4 - yearDigits : 2;
	return month + 1 + '/' + day + '/' + year.toString().substring(yearDigitsToShow);
}

//Add a .format property to the string object
Number.prototype.numberFormat = String.prototype.numberFormat = function (decimals) {
	var val = this.toString();
	var bigbit = val.split('.');
	var a = bigbit[0];
	var b = '';
	if (bigbit.length > 1) {
		var padded = bigbit[1];
		if (decimals) {
			while (padded.length < decimals) { padded = padded + '0'; }
		}
		b = '.' + padded;
	}
	var reg = /(\d+)(\d{3})/;
	while (reg.test(a)) {
		a = a.replace(reg, '$1' + ',' + '$2');
	}
	return a + b;
}

Array.max = function (array) {
	return Math.max.apply(Math, array);
};
Array.min = function (array) {
	return Math.min.apply(Math, array);
};

function capitalize(str) {
	return str.charAt(0).toUpperCase() + str.slice(1);
}

function selectText(element) {
	if ($.browser.msie) {
		var range = document.body.createTextRange();
		range.moveToElementText(element);
		range.select();
	} else if ($.browser.mozilla || $.browser.opera) {
		var selection = window.getSelection();
		var range = document.createRange();
		range.selectNodeContents(element);
		selection.removeAllRanges();
		selection.addRange(range);
	} else if ($.browser.safari) {
		var selection = window.getSelection();
		selection.setBaseAndExtent(element, 0, element, 1);
	}
}
