/***
 * Creates new filter box
 * For example for supplier or clinic filter
 * Needs functions.js and requests.js
 */
function filterbox(fieldId, fieldTextLength, fieldImg, boxId, boxAction, boxMode, boxWidth, boxHeight, icons, jsQuerySet, jsQueryDetail) {
	if(fieldId == '' || boxId == '') {
		return;
	}
	
	// Set field
	this.field	= GetObject(fieldId);
	if(typeof this.field == 'undefined') {
		alert('Field "'+fieldId+'" not found!');
		return;
	}

	var divBox = document.createElement('DIV');
    divBox.id = 'box' + boxId;
    divBox.name = 'box' + boxId;
    divBox.setAttribute('id', 'box' + boxId);
    divBox.setAttribute('name', 'box' + boxId);

	var bodies = document.getElementsByTagName('BODY');
	bodies[0].insertBefore(divBox, bodies[0].firstChild);
	
	// Set box
	this.box	= GetLayer('box' + boxId);
	if(this.box == null) {
		alert('Box "'+boxId+'" not found!');
		return;
	}

	// Sets parameter
	var intFieldTextLength	= parseInt(fieldTextLength);
	if(intFieldTextLength < 1) {
		this.fieldtextlength = 4;
	} else {
		this.fieldtextlength = intFieldTextLength;
	}

	if(boxWidth == null || typeof boxWidth == 'undefined' || boxWidth == '') {
		boxWidth = 0;
	}
	if(boxHeight == null || typeof boxHeight == 'undefined' || boxHeight == '') {
		boxHeight = 0;
	}

	this.root			= '';
	this.session		= '';

	this.action				= boxAction;
	this.mode				= boxMode;
	this.width				= boxWidth;
	this.height				= boxHeight;
	this.checkTextLength	= true;

	// Init open image
	this.openIcon	= GetImage(fieldImg);

	if(icons == null || typeof icons == 'undefined' || icons == '') {
		this.icons	= new Array();
	} else {
		this.icons	= icons.split(",");
	}

	if(jsQuerySet == null || typeof jsQuerySet == 'undefined' || jsQuerySet == '') {
		this.jsQuerySet		= '';
	} else {
		this.jsQuerySet		= jsQuerySet;
	}
	if(jsQueryDetail == null || typeof jsQueryDetail == 'undefined' || jsQueryDetail == '') {
		this.jsQueryDetail	= '';
	} else {
		this.jsQueryDetail	= jsQueryDetail;
	}

	// Sets default values
	this.currentPosition = 1;
	this.currentMarkedObject = null;
	this.lastFilterText = null;

	this.ajax = new ajax();
	this.ajax.parent = this;

	var current	= this;

	// Sets the event handler
	this.field.onkeyup	= function(e) { current.checkFilterbox(e); };
	this.field.onfocus	= function(e) { current.showFilterbox(e); };
	this.field.onclick	= function(e) { current.showFilterbox(e); };
	this.field.onblur	= function(e) { current.setFilterbox(e); current.closeFilterbox(e); };

	this.box.onfocus	= function(e) { current.showFilterbox(e); };
	this.box.onkeyup	= function(e) { current.checkFilterbox(e); };
	this.box.closed		= true;

	if(this.openIcon != null) {
		this.openIcon.onmouseover	= function() { switchCursor(this, '1'); };
		this.openIcon.onmouseout	= function() { switchCursor(this, '0'); };
	}
};

// Set root
filterbox.prototype.setRoot = function (strRoot) {
	if(typeof strRoot == 'string') {
		this.root = strRoot;
	}
};

// Returns root
filterbox.prototype.getRoot = function () {
	return this.root;
};

// Set session
filterbox.prototype.setSession = function (strSession) {
	if(typeof strSession == 'string') {
		this.session= strSession;
	}
};

// Returns session
filterbox.prototype.getSession = function () {
	return this.session;
};

// Set style
filterbox.prototype.setFilterboxStyles= function (styles) {
	styles = styles.replace(/: /g, ":");

	var arrStyles = styles.split(";");
	for(var s=0;s<arrStyles.length;s++) {
		var style = arrStyles[s];
		var arrStyle = style.split(":");
		if(arrStyle[0] && arrStyle[1] && arrStyle[0] != '' && arrStyle[1] != '') {
			arrStyle[0] = arrStyle[0].replace(/-q/g, "Q");
			arrStyle[0] = arrStyle[0].replace(/-w/g, "W");
			arrStyle[0] = arrStyle[0].replace(/-e/g, "E");
			arrStyle[0] = arrStyle[0].replace(/-r/g, "R");
			arrStyle[0] = arrStyle[0].replace(/-t/g, "T");
			arrStyle[0] = arrStyle[0].replace(/-z/g, "Z");
			arrStyle[0] = arrStyle[0].replace(/-u/g, "U");
			arrStyle[0] = arrStyle[0].replace(/-i/g, "I");
			arrStyle[0] = arrStyle[0].replace(/-o/g, "O");
			arrStyle[0] = arrStyle[0].replace(/-p/g, "P");
			arrStyle[0] = arrStyle[0].replace(/-a/g, "A");
			arrStyle[0] = arrStyle[0].replace(/-s/g, "S");
			arrStyle[0] = arrStyle[0].replace(/-d/g, "D");
			arrStyle[0] = arrStyle[0].replace(/-f/g, "F");
			arrStyle[0] = arrStyle[0].replace(/-g/g, "G");
			arrStyle[0] = arrStyle[0].replace(/-h/g, "H");
			arrStyle[0] = arrStyle[0].replace(/-j/g, "J");
			arrStyle[0] = arrStyle[0].replace(/-k/g, "K");
			arrStyle[0] = arrStyle[0].replace(/-l/g, "L");
			arrStyle[0] = arrStyle[0].replace(/-y/g, "Y");
			arrStyle[0] = arrStyle[0].replace(/-x/g, "X");
			arrStyle[0] = arrStyle[0].replace(/-c/g, "C");
			arrStyle[0] = arrStyle[0].replace(/-v/g, "V");
			arrStyle[0] = arrStyle[0].replace(/-b/g, "B");
			arrStyle[0] = arrStyle[0].replace(/-n/g, "N");
			arrStyle[0] = arrStyle[0].replace(/-m/g, "M");
			SetStyle(this.box, arrStyle[0], arrStyle[1]);
		}
	}
};

// Close filterbox
filterbox.prototype.closeFilterbox= function () {
	if(this.currentMarkedObject == null) {
		SwitchLayer(this.box, 0);
		this.box.closed = true;

		//showSelect();

		if(this.openIcon != null) {
			this.openIcon.src = this.openIcon.src.replace(/open/gi, 'close');
		}

		this.lastFilterText = null;
	}
};


filterbox.prototype.switchFilterbox = function (e) {
	if(this.box.closed) {
		this.checkTextLength = false;
		this.showFilterbox();
	} else {
		this.checkTextLength = true;
		this.closeFilterbox();
	}
};

filterbox.prototype.showFilterbox = function (e) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.showFilterbox(e); }, 100);
	} else {
		//this.field.focus();

		var on = GetStyle(this.box, 'display');
		var text = this.field.value.substr(0, this.fieldtextlength);

		SetStyle(this.box, 'position', 'absolute');
		SetStyle(this.box, 'left', GetX(this.field)+1);
		SetStyle(this.box, 'top', GetY(this.field)+20);

		if(this.width == 0) {
			SetStyle(this.box, 'width', GetWidth(this.field));
		} else {
			SetStyle(this.box, 'width', parseInt(this.width) + 'px');
		}
		if(this.height == 0) {
			SetStyle(this.box, 'height', '200px');
		} else {
			SetStyle(this.box, 'height', parseInt(this.height) + 'px');
		}

		if(this.checkTextLength && text.length < 1) {
			this.lastFilterText = null;
			return;
		}
		
		if(this.openIcon != null) {
			this.openIcon.src = this.openIcon.src.replace(/close/gi, 'open');
		}
		
		if(this.box.closed) {
			SwitchLayer(this.box, 1);
			this.box.closed = false;

			//hideSelect();
		}
		
		if(this.lastFilterText != text) {
			if(text.length > 0) {
				this.checkTextLength = true;
			}
			this.lastFilterText = text;
			this.ajax.load(this.box.id, this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=' + this.action + '&mode=' + this.mode + '&text=' + encodeURI(text));
		}
	}
};

filterbox.prototype.checkFilterbox = function (e) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.checkFilterbox(e); }, 500);
	} else {
		if(!e) e = window.event;
	
		var keyCode = '';
		if(e && e.keyCode) {
			keyCode = e.keyCode;
		} else if(e && e.keyCode) {
			keyCode = e.charCode;
		}
		var text = (this.field.value.length > this.fieldtextlength) ? this.field.value.substr(0, this.fieldtextlength) : this.field.value;
	
		// Last loading
		if(this.ajax.isBusy()) {
			this.ajax.reset();
		}
	
		if(this.box.closed) {
			this.showFilterbox();
		}

		if(keyCode == '38') { // Cursor up
			this.currentPosition--;
			if(this.currentPosition < 1) {
				this.currentPosition = 1;
			}
			this.setFilterboxPosition();
		} else if(keyCode == '40') { // Cursor down
			this.currentPosition++;
			this.setFilterboxPosition();
		} else if(keyCode == '13') { // Enter
			this.setFilterbox();
		} else if(keyCode == '27') { // Esc
			this.closeFilterbox();
		} else if(1==0 && keyCode == '8') { // Delete
			return;
		} else if(1==0 && keyCode == '46') { // Delete
			return;
		} else if(keyCode == '37') { // Cursor left
			return;
		} else if(keyCode == '39') { // Cursor right
			return;
		} else if(keyCode == '16') { // Shift
			return;
		} else if(keyCode == '17') { // Strg
			return;
		} else if(keyCode == '35') { // Ende
			return;
		} else if(keyCode == '36') { // Pos1
			return;
		} else {
			this.ajax.load(this.box.id, this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=' + this.action + '&mode=' + this.mode + '&text=' + encodeURI(text));
		}

		var tmpText = this.field.value;
		this.field.value = "";
		this.field.focus();
		this.field.value = tmpText;
	}
};

filterbox.prototype.setFilterbox = function () {
	this.loadNewList = false;
	
	if(this.currentMarkedObject != '' && this.currentMarkedObject != null) {
		var text = GetContent(this.currentMarkedObject);
		var objectPk = this.currentMarkedObject.getAttribute("objectPk");

		if(objectPk == 'all') {
			this.field.value = '';
	
			for(var i=0;i<this.icons.length;i++) {
				this.switchFilterboxIcon(0, 'img' + this.mode + this.icons[i], '');
			}
		} else {
			this.field.value = text.charEntities();
	
			for(var i=0;i<this.icons.length;i++) {
				if(this.currentMarkedObject.getAttribute(this.icons[i]) == 'true') {
					this.switchFilterboxIcon(1, 'img' + this.mode + this.icons[i], this.icons[i]);
				} else {
					this.switchFilterboxIcon(0, 'img' + this.mode + this.icons[i], '');
				}
			}
		}

		this.loadNewList = true;
	} else if(this.field.value.length < 1 && this.field.value != this.lastFilterText && this.lastFilterText && this.lastFilterText != '') {
		objectPk = 'all';
	
		for(var i=0;i<this.icons.length;i++) {
			this.switchFilterboxIcon(0, 'img' + this.mode + this.icons[i], '');
		}

		this.loadNewList = true;
	}
	
	if(this.loadNewList) {
		if(this.jsQuerySet != '') {
			var query = this.jsQuerySet;
			query = query.replace(/objectPk/gi, objectPk);
			eval(query);
		}

		this.lastFilterText = null;
		this.currentPosition = 1;
		this.currentMarkedObject = null;

		this.closeFilterbox();
	}
};
			
filterbox.prototype.switchFilterboxIcon = function (on, imgId, detailMode) {
	var img = GetImage(imgId);
	if(img != null) {
		if(on == '1') {
			var objectPk = this.currentMarkedObject.getAttribute("objectPk");
	
			SetStyle(img, 'visibility', 'visible');
			SetStyle(img, 'display', 'inline');
	
			if(this.jsQuerySet != '') {
				var query = this.jsQueryDetail;
				query = query.replace(/objectPk/gi, objectPk);
				query = query.replace(/detailMode/gi, detailMode);
	
				img.onclick = function() { eval(query); };
			} else {
				img.onclick = function() {};
			}
	
			img.onmouseover	= function() { switchCursor(this, '1'); };
			img.onmouseout	= function() { switchCursor(this, '0'); };
		} else {
			SetStyle(img, 'visibility', 'hidden');
			SetStyle(img, 'display', 'none');
	
			img.onclick = function() {};
		}
	}
};

filterbox.prototype.overFilterboxPosition = function (object, position) {
	this.currentPosition = position;
	this.currentMarkedObject = object;

	this.setFilterboxPosition();
	switchCursor(object, '1');
};

filterbox.prototype.outFilterboxPosition = function (object) {
	this.currentMarkedObject = null;
	switchCursor(object, '0');
};

filterbox.prototype.setFilterboxPosition = function () {
	var objects = this.box.getElementsByTagName("div");
	var chkId = this.mode + this.currentPosition; 
	this.currentMarkedObject = null;
	var pos = 0;
	
	for(var i=0;i<objects.length;i++) {
		var currentClass = objects[i].className;	
		var id = objects[i].id;
		
		if(id != '') {
			var Result = currentClass.search('Aktiv');
			if(Result != -1) {
				objects[i].className = currentClass.replace('Aktiv', '');
			}
	
			if(id == chkId) {
				this.currentMarkedObject = objects[i];
				this.currentMarkedObject.className = this.currentMarkedObject.className + "Aktiv";
			}
		}
	}

	if(this.currentMarkedObject == null) {
		this.currentMarkedObject = objects[objects.length-1];
		this.currentMarkedObject.className = this.currentMarkedObject.className + "Aktiv";
		this.currentPosition--;
	}
};
