function launchWindow( url, name, percent, scrollbars ) {
	if( arguments[4] ) {
		menubar = 'yes';
	} else {
		menubar='no';	
	}
	if( arguments[5] ) {
		toolbar = 'yes';
	} else {
		toolbar='no';	
	}
	var width = screen.availWidth;
	var height = screen.availHeight;
	var top = height * ( 1 - percent )/ 2;
	var left = width * ( 1 - percent )/ 2;
	if( scrollbars ) scrollbars = 'yes';
	var w = window.open( url, name, 'status=yes,resizable=yes,top=' + top + ',left=' + left + ',scrollbars=' + scrollbars + ',menubar=' + menubar + ',toolbar=' + toolbar);
	w.resizeTo(screen.availWidth * percent,screen.availHeight * percent);
}

// Hide an element based on its ID
function hide( id ) {
	document.getElementById( id ).className = 'hidden';
}

// Display an element based on its ID
function show( id ) {
	document.getElementById( id ).className = 'display';
}

// Toggle between 'hide' and 'show'
function toggle( id ) {
	//document.selection.empty();
	var el = document.getElementById( id );
	if( el.className == 'display' ) {
		el.className = 'hidden'
	} else {
		el.className = 'display'
	}
}

function normalizeId( id ) {
	// Remove the leading character from the id, leaving just the numerical portion
	return id.substring( 1 );
}

function refreshOpener() {
	try {
		opener.location.reload();
	} catch(e) {
		alert( e.message );	
	}
		
}

function openSurvey( id ) {
	// Prep the ID
	id = normalizeId( id );
	//document.selection.empty();
	w = window.open("surveyProperties.php?id=" + id, "surveyWindow", "resizable=yes,status=yes,top=0,left=0,scrollbars=yes");
	w.resizeTo(screen.availWidth,screen.availHeight);
}

function editQuestion( id ) {
	// Prep the ID
	id = normalizeId( id );
	//document.selection.empty();
	launchWindow( "editQuestion.php?id=" + id, "questionWindow", .8, true );
}

function editContact( id ) {
	// Prep the ID
	id = normalizeId( id );
	//document.selection.empty();
	launchWindow( "editContact.php?id=" + id, "contactWindow", .95, true );
}

function editUser( id ) {
	// Prep the ID
	id = normalizeId( id );
	//document.selection.empty();
	launchWindow( "editUser.php?id=" + id, "userWindow", .9, true );
}

function addQuestionToList( el ) {
	var TYPECELL = 0;
	var QUESTIONTEXTCELL = 1;
	var mainDoc = parent.parent.document;
	var selectedList = mainDoc.getElementById( "selectedQuestionsBody" );
	var id = el.id;
	
	// Clear the selection
	//document.selection.empty();

	// Make sure the id doesn't already exist
	if( mainDoc.getElementById( id ) ) {
		alert( "The question has already been added to the list" );
	} else {
		var row = selectedList.insertRow();
		row.id = id;
		row.ondblclick = removeElement;
		var cells = el.cells;
		var type = row.insertCell();
		type.innerHTML = cells[ TYPECELL ].innerHTML;
		var questionText = row.insertCell();
		questionText.innerHTML = cells[ QUESTIONTEXTCELL ].innerHTML;
	}
}

function removeElement() {
	// Clear the selection
	//document.selection.empty();
	this.removeNode(true);
}

function setElementText( id, text ) {
	var el = document.getElementById( id );
	if( el ) {
		el.innerHTML = text;	
	}
}

function reloadComponent( id ) {
	var el = document.getElementById( id );
	if( el ) {	
		var dispatch = el.getAttribute( "dispatch" );
		if( dispatch ) {
			var xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
			xmlhttp.open( "GET", document.location + "&dispatch=" + dispatch, false);
			xmlhttp.send();
			if( xmlhttp.status == 200 ) {
				el.outerHTML = xmlhttp.responseText;
			} else {
				alert( "Unable to update " + id + "; " + xmlhttp.statusText );
			}			
			if( this.init ) {
				this.init();	
			}
		}
	}
}

function padDigits( i ) {
	if( i < 10 ) {
		i = "0" + i;
	}
	return i;
}

function disable( id ) {
	var el = document.getElementById( id );
	if( el ) {
		el.disabled = true;
	}	
}

function doSort( col, rev ) {
	form = document.forms['displayTable'];
	form.sortCol.value = col;
	form.reverseSort.value = rev;
	form.submit();
}

function doGraph( year, indicatorId, dispatch ) {
	form = document.forms['displayTable'];
	form.graphYear.value = year;
	form.graphIndicatorId.value = indicatorId;
	form.target = 'graph';
	form.method = 'get';
	form.submit();
}

function selectAllByName( name ) {
	var form;
	var fields;
	if( arguments[1] ) {
		form = arguments[1];
		fields = getFieldsByName( name, form );
	} else {
		fields = getFieldsByName( name );		
	}
	for( var i = 0; i < fields.length; i++ ) {
		var element = fields[ i ];
		if( element.type == 'checkbox' ) {
			element.checked = true;
		}
	}
}
function unSelectAllByName( name ) {
	var form;
	var fields;
	if( arguments[1] ) {
		form = arguments[1];
		fields = getFieldsByName( name, form );
	} else {
		fields = getFieldsByName( name );		
	}
	for( var i = 0; i < fields.length; i++ ) {
		var element = fields[ i ];
		if( element.checked ) {
			element.checked = false;
		}
	}
}
function getFieldsByName( name ) {
	var form;
	var fields = new Array();
	if( arguments[1] ) {
		form = arguments[1];
	} else {
		form = document.forms[0];
	}
	var numElements = form.elements.length;
	for( var i = 0; i < numElements; i++ ) {
		if( form.elements[ i ].name == name ) {
			fields.push( form.elements[ i ] );
		}
	}
	return fields;
}
