/*
 * httpupload.js: JavaScript Upload Support for vipfile.com.ua
 *
 * Author: Sanito
 * Copyright (c) 2009
 */


// === globals --------------------------------------------------------
var frmValidator = null;

//---------------------------------------------------------------------
// Control visibility
//---------------------------------------------------------------------
function showControl(ctrl, visibility_only)
{
	if (arguments.length == 1)
	{
		visibility_only = false;
	}
	if (typeof(ctrl) == "string")
	{
		ctrl = document.getElementById(ctrl);
	}

	ctrl.style.visibility = "";
	if (!visibility_only)
	{
		ctrl.style.display = "";
	}
}
function hideControl(ctrl, visibility_only)
{
	if (arguments.length == 1)
	{
		visibility_only = false;
	}
	if (typeof(ctrl) == "string")
	{
		ctrl = document.getElementById(ctrl);
	}

	ctrl.style.visibility = "hidden";
	if (!visibility_only)
	{
		ctrl.style.display = "none";
	}
}

//---------------------------------------------------------------------
// Cookies support
//---------------------------------------------------------------------
function getCookie(name)
{
	var c = document.cookie;

	if (c.length == 0)
	{
		return null;
	}
	var offs = c.indexOf(' ' + name + '=');
	if (offs == -1)
	{
		return null;
	}
	var v_offs = 2 + name.length + offs;
	var end = c.indexOf(";", v_offs);

	if (end == -1)
	{
		end = c.length;
	}
	return unescape(c.substring(v_offs, end));
}
function setCookie(name, value, expires, path, domain)
{
	switch (arguments.length)
	{
		case 2:
			var d = new Date();
			d.setDate(d.getDate() + 1);
			expires = d.toGMTString();
		case 3:
			path = "/";
		case 4:
			domain = ".vipfile.com.ua";
		case 5:
			break;

		default:
			alert("setCookie(): invalid parameters");
			return;
	}

	var new_c = name + '=' + escape(value) +
		((expires != "") ? "; expires=" + expires : "") +
		((path != "") ? "; path=" + path : "") +
		((domain != "") ? "; domain=" + domain : "");
	document.cookie = new_c;
}

//---------------------------------------------------------------------
// Copy to clipboard text of input control
//---------------------------------------------------------------------
function CopyToClipboard(input_ctrl_id)
{
	var ctrl = document.getElementById(input_ctrl_id);
	if (ctrl != null && ctrl.value != "")
	{
		ctrl.focus();
		ctrl.select();
		if (typeof(ctrl.createTextRange) == "object")
		{
			var rng = ctrl.createTextRange();
			if (rng != null)
			{
				rng.execCommand("Copy");
			}
		}
	}
}

//---------------------------------------------------------------------
// Change row visibility in files list
//---------------------------------------------------------------------
var lastShowedInfoNum = -1;
function changeSearchVisibility(num)
{
	var sp = document.getElementById('addition_span' + num);
 	if (sp == null)
 	{
		return;
	}
	var new_vis = (sp.style.visibility == "") ? "hidden" : "";
	var new_disp = (sp.style.display == "") ? "none" : "";
	sp.style.visibility = new_vis;
	sp.style.display = new_disp;

	if (lastShowedInfoNum != -1 && lastShowedInfoNum != num)
	{
		hideControl('addition_span' + lastShowedInfoNum);
		lastShowedInfoNum = -1;
	}

	if (new_vis == "")
	{
		lastShowedInfoNum = num;
	}
}

//---------------------------------------------------------------------
// Returns readable size (such "10 KB") by given bytes count
//---------------------------------------------------------------------
function get_readable_size(size)
{
	for (i = 0; size > 1024; i++, size /= 1024);

	var s;
	switch (i)
	{
		case 0: s = "Bytes"; break;
		case 1: s = "KB"; break;
		case 2: s = "MB"; break;
		case 3: s = "GB"; break;
		case 4: s = "TB"; break;
	}

	// add zero(s)
	var ret = new String(Math.round(size * 100) / 100);
	var i = ret.indexOf('.');
	if (i == -1)
	{
		ret += '.00';
	}
	else
	if (i == ret.length - 2)
	{
		ret += '0';
	}
	return ret + ' ' + s;
}

//---------------------------------------------------------------------
// Returns readable time by given seconds count
//---------------------------------------------------------------------
function get_readable_time(seconds)
{
	if (seconds < 0)
	{
		return "";
	}

	var portions = new Array(60, 60, 60, 24);
	var names    = new Array("s", "m", "H", "D");

	var s = "";
	for (i = 0; i <= 3 && seconds > 0; i++)
	{
		var d = parseInt(seconds % portions[i]);
		seconds = parseInt(seconds/portions[i]);

		s = d + names[i] + s;
	}

	return s;
}

//---------------------------------------------------------------------
// Opens pop-under window
//---------------------------------------------------------------------
function openPopUnder(url, noreferer)
{
	var w;

	if (noreferer == 1)
	{
		w = window.open();
		w.document.write('<meta http-equiv="refresh" content="0; url=' + url + '">');
		w.document.close();
	}
	else
	{
		var params = "";
		var w = window.open(url, '_blank', params);
		w.blur();
	}
	window.focus();
}
