//------------------------------------------------------------------------------
// Espace de Téléchargements 2.2
//------------------------------------------------------------------------------
// Date started: 4 mars 2006
// Writing started by: Fabien
//------------------------------------------------------------------------------

function form_init()
{
	form_choice_tupload();
	form_enable_file_size();
}

function form_choice_tupload()
{
	//Upload
	if( document.getElementById('tupload_1') && document.getElementById('tupload_1').checked == true )
	{
		document.getElementById('tupload-upload').style.display = '';
		if( document.getElementById('tupload-externe') ) document.getElementById('tupload-externe').style.display = 'none';
		if( document.getElementById('tupload-interne') ) document.getElementById('tupload-interne').style.display = 'none';
	}
	//Externe
	else if( document.getElementById('tupload_0') && document.getElementById('tupload_0').checked == true )
	{
		document.getElementById('tupload-externe').style.display = '';
		document.getElementById('tupload-upload').style.display = 'none';
		if( document.getElementById('tupload-interne') ) document.getElementById('tupload-interne').style.display = 'none';
	}
	//Interne
	else if( document.getElementById('tupload_2') && document.getElementById('tupload_2').checked == true )
	{
		document.getElementById('tupload-interne').style.display = '';
		document.getElementById('tupload-upload').style.display = 'none';
		document.getElementById('tupload-externe').style.display = 'none';
	}
}

function form_enable_file_size()
{
	if( document.getElementById('tupload-externe-size') && document.getElementById('file_size_enable').checked == true )
	{
		document.getElementById('tupload-externe-size').style.display = '';
	}
	else if( document.getElementById('tupload-externe-size') && document.getElementById('file_size_enable').checked == false )
	{
		document.getElementById('tupload-externe-size').style.display = 'none';
	}
}

function logs_init()
{
	var th    = '';
	var link  = '';
	var order = '';
	var type  = '';
	var id    = '';
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	//----------------------------------
	// Replace links
	//----------------------------------
	for ( var i = 0 ; i < document.getElementsByTagName('th').length ; i++ )
	{
		if( document.getElementsByTagName('th')[i].id.match(/order/) )
		{
			th = document.getElementById(document.getElementsByTagName('th')[i].id);
			
			link = th.getElementsByTagName("a")[0].href;
			
			order = link.replace( /^(.*)order=(\w+)-(\w+)$/, "$2-$3" );
			
			if( link.search(/view=/) != -1 )
			{
				type = link.replace( /^(.*)view=(\w+)&(.*)$/, "$2" );
			}
			if( link.search(/id=/) != -1 )
			{
				id = link.replace( /^(.*)id=(\d+)&(.*)$/, "$2" );
			}
			
			th.getElementsByTagName("a")[0].href = "javascript:logs_sort('" + order + "','" + type + "','" + id + "');";
		}
	}
}

function logs_sort(order,type,id)
{
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return false;
	}
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( !xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.getElementById('result').innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=FileCP&xmlout=logs&view=' + type + '&order=' + order + '&id=' + id );
}

function quicksearch(qs)
{
	//----------------------------------
	// Init
	//----------------------------------
	var fields = new Array();
	
	//----------------------------------
	// Using fancy js?
	//----------------------------------
	if ( !use_enhanced_js )
	{
		return true;
	}
	
	//----------------------------------
	// f and q not empty?
	//----------------------------------
	if ( qs.elements['q'].value == '' || qs.elements['q'].value == '' )
	{
		return true;
	}
	else
	{
		document.getElementById('catview').innerHTML = "";
	}
	
	//----------------------------------
	// Fields
	//----------------------------------
	fields['f'] = qs.elements['f'].value;
	fields['q'] = qs.elements['q'].value;
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( !xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
	
		if ( html != 'error' )
		{
			document.getElementById('catview').innerHTML = html;
		}
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( ipb_var_base_url + 'autocom=Downloads&xmlout=quicksearch', 'POST', xmlobj.format_for_post(fields) );
	return false;
}
