function checkUncheckAll2(theElement, namestart) {
    // checks or unchecks all checkboxes in the calling form that
    // name begin with "namestart"

    var theForm = theElement.form;
    var z = 0;
    var theForm =document.getElementById('all').form;
    var debug="";

    //alert("checkUncheckAll " + theForm[1].type ); 
    var boxes = theElement.parentNode.getElementsByTagName('input');

    while (formel= boxes[z++]){
	//	if (formel.name.indexOf(namestart) == 0) {
	    formel.checked = theElement.checked;
	    //}
	debug += z + ": " + formel.name + " " +
	    formel.name.indexOf(namestart) + " " +  formel.type + ";";
    }
    alert (debug);
}

function checkUncheckAll(oCheckbox, namestart) {
    //alert("checkUncheckAll " + oCheckbox.type ); 
    var boxes = oCheckbox.form.getElementsByTagName( 'input' );
    var el, i = 0, bWhich = oCheckbox.checked;
    while (el = boxes[i++]) {
	if (el.type == 'checkbox' && el.name.indexOf(namestart) == 0) 
	    el.checked = bWhich;
	//alert(el.name);
    }
}


function errorInInputCharacters(input, allowedchars) {
    // check that a string only consists of characters found in "input"

    for (var i = 0; i < input.length; i++) {
	var chr = input.charAt(i);
	var found = false;
	//alert("testing " + chr);
	for (var j = 0; j < allowedchars.length; j++) {
	    var checkchr = allowedchars.charAt(j);
	    //alert("testing " + chr + " / " + checkchr);
	    if (chr == checkchr) found = true;
	}
	if (!found) {
	    errorText="Illegales Zeichen " + chr;
	    return true;
	}
    }
     return false;
}

function errorInMandatory(elem, mandatory) {
    if (elem.value == '' && mandatory) {
	errorText="Feld ist leer"; 
	if(! errorFound) {
	    elem.focus();
	    elem.select();
	}
	return true;
    }
    return false;
}


function errorInText(elem, mandatory) {
    //alert ("errorInText " + elem.name + ", " + mandatory);
    return (errorInMandatory(elem, mandatory))
}

function errorInDate(elem, mandatory) {

    if(errorInMandatory(elem, mandatory))
	return true;

    var subtype= elem.name.substring(elem.name.indexOf("[") +1, 
				     elem.name.indexOf("]"));
    //alert("subtype " + subtype + ": " + elem.value)

    if (elem.value == "")
	return false;
    num = (elem.value *1);  //make sure it is a number
    switch (subtype)   {
    case 'year' : 
	if (100 > num) {
	    // user specified 2-digit date, add "1900"
	    num += 1900;
	    elem.value = num;
	}	    

	if (1900 > num || num > 2005) {
	    errorText="Ungültiges Jahr '" + elem.value + "' "; 
	    return true;
	}
	break;

    case 'month' : 
	if (1 > num || num  > 12) {
	    errorText="Ungültiger Monat '" + elem.value + "' "; 
	    return true;
	}
	break;

    case 'day' : 
	if (1 > num || num  > 31) {
	    errorText="Ungültiger tag '" + elem.value + "' "; 
	    return true;
	}
	break;

    default: 
	alert ("Unknown field format " + subtype + " on " +
	       elem.name);
	return true;
    }
    
    return false;
}

function errorInEmail(elem, mandatory) {

    if(errorInMandatory(elem, mandatory))
	return true;

    if (elem.value == '') 
	return false;

    if ((elem.value.search(/@/) == -1) 
	|| (elem.value.search(/@[a-zA-Z].*\.[a-zA-Z]+$/) == -1)
	) {
	errorText="Ungültige Email-Adresse '" + elem.value + "' "; 
	
	if(! errorFound) {
	    elem.focus();
	    elem.select();
	}
	//alert ("error In Email" + elem.name + ", " + "Value " + elem.value );
	return true;
    }
    return false;
}

function errorInTelNo(elem, mandatory) {
    //alert ("errorInText" + elem.name + ", " + mandatory);
    if(errorInMandatory(elem, mandatory))
	return true;
    return (errorInInputCharacters(elem.value, '0123456789-/+'));
}

var errorFound=false;	
var errorText;

function checkMemberForm (theForm) {

    errorFound=false;	

    var errorMessage ="";
    var firstError;


    //var checklist is to be supplied by the PHP script
    checklist["password2"] = ["", "password", "0", "", ""];

    var z = 0;
    var elem;
    var name, type, mandatory;

    while (theForm.elements[z].type == 'text' ||
	   theForm.elements[z].type == 'password' ) {

	if(theForm.elements[z].name == "username" && !check_username(elem)) {
	    errorFound=true;
	    errorMessage +=  'Leerzeichen sind im Benutzername nicht erlaubt';
	}


	elem=theForm.elements[z];
	name=elem.name;
	//alert("checking " + name );
	if(name.indexOf("[") >= 0) {
	    // remove subfields for date
	    name = name.substr(0, name.indexOf("["));
	}
	type=checklist[name][1];
	//alert("checking " + name + " type " + type);


	mandatory=(checklist[name][2] == "1");
	errorText="";
	    
	switch (type) 
	    {
	    case 'password' : {
		if (elem.value != theForm.password2.value){
		    errorFound=true;
		    errorMessage +=  'Passwörter stimmen nicht überein\n';
		}
		break;
	    }
	    case 'text': {
		if (errorInText(elem, mandatory)) {
		    errorFound=true;
		    errorMessage +=  'Fehler in ' + checklist[name][0] + 
			": " + errorText + '\n';
		}
		break;

	    }
	    case 'email': {
		if (errorInEmail(elem, mandatory)) {
		    errorFound=true;
		    errorMessage +=  'Fehler in ' + checklist[name][0] + 
			": " + errorText + '\n';
		}
		break;

	    }
	    case 'date': {
		if (//elem.name.indexOf("year") > 0 &&
		    errorInDate(elem, mandatory)) {
		    errorFound=true;
		    errorMessage +=  'Fehler in ' + checklist[name][0] + 
			": " + errorText + '\n';
		}
		break;

	    }
	    case 'telno': {
		if (errorInTelNo(elem, mandatory)) {
		    errorFound=true;
		    errorMessage +=  'Fehler in ' + checklist[name][0] +
			": " + errorText + '\n';
		}
		break;

	    }
	    default: {
		alert ("Unknown field format " + type + " on " +
		       name);
		return false;
	    }
	    } //switch
	//alert("soweit: " + errorMessage);
	z++; 
    } //while


    if (errorFound || errorMessage!= "") {
	alert(errorMessage);
	return false;
    }
    return true;

}


function autoValidate(elem )
{
    //alert("autovalidate");

    // fill the username field from first and lastname
    if(((elem.name == "firstname") || (elem.name == "lastname")) 
       && (elem.form.username.value == "")
	) {
	var form=elem.form;
	var first_name = form.firstname.value.toLowerCase();
	var last_name = form.lastname.value.toLowerCase();
    
	if( last_name == '' ) {
	    return false;
	}
    
	var user_name = first_name + last_name.substr( 0,1 );
	form.username.value = user_name.replace(/ /g ,"");
    }
    

    if(elem.name == "username" && !check_username(elem)) {
	alert('Leerzeichen sind im Benutzername nicht erlaubt');
	//this.document.edit_member.username.focus(); // GEHT NICHT!!
	//elem.form.username.focus();
	return false;
    }
    return true;
}

function check_username(elem) {
    //check that username does not contain blanks
    var username=elem.form.username;
    if(username.value.indexOf(" ") > 0) {
	return false;
    }
    return true;
}


function really(mesg)
{
  // show alert before starting an action      
    if (confirm(mesg)) {
	//alert("1");
	return true;
    } else {
	//alert("0");
	return false;
    }
}



//end
