function sleep(naptime)
{
	naptime = naptime * 1000;
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
	while(sleeping)
	{
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if(alarmMSeconds - startingMSeconds > naptime)
		{
			sleeping = false;
		}
	}
}

function cblur(elm)
{
	if (elm.multiple)
		return true;

	var needElm;
	var founded = false;
	var doSearch = true;
	if (elm.form)
	{
		$(elm.form).getElements().each(function(el){
			if (!doSearch)
				return false;

			if (founded && el.type != 'hidden')
			{
				needElm = el;
				doSearch = false;
			}

			if (el == elm)
				founded = true;
		});

		if (!doSearch)
			needElm.focus();
		else
			elm.form.focus();
	}
	else
	{
		document.body.focus();
	}

	return true;
}

function deleteFile(url_loc, className, recordID, db_field, typeStock, idToHide, messageConfirm)
{
	if (confirm(messageConfirm))
	{
		url_loc = sg_isAdmin ? '../' : './';
		new AjaxRequest(url_loc+'core/_ajax/delete.php',
		{
			parameters :
			{
				action: "deleteFile",
				ajax: "1",
				recordID: recordID,
				className: className,
				db_field: db_field,
				typeStock: typeStock
			},
			onSuccess : function()
			{
				$(idToHide).hide();
			},
			onFailure : function(r)
			{
				$('debug').innerHTML = r.responseText;
			}
		});
	}
}

function parseUrl(str)
{
	var loc = window.location;
	var matches;
	var path;
	path = loc.href.replace('/([^/]+)\?', '');

	var res = {'base': loc.pathname, 'path': path};
	alert(res.path);
	return res;
}

function saveToFile(str, fileName)
{
	url_loc = sg_isAdmin ? '../' : './';
	new AjaxRequest(url_loc+'core/_ajax/common.php',
	{
		parameters :
		{
			action: "saveText",
			ajax: "1",
			text: str,
			fileName: fileName
		},
		onSuccess : function()
		{
			alert('saved');
		},
		onFailure : function(r)
		{
			$('debug').innerHTML = r.responseText;
		}
	});
}

var AjaxRequest = Class.create(
{
	initialize: function(url, options)
	{
		this.url = url || '';
		this.options = options || {};
		this.onSuccess = Object.isFunction(this.options.onSuccess) ? this.options.onSuccess : Prototype.emptyFunction;
		this.onFailure = Object.isFunction(this.options.onFailure) ? this.options.onFailure : Prototype.emptyFunction;
		this.parameters = this.options.parameters || {};
		this.method = this.options.method || 'post';
		this.caching = this.options.caching || false;
		this.loader = this.options.loader || null;
		this.user = this.options.user || null;
		this.password = this.options.password || null;
		this.async = this.options.password || true;

		this.send();
	},

	send : function()
	{
		var req = new JsHttpRequest();
		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				if (req.responseJS)
				{
					this.onSuccess(req.responseJS);
				}
			}
			else
			{
				this.onFailure(req);
			}
		}

		var fx = req.onreadystatechange;
		req.onreadystatechange = fx.bind(this);

		req.loader = this.loader;
		req.caching = this.caching;
		req.open(this.method, this.url, this.async, this.user, this.password);

		req.send(this.parameters);
	}
});

function pr(obj)
{
	var res = '';
	if (Object.isString(obj))
	{
		res += obj;
	}
	else
	{
		$H(obj).each(function(el)
		{
			res += el.key + ' = ' + el.value + '<br />';
		});
	}

	var debug = $('debug');
	if (!debug)
	{
		debug = new Element('div', {id:'debug'});
		$(document.body).insert(debug);
	}

	debug.update(res);
}