function resetem() {
	document.getElementsByName("module[1]")[0].checked = false ;
	document.getElementsByName("module[1]")[1].checked = false ;
	document.getElementsByName("module[2]")[0].checked = false ;
	document.getElementsByName("module[2]")[1].checked = false ;
	
	document.getElementById('clients').value = 1 ;
	document.getElementById('months').value = 54 ;
	
	for(var i=0;i<=3;i++) {
		document.getElementById('e' + i).style.display = 'none' ;
	}
	
	document.getElementById('button_disabled').style.display = 'block' ;
	document.getElementById('button_enabled').style.display = 'none' ;
	
}

function ajd_c(param) {
	var clients = document.getElementById('clients').value ;
	if (param == '+' && clients < 9999) {
		clients++ ;
	}
	if (param == '-' && clients > 1) {
		clients-- ;
	}
	document.getElementById('clients').value = clients ;
}

function showModule() {
	var choice = document.getElementById('product').value ;
	resetem() ;
	if (choice=='0') {
		document.getElementById('OLmods').style.display = 'none' ;
		document.getElementById('CLmods').style.display = 'none' ;
		document.getElementById('mask').style.display = 'none' ;
	} else {
		if (choice<='2') {
			document.getElementById('CLmods').style.display = 'block' ;
			document.getElementById('OLmods').style.display = 'none' ;
		} else {
			document.getElementById('OLmods').style.display = 'block' ;
			document.getElementById('CLmods').style.display = 'none' ;
		}
	} 
}

function resetall() {
	
	resetem() ;
	document.getElementById('CLmods').style.display = 'block' ;
	document.getElementById('OLmods').style.display = 'none' ;
	
	document.getElementById('result').style.display = 'none' ;
	document.getElementById('calculator').style.display = 'block' ;
	document.getElementById('product').value = 1 ;
	
}

function cf() {
	if (document.getElementsByName("module[1]")[0].checked == true ||
		document.getElementsByName("module[1]")[1].checked == true ||
		document.getElementsByName("module[2]")[0].checked == true ||
		document.getElementsByName("module[2]")[1].checked == true) {
		document.getElementById('button_disabled').style.display = 'none' ;
		document.getElementById('button_enabled').style.display = 'block' ;
	} else {
		document.getElementById('button_disabled').style.display = 'block' ;
		document.getElementById('button_enabled').style.display = 'none' ;
	}
}

function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
    return sa ? s : s[0];
}

function roundit(x, n) {
	  if (n < 1 || n > 14) return false;
	  var e = Math.pow(10, n);
	  var k = (Math.round(x * e) / e).toString();
	  if (k.indexOf('.') == -1) k += '.';
	  k += e.toString().substring(1);
	  return str_replace('.', ',', (k.substring(0, k.indexOf('.') + n+1)));
}

function calc() {
	
	var error = 0 ;
	
	/**
	* List of all main products
	*/
	var series = new Array ;
	series[1] = 'Classic Line Standard' ;
	series[2] = 'Classic Line Professional' ;
	series[3] = 'Office Line Basic' ;
	series[4] = 'Office Line Business' ;
	
	/**
	* Series to Module Price Relation
	*/
	var price = new Array ;
	for(var i=1;i<=4;i++) {
		price[i] = new Array ;
	}
	/* CL Std */
	price[1][0] = new Array( 'Auftragsbearbeitung', 995 , 22 ) ;
	price[1][1] = new Array( 'Finanzbuchhaltung'  , 995 , 22 ) ;
	price[1][2] = 595 ;
	/* CL Pro */
	price[2][0] = new Array( 'Auftragsbearbeitung', 1795, 42.8 ) ;
	price[2][1] = new Array( 'Finanzbuchhaltung'  , 1795, 42.8 ) ;
	price[2][2] = 595 ;
	/* OL Std */
	price[3][0] = new Array( 'Rechnungswesen' , 890 , 14 ) ;
	price[3][1] = new Array( 'Warenwirtschaft', 890 , 14 ) ;
	price[3][2] = 690 ;
	/* OL Pro */
	price[4][0] = new Array( 'Rechnungswesen' , 1990, 65 ) ;
	price[4][1] = new Array( 'Warenwirtschaft', 1990, 65 ) ;
	price[4][2] = 1290 ;
	
	var modules = new Array ;
	for(var i=1;i<=4;i++) {
		modules[i] = new Array ;
	}
	
	modules[1][1] = 'Auftragsbearbeitung' ;
	modules[1][2] = 'Finanzbuchhaltung' ;
	modules[1][3] = 'Auftragsbearbeitung, Finanzbuchhaltung' ;

	modules[2][1] = 'Auftragsbearbeitung' ;
	modules[2][2] = 'Finanzbuchhaltung' ;
	modules[2][3] = 'Auftragsbearbeitung, Finanzbuchhaltung' ;
	
	modules[3][1] = 'Warenwirtschaft' ;
	modules[3][2] = 'Rechnungswesen' ;
	modules[3][3] = 'Warenwirtschaft, Rechnungswesen' ;
	
	modules[4][1] = 'Warenwirtschaft' ;
	modules[4][2] = 'Rechnungswesen' ;
	modules[4][3] = 'Warenwirtschaft, Rechnungswesen' ;
	
	
	/**
	* Modules
	*/
	var input = new Array ;
	var licence = 0 ;
	var clients = 0 ;
	var total = 0 ;
	var maintenance = 0 ;
	var monthly_clients = 0 ;
	
	input['Months'] = document.getElementById('months').value ;
	input['Series'] = document.getElementById('product').value ;
	input['Clients'] = document.getElementById('clients').value ;
	input['Modules'] = 0 ;
	
	if (input['Series'] <= 2) {
		if (input['Series']==1 && input['Clients']>10) { error = 1 ; }
		if (input['Clients']>=11) {
			maintenance = (parseInt(input['Clients'])-1)*8 ;
			monthly_clients = 8 ;
		} else if (input['Clients']>=6) {
			maintenance = (parseInt(input['Clients'])-1)*9 ;
			monthly_clients = 9 ;
		} else if (input['Clients']>=2) {
			maintenance = (parseInt(input['Clients'])-1)*10 ;
			monthly_clients = 10 ;
		}
		for(var i=0;i<=1;i++) {
			if (document.getElementsByName("module[1]")[i].checked == true) {
				licence = parseInt(licence)+parseInt(price[input['Series']][i][1]) ;
				maintenance = parseInt(maintenance)+parseFloat(price[input['Series']][i][2]) ;
				var j = i+1 ;
				input['Modules'] = parseInt(input['Modules'])+j ;
			}
		}
		
	} else {
		if (input['Series']==4&&input['Clients']>=11) {
			maintenance = (parseInt(input['Clients'])-1)*10 ;
			monthly_clients = 10 ;
		} else if (input['Series']==4&&input['Clients']>=2) {
			maintenance = (parseInt(input['Clients'])-1)*13 ;
			monthly_clients = 13 ;
		} else if (input['Series']==3) {
			maintenance = parseInt(input['Clients'])*10 ;
			monthly_clients = 10 ;
			if (input['Clients']>5) { error = 2 ; }
		}
		for(var i=0;i<=1;i++) {
			if (document.getElementsByName("module[2]")[i].checked == true) {
				licence = parseInt(licence)+parseInt(price[input['Series']][i][1]) ;
				maintenance = parseInt(maintenance)+parseFloat(price[input['Series']][i][2]) ;
				var j = i+1 ;
				input['Modules'] = parseInt(input['Modules'])+j ;
			}
		}
	}
	
	clients = parseInt(price[input['Series']][2])*parseInt(input['Clients']) ;
	
	total = licence+clients ;
	
	if (total>50000) { error = 3 ; }
	
	/**
		percentage by month in relation to amount
		
		0 = 500-2500
		1 = 2501 - 5000
		2 = 5001 - 25000
		3 = 25001 - 50000
	*/
	var percent = new Array ;
	percent[ 15 ] = new Array( 6.85, 6.88, 6.74, 6.65 ) ;
	percent[ 24 ] = new Array( 4.54, 4.54, 4.45, 4.37 ) ;
	percent[ 30 ] = new Array( 3.80, 3.69, 3.63, 3.51 ) ;
	percent[ 36 ] = new Array( 3.24, 3.19, 3.09, 3.01 ) ;
	percent[ 42 ] = new Array( 2.85, 2.82, 2.74, 2.61 ) ;
	percent[ 48 ] = new Array( 2.57, 2.45, 2.42, 2.33 ) ;
	percent[ 54 ] = new Array( 2.41, 2.31, 2.18, 2.10 ) ;
	
	/**
	* Check for price limits to refer to the percentage
	*/
	var rated ;
	if (parseInt(total) <= 2500) {
		var rated = 0 ;
	} else if (parseInt(total) >= 2501 && parseInt(total) <= 5000) {
		var rated = 1 ;
	} else if (parseInt(total) >= 5001 && parseInt(total) <= 25000) {
		var rated = 2 ;
	} else if (parseInt(total) >= 25001 && parseInt(total) <= 50000) {
		var rated = 3 ;
	} else {
		var rated = 4 ;
	}
	
	/**
	* Calculate award and monthly rate
	*/
	var percentage = percent[input['Months']][rated] ;
	var award = (parseInt(total)/100)*parseFloat(percentage);
	var awarded = award+total ;
	var rate = parseFloat(awarded)/input['Months'] ;
	
	/* 595 x 10 + 795 = 6745 (lt. Leasingtabelle kommt dann der Satz 2,2o zur Anwendung) */
	var step1 = clients+licence ;
	/* 6745 : 10 = 675,50 */
	var step2 = step1/input['Clients'] ;
	/* 675,50 x 0,022 = 14,84 (pro AP mtl. Ohne Wartung) */
	var step3 = step2*(percentage/100) ;
	/* Wartung mtl. Pro Client: 10,03 */
	var step4 = monthly_clients ;
	/* Endpreis (gemäß PromotionBanner) 24,87 Euro pro Client mtl. Inkl. Wartung. Zzgl. Mwst. */
	var step5 = parseFloat(monthly_clients)+parseFloat(step3) ;
	
	
	/* Wartung pro AP: 32:2 = 16 */
	var step6 = parseFloat(maintenance)/parseInt(input['Clients']) ;
	/* Leasing und Wartung pro AP und Monat: 16 + 24,22 = 40,22 */
	var step7 = parseFloat(step6)+parseFloat(step3) ;
	
	document.getElementById('result').style.display = 'block' ;
	document.getElementById('calculator').style.display = 'none' ;
	
	for(var i=0;i<=3;i++) {
		document.getElementById('e' + i).style.display = 'none' ;
	}
	
	if (error == 3) {
		document.getElementById('e3').style.display = 'block' ;
		document.getElementById('contact').href = '/smb/kontakt/kontaktformular.asp?Thema=Leasingrechner&bestProduct=' + hp + ' - ' + modules[input['Series']][input['Modules']] + ': ' + input['Clients'] + ' Clients, ' + input['Months'] + ' Monate' ;
	} else if (error == 2) {
		document.getElementById('e2').style.display = 'block' ;
		document.getElementById('contact').href = '/smb/kontakt/kontaktformular.asp?Thema=Leasingrechner&bestProduct=' + hp + ' - ' + modules[input['Series']][input['Modules']] + ': ' + input['Clients'] + ' Clients, ' + input['Months'] + ' Monate' ;
	} else if (error == 1) {
		document.getElementById('e1').style.display = 'block' ;
		document.getElementById('contact').href = '/smb/kontakt/kontaktformular.asp?Thema=Leasingrechner&bestProduct=' + hp + ' - ' + modules[input['Series']][input['Modules']] + ': ' + input['Clients'] + ' Clients, ' + input['Months'] + ' Monate' ;
	} else {
	
		if (input['Clients'] == 1) {
			document.getElementById('i_clients').innerHTML = input['Clients'] + ' Arbeitsplatz' ;
		} else {
			document.getElementById('i_clients').innerHTML = input['Clients'] + ' Arbeitspl&auml;tze' ;
		}
		document.getElementById('i_modules').innerHTML = modules[input['Series']][input['Modules']] ;
		document.getElementById('i_months').innerHTML = input['Months'] ;
		/* document.getElementById('i_series').innerHTML = series[input['Series']] ; */
		
		if (hp == 'ol') {
			hp = 'Office Line' ;
		} else if (hp == 'cl') {
			hp = 'Classic Line' ;
		} else {
			hp = 'n/a' ;
		}
		
		document.getElementById('r_total').innerHTML = roundit(total,2) ;
		document.getElementById('r_rate_per_client').innerHTML = roundit(step7,2) ;
		/*
		document.getElementById('r_rate').innerHTML = roundit(rate,2) ;
		document.getElementById('r_rate_per_client').innerHTML = roundit((rate/input['Clients']),2) ;
		*/
		document.getElementById('r_clients').innerHTML = monthly_clients ;
		document.getElementById('r_maintenance').innerHTML = roundit(maintenance,2) ;
		document.getElementById('e0').style.display = 'block' ;
		document.getElementById('contact').href = '/smb/kontakt/kontaktformular.asp?Thema=Leasingrechner&bestProduct=' + hp + ' - ' + modules[input['Series']][input['Modules']] + ': ' + input['Clients'] + ' Clients, ' + input['Months'] + ' Monate, ' + roundit(total,2) + ' Eur Lizenzpreis, ' + roundit(step7,2) + ' Eur Finanzierung inkl Wartung pro Client, ' + roundit(maintenance,2) + ' Eur Wartungskosten' ;
	}
}
