function lib_view(name, search, restriction) {
	this.urlmatch = function(regexp, alternative) {
		var tmp = this.path.match(regexp);

		if (tmp) return tmp;
		else return alternative;
	}
	this.urlreplace = function(regexp, toreplace) {
		var tmp = this.path;

		tmp = (this.urlmatch(regexp)) ? tmp.replace(regexp, toreplace) : tmp+toreplace;

		return tmp;
	}
	this.open = function(value) {
		location.href = p.curr+value+"?OpenView";
	}
	this.restrict = function(value) {
		location.href = this.urlreplace(/&RestrictToCategory=[^&]*/gi, "")+((value) ? "&RestrictToCategory="+value : "");
	}
	this.collapse = function() {
		location.href = this.urlreplace(/&Expand[^&]*|&Collapse[^&]*/gi, "")+"&CollapseView";
	}
	this.expand = function() {
		location.href = this.urlreplace(/&Expand[^&]*|&Collapse[^&]*/gi, "")+"&ExpandView";
	}
	this.goTo = function() {
		location.href = this.urlreplace(/&Start=[^&]*/gi, "")+"&Start="+this.start.value;
	}
	this.back = function(start) {
		if (this.issearch) {
			if (this.iStart > 1) {
				var back = this.iStart-this.iCount;
				
				if (start) this.start.value = 1;
				else this.start.value = (back > 0) ? back : 1;

				this.goTo();
			}
		} else {
			if (start) {
				if (!this.isrestricted) scriptcode("p_sstartview");
			} else scriptcode("p_sback");
		}
	}
	this.next = function(end) {
		if (this.issearch) {
			if (end) {
				this.start.value = ((Math.ceil(this.iTotalHits/this.iCount)-1)*this.iCount)+1;

				this.goTo();
			} else {
				var next = this.iStart+this.iCount;
				if (next <= this.iTotalHits) {
					this.start.value = next;
					this.goTo();
				}
			}
		} else {
			if (end) {
				if (!this.isrestricted) scriptcode("p_sendview");
			} else scriptcode("p_snext");
		}
	}
	this.search = function(close) {
		if (close) location.href = p.curr+this.name+"?OpenView";
		else {
			if (!this.query.value) alert("Attenzione: query non specificata!");
			else {
				this.start.value = 1;
				location.href = p.curr+p.vw.name+"?SearchView"+
					"&Query="+this.query.value+
					"&Start="+this.start.value+
					"&Count="+this.count.value+
					"&SearchWv="+this.searchwv.value+
					"&SearchMax="+this.searchmax.value+
					"&SearchOrder="+this.searchorder.value;
			}
		}
	}
	this.searchpage = function(page, create) {
		if (create) {
			var i, limitpage, npage, currentpage, startpage, entry;

			if (!this.loaded) this.load();

			limitpage = 9;
			npage = Math.ceil(this.iTotalHits/this.iCount);
			currentpage = Math.ceil(this.iStart/this.iCount);
			startpage = currentpage-Math.ceil(limitpage/2)+1;
			startpage = (startpage<1) ? 1 : startpage;

			for (i=startpage;i<=npage && i<(startpage+limitpage);i++) {
				if (i==currentpage) document.write('<b>'+i+'</b> ');
				else document.write('<a href="" onclick="p.vw.searchpage('+((this.iCount*(i-1))+1)+'); return false;">'+i+'</a> ');
			}
		} else {
			this.start.value = page;
			this.goTo();
		}	
	}
	this.action = function(actions, IDs) {
		var obj, action, UNIDs;

		action = actions.match(/[^,]*/);

		if (action=="updatefullindex") {
			with (window.iLog) {
				document.forms[0].Actions.value = actions;
				scriptcode("p_s1");
			}	
		} else if (obj = document.forms[0].elements.$$SelectDoc) {
			if (action=="select") {
				for (var i=0;i<obj.length;i++) {
					if (obj[i].value==arguments[1]) obj[i].checked = arguments[2];
				}
			} else {
				if (IDs) {
					UNIDs = IDs.split(",");
				} else {
					UNIDs = [];

					if (obj.length) {
						for (var i=0;i<obj.length;i++) {
							if (obj[i].checked) UNIDs[UNIDs.length] = obj[i].value;
						}
					} else if (obj.checked) UNIDs[UNIDs.length] = obj.value;
				}

				if (!UNIDs[0]) alert("Attenzione: nessun documento selezionato!");
				else {
					if (action=="delete" && !confirm("Attenzione: facendo click su OK i documenti selezionati verranno cancellati. Continuare?")) return;

					with (window.iLog) {
						document.forms[0].Actions.value = actions;
						document.forms[0].UNIDs.value = UNIDs.join(",");
						scriptcode("p_s1");
					}
				}
			}
		}
	}
	this.load = function() {
		with (document.forms[0]) {
			this.query = elements.Query;
			this.start = elements.Start;
			this.count = elements.Count;
			this.searchwv = elements.SearchWv;
			this.searchmax = elements.SearchMax;
			this.searchorder = elements.SearchOrder;
			this.hits = elements.Hits;
			this.totalhits = elements.TotalHits;
		}

		if (this.start) this.iStart = (parseInt(this.start.value)==0) ? 1 : parseInt(this.start.value);
		if (this.count) this.iCount = (parseInt(this.count.value)==0) ? parseInt(this.totalhits.value) : parseInt(this.count.value);
		if (this.totalhits) this.iTotalHits = parseInt(this.totalhits.value);	

		this.loaded = 1;
	}
	this.parse = function() {
		var obj = p.bw.getById("p_vw_records");

		if (obj) {
			var i, j, objcoll, imgArr;
			imgArr = [
				["/icons/expand.gif", "/ktn/images/view/nav_expand.gif", 15, 15, "Expand"],
				["/icons/collapse.gif", "/ktn/images/view/nav_collapse.gif", 15, 15, "Collapse"]
			]

			objcoll = document.images;
			for (i=0;i<objcoll.length;i++) {
				for (j=0;j<imgArr.length;j++) {
					if (objcoll[i].src.match(imgArr[j][0])) {
						if (imgArr[j][1]!=null) objcoll[i].src = imgArr[j][1];
						if (imgArr[j][2]!=null) objcoll[i].style.width = imgArr[j][2]+"px";
						if (imgArr[j][3]!=null) objcoll[i].style.height = imgArr[j][3]+"px";
						if (imgArr[j][4]!=null) objcoll[i].alt = imgArr[j][4];
					}
				}
			}

			objcoll = p.bw.getByName("$$SelectDoc");
			for (i=0;i<objcoll.length;i++) {
				switch (objcoll[i].type) {
				case "checkbox":
					j = "checkbox";
				break;
				default:
					j = "button";
				}
				objcoll[i].parentNode.className = j;
			}

			//objcoll = p.bw.getByName("select");
			//for (i=0;i<objcoll.length;i++) {
			//	objcoll[i].parentNode.className = "select";
			//}

			objcoll = p.bw.getByTagName("H2");
			for (i=0;i<objcoll.length;i++) {
				if (objcoll[i].innerHTML.match(/No Documents Found/i)) {
					objcoll[i].className = "NoDocuments";
					objcoll[i].innerHTML = (this.totalhits) ? "Nessun elemento trovato" : "Nessun elemento disponibile";
				}
			}

			if (!this.loaded) this.load();
		}
	}

	this.name = name;
	this.issearch = search;
	this.isrestricted = restriction;
	this.path = location.href;
	if (!this.issearch) this.path = this.urlreplace(/[?|!]Open\w*/i, this.urlmatch(/[?|!]Open\w*/i, "?Open"));

	p.bw.addEvent(window, "load", new Function("p.vw.parse()"));
}