function confirmLink(theLink, confirmMsg)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(confirmMsg);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function  

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function MakeNum(str)
{
	if((str >= 0) && (str <= 9)) {
		return str;
	}
	switch(str.toUpperCase())
	{
		case "A": return 10;
		case "B": return 11;
		case "C": return 12;
		case "D": return 13;
		case "E": return 14;
		case "F": return 15;
	}
}

function Hex2Num (hex)
{
	var num;
	var total;
	var i;
	
	total = 0;
	for (i=0;i<hex.length;i++) {
		letter = hex.substr(i,1);
		num = MakeNum(letter);
		exponent = ((hex.length-1)-i);
		total += num * Math.pow(16,exponent);
	}
	
	return total;
}

function Num2Hex (num)
{
	s=(num.toString(16));
	s=s.toUpperCase();
	return(s); 
}

function Hex2String (hexstring)
{
	var str;
	var num;
	var hexval;
	
	totalnum = 0;
	str = "";
	for (i=0;i<hexstring.length;i=i+2) {
		hexval = hexstring.substr(i,2);
		num = Hex2Num(hexval);
		str += String.fromCharCode(num);
	}
	return str;
}

function String2Hex (str)
{
	var hex = "";
	for (i=0;i<str.length;i=i+1) {
		num = str.charCodeAt(i);
		hex += Num2Hex(num);
	}	
	
	return hex;
}

function selectElement(elemname)
{
	var elem = document.getElementById(elemname);
	if (elem) {
		if (elem.type == "checkbox") {
			elem.checked = !elem.checked;
		}
	}
}

// Funktion zum Löschen eines Arrayelementes
function array_del(n)
{
	var vorne = this.slice(0, n);
	var hinten = this.slice(n+1, this.length);

	return vorne.concat(hinten);
}

// als Methode für alle Arrays einführen
Array.prototype.del=array_del;

// wahr, wenn das Zeichen eine Zahl ist 
function isDigit( ch )
{
	if ( (ch >= '0') && (ch <= '9') )
		return true;
	else
		return false;
}

// wahr, wenn das Zeichen ein Buchstabe ist
function isAlpha( ch )
{
	if ( ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) )
		return true;
	else
		return false;
}

// wahr, wenn das Zeichen alphanumerisch ist
function isAlnum(ch)
{
	if ( isAlpha( ch ) || isDigit( ch ) )
		return true;
	else
		return false;
}

// wahr, wenn kein Zeichen aus str2 in str1 vorkommt 
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;
}

// wahr, wenn eine Zifferfolge vorliegt
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;
}

// wahr, wenn IP-Adresse als gütig eingestuft wurde 
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;
}

// wahr, wenn der Fully Qualified Domain Name als gütig eingestuft wurde
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;
}

// wahr, wenn der Username einer Mail gültig ist 
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;
}

// wahr, wenn der Hostname als gütig eingestuft wurde
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 );
}

// wahr, wenn die E-mail ohne Realname als gütig eingestuft wurde
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;
}

// wahr, wenn die E-Mail Adresse als gütig eingestuft wurde, wobei der zweite Parameter festlegt, ob Realname akzeptiert werden oder nicht
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 ;

}
/*Check input length of text fields 2006-02-01 MS*/
function CheckMaxLength(field, maxlen)
{
	var curLen = field.value.length;
	if ( curLen > maxlen)
	{
		field.value = field.value.substring(0,maxlen);
	return true;
	}
	else
	{
		// generate span field id-string
		var strFieldLeft = field.name + "_CharsLeft";
		//get pointer to text container:
		var objSpan=document.getElementById(strFieldLeft);

		if (!objSpan)
			return true;
	
		objSpan.innerHTML = (maxlen - curLen);
	}
	return true;
}

function toggleDisplay(element)
{
	element = document.getElementById(element);
	if (element.style.display=="none")
	{
		element.style.display="block";
	}
	else 
	{
		if (element.style.display=="block")
		{
			element.style.display="none";
		}
	}
}

function delete_gallery_image(image){
	if(confirm('Soll das Bild wirklich gelöscht werden?')) {
		window.location.replace('rendersite.php?LAYOUT=usercp_gallery&world=4players&delete='+image)
	}
}
