// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Projekt individuelle JS-Funktionen kommen in die m_project.js //
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var NAV6 = (parseInt(navigator.appVersion) >= 5 && navigator.appName == "Netscape") ? 1 : 0;
var NAV4 = (navigator.appName.indexOf("Netscape") >= 0 &&  parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
var IE5 = (IE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;
var OP = navigator.appName.indexOf("Opera") ? 1 : 0;

// ####################################################################################################
// 
// 	Formular-Checker
//
//	var felder=new Array();
//	var formname='form'; 	// Name des Formulares
//	var werte=new Array();
//
//	werte['feld']='email';	// Name des Formularfeldes
//	werte['pruef']='email';	// Prüfen auf (email,datum,plz,custom oder leer lassen)
//	werte['typ']='text';	// Feldtype
//	werte['pflicht']=1;		// Pflichtfeld
//	werte['fehler']='Bitte geben Sie eine gültige E-Mail Adresse an!'; // Fehlermeldung bei ungültiger E-Mail Adresse!
//	felder[felder.length]=werte;
//
// Prüftyp: Ein Feld mit Prüftyp wird zusätzlich genauer überprüft. Möglich ist hier email, datum oder plz.
// 			Eien Prüfung findet statt, wenn das Feld ein Pflichtfeld ist oder der Benutzer das Feld ausfüllt.
//
// Datum: Ein Datum wird auf das Format TT.MM.YYYY geprüft.
// PLZ: Eine PLZ wird auf 5-stellen und auf vorkommen von Zeichen ausser Zahlen geprüft.
// E-Mail: Eine E-Mail wird auf name, domain und toplevel geprüft. Es dürfen keine 2 @ vorkommen.
//		   Die Domain muss aus mind. 2 Zeichen bestehen. Der Name darf die Zeichen a-z, A-Z, 0-9, ., - und _ enthalten.
//		   Die Domain darf die Zeichen a-z, A-Z, 0-9, . und - enthalten.
// Custom: Eine Javascript Funktion mit den Parametern element-Objekt, Feld-Parameter (Ein trag aus dem Konfigurationsarray für dieses Feld) wird aufgerufen und der Rückgabewert auf Wahrheit überprüft
//
// by Thorsten Peters, t.peters@rosomm-partner.de
//
// ####################################################################################################

function mCheckAll() {
	for (feld=0;feld<felder.length;feld++) {
		var fld=felder[feld];
		var wert;
		var element=document.forms[formname][felder[feld]['feld']];
		if (fld['pflicht']) {
			if (fld['typ']=='text') wert=element.value;
			if (fld['typ']=='checkbox') wert=element.checked;
			if (fld['typ']=='radio') {
				wert=false;
				for (i=0; i<element.length;i++) if (element[i].checked) {
					wert=true;
				}
			}
			if (fld['typ']=='dropdown') wert=element[element.selectedIndex].value;
			if (!wert) {
				alert(fld['fehler']);
				if (fld['typ']!='radio')element.focus();
				return false;
			}
		}
		if (document.forms[formname][felder[feld]['feld']] && document.forms[formname][felder[feld]['feld']].value!='') {
			if (fld['pruef']=='email') if (!mCheckEmail(element,fld['fehler'])) return false;
			if (fld['pruef']=='datum') if (!mCheckDatum(element)) {
				alert(fld['fehler']);
				element.focus();
				return false;
			}
			if (fld['pruef']=='plz') if (!mCheckPLZ(element,fld['fehler'])) return false;
			if (fld['pruef']=='length') if (!mCheckLength(element,fld['fehler2'],fld['maxlength'])) return false;
			if (fld['pruef']=='password') if (!mCheckPassword(element,fld['fehler2'],document.forms[formname][fld['field2']])) return false;
			if (fld['pruef']=='custom') {
				eval('var result='+fld['function']+'(element,fld)');
				if (!result) return false;
			}
		}
	}
	return true;
}

function mCheckEmail(email,error) {
	check=email.value.indexOf("@");
	if (check==-1) {
		alert(error); email.focus(); return false;
	} else {
		ename=email.value.substring(0,check);
		rest=email.value.substring(check+1,email.value.length);
		for (i=0; i<ename.length; i++) {
			ok=false;
			if (ename.charAt(i)>='a' && ename.charAt(i)<='z') ok=true;
			if (ename.charAt(i)>='A' && ename.charAt(i)<='Z') ok=true;
			if (ename.charAt(i)>='0' && ename.charAt(i)<='9') ok=true;
			if (ename.charAt(i)=='.' || ename.charAt(i)=='-' || ename.charAt(i)=='_') ok=true;
			if (!ok) { alert(error); email.focus(); return false; }
		}
		if (rest.indexOf("@")!=-1) {
			alert(error); email.focus(); return false;
		} else {
			if (!ename) {
				alert(error); email.focus(); return false;
			} else {
				check=rest.lastIndexOf(".");
				domain=rest.substring(0,check);
				land=rest.substring(check+1,rest.length);
				for (i=0; i<domain.length; i++) {
					ok=false;
					if (domain.charAt(i)>='a' && domain.charAt(i)<='z') ok=true;
					if (domain.charAt(i)>='A' && domain.charAt(i)<='Z') ok=true;
					if (domain.charAt(i)>='0' && domain.charAt(i)<='9') ok=true;
					if (domain.charAt(i)=='.' || domain.charAt(i)=='-') ok=true;
					if (!ok) { alert(error); email.focus(); return false; }
				}
				for (i=0; i<land.length; i++) {
					ok=false;
					if (land.charAt(i)>='a' && land.charAt(i)<='z') ok=true;
					if (land.charAt(i)>='A' && land.charAt(i)<='Z') ok=true;
					if (!ok) { alert(error); email.focus(); return false; }
				}
				if (land.indexOf(".")!=-1) {
					alert(error); email.focus(); return false;
				} else {
					if (!land || !domain) {
						alert(error); email.focus(); return false;
					} else {
						if (domain.length<2) {
							alert(error); email.focus(); return false;
						} else return true;
					}
				}
			}
		}	
	}
}

function mCheckPLZ(plz,error) {
	if (plz.value.length!=5) {
		alert(error);
		plz.focus();
		return false;
	}
	for (i=0; i<plz.value.length; i++) {
		s=plz.value.substring(i,i+1);
		check=parseInt(s);
		if (isNaN(check)) {
			alert(error);
			plz.focus();
			return false;
		}
	}
	return true;
}

function mCheckPassword(password,error,password2) {
	if(password.value!=password2.value) {
		alert(error);
		password.value = '';
		password2.value = '';
		password.focus();
		return false;
	}
	return true;
}

function mCheckDatum(datefield,error) {
	var mNames="JanFebMarAprMayJunJulAugSepOctNovDec"
	var mValues="312831303130313130313031"
	var errormsg=error;
	var date=datefield.value;
	var dots=new Array();
	
	if (date.length<6) {
		return false;
	}
	for (var i=0; i < date.length;i++) if (date.substr(i,1)=='.') dots[dots.length]=i;
	if (dots.length!=2) {
		return false;
	}
	myDD=parseInt(date.substr(0,dots[0]),10);
	myMM=parseInt(date.substr(dots[0]+1,dots[1]-dots[0]-1),10);
	myYYYY=parseInt(date.substr(dots[1]+1,date.length),10);
	if (myYYYY < 1850) return false;
	if ((isNaN(myDD)) || (isNaN(myMM)) || ( isNaN(myYYYY))) {
		return false;		
	}
	var lastdatum = 0
	if (myMM == 2) {
		if (isLeapYear(myYYYY)) lastdatum = 29;
		else lastdatum = 28;
	} else lastdatum = mValues.substr((myMM-1)*2, (myMM-1)*2+2);
	
	if ((myDD > lastdatum) || (myDD <=0)) {
		return false;
	}
	var newValue="";
	if (myDD<10) myDD = "0"+myDD;
	if (myMM<10) myMM = "0"+myMM;
	if (myYYYY<10) myYYYY = "0"+myYYYY;
	if (myYYYY<1000) myYYYY = "20"+myYYYY;
	datefield.value=myDD+'.'+myMM+'.'+myYYYY;
	return true;
}

function isLeapYear (Year) { 
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) return true;
	else return false;
}

function mCheckLength(field, error, maxlength) {
	if(field.value.length>maxlength) {
		alert(error);
		return false;
	} else return true;
}

// ####################################################################################################

// Popup 
function openWin(url,weite,hoehe,id,parameters) {
	if (!parameters) parameters='menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,left=50,top=50';
	parameters+=',width='+weite+',height='+hoehe;
	popupWin = window.open(url,id,parameters);
}

function changeLang(value,loc) {
	pos1=loc.indexOf("&ID");
	pos2=loc.indexOf("?ID");
	if (pos2>-1) {
		pos=pos2;
		z="?";
	}
	if (pos1>-1) {
		pos=pos1;
		z="&";
	}
	if (pos>-1) {
		teil=loc.substring(pos,loc.length);
		pos3=teil.indexOf("&")
		if (pos3==-1) pos3=loc.length;
		teil2=loc.substring(pos,pos+pos3);
		ok=str_replace(teil2,z+"ID="+value,loc);
		if (ok) location.href=ok;
	}
}

// Abfrage der Fensterhoehe/-breite fuer IE und NN 
function getHeightAndWidth() {
	if (IE4 || IE5) {
		var screenHeight = document.body.clientHeight;
		var screenWidth = document.body.clientWidth;
	}
	
	if (NAV4) {
		var screenHeight = window.innerHeight;
		var screenWidth = window.innerWidth;
	}
}

// Seite / Layout Config im dms
function confWin(ID,eleID,onoff) {
	toggleFormColor(ID,onoff);
	if (parent.frames.win && parent.frames.win.document.forms.form) toggleFormColor(parent.frames.win.document.forms.form["field["+eleID+"]"]);
	if (window.opener.layout) {
		if (window.opener.layout.document.getElementById(eleID)) toggleFormColor(window.opener.layout.document.getElementById(eleID));
	}
}

function conf(ID,eleID,onoff) {
	toggleFormColor(ID,onoff);
	if (top.window.cmsWin && !top.window.cmsWin.closed && top.window.cmsWin.document.forms.form) 
		if (top.window.cmsWin.document.forms.form.copyLang) toggleFormColor(top.window.cmsWin.document.forms.form["field["+eleID+"]"]);
}

function toggleFormColor(ID,onoff) {
	if (!onoff) {
		if (ID.style.backgroundColor == '') onoff=2;
		else onoff=1;
	}
	
	if (onoff==2) ID.style.backgroundColor='#FFCE7A';
	else ID.style.backgroundColor='';
}

function openWindowScroll(url,weite,hoehe,name,ret) {
	if (!name) name='unitsPopup';
	popupWin = window.open(url,name,'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,width='+weite+',height='+hoehe+',left=50,top=50');
	if (ret) return popupWin;
}

function openWindow(url,weite,hoehe) {
	popupWin = window.open(url,'unitsPopup','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,width='+weite+',height='+hoehe+',left=50,top=50')
}

function openFreeWindow(url,name,mbar,tbar,res,scr,width,height,left,top) {
	if (!mbar) mbar="no"; else mbar="yes";
	if (!tbar) tbar="no"; else tbar="yes";
	if (!res) res="no"; else res="yes";
	if (!scr) scr="no"; else scr="yes";
	popupWin = window.open(url,name,'menubar='+mbar+',toolbar='+tbar+',resizable='+res+',scrollbars='+scr+',width='+width+',height='+height+',left='+left+',top='+top);
}

// Flip - Funktion fuer Bilder im Layer
// flLayer = Layername in dem sich das Bild befindet
// flName = Imagename
// flRoll = Rolloverimage

function fliplay(flLayer, flName, flRoll){
	sLN = '\'' + eval("flLayer") + '\'';
	if(document.images) {
		if(document.layers) document.layers[eval(sLN)].document.images[eval("flName")].src = eval(flRoll + '.src');
		else document.images[eval("flName")].src = eval(flRoll + '.src');
	}
} 

// Flip - Funktion (Austausch eines Bildes)

function flip(imgName,imgNo) { 
	document.images[imgName].src = eval(imgNo+'.src');
}

var current=0; // aktives image, wird bei onclick gesetzt
var sel=0; // ausgewähltes image, wird bei onmouseover gesetzt

function allout(){ // blendet sel aus und current ein
	if (sel != 0){
		flipmnav = 'sel' + sel;
		flipimage = 'no' + sel;
		flip(flipmnav,flipimage);
	}
	
	if (current != 0){
		himnav = 'sel' + current;
		hiimage = 'ro' + current;
		flip(himnav,hiimage);
	}
}

function alloutall(){  // blendet sel und current aus
	if (sel != 0){
		flipmnav = 'sel' + sel;
		flipimage = 'no' + sel;
		flip(flipmnav,flipimage);
	}
	
	if (current != 0){
		himnav = 'sel' + current;
		hiimage = 'no' + current;
		flip(himnav,hiimage);
	}
}

function delconfirmrelocate(msg,url) // OK Abbrechen fragen und bei OK weiterleiten
{
        var check;
        check = confirm(msg);
        if (check) 
        {
                window.location=url;
        }
}   

// Abfrage der Formularfelder
function chkmail() {
	if (document.mail.surname.value == "") {
		alert("Bitte Ihren Vornamen eingeben!");
		document.mail.surname.focus();
		return false;
	}
	if (document.mail.name.value == "") {
		alert("Bitte Ihren Namen eingeben!");
		document.mail.name.focus();
		return false;
	}
	if (document.mail.street.value == "") {
		alert("Bitte Ihre Straße eingebe!");
		document.mail.street.focus();
		return false;
    }
	if (document.mail.zipcity.value == "") {
		alert("Bitte geben sie ihre PLZ und den Ort ein!");
		document.mail.zipcity.focus();
		return false;
    }
	if (document.mail.email.value == "") {
		alert("Bitte Ihre eMail-Adresse eingeben!");
    	document.mail.email.focus();
    	return false;
    }	
	if (document.mail.email.value.indexOf('@') == -1) {
		alert("Bitte eine gültige eMail-Adresse eingeben!");
    	document.mail.email.focus();
    	return false;
    }
	if (document.mail.email.value.indexOf('.') == -1) {
		alert("Bitte eine gültige eMail-Adresse eingeben!");
    	document.mail.email.focus();
    	return false;
    }
	clickedButton();
}

function loadHeadline(nr, session) {
	parent.headline.location = '../../ginab/frm_headline.php?hl='+nr+'&dbc='+session;
}

function str_replace(from,to,str) {
	if (str.indexOf(from)==-1) return false;
	else {
		t1=str.substring(0,str.indexOf(from));
		t2=str.substring(str.indexOf(from)+from.length,str.length);
		ok=t1+to+t2;
		return ok;
	}
}

function chklogin()
  {

   if(document.login.usr.value == "")
    {
     alert("Bitte Ihren Benutzernamen eingeben!");
     document.login.usr.focus();
     return false;
    }
	
   if(document.login.pass.value == "")
    {
     alert("Bitte Ihr Passwort eingeben!");
     document.login.pass.focus();
     return false;
    }
}


function debugHandler(msg) {
	if (!top.frames.mnav) var f=top.opener.top.frames.mnav.document.forms.debug.elements[0];
	else var f=top.frames.mnav.document.forms.debug.elements[0];
	if (f && f.name) f.value+=msg+'\n';
}

function ZweiFrames(URL1,F1,URL2,F2) {  
	parent.frames[F1].location.href=URL1;
	parent.frames[F2].location.href=URL2;
}

// Für Vorschau im CMS
function openEditWin(ID,ele,parent,sess) {
	if (!top.window.cmsWin || (top.window.cmsWin && top.window.cmsWin.closed)) top.window.cmsWin=newReturnWindow('cms/elements/element_edit.php?frPrev=1&ID='+ID+'&element='+ele+'&parent='+parent+'&dbc='+sess,'layoutConf',470,600);
	else {
		top.window.cmsWin.location.href='cms/elements/element_edit.php?frPrev=1&ID='+ID+'&element='+ele+'&parent='+parent+'&dbc='+sess;
		top.window.cmsWin.moveTo(40,40);
		top.window.cmsWin.focus();
	}
}

// Für Vorschau im CMS
function newReturnWindow(url,name,width,height) {
	return window.open(url,name,'status=yes,left=20,top=20,width='+width+',height='+height+',scrollbars=yes,resizable=yes,status=yes');
}

//-->	

