﻿var msMonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function msOrdinal(i) {
	i = parseInt(i, 10);
	if (((i % 10) == 1) && ((i % 100) != 11)) return i + "st";
	else if (((i % 10) == 2) && ((i % 100) != 12)) return i + "nd";
	else if (((i % 10) == 3) && ((i % 100) != 13)) return i + "rd";
	return i + "th";
}

// aggiunge alcuni metodi alle classi base
String.prototype.strip = function() { return (m = this.match(/\s*(.+\w)\s*/)) ? m[1] : this; };
String.prototype.yesno = function() { return ((this == '0') ? 'no' : 'yes'); };
String.prototype.toShortDate = function() { return msMonth[parseInt(this.slice(3, 5)) - 1] + " " + msOrdinal(this.slice(0, 2)); };
Date.prototype.toShortDate = function() { return msMonth[this.getMonth()] + " " + msOrdinal(this.getDate()); };
Date.prototype.daysDiff = function(newDate) { return Math.round(Math.abs(this.getTime() - newDate.getTime()) / 86400000); };
Number.prototype.toCurrency = function() {
	var i = this, minus, rv;
	if (isNaN(this)) i = 0.00;
	minus = (i < 0) ? '-' : '';
	rv = (parseInt((Math.abs(i) + .005) * 100) / 100).toString();
	if (rv == 0) return "-.-- EUR";
	if (rv.indexOf('.') < 0) rv += '.00';
	if (rv.indexOf('.') == (rv.length - 2)) rv += '0';
	return minus + rv + " EUR";
}

// sistema subito la questione del metodo Array.indexOf
// che su quello schifo di Internet Explorer non c'è
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt)
	{
		var len = this.length >>> 0;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0) from += len;
		for (; from < len; from++) {
			if (from in this && this[from] === elt) return from;
		}
		return -1;
	};
}


//------------------------------------------------------------------------------------------
// micro-funzione per convertire stringhe in interi
function safeInt(v, defvalue) { var r = parseInt(v); return isNaN(r) ? (defvalue ? defvalue : 0) : r; }

//------------------------------------------------------------------------------------------
// micro-funzione per convertire stringhe in float
function safeFloat(v, defvalue) { var r = parseFloat(v.toString().replace(',', '.')); return isNaN(r) ? (defvalue ? defvalue : 0) : r; }

//------------------------------------------------------------------------------------------
// micro-funzione per convertire minuti in stringa hh:mm
function m2hm(v) { v = safeInt(v); m = v % 60; h = Math.floor(v / 60); return ((h > 0) ? (h + ' hours ') : '') + ((m > 0) ? (m + ' minutes') : ''); }


//------------------------------------------------------------------------------------------
// funzione di convalida codice fiscale
function isValidCF(cf)
{
	var i, c, l;
	var t2 = [1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23]

	cf = cf.toUpperCase();

	//1. controllo che sia lungo 16
	if (cf.length != 16) return false;

	//2. controllo sui caratteri pari
	c = 0
	for (i = 1; i <= 14; i += 2) {
		l = cf.charAt(i);
		if ((l >= '0') && (l <= '9')) c += parseInt(l);
		else if ((l >= "A") && (l <= "Z")) c += (cf.charCodeAt(i) - 65);
		else return false;
	}

	//3. controllo sui caratteri dispari
	for (i = 0; i <= 14; i += 2) {
		l = cf.charAt(i);
		if ((l >= "0") && (l <= "9")) c += t2[parseInt(l)];
		else if ((l >= "A") && (l <= "Z")) c += t2[cf.charCodeAt(i) - 65];
		else return false;
	}
	return ((cf.charCodeAt(15) - 65) == (c % 26));
}


//------------------------------------------------------------------------------------------
// estende jQuery per il metodo fillcombo per select
jQuery.fn.fillCombo = function(jsonURL, defval, deftext) {
	var _this = this;
	var rv;
	$.getJSON(jsonURL, {}, function(jsonData) {
		rv = _this.each(function() {
			obj = $(this);
			if (deftext) obj.append('<option value="' + defval + '">' + deftext + '</option>');
			for (i = 0; i < jsonData.length; i++) { 
				obj.append('<option value="' + jsonData[i].value + '">' + jsonData[i].text + '</option>');
			}
		});
	});
	return rv;
}


//--------------------------------------------------------------------
//query string parsing object
function QueryString() {

	this.params = {};
	
	this.get = function(key, defval) { var rv = this.params[key]; return ((rv == null) ? defval : rv); }
	this.contains = function(key) { return (this.params[key] != null); }
	
	var qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	// converti i + in spazi
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&');
	
	// estrae coppie a/v
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		if (pair.length == 2) this.params[decodeURIComponent(pair[0]).strip()] = decodeURIComponent(pair[1]);
	}
	
}

