var IE = false;
if(navigator.appName == "Microsoft Internet Explorer") IE = true;

function getById(_id) 
{
	return document.getElementById(_id);
}

function posX(_element) 
{
	return $(_element).offset().left;
}

function posY(_element) 
{
	return $(_element).offset().top;
}

function delegate(_object, _method) 
{
	return function(_e) 
	{
		return _method.call(_object, _e);
	}
}

function checkDate(_nDay, _nMonth, _nYear) 
{
	_nDay = Math.round(_nDay);
	_nMonth = Math.round(_nMonth);
	_nYear = Math.round(_nYear);
	var aLengths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if(_nDay <= aLengths[_nMonth] || (_nMonth == 1 && _nDay == 29 && _nYear % 4 == 0)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

// Saving user settings via AJAX
function saveUserSetting(_setting, _value) 
{
	$.get(pathPrefix + "/Ajax/sysSaveUserSetting" + firstSeperator + "setting=" + _setting + "&value=" + _value + sid2);
}


// Shows or hides a specific element
function showHide(_id) 
{
	if(getById(_id) == null)
	{
		return false;
	}
	
	if(getById(_id).style.display == "")
	{
		getById(_id).style.display = "none";
	}
	else
	{
		getById(_id).style.display = "";
	}
		
	return false;
}


// Send KeepAlive package
function sendKeepAlive(_acp) 
{
	$.get(pathPrefix + "/Ajax/sysKeepAlive" + (_acp ? firstSeperator + "acp=true" + sid2 : sid1));
	keepAliveTimeout = window.setTimeout("sendKeepAlive(" + (_acp ? "true" : "false") + ")", 300000);
}

//Toggle for lots of checkboxes
function toggleEventHandler(_formId, _elementNames, _sender) {
	
	var id = _elementNames.replace("[]", "");
	var checkBoxes = getById(_formId).elements[_elementNames];
	var checked = 0;
	var count = 0;
	
	if(!checkBoxes)
		return;
	
	if(checkBoxes.type == "checkbox")
		count = 1;
	else
		count = checkBoxes.length;
	
	if(count == 1) {
		if(_sender.id != id + "_ALL")
			getById(id + '_ALL').checked = checkBoxes.checked;
		else
			checkBoxes.checked = _sender.checked;
		return true;
	}
	
	for(var i = 0; i < count; i++) {
		if(checkBoxes[i].checked)
			checked++;
	} 

	if(_sender.id != id + '_ALL') {
		if(checked == 0)
			getById(id + '_ALL').checked = false;
		if(checked == count)
			getById(id + '_ALL').checked = true;
	}
	
	if(_sender) {
		if(_sender.id == id + "_ALL") {
			for(var i = 0; i < count; i++)
				checkBoxes[i].checked = _sender.checked;
		}
		else
			if(!_sender.checked)
				getById(id + '_ALL').checked = false;
	}
	
	return true;
	
}

function textCounter(_field, _counter, _max) {
	
	var length = _field.value.length
	
	if (length > _max)
		_field.value = _field.value.substring(0, _max);
	
	else {
		if(_max -length == 0)
			_counter.style.fontWeight = "bold";
		else
			_counter.style.fontWeight = "normal";
		
		_counter.innerHTML = _max - length;
	}
}


// Internal links

function showInternalLinkSelection(_input) {
	window.open(pathPrefix + "/DirectCall/wysInternalLinks" + firstSeperator + "targetInput=" + _input + sid2, "internalLinkSelection", "width=365, height=320");
}


// File selection

function showFileSelection(_input, _path, _filter) {
	window.open(pathPrefix + "/DirectCall/wysFileManager" + firstSeperator + "targetInput=" + _input + (_path == null ? "" : "&forcedDirectory=" + _path) + (_filter == null? "" : "&ffilter=" + _filter) + sid2, "internalLinkSelection", "width=640, height=465");
}


// Confirm message
function confirmMessage(_id) 
{
	$.get(pathPrefix + "/Ajax/sysConfirmMessage" + firstSeperator + "id=" + _id + sid2);
	
	getById("message_" + _id).style.display = "none";
	
	if(getVisibleRows(getById("messagetable")) == 0)
	{
		getById("messagebox").style.display = "none";
	}
	
	return false;
}

function getVisibleRows(_table) 
{
	var count = 0;
	for(var i = 0; i < _table.rows.length; i++) 
	{
		if(_table.rows[i].style.display != "none")
		{
			count++;
		}
	}
	return count;
}

// Toggle for checkboxes to disable input fields
function toggleInput(_input) 
{
	if(getById(_input).disabled == true) 
	{
		getById(_input).disabled = false;
	}
	else 
	{
		getById(_input).disabled = true;
	}
}


// This function executes all javascript found in string object
function evalJS(_content) {
	
	var container = document.createElement('div');
	container.innerHTML = _content;
	
	var code = "";
	
	for(var i = 0; i < container.getElementsByTagName('script').length; i++) {
		
		code += container.getElementsByTagName('script')[i].innerHTML;
	
	}
	
	eval(code);
	
	delete container;
}

function in_array(_needle, _haystack) 
{
	var count = _needle.length;
	
	for(var i = 0; i < count; i++) 
	{
		if(_needle == _haystack[i])
		{
			return true;
		}
	}
	
	return false;
}

function getNextSibling(_element) 
{
	if(IE)
	{
		return _element.nextSibling;
	}
	else
	{
		return _element.nextSibling.nextSibling;
	}
}

function scrollToTop() 
{
	$('html, body').animate({
		scrollTop: 0
	}, 1000);
	
	return false;
}

function updateMessageTableOpenedDate(_date)
{
	if($('#messagetable_outer').is(':visible'))
	{
		saveUserSetting('sysMessageTableOpenedDate', _date);
	}
}

$(document).ready(function() 
{
	// Setup boxes
	setupBoxes();
	
	// Setup defaultText textboxes/inputs
	$("input.defaultTextEnabled, textarea.defaultTextEnabled").each(function() {
		var obj = $(this);
		var defaultText = obj.attr("title");
		
		obj.focus(function() {
			if(obj.hasClass("defaultText"))
			{
				obj.removeClass("defaultText").val("");
			}
		});
		
		obj.blur(function() {
			if(obj.val() == "")
			{
				obj.addClass("defaultText").val(defaultText);
			}
		});
		
		obj.parents("form").submit(function() {
			if(obj.hasClass("defaultText"))
			{
				obj.val("");
			}
			
			window.setTimeout(function() {
				obj.blur();
			}, 50);
		});
		
		obj.blur();
	});
	
	// Setup tabs
	$("div.box.hasTabs").fcpTabs();
	
	// Scroll to top icon
	$(window).scroll(function() {
		if($(window).scrollTop() > 10)
		{
			$('#scrolltotop').fadeIn(2000);
		}
		else
		{
			$('#scrolltotop').fadeOut(2000);
		}
	});
	$("#scrolltotop").click(scrollToTop);
	
	// Open links in new window
	$('a[rel*=new-window]').attr('target', '_blank');
	
	// Open links in lightbox
	$('a[rel*=lightbox]').lightBox();

	$.get(pathPrefix + "/Ajax/sysJob" + sid1);
});
