//****************************
window.callFolderTreePopup = function(selector_name, show_marker_selectors)
{
	var marker_selector_parameter = (show_marker_selectors ? 1 : 0);

	top.newWin = window.open('/intranet/common/folderstree_popup.php?selector_name='+selector_name+'&callback=fileUploadDocumentSelected' +
		'&document_select=1&marker_select=' + marker_selector_parameter + '&folder_select=0&class_select=0', 'tree',
		'dependent=yes,width=500,height=430,screenX=200,screenY=300,titlebar=yes, scrollbars=yes,status=yes,resizable=yes');
	return false;
};

// Function called from popup window with folderstree_popup.php
window.fileUploadDocumentSelected = function(object_id, object_aggregation, object_name, dest_name)
{
	clearStandardFileUpload(dest_name);
	jQuery('#' + dest_name + '_special_id').val(object_id).change();
	jQuery('#' + dest_name + '_realname').val(object_name).change();

	return true;
};

//****************************

function onChangeFileUpload(dest_name)
{
	fileUploadDocumentSelected(dest_name, 0, 0, '');
	var browse_input = document.getElementById(dest_name + '_browse');

	//browse_input.click();
	jQuery('#' + dest_name + '_special_id').val('');

	var filename = browse_input.value;
	filename = filename.replace('C:\\fakepath\\', '');

	// Trim long filenames while retaining any extension.
	MAX_FILENAME_LENGTH = 37;
	if (filename.length > MAX_FILENAME_LENGTH)
	{
		extension = '';
		parts = filename.split('.');
		if (parts.length > 1)
		{
			extension = parts[parts.length-1];
			parts[parts.length-1] = '';
		}

		filename = parts.join('.');
		filename = filename.substr(0, filename.length-1);

		filename = filename.substr(0, MAX_FILENAME_LENGTH - 8 - extension.length) + '...';
		if (extension != '')
			filename = filename + ' .' + extension;
	}

	jQuery('#' + dest_name + '_realname').val(filename);
}

function callInnerDocument(dest_name)
{
	clearStandardFileUpload(dest_name);
	clearInnerFileUpload(dest_name);
	return callFolderTreePopup(dest_name);
}

function callImageSelect(dest_name)
{
	var cur_id = jQuery('#' + dest_name + '_special_id').val();

	top.imageWin = window.open('/intranet/common/image_select_popup.php?dest_name='+dest_name + '&image_code=' + cur_id, 'image_win',
			'dependent=yes,width=500,height=550,screenX=200,screenY=300,titlebar=yes, scrollbars=yes,status=yes,resizable=yes');
	return false;
}

function fileUploadImageSelected(dest_name, image_code, realname)
{
	clearStandardFileUpload(dest_name);
	jQuery('#' + dest_name + '_special_id').val(image_code);
	jQuery('#' + dest_name + '_realname').val(realname);
}

function clearStandardFileUpload(dest_name)
{
	jQuery('#' + dest_name + '_browse').val('');
}

function clearInnerFileUpload(dest_name)
{
	jQuery('#' + dest_name + '_special_id').val('');
	jQuery('#' + dest_name + '_realname').val('');
}
