// Setup ------------------------------------------------------------------------------------------------------------------------------

// Liste der Softwarelösungen
var Products = new Array('Office Line Evolution Basic',
						 'Office Line Evolution Business',
						 'Sage New Classic Standard',
						 'Sage New Classic Professional',
						 'Personalwirtschaft',
						 'ACT!',
						 'Sage CRM',
						 'Sage SalesLogix');

// Liste der Packshot der Softwarelösungen
var Packshot = new Array('/images/smb/packshots/office_line_112.png',
						 '/images/smb/packshots/office_line_112.png',
						 '/images/smb/packshots/classic_line_112.png',
						 '/images/smb/packshots/classic_line_112.png',
						 '/images/smb/packshots/personalwirtschaft_112.png',
						 '/images/smb/packshots/act_112.png',
						 '/images/smb/packshots/sage_crm_112.png',
						 '/images/smb/packshots/sales_logix_112.png');

// Produktgruppen
var ProductGrps 		= 	new Array(3);
	ProductGrps[0] 		=	new Array(0,1,2,3);
	ProductGrps[1] 		=	new Array();
	ProductGrps[1][0] 	= 	4;
	ProductGrps[2] 		=	new Array(5,6,7);
	
// Varible ID der HTML Seite							
var pfBestStr = new Array(
						  'pfOLBest',
						  'pfOLBest',
						  'pfOLBest',
						  'pfOLBest',
						  'pfOLBestPW',
						  'pfCRMBest',
						  'pfCRMBest',
						  'pfCRMBest'
						  );
						
var pfVarBestStr = new Array(8);
	pfVarBestStr[0] = new Array('pfOLProductName','pfOLBr','');
	pfVarBestStr[1] = new Array('pfOLProductName','pfOLBr','');
	pfVarBestStr[2] = new Array('pfOLProductName','pfOLBr','');
	pfVarBestStr[3] = new Array('pfOLProductName','pfOLBr','');
	pfVarBestStr[4] = new Array('','','');
	pfVarBestStr[5] = new Array('pfCRMProductName','pfCRMBr','');
	pfVarBestStr[6] = new Array('pfCRMProductName','pfCRMBr','');
	pfVarBestStr[7] = new Array('pfCRMProductName','pfCRMBr','');
							 
// Produkt Links
var ProdLinks= new Array('/smb/prodloes/ol/infopaket-anfordern.asp?Thema=prodkonf',
						 '/smb/prodloes/ol/infopaket-anfordern.asp?Thema=prodkonf',
						 '/smb/prodloes/cl/infopaket-anfordern.asp?Thema=prodkonf',
						 '/smb/prodloes/cl/infopaket-anfordern.asp?Thema=prodkonf',
						 '/smb/prodloes/pw/',
						 '/smb/kontakt/kontaktformular.asp?Thema=prodkonf',
						 '/smb/prodloes/sage_crm/infopaket-anfordern.asp?Thema=prodkonf',
						 '/smb/prodloes/sales_logix/infopaket-anfordern.asp?Thema=prodkonf');

// Anwendungsbereiche welche die Software erfüllen soll
var AppProp  = new Array('Finanzbuchhaltung', /* 0 */
						 'Anlagenbuchhaltung', /* 1 */
						 'Controlling', /* 2 */
						 'Business Intelligence', /* 3 */
						 'Kostenrechnung', /* 4 */
						 'Mahnwesen', /* 5 */

						 'Lager und Beschaffung', /* 6 */
						 'Angebote und Faktura', /* 7 */
						 'Produktionsplanung', /* 8 */
						 'Produktkonfiguration', /* 9 */
						 'Projektverwaltung', /* 10 */

						 'Lohn und Gehalt', /* 11 */
						 
						 'Kundenservice / Support', /* 12 */
						 'Marketing', /* 13 */
						 'Kundenmanagement / CRM', /* 14 */
						 'Mobiler Datenzugriff', /* 15 */
						 'eBusiness'); /* 16 */
						
// 	weitere Angaben
var AppSett  = new Array('1-3 Arbeitsplätze',
						 '4-10 Arbeitsplätze',
						 '11-20 Arbeitsplätze',
						 '>20 Arbeitsplätze',

						 'Dienstleister',
						 'Hersteller',
						 'Handel',
						 'Öffentliche Einrichtung',
						 'Non-Profit Organisationen',

						 'Windows',
						 'Linux/Unix',
						 'MacOS',
						 'egal',
		
						 'günstige Standardlösung',
						 'egal',
						 'individuelle, integrierbare Lösung');

// 	Matrix nach welcher das Produkt bestimmt wird!!!
//	Die Zeilen entsprechen den Einträgen des AppProp Arrays - Spalten  entsprechen den Softwarelösungen ( Products Array )
//	
//	Die einzelnen Zeilen werden durch die Checkboxen auf der Html Seite ausgewählt.
//	Ausgwählte Zeilen werden spaltenweise aufsummiert - Die größte Summe gewinnt
var AppPropArray       = new Array(AppProp.length);
	AppPropArray[  0 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  1 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  2 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  3 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  4 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  5 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  6 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  7 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[  8 ] = new Array( 10, 10,  5,  5,  0,  0,  0,  0 );
	AppPropArray[  9 ] = new Array(  0, 100, 0,  0,  0,  0,  0,  0 );
	AppPropArray[ 10 ] = new Array( -10, 10, 0,  0,  0,  0,  0,  0 );
	AppPropArray[ 11 ] = new Array(  0,  0, 10, 10,  0,  0,  0,  0 );
	AppPropArray[ 12 ] = new Array(  0,  0,-100,-100, 0,-1, 10, 10 );
	AppPropArray[ 13 ] = new Array(  0,  0,-100,-100, 0,-1, 10, 10 );
	AppPropArray[ 14 ] = new Array(  0,  0,  0,  0,  0, 10,  5,  5 );
	AppPropArray[ 15 ] = new Array( 10, 10, 10, 10,  0,  0,  0,  0 );
	AppPropArray[ 16 ] = new Array(  1,  1,  1,  1,  0, -5,  1,  1 );

// 	weitere Angaben (Rechengrundlage)
var ArrayOffset = 17;
var AppSettArray      = new Array(AppSett.length);
	//workstations
	AppSettArray[ 0 ] = new Array( 10,  3, 10,  3,  0, 10,  5,  3 );
	AppSettArray[ 1 ] = new Array(  0, 10,  0, 10,  0,  5, 10,  5 );
	AppSettArray[ 2 ] = new Array(  0, 10,  0,  5,  0,  0, 10, 10 );
	AppSettArray[ 3 ] = new Array(  0, 10,  0,  0,  0,  0,  4, 10 );

	//industry
	AppSettArray[ 4 ] = new Array( 10, 10,  0,  0,  0, 10, 10, 10 );
	AppSettArray[ 5 ] = new Array( 10, 10, 10, 10,  0, 10, 10, 10 );
	AppSettArray[ 6 ] = new Array( 10, 10, 10, 10,  0, 10, 10, 10 );
	AppSettArray[ 7 ] = new Array( 10, 10,  5,  5,  0, 10, 10, 10 );
	AppSettArray[ 8 ] = new Array( 10, 10,  5,  5,  0, 10, 10, 10 );

	//operating system
	AppSettArray[ 9 ]  = new Array(   0,   0,  0,  0,   0,   0,  0,   0 );
	AppSettArray[ 10 ] = new Array(-100,-100,100,100,   0,-100,100,-100 );
	AppSettArray[ 11 ] = new Array(-100,-100,100,100,   0,-100,100,-100 );
	AppSettArray[ 12 ] = new Array(   0,   0,  0,  0,   0,   0,  0,   0 );

	//customisability
	AppSettArray[ 13 ] = new Array(  5, -5, 10,  5,  0, 10,  0,-10 );
	AppSettArray[ 14 ] = new Array(  1,  1,  0,  0,  0,  0,  5,  0 );
	AppSettArray[ 15 ] = new Array(  7, 10,  0,  0,  0,  0,  5, 10 );

var settingsCount = AppPropArray.length+AppSettArray.length;
var productsCount = Products.length;
var choice = new Array( settingsCount );

var step = 1;
var steps = 4;

function pfInit() // Setz wohl beim starten alles auf Anfang
{
	pfResetChoice();
	for ( var i = 1; i <= steps; i++ )
	{
		var o = pfByID( 'pfStep' + i );
		o.style.display = 'none';
	}
	ResetProd();
	pfReset();		// Setzt die Comboboxen auf none
	pfSwapStep();	// Erste Seite wird erstellt
};
// Setup end -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------

function pfReset(){	// Reset der Comboboxen
	var arr = new Array('pfDefault','pfOLBest','pfOLBestPW','pfOLBestCRM','pfCRMBest','pfCRMBestOL');
	var o;
	for(var i in arr){
		o = pfByID(arr[i]);
		o.style.display = 'none';
	}	
};
function pfSubmit() {
	return false;
};
function pfByID( id ) { // Elemente des Dokuments werden ausgelesen
	return document.getElementById( id );
};
function pfSwapStep() {
	var o = pfByID( 'pfStep' + step );

	if ( o.style.display == 'none' )
		o.style.display = 'block';
	else
		o.style.display = 'none';
};
function pfNextStep( id ) { // schaltet die Seiten weiter (ist exakt das gleiche wie die nächste Funktion!?)
	if(id == 2 && !testChecked('pfID',ArrayOffset))
		alert(unescape("Bitten w%E4hlen Sie einen Anwendungsbereich aus"));
	else{
		pfSwapStep();
		step = id;//++;
		pfSwapStep();
	}
};
function pfPrevStep( id )
{
	pfSwapStep();
	step = id;//--;
	pfSwapStep();
};
function pfResetChoice() { //Löscht die Auswahl (Sowohl aus dem Anwendungbereich wie auch aus den Anderen)
	for ( var i = 0; i < settingsCount; i++ )
		choice[ i ] = false;
};
function ResetProd() {
	for ( var i = -1; i <= productsCount; i++ )
	{
		var o = pfByID( 'pfProduct' + i );
		o.style.display = 'none';
	}	
};
function pfGetRadioChoice( id, num, index ) { // Liest die Radiobuttons aus und schreibt die Auswahl in den choice Array
	for ( var i = 0; i < num; i++ )
	{
		var o = pfByID( id + i );
		choice[ index + i ] = o.checked;
		if(choice[ index + i ])
		 	var ret = index + i;
	}
	return(ret);
};
function testChecked( id, num){
var ret = false;
for ( var i = 0; i < num; i++ )
	{
		var o = pfByID( id + i );
		if( o.checked )
			ret = true;
	}
	return ret;
}
function getUserStr(){
	var ret = AppSett[pfGetRadioChoice( 'pfBr', 5, ArrayOffset+4 )-AppProp.length];
	return (ret);
}
function SumProduct(){	// Macht das gleiche wie die Exel Datei
	
	var SumAppProp = new Array( productsCount );
	var SumAppSett = new Array( productsCount );
		
	for (var ProdIndex = 0; ProdIndex < productsCount; ProdIndex++){							// For-Schleife über alle Produkte
		SumAppProp[ ProdIndex ] = 0;
		SumAppSett[ ProdIndex ] = 0;									
			
		for ( var i in AppPropArray)
			SumAppProp[ ProdIndex ] += (choice[i]) ? AppPropArray[i][ProdIndex] : 0;			// wenn Anwendungseigenschaft ausgewählt dann Summe(Auswahl & Wert)
																								// (über Anwendunsbereich, AnBu, Controlling etc.)
		if (SumAppProp[ ProdIndex ]>0){
			for ( var j = 0; j < AppSettArray.length; j++)
				SumAppSett[ ProdIndex ] += (choice[j+ArrayOffset]) ? AppSettArray[j][ProdIndex] : 0;		// wenn Summe > 0 dann wird weiter summiert
																								// (über die Einträge Arbeitsplätze, Brache ...)
			results[ ProdIndex ] = SumAppProp[ ProdIndex ]+SumAppSett[ ProdIndex ];
		}else{
			results[ ProdIndex ] = 0;
		}
	}
	//alert(SumAppProp+" - "+SumAppSett);
};
function getMaxResultValue(Indexes){ //gibt den Maximalwert aus den angegebenen Indices von results an
	var maxRes = 0;
	var maxInd = -1
	for(var i in Indexes){
		if(results[Indexes[i]]>maxRes){
			maxRes = results[Indexes[i]];
			maxInd = Indexes[i];
		}
	}
	return(maxInd);
}
function setDElement(IDs , Val) {
	for(var ID in IDs){
		if(IDs[ID] != ''){
			document.getElementById(IDs[ID]).firstChild.data = Val;	
		}
	}
}
function pfCalculate() {
	pfResetChoice();
	ResetProd();
	pfReset();
	
	var solutionString = '';
	var osString = '?';
	var workspaceString = '?';
	var userString = '?';
	
	var customString = '?';
	for ( var i = 0; i < AppProp.length; i++ )
	{
		var o = pfByID( 'pfID' + i );
		choice[ i ] = o.checked;
		if ( o.checked ) {
			solutionString += AppProp[ i ] + ', ';
		}		
	}
	
	solutionString = solutionString.substr( 0, solutionString.length - 2 );
	
	// Auswahl String wird erstellt
	workspaceString = AppSett[pfGetRadioChoice( 'pfWS', 4, ArrayOffset    )-AppProp.length];
	userString      = AppSett[pfGetRadioChoice( 'pfBr', 5, ArrayOffset+4  )-AppProp.length];
	osString		= AppSett[pfGetRadioChoice( 'pfOS', 4, ArrayOffset+9  )-AppProp.length];
	customString	= AppSett[pfGetRadioChoice( 'pfCA', 3, ArrayOffset+13 )-AppProp.length];
	
	setDElement(new Array('pfOLBr','pfUserString','pfCRMBr') , userString);
	setDElement(new Array('pfCRMId','pfOLId') , solutionString);

// Produktauswahlroutine -----------------------------------------------------------------------------------------------------------------------------
	results    = new Array( productsCount );
	SumProduct();	
	//alert(results); // Unkommentieren um mit Exeltabelle zu vergleichen ( Achtung Reihenfolge ist etwas anders) 
	Res = getMaxResultValue(new Array(0,1,2,3,4,5,6,7));
	//alert(Res);
	
	o = pfByID( 'pfProduct' + Res );	// Produktauswahl
	o.style.display = 'block';
	var CRMHeadline = 'Sage CRM';
	var DPackShot = -1;
	
	for(var Index in ProductGrps){	// Produktbeschreibung
		var ID = getMaxResultValue(ProductGrps[Index]);
		if(ID > -1){
			if(Index == 0){
				// Result in Hauptgruppe
				CRMHeadline = Products[ID];
				DPackShot = ID;
			}
			o = pfByID('pfDefault');
			o.style.display = 'block';
			
			o = pfByID(pfBestStr[ID]);
			o.style.display = 'block';
			
			//alert(pfBestStr[ID]);
			
			setDElement(new Array(pfVarBestStr[ID][0]) , Products[ ID ]);
			selID = ID;
		}
	}
	// Headline Sage CRM
	setDElement(new Array('pfCRMHead') , CRMHeadline);
	
// Ende Produktauswahl ----------------------------------------------------------------------------------------------------------------------------
	var btn = pfByID( 'pfContactButton' );
	
	if(Res > -1) {
		
		// Angebotbutton Auswahl wird gesetzt
		// Infobutton Auswahl wird gesetzt
		if(DPackShot > -1){
			btn.href = ProdLinks[ DPackShot ] + '&text=' + Products[ DPackShot ] + ' für ' + solutionString + ' für ' + userString + ' mit ' + workspaceString + ' auf dem Betriebssystem ' + osString + '. Anpassbarkeit: ' + customString;
			document.getElementById('pfName').innerHTML = 'Infopaket ' + Products[ DPackShot ] + ' anfordern';
			document.getElementById('pfCRMImg').src = Packshot[ DPackShot ] ;
		}else{
			btn.href = ProdLinks[ Res ] + '&text=' + Products[ Res ] + ' für ' + solutionString + ' für ' + userString + ' mit ' + workspaceString + ' auf dem Betriebssystem ' + osString + '. Anpassbarkeit: ' + customString;
			document.getElementById('pfName').innerHTML = 'Infopaket ' + Products[ Res ] + ' anfordern';
		}
		btn.style.display = 'block';
		
		document.getElementById('brName').innerHTML = getUserStr() ;
		
		Cufon.replace('.cufon');
		Cufon.replace('.cufon2');
		
	} else {
		btn.style.display = 'none';
	}
}
