/**
 * Start upload of an input file content to a temporary directory
 * 
 * @author mrandy
 * @since 2008-04-15
 * @version 2008-04-15
 * @param string type - type of content uploaded
 * @param string url_action - url of upload treatment script
 */
function loading_content(input, url_action, type)
{
	// 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;
		
		// 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 +"', '"+ type +"')", 100);
	
		// Disable input file to block multiple upload
		Element.show('loading_'+type);
		//Element.hide('button_local_'+type);
		input.disabled=true;
	}
}



function checking_loading_content(iframe_id, type)
{
	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,type);
		// 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 +"','"+ type +"')", 100);
	}
}


/**
 * Delete file associate to a content and display input to upload new content
 */
function delete_content( type )
{
	var input 				= document.getElementById(type+'_input');
	
	var input_hidden_file 	= document.getElementById('new_'+type+'_filename');

	var input_hidden_state 	= document.getElementById('new_'+type+'_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_'+type);
	//Element.show('button_local_'+type);
	Element.hide('loading_'+type);
	Element.hide('current_'+type);
}



/**
 * Valid is no file in upload before form validation
 * 
 * @author mrandy
 * @since 1.0 - 2008-04-16 - MRY
 * @version 1.0 - 2008-04-16 - MRY
 * @param form - Form to submit
 * @param error_message - Message to display if it's upload in progress
 */
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;
}

/**
 * Valid is no file in upload before form validation
 * 
 * @author yvalentin
 * @since 1.0 - 2008-07-01 - YVN
 * @version 1.0 - 2008-07-01 - YVN
 * @param reponse - reponse of upload
 * @param type - type of the content upload
 */
function valid_reponse( response, type)
{

	// Upload correctly endend
	if(response.substr(0,2)=='OK')
	{
		
		

		// Hide upload file and show message
		Element.hide('new_'+type);
		//Element.hide('button_local_'+type);
		Element.hide('loading_'+type);
		Element.show('current_'+type);
		// Memorize filename in an hidden input and put new state in an other input hidden
		var input_hidden_file 		= document.getElementById('new_'+type+'_filename');
		var input_hidden_state 		= document.getElementById('new_'+type+'_value');

		input_hidden_file.value		= response.substr(3);
		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(type);
	}
}

function loading_move_content(type)
{
	// Hide upload file and show loading
	Element.hide('new_'+type);
	//Element.hide('button_local_'+type);
	Element.show('loading_'+type);
	Element.hide('current_'+type);
}

function display_success()
{
	var email  = document.getElementById('email');
	var commentaire = document.getElementById('commentaire');
	email.disabled=true;
	commentaire.disabled=true;
	Element.show('commentaire_confirm');
			
}
/**
 * Oblige le t�l�chargement d'une video
 * @Author 	GPR
 * @since 	1.0 - 2008-08-08
 * @version 1.1 - 2009-02-20 - DEY
 */
function valid_video(error_message)
{
	if($("new_movie_value").value != 3 )
	{
		alert(error_message);
		return false;
	}
	
	return true;	
}

/**
 * permet cacher apres retour du validator le champ upload
 * @author GPR  
 * @since 1.0 -2008-09-24
 * @version 1.0 -2008-09-24
 */
function check_file_is_uploaded(type){
	
	upload_type = document.getElementById('new_'+type+'_filename');

	if(upload_type.value.length>0)
	{
		Element.hide('new_'+type);
		Element.show('current_'+type);
	}
}


