/**
 * this file is part of the APM framework
 * 
 * @author Valentin Staubmann valentin.staubmann@oeaw.ac.at
 */

if (document.getElementsByTagName)
	onload = function() {
		document.getElementsByTagName("BODY")[0].onclick = body_onclick;
	};

	
/**
 * pack in there all neccessary action we need to do if there is a body click
 * recognized
 * 
 * @return void
 */
function body_onclick() {
	var o = getElementsByClass('body_onclick');
	var oLen = o.length;
	
	var h = getElementsByClass('body_onclick_hide');
	var hLen = h.length;
	
	for (var i = 0; i < oLen; i++) {
		if (o[i].style.display == 'block') {
			enableHide(o[i].id);
			
		}
		
	}
	
	

	for (var u = 0; u < hLen; u++) {
		var classNames = h[u].className.split(' ');
		var cNLen = classNames.length;
		
		for (var g = 0; g < cNLen; g++) {
			var has_parent = classNames[g].indexOf('parent_');
			
			if (has_parent != -1) {
				var close_object_id = classNames[g].substring(7);
				close_object = document.getElementById(close_object_id);
				
				if (close_object.DateChooser === undefined) {
					close_object.DateChooser = new DateChooser(close_object, h[u].id);
					
				}
				
				close_object.DateChooser.hide();
				
			}
		
		}
		
		h[u].style.display = 'none';
			
		disableHide(h[u].id);
		
	}

}

function enableHide( id ){
	var newClass = '';
	
	el = document.getElementById( id );
	
	var classNames = el.className.split(' ');
	var cNLen = classNames.length;
	
	for (var j = 0; j < cNLen; j++) {
		var className = classNames[j];
		
		if (className == 'body_onclick') {
			classNames[j] = 'body_onclick_hide';
			
		}
		
		newClass+= ' ' + classNames[j];
		
	}
	
	el.className = newClass;
	
}

function disableHide( id ){
	var newClass = '';
	
	el = document.getElementById( id );
	
	var classNames = el.className.split(' ');
	var cNLen = classNames.length;
	
	for (var j = 0; j < cNLen; j++) {
		var className = classNames[j];
		
		if (className == 'body_onclick_hide') {
			classNames[j] = 'body_onclick';
			
		}
		
		newClass+= ' ' + classNames[j];
		
	}
	
	el.className = newClass;

}



/**
 * 1. Supply a class name as a string.
 * 2. (optional) Supply a node. This can be obtained by getElementById,
 * or simply by just throwing in “document” (it will be document if don’t
 * supply a node)). It’s mainly useful if you know your parent and you 
 * don’t want to loop through the entire D.O.M.
 * 3. (optional) Limit your results by adding a tagName. Very useful
 * when you’re toggling checkboxes and etcetera. You could just supply
 * “input“. Or, if you’re like me, and you said Good Bye to IE5, you
 * can use the “*” asterisk as a catch-all (meaning ‘any element).
 *
 * @param searchClass
 * @param node
 * @param tag
 * @return
 */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


/**
 * enable press enter for unformed forms - means forms without form tag
 * 
 * @return bool
 */
function isEnterKey(e) {
	var key;
	
	if (window.event) {
		key = window.event.keyCode;
	
	} else {
		key = e.which;
	
	}
	
	if (key == 13) {
		return true;
	
	} else {
		return false;
	
	}

}
