/***
 * Creates new prospitalia object
 */
function prospitalia() {
	this.root			= '';
	this.session		= '';

	this.pageLoaded		= false;
	this.diffListHeight	= 0;
	this.menu			= '';
	this.title			= '';

	this.ajax = new ajax();
	this.ajax.parent = this;
};

// Set root
prospitalia.prototype.setRoot = function (strRoot) {
	if(typeof strRoot == 'string') {
		this.root = strRoot;
	}
};

// Returns root
prospitalia.prototype.getRoot = function () {
	return this.root;
};

// Set session
prospitalia.prototype.setSession = function (strSession) {
	if(typeof strSession == 'string') {
		this.session = strSession;
	}
};

// Returns session
prospitalia.prototype.getSession = function () {
	return this.session;
};

// Set title
prospitalia.prototype.setTitle = function (title) {
	this.title = title;
	window.status = this.title;
};

// Returns session
prospitalia.prototype.getTitle = function () {
	return this.title;
};

// Set page loaded flag
prospitalia.prototype.setPageLoaded = function (value) {
	this.pageLoaded = value;
};

// Returns page loaded flag
prospitalia.prototype.isPageLoaded = function () {
	return this.pageLoaded;
};

prospitalia.prototype.go = function (url) {
	window.parent.location = url;
};

// Switch loading box
prospitalia.prototype.switchLoading = function (show) {
	var lay = GetLayer('boxLoading');
	var body = document.getElementsByTagName("body")[0];
	if(lay && body) {
		if(show == '1' || show == 1) {
			var bodyWidth = parseInt(GetWidth(body));
			var bodyHeight = parseInt(GetHeight(body));
			
			var loadWidth = parseInt(GetStyle(lay, 'width'));
			var loadHeight = parseInt(GetStyle(lay, 'height'));

			SetStyle(lay, 'left', (bodyWidth/2-loadWidth/2) + 'px');
			SetStyle(lay, 'top', (bodyHeight/2-loadHeight/2) + 'px');
	
			SwitchLayer(lay, 1);
		} else {
			SwitchLayer(lay, 0);
		}
	}
};

// Load box
prospitalia.prototype.load = function (target, query) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.load(target, query); }, 10);
	} else {
		this.ajax.load(target, this.getRoot() + 'load.php?' + this.getSession() + '&type=box' + query);
	}
};

// Send box
prospitalia.prototype.send = function (target, query, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.send(target, query, formname); }, 10);
	} else {
		this.ajax.send(target, this.getRoot() + 'load.php', this.getSession() + '&type=box' + query, formname);
	}
};

// Finish upload
prospitalia.prototype.finishUpload = function (progressId) {
	var current = this;
	
	this.ajax._closeUploadStatus(progressId);
};

// Open detail box
prospitalia.prototype.openDetail = function (query) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.openDetail(query); }, 10);
	} else {
		this.ajax.load('boxDetail', this.getRoot() + 'load.php?' + this.getSession() + '&type=box' + query);
		//if(Detail) Detail.showBox();
	}
};

// Send detail box
prospitalia.prototype.sendDetail = function (query, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.sendDetail(query, formname); }, 10);
	} else {
		this.ajax.send('boxDetail', this.getRoot() + 'load.php', this.getSession() + '&type=box' + query, formname);
		//if(Detail) Detail.showBox();
	}
};

// Close detail box
prospitalia.prototype.closeDetail = function () {
	if(Detail) Detail.closeBox();
};

// Open list
prospitalia.prototype.openList = function (action, query) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.openList(action, query); }, 100);
	} else {
		this.ajax.load('boxList', this.getRoot() + 'load.php?' + this.getSession() + '&action=' + action + '&type=list' + query);
	}
};

// Open list
prospitalia.prototype.switchPersons = function (img, action, objectPk) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.switchPersons(img, action, objectPk); }, 10);
	} else {
		var lay = GetLayer('boxPersons'+objectPk);
		var on = GetStyle(lay, 'display');

		if(on == 'none') {
			SwitchLayer(lay, 1);
			if(img) { img.src = img.src.replace(/close/g, 'open'); }
			
			var content = GetContent(lay);
			if(content == '') {
				this.ajax.load('boxPersons'+objectPk, this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=persons&mode=' + action + '&objectPk=' + objectPk);
			}
		} else {
			SwitchLayer(lay, 0);
			if(img) { img.src = img.src.replace(/open/g, 'close'); }
		}
	}
};

// Open list
prospitalia.prototype.switchBulletBox = function (id) {
	var img = GetImage('img' + id);
	var lay = GetLayer('div' + id);
	var on = GetStyle(lay, 'display');

	if(on == 'none') {
		SwitchLayer(lay, 1);
		if(img) { img.src = img.src.replace(/plus/g, 'minus'); }
	} else {
		SwitchLayer(lay, 0);
		if(img) { img.src = img.src.replace(/minus/g, 'plus'); }
	}
};

// Do search
prospitalia.prototype.doSearch = function (action, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.doSearch(action, formname); }, 10);
	} else if(document.forms[formname] && document.forms[formname].search && document.forms[formname].filtermethods) {
		var searchQuery = document.forms[formname].query.value;
		var searchValue = document.forms[formname].search.value;
		var searchFiltermethods = document.forms[formname].filtermethods.value;
		var searchFilterphonetics = document.forms[formname].filterphonetics.value;
		var query = searchQuery + '&type=list&list[0]=filtermethods-' + encodeURI(searchFiltermethods) + '&list[1]=filterphonetics-' + encodeURI(searchFilterphonetics) + '&list[2]=filter-' + encodeURI(searchValue);

		this.openList(action, query);
	}
};

// Do search
prospitalia.prototype.doSearchWithSupplier = function (action, formname, supplierPk) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.doSearch(action, formname); }, 10);
	} else if(document.forms[formname] && document.forms[formname].search && document.forms[formname].filtermethods) {
		var searchQuery = document.forms[formname].query.value;
		var searchValue = document.forms[formname].search.value;
		var searchFiltermethods = document.forms[formname].filtermethods.value;
		var searchFilterphonetics = document.forms[formname].filterphonetics.value;
		var query = searchQuery + '&type=list&list[0]=filtermethods-' + encodeURI(searchFiltermethods) + '&list[1]=filterphonetics-' + encodeURI(searchFilterphonetics) + '&list[2]=filter-' + encodeURI(searchValue) + '&supplier=' + supplierPk;

		this.openList(action, query);
	}
};

// Do search
prospitalia.prototype.doClassification = function (action, formname) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.doClassification(action, formname); }, 10);
	} else if(document.forms[formname] && document.forms[formname].elements["classification[]"]) {
		var query = '&type=list';
		var searchClassificationField = document.forms[formname].elements["classification[]"];
		var txtClassification = '';

		if(searchClassificationField.length) {
			var v=0;
			for(var i=0;i<searchClassificationField.length;i++) {
				if(searchClassificationField[i].checked) {
					txtClassification += ((txtClassification != '') ? ', ' : '') + searchClassificationField[i].getAttribute('description', 0);
					query += '&classification[' + v + ']=' + encodeURI(searchClassificationField[i].value);
					v++;
				}
			}
		} else if(searchClassificationField.checked) {
			txtClassification += ((txtClassification != '') ? ', ' : '') + searchClassificationField.getAttribute('description', 0);
			query += '&classification[0]=' + encodeURI(searchClassificationField.value);
		}

		if(document.forms['FORMCLASSIFICATION'].elements['classification']) {
			document.forms['FORMCLASSIFICATION'].elements['classification'].value = txtClassification;
		}

		this.openList(action, query);
	}
};

// Set search value
prospitalia.prototype.setSearchValue = function (val, formname) {
	if(document.forms[formname] && document.forms[formname].search) {
		document.forms[formname].search.value = val;
	}
};

// Set search
prospitalia.prototype.setSearch = function (targetFormName, sourceFormName) {
	var targetForm = document.forms[targetFormName];
	var sourceForm = document.forms[sourceFormName];
	
	if(!targetForm || !sourceForm)
		return;
	
	if(sourceForm.phonetics.checked == true) {
		targetForm.filterphonetics.value = 'true';
	} else {
		targetForm.filterphonetics.value = 'false';
	}
	
	var newMethods = new Array();
	var methods = sourceForm.elements;
	for(var i=0;i<methods.length;i++) {
		var method = methods[i];
		if(method.name != 'phonetics' && method.checked == true) {
			newMethods[newMethods.length] = method.value;
		}
	}

	targetForm.filtermethods.value = newMethods.join(",");
};

// Switch extended search
prospitalia.prototype.switchExtendedSearch = function (box, action, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.switchExtendedSearch(box, action, formname); }, 100);
	} else {
		if(box.closed) {
			box.showBox();
						
			if(box.getBoxContent() == '') {
				this.ajax.load('boxListSearch', this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=search&mode=' + action + '&searchform=' + formname);
			}
		} else {
			box.closeBox();
		}
	}
};

// Switch extended search
prospitalia.prototype.openClassificationSearch = function (box, action, formname) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.openClassificationSearch(box, action, formname); }, 100);
	} else {
		box.showBox();
					
		this.ajax.load('boxListClassification', this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=classification&mode=' + action + '&searchform=' + formname);
	}
};

// Set the difference of list height
prospitalia.prototype.setListHeightDifference = function (height) {
	this.diffListHeight = parseInt(height);
};

// Set current list height, only for FireFox or Browser with Style 'maxHeight'
prospitalia.prototype.setListHeight = function () {
	var objTbl = GetObject('boxList');
	if(objTbl) {
		var height = parseInt(GetHeight(objTbl));
		if(height > 0 && !IE) {
			var objDiv = GetObject('List');
			var objContent = GetObject('ListContent');

			if(!this.isPageLoaded()) {
				height = height - 44 + this.diffListHeight;
				SetStyle(objTbl, 'height', (height) + 'px');
			}

			SetStyle(objDiv, 'height', (height) + 'px');

			height = height-22; // only for Mozilla, because of the footer margin
			SetStyle(objContent, 'height', height + 'px');
		}
	}
};

// Set import boxes
prospitalia.prototype.setImportBoxes = function (mode) {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.setImportBoxes(mode); }, 100);
	} else {
		if(mode == 'status') {
			this.ajax.load('boxImportStatus', this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=import.status&mode=status');
			mode = 'messages';
		} else {
			this.ajax.load('boxImportMessages', this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=import.status&mode=messages');
			mode = 'status';
		}
		
		window.setTimeout(function() { current.setImportBoxes(mode); }, 10000);
	}
};

// Open download window
prospitalia.prototype.openPrint = function (query, formname) {
	var form = document.forms[formname];
	if(form) {
		var elems = form.elements;
		for(var i=0;i<elems.length;i++) {
			var elem = elems[i];
			if(elem.name != '' && elem.value != '') {
				if(elem.type == 'file') {
					hasFiles = true;
				} else {
					var value = encodeURI(elem.value);
					if(elem.type == 'checkbox' && elem.checked == false) {
						value = '';
					}
				
					query += '&' + elem.name + '=' + value;
				}
			}
		}
	}

	var winPrint = window.open(this.getRoot() + 'print.php?' + this.getSession() + query, 'winPrint', 'dependent=true,toolbar=yes,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=900,height=600,top=0,left=0');
	if(winPrint) {
		winPrint.focus();
	}
};

// Open download window
prospitalia.prototype.openDownload = function (query) {
	var winDownload = window.open(this.getRoot() + 'download.php?' + this.getSession() + query, 'winDownload', 'dependent=true,toolbar=yes,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=900,height=600,top=0,left=0');
	if(winDownload) {
		winDownload.focus();
	}
};

// Load navigation
prospitalia.prototype.refreshNavigation = function () {
	var current = this;
	
	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.refreshNavigation(); }, 10);
	} else {
		this.ajax.load('boxNavigation', this.getRoot() + 'load.php?' + this.getSession() + '&dtype=general&action=navigation');
		if(BoxManager) BoxManager.closeBoxes();
	}
};

//Switch list field for editing
prospitalia.prototype.openField = function (cell, action) {
	if(cell.opened) {
		return;
	}

	Prospitalia.closeFields();
	
	var fieldPk = cell.getAttribute('fieldPk', true);
	var fieldValue = cell.getAttribute('fieldValue', true);
	var fieldQuery = cell.getAttribute('fieldQuery', true);
	
	var fld = document.createElement("input");
	fld.name = 'newNumber';
	fld.type = 'text';
	fld.value = fieldValue;
	fld.className = 'Field';
	fld.onclick = function(e) {
			try { fld.focus(); } catch(exp) {}
			if(!e) e = window.event;
			if(e && e.preventDefault) {
				e.preventDefault(); e.stopPropagation();
			} else {
				e.returnValue = false; e.cancelBubble = true;
			}
			return false; };
	fld.onfocus = function(e) { cell.noclose = true; };
	fld.onblur = function(e) { cell.noclose = false; };
	fld.onmouseout = function() { Prospitalia.closeFields(); };
	fld.onkeydown = function(e) {
			if(!e) e = window.event;
			var keyCode = (e.keyCode)? e.keyCode: e.charCode;

			if(keyCode == '13') {
				Prospitalia.openList(action, fieldQuery + '&fieldPk=' + fieldPk + '&fieldValue=' + encodeURIComponent(fld.value));

				if(e && e.preventDefault) {
					e.preventDefault(); e.stopPropagation();
				} else {
					e.returnValue = false; e.cancelBubble = true;
				}
				return false;
			} };
	fld.style.width = '65px';
	fld.style.height = '18px';
	
	var icon = document.createElement("img");
	icon.src = "layout/images/yes.gif";
	icon.style.cursor = "pointer";
	icon.style.width = "12px";
	icon.style.height = "11px";
	icon.style.margin = "0px 0px 0px 5px";
	icon.onclick = function() {
			Prospitalia.openList(action, fieldQuery + '&fieldPk=' + fieldPk + '&fieldValue=' + encodeURIComponent(fld.value));
		};
	
	SetContent(cell, '');

	cell.appendChild(fld);
	cell.appendChild(icon);
	
	cell.noclose = false;
	cell.opened = true;
};

//Switch list field for editing
prospitalia.prototype.closeFields = function () {
	
	var elems = document.getElementsByTagName("div");
	for(var e=0;e<elems.length;e++) {
		var elem = elems[e];
	
		var fieldPk = elem.getAttribute('fieldPk', true);
		var fieldValue = elem.getAttribute('fieldValue', true);

		if(fieldPk && fieldPk != '') {
			if(elem.noclose) {
				continue;
			}
		
			SetContent(elem, fieldValue);
			elem.opened = false;
		}
	}
	
};
