var Event = {
	add: function(obj,type,fn) {
		if (obj.attachEvent) {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function() { obj['e'+type+fn](window.event); }
			obj.attachEvent('on'+type,obj[type+fn]);
		} else
		obj.addEventListener(type,fn,false);
	},
	remove: function(obj,type,fn) {
		if (obj.detachEvent) {
			obj.detachEvent('on'+type,obj[type+fn]);
			obj[type+fn] = null;
		} else
		obj.removeEventListener(type,fn,false);
	}
}
function toggle(what){
   var it=document.getElementById(what);
   var moreit=document.getElementById(what+'_more');
   if(it){
     if(it.style.display=="none")
        newstyle="";
     else
        newstyle="none";
     if(moreit)
       moreit.style.display=it.style.display;
     it.style.display=newstyle;
   }
   window.location="#a_"+what;
}
function stoggle(what){
  var that=document.getElementById(what);
  var thattext=document.getElementById('toggle_'+what);
  if(that.style.display=="none")
    thattext.innerHTML="Mehr Details";
  else
    thattext.innerHTML="Weniger Details";
}
function fillts(what,whatelse,page){
	ftoset=document.getElementById(whatelse);
	htmltoset=document.getElementById(what);
	catchar="?";
	if(page.indexOf('?')!=-1)//auch nicht schoen
		catchar="&";
	window.open("/"+page+catchar+"id="+document.getElementById(whatelse).value+"&userid={userid}","cal","width=200,height=230");
}
function opencal(what,page){
	ftoset=document.getElementById(what);
	ftype="select";
	catchar="?";
	if(page.indexOf('?')!=-1)//auch nicht schoen
		catchar="&";
	window.open("/"+page+catchar+"id="+document.getElementById(what)[document.getElementById(what).selectedIndex].value,"cal","width=170,height=290");
}
function specialopen(link,name,params){
    var partparams=params.split(",");
    var width="300px";
    var height="400px";
    var left="150px";
    var top="150px";
    for(var i=0;i<partparams.length;i++){
        var check=partparams[i].split("=");
        if(check[0]=="width")
            width=check[1]+"px";
        else if(check[0]=="height")
            height=check[1]+"px";
        else if(check[0]=="left")
            left=check[1]+"px";
        else if(check[0]=="top")
            top=check[1]+"px";
    }
   // alert(width+" "+height);
    eframe=document.getElementById('editframe');
    if(!eframe)
        return window.open(link,name,params);
    eframe.style.width=width;
    eframe.style.height=height;
    eframe.style.position="absolute";
    eframe.style.left=left;
    eframe.style.top=top;
    eframe.contentWindow.location.replace(link);
    eframe.style.display="";
    return false;
   //
}
function specialclose(){
    eframe=document.getElementById('editframe');
    eframe.contentWindow.location.replace("about:blank");
    eframe.style.display="none";
}

function pageadvance(theget,maxp,actp,wantp){
    rwantp=parseInt(wantp);
    if(isNaN(rwantp)){
        alert('Please enter a valid pagenumber!');
        return false;
    }
    else if(wantp>maxp || wantp<0){
        alert("Seite "+(wantp+1)+" existiert nicht!");
        return false;
    }
    else if(wantp==actp)
        return false;
    else filterme(theget);
}
function filterme(theget,doit){
	if(filterme.arguments.length!=2 && (ff=document.getElementById('filterform'))){
		getarr=theget.split("&");
		for(i=0;i<getarr.length;i++){
			gv=getarr[i].split("=");
			//sollte man noch gucken, ob das feld schon existiert
			newinput=document.createElement("input");
			newinput.setAttribute("type","hidden");
			newinput.setAttribute("name",gv[0]);
			newinput.setAttribute("id",gv[0]);
			newinput.setAttribute("value",gv[1]);
			ff.appendChild(newinput);
		}
		ff.submit();
	}
	else
		window.location.replace("?"+theget);
}
function validate(that){
	errortext="";
	var multis=new Object();
	etext=new Array("darf nicht leer sein!","","darf nur ganze Zahlen enthalten!","muss eine fuenfstellige Postleitzahl enthalten!","muss eine gültige E-Mail Adresse enthalten!","must be a date formatted: tt.mm.yyyy!","darf nur ganze Zahlen enthalten!","enthaelt keine handynummer!","muss ausgewählt sein","is not a valid IP address","is a special IP address, that cannot be used here");
	for(var i=0;i<that.length;++i){
    //if(that.elements[i].id.substr(0,2)=="x_"){
	 	//	must=that.elements[i].id.substr(2,1);
		//	checktype=that.elements[i].id.substr(3,1);
    thevalidate=that.elements[i].getAttribute('validate');
		if(thevalidate!=null && thevalidate.length>1){
	 		must=thevalidate.substr(0,1);
			checktype=thevalidate.substr(1);
			b=checkVal(must,checktype,that.elements[i]);
		//	alert(thevalidate+" "+must+" "+b+" "+that.elements[i].name);
			if(checktype=="mc"){
        if(typeof(multis[that.elements[i].name])=="undefined")
           multis[that.elements[i].name]=b;
        if(b==1 || multis[that.elements[i].name]==1){
            multis[that.elements[i].name]=1;
        }
        b=1;
      }
			if(b!=1){
				if(!isNaN(b)){
          var jstitle=that.elements[i].getAttribute('jstitle');
          if(jstitle!=null)
            errortext+="Das Eingabefeld "+jstitle+" "+etext[b];
					else if(that.elements[i].title)
						errortext+="Das Eingabefeld "+that.elements[i].title+" "+etext[b];
					else
						errortext+="Das Eingabefeld "+that.elements[i].name+" "+etext[b];
					if(must=="c")
						errortext+=" (oder leer lassen)";
					errortext+="\n"
				}
				else that.elements[i].value=b;
			}
		}
        if(that.elements[i].type=="file"){
            if(that.elements[i].accept!="" && that.elements[i].value!=""){
                ext=that.elements[i].value.substring(that.elements[i].value.length-3,that.elements[i].value.length).toLowerCase();
                if(ext!="jpg" && ext!="jpeg" && ext!="gif" && ext!="png")
                    errortext+=that.elements[i].name+" must contain GIF, JPEG or PNG data \n";
            }
        }

	}
	for (var mas in multis){
          themulti=document.getElementsByName(mas)[0];
          if(multis[mas]!=1){
            if(themulti.title)
						errortext+="Das Eingabefeld "+themulti.title+" "+etext[8];
					else
						errortext+="Das Eingabefeld "+themulti.name+" "+etext[8];
					errortext+="\n"
          }

        }
	if(errortext!=""){
 		alert(errortext);
		return false;
	}
	else
		return true;
}
function checkVal(m,c,fval){
	val=fval.value;
	//alert(val+" "+m+" "+c);
	if(m=="m" && val=="")
		return 0;
	else if(m=="c" && val=="")
		return 1;
	else{
		if(c=="c"){
      if(!fval.checked)
				return 8;
		}
		if(c=="mc"){
      if(!fval.checked)
				return 8;
		}
		if(c=="t")
	 		return 1;//oder laenge?
		if(c=="i" || c=="p"){
      if(isNaN(val) || parseInt(val)!=val)
				return 6;
			else if(c=="p" && val.length!=5)
				return 3;
		}
		else if(c=="i0"){
       if(val==0 || isNaN(val) || parseInt(val)!=val)
				return 0;
    }
		if(c=="e"){
			var i = 1;
  			var sEmail = val;
  			var sLength = sEmail.length;
   			while ((i < sLength) && (sEmail.charAt(i) != "@"))
    			i++;
  			if ((i >= sLength) || (sEmail.charAt(i) != "@"))
    			return 4;
  			else
    			i += 2;
  			while ((i < sLength) && (sEmail.charAt(i) != "."))
    			i++;
  			if ((i >= sLength - 2) || (sEmail.charAt(i) != "."))
    			return 4;
  			else
    			return 1;
		}
		if(c=="d"){
			val=val.replace(/[.:-]/g," ");
			dparts=val.split(" ");
			if(dparts.length!=3)
				return 5;
			for(y=0;y<dparts.length;y++){
				if(isNaN(dparts[y]))
					return 5;
				else{
					if(y==0){
						if(dparts[y]>31)
							return 5;
						else if(dparts[y].length<2)
							dparts[y]="0"+dparts[y];
					}
					else if(y==1){
						if(dparts[y]>12)
							return 5;
						else if(dparts[y].length<2)
							dparts[y]="0"+dparts[y];
					}
					else if(y==2){
						if(dparts[y].length<4)
							dparts[y]="19"+dparts[y];
						if(dparts[y]>2003)
							return 5;
					}
				}
			}
			return dparts[0]+"."+dparts[1]+"."+dparts[2];
		}
		if(c=="u")
			fval.value=val.toUpperCase();
		if(c=="f"){
			val=val.replace(/,/,".");
			if(isNaN(val))
				return 2;
			else
				fval.value=val;
		}
        if(c=="a"){//IP
			var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
            var ipArray = val.match(ipPattern);
            if (ipArray == null)
                return 9;
            else {
                for (i = 1; i < 5; i++) {
                    if (ipArray[i] > 255)
                        return 9;
                }
            }
            if (val == "0.0.0.0" || val == "255.255.255.255")
                return 10;
		}
		if(c=="m"){//handynummer
			val=val.replace(/\ /g,"");
			regex=/^01[5,6,7][0-9]{1,2}[- \/]?[1-9][0-9]{6,9}$/;//deutsche handynummern
			regex2=/^\+491[5,6,7][0-9]{1,2}[- \/]?[1-9][0-9]{6,9}$/;//deutsche mit internationaler vorwahl
			regex3=/^\+/;//internationale, kein weiterer check
			regex4=/^\+49/;//regex3 und 4 sollte man zusammenfassen koennen
			if(!regex.test(val) && !regex2.test(val) && !(regex3.test(val) && !regex4.test(val)))
				return 7;
			else
				fval.value=val;
		}
        if(c=="s"){
            if(fval.options[fval.selectedIndex].value=="" || fval.options[fval.selectedIndex].value==0){
               // alert(fval.selectedIndex);
                return 8;
            }
        }
        else if(c=="s0"){
            if(fval.options[fval.selectedIndex].value==""){
               // alert(fval.selectedIndex);
                return 8;
            }
        }
        if(c=="tt0"){//timestamp nicht 0

        }
	}
	return 1;
}

