

/*
	Select the image
*/
function SetImage ( sakid, imageid )
{

	var listid = "list" + sakid;
	// Collapse the list
	ExpandList ( listid );
	
	doSet ( imageid, sakid );
}



function OppdaterBildetekst ( bilde, ele )
{

	var xmlHttp;
  	try
    {
    	xmlHttp=new XMLHttpRequest();
    }
  	catch (e)
    {
    	try
      	{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
    	catch (e)
      	{
      		try
        	{
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
      		catch (e)
        	{
        		alert("Your browser does not support AJAX!");
        	return false;
        	}
      	}
    }
    
    xmlHttp.onreadystatechange=function()
  	{
  		if(xmlHttp.readyState==4)
    	{
   			document.location.reload();
    	}
  	}
  	
  	var editor = tinyMCE.getInstanceById(ele);
	var innhold = editor.getHTML();
	innhold = innhold.replace  (/&/g, "zxy1WWWZX1");
  	var url = "admin.php?oppdaterbildetekst=" + bilde + "&verdi=" + innhold;
	xmlHttp.open ( "GET", url, true );
  	xmlHttp.send ( null );
}

function DestroyImage ( bilde, galleri )
{
	var xmlHttp;
  	try
    {
    	xmlHttp=new XMLHttpRequest();
    }
  	catch (e)
    {
    	try
      	{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
    	catch (e)
      	{
      		try
        	{
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
      		catch (e)
        	{
        		alert("Your browser does not support AJAX!");
        	return false;
        	}
      	}
    }
    
    xmlHttp.onreadystatechange=function()
  	{
  		if(xmlHttp.readyState==4)
    	{
   			document.location.reload();
    	}
  	}
  		
 	var url = "admin.php?destroyimage=" + bilde + "&gallerislett=" + galleri;
	xmlHttp.open ( "GET", url, true );
	xmlHttp.send ( null );
}

function AddGalleri ( bruker  )
{
	
  var xmlHttp;
  try
    {
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    
    xmlHttp.onreadystatechange=function()
  	{
  		if(xmlHttp.readyState==4)
    	{
   			document.location.reload();
    	}
  	}
  	
  		var verdi = document.getElementById ( "galnavn" ).value;
  		
 	  	var url = "admin.php?laggalleri=" + verdi + "&galleribruker=" + bruker
		xmlHttp.open ( "GET", url, true );
	  	xmlHttp.send ( null );
}


function doSet ( idbilde, idsak)
{
	
  var xmlHttp;
  try
    {
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    
    xmlHttp.onreadystatechange=function()
  	{
  		if(xmlHttp.readyState==4)
    	{
   			document.location.reload();
    	}
  	}
  	
 	  	var url = "admin.php?setsakbilde=" + idbilde + "&sak=" + idsak;
 
		xmlHttp.open ( "GET", url, true );
	  	xmlHttp.send ( null );
}

/*
	Expand an image list
*/
function ExpandList ( listid )
{

	var ele = document.getElementById ( listid );
	
	if ( ele.style.display == "block" )
		ele.style.display = "none";
		
	else
		ele.style.display = "block";
}


/*
	Function to open a gallery when it is selected in the <select> element
*/
function OpenGallery ( openpath )
{

	// Get the element
	var element = document.getElementById ("galselect");

	
	// Get the index of the selected entry
	var selected = element.selectedIndex;
	

	// Get the value of the selected element
	var value = element.options[selected].value;
	
	if ( value > 0 )
	{
		// Open the gallery
		window.location.assign( openpath + value );
	}
}


/*
	Clear a text field
*/
function ClearField ( id1 )
{
	// Get the element
	var element = document.getElementById ( id1 );

	// Clear the value
	element.value = "";
}





/*
	Multiple file selector 
*/
function FileList ( target_list, max )
{

	// Set the local variables
	this.target = target_list;
	this.id = 0;
	this.count = 0;
	
	if ( max )
	{
		this.max = max;
	}
	else
	{
		this.max = -1;
	}
	
	
	// Add an element to the list
	this.AddElement = function  ( element )
	{
		// Make sure it's a file input element
		if ( element.tagName == 'INPUT' && element.type == 'file' )
		{
			// Set the name and increase the id counter
			element.name = 'bilder[]';
			this.id++;
			
			// Make a pointer to this list
			element.fileList = this;
			
			element.onchange = function ()
			{
				// Create a new input element in the document
				var newElement = document.createElement ( 'input' );
				
				var newDesc = document.createElement ( 'input' );
				newDesc.name = 'desc[]';
				
				
				
				// Set the type to file
				newElement.type = 'file';
				newElement.accept = 'image/*';
				
				// Add the newly created element to the list
				this.parentNode.insertBefore ( newElement, this );
				
				// Add it to the file list
				this.fileList.AddElement ( newElement );
				
				// Add it to the list
				this.fileList.AddRow ( this, newDesc);
				
				// Hide the element
				this.style.display = 'none';
			};
			
			this.count++;
			this.current = element;
		}
	}
	
	this.AddRow = function( element, desc )
	{

		// Row div
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';

		// Description button
		desc.value = 'Bildebeskrivelse';
		desc.style.width = '100px';
		desc.style.marginLeft = 10;
		desc.style.marginRight = 10;
		desc.id = 'desc_' + this.id;
		
		// Clear the field when it gets focus
		desc.onfocus = function ()
		{
			ClearField ( desc.id );
		}
		
		// 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.fileList.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.fileList.current.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 = element.value;
		
		var first = new_row.firstChild;
		
		// Add button
		new_row.insertBefore( new_row_button, first );
		
		new_row.insertBefore ( desc, first );
		
		
		
		// Add it to the list
		this.target.appendChild( new_row );
		
		
		
	};
	
};