// JavaScript Document

function OpenNextWindow( url ) 
{
	location.href = url;
}

function ShowElement( id ) 
{
	if (document.getElementById( id ))
	{
		document.getElementById( id ).className = 'show';
	}
}

function HideElement( id ) 
{
	if (document.getElementById( id ))
	{
		document.getElementById( id ).className = 'hide';
	}
}

function HideAllMarkers() 
{
	HideElement( 'course' );
	HideElement( 'date' );
	HideElement( 'firstname' );
	HideElement( 'lastname' );
	HideElement( 'street' );
	HideElement( 'city' );
	HideElement( 'email' );
	HideElement( 'message' );
	HideElement( 'tel' );
	HideElement( 'dog' );
	HideElement( 'breed' );
	HideElement( 'pax' );
	HideElement( 'malert' );
	document.getElementById( 'tfirstname' ).focus();
}

function CheckChar( c, string ) 
{
	var i = 0;
	
	while (i < string.length) 
	{
		if (string.charAt( i ) == c) 
		{
			return (true);
		}
		i++;
	}
	
	return (false);
}

function CheckEmail( string ) 
{
	var atCount = 0;
	var dotFlag = false;
	var spaceFlag = false;
	var domainFlag = true;
	var i = 0, count = 0, domain = 0;
	
	if (string.length < 5) return false;
	
	while (i < string.length)
	{
		if (string.charAt( i ) == '@') 
		{
			atCount++;
			break;
		}
		count++;
		i++;
	}
	i++;
	if (count < 1 || count > 64) return false;
	count = 0;
	
	while (i < string.length)
	{
		if (string.charAt( i ) == '@') 
		{
			atCount++;
		}
		if (string.charAt( i ) == '.') 
		{
			dotFlag = true;
			domain = i;
		}
		if (string.charAt( i ) == ' ') 
		{
			spaceFlag = true;
		}
		count++;
		i++;
	}
	if (count < 1 || count > 255 || dotFlag == false) return false;
	count = 0;
	i = domain + 1;

	while (i < string.length)
	{
		if (string.charAt( i ) <= 'a' || string.charAt( i ) >= 'z') 
		{
			domainFlag = false;
		}
		count++;
		i++;
	}
	if (count < 2 || count > 6) return false;
	
	return ((atCount == 1) && dotFlag && !spaceFlag && domainFlag);
}

/*
function CheckEmail( string ) 
{
	var atFlag = 0;
	var dotFlag = false;
	var spaceFlag = false;
	var i = 0;
	
	if (string.length < 5) return false;
	
	while (i < string.length) 
	{
		if (string.charAt( i ) == '@') 
		{
			atFlag++;
		}
		if (string.charAt( i ) == '.') 
		{
			dotFlag = true;
		}
		if (string.charAt( i ) == ' ') 
		{
			spaceFlag = true;
		}
		i++;
	}
	
	return ((atFlag == 1) && dotFlag && !spaceFlag);
}
*/

function CheckPLZ( string ) 
{
	var i = 0;
	var digitCnt = 0;
	
	if (string.length < 7) return false;
	
	while ((string.charAt( i ) < '0' || string.charAt( i ) > '9') && i < string.length) i++; 
	while (string.charAt( i ) >= '0' && 
			string.charAt( i ) <= '9' && 
			i < string.length) 
	{
		i++;
		digitCnt++;
	}
	
	return (digitCnt == 5);
}

function PointTo( id ) 
{
	var	tid;
	
	tid = "t" + id;
	if (document.getElementById( id ))
	{
		document.getElementById( tid ).focus();
	}

//	document.getElementsByName( name )[0].focus();
//	ShowElement( id );
}

function HandleError( id, error ) 
{
	ShowElement( 'malert' );
	
	ShowElement( id );
	if (error == true)
	{
		PointTo( id );
	}
	return false;
}
 
function CheckForm( kind ) 
{
	var chk;
	var error = true;
	var f = document.forms[0];
	
	if (!f) 
	{
		alert( unescape( "Keine Eingaben%FCberpr%FCfung im Browser m%F6glich%21" ));
		return true;
	}
	
	HideAllMarkers();

	if (kind == 'kontakt')
	{
		chk = CheckEmail( f.email.value );
		if (chk == false) error = HandleError( 'email', error );

		if (f.message.value.length < 5) error = HandleError( 'message', error );
	}
	else if (kind == 'wanderung' || kind == 'doho' || kind == 'course')
	{
		if (f.firstname.value.length < 2) error = HandleError( 'firstname', error );
		if (f.lastname.value.length < 3) error = HandleError( 'lastname', error );
		if (f.street.value.length < 3) error = HandleError( 'street', error );
//		if (f.city.value.length < 1) error = HandleError( 'city', error );
		chk = CheckPLZ( f.city.value );
		if (chk == false) error = HandleError( 'city', error );
		chk = CheckEmail( f.email.value );
		if (f.tel.value.length < 3 && chk == false) 
		{
			error = HandleError( 'email', error );
			error = HandleError( 'tel', error );
		}
	}

	
	if (kind == 'wanderung')
	{
		if (f.dog.value.length < 1) error = HandleError( 'dog', error );
		if (f.breed.value.length < 1) error = HandleError( 'breed', error );
		if (f.pax.value.length < 1) error = HandleError( 'pax', error );
	}
	return error;
}


function HideMarkers( id ) 
{
	var area = document.getElementById( id );
	var label;
	var text;
	var textId;
	
	for (i = 0; i < area.getElementsByTagName('p').length; i++) 
	{
		label = area.getElementsByTagName('label')[i];
		text = label.getElementsByTagName('strong')[0];
		if (!text) continue;
		textId = text.id;
		HideElement( textId );
		if (i == 0) PointTo( textId );
	}
	HideElement( 'malert' );
}

function InitLists( change ) 
{
	var	area = document.getElementById( "listToClose" );
	var base;
	var liBase;
	var id;
	var i;
    
	if (!area) return;
	
	for (i = 0; i < area.getElementsByTagName('span').length; i++) 
	{
		base = area.getElementsByTagName('span')[i];
		if (!base) continue;

		if (base.className == "initShow")
		{
			base.className = "initHide";			
		}
		else if (base.className == "initHide")
		{
			base.className = "initShow";			
		}
	}
}

/*
function InitLists( change ) 
{
	var	area = document.getElementById( "listToClose" );
	var base;
	var liBase;
	var id;
	var i;
    
	if (!area) return;
	
	for (i = 0; i < area.getElementsByTagName('li').length; i++) 
	{
		base = area.getElementsByTagName('li')[i];
		liBase = base.getElementsByTagName('ul')[0];
		if (!liBase) continue;
		id = liBase.id;

		if (change == 0)
		{
			liBase.className = "closed";			
		}
		else
		{
			liBase.className = "opened";
		}
	}
}
*/

function ToggleClass( id ) 
{
	var ul_element = document.getElementById( id );

	if (ul_element) 
	{
		if (ul_element.className == 'closed') 
		{
			ul_element.className = "opened";
		} 
		else 
		{
			ul_element.className = "closed";
		}
	}
}

function OpenClass( id ) 
{
	var ul_element = document.getElementById( id );

	if (ul_element) 
	{
		ul_element.className = "opened";
	}
}
function CloseClass( id ) 
{
	var ul_element = document.getElementById( id );

	if (ul_element) 
	{
		ul_element.className = "closed";
	}
}

function OpenOneClass( id ) 
{
	var ul_element = document.getElementById( id );
	var area = document.getElementById( "listToClose" );
	var ulBase;
	var base;
	var liBase;
	var id;
	var i, flag = 0;
    
	if (ul_element.className == 'closed') flag = 1; 
	if (area)
	{
		for (j = 0; j < area.getElementsByTagName('ul').length; j++)
		{
			ulBase = area.getElementsByTagName('ul')[j];
			for (i = 0; i < ulBase.getElementsByTagName('li').length; i++) 
			{
				base = ulBase.getElementsByTagName('li')[i];
				liBase = base.getElementsByTagName('ul')[0];
				if (!liBase) continue;
				id = liBase.id;
		
				liBase.className = "closed";
			}			
		}
	}
			
	if (ul_element) 
	{
		if (flag == 1) 
		{
			ul_element.className = "opened";
		} 
		else 
		{
			ul_element.className = "closed";
		}
	}
}



