/*
	This jscript serves several functionality for the p2p search form	
	TODO: Better browser compatibility according the change_layer function
*/

/*
	Changes the content of a certain layer	
	@param	id	The name of the layer as specified in html
	@param	str	The new content
*/
function change_layer(id, str) 
{
	if (type=="IE") 
	{
		document.all[id].innerHTML = str;
	}
	if (type=="NN") 
	{ 
		document.layers[id].document.open();
		document.layers[id].document.write(str);
		document.layers[id].document.close();
	}
	if (type=="MO") 
	{
		document.getElementById(id).innerHTML = str;
	}
	if (type=="OP") 
	{
		document.getElementById(id).innerHTML = str;
	}
}

/*
	Call this to set the browser type in your html page
*/
function BrowserSniffer() 
{
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";
	else if (document.all) type="IE";
	else if (document.layers) type="NN";
	else if (!document.all && document.getElementById) type="MO";
	else type = "IE";
}

/*
	Brings up a popup with the current browser shorten
*/
function whatBrows() 
{
	window.alert("Browser is : " + type);
}

/*
	Changes the bgcolor of a layer	
	@param	id		The name of the layer as specified in html
	@param	color	The new color
*/
function ChangeLayerBgColor(id, color){
	if (type=="IE") document.all[id].style.backgroundColor=color;
	if (type=="NN") document.layer['id'].bgColor=color;
	if (type=="MO" || type=="OP") document.getElementById(id).style.backgroundColor=color;
}

/*
	Changes the visibility of a layer	
	@param	id		The name of the layer as specified in html
	@param	action	The new visibility value (show, hide,...)
*/
function ShowLayer(id, action){
	if (type=="IE") eval("document.all." + id + ".style.visibility='" + action + "'");
	if (type=="NN") eval("document." + id + ".visibility='" + action + "'");
	if (type=="MO" || type=="OP") eval("document.getElementById('" + id + "').style.visibility='" + action + "'");
}

/*
	Proves if the form is valid or not and submits it if possible	
	@param	msg		The message to show if the form is invalid
*/
function submitIt(msg)
{	
	var ToCorrect = false;
	var FromCorrect = false;
	if(document.myForm.from!=null){
		if(document.myForm.from!=null && document.myForm.from.value.length >2) FromCorrect = true;
	}
	if(document.myForm.fromloc!=null){
		if(document.myForm.fromloc.options!=null && document.myForm.fromloc.selectedIndex!=-1){
			var value = document.myForm.fromloc.options[document.myForm.fromloc.selectedIndex].value;
			if(value!=null && value.length==5) FromCorrect = true;
		}
	}
	if(document.myForm.to!=null){
		if(document.myForm.to!=null && document.myForm.to.value.length >2) ToCorrect = true;
	
	}
	if(document.myForm.toloc!=null){
		if(document.myForm.toloc.options!=null && document.myForm.toloc.selectedIndex!=-1){
			var value = document.myForm.toloc.options[document.myForm.toloc.selectedIndex].value;
			if(value!=null && value.length==5) ToCorrect = true;		
		}
	}
	if(FromCorrect==false){
		alert(msg);		
		return;
	}
	else
	{
		if(ToCorrect==false){
			alert(msg);		
			return;
		}
		else{
			//if(!complete){
				if(document.myForm.fromloc!=null){
					if(document.myForm.fromloc.options!=null){
						var value = document.myForm.fromloc.options[document.myForm.fromloc.selectedIndex].text;
						document.myForm.from.value=value;						
					}
				}
				if(document.myForm.toloc!=null){
					if(document.myForm.toloc.options!=null){
						var value = document.myForm.toloc.options[document.myForm.toloc.selectedIndex].text;
						document.myForm.to.value=value;
					}
				}
				document.myForm.submit();
			//}
		}
	}
}

/*
	Writes the clicked value of select into from and to	
	@param	select		The select box with the favourites
*/
function doChange(select)
{
	if(select.selectedIndex!=-1)
	{
		var selectValue = select.options[select.selectedIndex].text;
		var fromTxt = selectValue.substring(0, selectValue.lastIndexOf("»")-1);
		var toTxt = selectValue.substring(selectValue.lastIndexOf("»")+2, selectValue.length);
		var txt = "<INPUT TYPE=\"TEXT\" SIZE=\"30\" MAXLENGTH=\"50\" NAME=\"from\" VALUE=\""+fromTxt+"\" class=\"inputStyle\" style=\"width:195px\">";	
		change_layer('fromLayer', txt);
		var txt = "<INPUT TYPE=\"TEXT\" SIZE=\"30\" MAXLENGTH=\"50\" NAME=\"to\" VALUE=\""+toTxt+"\" class=\"inputStyle\" style=\"width:195px\">";	
		change_layer('toLayer', txt);
	}
}

/*
	Removes a favourite from the list of favourites
*/	
function removeFavourite()
{
	if(document.myForm.favRoute.selectedIndex!=-1 && document.myForm.favRoute!=null && document.myForm.favRoute.options!=null)
	{
		var route = document.myForm.favRoute.options[document.myForm.favRoute.selectedIndex].value;
		if(route!=null && route.length>0 && document.myForm.remove!=null) document.myForm.remove.value=route;
		document.myForm.from.value = "";
		if(document.myForm.fromloc!=null) document.myForm.fromloc.value = "";
		document.myForm.to.value = "";
		if(document.myForm.toloc!=null) document.myForm.toloc.value = "";
		document.myForm.submit();
	}
}

/*
	Opens up a new Window with a specified href, title, width and height. This window has no toolbar, no scrollbars, no menubar and is not resizable.
	The xPos is 150, yPos also 150.	
	@param		href	The url to open in the new window
	@param		title	The internal title for tihs window (!= the title in the titlebar of the browser)
	@param		width	The width for the new window
	@param		height	The height of the new window
*/
function openDetailsPopUp(href, title, width, height)
{
	window1 = window.open(href,title,"height="+height+",width="+width+",toolbar=no,status=no,scrollbars=no,resizable=no,menubar=no,location=no,left=150,top=150");
	window1.focus();
}

/*
	Opens up a new Window with a specified href, title, width and height. This window has no toolbar, no scrollbars, no menubar and is not resizable.
	The xPos is 150, yPos also 150.	
	@param		href	The url to open in the new window
	@param		title	The internal title for tihs window (!= the title in the titlebar of the browser)
	@param		width	The width for the new window
	@param		height	The height of the new window
*/		
function openPrintPopUp(href, title, width, height)
{
	window2 = window.open(href,title,"height="+height+",width="+width+",toolbar=no,status=no,scrollbars=yes,resizable=no,menubar=no,location=no,left=0,top=0");
	window2.focus();
}

/*
	This method calls submitIt(msg), if the keyCode (ms-ie only) was 13 ( = return)	
	@param	msg		The error message to diplay, if something went wrong (at least 3 chars)
*/
function submitIfReturn(msg)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode==13)
	{
		submitIt(msg);
	}
}