var Wiz = {

	un : function (item){
		if ((item)&&(item.childNodes)){
			for(var i = 0; i< item.childNodes.length; i++){
				Wiz.un(item.childNodes[i]);
			}
		}
		if ((item)&&(item.parentNode)){
			if (item.stopObserving){
				item.stopObserving();
			}
			item.parentNode.removeChild(item);
		}
	},
	unChild : function (item){
		if ((item)&&(item.childNodes)){
			for(var i = 0; i < item.childNodes.length; i++){
				var ch = item.childNodes[i];
				
				Wiz.un(ch);
				
//				if (ch.stopObserving){
//					ch.stopObserving();
//				}
//				item.removeChild(ch);
			}
		}
		return true;
	},
	div : function (attach, props, content){
		var item = Builder.node('div', props, content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	label : function (attach, props, content){
		var item = Builder.node('label', props, content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	form : function (attach, props, content){
		var item = Builder.node('form', props, content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	button : function (attach, props, content){
		var item = Builder.node('input', this.mergeObject({'type':'button'}, props), content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	submit : function (attach, props, content){
		var item = Builder.node('input', this.mergeObject({'type':'submit'}, props), content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	inputText : function (attach, props, content){
		var item = Builder.node('input', this.mergeObject({'type':'text'}, props), content);
		if (content){
//			props = this.mergeObject({'value':content}, props);
			item.value = content;
		}
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	inputPassword : function (attach, props, content){
		var item = Builder.node('input', this.mergeObject({'type':'password'}, props), content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	inputCheckbox : function (attach, props, content){
		var item = Builder.node('input', this.mergeObject({'type':'checkbox'}, props), content);
		if (attach){
			attach.appendChild(item);
		}
		return item;
	},
	mergeObject: function (a, b){
		var obj = {};
		if (a){
			for(var i in a){
				obj[i] = a[i];
			}
		}
		if (b){
			for(var i in b){
				obj[i] = b[i];
			}
		}
		return obj;
	},
	addArray : function (arr1, arr2){
		for (var i = 0; i < arr2.length; i++){
			arr1.push(arr2[i]);
		}
	},
	img : function (attach, props, content){
		var item = Builder.node('img', props, content);	if (attach){ attach.appendChild(item); } return item;
	},
	undiv : function (attach, target){
		if (attach && target){
			if (true == this.isChildOf(attach, target)){
				attach.removeChild(target);
			}
		}
	},
	isChildOf: function (attach, target){
		for(var i = 0; i < attach.childNodes.length; i++){
			if (attach.childNodes[i] == target){
				return true;
			}
		}
		return false;
	},
	iframe : function (attach, props, content, contentEditable){
		var item = Builder.node('iframe', props, null);
		if (attach){
			attach.appendChild(item);
		}
		if(content){
			Event.observe(item, 'load', function (){
				var frame = Wiz.iframeContent(item);
				frame.body.innerHTML = content;
				if (contentEditable){
					frame.body.contentEditable = true;
				}
			});
		};
		return item;
	},
	iframeContent : function (iframe){
		return (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow;
	},
	timer : function (timer, fnc, speed){
		if ((fnc == null) && (speed == null)){
			clearInterval(timer);
			return null;
		}
		if (timer != null){
			clearInterval(timer);
			timer = null;
		}
		return setInterval(fnc, speed);
	},
	timeout : function (timer, fnc, speed){
		if ((fnc == null) && (speed == null)){
			clearTimeout(timer);
			return null;
		}
		if (timer != null){
			clearTimeout(timer);
			timer = null;
		}
		return setTimeout(fnc, speed);
	},
	cleanPath : function (input){
		return input.replace(/#|@|\$|'|\||"|\&|\[|\]|\\|\*|\(|\)|\.|\?| |;|nbsp;|\.|\\|__/g, function(org, s1) {	return '_';	})
		.replace(/_$/g, function(org, s1) { return ''; })
		.toLowerCase();
	},
	findPos : function(obj) {
		var top =0, left = 0;
		if (obj && obj.offsetParent) {
			do {
				left += obj.offsetLeft;
				top += obj.offsetTop;
			} while (obj = obj.offsetParent);
			
			return {top:top,left:left};
		}else{
			return {top:0,left:0};
		}
	},
	windowSize: function (){
		return {width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
	    height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)};
	},
	offsetRight : function (object){
		if ((object.parentNode)&&(object.parentNode.getWidth)){
			var a = object.parentNode.getWidth();
			var b = object.offsetLeft + object.getWidth();
			
			return a - b;
		}else{
			return;
		}
	},
	offsetBottom : function (object){
		// FIXME: get correct parentNode height
//		var a = object.parentNode.getHeight();
		var a = this.windowSize().height;
		var b = object.offsetTop + object.getHeight();
		
		return a - b;
	},
	setUnselectable : function (object){
		object.unselectable = 'on';
		object.style.KhtmlUserSelect = 'none';
		object.style.MozUserSelect = 'none';
	},
	setSelectable : function (object){
		object.unselectable = 'off';
		object.style.KhtmlUserSelect = 'auto';
		object.style.MozUserSelect = 'auto';
	},
	getScrollPosition : function(node){
		
		var x = 0;
		var y = 0;
		
		if (node){
			if(node.pageXOffset){
					do {
						if (node.nodeName == 'BODY'){
							break;
						}
						x += Number((node.pageXOffset)? node.pageXOffset:0);
						y += Number((node.pageYOffset)? node.pageYOffset:0);
					} while (node = node.parentNode);
			}else{
					do {
						if (node.nodeName == 'BODY'){
							break;
						}
						x += Number((node.scrollLeft)?node.scrollLeft:0);
						y += Number((node.scrollTop)?node.scrollTop :0);
					} while (node = node.parentNode);
			}
		}
		return {'x':x,'y':y};
	},
	mouseOffset: function (e){
		var x = e.offsetX?(e.offsetX):10;
		var y = e.offsetY?(e.offsetY):10;
		return {'x':x,'y':y};
	},
	mousePosition: function (e){
		var x =  (e.pageX)? (e.pageX) : (e.clientX)?(e.clientX): 0;
		var y =  (e.pageY)? (e.pageY) : (e.clientY)?(e.clientY): 0;
		return {x:x,y:y};
	},
	getMousePosition : function(e) {
		var x = (e.pageX) ? (e.pageX) : (e.clientX) ? (e.clientX) : 0;
		var y = (e.pageY) ? (e.pageY) : (e.clientY) ? (e.clientY) : 0;
		return {
			x : x,
			y : y
		};
	},
	deselectAll: function(){
		if (document.selection && document.selection.empty) {
	        try {
	            document.selection.empty();
	        } catch(error) {
	        	
	        }
	    }else if (window.getSelection) {
	        var selection = window.getSelection();
	        if (selection && selection.removeAllRanges){ selection.removeAllRanges(); };
	    }
	},
	validateEmail: function (input){
		
		var goodEmail = input.value.match(/\b(^(\S+@).+((\....)|(\...)|(\.....)(\..{2,2}))$)\b/gi);
		if (goodEmail){
			input.style.color = "#00aa10";
			return true;
		}else{
			input.style.color = "#aa0000";
			return false;
		}
		
	},
//	deselectAll : function (){
//		if (window.getSelection){
//			var x = window.getSelection();
//			if (x.removeAllRanges){
//				x.removeAllRanges();
//			}
//		}else if ((document.selection) && (document.selection.empty)){
//			document.selection.empty();
//		}
//	},
	empty : function (value){
		switch(value){
			case '':
			case undefined:
			case NaN:
			case ' ':
				return true;
		}
		return false;
	}
};
var px = 'px', 
R ='mouseover', 
T ='mouseout', 
K = 'click', 
N ='mousedown', 
P='mouseup',
E = Event.observe,
Wd = Dom.div,
Wuc = Dom.unChild,
Wu = Dom.un
;



var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
thisMovie = function(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};
/**
 * , height='150', width='150', toolbar='yes', directories='no', status='no',
 * scrollbars='no', resizable='yes'
 */
popWindowScrollable = function(url, name, newWidth, newHeight) {
//	var dim = "width=" + newWidth + ", height=" + newHeight;
	newwindow = window.open(url, 'name=' + name, 'width=' + newWidth
			+ ',height=' + newHeight + ', resizable=yes, scrollbars=yes');
	if (window.focus) {
		newwindow.focus();
	}
	return false;
};

/** ADD BUTTON ACTIONS TO MENU */
xwizMouse = function(obj, action, url, newCssStyle) {
	if (obj) {
		switch (action) {
		case 'over':
			if (newCssStyle) {
				var oid = obj.id;
				obj.id = newCssStyle;
				return oid;
			} else {
				return 0;
			}
		case 'down':
			window.location = url;
			break;
		case 'out':
			if (newCssStyle) {
				obj.id = newCssStyle;
			}
		}
	}
};
// global functions
nodeAttr = function(node, attributeName) {
	if (!node) {
		return '';
	}
	if (isIE) {
		var ret = node.getAttribute(attributeName);
		if (ret) {
			return ret;
		} else {
			return '';
		}
	} else {
		if (!node.hasAttribute) {
			return '';
		}
		if (node.hasAttribute(attributeName)) {
			return node.getAttribute(attributeName);
		} else {
			return '';
		}
	}
};
isArray = function(obj) {
	if (!obj) {
		return false;
	};
	if (obj.constructor.toString().indexOf("Array") == -1){
		return false;
	}else{
		return true;
	}
};
// returns the element value of the given key
nob = function(data, idVal) {
	if (!data) {
		return '';
	}
	if (data.childNodes) {
		for ( var i = 0; i < data.childNodes.length; i++) {
			if (idVal == data.childNodes[i].nodeName) {
				if (data.childNodes[i].childNodes) {
					if (data.childNodes[i].childNodes[0]) {
						return data.childNodes[i].childNodes[0].nodeValue;
					} else {
						return '';
					}
				} else {
					return data.childNodes[i].nodeValue;
				}
			}
		}
	}
	return '';
};
nodeVal = function(node) {
	if (node) {
		if (node.firstChild) {
			if (node.firstChild.nodeValue) {
				return node.firstChild.nodeValue;
			}
		} else if (node.nodeValue) {
			return node.nodeValue;
		}
	}
	return '';
};
childNodeByTag = function(node, name) {
	if (node) {
		for ( var i = 0; i < node.length; i++) {
			if (node[i].nodeName == name) {
				return node[i];
			}
		}
	}
	return '';
};
/* general helper */
getChildNodeById = function(obj, idName) {
	var items = obj.childNodes;
	for ( var i = 0; i < items.length; i++) {
		if (items[i].id == idName) {
			return items[i];
		}
	}
	;
};
getChildNodeByName = function(obj, idName) {
	var items = obj.childNodes;
	for ( var i = 0; i < items.length; i++) {
		if (items[i].name == idName) {
			return items[i];
		}
	}
	;
	return;
};
getChildinDepth = function(obj, idName) {
	var items = obj.childNodes;
	for ( var i = 0; i < items.length; i++) {
		if (x = getChildNodeByName(items[i], idName)) {
			return x;
		} else {
			if (x = getChildinDepth(items[i], idName)) {
				return x;
			}
		}
	}
	;
	return 0;
};
removeWhiteSpace = function(str) {
	return str.replace(/^\s*|\s*$/g, '');
};
function setEndOfContenteditable(contentEditableElement) {
	var range, selection;
	// Firefox, Chrome, Opera, Safari, IE 9+
	if (document.createRange) {
		range = document.createRange();
		// Create a range (a range is a like the selection but invisible)
		range.selectNodeContents(contentEditableElement);
		// Select the entire contents of the element with the range
		range.collapse(false);
		// collapse the range to the end point. false means collapse to end
		// rather than the start
		selection = window.getSelection();
		// get the selection object (allows you to change selection)
		selection.removeAllRanges();
		// remove any selections already made
		selection.addRange(range);
		// make the range you have just created the visible selection
	} else if (document.selection) {
		// IE 8 and lower
		range = document.body.createTextRange();
		// Create a range (a range is a like the selection but invisible)
		range.moveToElementText(contentEditableElement);
		// Select the entire contents of the element with the range
		range.collapse(false);
		// collapse the range to the end point. false means collapse to end
		// rather than the start
		range.select();
		// Select the range (make it the visible selection
	}
};
