Array.prototype.index = function(val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == val) return i;
	}
	return null;
};
Array.prototype.include = function(val) {
	return this.index(val) !== null;
};
String.prototype.setCharAt = function(index,ch) {
	if (index > this.length - 1) return this;
	return this.substr(0,index) + ch + this.substr(index+1);
}
String.prototype.toUnaccented = function() {
	var accented_chars   = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýĄĆĘŁŃÓŚŻŹąćęłńóśżź";
	var unaccented_chars = "AAAAAACEEEEIIIIDNOOOOOxOUUUUYaaaaaaceeeeiiiinoooooouuuuyACELNOSZZacelnoszz";
	var value = this
	for(var i=0; i<accented_chars.length; i++) {
		var k = value.indexOf( accented_chars[i] );
		while (k >= 0) {
			value = value.setCharAt(k,unaccented_chars[i]);
			k = value.indexOf( accented_chars[i] );
		}
	}
	return value;
}
Date.setISO8601 = function(string) {
	if (!string) return null;
	var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
		"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
		"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
	var d = string.match(new RegExp(regexp));

	var offset = 0;
	var date = new Date(d[1], 0, 1);

	if (d[3]) { date.setMonth(d[3] - 1); }
	if (d[5]) { date.setDate(d[5]); }
	if (d[7]) { date.setHours(d[7]); }
	if (d[8]) { date.setMinutes(d[8]); }
	if (d[10]) { date.setSeconds(d[10]); }
	if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
	if (d[14]) {
		offset = (Number(d[16]) * 60) + Number(d[17]);
		offset *= ((d[15] == '-') ? 1 : -1);
	}

	offset -= date.getTimezoneOffset();
	time = (Number(date) + (offset * 60 * 1000));
	result = new Date();
	result.setTime(Number(time));
	return result;
};
Date.prototype.getWeek = function(dowOffset) {
	dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
	var newYear = new Date(this.getFullYear(),0,1);
	var day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	var daynum = Math.floor((this.getTime() - newYear.getTime() - 
	(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
	var weeknum;
	//if the year starts before the middle of a week
	if(day < 4) {
		weeknum = Math.floor((daynum+day-1)/7) + 1;
		if(weeknum > 52) {
			nYear = new Date(this.getFullYear() + 1,0,1);
			nday = nYear.getDay() - dowOffset;
			nday = nday >= 0 ? nday : nday + 7;
			weeknum = nday < 4 ? 1 : 53;
		}
	}
	else {
		weeknum = Math.floor((daynum+day-1)/7);
	}
	return weeknum;
};
Number.prototype.format = function( decimalPoints, thousandsSep, decimalSep) {
	var val = this + '', re = /^(-?)(\d+)/, x, y;
	if (decimalPoints != null) val = this.toFixed( decimalPoints );
	if (thousandsSep && (x = re.exec(val))) {
		for (var a = x[2].split(''), i = a.length-3; i > 0; i -= 3) 
			a.splice(i,0,thousandsSep);
		val = val.replace( re, x[1] + a.join(''));
	}
	if (decimalSep) val = val.replace(/\./, decimalSep);
	return val;
};
if (typeof Number.prototype.toFixed != 'function' || (.9).toFixed() == '0' || (.007).toFixed(2) == '0.00') Number.prototype.toFixed = function(f) {
	if (isNaN(f*=1) || f<0 || f>20) f = 0;
	var s = '', x = this.valueOf(), m = '';
	if (this < 0) { s = '-'; x *= -1; }
	if (x >= Math.pow(10,21))
		m = x.toString();
	else {
		m = Math.round( Math.pow(10,f) * x ).toString();
		if (f != 0) {
			var k = m.length;
			if (k <= f) {
				var z='00000000000000000000'.substring(0,f+1-k);
				m = z + m;
				k = f + 1;
			}
			var a = m.substring(0,k-f);
			var b = m.substring(k-f);
			m = a+'.'+b;
		}
	}
	if (m == '0') s = '';
	return s + m;
};

function remove_fields(link) {
	var fields = $(link).closest(".fields");
	var input_id = $(fields).find("input[id$='_id']");
	var input_destroy = $(link).prev("input[type=hidden]");
	var value_destroy = parseInt( $(input_destroy).val() );
	if ( $(input_id).length == 0 || $(input_id).val() == 0) {
		$(fields).remove();
	} else if (isNaN(value_destroy) || value_destroy == 0) {
		$(input_destroy).val("1");
		$(fields).addClass("remove");
		$(link).text("Przywróć");
	} else {
		$(input_destroy).val("0");
		$(fields).removeClass("remove");
		$(link).text("Usuń");
	}
	return false;
}
function add_fields(link, association, content, parent) {
	var new_id = new Date().getTime();
	var regexp = new RegExp("new_" + association, "g");
	if (parent == "") {
		$(link).parent().before(content.replace(regexp, new_id));
	} else {
		$(parent).append(content.replace(regexp, new_id));
	}
	return false;
}