//*********************************************\
// (c)2001 dun@argo-tec.de : FORM_VALIDATOR.js *
//*********************************************/

// functions for testing (write your own!!)

// ------- List of Functions ----------
// * R_Text() 		: Required Text
// * O_Text() 		: Optional Text
// * R_Email() 		: Required Email
// * R_Date()		: Required Date-Format (!!not working))
// * R_Name()		: Required Name without Special Chars
// * R_Telefone()	: Required Telefone without Special Chars
// * R_Radio()		: Required Radio1/Radio2


// DUMMY-Funktion for new test-functions
function DUMMY( msg ) { tF=testField;
	return ( 'enter isValid-condition here!' ) ? true : msg ;
}

// not Default and more than x-letters
function O_Text( msg,len ){ tF=testField;
	return ( 	tF.value==""
			|| 	tF.value.length > len  ) ? true: msg ;
}

// not Default and more than x-letters
function R_Text( msg,len ){ tF=testField;
	return ( 	tF.value.length > len  ) ? true: msg ;
}

// not Default and a valid Email containing @ and .
function R_Email( msg ){ tF=testField;
	return ( 	tF.value.length>6
			&& 	tF.value.indexOf('@')>0
			&& 	tF.value.indexOf('.')>0 ) ? true: msg;
}

// not Default and a valid Email containing @ and .
function O_Email( msg ){ tF=testField;
	if (tF.value==tF.defaultValue ) return true;
	return ( 	tF.value.length>6
			&& 	tF.value.indexOf('@')>0 
			&& 	tF.value.indexOf('.')>0 ) ? true: msg;
}

// a valid Date !!!wrong, does R_Text only !!!
function R_Date( msg  ){ tF=testField;
	//not good!
	return ( 	tF.value.length > 3  ) ? true: msg;
}

// a name which cannot contain special characters
function R_Name( msg, len ) { tF=testField;
	if (tF.value.length < len ) {return msg;}
	
	for(i=0; i<tF.value.length; i++) {  
        x=tF.value.charCodeAt(i);
        if(   x < 32) {return msg;}
        if( (x != 32) && // Leerzeichen
        	(x != 38) && // &
        	(x != 39) && // '
        	(x != 43) && // +
        	(x != 45) && // -
        	(x != 46) && // .
			(x <  65 || x > 90) &&  // A-Z
        	(x <  97 || x > 122) && // a-z
			(x <  192) // alle möglichen Sonderzeichen
			) {  return msg;  }
    }
return true;
}
 
//check if Radio 1 or 2 is checked
function R_Radio( msg ) { tF=testField;
	return ( tF[0].checked || tF[1].checked ) ? true : msg ;
}

//check if Radio 1 or 2 is checked
function R_Checkbox( msg ) { tF=testField;
	return ( tF.checked ) ? true : msg ;
}

//check if selected-value in combo-box is set
function R_Select( msg ) { tF=testField;
	return ( tF.options[ tF.selectedIndex ].value != "" ) ? true : msg ;
}


//check valid Telephone
function R_Telephone(msg, erlaubte_Zeichen,maxlen) { tF=testField;
	// check length beween 1 and maxlength
	if (  (tF.value.length > maxlen) || (tF.value.length=0 )  ) { return msg; } 

	// check for invalid characters
	for(i=0;i< tF.value.length;i++) { 
		if(erlaubte_Zeichen.indexOf(tF.value.charAt(i))== -1)  { return msg ; }
	}

	return true;
}

//loop through all formfield and validate them.

//make globals;
var errors		='';
var testField	='';

function checkForm( f,prefix ){
	//clear values
	var errors		='';
	var firstFocus  ='';
		
		for ( i=0; i<f.length ; i++ ){
			// prepare some vars from Form-Data
			fe		=f.elements[i];
			f_cmd 	=fe.name.substring(0,9);
			f_aim 	=fe.name.substring(9);
			f_test  =fe.value;
			
			// if Command is Validate then doValidate and add Error to stack
			if ( f_cmd == 'Validate_' ){
				testField=f.elements[ f_aim ]; /* global testValue */
				if ( (msg=eval( f_test )) !=true  ) {
					errors+=msg +'\n';
					// put focus on first and only first wrong field
					if (firstFocus=='' ) { firstFocus='set'; testField.focus(); }
				}
			}
		}
	
	if ( errors!='') {
		alert( prefix + errors );
		return false;
	}
	
	//alert( 'ok' );
	return true;
	//return false; // later return true, means accept submit
}

