﻿als.forms = {};

als.forms.objects = {};
als.forms.errortipInterval = 0;

als.forms.focus = function(obj) {

	if (obj.className.indexOf('error') >= 0) {

		obj.style.backgroundColor = "#ffdada";

	} else {

		obj.style.backgroundColor = "#ffc";
	}
};

als.forms.blur = function(obj) {


	if (obj.className.indexOf('error') >= 0) {

		obj.style.backgroundColor = "#fff2f2";

	} else {

		obj.style.backgroundColor = "#fff";
	}
};

als.forms.showForm = function (id) {

	var formObject = document.getElementById(id);

	if (formObject != null) {

		formObject.style.display = 'block';

		if (typeof(formObject.elements) != 'undefined') {

			if (formObject.elements.length > 0) {

				for (var iterator = 0; iterator < formObject.elements.length; iterator ++) {

					var element = formObject.elements[iterator];

					var tagName = element.tagName.toLowerCase();

					if (((tagName == 'input') && (element.type != 'hidden')) ||
						(tagName == 'select')) {

						element.focus();

						break;
					}
				}
			}
		}
	}
};

als.forms.hideForm = function(id, code) {

	var sourceObject = null;

	var formObject = document.getElementById(id);

	if (formObject != null) {

		formObject.style.display = 'none';

		eval('sourceObject = als.forms.objects.' + code + 'Object;');

		if (sourceObject != null) {

			sourceObject.style.display = 'inline';

			sourceObject.focus();

			eval('als.forms.objects.' + code + 'Object = null;');
		}
	}
};

als.forms.selectList = function(id, width) {
    
	var source = document.getElementById(id);

	var list = document.createElement('ul');

	list.id = id + '-list';
	list.className = 'select-list';
	list.onselectstart = function() { return false; };
	
	if (typeof(width) == 'undefined') {

		list.style.width = '100%';

	} else {

		list.style.width = width;
	}

	var listSize = source.options.length;

	if (listSize > 6) {

		list.style.height = '9em';
	}
    
	source.style.position = 'absolute';
	source.style.top = 0;
	source.style.left = 0;   
	source.style.display = 'none';

	source.disableList = function() {

		for (var iterator = 0; iterator < list.childNodes.length; iterator ++) {

			var child = list.childNodes[iterator];

			child.childNodes[0].setAttribute('disabled', 'disabled');
			child.childNodes[0].childNodes[0].setAttribute('disabled', 'disabled');
			child.setAttribute('disabled', 'disabled');

			child.className = '';
			child.childNodes[0].childNodes[0].checked = false;
			source.options[iterator].selected = false;
		};

		list.setAttribute('disabled', 'disabled');
	};

	source.enableList = function() {

		for (var iterator = 0; iterator < list.childNodes.length; iterator ++) {

			var child = list.childNodes[iterator];

			child.childNodes[0].removeAttribute('disabled');
			child.childNodes[0].childNodes[0].removeAttribute('disabled');

			child.removeAttribute('disabled');
		};

		list.removeAttribute('disabled');
	};

	source.clearList = function() {

		source.options.length = 0;

		for (var iterator = list.childNodes.length - 1; iterator >= 0; iterator --) {

			list.removeChild(list.childNodes[iterator]);
		}
	};

	source.rebuildList = function() {

		for (var iterator = 0; iterator < source.options.length; iterator ++) {

			var option = source.options[iterator];

			var item = document.createElement('li');
			var itemLabel = document.createElement('label');

			itemLabel.innerHTML = '<input type="checkbox"' + (option.selected ? ' checked="checked"' : '') + ' />' + option.text;
			itemLabel.__index = iterator;

			var itemCheck = itemLabel.childNodes[0];

			item.onmouseover = function() {

				if (this.getAttribute('disabled')) return;

				this.className += ' hover';
			};

			item.onmouseout = function() {

				if (this.getAttribute('disabled')) return;

				this.className = this.className.replace('hover', '');
			};

			itemCheck.onclick = function(e) {

				if (this.getAttribute('disabled')) return;

				e = (e || event);

				e.cancelBubble = true;

				if (e.stopPropagation) e.stopPropagation();

				this.parentNode.onclick(true);
			};

			itemLabel.onclick = function(e) {

				if (this.getAttribute('disabled')) return;

				e = (e || event);

				var child = this.childNodes[0];

				if (e != true) {

					child.checked = ! child.checked;

					e.returnValue = true;
					e.cancelBubble = true;

					if (e.stopPropagation) e.stopPropagation();
					if (e.preventDefault) e.preventDefault();
				}

				this.parentNode.className = (child.checked ? 'checked' : '');

				source.options[this.__index].selected = child.checked;

				return false;
			};

			item.className = (itemCheck.checked ? 'checked' : '');

			item.appendChild(itemLabel);
			list.appendChild(item);
		}
	};

	list.disable = source.disableList;
	list.enable = source.enableList;
	list.rebuild = source.rebuildList;
	list.clear = source.clearList;

	list.rebuild();

	if (source.getAttribute('disabled')) {

		list.disable();
	}

	source.parentNode.insertBefore(list, source);
};

function getWindowHeight() {

	var windowHeight = 0;
	
	if (typeof(window.innerHeight) == 'number') {
	
		windowHeight = window.innerHeight;
	}
	else {
	
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
		
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
		
function setFooter() {

	if (document.getElementById) {
	
		var windowHeight = getWindowHeight();
		
		if (windowHeight > 0) {
		
			var contentHeight = document.getElementById('content').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'relative';
				footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}


 function ShowYesNoMessage(message, hiddenInputResult) {  
 
        var recalculate = document.getElementById(hiddenInputResult);   
               
        if (confirm(message)) {
            recalculate.value = true;
        }
        else{
            recalculate.value = false;
        }
}
