//*********************************************\
// (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
}

// mod ck: zusätzlich diese:

 function checkform()
 {
   if (document.reg_form.firstname.value == "" || document.reg_form.firstname.value == "First name")
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_firstname').style.color = '#ff0000';
     //alert("Please enter your first name!");
     document.reg_form.firstname.focus();
     return false;
   } else {
      document.getElementById('reg_firstname').style.color = '#000000';
   }
   
   if (document.reg_form.lastname.value == "" || document.reg_form.lastname.value == "Last name")
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_lastname').style.color = '#ff0000';
     //alert("Please enter your last name!");
     document.reg_form.lastname.focus();
     return false;
   } else {
      document.getElementById('reg_lastname').style.color = '#000000';
   }
   
    if (document.reg_form.clinic.value == "")
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_clinic').style.color = '#ff0000';
     //alert("Please enter your job title !");
     document.reg_form.clinic.focus();
     return false;
   }    else {
      document.getElementById('reg_clinic').style.color = '#000000';
   }
   
   if (document.reg_form.jobtitle.value == "")
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_jobtitle').style.color = '#ff0000';
     //alert("Please enter your job title !");
     document.reg_form.jobtitle.focus();
     return false;
   }    else {
      document.getElementById('reg_jobtitle').style.color = '#000000';
   }
   
   
    if (document.reg_form.password.value == "" || document.reg_form.password.value.length < 6 || document.reg_form.password.value=='zimmer2007' || document.reg_form.password.value=='zimmer2006' || document.reg_form.password.value=='revision2006')
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_pw').style.color = '#ff0000';
     if (document.reg_form.password.value=='zimmer2007' || document.reg_form.password.value=='zimmer2006' || document.reg_form.password.value=='revision2006') {
        alert("Bitte wählen Sie ein anderes Passwort als zimmer2007, zimmer2006 oder revision2006 !");
     }
     document.reg_form.password.focus();
     return false;
   }  else {
      document.getElementById('reg_pw').style.color = '#000000';
   }
   
    if (document.reg_form.password_confirm.value == "" || document.reg_form.password_confirm.value!= document.reg_form.password.value)
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_pwconfirm').style.color = '#ff0000';
     //alert("Please enter your email address!");
     document.reg_form.password_confirm.focus();
     return false;
   }  else {
      document.getElementById('reg_pwconfirm').style.color = '#000000';
   }
   
    if (!document.reg_form.legal.checked)
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_legal').style.color = '#ff0000';
     //alert("Please enter your email address!");
     document.reg_form.regb_legal.focus();
     return false;
   }  else {
      document.getElementById('reg_legal').style.color = '#000000';
   }
   
   if (document.reg_form.email.value == "")
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_email').style.color = '#ff0000';
     //alert("Please enter your email address!");
     document.reg_form.email.focus();
     return false;
   }  else {
      document.getElementById('reg_email').style.color = '#000000';
   }
   
   if (document.reg_form.email.value.indexOf('@') == -1)
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_email').style.color = '#ff0000';
     // alert("Please correct your email address!");
     document.reg_form.email.focus();
     return false;
   }
   
   if (document.reg_form.email.value.indexOf('.') == -1)
   {
     document.getElementById('errormsg').style.display = 'block';
     document.getElementById('reg_email').style.color = '#ff0000';
     //alert("Please correct your email address!");
     document.reg_form.email.focus();
     return false;
   }
      
 }
