/* common javascript */

// PHP trim analogue
trim = function(str) { return str.replace(/^\s+|\s+$/g, ""); };

// array value check
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(obj)
	{
		for (var i = 0; i < this.length; i++)
		{
			if (this[i] == obj)
				return i;
		}
		return -1;
	}
}


// trying to get rid of IE image flickering
if (iBrowser.isIE)
{
	try
	{
		document.execCommand('BackgroundImageCache', false, true);
	}
	catch (e) { }
}

// go to address (if relative - tries to find <base> tag)
function go(url)
{
	if ((url.indexOf('/') == 0) || (url.indexOf('http://') == 0) || (url.indexOf('https://') == 0)) // absolute url
	{
		window.location = url;
	}
	else if (url.indexOf('#') == 0) // anchor
	{
		var place = window.location.href;
		place = place.lastIndexOf('/') != (place.length - 1) ? place : place.slice(0, place.length - 1);
		if (place.lastIndexOf('#') != -1)
			place = place.slice(0, place.lastIndexOf('#'));
		window.location = place + url;
	}
	else // relative (tries to get <base> due to IE ignorance of it in javascript redirects)
	{
		// trying find base tag
		var base = document.getElementsByTagName('base');
		if (base && (base.length > 0)) // exists
		{
			base = base[0].getAttribute('href');
			if (base.lastIndexOf('/') != (base.length - 1))
				base += '/';
			window.location = base + url;
		}
		else // redirecting with relative link
		{
			window.location = url;
		}
	}
}

// set redirect with timeout
function redirect(url, timeout)
{
	setTimeout("go('" + url + "');", timeout * 1000);
}

// make full url w/base href
function withBase(url)
{
	if ((url.indexOf('/') == 0) || (url.indexOf('http://') == 0) || (url.indexOf('https://') == 0)) // absolute url
	{
		return url;
	}
	else // relative (tries to get <base> due to IE ignorance of it in javascript redirects)
	{
		// trying find base tag
		var base = document.getElementsByTagName('base');
		if (base && (base.length > 0)) // exists
		{
			base = base[0].getAttribute('href');
			if (base.lastIndexOf('/') != (base.length - 1))
				base += '/';
			return base + url;
		}
		else // relative link
		{
			return url;
		}
	}
}

// confirm action
function act_confirm(phrase)
{
	return window.confirm(phrase);
}

// set cookie
function setCookie(name, value, expires, path, domain, secure)
{
	var c_value = escape(value);
	var c_expires = expires ? "; expires=" + expires.toGMTString() : '';
	var c_path = path ? "; path=" + path : '';
	var c_domain = domain ? "; domain=" + domain : '';
	var c_secure = secure ? "; secure" : '';
	var cur_cookie = name + "=" + c_value + c_expires + c_path + c_domain + c_secure;
	
	document.cookie = cur_cookie;
}

// set sort on column
function setSortColumn(sort_id)
{
	setCookie('sort', sort_id, new Date(new Date().getTime() + 6000000));
	window.location.href = window.location.href;
}

// hide closed issues control
function hideClosedIssues()
{
	setCookie('hide_closed', '1', new Date(new Date().getTime() + 6000000));
	window.location.href = window.location.href;
}

// reset report filter
function resetReportFilter()
{
	var elements = document.forms['filter'].elements;
	for (var i = 0, n = elements.length; i < n; i++)
	{
		var element = elements[i];
		switch (true)
		{
			// select
			case element.tagName == 'SELECT':
				element.selectedIndex = 0;
				break;
			
			// input text
			case (element.tagName == 'INPUT') && (element.type == 'text'):
				element.value = '';
				break;
			
			// input checkbox
			case (element.tagName == 'INPUT') && (element.type == 'checkbox'):
				element.checked = false;
				break;
		}
	}
}

// scrolling to object
function scrollToObj(id)
{
	var el = document.getElementById(id);
	window.scrollTo(getX(el), getY(el));
}
