// place the ids of every element...
var ids = new Array('projectInfoOpener', 'projectForm','projectInfo', 'bugHdr', 'bugHdrEdit', 'logUpdate', 'logUpdateOpener', 'logModify', 'filterClosed' , 'filterOpen');

function switchid(page,id){	
	hideallids(page);
	showdiv(id);
}

function hideallids(page){
	// loop through the array and hide each element by id...
	for (var i=0;i<ids.length;i++){
		// check for page/div call (p = project; b = bug; l = log)
		if (ids[i].substr(0,1) == page) {
			hidediv(ids[i]);
		}
	}		  
}

function hidediv(id) {
	// safe function to hide an element with a specified id...
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	// safe function to show an element with a specified id...
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}


var TimeToFade = 500.0;

function fadediv(id) {
	var element = document.getElementById(id);
	if (element == null) return;
  
	if (element.FadeState == null) {
		if(element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1') {
			element.FadeState = 2;
		} else {
			element.FadeState = -2;
		}
	}

	if (element.FadeState == 1 || element.FadeState == -1) {
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
	} else {
		element.FadeState = element.FadeState == 2 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade;
		setTimeout("animateFade(" + new Date().getTime() + ",'" + id + "')", 33);
	}
}

function animateFade(lastTick, eid) { 
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	var element = document.getElementById(eid);

	if (element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		hidediv(eid);
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	
	if (element.FadeState == 1) newOpVal = 1 - newOpVal;
	
	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}




function swapStatus() {
	t = document.getElementById("state_id");
	s = document.getElementById("status_id");
	s.options.length = 0;
	
	// get state value...
	stateVal = t.options[t.options.selectedIndex].value;
	stateVal = stateVal.toString();
	
	// explode the state divisions...
	valuesArray = statusValues.split(";");
	
	// find the proper value set...
	for (x = 0; x < valuesArray.length; x++) {
		valueSet = valuesArray[x].split("&");
		if (valueSet[0] == stateVal) {
			// explode the options...
			optionIDs = valueSet[1].split(",");
			optionValues = valueSet[2].split(",");
			for (y = 0; y < optionIDs.length; y++) {
				s.options[y] = new Option(optionValues[y],optionIDs[y]);
			}
			break;
		}
	}
	// fix the assignments...
	swapAssignment();
}

function swapAssignment() {
	// start with assignment to Leader...
	// check for status, if techFlag use techs; if reviewFlag use reviewers
	// assumes first set = Leader and will default to that if nothing found
	
	t = document.getElementById("status_id");
	s = document.getElementById("next_user_id");
	s.options.length = 0;
	
	// get status value...
	statusVal = t.options[t.options.selectedIndex].value;
	statusVal = statusVal.toString();
	
	// explode the assignment divisions...
	valuesArray = assignmentValues.split(";");
	
	// find the proper value set...
	foundValues = false;
	for (x = 0; x < valuesArray.length; x++) {
		valueSet = valuesArray[x].split("&");
		if (valueSet[0] == statusVal) {
			// explode the options...
			optionIDs = valueSet[1].split(",");
			optionValues = valueSet[2].split(",");
			for (y = 0; y < optionIDs.length; y++) {
				s.options[y] = new Option(optionValues[y],optionIDs[y]);
			}
			foundValues = true;
			break;
		}
	}
	if (foundValues == false) {
		valueSet = valuesArray[0].split("&");
		s.options[0] = new Option(valueSet[2],valueSet[1]);
	}
}

function swapFilterStatus() {
	t = document.getElementById("filterState");
	s = document.getElementById("filterStatus");
	s.options.length = 0;
	
	// get state value...
	stateVal = t.options[t.options.selectedIndex].value;
	stateVal = stateVal.toString();
	
	// explode the state divisions...
	valuesArray = statusValues.split(";");
	
	// find the proper value set...
	for (x = 0; x < valuesArray.length; x++) {
		valueSet = valuesArray[x].split("&");
		if (valueSet[0] == stateVal) {
			// explode the options...
			optionIDs = valueSet[1].split(",");
			optionValues = valueSet[2].split(",");
			for (y = 0; y < optionIDs.length; y++) {
				s.options[y] = new Option(optionValues[y],optionIDs[y]);
			}
			break;
		}
	}
	// fix the assignments...
	swapFilterMember();
}

function swapFilterMember() {
	// start with assignment to Any...
	// check for status, if techFlag use techs; if reviewFlag use reviewers
	// assumes first set = Leader and will default to that if nothing found
	
	t = document.getElementById("filterStatus");
	s = document.getElementById("filterMember");
	s.options.length = 0;
	
	// get status value...
	statusVal = t.options[t.options.selectedIndex].value;
	statusVal = statusVal.toString();
	
	// explode the assignment divisions...
	valuesArray = assignmentValues.split(";");
	
	// find the proper value set...
	foundValues = false;
	for (x = 0; x < valuesArray.length; x++) {
		valueSet = valuesArray[x].split("&");
		if (valueSet[0] == statusVal) {
			// explode the options...
			optionIDs = valueSet[1].split(",");
			optionValues = valueSet[2].split(",");
			for (y = 0; y < optionIDs.length; y++) {
				s.options[y] = new Option(optionValues[y],optionIDs[y]);
			}
			foundValues = true;
			break;
		}
	}
	if (foundValues == false) {
		valueSet = valuesArray[1].split("&");
		s.options[0] = new Option(valueSet[2],valueSet[1]);
	}
}

function resetFilter() {
	document.getElementById("filterPriority").selectedIndex = 0;
	document.getElementById("filterState").selectedIndex = 0;
	document.getElementById("filterStatus").selectedIndex = 0;
	document.getElementById("filterMember").selectedIndex = 0;
	document.getElementById("filter").submit();
}

function openHelp($rootpath) {
	helpWindow = window.open ($rootpath + "/help/","helpwindow","location=0,status=0,scrollbars=0,width=640,height=600");
	helpWindow.focus();
	setTimeout('helpWindow.focus()',200);
}

function submitForm(theForm,theConfirm) {
	if (theConfirm != "") {
		if (confirm(theConfirm)) {
			document.getElementById(theForm).submit();
		}
	} else {
		document.getElementById(theForm).submit();
	}
}

function focusSearch() {
	theField = document.getElementById("searchTerms");
	if (theField.value == "search the logs") theField.value = "";
}

function modifyLog(logID) {
	theText = document.getElementById(logID).innerHTML;
	theForm = document.getElementById("modifyForm");
	theForm.log_id.value = logID.substr(1) + "...";
	// remove junk from notes...
	// empty notes...
	theText = theText.replace("none<span>Click to modify.</span>","");
	// non-empty notes...
	theText = theText.replace("<span>Click to modify.</span>","");
	// line-breaks...
	theText = theText.replace(/<br>/g,"\n");
	theText = theText.replace(/<br \/>/g,"\n");
	//theText = theText.replace(/<span class=\"txtNull\">none<\/span>/g,"");
	theForm.notes.value = theText;
	location.href="#modifyBox";
	switchid('l','logModify');
}

function trialSubmit() {
	var submitForm = document.getElementById("subscriptionForm");
	submitForm.trial.value = 1;
				submitForm.submit();
}