function displayoff(element){
document.getElementById(element).style.display = 'none';
}


function displayon(element){
document.getElementById(element).style.display = 'inline';
}


function showHide(obj,id, msg1, msg2){
 var s=document.getElementById(id).style;
 if (s.display=='none'){
  obj.firstChild.data=msg2;
  s.display='block'
 }
 else {
  obj.firstChild.data=msg1;
  s.display='none'
 }
}


function popup(mylink, windowname, wd, hg)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open
(href, windowname, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+ wd +',height=' + hg);
return false;
}


function highlight(field)
{
field.focus();
field.select();
}


checked=false;
function checkedall(gallery_form)
{
var aa= document.getElementById('gallery_form');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++) 
{
aa.elements[i].checked = checked;
}
}

function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
} // lookup
	
function fill(thisValue) {
		setTimeout("$('#suggestions').hide();", 200);
		// document.upload_form.tags.focus();
		document.getElementById('inputString').focus();
		$('#inputString').val(thisValue);
}

	
function fill2(thisValue) {
		$('#inputString').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
		}


function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 1;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){
	if (this.id > 1) {document.getElementById('files_title').style.display = 'block';}
		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){
		
			// Element name -- what number am I?
			element.name = 'userfile' + this.id++;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){
			
				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				new_element.size = '30';
				

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				document.getElementById('files_max').style.display = 'block';
				element.disabled = true;
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );
		
		
		// Delete button
		var new_row_button = document.createElement( 'img' );
		new_row_button.src = 'delete.gif';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		new_row.innerHTML = '<span style="float: left">' + element.value.substr(0,30) + '</span>';

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};
