// ===============================================================================

function AjaxUserLogin() {	
	new Ajax( '/ajax/userLogin.php?r=' + $random(0, 10000), {
		method: 'POST',	data: $('loginForm').toQueryString(),
		onComplete: function() {
			if( this.response.text == 'true' )
			{
				$('myFilesTab').style.display = 'block';
				changeSWFUUploadAction();
				getUserPanel();
				getUploadOptions();
				showHidenTabs();
				//document.getElementById('fileupload').action = '/bla-bla-nna';
				//alert(document.getElementById('fileupload').action);
			}  
			else
			{
				alert('Логин или пароль не верен.');
				changeSWFUUploadAction();
			}
		}
	}).request();
}

function AjaxUserExit() {
	new Ajax( '/ajax/userExit.php?r=' + $random(0, 10000), {
		method: 'get', onComplete: function() {
			$('myFilesTab').style.display = 'none';
			$('ajaxLoading').style.display = 'none';
			getUserPanel(); getUploadOptions(); changeSWFUUploadAction();
		}
	}).request();	
	$('ajaxLoading').style.display = 'block';
}

// ===============================================================================

function getUserPanel() {
	new Ajax( '/ajax/userPanel.php?r=' + $random(0, 10000), {			
		method: 'get', onComplete: function() {
			$('ajaxLoading').style.display = 'none';			
			$('userPanel').innerHTML = this.response.text;
		}			
	}).request();	
	$('ajaxLoading').style.display = 'block';
}

function getUploadOptions() {
	new Ajax( '/ajax/uploadOptionsDiv.php?r=' + $random(0, 10000), {			
		method: 'get', onComplete: function() {
			$('ajaxLoading').style.display = 'none';
			$('optionsDiv').innerHTML = this.response.text;
		}			
	}).request();	
	$('ajaxLoading').style.display = 'block';
}

function getDeletenFiles() {
	new Ajax( '/ajax/myDeletenFilesList.php?r=' + $random(0, 10000), {			
		method: 'get', onComplete: function() {
			$('ajaxLoading').style.display = 'none';
			$('myDeletenFiles').innerHTML = this.response.text;
		}	
	}).request();
	$('ajaxLoading').style.display = 'block';
}

function checkAll() {
	var inputns = $$('input#myDeletenCheckBox');
	if ( inputns.length > 0) inputns.each( function(input) {input.checked = true;} );
}

function showHidenTabs() {
	$('hiddenTab').style.display = 'block';
}

// ===============================================================================

function showOrHideOptions() {
	if( $('optionsDiv').style.display == 'none' ) {
		$('optionsDiv').style.display = 'block';
	} else {
		$('optionsDiv').style.display = 'none';
	}
}

// ===============================================================================

function reloadMyDeletenFiles() {
	new Ajax( '/ajax/reloadMyDeletenFiles.php?r=' + $random(0, 10000), {
		method: 'POST',	data: $('myDeletenFilesForm').toQueryString(), evalScripts: true,
		onComplete: function() { $('ajaxLoading').style.display = 'none'; }
	}).request();
	
	$('ajaxLoading').style.display = 'block';
}

function reload( fileID ) {
	new Ajax('/ajax/reload.php?fileID=' + fileID + '&r=' + $random(0, 10000), {
		method: 'get', onComplete: function(){ 
			$('ajaxLoading').style.display = 'none';			
				
			var div = $( 'upload-' + fileID );
			
			if( div == null ) {
				var fileList = document.getElementById('fileList');		
				var div = document.createElement('div'); div.id = "upload-" + fileID;
				fileList.appendChild( div );
			}
			getFileInfo(fileID, 'upload');
		}
	}).request();
	document.getElementById('ajaxLoading').style.display = 'block';	
}

function beginSubmit() {	
	$('ajaxLoading').style.display = 'block';	
	$('submitButton').disabled = true;	
}

function updateUploadLog( mes, files, ready ) {

	if( mes != '' ) {
		$('errorList').style.display = 'block';
		$('errorList').innerHTML = '<fieldset style="padding: 5px;"><legend>Лог ошибок</legend>' + mes + '</fieldset><br>';					
	} else { $('errorList').style.display = 'none'; }
	
	if( files.length > 0 ) 
	{	
		var fileList = $('fileList');			
		for( var i = 0; i < files.length; i++ ) {			
			var div = document.createElement('div'); div.id = "upload-"+files[i]; fileList.appendChild( div );		
			setTimeout("getFileInfo(" + files[i] + ", 'upload');", ( i + 1 ) * 10 );			
		}		
		fileList.style.display = 'block'; 
	}
	endSubmit();
}

function endSubmit() {
	$('uploadLog').style.display = 'block';
	//$('submitButton').disabled = false;
	//$('file').value = '';
	$('ajaxLoading').style.display = 'none';
}

// ===============================================================================

function showTab( id ) {	
	 for( var i = 0; i < tabsList.length; i++ ) {
            if( tabsList[i] == id ) $(tabsList[i]).style.display = 'block';
			else $(tabsList[i]).style.display = 'none';
	}	
}

// ===============================================================================

function getMyFilesList( pageID ) {
	new Ajax('/ajax/myFilesList.php?pageID=' + pageID + '&r=' + $random(0, 10000), {
		method: 'get',
		onComplete: function(){ $('ajaxLoading').style.display = 'none'; $('myFilesList').innerHTML = this.response.text; }
	}).request();
	$('ajaxLoading').style.display = 'block';	
}


function getLastFilesList() {
	new Ajax('/ajax/lastFilesList.php?&r=' + $random(0, 10000), {
		method: 'get',
		onComplete: function(){ $('ajaxLoading').style.display = 'none'; $('lastFilesList').innerHTML = this.response.text; }
	}).request();
	$('ajaxLoading').style.display = 'block';	
}

// ===============================================================================

function getFileInfo( fileID, elementID  ) {
	new Ajax( '/upload/getFileInfo/'+fileID+'/'+elementID+'/?r=' + $random(0, 10000), {			
		method: 'get',	evalScripts: true,
		onComplete: function() {$('ajaxLoading').style.display = 'none'; $( elementID + '-' + fileID ).innerHTML = this.response.text; }
	}).request();
	$('ajaxLoading').style.display = 'block';
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}



function changeSWFUUploadAction ()
{
	var userID, saveCode, RID;
	userID = getCookie('userID');
	saveCode = getCookie('saveCode');
	RID = getCookie('RID');

	if ((null != userID) && ('' != userID) && (null != saveCode) && ('' != saveCode))
	{
		swfu.setUploadURL('/upload/?userID=' + userID + '&saveCode=' + saveCode);
	}
	else if ((null != RID) && ('' != RID))
	{
		swfu.setUploadURL('/upload/?RID=' + RID);
	}
	else
	{
		alert('else!');
		swfu.setUploadURL('/');
	}
}