jQuery(document).ready(function(){
	if (get_GET_param('IA-Code')) {
		jQuery(".tx-srfeuserregister-pi1 #tx-srfeuserregister-pi1-customer").attr("checked","checked");
		jQuery("#tx-srfeuserregister-pi1-kunde_kg").val(get_GET_param('IA-Code'));
		jQuery("#tx-srfeuserregister-pi1-kunde_luf").val(get_GET_param('IA-Code'));
	}
	jQuery.each(jQuery(".tx-srfeuserregister-pi1 input"), function(){
		if (jQuery(this).attr("title")) {
			jQuery(this).before('<div class="help"><div class="help-top">' + jQuery(this).attr("title") + '</div><div class="help-bottom">&nbsp;</div></div>');
		}
		jQuery(this).removeAttr("title");
	});
	jQuery(".tx-srfeuserregister-pi1 input").focus(function() {
		cObj = this;
		jQuery(cObj).siblings(".help").css("top", -jQuery(cObj).siblings(".help").height());
		jQuery(cObj).siblings(".help").fadeIn();
		// hide error message for this element (after form has been submitted)
		jQuery(cObj).siblings(".tx-srfeuserregister-pi1-error").slideUp();
	}).blur(function() {
		jQuery(this).siblings(".help").fadeOut();
	});
	/*jQuery(".tx-srfeuserregister-pi1 input").focus(function() {
		jQuery('<div class="help">' + jQuery(this).attr("title") + '</div>').insertBefore(jQuery(this)).hide().slideDown();
	}).blur(function() {
		jQuery(this).siblings(".help").slideUp().remove();
	});
	*/
	if (jQuery(".tx-srfeuserregister-pi1 #tx-srfeuserregister-pi1-customer:checked").length) {
		jQuery(".tx-srfeuserregister-pi1 .customer").slideDown();
	}
	jQuery(".tx-srfeuserregister-pi1 #tx-srfeuserregister-pi1-customer").click(function() {
		jQuery(".tx-srfeuserregister-pi1 .customer").slideToggle();
		jQuery(".tx-srfeuserregister-pi1 .customer input").val("");
	});
	
	jQuery("input#tx-srfeuserregister-pi1-password").pstrength({
		verdects: ['sehr unsicher', 'unsicher', 'normal', 'sicher', 'sehr sicher'],
		scores : [10,15,20,25],
		pathToFiles : 'fileadmin/templates/design/images/'
	});
	
	// hide all sr_feuser_register error messages (validated by php) by submit
	jQuery("#tx-srfeuserregister-pi1-fe_users_form").submit(function() {
		jQuery(".tx-srfeuserregister-pi1-error").hide();
	});
	// form validation
	jQuery.validator.setDefaults({
		errorClass: "validation-error",
		errorElement: "div",
		errorPlacement: function(label, element) {
			label.insertAfter(element);
		},
		highlight: function( element, errorClass, validClass ) {
			jQuery(element).closest(".field-wrap").addClass("field-wrap-error").removeClass("field-wrap-valid");
			jQuery(element).addClass(errorClass).removeClass(validClass);
		},
		unhighlight: function( element, errorClass, validClass ) {
			jQuery(element).closest(".field-wrap").removeClass("field-wrap-error").addClass("field-wrap-valid");
			jQuery(element).removeClass(errorClass).addClass(validClass);
		}
	});
	jQuery("#tx-srfeuserregister-pi1-fe_users_form").validate({
		rules: {
			"FE[fe_users][username]": {
				required: true,
				minlength: 4
			},
			"FE[fe_users][password]": {
				required: true,
				minlength: 5
			},
			"FE[fe_users][password_again]": {
				required: true,
				minlength: 5,
				equalTo: "#tx-srfeuserregister-pi1-password"
			},
			"FE[fe_users][email]": {
				required: true,
				email: true
			},
			"FE[fe_users][first_name]": {
				required: true
			},
			"FE[fe_users][last_name]": {
				required: true
			}
		},
		messages: {
			"FE[fe_users][username]": {
				required: "Bitte geben Sie einen Benutzernamen ein.",
				minlength: "Ihr Benutzername muss mindestens 4 Zeichen enthalten."
			},
			"FE[fe_users][password]": {
				required: "Bitte geben Sie ein Passwort ein.",
				minlength: "Das Passwort muss mindestens 5 Zeichen enthalten."
			},
			"FE[fe_users][password_again]": {
				required: "Bitte wiederholen Sie das Passwort.",
				minlength: "Das Passwort muss mindestens 5 Zeichen enthalten.",
				equalTo: "Sie müssen zweimal dasselbe Passwort eingeben."
			},
			"FE[fe_users][email]": "Bitte geben sie eine gültige E-Mail-Adresse an.",
			"FE[fe_users][first_name]": {
				required: "Bitte geben Sie Ihren Vornamen ein."
			},
			"FE[fe_users][last_name]": {
				required: "Bitte geben Sie Ihren Nachnamen ein."
			},
			"FE[fe_users][kunde_luf]": {
				creditcard: "Bitte geben Sie eine gültige Kundennummer ein (Zahlen und Minus sind erlaubt)."
			},
			"FE[fe_users][kunde_kg]": {
				creditcard: "Bitte geben Sie eine gültige Kundennummer ein (Zahlen und Minus sind erlaubt)."
			}
		}
	});
	
	// check if confirm password is still valid after password changed
	jQuery("#tx-srfeuserregister-pi1-password").blur(function() {
		if (jQuery("#tx-srfeuserregister-pi1-password_again").val()) {
			jQuery("#tx-srfeuserregister-pi1-password_again").valid();
		}
	});
});

/* jQuery Password Strength Plugin (pstrength) - A jQuery plugin to provide accessibility functions
 * Author: Tane Piper (digitalspaghetti@gmail.com) 
 * Website: http://digitalspaghetti.me.uk
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * 
 * === Changelog ===
 * Version 1.1 (20/08/2007)
 * Changed code to be more jQuery-like
 * 
 * Version 1.0 (20/07/2007)
 * Initial version.
 */
(function($){
	$.fn.pstrength = function(o) {
		var o = $.extend({
			verdects: ['very weak', 'weak', 'medium', 'strong', 'stronger'],
			scores : [10,15,20,25],
			pathToFiles : ''
		},o);		
		return this.each(function(){
			var e = $(this).attr('id');
			$(this).after('<div id="' + e + '_text"></div>');
			$(this).after('<div id="' + e + '_bar" style="border: 1px solid white; font-size: 1px; height: 2px; width: 0px;"></div>');
			$(this).keyup(function(){				
				$.fn.runPassword($(this), $(this).val(), e, o);
			});
		});
	}
	$.fn.runPassword = function (cObj, p, f, o){
			// Check password
			nPerc = $.fn.checkPassword(p, o);	
	 		// Get controls
    		var ctlBar = "#" + f + "_bar"; 
    		var ctlText = "#" + f + "_text";
    		// Set new width
    		var nRound = Math.round(nPerc * 2.2);
			if (nRound < (p.length * 5)) 
			{ 
				nRound += p.length * 5; 
			}
			if (nRound > 100)
				nRound = 100;
				$(ctlBar).css({width: nRound + "%"});
			// Color and text		
			if(nPerc <= o.scores[0])
			{
		   		//strColor = "000000";
		   		strFile = "password_veryweak.gif";
	 			strText = o.verdects[0];
			}
			else if (nPerc > o.scores[0] && nPerc <= o.scores[1])
			{
		   		//strColor = "000000";
		   		strFile = "password_weak.gif";
	 			strText = o.verdects[1];
			}
			else if (nPerc > o.scores[1] && nPerc <= o.scores[2])
			{
			   	//strColor = "#000000";
			   	strFile = "password_normal.gif";
	 			strText = o.verdects[2];
			}
			else if (nPerc > o.scores[2] && nPerc <= o.scores[3])
			{
			   	//strColor = "000000";
			   	strFile = "password_strong.gif";
	 			strText = o.verdects[3];
			}
			else
			{
			   	//strColor = "#000000";
			   	strFile = "password_stronger.gif";
	 			strText = o.verdects[4];
			}
			//cObj.css({backgroundColor: strColor});
			//$(ctlText).html("<span style='color: " + strColor + ";'>" + strText + "</span>");
			$(ctlText).html('Passwortsicherheit: <img src="' + o.pathToFiles + strFile + '" />');
		}
		$.fn.checkPassword = function(p, o)
		{
			var intScore = 0;
			var strVerdict = o.verdects[0];	
			// PASSWORD LENGTH
			if (p.length<5)                         // length 4 or less
			{
				intScore = (intScore + 3)
			}
			else if (p.length>4 && p.length<8) // length between 5 and 7
			{
				intScore = (intScore+6)
			}
			else if (p.length>7 && p.length<16)// length between 8 and 15
			{
				intScore = (intScore+12)
			}
			else if (p.length>15)                    // length 16 or more
			{
				intScore = (intScore+18)
			}
			// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
			if (p.match(/[a-z]/))                              // [verified] at least one lower case letter
			{
				intScore = (intScore+1)
			}
			if (p.match(/[A-Z]/))                              // [verified] at least one upper case letter
			{
				intScore = (intScore+5)
			}
			// NUMBERS
			if (p.match(/\d+/))                                 // [verified] at least one number
			{
				intScore = (intScore+5)
			}
			if (p.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
			{
				intScore = (intScore+5)
			}
			// SPECIAL CHAR
			if (p.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
			{
				intScore = (intScore+5)
			}
			// [verified] at least two special characters
			if (p.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
			{
				intScore = (intScore+5)
			}
			// COMBOS
			if (p.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
			{
				intScore = (intScore+2)
			}
			if (p.match(/([a-zA-Z])/) && p.match(/([0-9])/)) // [verified] both letters and numbers
			{
				intScore = (intScore+2)
			}
	 		// [verified] letters, numbers, and special characters
			if (p.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
			{
				intScore = (intScore+2)
			}
			return intScore;
		}
	
})(jQuery);
