/*** toolcontainer.js (c) 2005  Zuber Sven ***/

// toggles an image
function toggleImg(imgName){
	var f = document[imgName].src;
	var g = f;
 	f = f.substr(f.length-9,f.length);
 	if (f=="_opak.gif"){
 		g = g.substr(0,g.length-9);
 		g +=".gif"; 		
 	}
 	else{
 		g = g.substr(0,g.length-4);
 		g +="_opak.gif";
 	}
 	document[imgName].src = g;
}

// sets an image active
function setImgActive(imgName){
	var u = document[imgName].src;
	var v = u;
 	u = u.substr(u.length-9,u.length);
 	if (u == "_opak.gif"){
 		v = v.substr(0,v.length-9);
 		v +=".gif"; 		
 	}
 	document[imgName].src = v;
}

// select all checkboxes and phases
function selectAll(){
	// select all checkboxes
	var x=document.forms.checkboxes;
	for (var ii=0; ii<x.length; ii++){
		x[ii].checked=true;
	}
	toggleBgColor("support_process",3);
	toggleBgColor("main_process",4);
	toggleBgColor("regulatory_process",5);
	
	// select all phases
	setImgActive('Preparation');
	setImgActive('Planning');
	setImgActive('Procurement');
	setImgActive('Operation');
	setImgActive('Renewal');
}



// Toggles backgroundcolor of a div, when a checkbox got clicked
function toggleBgColor(divID, checkID){
	var x = document.getElementById(divID);
	var checkName = document.forms.checkboxes[checkID];
	if (checkName.checked){
		x.style.backgroundColor = '#E0E6ED';
	}
	else{
		x.style.backgroundColor = '#CFD8E3';
	}

}

// checks if an image is active
// if active -> it returns the phase name
function checkImgActive(imgName){
	var u = document[imgName].src;
	var v = u;
 	u = u.substr(u.length-9,u.length);
 	if (!(u == "_opak.gif")){
 		// returns the name of the phase
 		// including the pre-string "___"
 		return '___'+imgName;		
 	}
 			// just a string, if the phase is NOT active!
			// -> to avoid an empty search-string
 	else return "NEVERSEENONTHISPLANET";
}

// checks all checkboxes and img (phases), if they're active
function checkAll(){
	var c=document.forms.checkboxes;
	var result = new Array();
	for (var i=0; i<c.length; i++){
		if( c[i].checked){
				result[i]= '___'+c[i].value;
		}
		else{
			// just a string, if the checkbox is NOT checked!
			// -> to avoid an empty search-string
			result[i]= "NEVERSEENONTHISPLANET";
		}
	}
	

	// checks all phases
	var resultPhase = new Array();
	resultPhase[0] = checkImgActive('Preparation');
	resultPhase[1] = checkImgActive('Planning');
	resultPhase[2] = checkImgActive('Procurement');
	resultPhase[3] = checkImgActive('Operation');
	resultPhase[4] = checkImgActive('Renewal');
	
	
	// checks if at least one phase is selected
	
	if (resultPhase[0]!="NEVERSEENONTHISPLANET" || resultPhase[1]!="NEVERSEENONTHISPLANET" || resultPhase[2]!="NEVERSEENONTHISPLANET" || resultPhase[3]!="NEVERSEENONTHISPLANET" || resultPhase[4]!="NEVERSEENONTHISPLANET" ){
		var searchArray = result.concat(resultPhase);
		document.getElementById("infoBar").style.display = "none";
		document.getElementById("result").style.display = "";
		// function call -> function filter, with the following parameters
		searchUpper(searchArray);
		this.tables = document.getElementsByTagName("TABLE");
		var aTable = this.tables[0];
		QuickFilter.doFilter(aTable);
		
	}
	else{
		document.getElementById("infoBar").style.display = "";
		document.getElementById("result").style.display = "none";
	}
}

function searchUpper(searchCondition){
	this.tables = document.getElementsByTagName("TABLE");
	var aTable = this.tables[0];
	var tbody = aTable.tBodies[0];
	var count = 0;
	var found = false;
	for (var i = 0; i < tbody.rows.length; i++){
		var visible = true;
		var rowTest = tbody.rows[i].innerHTML;
		for (var j =0; j< 3; j++){	// loop for language
			var result = rowTest.search(searchCondition[j])
			if (result != -1){		// if language is found go on
				for (var h =3; h< 6; h++){	// loop for the processes
					var result2 = rowTest.search(searchCondition[h])
					if (result2 !=-1){		// if process is found go on
						for (var k =6; k< 16; k++){	// loop for the factors
							var result3 = rowTest.search(searchCondition[k])
							if (result3 !=-1){		// if factor is found go on
								for (var l =16; l< searchCondition.length; l++){  	// loop for the phases
									var result4 = rowTest.search(searchCondition[l])
									if (result4 !=-1){								// if phase is found set found = true
										found = true;
										count++;
										break;
									}
								}
								break;
							}
						}
						break;
					}
				}
				break;
			}			
			else{
				found = false;
			}
		}
		if (found){
			tbody.rows[i].style.display = "";
			tbody.rows[i].setAttribute("font-size","10","false");
		}
		else{
			tbody.rows[i].style.display = "none";
			tbody.rows[i].setAttribute("font-size","5","false");
		}
	}
	
	// count the Results
	
		var resultCount = document.createElement("span");
		resultCount.innerHTML = count+' results';
		document.getElementById("searchResult").replaceChild(resultCount,document.getElementById("searchResult").firstChild);
	
}
