var current_val=true;
var tooltips=new Array();

tooltips['hide']=new Array();
tooltips['hide']['img']='nz/html/mag.png'
tooltips['hide']['msg']='Results of this type will not be available in the previous/next links'

tooltips['show']=new Array();
tooltips['show']['img']='nz/html/mag.png'
tooltips['show']['msg']='Results of this type are now available in the previous/next links'

function hide_desc(){
	
	if (!document.styleSheets) return;
	
	var theRules = new Array();
	
	if (document.styleSheets[1].cssRules)
		theRules = document.styleSheets[1].cssRules
	else if (document.styleSheets[1].rules)
		theRules = document.styleSheets[1].rules
	else return;
	
	var rule_style=theRules[theRules.length-1].style;
	var desc_text=document.getElementById('hide_desc');
	
	(current_val==true) ? rule_style.display='none' : rule_style.display='block';
	(current_val==true) ? desc_text.firstChild.nodeValue='Show descriptions' : desc_text.firstChild.nodeValue='Hide descriptions';
	
	var spans=document.getElementsByTagName('span');
	
	for(var i=0; i<spans.length; i++){
		if(spans[i].className=='toggle')
			(current_val==true) ? spans[i].firstChild.firstChild.nodeValue='Show' : spans[i].firstChild.firstChild.nodeValue='Hide';
	}
	
	current_val=!current_val;
	
	//desc_text.firstChild.nodeValue='Show descriptions';
	
}

function load_triggers(){
	//document.getElementById('hide_desc').onclick=new Function("hide_desc(); return false;");
}

function show_all(el, num_instances){
	var parent=el.parentNode.parentNode;

	var children=parent.childNodes;
	
	for(var i=0; i<children.length; i++){
		if(children[i].className=='hidden_instances'){
			if(children[i].style.display!='block'){
				children[i].style.display='block';
				el.firstChild.nodeValue='Hide extra results';
			} else {
				children[i].style.display='none';
				el.firstChild.nodeValue='Show all ' + num_instances + ' results';
			}
			break;
		}
	}
}

function hide_extras(el, anchor_count, num_results){
	el.parentNode.parentNode.style.display='none';
	
	var anchor=document.getElementById('a' + anchor_count);
	
	window.location='#a' + anchor_count;
	
	anchor.parentNode.getElementsByTagName('a')[1].firstChild.nodeValue='Show all ' + num_results + ' results';
	
}

var intervalTimer=0;

function filter_results(el, offset){
	
	var type=null;
	
	if(el.className!='filtered'){
		el.className='filtered';
		el.parentNode.className='filtered';
		type='hide';
	} else {
		el.className='';
		el.parentNode.className='';
		type='show';
	}
		
	//position the tooltip
		
	var left=0;
	var top=0;
	while(el.offsetParent){
		left+=el.offsetLeft;
		top+=el.offsetTop;
		el=el.offsetParent;
	}	
	
	var tooltip=document.getElementById('tooltip');
	
	if(tooltip.hasChildNodes){
		while (tooltip.hasChildNodes()){
		  tooltip.removeChild(tooltip.firstChild);
		}	
	}
	
	var img=document.createElement('img');
	img.setAttribute('src', tooltips[type]['img']);
	img.setAttribute('width', 22);
	img.setAttribute('height', 22);
	
	var p=document.createElement('p');
	var txt=document.createTextNode(tooltips[type]['msg']);
	p.appendChild(txt);
	
	tooltip.appendChild(img);
	tooltip.appendChild(p);
	
	left-=tooltip.offsetWidth;
	left-=35;
	tooltip.style.visibility='visible';
	tooltip.style.top=top + 'px';
	tooltip.style.left=left + 'px';
	tooltip.style.opacity=5;
	intervalTimer=setInterval("fadeout('tooltip')", 100);
}

function fadeout(el_id){
	
	var tooltip=document.getElementById(el_id);
	var val=tooltip.style.opacity;
	if(val<=0){
		tooltip.style.visibility='hidden';
		clearInterval(intervalTimer);
	} else {
		tooltip.style.opacity=tooltip.style.opacity-0.1;
	}

}

function next_result(el, header_count){
	
	var h3s=document.getElementsByTagName('h3');
	
	var count=0;
	var use_next=false;
	var found=false;
	for(var i=0; i<h3s.length; i++){
		if(h3s[i].className=='header'){
			count++;
			
			if(use_next){
				var anchor=h3s[i].getElementsByTagName('a')[0];
				var bits=anchor.className.split(' ');
				
				var can_jump=false;
				for(var j=0; j<bits.length; j++){
					if(document.getElementById('rl_' + bits[j]).className!='filtered'){
						can_jump=true;
						break;
					}
				}
				if(can_jump){
					window.location='#' + anchor.name;
					found=true;
					break;	
				}
			}
			
			if(count==header_count){
				use_next=true;
			}
						
		}
	}
	
	if(!found){
		var div_alert=document.getElementById('alert');
		var div_msg=document.getElementById('alert_msg');
		
		clear_nodes(div_msg);
		
		add_element(div_msg, 'h4', {text: 'The end'});
		add_element(div_msg, 'p', {text: 'There are no more results for the type you are looking at'});
		
		position_tooltip(el, div_alert);
	}
	
}

function position_tooltip(el, tooltip){
	var left=0;
	var top=el.offsetHeight;
	while(el.offsetParent){
		left+=el.offsetLeft;
		top+=el.offsetTop;
		el=el.offsetParent;
	}
	
	tooltip.style.opacity=1;
	tooltip.style.top=top + 'px';
	tooltip.style.left=left + 'px';
	tooltip.style.visibility='visible';	
}

function previous_result(el, header_count){
	
	var h3s=document.getElementsByTagName('h3');
	var count=document.getElementById('h_total').className.substr(1);
	var use_next=false;
	var found=false;
	
	for(var i=h3s.length-1; i>=0; i--){
		if(h3s[i].className=='header'){
			
			count--;
			
			if(use_next){
				var anchor=h3s[i].getElementsByTagName('a')[0];
				var bits=anchor.className.split(' ');
				
				var can_jump=false;
				for(var j=0; j<bits.length; j++){
					if(document.getElementById('rl_' + bits[j]).className!='filtered'){
						can_jump=true;
						break;
					}
				}
				if(can_jump){
					window.location='#' + anchor.name;
					found=true;
					break;	
				}
			}
			
			if(count==header_count-1){
				use_next=true;
			}
						
		}
	}
	
	if(!found){
		var div_alert=document.getElementById('alert');
		var div_msg=document.getElementById('alert_msg');
		
		clear_nodes(div_msg);
		
		add_element(div_msg, 'h4', {text: 'The start'});
		add_element(div_msg, 'p', {text: 'This is the first result for the type you are looking at'});
		
		position_tooltip(el, div_alert);	
	}
	
}

function toggle_header(el){
	var div=el.parentNode.parentNode.getElementsByTagName('div')[0];
	
	if(div.style.display=='' || div.style.display=='none'){
		div.style.display='block';
		el.firstChild.nodeValue='Hide';
		el.parentNode.parentNode.className='';
	} else {
		div.style.display='none';
		el.firstChild.nodeValue='Show';
		el.parentNode.parentNode.className='no_print';
	}
}

var current_tab=''
function show_toc(type){
	var el=document.getElementById('toc_' + type);

	var bits=new Array();
	bits[0]='pass';
	bits[1]='user';
	bits[2]='check';
	bits[3]='warning';
	bits[4]='fail';
	
	for(var i=0; i<bits.length; i++){
		document.getElementById('toc_' + bits[i]). style.display='none';
		document.getElementById('tab_' + bits[i]).className='';
		rl_link=document.getElementById('rl_' + bits[i]);
		rl_link.className='filtered';
		rl_link.parentNode.style.backgroundColor='#ddd';
		
	}
	
	el.style.display='block';
	document.getElementById('tab_' + type).className='selected';
	rl_link=document.getElementById('rl_' + type);
	//rl_link.style.textDecoration='underline';
	rl_link.className='';
	rl_link.parentNode.style.backgroundColor='transparent';
	
}

function close_alert(){
	document.getElementById('alert').style.opacity=1;
	intervalTimer=setInterval("fadeout('alert')", 100);
}

function update_print_view(el, look_for){
	
	var type=null;
	
	/*if(el.className!='filtered'){
		el.className='filtered';
		el.parentNode.className='filtered';
		//type='hide';
	} else {
		el.className='';
		el.parentNode.className='';
		//type='show';
	}
	*/
	
	var li=el.parentNode.parentNode;
	if(li.className=='print_no'){
		li.className='print_yes';	
	} else {
		li.className='print_no';
	}
	
	parent = document.getElementsByTagName('body')[0];
	children = parent.childNodes;
	
	// loop thru all child nodes
	for(i=0; i<children.length; i++){
		child = children[i];
		// do something with node here
		class_name=child.className;
		if(class_name){
			bits=class_name.split(' ');
			if(bits[0]=='total_container' && bits[1]==look_for){
				if(bits.length<2 && bits[2]=='no_print'){
					child.className=bits[0] + ' ' + bits[2];	
				} else {
						
					child.className=child.className + ' no_print';		
				}
				
			}
		}
	}
	//fix up the table of contents
	toc=document.getElementById('toc_container');
	divs=toc.getElementsByTagName('div');
	for(i=0; i<divs.length; i++){
		if(divs[i].id=='toc_' + look_for){
			if(divs[i].className=='no_print')
				divs[i].className='';
			else
				divs[i].className='no_print';
		}
	}
}

function show_print_options(el){
	clear_nodes(el);
	
	ul=document.getElementById('print_options');
	
	if(ul.style.display=='none' || ul.style.display==''){
		ul.style.display='block';
		add_text(el, 'Hide');
	} else {
		ul.style.display='none';
		add_text(el, 'Show');
	}
	
}

function show_options(el, type){
	clear_nodes(el);
	
	ul=document.getElementById(type);
	
	if(ul.style.display=='block' || ul.style.display==''){
		ul.style.display='none';
		add_text(el, 'Show');
	} else {
		ul.style.display='block';
		add_text(el, 'Hide');
	}
}

function confirm_checked(el){
	var img=el.parentNode.parentNode.getElementsByTagName('img')[0];
	var span=el.parentNode.getElementsByTagName('span')[0];
	var container=el.parentNode.parentNode.parentNode.parentNode.parentNode;
	var anchor=container.getElementsByTagName('h3')[0].getElementsByTagName('a')[0];
	
	var src=img.src;
	var name=anchor.name;
	var error_type=span.className;
	var toc=document.getElementById('toc_' + error_type);
	
	var check_text='This requirement cannot be automatically checked.';
	var user_text='The site administrator needs to confirm this.';
	
	if(error_type=='user')
		reverted_text=user_text;
	else
		reverted_text=check_text;
	
	clear_nodes(el);
	clear_nodes(span);
	delete_notes=false;
	if(src.indexOf('-bw')!=-1) {
		img.src='/nz/html/question.png';
		add_text(el, 'Click here to confirm this');
		add_text(span, reverted_text);
		
		notes=el.parentNode.parentNode.getElementsByTagName('div')[0];
		el.parentNode.parentNode.removeChild(notes);
		
		filtered=false;
		
		delete_notes=true;
		
	}else {
		img.src='/nz/html/question-bw.png';
		add_text(el, 'Revert back to unchecked');
		add_text(span, 'This rule has been checked.');
		notes=add_element(el.parentNode.parentNode, 'div');
		notes.className='result_notes';
		
		ta=add_element(notes, 'textarea', {text: 'Enter notes here'});
		ta.onblur=new Function('make_p(this);');
		ta.focus();
		
		filtered=true;
	}
	
	//fix up the table of contents
	var as=toc.getElementsByTagName('a');
	
	for(var i=0; i<as.length; i++){
		href=as[i].href;
		href_anchor=href.substr(href.lastIndexOf('#'), href.length);
		if(href_anchor=='#' + name){
			if(filtered){
				as[i].className='approved';
				container.className=container.className + ' no_print';
			} else {
				as[i].className='';
				container.className=container.className.substring(0, container.className.lastIndexOf(' '));
			}
			break;
		}
	}
	
	if(delete_notes){
		var divs=toc.getElementsByTagName('div');
		for(var i=0; i<divs.length; i++){
			class_name=divs[i].className;
			if(class_name==name + ' notes'){
				clear_nodes(divs[i]);
			}
		}
	}
}

function make_p(el){
	
	var val=el.value
	var span=el.parentNode.parentNode.getElementsByTagName('span')[0];
	var container=el.parentNode.parentNode.parentNode.parentNode.parentNode;
	var anchor=container.getElementsByTagName('h3')[0].getElementsByTagName('a')[0];
	
	var name=anchor.name;
	
	if(val=='Enter notes here' || val=='No notes entered')
		val='';
	
	p_class='';
	if(val==''){
		val='No notes entered';
		p_class='no_print';
	}
	
	p=add_element(el.parentNode, 'p', {text: val});
	p.className='p_class';
	p.onclick=new Function('make_textarea(this);');
	
	el.parentNode.removeChild(el);
	
	//update the toc
	var toc=document.getElementById('toc_' + span.className);
	
	//fix up the table of contents
	if(val!='No notes entered'){
		var divs=toc.getElementsByTagName('div');
		var use_next=false;
		for(var i=0; i<divs.length; i++){
			class_name=divs[i].className;
			if(class_name==name + ' notes'){
				clear_nodes(divs[i]);
				add_text(divs[i], val);
			}
		}
	}
}

function make_textarea(el){
	
	var val=el.firstChild.nodeValue;
	
	p_class='';
	if(val=='' || val=='No notes entered'){	
		p_class='no_print';
	}
	
	ta=add_element(el.parentNode, 'textarea', {text: val});
	ta.className='p_class';
	ta.onblur=new Function('make_p(this);');
	ta.focus();
	
	el.parentNode.removeChild(el);
}
				
window.onload=load_triggers;