var PAGE_VERSION = '';


function loading_content(input, url_action)
{
	
	// Input must be fill and don't contain only a directory adress
	if(input.value!="" && input.value.lastIndexOf("/")!=-1 || input.value.lastIndexOf("\\"))
	{
		var form = input.form;
		
		//console.log
		
		if(form.id=='formFileUpload')
		{
			PAGE_VERSION 		= 'CREATE_FILE';
			var loading_file	= $('loading_file');
		//	var input 			= document.getElementById('file_input');
		}
		else if (form.id=='formFileVersionUpload')
		{
			PAGE_VERSION 		= 'ADD_VERSION';
			var loading_file	= $('loading_file_version');
		//	var input 			= document.getElementById('file_version_input');
		}
		else
		{
			PAGE_VERSION 		= 'CREATE_PERSONAL_FILE';
			var loading_file	= $('personal_loading_file');
		//	var input			= document.getElementById('personal_file_input');
		}
		
		
		// Save default settigns of the form
		form.bak_onsubmit	= form.onsubmit;
		form.bak_target		= form.target;
		form.bak_action		= form.action;
		
		// Submit form in an hidden iframe
		var iframe_id 			= 'iframe_file_'+ Math.random();
		var frame_conteneur 	= document.createElement('div');
		
		// This style hidde iframe
		frame_conteneur.className= 'container_hidden_iframe';
		
		// For Ie compatibility, iframe cant be create with an "createElement"
		frame_conteneur.innerHTML= '<iframe id="'+ iframe_id +'" name="'+ iframe_id +'"></iframe>';
		form.appendChild(frame_conteneur);
		
		// Submit form
		form.onsubmit 	= "";
		form.target 	= iframe_id;
		form.action 	= url_action;
		form.submit();
		
		// Save number of upload in progress
		form.setAttribute('nb_file_in_upload', parseInt( form.getAttribute('nb_file_in_upload') )+1 );
		
		// Restore default setting of the form
		form.onsubmit	= form.bak_onsubmit;
		form.target		= form.bak_target;
		form.action		= form.bak_action;
		
		// Start viewing of upload state
		setTimeout("checking_loading_content('"+ iframe_id +"')", 100);
		
		// Disable input file to block multiple upload
		//Element.show('loading_file');
		loading_file.show();
		
		input.disabled=true;
	}
}



function checking_loading_content(iframe_id)
{
	var iframe 		= window.frames[iframe_id];
	var iframe_node = document.getElementById(iframe_id);
	var form 		= iframe_node.parentNode.parentNode;
	
	var response = '';
	
	// Backup current iframe content
	if(iframe.document && iframe.document.body && iframe.document.body!=null )
		response = iframe.document.body.innerHTML;
	
	// If content don't be empty, upload is finished and response is returned
	if( response.length != 0 )
	{
		// Upload correctly endend
		valid_reponse(response);
		// Delete hidden iframe
		iframe_node.parentNode.removeChild(iframe_node);
		// Try an other delete method for navigator compatibility
		try {
			delete window.frames[iframe_id];
		} catch (e) {} // Do nothing
		
		// Update number of upload in progress
		form.setAttribute('nb_file_in_upload', form.getAttribute('nb_file_in_upload')-1);
	}
	// Upload is in progress
	else
	{
		setTimeout("checking_loading_content('"+ iframe_id +"')", 100);
	}
}


/**
 * Delete file associate to a content and display input to upload new content
 */
function delete_content()
{	
	if( PAGE_VERSION == 'CREATE_FILE' ) 
	{
		var input 				= document.getElementById('file_input');
		var input_hidden_file 	= document.getElementById('task_file_new_file_filename');
		var input_hidden_state 	= document.getElementById('task_file_new_file_value');
	}
	else if ( PAGE_VERSION == 'ADD_VERSION' ) 
	{ 
		var input 				= document.getElementById('file_version_input');
		var input_hidden_file 	= document.getElementById('file_version_new_file_filename');
		var input_hidden_state 	= document.getElementById('file_version_new_file_value');		
	}
	else
	{
		var input 				= document.getElementById('personal_file_input');
		var input_hidden_file 	= document.getElementById('file_version_new_file_filename');
		var input_hidden_state 	= document.getElementById('file_version_new_file_value');		
	}
	
	// Clean new filename and put "no file" to new state hidden input
	input_hidden_file.value 	= '';
	input_hidden_state.value 	= 0;
	
	// Clean file input value to have a new input file
		// command for IE
	input_tmp  		= input.cloneNode(true);
		// command for other navigator
	input_tmp.value = "";
	
	// Reactivation of input file
	input.parentNode.appendChild(input_tmp);
	input.parentNode.removeChild(input);
	input_tmp.disabled 	= false;
	
	// Show upload file and hide message
	Element.show('new_file');
	if($('button_local_file'))
		Element.show('button_local_file');
	Element.hide('loading_file');
	Element.hide('current_file');
}

function valid_sumbit( form, error_message )
{
	// Si un fichier est en cours de transfert, interdiction de soumettre le formulaire
	if( parseInt(form.getAttribute('nb_file_in_upload')) > 0 )
	{
		alert(error_message);
		return false;
	}
	return true;
}

function valid_reponse( response )
{
	// Upload correctly endend
	if( response.substr( 0, 2 )=='OK' )
	{		
		//var new_file = $('new_file')
		
		// Hide upload file and show message
		//Element.hide('new_file');
		
//		if( $('button_local_file') )
//			Element.hide('button_local_file');
			
	//	Element.hide('loading_file');
		
	//	Element.show('current_file');

		// Memorize filename in an hidden input and put new state in an other input hidden
		if( PAGE_VERSION == 'CREATE_FILE' ) 
		{
			var new_file 					= $('new_file');
			var loading_file				= $('loading_file');
			var current_file				= $('current_file');
			var input_hidden_file 			= $('task_file_new_file_filename');	
			var input_hidden_source_file	= $('source_file_filename');
			var input_hidden_state 			= $('task_file_new_file_value');
		}
		else if( PAGE_VERSION == 'ADD_VERSION' )
		{
			var new_file 					= $('new_file_version');
			var loading_file				= $('loading_file_version');
			var current_file				= $('current_file_version');			
			var input_hidden_file 			= $('file_version_new_file_filename');
			var input_hidden_source_file	= $('source_file_filename');
			var input_hidden_state 			= $('file_version_new_file_value');
		}
		else
		{
			var new_file 					= $('personal_new_file');
			var loading_file				= $('personal_loading_file');
			var current_file				= $('personal_current_file');			
			var input_hidden_file 			= $('personal_file_new_file_filename');
			var input_hidden_source_file	= $('source_file_filename');
			var input_hidden_state 			= $('personal_file_new_file_value');			
		}
		
		// Hide upload file and show message
		//Element.hide(new_file);
		new_file.hide();
		//if( $('button_local_file') )
			//Element.hide('button_local_file');

		//Element.hide(loading_file);
		loading_file.hide();
		
		//Element.show(current_file);
		current_file.show();
		
		$response_value = response.substr(3).split('---');
		
		input_hidden_file.value				= $response_value;
		
		if(input_hidden_source_file)
			input_hidden_source_file.value	= $response_value;
			
		input_hidden_state.value		= 3;
	}
	// An error occured while uploading
	else
	{
		// Display error message
		alert( response.substr(2) );
		// Allow user to upload an other file
		delete_content();
	}
}

function loading_move_content()
{
	// Hide upload file and show loading
	Element.hide( 'new_file' );
	if( $( 'button_local_file' ) )
		Element.hide( 'button_local_file' );
	Element.show( 'loading_file' );
	Element.hide( 'current_file' );
}
