// JavaScript Document
function newWin(url,name,feats)
{
	window.open(url,name,feats);
}

function toggle_all(an)
{
	// zeigt alle layer (wow member availability)
	var a = an;
	layerArray = new Array('contentpr','contentma','contentjä','contentsc','contentdr','contentkr','contenthx','contentpa','contentsh');
	
	for (i=0; i < layerArray.length; i++)
	{
		if ( a == 1 )
		{
			document.getElementById( layerArray[i] ).style.display = 'block';
		} else
		{
			document.getElementById( layerArray[i] ).style.display = 'none';
		}
	}
	return false;
}

function showOptional(targetLayer)
{
	// zeigt layer an | blendet layer aus
	var tl = targetLayer;
	var tI = targetLayer + 'Image';
	displayType = (document.getElementById( tl ).style.display == 'none') ? 'block' : 'none';
	displayImage = (document.getElementById( tI ).src == 'http://www.warplaner.net/images/icons/plusImg.gif') ? 'http://www.warplaner.net/images/icons/minusImg.gif' : 'http://www.warplaner.net/images/icons/plusImg.gif';
	document.getElementById( tl ).style.display = displayType;
	document.getElementById( tI ).src = displayImage;
}

// toggled elemente anhand ihrer id
function toggle_vis(targetLayer) 
{
	// zeigt layer an | blendet layer aus
	var tl = targetLayer;
	displayType = (document.getElementById( tl ).style.display == 'none') ? 'block' : 'none';
	document.getElementById( tl ).style.display = displayType;
}

// toggled class
function toggle_vis_using_class(targetElement)
{
	// available classes .invisible | .visible
	var te = targetElement;
	if( document.getElementById( te ).className == 'invisible' )
	{
		document.getElementById( te ).className= 'visible';
	} else
	{
		document.getElementById( te ).className= 'invisible';
	}
}

function focusField(whichfield)
{
	var w = whichfield;
	document.getElementById( w ).focus();
	/*
	var w = whichfield;
	var wVal = w.value;
	invalidStrings = new Array();
	var fieldError = false;
	for (i=0; i < invalidStrings.length; i++)
	{
		if( wVal.indexOf(invalidStrings[i]) != -1 )
		{
			// hit, das feld ist noch mit dem default-wert gefüllt oder es ist leer
			document.getElementById( w.name ).value = "";
		}
	}
	*/
}

function checkAllFields()
{
	fieldArray = new Array('formFIELD[0]','addARRAY[3]','formFIELD[3]','formFIELD[4]','addARRAY[2]','addARRAY[4]');
	for (i=0; i < fieldArray.length; i++)
	{
		var cc = document.getElementById( fieldArray[i] ).className;
		if( cc == 'valid' )
		{
			document.getElementById('submitButton').disabled = '';
			document.getElementById('submitButton').title = 'Klicke hier um ihren Account anzulegen.';
		} else
		{
			document.getElementById('submitButton').disabled = 'disabled';
			document.getElementById('submitButton').title = 'Es sind nicht alle erforderlichen Felder ausgefüllt.';			
		}
	}
}

function checkAllFieldsClanbase()
{
	fieldArray = new Array('teamNAME','teamTAG');
	for (i=0; i < fieldArray.length; i++)
	{
		var cc = document.getElementById( fieldArray[i] ).className;
		if( cc == 'valid' )
		{
			document.getElementById('submitButton').disabled = '';
			document.getElementById('submitButton').title = 'Klicke hier um den Gegner anzulegen.';
		} else
		{
			document.getElementById('submitButton').disabled = 'disabled';
			document.getElementById('submitButton').title = 'Es sind nicht alle erforderlichen Felder ausgefüllt.';			
		}
	}	
}

function checkAllFieldsCreateTeam()
{
	fieldArray = new Array('teamNAME','teamTAG','gameTYPE','adminPASS','adminPASS2','joinPASS','joinPASS2','contactEMAIL');
	for (i=0; i < fieldArray.length; i++)
	{
		var cc = document.getElementById( fieldArray[i] ).className;
		if( cc == 'valid' )
		{
			document.getElementById('submitButton').disabled = '';
			document.getElementById('submitButton').title = 'Klicke hier um das Team anzulegen.';
		} else
		{
			document.getElementById('submitButton').disabled = 'disabled';
			document.getElementById('submitButton').title = 'Es sind nicht alle erforderlichen Felder ausgefüllt.';			
		}
	}	
}

function checkAllFieldsAddMember()
{
	fieldArray = new Array('nick','email');
	for (i=0; i < fieldArray.length; i++)
	{
		var cc = document.getElementById( fieldArray[i] ).className;
		if( cc == 'valid' )
		{
			document.getElementById('submitButton').disabled = '';
			document.getElementById('submitButton').title = 'Klicke hier um die Einladung zu schicken.';
		} else
		{
			document.getElementById('submitButton').disabled = 'disabled';
			document.getElementById('submitButton').title = 'Es sind nicht alle erforderlichen Felder ausgefüllt.';			
		}
	}
}

function checkFieldClanbase(whichfield,checkForEmail)
{
	var w = whichfield;
	var wCheckMail = checkForEmail;
	var wVal = w.value;
	invalidStrings = new Array('-------','--',' >> select');
	var fieldError = false;
	for (i=0; i < invalidStrings.length; i++)
	{
		if( wVal.indexOf(invalidStrings[i]) != -1 || wVal == '' )
		{
			// hit, das feld ist noch mit dem default-wert gefüllt oder es ist leer
			fieldError = true;
		}
		if( wCheckMail != 0 && !checkEmail( wVal, true ) )
		{	
			// email-adresse ist nicht gültig
			fieldError = true;
		}
	}
	if( fieldError )
	{
			changeColor(w,0);
	} else
	{
			changeColor(w,1);
	}
	checkAllFieldsClanbase();
}

function checkFieldAddMember(whichfield,checkForEmail)
{
	var w = whichfield;
	var wCheckMail = checkForEmail;
	var wVal = w.value;
	invalidStrings = new Array('-------','--',' >> select');
	var fieldError = false;
	for (i=0; i < invalidStrings.length; i++)
	{
		if( wVal.indexOf(invalidStrings[i]) != -1 || wVal == '' )
		{
			// hit, das feld ist noch mit dem default-wert gefüllt oder es ist leer
			fieldError = true;
		}
		if( wCheckMail != 0 && !checkEmail( wVal, true ) )
		{	
			// email-adresse ist nicht gültig
			fieldError = true;
		}
	}
	if( fieldError )
	{
			changeColor(w,0);
	} else
	{
			changeColor(w,1);
	}
	checkAllFieldsAddMember();
}

function checkFieldCreateTeam(whichfield,checkForEmail)
{
	var w = whichfield;
	var wCheckMail = checkForEmail;
	var wVal = w.value;
	invalidStrings = new Array('-------','--',' >> select');
	var fieldError = false;
	for (i=0; i < invalidStrings.length; i++)
	{
		if( wVal.indexOf(invalidStrings[i]) != -1 || wVal == '' )
		{
			// hit, das feld ist noch mit dem default-wert gefüllt oder es ist leer
			fieldError = true;
		}
		if( wCheckMail != 0 && !checkEmail( wVal, true ) )
		{	
			// email-adresse ist nicht gültig
			fieldError = true;
		}
	}
	if( fieldError )
	{
			changeColor(w,0);
	} else
	{
			changeColor(w,1);
	}
	checkAllFieldsCreateTeam();
}

function revalidateField()
{
	return true;
}

function checkField(whichfield,checkForEmail)
{
	var w = whichfield;
	var wCheckMail = checkForEmail;
	var wVal = w.value;
	//document.write(wVal);
	invalidStrings = new Array('-------','--',' >> select');
	var fieldError = false;
	for (i=0; i < invalidStrings.length; i++)
	{
		if( wVal.indexOf(invalidStrings[i]) != -1 || wVal == '' )
		{
			// hit, das feld ist noch mit dem default-wert gefüllt oder es ist leer
			fieldError = true;
		}
		if( wCheckMail != 0 && !checkEmail( wVal, true ) )
		{	
			// email-adresse ist nicht gültig
			fieldError = true;
		}
	}
	if( fieldError )
	{
			changeColor(w,0);
	} else
	{
			changeColor(w,1);
	}
	checkAllFields();
}

function changeColor(targetEntity,toWhat)
{
	var tar = targetEntity;
	var to = toWhat;
	if( to == 0 ) 
	{
		var c = '#AE5252';
		var cName = 'invalid';
	} else
	{
		var c = '#1C9A37';
		var cName = 'valid';
	}
	// document.write(targetClass);
	// document.getElementById( tar.name ).style.border = '1px solid '+c;
	document.getElementById( tar.name ).className= cName;
}

/*
 Copyright Jan Müller <jan@rt-deigns.de>, 2006
 reibtec. IT-Solutions, Bickenbach
*/
function count_slots()
{
	fieldArray = new Array('DR','HX','JÄ','MA','KR','PA','PR','SH','SC');
	var fin_count = 0;
	for (i=0; i < fieldArray.length; i++)
	{
		// var cc = document.getElementById( fieldArray[i] ).className;
		// if( cc == 'valid' )
		// {
			fin_count += Number( document.getElementById( fieldArray[i] ).value );
	}
	if ( document.getElementById('classlimitmode').value == "time" )
	{
		document.getElementById('DR').disabled = false;
		document.getElementById('HX').disabled = false;
		document.getElementById('JÄ').disabled = false;
		document.getElementById('MA').disabled = false;
		document.getElementById('KR').disabled = false;
		document.getElementById('PA').disabled = false;
		document.getElementById('PR').disabled = false;
		document.getElementById('SH').disabled = false;
		document.getElementById('SC').disabled = false;
		document.getElementById('limitTrTime').style.display = 'block';
		document.getElementById('limitTrAdmin').style.display = 'none';		
		document.getElementById('slotcounter').value = fin_count;
		if( fin_count > 0 )
		{
			document.getElementById('slotcounterRetMsg').innerHTML = '<strong>Es sind '+ fin_count + ' Slots für Signups verfügbar.</strong>';
			document.getElementById('createRaidButton').disabled = false;
		} else
		{
			if( document.getElementById('classlimitmode').selectedIndex == 0 )
			{
				document.getElementById('slotcounterRetMsg').innerHTML = '<strong>Sie müssen mindestens einen Slot verfügbar machen.</strong>';
				document.getElementById('createRaidButton').disabled = true;
			} else
			{
				document.getElementById('slotcounterRetMsg').innerHTML = '<strong>Es sind '+ fin_count + ' Slots für Signups verfügbar.</strong>';
				document.getElementById('createRaidButton').disabled = true;				
			}
		}
	} else
	{
		document.getElementById('DR').selectedIndex = 0;
		document.getElementById('DR').disabled = true;
		document.getElementById('HX').selectedIndex = 0;
		document.getElementById('HX').disabled = true;		
		document.getElementById('JÄ').selectedIndex = 0;
		document.getElementById('JÄ').disabled = true;		
		document.getElementById('MA').selectedIndex = 0;
		document.getElementById('MA').disabled = true;		
		document.getElementById('KR').selectedIndex = 0;
		document.getElementById('KR').disabled = true;		
		document.getElementById('PA').selectedIndex = 0;
		document.getElementById('PA').disabled = true;		
		document.getElementById('PR').selectedIndex = 0;
		document.getElementById('PR').disabled = true;		
		document.getElementById('SH').selectedIndex = 0;
		document.getElementById('SH').disabled = true;		
		document.getElementById('SC').selectedIndex = 0;
		document.getElementById('SC').disabled = true;		
		document.getElementById('limitTrTime').style.display = 'none';									
		document.getElementById('limitTrAdmin').style.display = 'block';									
		document.getElementById('slotcounterRetMsg').innerHTML = '<strong>Singup-Modus Admin - Slotlimit disabled</strong>';
		document.getElementById('createRaidButton').disabled = false;
	}
}

/*
  Copyright Lutz Eymers <ixtab@polzin.com>, 1997
  Polzin GmbH, Duesseldorf
*/
  function isDigit( ch )
  {
    if ( (ch >= '0') && (ch <= '9') )
      return true;
    else
      return false;
  }


  function isAlpha( ch )
  {
    if ( ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) )
      return true;
    else
      return false;
  }


  function isAlnum( ch )
  {
    if ( isAlpha( ch ) || isDigit( ch ) )
      return true;
    else
      return false;
  }


  function notIn( str1, str2 )
  {
    var i = 0;
    var j = str2.length;
    for( ; i<j; i++ )
    {
      var str3 =  str2.charAt(i);
      if( str1.indexOf( str3 ) != -1 )
        return false;
    }
    return true;
  }


  function checkUsername( username, mustBeQuoted )
  {
    var i = 0;
    var j = username.length;
    if ( username.charAt(0) != '"' )
    {
      if ( (username.charAt(0) <  ' ') || (username.charAt(0) >  '~')
            || !notIn( mustBeQuoted, username.charAt(0) ) )
        return false;
      for( i=1; i<j; i++ )
      {
        if ( ( (username.charAt(i) < ' ') || (username.charAt(i) >  '~')
              || !notIn ( mustBeQuoted, username.charAt(i) ) )
             && ( username.charAt(i-1) != '\\' ) )
          return false;
      }
    }
    else
    {
      if ( username.charAt( j-1 ) != '"' )
        return false;
      for( i=1; i<j-1; i++ )
      {
        if ( ( (username.charAt(i) == '\n') || (username.charAt(i) == '\r')
              || (username.charAt(i) == '\"') )
            && (username.charAt(i-1) != '\\') )
          return false;
      }

    }
    return true;
  }


  function checkNr ( nr )
  {
    var i=0;
    var j=nr.length;

    if( j < 1 )
      return false;

    for( ; i<j; i++ )
      if( ( nr.charAt(i) < '0' ) || ( nr.charAt(i) > '9' ) )
        return false;

    return true;
  }


  function checkIpnr( ipnr )
  {
    var iL=0;
    var iC=0;
    var i=0;
    var sNr = "";

    for( ; i< ipnr.length; i++ )
    {
      if ( ipnr.charAt(i) == '.' )
      {
        if ( !iL || (iL> 3) || parseInt( sNr,10 ) > 255 )
          return false;
        iC++;
        iL = 0;
        sNr = "";
        continue;
      }
      if ( isDigit ( ipnr.charAt(i) ) )
      {
        iL++;
        sNr = sNr + ipnr.charAt(i);
        continue;
      }
      return false;
    }

    if ( parseInt( sNr,10 ) > 255 )
      return false;
    if ( ( (iC==3) && (iL>=1) && (iL<=3) ) || ( (iC==4) && (!iL) )  )
      return true;
    else
      return false;
  }


  function checkFqdn( fqdn )
  {
    var iL=0;
    var iC=0;
    var i=fqdn.length-1;

    if ( (fqdn.charAt(0) == '.') || (fqdn.charAt(0) == '-') )
      return false;
    if ( fqdn.charAt(i) == '.' )
      i=i-1;

    for( ; i>=0; i-- )
    {
      if ( fqdn.charAt(i) == '.' )
      {
        if ( iL < 2 && iC < 2 )
          return false;
        if ( fqdn.charAt(i-1) == '-' )
          return false;
        iC++;
        iL = 0;
        continue;
      }
      if ( isAlnum ( fqdn.charAt(i) ) )
      {
        iL++;
        continue;
      }
      if ( fqdn.charAt(i) == '-' )
      {
        if ( !iL )
          return false;
        iL++;
        continue;
      }
      return false;
    }

    if ( !iC || ( iL == 1 && iC < 2 ) || ( !iL && iC==1 ) ) {
      return false;
    }

    return true;

  }


  function checkHostname( hostname )
  {
    if ( hostname.charAt(0) == '[' )
    {
      if ( hostname.charAt(hostname.length-1) != ']' )
        return false;
      var ipnr = hostname.substring( 1, hostname.length -1 );
        return checkIpnr( ipnr );
    }

    if ( hostname.charAt(0) == '#' )
    {
      var nr = hostname.substring( 1, hostname.length );
        return checkNr( nr );
    }

    return checkFqdn( hostname );
  }


  function checkEmailAdr( address )
  {
    var status = true;
    var username = "";
    var hostname = "";

    if ( address.length < 8 )
      return false;

    var seperate = address.lastIndexOf("@");
    if ( seperate == -1 )
      return false;

    username = address.substring(0, seperate );
    if ( ! checkUsername( username, "<>()[],;:@\" " ) )
      return false;

    hostname = address.substring(seperate+1, address.length );
    if ( ! checkHostname( hostname ) )
      return false;

    return true;
  }


  function checkEmail( email, allowFullname )
  {
    var existFullname = false;
    var status = true;
    var fullname = "";
    var adress = "";
    if ( email.length < 8 )
      return false;
    var emailBegin = email.indexOf("<");
    var emailEnd = email.lastIndexOf(">");

    if ( (emailBegin == -1) && (emailEnd == -1) )
      return checkEmailAdr( email );

    if ( ( (emailBegin == -1) && (emailEnd != -1) )
        || ( (emailBegin != -1) && (emailEnd == -1) ) )
      return false;

    adress = email.substring( emailBegin+1, emailEnd );

    if ( ! checkEmailAdr( adress ) )
      return false;

    if ( email.length == adress.length + 2 )
      return true;
    else
      if ( ! allowFullname )
        return false;

    if ( emailEnd == email.length - 1 )
    {
      if ( emailBegin == 0 )
        return true;
      if ( email.charAt( emailBegin -1 ) != ' ' )
        return false;
      fullname = email.substring( 0, emailBegin-1 );
      return checkUsername ( fullname, "<>()[],;:@\"" );
    }
    return false ;
  }