// FOlgende Variablen solltne im Script durch sinnvollere Werte ersetzt werden
var unMinLength = 0;
var unMaxLength = 255;
var pwMinLevel = 0;
var pwLowLevel = 1;
var pwMinLength = 0;

var cUNTimer = null;
var cEATimer = null;
var cPWTimer = null;
var unCheckResult = -99;
var eaCheckResult = -99;
var pwCheckResult = -99;

//Cache für UserName (Values: "-":Busy, -10:Invalid, -1:TooShort, -2:TooLong, -3:Missing, 0:AlreadyUsed, 1:Available)
var cUNCache = new Hash();

//Cache für eMail-Adresse (Values: "-":Busy, -10:Invalid, -3:Missing, 0:AlreadyUsed, 1:Available)
var cEACache = new Hash();

//Cache für PW (Values: "-":Busy, -10:Invalid, -4: 2Short, -3:Missing, -2: Blacklist, -1: Username, 0-8: Level)
var cPWCache = new Hash();

var showAllErrors=false;

function cFN(noErrToolTip) {
	var ret = true;
	var showErr = false;

	$$("#tt_fn div.registerBoxMsg").each( function(s) { $(s).hide(); } );
	if ( $('fn').value.length < 2 ) {
		ret=false;
		if (showAllErrors) {
			showErr=true;
			$('fn_msg_required').show();
		}
	}	
	
	if (noErrToolTip == undefined || noErrToolTip != true) $('fn').repositionToolTip();
	if ( ret == 1 ) {
		$('fn_ok').show();
		$('fn_err').hide();
	} else if (! showErr) {
		$('fn_ok').hide();
		$('fn_err').hide();
	} else {
		$('fn_ok').hide();
		$('fn_err').show();
		if (noErrToolTip == undefined || noErrToolTip != true) {
			$('fn').showToolTip(true);
		}
	}	
	return ret;
}

function cUN() {
	if ( cUNTimer != null ) window.clearTimeout( cUNTimer );
	cUNTimer = null;

	// Cache-Zugriff versuchen --> Timer Verzögerung umgehen
	var un = $('un').value;
	var cachedValue = cUNCache.get(un);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cUN_callback(cachedValue, un);
		return;
	}

	cUNTimer = window.setTimeout("cUNDo();", 350);
}

function cUNDo(noErrToolTip) {
	if ( noErrToolTip == undefined ) noErrToolTip = false;
	
	$('un_ok').hide();
	$('un_err').hide(); 
	$("un_busy").hide();

	if ( cUNTimer != null ) {
		window.clearTimeout( cUNTimer );
		cUNTimer = null;
	}

	// Cache-Zugriff versuchen
	var un = $('un').value;
	var cachedValue = cUNCache.get(un);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cUN_callback(cachedValue, un, noErrToolTip);
		return;
	}

	// Client-seitig prüfbare Fehler abfangen
	var errCode=0;
	if ( un == "" ) errCode = -3;						// Leer?
	else if ( un.length < unMinLength) errCode = -1; 	// Zu kurz?
	else if ( un.length > unMaxLength) errCode = -2; 	// Zu lang?
	if (errCode != 0) {
		cUN_callback(errCode, un, noErrToolTip);
		return;
	}

	// Start AJAX-Check
	$("un_busy").show();
	unCheckResult = -99;
	
	cUNCache.set(un , "-");
	
	ajaxURL = "/ajax/usernamecheck.php?check=" + encodeURIComponent(un);

	new Ajax.Request(ajaxURL, {
		onSuccess: function(transport) {
			// AJAX-request succeeded
			$("un_busy").hide();
			cUN_callback(transport.responseText, un, noErrToolTip);
		}
	});	
}

function cUN_callback(resp, un, noErrToolTip) {	
	unCheckResult = parseInt(resp);

	var showErr = false;

	$$("#tt_un div.registerBoxMsg").each( function(s) { $(s).hide(); } );		
	if ( unCheckResult == 1  ) {		
		$("un_msg_ok").show();
	} else if ( unCheckResult == 0 ) {
		$("un_msg_alreadyused").show();
		showErr = true;
	} else if ( unCheckResult == -1 ) {
		if (showAllErrors) {
			$("un_msg_tooshort").show();
			showErr = true;
		} 
	} else if ( unCheckResult == -3 ) {
		if (showAllErrors) {
			$("un_msg_required").show();
			showErr = true;
		} 
	} else if ( unCheckResult == -10 ) {
		$("un_msg_invalid").show();
		showErr = true;
	} else {
		$("un_msg_other").show();
		showErr = true;
	}

	cUNCache.set(un, unCheckResult);
	if (noErrToolTip == undefined || noErrToolTip != true) $('un').repositionToolTip();
	if ( unCheckResult == 1 ) {
		$('un_ok').show();
		$('un_err').hide();
	} else if (! showErr) {
		$('un_ok').hide();
		$('un_err').hide();
	} else {
		$('un_ok').hide();
		$('un_err').show();
		if (noErrToolTip == undefined || noErrToolTip != true) {
			$('un').showToolTip(true);
		}
	}
}

function cEA() {
	if ( cEATimer != null ) window.clearTimeout( cEATimer );
	cEATimer = null;

	// Cache-Zugriff versuchen --> Timer Verzögerung umgehen
	var ea = $('ea').value;
	var cachedValue = cEACache.get(ea);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cEA_callback(cachedValue, ea);
		return;
	}

	cEATimer = window.setTimeout("cEADo();", 350);
}

function cEADo(noErrToolTip) {
	if ( noErrToolTip == undefined ) noErrToolTip = false;
	
	$('ea_ok').hide();
	$('ea_err').hide(); 
	$("ea_busy").hide();

	if ( cEATimer != null ) {
		window.clearTimeout( cEATimer );
		cEATimer = null;
	}

	// Cache-Zugriff versuchen
	var ea = $('ea').value;
	var cachedValue = cEACache.get(ea);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cEA_callback(cachedValue, ea, noErrToolTip);
		return;
	}

	// Client-seitig prüfbare Fehler abfangen
	var errCode=0;
	if ( ea == "" ) errCode = -3;						// Leer?
	else if ( ea.length < 6) errCode = -10;				// Zu kurz? (x@Y.TL)
	else if ( ea.indexOf('@') == -1) errCode = -10;		// Kein "@"?
	else if ( ea.indexOf('.') == -1) errCode = -10;		// Kein "."?
	if (errCode != 0) {
		cEA_callback(errCode, ea, noErrToolTip);
		return;
	}

	// Start AJAX-Check
	$("ea_busy").show();
	eaCheckResult = -99;
	
	cEACache.set(ea , "-");
	
	ajaxURL = "/ajax/emailcheck.php?check=" + encodeURIComponent(ea);

	new Ajax.Request(ajaxURL, {
		onSuccess: function(transport) {
			// AJAX-request succeeded
			$("ea_busy").hide();
			cEA_callback(transport.responseText, ea, noErrToolTip);
		}
	});	
}

function cEA_callback(resp, ea, noErrToolTip) {	
	eaCheckResult = parseInt(resp);

	var showErr = false;

	$$("#tt_ea div.registerBoxMsg").each( function(s) { $(s).hide(); } );		
	if ( eaCheckResult == 1  ) {		
		$("ea_msg_ok").show();
	} else if ( eaCheckResult == 0 ) {
		$("ea_msg_alreadyused").show();
		showErr = true;
	} else if ( eaCheckResult == -3 ) {
		if (showAllErrors) {
			$("ea_msg_required").show();
			showErr = true;
		} 
	} else if ( eaCheckResult == -10 ) {
		if (showAllErrors) {
			$("ea_msg_invalid").show();
			showErr = true;
		} 
	} else {
		$("ea_msg_other").show();
		showErr = true;
	}


	// Problematisch?
	if ( eaCheckResult == 1 && ea.indexOf('@aol.') >= 0 ) {
		$('ea_msg_problematic').show();
	} else {
		$('ea_msg_problematic').hide();
	}
	
	

	cEACache.set(ea, eaCheckResult);
	if (noErrToolTip == undefined || noErrToolTip != true) $('ea').repositionToolTip();
	if ( eaCheckResult == 1 ) {
		$('ea_ok').show();
		$('ea_err').hide();
	} else if (! showErr) {
		$('ea_ok').hide();
		$('ea_err').hide();
	} else {
		$('ea_ok').hide();
		$('ea_err').show();
		if (noErrToolTip == undefined || noErrToolTip != true) {
			$('ea').showToolTip(true);
		}
	}
}

function cPW() {
	if ( cPWTimer != null ) window.clearTimeout( cPWTimer );
	cPWTimer = null;

	// Cache-Zugriff versuchen --> Timer Verzögerung umgehen
	var pw = $('pw').value;
	var cachedValue = cPWCache.get(pw);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cPW_callback(cachedValue, pw);
		return;
	}

	cPWTimer = window.setTimeout("cPWDo();", 350);
}

function cPWDo(noErrToolTip) {
	if ( noErrToolTip == undefined ) noErrToolTip = false;
	
	$('pw_ok').hide();
	$('pw_err').hide(); 
	$("pw_busy").hide();

	if ( cPWTimer != null ) {
		window.clearTimeout( cPWTimer );
		cPWTimer = null;
	}

	// Cache-Zugriff versuchen
	var pw = $('pw').value;
	var cachedValue = cPWCache.get(pw);
	if ( cachedValue != undefined ) {
		if ( cachedValue != "-") cPW_callback(cachedValue, pw, noErrToolTip);
		return;
	}

	// Client-seitig prüfbare Fehler abfangen
	var errCode=0;
	var un = $('un').value;
	if ( pw == "" ) errCode = -3;						// Leer?
	else if ( pw.length < pwMinLength) errCode = -4;	// Zu kurz?
	else if ( pw == un ) errCode = -1;					// Username
	else if ( pw.toUpperCase() == un.toUpperCase() ) errCode = -1;	// Username
	if (errCode != 0) {
		cPW_callback(errCode, pw, noErrToolTip);
		return;
	}

	// Start AJAX-Check
	$("pw_busy").show();
	pwCheckResult = -99;
	
	cPWCache.set(pw , "-");
	
	ajaxURL = "/ajax/pwseccheck.php?check=" + encodeURIComponent(pw);

	new Ajax.Request(ajaxURL, {
		onSuccess: function(transport) {
			// AJAX-request succeeded
			$("pw_busy").hide();
			cPW_callback(transport.responseText, pw, noErrToolTip);
		}
	});	
}

function cPW_callback(resp, pw, noErrToolTip) {	
	pwCheckResult = parseInt(resp);

	var showErr = false;

	$$("#tt_pw div.registerBoxMsg").each( function(s) { $(s).hide(); } );		
	if ( pwCheckResult >= 7  ) {		
		$("pw_msg_top").show();
		showErr = true;
	} else if ( pwCheckResult >= pwMinLevel ) { 		
		$("pw_msg_avg").show();
		showErr = true;
	} else if ( pwCheckResult >= pwLowLevel ) { 		
		$("pw_msg_low").show();
		showErr = true;
	} else if ( pwCheckResult >= 0 ) {
		$("pw_msg_bad").show();
		showErr = true;
	} else if ( pwCheckResult == -1 ) {
		$("pw_msg_usr").show();
		showErr = true;
	} else if ( pwCheckResult == -2 ) {
		$("pw_msg_bl").show();
		showErr = true;
	} else if ( pwCheckResult == -3 ) {
		if (showAllErrors) {
			$("pw_msg_required").show();
			showErr = true;
		}
	} else if ( pwCheckResult == -4 ) {
		if (showAllErrors) {
			$("pw_msg_short").show();
			showErr = true;
		}
	} else if ( pwCheckResult == -10 ) {
		$("pw_msg_invalid").show();
		showErr = true;
	} else {
		$("pw_msg_other").show();
		showErr = true;
	}

	cPWCache.set(pw, pwCheckResult);
	if (noErrToolTip == undefined || noErrToolTip != true) $('pw').repositionToolTip();
	if ( pwCheckResult >= pwMinLevel ) {
		$('pw_ok').show();
		$('pw_err').hide();
	} else if (! showErr) {
		$('pw_ok').hide();
		$('pw_err').hide();
	} else {
		$('pw_ok').hide();
		$('pw_err').show();
		if (noErrToolTip == undefined || noErrToolTip != true) {
			$('pw').showToolTip(true);
		}
	}

	// Pfeil schieben
	pwLevel = Math.max(pwCheckResult, 0);
	pwLeft = Math.floor( (160 / 8) * pwLevel );

	new Effect.Morph('pws_slider', {
		  style:{'left': pwLeft+"px" }, // CSS Properties
		  duration: 0.35 // Core Effect properties
		});

	cPW2(true);	
}

function cPW2(noErrToolTip) {
	var showOK = false;
	var showErr = false;

	$$("#tt_pw2 div.registerBoxMsg").each( function(s) { $(s).hide(); } );
	
	if ( $('pw2').value == "" ) {
		if (showAllErrors) {
			$("pw2_msg_required").show();
			showErr = true;
		}
	} else if ( $('pw').value.toUpperCase() == $('pw2').value.toUpperCase() ) {
		if ( pwCheckResult >= pwMinLevel ) {
			$("pw2_msg_ok").show();
			showOK = true;
		}
	} else {
		if (showAllErrors) {
			$("pw2_msg_err").show();
			showErr = true;
		}		
	}

	if (noErrToolTip == undefined || noErrToolTip != true) $('pw2').repositionToolTip();
	if ( showOK ) {
		$('pw2_ok').show();
		$('pw2_err').hide();
	} else if (! showErr) {
		$('pw2_ok').hide();
		$('pw2_err').hide();
	} else {
		$('pw2_ok').hide();
		$('pw2_err').show();
		if (noErrToolTip == undefined || noErrToolTip != true) {
			$('pw2').showToolTip(true);
		}
	}

	return showOK;	
}

function initForm() {
	// Meldungen, die nur für Nicht-JS-User gedacht sind ausblenden
	$$('.nonjsmsg').each( function (s) { s.hide(); } );
	// Meldungen, die nur für JS-User gedacht sind einblenden
	$$('.jsmsg').each( function (s) { s.show(); } );
	// Es soll immer nur 1 Tooltip für die Eingabemasken sichtbar sein 
	$$('.tooltip').each( function (s) { s.alwaysHideOtherToolTips=true; } );
	// Tooltips sollen nicht bei MouseOver ausgeblendet werden
	$$('.register_form .tooltip').each( function (s) { s.noAutoHideToolTip=true; } );
	
	cFN(); 
	cUNDo(true);
	cEADo(true);
	cPWDo(true);
	cPW2(true);
	
	setCursor(true);
}

function setCursor(noFNToolTip) {
	if (! cFN()) {
		$('fn').focus();
		if (noFNToolTip == undefined || noFNToolTip != true) $('fn').showToolTip();
	} else if (unCheckResult < 1) $('un').focus();
	else if (eaCheckResult < 1) $('ea').focus();
	else if (pwCheckResult < pwMinLevel) $('pw').focus();	
	else $('pw2').focus();	
}

function checkForm(jsSubmit) {
	showAllErrors = true;
	allOK = true;
	allOK = allOK && cFN(true);

	// Falls AJAX-Antwort noch nicht da ist, verzögern!
	cUNDo(true);
	if ( unCheckResult == -99 ) {
		window.setTimeout('checkForm(true)', 500);
		return false;
	}
	allOK = allOK && (unCheckResult == 1);

	// Falls AJAX-Antwort noch nicht da ist, verzögern!
	cEADo(true);
	if ( eaCheckResult == -99 ) {
		window.setTimeout('checkForm(true)', 500);
		return false;
	}
	allOK = allOK && (eaCheckResult == 1);

	// Falls AJAX-Antwort noch nicht da ist, verzögern!
	cPWDo(true);
	if ( pwCheckResult == -99 ) {
		window.setTimeout('checkForm(true)', 500);
		return false;
	}
	allOK = allOK && (pwCheckResult >= pwMinLevel);

	allOK = allOK && cPW2(true);
	
	if ( allOK ) {
		if (jsSubmit != undefined && jsSubmit == true) {
			$("register_form").submit();
			return false;
		}
		return true;
	} else {
		alert("Einige Deiner Angaben sind leider nicht korrekt.\nBitte prüfe die Felder, die mit einem Ausrufezeichen markiert sind.\nWeitere Informationen zum Problem erhältst Du, wenn Du in das jeweilige Feld klickst.");
		setCursor();
	}
	return false;
}

