// JavaScript Document

// show extended news list
function showExtendedListNews( source, target, current ) {
	
	var targetTable = document.getElementById( target );
	var rowStyle = 'reportsList_even';
	var offset = 5;
	
	if( targetTable.rows.length == offset ) {
		
		for( var key in source ) {
			
			var loopOffset = targetTable.rows.length + 1;
			
			// create row
			var lastRow = targetTable.rows.length;
			var newRow = targetTable.insertRow( lastRow );
			newRow.className = rowStyle;
			
			// create left cell
			var cellLeft = newRow.insertCell( 0 );
			cellLeft.name = 'news_'+loopOffset;
			cellLeft.className = 'c1';
			cellLeft.onmouseover = function() { this.parentNode.className = 'reportsList_hover'; }
			cellLeft.onmouseout = function() { this.parentNode.className = rowStyle; }
			cellLeft.onclick = function() { toggleCheckboxNews( this.name ); }
			
			// create checkbox
			var cb = document.createElement( 'input' );
			cb.name = 'news_'+loopOffset;
			cb.id = 'news_'+loopOffset;
			cb.type = 'checkbox';
			cb.className = 'formCheck';
			cb.value = source[key][0];
			cb.onclick = function() { toggleCheckboxNews( this.name ); }
			cellLeft.appendChild( cb );
			if( isSelected( 'news', source[key][0] ) ) {
				cb.checked = true;
			}

			var cellRight = newRow.insertCell( 1 );
			cellRight.name = 'news_'+loopOffset;
			cellRight.className = 'c2';
			cellRight.onmouseover = function() { this.parentNode.className = 'reportsList_hover'; }
			cellRight.onmouseout = function() { this.parentNode.className = rowStyle; }
			cellRight.onclick = function() { toggleCheckboxNews( this.name ); }			
			
			var textNode = document.createTextNode( source[key][1] );
			cellRight.appendChild( textNode );
			
			switch( rowStyle ) {
			case 'reportsList_even' :
				rowStyle = 'reportsList_odd';
				break;
			case 'reportsList_odd' :
				rowStyle = 'reportsList_even';
				break;
			}
		
		}
	}
}


// show extended sections list
function showExtendedListSections( source, target, current ) {
	
	var targetTable = document.getElementById( target );
	var rowStyle = 'reportsList_even';
	var offset = 5;
	
	if( targetTable.rows.length == offset ) {
		
		for( var key in source ) {
			
			var loopOffset = targetTable.rows.length + 1;
			
			var lastRow = targetTable.rows.length;
			var newRow = targetTable.insertRow( lastRow );
			newRow.className = rowStyle;
			
			var cellLeft = newRow.insertCell( 0 );
			cellLeft.name = 'sections_'+current+'_'+loopOffset;
			cellLeft.className = 'c1';
			cellLeft.onmouseover = function() { this.parentNode.className = 'reportsList_hover' }
			cellLeft.onmouseout = function() { this.parentNode.className = rowStyle }
			cellLeft.onclick = function() { toggleCheckboxSections( this.name ); }

			var cb = document.createElement( 'input' );
			cb.name = 'sections_'+current+'_'+loopOffset;
			cb.id = 'sections_'+current+'_'+loopOffset;
			cb.type = 'checkbox';
			cb.className = 'formCheck';
			cb.value = source[key][0]
			cb.onclick = function() { toggleCheckboxSections( this.name ); }
			cellLeft.appendChild( cb );
			if( isSelected( 'sections', source[key][0] ) ) {
				cb.checked = true;
			}			

			var cellRight = newRow.insertCell( 1 );
			cellRight.name = 'sections_'+current+'_'+loopOffset;
			cellRight.className = 'c2';
			cellRight.onmouseover = function() { this.parentNode.className = 'reportsList_hover'; }
			cellRight.onmouseout = function() { this.parentNode.className = rowStyle; }
			cellRight.onclick = function() { toggleCheckboxSections( this.name ); }	

			var textNode = document.createTextNode( source[key][1] );
			cellRight.appendChild( textNode );
			
			switch( rowStyle ) {
			case 'reportsList_even' :
				rowStyle = 'reportsList_odd';
				break;
			case 'reportsList_odd' :
				rowStyle = 'reportsList_even';
				break;
			}
		
		}
	}
}



// hide extended listings
function hideExtendedList( target ) {
	
	var targetTable = document.getElementById( target );
	var offset = 6;
	
	while( targetTable.rows.length >= offset ) {
		targetTable.deleteRow( targetTable.rows.length - 1 );
	}
	
}





// function to toggle news checkboxes
function toggleCheckboxNews( target ) {
	var targetBox = document.getElementById( target ); 
	if( targetBox.checked == true ) {
		targetBox.checked = false;
		modifyStealth( 'news', targetBox.value, 'n' );
	} else {
		targetBox.checked = true;
		modifyStealth( 'news', targetBox.value, 'y' );
	}
}


// function to toggle section checkboxes
function toggleCheckboxSections( target ) {
	var targetBox = document.getElementById( target );
	if( targetBox.checked == true ) {
		targetBox.checked = false;
		modifyStealth( 'sections', targetBox.value, 'n' );
	} else {
		targetBox.checked = true;
		modifyStealth( 'sections', targetBox.value, 'y' );
	}
}





// function to check if item is enabled in the stealth array
function isSelected( type, id ) {
	if( type == 'news' ) {
		for( var key in stealthArray_news ) {
			if( stealthArray_news[key][0] == id && stealthArray_news[key][1] == 'y' ) {
				return true;
			}
		}
		return false;
	} else if( type =='sections' ) {
		for( var key in stealthArray_sections ) {
			if( stealthArray_sections[key][0] == id && stealthArray_sections[key][1] == 'y' ) {
				return true;
			}
		}
		return false;
	}
}


// function to enable disable items from the stealth array
function modifyStealth( type, id, value ) {
	if( type == 'news' ) {
		for( var key in stealthArray_news ) {
			if( stealthArray_news[key][0] == id ) {
				stealthArray_news[key][1] = value;
			}
		}
		buildStealth( 'news' );
	
	} else if( type == 'sections' ) {
		for( var key in stealthArray_sections ) {
			if( stealthArray_sections[key][0] == id ) {
				stealthArray_sections[key][1] = value;
			}
		}
		buildStealth( 'sections' );
	}
}


// build Stealth Arrays
function buildStealth( type ) {
	
	var outputString = '\n';
	
	if( type == 'news' ) {
		var target = document.getElementById( 'newsStealth' );
		
		for( var key in stealthArray_news ) {
			if( stealthArray_news[key][1] == 'y' ) {
				outputString += '<input name="newsStealth[]" type="hidden" value="'+stealthArray_news[key][0]+'" />\n';
			}
		}
		target.innerHTML = outputString;
		
	} else if( type == 'sections' ) {
		var target = document.getElementById( 'sectionsStealth' );
		
		for( var key in stealthArray_sections ) {
			if( stealthArray_sections[key][1] == 'y' ) {
				outputString += '<input name="sectionsStealth[]" type="hidden" value="'+stealthArray_sections[key][0]+'" />\n';
			}
		}
		target.innerHTML = outputString;
	}
	
}






// submit form
function submitReport( type ) {
	document.reports.action.value = type;
	document.reports.submit();
}
