// ==================================
//	nsm_FormCheck
//
//	Anwendung:
//			nsm_FormCheck.init( meinFormular );
//			nsm_FormCheck.addField( "email", "Ihre E-Mail Adresse" );
// 		oder
// 			nsm_FormCheck.addFields( "email:Ihre E-Mail Adresse", "vorname:Ihren Vornamen" );
//
// 			nsm_FormCheck.check();
//		
//	matchPasswords		: sobald genau zwei Passwort-Felder gefunden werden, findet ein Vergleich statt
// 	doSubmit		 	: wird die Funktion nicht durch einen Submit-Button aufgerufen, kann das Formular nach
//					  erfolgreicher Ueberpruefung automatisch gesendet werden
// ==================================


var nsm_FormCheck =
{
	dialogName		: "Fehlende Angaben.",
	msg				: "Bitte geben Sie %s an.",
	msgCorrect      : "Bitte geben Sie %s korrekt an.",
	pwNotEqualMsg	: "Die eingegebenen Passw%F6rter stimmen nicht %FCberein. Bitte wiederholen Sie die Eingabe."
}


nsm_FormCheck.init = function( formName )
{
	this.formName	= formName;
	this.requiredFields	= new Array();
	this.pwFields		= new Array();
}


nsm_FormCheck.addField = function( fieldName, fieldMsg )
{
	this.requiredFields[ this.requiredFields.length ] = fieldName + "#*#" + fieldMsg;
}


nsm_FormCheck.addFields = function( args )
{
	for ( var i = 0; i < this.addFields.arguments.length; i++ )
	{
		var data = this.addFields.arguments[ i ].split( ":" );
		this.addField( data[ 0 ], data[ 1 ] );
	}
}


nsm_FormCheck.autoRegisterRequiredFields = function()
{
    this.init(this.formName);
	for ( var i = 0; i < document[ this.formName ].elements.length; i++ )
	{	
		field = document[ this.formName ].elements[ i ];
		if ( field.title == undefined )
			continue;
		
		if ( (	field.type == "text"
			 || field.type == "textarea"
			 || field.type == "password"
			 || field.type == "select-one" )
			&& field.title.replace( /\s/g, "" ) != "" 
			&& field.name.replace( /\s/g, "" ) != "" )
		{
			this.addField( field.name, field.title )
		}
	}
}


nsm_FormCheck.check = function( matchPasswords, doSubmit )
{
	for ( var i = 0; i < this.requiredFields.length; i++ )
	{
		var data		= this.requiredFields[ i ].split( "#*#" );
		var fieldName	= data[ 0 ];
		var fieldMsg	= data[ 1 ];
		
		// Formularfeld evaluieren
		try
		{
			var field	= document[ this.formName ][ fieldName ];
			var value	= unescape( field.value.replace( /\s/g, "" ) );
		}
		catch ( e )
		{
			alert( unescape( "Skriptfehler! Das Feld mit dem Namen " + fieldName + " existiert nicht im Formular " + this.formName + ". Bitte %FCberpr%FCfen Sie das Skript." ) );
			return false;
		}
		
		
		// Leeres Feld gefunden	
		if ( value == "" )
		{
			this.displayAlertMsg( fieldMsg );
			this.clearAndFocusField( field );
			
			return false;
		}
		
		
		// eMail-Feld gefunden
		if ( field.name == "email" )
		{
			if ( this.isValidEmail( value ) )
				continue;
			else
			{
				this.displayAlertMsgC( fieldMsg );
				this.clearAndFocusField( field );
				
				return false;
			}
		}
		
		
		// Passwort-Feld gefunden
		if ( field.type == "password" )
			this.pwFields[ this.pwFields.length ] = field;
		
		
	}
	
	// Wenn genau 2 Passwort-Felder gefunden wurden, diese vergleichen
	if ( this.pwFields.length == 2 && matchPasswords == true )
	{
		var pw		= this.pwFields[ 0 ].value.replace( /\s/g, "" );
		var pwConfirm	= this.pwFields[ 1 ].value.replace( /\s/g, "" );
		
		if ( pw != pwConfirm )
		{
			alert( unescape( this.pwNotEqualMsg ) );
			this.pwFields[ 1 ].value = "";
			this.clearAndFocusField( this.pwFields[ 0 ] );
			
			return false;
		}				
	}
	
	if ( doSubmit == true )
		document[ formName ].submit();
		
		
	return true;
}


nsm_FormCheck.clearAndFocusField = function( field )
{
	//field.value = "";
	field.scrollIntoView();
	field.focus();
}


nsm_FormCheck.displayAlertMsg = function( msg )
{
	if ( msg.substr( 0, 1 ) == "-" )
		alert( unescape( msg.substr( 1 ) ) );
	else if ( msg.substr( 0, 1 ) == "+" )
		alert( unescape( this.dialogName + " " + msg.substr( 1 ) ) );
	else
		alert( unescape( this.dialogName + " " + this.msg.replace( /%s/, msg ) ) );
}

nsm_FormCheck.displayAlertMsgC = function( msg )
{
	if ( msg.substr( 0, 1 ) == "-" )
		alert( unescape( msg.substr( 1 ) ) );
	else if ( msg.substr( 0, 1 ) == "+" )
		alert( unescape( this.dialogName + " " + msg.substr( 1 ) ) );
	else
		alert( unescape( this.dialogName + " " + this.msgCorrect.replace( /%s/, msg ) ) );
}

nsm_FormCheck.isValidEmail = function( email )
{
 	var a		= false;
 	var res	= false;
 
 	if( typeof( RegExp ) == 'function' )
 	{
  		var b = new RegExp( 'abc' );
  		
  		if ( b.test( 'abc' ) == true )
  			a = true;
  	}

 	if ( a == true )
 	{
  		reg = new RegExp( 	'^([a-zA-Z0-9\\-\\.\\_]+)' +
                   				'(\\@)([a-zA-Z0-9\\-\\.]+)' +
                   				'(\\.)([a-zA-Z]{2,4})$');
  		res = ( reg.test( email ) );
 	}
 	else
 	{
  		res = ( email.search( '@' ) >= 1 &&
         		email.lastIndexOf( '.' ) > email.search( '@' ) &&
         		email.lastIndexOf( '.' ) >= email.length-5 )
 	}
 	
 	return( res );
 }
 
 
nsm_FormCheck.setDialogName = function( txt )
{
	this.dialogName = txt;
}


nsm_FormCheck.setErrorMsg = function( txt )
{
	this.msg = txt;
}


nsm_FormCheck.setPwNotEqualMsg = function( txt )
{
	this.pwNotEqualMsg = txt;
}
