
var isIe = navigator.userAgent.indexOf('MSIE') > -1 ? 1 : 0;
if ( navigator.userAgent.indexOf('Opera') > -1  ) isIe = 0; 

// page edit help box, takes given cat vals and rewrites the input fields with them -------------------------------------
function splitCatOptionToFields (optionval) {
	if ( !optionval ) return false;
	var optionvalparts = optionval.split('_');
	document.forms['seditfo'].elements['hasparent'].value = optionvalparts[0];
	document.forms['seditfo'].elements['pcat'].value = optionvalparts[1];
}

// window open for menue position tree popup ----------------------------------------------------------------------------
function positionTreeWin(aktid) {
	if ( window.positiontreewin && typeof(window.positiontreewin) == 'object' &&  !window.positiontreewin.closed )  {
	 	window.positiontreewin.close();
	}
	window.positiontreewin = window.open('/index.php?cat=Verwaltung&page=Menuetree&aktmenuid=' + aktid , 'positiontreewin');
	window.positiontreewin.focus();
}
// window open for other purposes with popupblockcheck ----------------------------------------------------------------------------
var eeWin = 0;	
function nwin(PFAD,PROPS,ERRTXT) {
 if ( ERRTXT == '' ) ERRTXT = "Bitte deaktivieren Sie Ihren Popup Blocker!";
 if( eeWin && typeof(eeWin) == "object" && !eeWin.closed ) {
	eeWin.close();
 }
 try { 
  eeWin = window.open(PFAD,"eeWin",PROPS);
  if ( !eeWin || typeof(eeWin) != 'object' || eeWin.closed ) {
   throw "notopen"; 
  }
	eeWin.focus();
 }
 catch(e){ 
 	if ( e == "notopen" ) {
		if ( !ERRTXT ) {
			ERRTXT = 'Bitte deaktivieren Sie Ihren Popup Blocker, um diesen Link nutzen zu können!';
		} 
		alert("PopUp Fehler!\n\n"+ERRTXT);
		return false;
	}
 }
 return true;
}
// functions to check the input legth in textareas ----------------------------------------------------------------------
var ivvar = 0;
function startCheckLen(zeichenmax,formindex,zeichenfeld,anzeigefeld)	{
	ivvar = window.setInterval('checkLen('+zeichenmax+',"'+formindex+'","'+zeichenfeld+'","'+anzeigefeld+'")', 50);
}
function checkLen(zeichenmax,formindex,zeichenfeld,anzeigefeld)	{
	minus = document.forms[formindex].elements[zeichenfeld].value!=''? document.forms[formindex].elements[zeichenfeld].value.length : 0;
	remain = zeichenmax - minus;
	document.forms[formindex].elements[anzeigefeld].value = remain > 0 ? remain : 0;
	if ( remain < 0 ) {
	  document.forms[formindex].elements[zeichenfeld].value=document.forms[formindex].elements[zeichenfeld].value.substring(0,zeichenmax);
		alert("Zeichenmenge zu hoch!\n\nText wird automatisch gekürzt.");
	}
}
function stopCheckLen()	{
	if ( typeof(ivvar) == 'number' )	{
		window.clearInterval(ivvar);
	}	
}


/// functions to add tags to textareas by click ------------------------------------------------------------------------
function addTagToBox(foname,boxname,strToAdd) {
	document.forms[foname].elements[boxname].focus();
	if ( strToAdd.indexOf('°°°') > -1 ) {
		if ( isIe && document.all  ) {
			var sel   = document.selection.createRange();
			if ( sel.text.length > 0 ) {
				repadd = strToAdd.replace('°°°',sel.text);
			} else {
				var text = prompt("Text innerhalb dieses Tags:","");
				repadd = strToAdd.replace('°°°', !text? '...' : text);
			}
			sel.text  = repadd;
		} 
		else {
			var text = prompt("Text innerhalb dieses Tags:","");
			repadd    = strToAdd.replace('°°°', !text? '' : text);
			document.forms[foname].elements[boxname].value += repadd;
		}
	}
	else {		
		document.forms[foname].elements[boxname].value += strToAdd;
	}
	document.forms[foname].elements[boxname].focus();
}

// functions for user message sending ------------------------------------------------------------------------------------
function rcpadd(theval) {
	if ( theval != '') {
		document.forms['msgfo'].elements['nrecipient'].value = theval;
	}
	document.forms['msgfo'].elements['nrecipient'].focus();
	return true;
}
function msgfocheck() {
	var haserrors = 0;
	var msg = "Leider sind Eingabefehler aufgetreten:\n\n";
	if ( document.forms['msgfo'].elements['nrecipient'].value.length < 4 ) {
		haserrors++;
		msg += "- Bitte Empfänger angeben.\n";
	} else if ( !rcpcheck(document.forms['msgfo'].elements['nrecipient'].value) ) {
		haserrors++;
		msg += "- Der Empfänger ist nicht bekannt oder wünscht keine Nachrichten.\n";
	}
	if ( document.forms['msgfo'].elements['nsubject'].value.length < 3 ) {
		haserrors++;
		msg += "- Bitte einen Betreff mit mindestens drei Zeichen eingeben.\n";	
	}	
	if ( document.forms['msgfo'].elements['nsubject'].value.length > 128 ) {
		haserrors++;
		msg += "- Bitte einen Betreff mit höchstens 64 Zeichen eingeben.\n";
		document.forms['msgfo'].elements['nsubject'].value = document.forms['msgfo'].elements['nsubject'].value.substr(0,128);	
	}		
	if ( document.forms['msgfo'].elements['nbody'].value.length < 3 ) {
		haserrors++;
		msg += "- Bitte einen Nachrichtentext eingeben.\n";	
	}	
	if ( document.forms['msgfo'].elements['nbody'].value.length > 2048 ) {
		haserrors++;
		msg += "- Bitte einen Nachrichtentext mit höchstens 2048 Zeichen eingeben.\n";
		document.forms['msgfo'].elements['nbody'].value = document.forms['msgfo'].elements['nbody'].value.substr(0,2048);	
	}
	if ( haserrors == 0 ) {
		return true;
	}
	alert(msg);
	return false;
}
function rcpcheck(theval) {
	if ( !document.forms['msgfo'].elements['forcplist'] ) {
		return true;
	}
	var theselbox = document.forms['msgfo'].elements['forcplist'];
	for ( i=1; i < theselbox.options.length; i++) {
		if ( theval == theselbox.options[i].value ) {
			//alert("val "+i+": "+theselbox.options[i].value);
			return true;
		}
	}
	return false;
}
function recalcprices(thefo,theval) {
	if ( !theval.match(/^\d+$/) ) {
		alert('Geben Sie bitte eine Zahl ein.');
		thefo.elements['amount'].focus();
		return false;
	}
	if ( Number(theval) > thefo.elements['maxavailable'].value ) {
		alert('Leider maximal nur '+thefo.elements['maxavailable'].value+' Stück erhältlich.');
		theval = thefo.elements['maxavailable'].value;		
		thefo.elements['amount'].value = theval;
	}
	xp1 = theval * thefo.elements['pprice'].value;
	xp1 = xp1.toFixed(2);
	xp1 = xp1.replace(/\./, ',');
	document.getElementById('prodprice').innerHTML = xp1;
	xp2 = theval * thefo.elements['pdeliverprice'].value;
	xp2 = xp2.toFixed(2);
	xp2 = xp2.replace(/\./, ',');
	document.getElementById('deliverprice').innerHTML = xp2;
	xp3 = theval * thefo.elements['pprice'].value + theval * thefo.elements['pdeliverprice'].value;
	xp3 = xp3.toFixed(2);
	xp3 = xp3.replace(/\./, ',');
	document.getElementById('sumprice').innerHTML = xp3;
	return false;
}

function checkordersend(thefo) {
	var errmsg = '';
	if ( !thefo.elements['amount'].value.match(/^\d+$/) || thefo.elements['amount'].value < 1 ) {
		errmsg += "Um eine Bestellung ausführen zu können sollte im Feld Menge mindestens 1 stehen.\n";
		thefo.elements['amount'].focus();
	}
	if ( !thefo.elements['agb'].value.match(/^ja$/i) ) {
		errmsg += "Bitte bestätigen Sie die Anerkennung der AGB, in dem Sie in das Feld \"Ja\" eingeben.\n";
		thefo.elements['agb'].focus();
	}
	if ( errmsg != '' ) {
		alert(errmsg);
		return false;	
	}
	return true;
}

// functions for calculator Window
var pcalcwindow = false;
function pcalcwin(sprice) {
	var winleft = (screen && screen.width)? screen.width : 800;
	var wintop = (screen && screen.height)? screen.height : 600;
	winleft = Math.floor ( (winleft - 260) / 2 );
	wintop = Math.floor ( (wintop - 400) / 2 );
  if( pcalcwindow && typeof(pcalcwindow) == "object" && !pcalcwindow.closed ) {
	pcalcwindow.close();
 }
 try { 
  pcalcwindow = window.open("","pcalcwindow","left=" + winleft + ",top=" + wintop + ",width=260,height=420,scrollbars=0");
  if ( !pcalcwindow || typeof(pcalcwindow) != 'object' || pcalcwindow.closed ) {
   throw "notopen"; 
  }
	pcalcwindow.focus();
	with(pcalcwindow.document) {
		open();
		writeln('<html>');
		writeln('<head>');
		writeln('	<title>Preis Kalkulator</title>');
		writeln('	<link href="/global.css" rel="stylesheet" type="text/css" />');
		writeln('</head>');
		writeln('<body>');
		writeln('<div style="border:4px double navy;padding:4px;font-size:13px;">');
		writeln('<b>Preis Kalkulator</b>');
		writeln('<p>');
		writeln('Berechnen Sie mit dem Preiskalkulator die Geb&uuml;hren f&uuml;r Ihre Verk&auml;ufe bei autofriedhof.at, denn Preistranzparenz wird bei uns GROSS geschrieben! Einfach über das Nummernfeld Ihren Verkaufspreis eingeben und auf <i>Preis anzeigen</i> klicken! Mit dem <i>C</i> setzen Sie das Formular zurück, wenn Sie weitere Preise berechnen möchten.');
		writeln('</p>');
		writeln('<form action="preis.calc.php" method="post" onsubmit="return false;" style="margin-left:37px;">');
		writeln('<input type="button" value="1" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 1;" />');
		writeln('<input type="button" value="2" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 2;" />');
		writeln('<input type="button" value="3" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 3;" />');
		writeln(' Verkaufspreis');
		writeln('<br />');
		writeln('<input type="button" value="4" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 4;" />');
		writeln('<input type="button" value="5" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 5;" />');
		writeln('<input type="button" value="6" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 6;" />');
		writeln('<input type="text" name="eingabepreis" value="" style="width:80px;text-align:right;color:#000000;" onfocus="this.value=\'\'" disabled="disabled" /> <b>EUR</b>');
		writeln('<br />');
		writeln('<input type="button" value="7" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 7;" />');
		writeln('<input type="button" value="8" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 8;" />');
		writeln('<input type="button" value="9" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 9;" />');
		writeln('Gebühr');
		writeln('<br />');
		writeln('<input type="button" value="0" style="width:18px;margin-top:4px;" onclick="this.form.elements[\'eingabepreis\'].value += 0;" />');
		writeln('<input type="button" value="." style="width:18px;margin-top:4px;" onclick="if ( this.form.elements[\'eingabepreis\'].value == \'\' ) this.form.elements[\'eingabepreis\'].value += \'0.\'; else this.form.elements[\'eingabepreis\'].value += \'.\';" />');
		writeln('<input type="reset" value="C" style="width:18px;margin-top:4px;" />');
		writeln('<input type="text" name="afpreis" value="" style="width:80px;text-align:right;color:#000000;" onfocus="this.value=\'\'" disabled="disabled" /> <b>EUR</b>');
		writeln('<br />');
		writeln('<input type="submit" value="Preis anzeigen" style="width:150px;margin-top:4px;" onclick=" if ( this.form.elements[\'eingabepreis\'].value > 1000 ) this.form.elements[\'afpreis\'].value = Math.round( ( ( this.form.elements[\'eingabepreis\'].value - 1000 ) * 0.02 + 35) * 100 ) / 100 ; else if ( this.form.elements[\'eingabepreis\'].value > 500 ) this.form.elements[\'afpreis\'].value = Math.round( ( (this.form.elements[\'eingabepreis\'].value - 500 ) * 0.03 + 20) * 100 ) / 100 ; else this.form.elements[\'afpreis\'].value = Math.round( ( this.form.elements[\'eingabepreis\'].value * 0.04) * 100 ) / 100 ; if (this.form.elements[\'afpreis\'].value.indexOf(\'.\') < 0 ) this.form.elements[\'afpreis\'].value += \'.00\'; else if (this.form.elements[\'afpreis\'].value.match(/\\.\\d$/) ) this.form.elements[\'afpreis\'].value += \'0\'; " />');
		writeln('<br />');
		writeln('<input type="button" value="Rechner schliessen" style="width:150px;margin-top:4px;" onclick="self.close()" />');
		writeln('</form>');
		writeln('</div>');
		writeln('<sc'+'ript>document.forms[0].elements[\'eingabepreis\'].value = "' + sprice.replace(/,/, '.') + '";</sc'+'ript>');
		writeln('</body>');
		writeln('</html>');
		close();
	}
 }
 catch(e){ 
 	if ( e == "notopen" ) {
		alert("PopUp Fehler!\n\nBitte deaktivieren Sie Ihren Popup Blocker, um diesen Link nutzen zu können!");
		return false;
	}
 }
}