// JavaScript Document


//---------------------------------------------------------------------------
//
//  c h a n g e E l e m e n t S t a t u s (elementId): hides or get Content from div
//
// fuctions: setChilds2passive[getPartentChilds,returnChilds]; doRequest[getData]; storeStatus
// vars:	 idRequesttag(doRequest)

function changeElementStatus (elementId, url) {
	
	setChilds2passive (elementId);
	var currentRequest,
	  thisElement = document.getElementById(elementId);
		
	if (thisElement.className=="passive") {
		storeStatus (elementId);
		thisElement.className="active";
		
		currentRequest = doRequest (url, 0);
		idRequesttag[currentRequest] = elementId;
	}
	
	else {
		storeStatus (elementId);
		thisElement.className="passive";
}	}


//---------------------------------------------------------------------------
//
//  c h a n g e E l e m e n t S t a t u s F r o m I d (elementId): hides or get Content from a id like SourceID
//
// fuctions: changeElementStatusFromURL[changeElementStatus[setChilds2passive[getPartentChilds;returnChilds];doRequest[getData]]

function changeElementStatusFromId (elementId) {

	if (elementId.match (/(\D+)(\d+)/)) {
		changeElementStatusFromURL (elementId, 'Where=0='+RegExp['$2']);
}	}


//---------------------------------------------------------------------------
//
//  c h a n g e E l e m e n t S t a t u s F r o m U R L (elementId, url): hides or get Content from a id like SourceID
//
// fuctions: changeElementStatus[setChilds2passive[getPartentChilds;returnChilds];doRequest[getData]]

function changeElementStatusFromURL (elementId, url) {

	if (elementId.match (/^(\D+)(\d+)/)) {
		link = 'media/AJAX/'+RegExp['$1']+'.php?';

		if (url.match (/^(\D+)(=)(.+)/)) changeElementStatus (elementId, link+url);
		
		else if (url.match (/^(\d+)(=)(.+)/)) changeElementStatus (elementId, link+'Where='+url);				
		
		else changeElementStatus (elementId, url);							
	} else {
		changeElementStatus (elementId, url);							
}	}


//---------------------------------------------------------------------------
//
//  c h a n g e M a s t e r C h e c k o x: set all checkboxes in elementId or set checkbox elementId to their correct value
//
// functions:	returnChilds
// vars:		thisRequest, lastRequest (doRequest)

function changeMasterCheckbox (elementId) {

	if (window.thisRequest && thisRequest>=lastRequest) setTimeout("changeMasterCheckbox('"+elementId+"');",(thisRequest-lastRequest)*10+1);
	
	else {
	
		var 
		  n=0, MasterVisible=0, childs, status = "active", thisValue,
		  thisElement = document.getElementById(elementId);
		
		childs = returnChilds (document.getElementById(getParentChild(elementId)).childNodes);
	
		childs = returnChilds (document.getElementById(childs[1]).childNodes);


		if (thisElement.style.opacity==.7) {
			thisElement.style.opacity = 1;
			thisElement.checked = false;	
		}
		
		if (thisElement.checked==false) {
			status = "passive";
		} 
		
		for (n=0;n<childs.length;n++) {
			thisChild = document.getElementById(childs[n]);
			if (document.getElementById(thisChild.className).className!=status) {
				changeElementStatusFromURL(thisChild.className, thisChild.value);
				setCheckboxes (childs[n]);
}	}	}	}

  
//---------------------------------------------------------------------------
//
//  d o R e q u e s t (url, order): Does AJAX-Request with URL and returns thisRequest. order=0 to start
//
// fuctions: 	getData
// globalvars:	thisRequest, lastRequest, http_request

function doRequest (url, order) {
	
	if (!window.lastRequest) window.lastRequest = 1;
	
	if (order==0) {
		if (!window.thisRequest) window.thisRequest = 0;
		if (!window.idRequesttag) window.idRequesttag = new Array('');


		thisRequest++;
		setTimeout("doRequest('"+url+"', "+thisRequest+");", 5);
		return (thisRequest);
	} 
	
	else if (order!=lastRequest) setTimeout("doRequest('"+url+"', "+order+");",5);
 
	else if (order<=lastRequest) {

		window.http_request = false;

		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
		
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	 
		if (!http_request) return false;
		http_request.open('GET', url, true);
		http_request.onreadystatechange = getData;
		http_request.send(null); 
}	}


//---------------------------------------------------------------------------
//
//  Array.find (searchStr): Searches in Array searchStr and returns Key(s) in Array

Array.prototype.find = function(searchStr) {

	var returnArray = false;
	
	for (i=0; i<this.length; i++) {
		if (typeof(searchStr) == 'function') {
			if (searchStr.test(this[i])) {
				if (!returnArray) { returnArray = [] }
				returnArray.push(i);
			}
		} else {
			if (this[i]===searchStr) {
				if (!returnArray) { returnArray = [] }
				returnArray.push(i);
	}	}	}
	
	return returnArray;
}


//---------------------------------------------------------------------------
//
// getData (): Puts innerHTML in thisRequest[current4Request]
//
// globalvars:	idRequesttag, lastRequest

function getData () {
	if (http_request.readyState == 4) {
		document.getElementById(idRequesttag[lastRequest]).innerHTML = http_request.responseText;
		setTimeout("lastRequest++;",10);
}	}


//---------------------------------------------------------------------------
//
//  g e t P a r e n t C h i l d (elementId): retruns parent-id

function getParentChild (elementId) {
	
	var 
	  thisNode = 'Start',
	  moreChilds = true;

	while (moreChilds==true) {
		if (thisNode == 'Start') thisNode = document.getElementById(elementId).parentNode;
		else thisNode = thisNode.parentNode;

		if (!thisNode) {
			moreChilds = false;

		} else if (thisNode.id!='') {
			return(thisNode.id);
			moreChilds = false;
}	}	}


//---------------------------------------------------------------------------
//
//  g e t P a r e n t C h i l d s (elementId): retruns all parent-ids from AJAX to elementId in Array

function getParentChilds (elementId) {
	
	var 
	  thisNode = 'Start',
	  moreChilds = true,
	  childs = new Array (elementId);

	while (moreChilds==true) {
		if (thisNode == 'Start') thisNode = document.getElementById(elementId).parentNode;
		else thisNode = thisNode.parentNode;

		if (!thisNode) {
			moreChilds = false;
			childs.pop ();

		} else if (thisNode.id=="AJAX") {
			moreChilds = false;
			childs.push(thisNode.id);

		} else if (thisNode.id!='') {
			elementId = thisNode.id;
			childs.push(thisNode.id);
	}	}
	
	childs.reverse();
	if (childs[0].length==0) childs.splice(0,1); 

	return (childs);
}


//---------------------------------------------------------------------------
//
//  r e t u r n C h i l d s (childNode): searches the childleves for id. Returns all ids form the same level in array. childNode = document.getElementById(id).childNodes

function returnChilds (childNode) {

	var n,
	  childs = new Array ();

	for (n=0;n<childNode.length;n++) {
		if (childNode[n].id) childs.push (childNode[n].id);
	}
	
	if (childs.length==0) {
		for (n=0;n<childNode.length;n++) {
			if (childNode[n].childNodes) childs = returnChilds (childNode[n].childNodes);
			if (childs && childs.length > 0) {
				n=childNode.length;
	}	}	}
		
	if (childs.length>0) return (childs);
	else return(false);
}


//---------------------------------------------------------------------------
//
//  s e t A l l C h i l d s F r o m I n p u t: set all types to active form tag.elementId with inputs name->id & value->URL
//
// functions:	returnChilds; changeElementStatusFromURL[changeElementStatus[setChilds2passive[getPartentChilds];doRequest[getData]]

function setAllChildsFromInput (elementId) {

	var 
	  n=0, childs, thisValue,
	  childs = returnChilds (document.getElementById(elementId).childNodes);
	for (n=0;n<childs.length;n++) {
		thisChild = document.getElementById(childs[n]);
		changeElementStatusFromURL(thisChild.className, thisChild.value);
}	}


//---------------------------------------------------------------------------
//
//  s e t A l l C h i l d s: set all types to active or passive from third level. If one is active -> all to passive
//
// functions:	returnChilds; changeElementStatusFromId[changeElementStatus[setChilds2passive[getPartentChilds];doRequest[getData]]

function setAllChilds (elementId) {

	if (thisRequest>(lastRequest-1)) {
		setTimeout("setAllChilds('"+elementId+"');", 10);
	} else {

		var 
		  n=0, childs, Style="active",
	  	  childs = returnChilds (document.getElementById(elementId).childNodes);

		for (n=0;n<childs.length;n++) {
			if (document.getElementById(childs[n]).className=="active") Style = "passive";
		}
		
		for (n=0;n<childs.length;n++) {
			if (document.getElementById(childs[n]).className!=Style) {
				changeElementStatusFromId (childs[n]);
}	}	}	}


//---------------------------------------------------------------------------
//
//  s e t C h e c k b o x e s: set all checkboxes in elementId or set checkbox elementId to their correct value
//
// functions:	returnChilds
// vars:		thisRequest, lastRequest (doRequest)

function setCheckboxes (elementId) {

	var 
	  n=0, childs,
	  thisElement = document.getElementById(elementId);
	
	if (thisElement && thisElement.nodeName=='INPUT') {
		if (thisRequest>lastRequest) setTimeout("setCheckboxes('"+elementId+"');",(thisRequest-lastRequest)*10+1);

		if (document.getElementById(thisElement.className).className=='active') thisElement.checked=true;
		else thisElement.checked=false;

	} else if (thisElement){
		childs = returnChilds (thisElement.childNodes);
		
		for (n=0;n<childs.length;n++) {
			setCheckboxes(childs[n]);
}	}	}	


//---------------------------------------------------------------------------
//
//  s e t C h i l d s 2 p a s s i v e (elementId): sets all ids.class from active to passive
//
// fuctions: getPartentChilds; returnChilds; storeStatus;

function setChilds2passive (elementId) {
	
	var n, childs,
	  thisElement = document.getElementById(elementId),
	  level = getParentChilds(elementId).length;
	
	childs = returnChilds(thisElement.childNodes);

	if (childs && childs.length>0 && level<3) {
		for (n=0;n<childs.length;n++) {
			if (document.getElementById(childs[n]).className=='active') {
				storeStatus (childs[n]);
				document.getElementById(childs[n]).className='passive';
			}
			if (level<3) setChilds2passive (childs[n]);
}	}	}


//---------------------------------------------------------------------------
//
//  s e t H i d e T i t l e (elementId): sets all ids.class from active to passive
//
// fuctions: getPartentChilds; returnChilds; storeStatus;

function setHideTitle (elementId) {
	
	if (thisRequest>(lastRequest-1)) {
		setTimeout("setHideTitle('"+elementId+"');", 5);
	} else {

		var n, HideTitle=false;
		  thisElement = document.getElementById(getParentChild(elementId)),
		  childs = returnChilds(thisElement.childNodes);
		 
		thisElement = thisElement.getElementsByTagName("SPAN")[0];
		
		if (childs && childs.length>0) {
			for (n=0;n<childs.length;n++) {
				if (document.getElementById(childs[n]).className=='active') HideTitle = true;
		}	}
	
		if (thisElement.className=="Title" && HideTitle==true) 
			thisElement.className = "TitleHide";	
	
		if (thisElement.className=="TitleHide" && HideTitle==false) 
			thisElement.className = "Title";	
}	}


//---------------------------------------------------------------------------
//
//  s e t M a s t e r C h e c k o x: set all checkboxes in elementId or set checkbox elementId to their correct value
//
// functions:	returnChilds
// vars:		thisRequest, lastRequest (doRequest)

function setMasterCheckbox (elementId) {

	if (thisRequest>=lastRequest) setTimeout("setMasterCheckbox('"+elementId+"');",(thisRequest-lastRequest)*10+1);
		
	var 
	  n=0, MasterVisible=0, childs, thisValue,
	  thisElement = document.getElementById(elementId);
	
	childs = returnChilds (document.getElementById(getParentChild(elementId)).childNodes);	

	childs = returnChilds (document.getElementById(childs[1]).childNodes);				
	
	for (n=0;n<childs.length;n++) {
		thisName = document.getElementById(childs[n]).className;
		if (document.getElementById(thisName).className=='active') MasterVisible++;
	}
	
	if (MasterVisible==childs.length) {
		thisElement.checked = true;
			thisElement.style. opacity = 1;
	} else if (MasterVisible==0) {
			thisElement.checked = false;
		thisElement.style. opacity = 1;
		} else {
		thisElement.checked = true;
		thisElement.style. opacity = .7;
}	}


//---------------------------------------------------------------------------
//
//  s t o r e S t a t u s (elementId): saves or insert elementId.innerHTML
//
// globalvars:	saveStatusId; saveStatusValue

function storeStatus (elementId) {
	
	if (!window.saveStatusId) window.saveStatusId = new Array ();
	if (!window.saveStatusValue) window.saveStatusValue = new Array ();
	
	var 
	  position = saveStatusId.find (elementId),
	  thisElement = document.getElementById (elementId);

	if (position===false && thisElement.className=="passive") {
		saveStatusId.push (elementId);
		saveStatusValue.push (thisElement.innerHTML);
	} else if (position===false  && thisElement.className!="passive") {
		thisElement.innerHTML = '';
	} else {
		thisElement.innerHTML = saveStatusValue[position];
		saveStatusId.splice(position,1);
		saveStatusValue.splice(position,1);
}	}

