// video
var obj_video_upload = new upload_form('video');
obj_video_upload.set_extensions('.mpeg, .mpg, .mp4, .wmv, .avi, .3gp, .flv, .mov, .vob');

/**
 * =)
 *
 */
function upload_form(code_word)
{
	this.code_word		= code_word;

	this.form			= null;
	this.file			= null;
	this.file_name		= null;
	this.file_extension	= null;
	this.file_path		= null;
	this.rules			= null;
	this.submit_button	= null;

	this.text_extensions	= '';
	this.allow_extensions	= '*';
	this.current_extension	= '';

	this.allow_time = 0;
	this.object_var_name = '';

	/**
	 * инициализация
	 *
	 */
	this.init = function()
	{
		var form_id = 'upload_' + this.code_word + '_form';

		this.form = document.getElementById(form_id);
		this.file = this.form.file;
		this.rules = this.form.rules;
		this.submit_button = this.form.submit_button;

		this.file_path = this.file.value;
		this.file_name = this._get_file_name();

		var ext_re = /(\.[a-z0-9]{0,5})$/i;
		var a = ext_re.exec(this.file_name);

		if ( a != null )
		{
			this.file_extension = a[0];
		}

		this.re = /^[\s\wа-я][\s\wа-я\.\-]{1,50}$/i;
	};


	/**
	 * установить возможные расширения
	 *
	 */
	this.set_extensions = function(extensions)
	{
		this.text_extensions = extensions;
		this.allow_extensions = extensions.split(', ');
	};


	/**
	 * проверить расширение файла
	 *
	 */
	this.check_extension = function()
	{
		for (var i = 0; i < this.allow_extensions.length; i++)
		{
			//alert(this.allow_extensions[i]);
			if ( this.allow_extensions[i] == this.file_extension.toLowerCase() )
			{
				return false;
			}
		}

		return true;
	};


	/**
	 * начать загрузку
	 *
	 */
	this.start_upload = function(time, object_var_name)
	{
		if ( this.allow_time == 0 )
		{
			this.object_var_name = object_var_name;
			this.allow_time = time;
			this._progress_allow_time();
		}

		if ( this.form.file.value.length == 0 )
		{
			alert('Не выбран файл');
		}
		else if ( this.allow_time > 0 )
		{
			alert('Следующее видео вы сможете загрузить через ' + this.allow_time + ' сек.');
		}
		else
		{
			this.init();
			this.check_upload();
		}
	};


	this._progress_allow_time = function()
	{
		if ( this.allow_time > 0 )
		{
			this.allow_time -= 1;
			setTimeout(this.object_var_name+'._progress_allow_time()', 1000);
		}
		else
		{
			this.allow_time = -1;
		}
	};

	/**
	 * проверить загружаемый файл
	 *
	 */
	this.check_upload = function()
	{
		var error_text = "";
		var error = false;

		if ( this.check_extension() && (this.allow_extensions != '*') && (this.file_name.length > 0) )
		{
			error = true;
			error_text += "Доступные расширения файла: " + this.text_extensions + "\n";
		}

		if ( !this.rules.checked )
		{
			error = true;
			error_text += "Вы не согласны с правилами? Почему?\n";
		}


		this.file_path = this.file.value;
		this.file_name = this._get_file_name();

		if ( this.file_name.length == 0 )
		{
			error = true;
			error_text += "Не выбран файл\n";
		}
		else if( !this.re.test(this.file_name) )
		{
			error = true;
			error_text += "Имена файлов должны быть следующего формата:\n\n— Имя файла не может сдержать больше 50 символов\n— Имя файла может содержать следующие символы: 1-9, a-z, A-Z, '.', '_', '-'";
		}

		if ( error )
		{
			alert(error_text);
			return false;
		}
		else
		{
			this._onsubmit();
			this.form.submit();

			this.submit_button.disabled = true;
			this.rules.disabled = true;
			this.file.disabled = true;
			return true;
		}
	};

	/**
	 * остановить загрузку файла
	 *
	 */
	this.stop_upload = function ()
	{
		try
		{
			window.stop();
		}
		catch(e)
		{
			try
			{
				document.execCommand('Stop');
			}
			catch(e)
			{
				location.href = self.location;
			}
		}

		location.href = self.location;
	};

	/**
	 * получить имя файла
	 *
	 */
	this._get_file_name = function()
	{
		var last_slash = this.file_path.lastIndexOf("\\");

		if ( last_slash < 1 )
		{
			last_slash = this.file_path.lastIndexOf("/");
		}

		return this.file_path.slice(last_slash + 1, this.file_path.length);
	};

	/**
	 * смена статуса кнопки "Загрузить"
	 *
	 */
	this._submit_button_status = function(status)
	{
		switch ( status )
		{
			case 0:
				this.submit_button.disabled = true;
				break;

			case 1:
				this.submit_button.disabled = false;
				break;

			default:
				break;
		}
	};

	/**
	 * onsubmit
	 *
	 */
	this._onsubmit = function()
	{
		this._show_hide_by_id(this.code_word + '_select_file');
		this._show_hide_by_id(this.code_word + '_upload_status');
	};

	/**
	 * showhideform
	 *
	 */
	this._show_hide_form = function()
	{
		if ( this.form.style.display == 'block' )
		{
			this.form.style.display = 'none';
		}
		else
		{
			this.form.style.display = 'block';
		}
	};

	/**
	 * showhideform
	 *
	 */
	this._show_hide_by_id = function(id)
	{
		if ( document.getElementById(id).style.display == 'block' )
		{
			document.getElementById(id).style.display = 'none'
		}
		else
		{
			document.getElementById(id).style.display = 'block'
		}
	};
};
/*


















*/
