var nav=getNav();



var ctr=1;



function item(name, w, h, l, a, t)	// item - Constructor 

	{

	this.name = name;

	this.w = w;

	this.h = h;

	this.l = l;

	this.a = a;

	this.t = t;

	this.res = 0;

	}

	

var line = new Array();



var p=new Array();



function chc(name, p)

	{

	this.name=name;

	this.p=p;

	}	



var txt = "";



var title="Titel";

var more=0;

line[0] = new item('Bezeichnung', 0, 0, 0, 'y', 1);



function check(nr)

	{

	line[nr].w=chkinp(getInp('w' + nr).value);

	getInp('w' + nr).value=line[nr].w;

	if (getInp('w' + nr).value < 4)

		getInp('h' + nr).value = 5;

	else if (getInp('w' + nr).value > 10)

		getInp('h' + nr).value = 10;

	else

		getInp('h' + nr).value = getInp('w' + nr).value;	

	calc(nr);

	}



function chkinp(str) 

	{

	var valid=".0123456789[]()-+*%/";

//	getRef("debug").innerHTML+="eingabe: "+str+"<BR>";

	for (var i = 0; i < str.length; i++)

   		if (valid.indexOf(str.charAt(i))<0 ) return 0;

 	return str;

 	}

	

function calc(nr)

	{

	line[nr].w=eval(chkinp(getInp('w' + nr).value));

	getInp('w' + nr).value=line[nr].w;

	line[nr].h=eval(chkinp(getInp('h' + nr).value));

	getInp('h' + nr).value=line[nr].h;

	line[nr].l=eval(chkinp(getInp('l' + nr).value));

	getInp('l' + nr).value=line[nr].l;

	var t=getInp('t'+nr);

//	getInp("debug").innerHTML+="t: "+t.checked+"<BR>";

	if (nav=="N6")

		if (t.checked)

			line[nr].t=1;

		else

			line[nr].t=2;

	else

		if (t[0].checked)

			line[nr].t=1;

		else

			line[nr].t=2;

	line[nr].name = getInp('name'+nr).value;

	title = getInp('title').value;

	more = getInp('more').value;

	

	line[nr].res=line[nr].w*line[nr].h*line[nr].l/line[nr].t;

	

//	getInp('res' + nr).value=line[nr].res;

	

	var res=0;

	for (i=0; i<ctr; i++)

		{

//		var v = getInp('res' + i).value;

		res += line[i].res*1;	

		}

	res*=1+more/100;



//	getInp("debug").innerHTML+="erg.: "+res+"<BR>";



	

//	getInp('result').value=res;

	getInp('r310').value=Math.ceil(res/310);

	getInp('r310b').value=Math.round(Math.ceil(res/310)/2)/10;

	getInp('r400').value=Math.ceil(res/400);

	getInp('r400b').value=Math.round(Math.ceil(res/400)/2)/10;

	getInp('r600').value=Math.ceil(res/600);

	getInp('r600b').value=Math.round(Math.ceil(res/600)/2)/10;

	}



function drop(nr)

	{

	for (i=nr; i<ctr; i++)

		line[i] = line[i+1];	

	ctr--;

	line[ctr]="";

	drawList();



	calc(0);

	}



function drawList()

	{

	if (nav=="NC4")

		id="name";

	else

		id="id";

	txt='<FORM name="Lines"><SPAN class="fontR-KAT">Projekt:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><INPUT type="text" '+id+'="title" class="left" size="42" value="' + title + '" width="300" onChange="calc(0);">'+

	'<IMG SRC="pictures/1x1.gif" WIDTH=20 HEIGHT=1 BORDER=0 ALT="-">'+

	'<SPAN id="del" style="cursor:pointer;" onClick="newList();" class="fontR-NAV2">neu</SPAN>'+

	'<IMG SRC="pictures/1x1.gif" WIDTH=12 HEIGHT=1 BORDER=0 ALT="-">'+

//	'<SPAN id="save" style="cursor:pointer;" onClick="save();" class="fontR-NAV2">speichern</SPAN>'+

	'<SPAN id="refresh" style="cursor:pointer;" class="fontR-NAV2">aktualisieren</SPAN>'+

	'<IMG SRC="pictures/1x1.gif" WIDTH=12 HEIGHT=1 BORDER=0 ALT="-">'+

//	'<A id="load" style="cursor:pointer;" onClick="load();" class="fontR-NAV2">laden</A><BR>'+

	'<SPAN id="print" style="cursor:pointer;" onClick="window.print();" class="fontR-NAV2">drucken</SPAN>'+

	'<SPAN id="form" style="position:absolute;"><SPAN id="lchc" class="fontR-NAV2" style="position:absolute; left:500px; top:0px; z-index:3; visibility:hidden; background-color:#E0E0E0;" onMouseover="show(this);" onMouseout="hide(this);"></SPAN></SPAN>'+

	'<BR><BR><TABLE class="fontFT" style="background-color:'+"'#FFFFFF'"+'; border-style:solid; border-width:1px; border-color:'+"'#000000'"+'; padding:0px;"><TR><TD></TD><TD></TD><TD class="fontR-NAV2" style="position:relative; vertical-align:top; text-align:center;">B<BR>[mm]</TD><TD class="fontR-NAV2" style="position:relative; vertical-align:top; text-align:center;">H<BR>[mm]</TD><TD class="fontR-NAV2" style="position:relative; vertical-align:top; text-align:center;">L<BR>[m]</TD><TD class="fontR-NAV2" colspan=2 style="position:relative; vertical-align:top; text-align:center;">Form<BR><IMG src="http://www.ramsauer.at/ramsauer07/images/p1.gif" style="position:relative; left:-6px;"><IMG src="http://www.ramsauer.at/ramsauer07/images/p2.gif" style="position:relative; left:8px;"></TD></TR>';

	for (var i=0; i<ctr; i++)

		{

		txt+='<TR style="border-style:solid; border-width:1px; border-color:#000000; padding:0px;">'+

		'<TD onClick="drop('+i+');" style="border-style:solid; border-width:2px; border-color:#EEEEEE #666666 #666666 #EEEEEE; background-color:#BBBBBB; padding:3px; font-weight:bold; cursor:pointer;" onMouseover="this.style.backgroundColor='+"'#FF0000'"+'" onMouseout="this.style.backgroundColor='+"'#BBBBBB'"+'"><SPAN id="x'+i+'">x</SPAN></TD>'+ 

		'<TD width=150><INPUT type="text" '+id+'="name'+i+'" style="text-align:left;" size="20" value="' + line[i].name + '" onChange="calc(' + i + ');"></TD>'+

		'<TD width=50><INPUT type="text" '+id+'="w'+i+'" align="right" style="text-align:right;" size="5" value="' + line[i].w + '" onChange="check(' + i + ');"></TD>'+ 

		'<TD width=50><INPUT type="text" '+id+'="h'+i+'" align="right" style="text-align:right;" size="5" value="' + line[i].h + '" onChange="calc(' + i + ');"></TD>'+ 

		'<TD width=50><INPUT type="text" '+id+'="l'+i+'" align="right" style="text-align:right;" size="5" value="' + line[i].l + '" onChange="calc(' + i + ');"></TD>'+ 

		'<TD width=20><INPUT type="radio" '+id+'="t'+i+'" name="t'+i+'" style="text-align:right;" value="1" onClick="calc(' + i + ');"';

		if (line[i].t==1)

			txt+=' checked';

		txt+='></TD><TD width=20><INPUT type="radio" '+id+'="t'+i+'" name="t'+i+'" style="text-align:right;" value="2" onclick="calc(' + i + ');"';

		if (line[i].t==2)

			txt+=' checked';

		txt+='></TD>'; //+'<TD width=100><INPUT type="text" '+id+'="res'+i+'" align="right" style="text-align:right;" size="10" value="' + line[i].res + '" readonly></TD>'

		txt+='</TR>';

		}

	txt+='<TR><TD style="border-style:solid; border-width:2px; border-color:#EEEEEE #666666 #666666 #EEEEEE; background-color:#BBBBBB; padding:3px; font-weight:bold; cursor:pointer;" onMouseover="this.style.backgroundColor='+"'#00AA00'"+'" onMouseout="this.style.backgroundColor='+"'#BBBBBB'"+'" onClick="newLine();"><SPAN id="plus">*</SPAN></TD></TR>'; //+	'<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD width=100><INPUT type="text" id="result"  class="right" size="10" value="0" readonly></TD></TR>';

	txt+='</TABLE><SPAN class="fontFT">';

	txt+='Zu berücksichtigender Mehrbedarf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUT type="text" '+id+'="more" class="right" size="5" value="' + more + '" width="50" onChange="calc(0);"> %</SPAN><BR><BR>';

	txt+='<TABLE class="fontFT"><TR><TD width="200" class="fontR-KAT">Errechnete Menge:</TD><TD width="50">Stück</TD><TD width="50">Kartons</TD></TR><TR><TD>310 ml Kartusche</TD><TD><INPUT type="text" '+id+'="r310"  class="right" size="5" value="0" readonly></TD>';

	txt+='<TD><INPUT type="text" '+id+'="r310b"  class="right" size="5" value="0" readonly></TD></TR>';

	txt+='<TR><TD>400 ml Folienbeutel</TD><TD><INPUT type="text" '+id+'="r400"  class="right" size="5" value="0" readonly></TD>';

	txt+='<TD><INPUT type="text" '+id+'="r400b"  class="right" size="5" value="0" readonly></TD></TR>';

	txt+='<TR><TD>600 ml Folienbeutel</TD><TD><INPUT type="text" '+id+'="r600"  class="right" size="5" value="0" readonly></TD>';

	txt+='<TD><INPUT type="text" '+id+'="r600b"  class="right" size="5" value="0" readonly></TD></TR></TABLE></FORM>';

//	txt+='<DIV '+id+'="debug" style="position:absolute; top:10; left:10; z-index:2;"></DIV>';

	

	if (nav=="NC4")

		getRef("list").document.write(txt);

	else		

		getRef("list").innerHTML=txt;

	}



function show(elem)

	{

	getRef("lchc").style.visibility='visible';

	}



function hide(elem)

	{

	getRef("lchc").style.visibility='hidden';

	}



function choose(nr)

	{

	var chk=confirm('Das Laden des Projekts "'+p[nr].name+'" überschreibt die aktuellen Daten.');

	if (chk == true)

		{

		getRef("lchc").innerHTML="";

		getRef('lchc').style.visibility="hidden";

		title=p[nr].name;

		i=p[nr].p;

		n=document.cookie.indexOf("|", i);

		title=document.cookie.substring(i, n);

		i=n+1;

	//	l=document.cookie.indexOf("|||", i);

		n=document.cookie.indexOf("|", i);

		ctr=document.cookie.substring(i, n);

	//	alert("ctr="+ctr);

		i=n+1;

		n=document.cookie.indexOf("|", i);

		more=document.cookie.substring(i, n);

		i=n+1;

		for (j=0; j<ctr; j++)

			{

			line[j]=new item();	

			n=document.cookie.indexOf("|", i);

			line[j].name=document.cookie.substring(i, n);

			i=n+1;

			n=document.cookie.indexOf("|", i);

			line[j].w=document.cookie.substring(i, n);

			i=n+1;

			n=document.cookie.indexOf("|", i);

			line[j].h=document.cookie.substring(i, n);

			i=n+1;

			n=document.cookie.indexOf("|", i);

			line[j].l=document.cookie.substring(i, n);

			i=n+1;

			n=document.cookie.indexOf("|", i);

			line[j].t=document.cookie.substring(i, n);

			i=n+1;

	//		alert(j+": "+line[j].name+"\n");

			}

		drawList();

		for (j=0; j<ctr; j++)

			calc(j);

		}

	}

	

function droplist(nr)

	{

	i=p[nr].p;

	l=document.cookie.indexOf("|", i);

	var chk=confirm('Sind Sie sicher, dass Sie das Projekt "'+document.cookie.substring(i,l)+'" löschen wollen?');

	if (chk == true)

		{

		getRef('lchc').style.visibility="hidden";

		l=document.cookie.indexOf("|||", i);

		document.cookie=document.cookie.substring(0, i-3)+document.cookie.substring(l+3, document.cookie.length);	

		load();

//		getRef("debug").innerHTML=document.cookie;

		}

	}



function newLine()

	{

	line[ctr] = new item('Bezeichnung', 0, 0, 0, 'y', 1);

	ctr++;

	drawList();

	calc(0);

	}



function init()

	{

	drawList();

	calc(0);

	}

	

function newList()

	{

	var chk=confirm('Die aktuellen Daten werden für ein neues Projekt gelöscht.');

	if (chk == true)

		{

		ctr=1;	

		title="Titel";

		line[0] = new item('Bezeichnung', 0, 0, 0, 'y', 1);

		init();

		}

	}

		

function save()

	{

	if(navigator.cookieEnabled == true)

		{

		var chk=confirm('Das Projekt wird unter "'+title+'" gespeichert.');

		if (chk == true)

			{

			if (document.cookie) 

				{

				var c="";

				var i=document.cookie.indexOf("|||");

				while (i!=-1)

					{

					c+=document.cookie.substring(i, (i = document.cookie.indexOf("|||", i+3))+3);

					i = document.cookie.indexOf("|||", i+3);

					}

				}

			else

				{

				var c="";

				}

					

			c+="|||"+title+"|"+ctr+"|"+more+"|";

			for (i=0; i<ctr; i++)

				c+=line[i].name+"|"+line[i].w+"|"+line[i].h+"|"+line[i].l+"|"+line[i].t+"|";

			var now=new Date();

			var exp=new Date(now.getTime() + 1000*60*60*24*365*5);

			c+="||"+"; expires="+exp.toGMTString()+";";

			document.cookie=c;

//			getRef("debug").innerHTML=document.cookie;

			}	

		}

	else

		alert('Um Projekte zu speichern müssen Sie Cookies aktivieren. (Diese Applikation übermittelt keine Daten an den Server, das Cookie wird nur auf Ihrem Rechner von einer Java-Script Routine ausgelesen.)');

	}



function load()

	{

	if (document.cookie) 

		{

//		var c=document.cookie;

		var c="";

//		getRef("debug").innerHTML=document.cookie;

		l=document.cookie.length;

		var i=document.cookie.indexOf("|||");

		if (i == -1)

			{

			alert ("Keine Daten gespeichert!");

			}

		else

			{

			i+=3;

			pctr=0;

	//		document.write(c+"<BR>");

			c='<TABLE class="fontFT" style="background-color:#E0E0E0;">';

			while (i<l)

				{

				n=document.cookie.indexOf("|", i);

	//			document.write("n="+n+", ");

				p[pctr]=new chc(document.cookie.substring(i, n), i);

				c+='<TR><TD style="border-style:solid; border-width:2px; border-color:#EEEEEE #666666 #666666 #EEEEEE; background-color:#BBBBBB; padding:3px; font-weight:bold; cursor:pointer;" onMouseover="this.style.backgroundColor=\'#FF0000\';" onMouseout="this.style.backgroundColor=\'#BBBBBB\';" onClick="droplist('+pctr+');"><SPAN name="dropc'+(pctr+1)+'">x</SPAN></TD><TD onMouseover="this.style.backgroundColor=\'#999999\';" onMouseout="this.style.backgroundColor=\'#EEEEEE\';" style="font-weight:bold; cursor:pointer;"><A '+id+'="chc'+(pctr+1)+'" onClick="choose('+pctr+');"> '+p[pctr].name+'</A></TD></TR>';

				m=document.cookie.indexOf("||||||", n+1);

				if (m>=0)

					i=m+6;

				else

					i=l;

	//			document.write("i="+i+", l="+l+", ctr="+pctr+", p="+p[pctr]+": <BR>");

				pctr++;

				}

			c+="</TABLE>";

			getRef('lchc').innerHTML=c;

			getRef('lchc').style.visibility="visible";

			}

		}

	else

		{

		alert ("Keine Daten gespeichert!");

		}

	}



function del()

	{

	var chk = confirm("Wollen Sie wirklich alle gespeicherten Projekte löschen?");

	if (chk == true)

		document.cookie="";

	}

	

function getNav()

	{

	var s = navigator.userAgent;

	if (s.lastIndexOf("Opera") > -1)

		return("Opera");

	if (s.lastIndexOf("Gecko") > -1 )

		return("N6");

	if (document.layers)

		return("NC4");

	if ((s.lastIndexOf("MSIE 5") > -1 || s.lastIndexOf("MSIE 6") > -1 || s.lastIndexOf("MSIE 7")) && s.lastIndexOf("Opera") == -1)

		return("MSIE");

	return("unknown");

	}



function getInp(elem)

	{

	if (nav=="NC4")

		return(getRef('list.document.Lines.'+elem));

	else

		return(getRef(elem));

	}

	

function getRef(elem)

	{

	var ref;



	if (nav == "NC4")

		ref = eval("document."+elem);

	

	if ((nav == "N6") || (nav == "Opera"))

	        ref = eval(document.getElementById(elem));

	

	if (nav == "MSIE")

	        ref = eval("document.all."+elem);

	 

	       

	return(ref);

	}


