
// Globale Variablen
var browser = navigator.appName;
var Xpos = 0;
var Ypos = 0;

var showTimer = 0;
var hideTimer = 0;

// Get the  mouse coordination
function GetMousePos(e)
{
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer")
	{
		Xpos = event.clientX + document.body.scrollLeft;
		Ypos = event.clientY + document.body.scrollTop;
	}
	else
	{
		Xpos = e.pageX;
		Ypos = e.pageY;
	}
}

// Make the tooltip visible
function ShowToolTip(headline, network, channel, bot, pack, irclink)
{
	clearTimeout(hideTimer);
	Hide();
	
	ToolTip = document.getElementById('tooltip');
	ToolHead = document.getElementById('toolhead');
	ToolNetwork = document.getElementById('toolnetwork');
	ToolChannel = document.getElementById('toolchannel');
	ToolBot = document.getElementById('toolbot');
	ToolIRCLink = document.getElementById('toolirclink');
	
	ToolHead.innerHTML = headline;
	ToolNetwork.innerHTML = network;
	ToolChannel.innerHTML = channel;
	ToolBot.innerHTML = bot;
	
	IRCCommand = "/msg " + bot + " xdcc send #" +pack;
	IRCLinkInner = "<a href=\u0022" + irclink + "\u0022 OnClick=\u0022copyToClipboard('" + IRCCommand + "')\u0022>" +IRCCommand + "</a>";
	ToolIRCLink.innerHTML = IRCLinkInner;
	
	showTimer = setTimeout('Show()',1000);
}

function Show()
{
	clearTimeout(hideTimer);
	
	ToolTip = document.getElementById('tooltip');
	ToolHead = document.getElementById('toolhead');
	
	if(ToolHead.offsetWidth / 2 < Xpos)
	{
		ToolTip.style.left = Xpos - (ToolHead.offsetWidth / 2) - 7;
	}
	else
	{
		ToolTip.style.left = 5;
	}
	
	// Show under the mouse cursor if there is not enough space
	if(ToolTip.offsetHeight < Ypos)
	{
		ToolTip.style.top = Ypos - (ToolTip.offsetHeight);
	}
	else
	{
		ToolTip.style.top = Ypos;
	}
	
	ToolTip.style.visibility = 'visible';
}

function Visible()
{
	clearTimeout(hideTimer);
	ToolTip = document.getElementById('tooltip');
	ToolTip.style.visibility = 'visible';
}

// Make the tooltip invisible
function HideToolTip()
{
	clearTimeout(showTimer);
	hideTimer = setTimeout('Hide()',5000);
}

function Hide()
{
	clearTimeout(showTimer);
	ToolTip = document.getElementById('tooltip');
	ToolTip.style.visibility = 'hidden';
}

// Change an image
function ChangeImg(ID, src) 
{
	element = document.getElementById(ID);
	element.src = src;
}

// Show/Hide an element
function ShowHide(ID)
{
	element = document.getElementById(ID);
	
	if(element.style.visibility == 'visible')
	{
		element.style.visibility = 'hidden';
		element.style.display = 'none';
	}
	else
	{
		element.style.visibility = 'visible';
		element.style.display = 'inline';
	}
}

// Get the mouse position on mouse move
document.onmousemove = GetMousePos;

// AJAX functions
var ajax = new Array();

function getChannelList(sel)
{
	var network = sel.options[sel.selectedIndex].value;
	document.getElementById('channellist').options.length = 0;	// Empty channel select box
	document.getElementById('botlist').options.length = 0;		// Empty bot select box
	if(network.length > 0)
	{
		//document.searchform.Network.value = network;
		
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = '../ajax/getChannelList.php?Network='+network;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createChannels(index) };		// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function createChannels(index)
{
	var obj = document.getElementById('channellist');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function getBotList(sel)
{
	var network = document.advsearchform.networklist.options[document.advsearchform.networklist.selectedIndex].value;
	var channel = sel.options[sel.selectedIndex].value;
	document.getElementById('botlist').options.length = 0;	// Empty bot select box
	if(channel.length > 0)
	{
		//var channelvalue = channel.replace(/%23/,"#");
		//document.searchform.Channel.value = channelvalue;

		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = '../ajax/getBotList.php?Network='+network+'&Channel='+channel;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createBots(index) };		// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	}
}

function createBots(index)
{
	var obj = document.getElementById('botlist');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

function getBot(sel)
{
	var bot = sel.options[sel.selectedIndex].value;
	if(bot.length > 0)
	{
		document.searchform.Bot.value = bot;
	}
}

function isValidEmail(email)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(email))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isValidPhone(Phone)
{
	for( var i = 0; i < Phone.length; i++ )
	{
		if(Phone.charAt(i) != "/" && Phone.charAt(i) != " " && Phone.charAt(i) != "-" && Phone.charAt(i) != "+")
		{
			if(Phone.charAt(i) > "9" || Phone.charAt(i) < "0")
			{
				return false;
            }  
		}
	}
	
	return true;
}

function submitMail()
{
	var Name = document.contact.Name.value;
	
	if(Name.length < 5)
	{
		alert(unescape('Bitte geben Sie Ihren vollst%E4ndigen Namen ein!'));
		document.contact.Name.focus();
		return false;
	}
	else if(Name.indexOf(" ") == -1)
	{
		alert(unescape('Bitte geben Sie Ihren vollst%E4ndigen Namen ein!'));
		document.contact.Name.focus();
		return false;
	}
	
	var Email = document.contact.EA.value;
	
	if(!isValidEmail(Email))
	{
		alert(unescape('Bitte geben Sie eine g%FCltige Email Adresse ein!'));
		document.contact.EA.focus();
		return false;
	}
	
	var Telefon = document.contact.Telefon.value;
	
	if(Telefon.length < 5)
	{
		alert(unescape('Bitte geben Sie Ihre vollst%E4ndige Telefon-Nummer ein!'));
		document.contact.Telefon.focus();
		return false;
	}
	else if(!isValidPhone(Telefon))
	{
		alert(unescape('Die von Ihnen angegebene Telefon-Nummer ist ung%FCltig!'));
		document.contact.Telefon.focus();
		return false;
	}
	
	var Nachricht = document.contact.Nachricht.value;
	
	if(Nachricht.length < 50)
	{
		alert(unescape('Ihre Nachricht muss mindestens 50 Zeichen lang sein!'));
		document.contact.Nachricht.focus();
		return false;
	}
	
	document.contact.submit();
}

function submitSearch()
{
	var description = document.searchform.Description.value;
	
	if(description.length > 0)
	{
		document.searchform.submit();
	}
	else
	{
		alert('Please enter a search term!');
		document.searchform.Description.focus();
		return false;
	}
}

function submitAdvSearch()
{
	var description = document.searchform.Description.value;
	
	if(description.length > 0)
	{
		var network = document.advsearchform.networklist.options[document.advsearchform.networklist.selectedIndex].value;
		document.searchform.Network.value = network;
		
		if(document.advsearchform.botlist.selectedIndex >= 0)
		{
			var channel = document.advsearchform.channellist.options[document.advsearchform.channellist.selectedIndex].value;
			var channelvalue = channel.replace(/%23/,"#");
			document.searchform.Channel.value = channelvalue;
		}
		else
		{
			document.searchform.Channel.value = '';
		}
		
		if(document.advsearchform.botlist.selectedIndex >= 0)
		{
			var bot = document.advsearchform.botlist.options[document.advsearchform.botlist.selectedIndex].value;
			document.searchform.Bot.value = bot;
		}
		else
		{
			document.searchform.Bot.value = '';
		}
	
		document.searchform.submit();
	}
	else
	{
		alert('Please enter a search term!');
		document.searchform.Description.focus();
		return false;
	}
}

function copyToClipboard(text)
{
	if(window.clipboardData && clipboardData.setData)
	{
		clipboardData.setData("Text", text);
	}
}
