Array.prototype.sum = function() {
    return (! this.length) ? 0 : this.slice(1).sum() +
    ((typeof this[0] == 'number') ? this[0] : 0);
};

function pause(millisecondi) {
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true) {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function goPage(url) {
	var obj = document.getElementById('goPage');
	window.location.href = url+'&page='+obj.value;
}

function setCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1) {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
 return "";
}

function swicz(id) {
    if(document.getElementById(id).style.display=='none') {
        document.getElementById(id).style.display = 'block';
    } else { document.getElementById(id).style.display = 'none'; }
}

function checkFieldLength(obj,mLength,target) {
  var cLength = $j(obj).val().length+1;
  if (cLength>=mLength) {
		obj.value = obj.value.substr(0,(mLength-1));
		$j('#'+target).html("Pozostało 0 znaków");
  } else { $j('#'+target).html("Pozostało "+(mLength-cLength)+" znaków"); }
}

function serializeArray(a) {
	var serializedString = '';
	var arrayLength = 0;
	for(var aKey in a) {
		//key definition
		if(aKey * 1 == aKey) { //is_numeric?
			//integer keys look like i:key
			serializedString += 'i:' + aKey + ';';
		}
		else {
			//string keys look like s:key_length:key;
			serializedString += 's:' + aKey.length + ':"' + aKey + '";';
		}

		//value definition
		if(a[aKey] * 1 == a[aKey]) {
			//integer value look like i:value
			serializedString += 'i:' + a[aKey] + ';';
		}
		else if(typeof(a[aKey]) == "string") {
			//string value look like s:key_length:value;
			serializedString += 's:' + a[aKey].length + ':"' + a[aKey] + '";';
		}
		else if(a[aKey] instanceof Array) {
			serializedString += serializeArray(a[aKey]);
		}
		arrayLength++;
	}
	serializedString = 'a:' + arrayLength + ':{' + serializedString + '}';

	return serializedString;
}

