/*
Kalkscript_V2.js
	erstellt:	01.02.2004
	durch:	Rüdiger Ockert
	letzte Änderung:	02.04.2004
	durch:	Rüdiger Ockert
*/

	



	function SiteLoad($showpreis){	
		chg('SITELOAD',$showpreis);
	}
	
	
	function FillListBox($w,$b,$v,$c,$mcc){
		
		$li=null;
		$LastValue=$w.value;
		$NewIndex=0;
		$selectyp=document.FrmKalk.ListBoxTYP.value

		$w.selectedIndex = $w.length-1;
		while($w.length > 0) $w.options[$w.length-1] = null;
		
		$w.options[$w.length]= new Option("[-Auswahl-]","bnA");
		

		for(var i = 0; i < ArtikelDaten[$selectyp].length; i++){	
			with(document.FrmKalk){
				var $TYP=($c >= 0) ? $T=ArtikelDaten[$selectyp][i][0] : "" ;
				var $TYP=($c >= 1) ? $T=ArtikelDaten[$selectyp][i][1]==ListBoxFORMAT.value : "" ;
				var $TYP=($c >= 2) ? $T=ArtikelDaten[$selectyp][i][1]==ListBoxFORMAT.value && ArtikelDaten[$selectyp][i][2]==ListBoxPAPIER.value : "" ;	
				var $TYP=($c >= 3) ? $T=ArtikelDaten[$selectyp][i][1]==ListBoxFORMAT.value && ArtikelDaten[$selectyp][i][2]==ListBoxPAPIER.value && ArtikelDaten[$selectyp][i][3]==ListBoxFARBIGKEIT.value : "" ;	
				var $TYP=($c >= 4) ? $T=ArtikelDaten[$selectyp][i][1]==ListBoxFORMAT.value && ArtikelDaten[$selectyp][i][2]==ListBoxPAPIER.value && ArtikelDaten[$selectyp][i][3]==ListBoxFARBIGKEIT.value && ArtikelDaten[$selectyp][i][4]==ListBoxFALZ.value : "" ;

				if($T){
					for(var i1 = 0; i1 < KeyDaten[$v].length; i1++){
						if(KeyDaten[$v][i1][0]==ArtikelDaten[$selectyp][i][$c+1] && KeyDaten[$v][i1][1]!=$li){
							$w.options[$w.length]= new Option(KeyDaten[$v][i1][1 + $showshort],KeyDaten[$v][i1][0]);
							$li=KeyDaten[$v][i1][1];
							
							
							if($mc[0]!='NULL'&&$siteload==1){
								
								if (KeyDaten[$v][i1][0]==$mcc){
									$NewIndex = $w.length-1;
								}	
							
							}
							else{
							
								if (KeyDaten[$v][i1][0]==$LastValue){
									$NewIndex = $w.length-1;
								}
								
							}
														
						}
					}//end for
				}// end if
			}//end with
		}//end for
			
		if($w.length==2){
			$w.selectedIndex=1;						
		}
		else{
			$w.selectedIndex=$NewIndex;
		}		
			
		if($v=='auflage')$siteload=0;

		chgValue($b);

	}// end function
	
	


	function chg($name,$showpreis){

		switch($name){
			case "SITELOAD":
				with(document.FrmKalk.ListBoxTYP){
					selectedIndex = length-1;
					while(length > 0) options[length-1] = null;
					
					for(var i = 0; i<KeyDaten['typ'].length; i++){
						options[length]= new Option(KeyDaten['typ'][i][1 + $showshort],KeyDaten['typ'][i][0]);
						
						if($mc[0]!='NULL' && $siteload==1){
							if($mc[1]==KeyDaten['typ'][i][0]){
								$NewIndex=length-1;
							}
						}
						else{
							$NewIndex=0;
						}
					}
					selectedIndex =$NewIndex;
				}
				if($name != "SITELOAD")break;			
			
			case "ListBoxTYP":
				FillListBox(document.FrmKalk.ListBoxFORMAT,document.FrmKalk.ListBoxTYP,'format',0,$mc[2])
				mitInprint();
				
			case "ListBoxFORMAT":
				FillListBox(document.FrmKalk.ListBoxPAPIER,document.FrmKalk.ListBoxFORMAT,'papier',1,$mc[3]+" "+$mc[4]+" "+$mc[5])

			case "ListBoxPAPIER":
				FillListBox(document.FrmKalk.ListBoxFARBIGKEIT,document.FrmKalk.ListBoxPAPIER,'farbigkeit',2,$mc[6])

			case "ListBoxFARBIGKEIT":
				FillListBox(document.FrmKalk.ListBoxFALZ,document.FrmKalk.ListBoxFARBIGKEIT,'falz',3,$mc[7]+" "+$mc[8])

			case "ListBoxFALZ":
				FillListBox(document.FrmKalk.ListBoxAUFLAGE,document.FrmKalk.ListBoxFALZ,'auflage',4,$mc[0])

			case "ListBoxVERSAND":
			case "ListBoxAUFLAGE":
				if($showpreis=='on'){					
					chgValue(document.FrmKalk.ListBoxAUFLAGE);	
				}

		}		
		with(document.FrmKalk){
		
		$auswahl=ListBoxAUFLAGE.value + " " + ListBoxTYP.value + " " + ListBoxFORMAT.value + " " + ListBoxPAPIER.value + " " + ListBoxFARBIGKEIT.value + " " + ListBoxFALZ.value;
		document.FrmKalk.TextBoxMatchcode.value=$auswahl;
			
		if($showpreis=='on'){				
			if($auswahl.indexOf("bnA")==-1){
				for(var i = 0; i < ArtikelDaten[document.FrmKalk.ListBoxTYP.value].length; i++){
					if(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][7]==$auswahl){
						Inetto=Math.round(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][8]*100);
						for(var i1 = 0; i1 < VersandDaten.length; i1++){
							if(Number(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][9]) > Number(VersandDaten[0][0])){
								$LastVersandIndex=0;
							}
							else{
								$LastVersandIndex=i1;
							}
							if(Number(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][9]) <= Number(VersandDaten[$LastVersandIndex][0]) || Number(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][9]) > Number(VersandDaten[$LastVersandIndex][0])&&$LastVersandIndex==0){				
								if(ListBoxVERSAND.value=="VK") IVersand=VersandDaten[$LastVersandIndex][1]*100;						
								if(ListBoxVERSAND.value=="NN"){
									IVersand=(VersandDaten[$LastVersandIndex][1]*100)+($NN_Aufschlag*100);
									if(Number($max_NNVersand)<=Number(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][9])){
										alert('Achtung! \nÜber '+ $max_NNVersand +' kg Versandgewicht ist die Bezahlung per Nachname nicht möglich. \nVersand erfolgt per Spedition');
										document.FrmKalk.ListBoxVERSAND.selectedIndex=0;
										IVersand=VersandDaten[$LastVersandIndex][1]*100;
									}
								}
								if(ListBoxVERSAND.value=="BS") IVersand=0;
							}
						}
				
						if (ChInprint.checked == false && MitImprint[document.FrmKalk.ListBoxTYP.value]=='J')Inetto=Math.round(Inetto * $ohne_Imprit_Aufschlag);

						IMwst=Math.round((Inetto  + IVersand) * $MwSt_Satz);
						
						$strNetto=FormatCurrency(Inetto)+ " €"
						$strVersand=FormatCurrency(IVersand) + " €";
						$strMwst=FormatCurrency(IMwst)+ " €";
						$strBrutto=FormatCurrency(Inetto + IVersand + IMwst)+ " €";
						$strGewicht=FormatCurrency(Number(ArtikelDaten[document.FrmKalk.ListBoxTYP.value][i][9])*100)+ " kg";
						
					}
				}
			}
			else{
				$strNetto="unvollständige Auswahl";
				$strVersand="";
  				$strMwst="";
				$strBrutto="";
				$strGewicht="";
			}
			
				
			
			TextBoxPreis.value=$strNetto;
			TextBoxVERSAND.value=$strVersand;
			TextBoxMWST.value=$strMwst;			
			TextBoxBRUTTO.value=$strBrutto;	
			TextBoxGEWICHT.value=$strGewicht;		
		}
		}		
	}


	function chgValue($opfield){
		//löscht nach auswahl eines Eintrag den Eintrag -> [-Auswahl-]
		if($opfield.length > 1 && $opfield.options[0].value == "bnA" && $opfield.selectedIndex !=0){
			$opfield.options[0] = null;
		}
	}
	
	
	
	
	
	function FormatCurrency($ValCur){
		$cur=String($ValCur);

		if ($cur.length == 1)$tcur="00" + $cur;		
		if ($cur.length == 2)$tcur="0" + $cur;
		if ($cur.length >= 3 && $cur.length <= 5)$tcur=$cur;
		if ($cur.length > 5){
			$tcur=$cur.substring(0,$cur.length-5)+"."+$cur.substring($cur.length-5,$cur.length);
		}
		return $tcur.substring(0,$tcur.length-2)+","+ $tcur.substring($tcur.length-2,$tcur.length);		
	
	}
	
	function mitInprint(){
		if (MitImprint[document.FrmKalk.ListBoxTYP.value]=='N'){
			document.FrmKalk.ChInprint.checked=false;
			document.FrmKalk.ChInprint.disabled=true;
		}
		else{
			document.FrmKalk.ChInprint.checked=true;
			document.FrmKalk.ChInprint.disabled=false;
		}
	}
