/**
 * Wijzigingen:
 * 11/16/2011 Taco: Voor zowel het berekenen van de prijs voor "huidig pakket" als voor het berkenen van de prijs van het "uitbreiding pakket" wordt er door de gekozen "Extra modules" geloopt.
 *                  Deze "Extra modules" zijn voor alle combinaties van "Versie" en "Financiele module" hetzelfde. Toch werd in de JavaScript per "Financiele module" gezocht naar een verschillende set van
 *                  "Extra Modules". Dit was onterecht omdat de set van "Extra Modules" voor elke "Financiele Module" hetzelfde is.
 *                  Ik heb de jQuery selector die dit verschil maakt aangepast zodat het niet meer per "Financiele Module" gaat zoeken naar een verschillende set, maar dat het altijd naar dezelfde set "Extra Modules" zoekt.
 *                  Zoek op fix nummer: #001
 *
 * 
 */

$(document).ready(function(){
    $('#calcutePackagePriceHolder, body').click(function() {
        calcutePackagePrice(false);
    });

    $('#calcutePackagePriceHolder .huidig input[type="checkbox"].module').click(function(){
        copyForm(true);
    }).change(function(){
        var module = $(this);
        checkConstraints(module, '.huidig');
		checkConstraints(module, '.uitbreiding');
		calcutePackagePrice(false);
    });
    
	// Click function produces module hold on expension!
    $('#calcutePackagePriceHolder .uitbreiding input[type="checkbox"].module').click(function(){
        copyForm(false);
		/* Chrome sucks */
        var module = $(this);
        checkConstraints(module, '.uitbreiding');
		/* End of Chrome sucks, but it still sucks */
    }).change(function(){
        var module = $(this);
        checkConstraints(module, '.uitbreiding');
		calcutePackagePrice(false);
    });

    calcutePackagePrice(false);
});

/**
 * Pricetable
 */
var pricetable_fees = new Array();
pricetable_fees['CASH Financieel'] = 'a';
pricetable_fees['CASH Compact'] = 'a'; 
pricetable_fees['CASH Compact Pro'] = 'a';
pricetable_fees['CASH Facturering'] = 'a';
pricetable_fees['CASH Compact Facturering'] = 'a'; 
pricetable_fees['CASH Relatie Module (CRM)'] = 'a';
pricetable_fees['CASH Personeel'] = 'a';
pricetable_fees['CASH Consolidatie'] = 'a';
pricetable_fees['CASH Projecten'] = 'a';
pricetable_fees['CASH Handel'] = 'a';
pricetable_fees['CASH Compact Handel'] = 'a';
pricetable_fees['CASH Service & Onderhoud'] = 'b';
pricetable_fees['CASH Productie'] = 'b';

var pricetable = new Array();
pricetable['CASHWeb (online)'] = new Array();
pricetable['CASHWeb (online)']['CASH Financieel'] = 69;
pricetable['CASHWeb (online)']['CASH Facturering'] = 39;
pricetable['CASHWeb (online)']['CASH Compact Facturering'] = 35;
pricetable['CASHWeb (online)']['CASH Relatie Module (CRM)'] = 35;
pricetable['CASHWeb (online)']['CASH Personeel'] = 35;
pricetable['CASHWeb (online)']['CASH Consolidatie']= 35;
pricetable['CASHWeb (online)']['CASH Projecten'] = 49;
pricetable['CASHWeb (online)']['CASH Handel'] = 129;
pricetable['CASHWeb (online)']['CASH Compact Handel'] = 89;
pricetable['CASHWeb (online)']['CASH Service & Onderhoud'] = 49;
pricetable['CASHWeb (online)']['CASH Productie'] = 229;
pricetable['CASHWeb (online)']['CASH Compact'] = 29;
pricetable['CASHWeb (online)']['CASH Compact Pro'] = 49;

pricetable['CASHWin (offline)'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['div'] = "financieel";
pricetable['CASHWin (offline)']['CASH Financieel']['divextend'] = "uitbreiding_financieel";
pricetable['CASHWin (offline)']['CASH Financieel']['1'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Financieel'] = 1920;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Facturering'] = 1380;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Compact Facturering'] = 960;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Relatie Module (CRM)'] = 960;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Personeel'] = 960;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Consolidatie'] = 960;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Projecten'] = 1650;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Handel'] = 3540;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Compact Handel'] = 2499;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Service & Onderhoud'] = 2460;
pricetable['CASHWin (offline)']['CASH Financieel']['1']['CASH Productie'] = 5460;
pricetable['CASHWin (offline)']['CASH Financieel']['2'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Financieel'] = 2730;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Facturering'] = 1515;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Compact Facturering'] = 1095;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Relatie Module (CRM)'] = 1095;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Personeel'] = 1095;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Consolidatie'] = 1095;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Projecten'] = 1785;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Handel'] = 3825;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Compact Handel'] = 2699;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Service & Onderhoud'] = 2730;
pricetable['CASHWin (offline)']['CASH Financieel']['2']['CASH Productie'] = 6135;
pricetable['CASHWin (offline)']['CASH Financieel']['4'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Financieel'] = 3540;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Facturering'] = 1650;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Compact Facturering'] = 1230;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Relatie Module (CRM)'] = 1230;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Personeel'] = 1230;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Consolidatie'] = 1230;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Projecten'] = 1920;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Handel'] = 4110;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Compact Handel'] = 2899;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Service & Onderhoud'] = 3000;
pricetable['CASHWin (offline)']['CASH Financieel']['4']['CASH Productie'] = 6810;
pricetable['CASHWin (offline)']['CASH Financieel']['10'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Financieel'] = 4380;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Facturering'] = 1920;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Compact Facturering'] = 1500;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Relatie Module (CRM)'] = 1500;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Personeel'] = 1500;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Consolidatie'] = 1500;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Projecten'] = 2190;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Handel'] = 4650;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Compact Handel'] = 3199;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Service & Onderhoud'] = 3540;
pricetable['CASHWin (offline)']['CASH Financieel']['10']['CASH Productie'] = 8190;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply'] = new Array();
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Financieel'] = 840;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Facturering'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Compact Facturering'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Relatie Module (CRM)'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Personeel'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Consolidatie'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Projecten'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Handel'] = 540;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Compact Handel'] = 270;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Service & Onderhoud'] = 540;
pricetable['CASHWin (offline)']['CASH Financieel']['multiply']['CASH Productie'] = 1380;

pricetable['CASHWin (offline)']['CASH Compact'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact']['div'] = "compact";
pricetable['CASHWin (offline)']['CASH Compact']['divextend'] = "uitbreiding_compact";
pricetable['CASHWin (offline)']['CASH Compact']['1'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Compact'] = 960;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Facturering'] = 1380;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Compact Facturering'] = 960;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Relatie Module (CRM)'] = 960;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Personeel'] = 960;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Consolidatie'] = 960;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Projecten'] = 1650;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Handel'] = 3540;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Compact Handel'] = 2499;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Service & Onderhoud'] = 2460;
pricetable['CASHWin (offline)']['CASH Compact']['1']['CASH Productie'] = 5460;
pricetable['CASHWin (offline)']['CASH Compact']['2'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Compact'] = 1365;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Facturering'] = 1515;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Compact Facturering'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Relatie Module (CRM)'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Personeel'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Consolidatie'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Projecten'] = 1785;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Handel'] = 3825;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Compact Handel'] = 2699;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Service & Onderhoud'] = 2730;
pricetable['CASHWin (offline)']['CASH Compact']['2']['CASH Productie'] = 6135;
pricetable['CASHWin (offline)']['CASH Compact']['4'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Compact'] = 1770;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Facturering'] = 1650;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Compact Facturering'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Relatie Module (CRM)'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Personeel'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Consolidatie'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Projecten'] = 1920;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Handel'] = 4110;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Compact Handel'] = 2899;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Service & Onderhoud'] = 3000;
pricetable['CASHWin (offline)']['CASH Compact']['4']['CASH Productie'] = 6810;
pricetable['CASHWin (offline)']['CASH Compact']['10'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Compact'] = 2190;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Facturering'] = 1920;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Compact Facturering'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Relatie Module (CRM)'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Personeel'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Consolidatie'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Projecten'] = 2190;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Handel'] = 4650;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Compact Handel'] = 3199;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Service & Onderhoud'] = 3540;
pricetable['CASHWin (offline)']['CASH Compact']['10']['CASH Productie'] = 8190;

pricetable['CASHWin (offline)']['CASH Compact Pro'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact Pro']['div'] = "compactpro";
pricetable['CASHWin (offline)']['CASH Compact Pro']['divextend'] = "uitbreiding_compactpro";
pricetable['CASHWin (offline)']['CASH Compact Pro']['1'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Compact Pro'] = 1499;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Facturering'] = 1380;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Compact Facturering'] = 960;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Relatie Module (CRM)'] = 960;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Personeel'] = 960;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Consolidatie'] = 960;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Projecten'] = 1650;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Handel'] = 3540;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Compact Handel'] = 2499;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Service & Onderhoud'] = 2460;
pricetable['CASHWin (offline)']['CASH Compact Pro']['1']['CASH Productie'] = 5460;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Compact Pro'] = 1945;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Compact Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Relatie Module (CRM)'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Personeel'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Consolidatie'] = 1095;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Projecten'] = 1785;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Handel'] = 3825;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Compact Handel'] = 2699;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Service & Onderhoud'] = 2730;
pricetable['CASHWin (offline)']['CASH Compact Pro']['2']['CASH Productie'] = 6135;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Compact Pro'] = 2450;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Compact Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Relatie Module (CRM)'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Personeel'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Consolidatie'] = 1230;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Projecten'] = 1920;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Handel'] = 4110;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Compact Handel'] = 2899;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Service & Onderhoud'] = 3000;
pricetable['CASHWin (offline)']['CASH Compact Pro']['4']['CASH Productie'] = 6810;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10'] = new Array();
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Compact Pro'] = 2875;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Compact Facturering'] = 0;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Relatie Module (CRM)'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Personeel'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Consolidatie'] = 1500;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Projecten'] = 2190;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Handel'] = 4650;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Compact Handel'] = 3199;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Service & Onderhoud'] = 3540;
pricetable['CASHWin (offline)']['CASH Compact Pro']['10']['CASH Productie'] = 8190;

function resetCalculateCheckboxes()
{
    $('#calcutePackagePriceHolder .huidig input[type="checkbox"]').attr('checked', false);
}

function resetCalculateExtendCheckboxes()
{
    $('#calcutePackagePriceHolder .uitbreiding input[type="checkbox"]').attr('checked', false);
}

function checkConstraints(module, subclass)
{
	var hard_check = $('#calcutePackagePriceHolder .huidig .c_facturering').attr('checked');
	var hard_check2 = $('#calcutePackagePriceHolder .huidig .c_voorraad').attr('checked');
    var c1 = $('#calcutePackagePriceHolder ' + subclass + ' .c_facturering');
    var c2 = $('#calcutePackagePriceHolder ' + subclass + ' .c_compact_facturering');
    var c3 = $('#calcutePackagePriceHolder ' + subclass + ' .c_voorraad');
    var c4 = $('#calcutePackagePriceHolder ' + subclass + ' .c_service');
    var c5 = $('#calcutePackagePriceHolder ' + subclass + ' .c_productie');
	var c6 = $('#calcutePackagePriceHolder ' + subclass + ' .c_compact_voorraad');

    if (module.hasClass('c_facturering') && module.attr('checked') == true)
    {
        c2.attr('checked', false);
    }
	else if (module.hasClass('c_facturering') && module.attr('checked') == false)
	{
		c1.attr('checked', false);
		
		if (subclass == ".uitbreiding")
		{
			c2.attr('disabled', false);
		}
	}

    if (module.hasClass('c_compact_facturering') && module.attr('checked') == true)
    {
        c1.attr('checked', false);
    }

    if ((module.hasClass('c_service') && module.attr('checked') == true) || (module.hasClass('c_productie') && module.attr('checked') == true))
	{
        c3.attr('checked', true);
		c6.attr('checked', false);
	}

    if (module.hasClass('c_voorraad') && module.attr('checked') == true)
    {
        c6.attr('checked', false);
    }
	else if (module.hasClass('c_voorraad') && module.attr('checked') == false)
	{
		c3.attr('checked', false)
		c4.attr('checked', false);
        c5.attr('checked', false);
		
		if (subclass == ".uitbreiding")
		{
			c6.attr('disabled', false);
		}
    }
	if (module.hasClass('c_compact_voorraad') && module.attr('checked') == true)
    {
        c3.attr('checked', false);
    }

	if (subclass == ".uitbreiding")
	{
		if ( hard_check == true)
		{
			c2.attr('disabled', true);
		}
		else
		{
			c2.attr('disabled', false);
		}
		if ( hard_check2 == true)
		{
			c6.attr('disabled', true);
		}
		else
		{
			c6.attr('disabled', false);
		}
	}
	
	return true;
}

function copyForm(also_users)
{
    var versie = $('#calcutePackagePriceHolder .huidig select#versie option:selected').val();
    var pakket = $('#calcutePackagePriceHolder .huidig select#software_pakket option:selected').val();
	var pakket_extend = $('#calcutePackagePriceHolder .uitbreiding select#uitbreiding_software_pakket option:selected').val();
    
    if (versie !== undefined && versie != "" && pakket !== undefined && pakket != "" && pakket_extend !== undefined && pakket_extend != "")
    {
        if (versie == "CASHWin (offline)" && pricetable[versie][pakket] instanceof Array)
        {
            //var currents = $('#calcutePackagePriceHolder .huidig fieldset#' + pricetable[versie][pakket]['div'] + ' input[type="checkbox"]');
			//var extend = $('#calcutePackagePriceHolder .uitbreiding fieldset#' + pricetable[versie][pakket_extend]['divextend'] + ' input[type="checkbox"]');
			var currents = $('#calcutePackagePriceHolder .huidig input[type="checkbox"].module');
            var extend = $('#calcutePackagePriceHolder .uitbreiding input[type="checkbox"].module');
            
			if (also_users == true)
			{
            	var users_val = $('#calcutePackagePriceHolder .huidig select#gebruikers option:selected').val();
            	var users_extend = $('#calcutePackagePriceHolder .uitbreiding select#uitbreiding_gebruikers option');
            
				users_extend.each(function(index){
					if (users_val == $(this).val()) 
					{
						$(this).attr('selected','selected');
					}
				});
			}
        }
        else if (versie == "CASHWeb (online)")
        {
            var currents = $('#calcutePackagePriceHolder .huidig input[type="checkbox"].module');
            var extend = $('#calcutePackagePriceHolder .uitbreiding input[type="checkbox"].module');

			if (also_users == true)
			{
				var users_val = $('#calcutePackagePriceHolder .huidig input#gebruikers').val();       
				$('#calcutePackagePriceHolder .uitbreiding input#uitbreiding_gebruikers').val(users_val);
			}
        }
        else
        {
            return false;
        }

        currents.each(function(i, current){
            extend.each(function(j, copy){
                if (copy.value.toLowerCase() == current.value.toLowerCase())
                {
                    if (!copy.checked)
                        copy.checked = current.checked;
                }
            });
         });
    }
}

function calcutePackagePrice (skip_empty_users)
{
    // Get variables
    var price_div = $('#price');
    var price = 0;
    var price_month_fee = 0;
    var price_year_fee = 0;
    var uitbreiding = $('#bestel .uitbreiding');
    var versie = $('#bestel .huidig #versie option:selected');
    var pakket = $('#bestel .huidig #software_pakket option:selected');
    var hosting = $('#bestel .huidig input.hostingabonnement:checked');
    var modules = new Array();
    var module_price = 0;
    var module_fee = 0;
    var module_div = '';
    var gebruikers = '';
    var html = '<table cellpadding="0" cellspacing="2" border="0">';
    
    // Default message
    if (uitbreiding && uitbreiding.length > 0)
    {
        html += '<tr><td nowrap="nowrap">Totaal aanschaf:</td><td></td><td>&euro;</td><td><span class="price" id="new_price">{4}</span></td></tr>';
        html += '<tr><td>Inruil:</td><td>- </td><td>&euro;</td><td><span class="price" id="old_price">{1}</span></td></tr>';
    }
    else
    {
        html += '<tr><td>Aanschaf:</td><td>&euro;</td><td><span class="price" id="old_price">{1}</span></td></tr>';
        html += '<tr><td>Onderhoud:</td><td>&euro;</td><td><span class="price">{3}*</span></td></tr>';
        html += '<tr><td>Maandelijks:&nbsp;&nbsp;</td><td>&euro;</td><td><span class="price">{2}</span></td></tr>';
    }
    
    if (versie && versie.val() !== undefined && versie.val() != "" && pricetable[versie.val()] instanceof Array)
    {
        versie = versie.val();
        
        if (versie == "CASHWeb (online)")
        {
            gebruikers = $('#bestel .huidig input#gebruikers');

            if (pakket && pakket.val() !== undefined && pakket.val() != "")
            {
                pakket = pakket.val();
                            
                var modules_tmp = $('#bestel #calcutePackagePriceHolder .huidig input[type="checkbox"].module:checked');
        
                modules_tmp.each(function(index){
                    modules[index] = $(this).val();
                });

                modules[modules.length] = pakket;
                
                if (gebruikers && gebruikers.val() !== undefined && gebruikers.val() != "")
                {
                    gebruikers = parseInt(gebruikers.val());

                    if (gebruikers > 0)
                    {
                        $.each(modules, function(key, module){
                            module_price = parseInt(pricetable[versie][module]);
                            price += module_price;
                        });
                        
                        if (price > 0)
                        {
                            gebruikers = gebruikers-1;

                            price += gebruikers * 10;
                            
                            html = html.replace('{2}', price);
                        }
                    }
                }
            }
        }
        else if (versie == "CASHWin (offline)")
        {
            gebruikers = $('#bestel .huidig select#gebruikers option:selected');

            if (pakket && pakket.val() !== undefined && pakket.val() != "" && pricetable[versie][pakket.val()] instanceof Array)
            {
                pakket = pakket.val();
                module_div = pricetable[versie][pakket]['div'];

                // Fix: #001
                //var modules_tmp = $('#bestel #calcutePackagePriceHolder .huidig #' + module_div + ' input[type="checkbox"].module:checked');
                var modules_tmp = $('#bestel #calcutePackagePriceHolder .huidig #financieel input[type="checkbox"].module:checked');
        
                modules_tmp.each(function(index){
                    modules[index] = $(this).val();
                });

                modules[modules.length] = pakket;
                
                if (gebruikers && gebruikers.val() !== undefined && gebruikers.val() != "")
                {
                    gebruikers = gebruikers.val();
                    gebruikers = gebruikers.split('-');
                    gebruikers = parseInt(gebruikers[gebruikers.length-1]);
                    
                    if (gebruikers <= 10 && pricetable[versie][pakket][gebruikers] instanceof Array)
                    {
                        $.each(modules, function(key, module){
                            module_price = parseInt(pricetable[versie][pakket][gebruikers][module]);
                            price += module_price;

                            if (pricetable_fees[module] !== undefined)
                            {
                                if (pricetable_fees[module] == "a")
                                    module_fee = "16.5";

                                if (pricetable_fees[module] == "b")
                                    module_fee = "22";

                                price_year_fee += (module_price / 100) * module_fee;
                            }
                            
                            price_multiply = 0;
                            module_fee = 0;
                            module_price = 0;
                        });
                    }
                    else if (gebruikers > 10)
                    {
                        $.each(modules, function(key, module){
                            module_price = parseInt(pricetable[versie][pakket]['10'][module]);
                            price += module_price;
                            multiply = parseInt((gebruikers - 10) / 6);

                            if (pricetable[versie][pakket]['multiply'] instanceof Array && pricetable[versie][pakket]['multiply'][module] !== undefined)
                            {
                                price_multiply = parseInt(pricetable[versie][pakket]['multiply'][module]) * multiply;
                                price += price_multiply;
                                module_price += price_multiply;
                            }

                            if (pricetable_fees[module] !== undefined)
                            {
                                if (pricetable_fees[module] == "a")
                                    module_fee = "16.5";

                                if (pricetable_fees[module] == "b")
                                    module_fee = "22";

                                price_year_fee += (module_price / 100) * module_fee;
                            }
                            
                            price_multiply = 0;
                            module_fee = 0;
                            module_price = 0;
                        });
                    }
                    
                    if (price > 0)
                    {
                        if (hosting && hosting.val() !== undefined && hosting.val() == "Ja")
                        {
                            price_month_fee += 17.50;
                        }
                        
                        html = html.replace('{1}', price);

                        if (price_month_fee > 0 || price_year_fee > 0)
                        {
                            price_year_fee = price_year_fee.toFixed(2);
                            price_year_fee = price_year_fee.replace(".", ",");

                            if (price_year_fee < 1)
                            {
                                html = html.replace('{2}', price_month_fee);
                            }
                            else if (price_month_fee < 1)
                            {
                                html = html.replace('{3}', price_year_fee);
                            }
                            else
                            {
                                html = html.replace('{2}', price_month_fee);
                                html = html.replace('{3}', price_year_fee);
                            }
                        }
                    }
                }
            }
        }
    }

    html = html.replace('{1}','0');
    html = html.replace('{2}','0');
    html = html.replace('{3}','0');
    
    if (uitbreiding && uitbreiding.length > 0)
    {
        calcutePackageExtendPrice(html, price_div, versie, gebruikers, skip_empty_users);
    }
    else
    {
        html += '</table>';

        price_div.html(html);
    }
}

function calcutePackageExtendPrice (html, el, versie, current_amount_users, skip_empty_users)
{
    var price = 0;
    var price_month_fee = 0;
    var price_year_fee = 0;
    var pakket = $('#bestel #uitbreiding_software_pakket option:selected');
    var hosting = $('#bestel .uitbreiding input.hostingabonnement:checked');
    var modules = new Array();
    var module_price = 0;
    var module_fee = 0;
    var module_div = '';
    var gebruikers = '';

    html += '<tr><td>Totaal:</td><td></td><td>&euro;</td><td><span class="price">{7}</span></td></tr>';
    html += '<tr><td colspan="4"class="totaal"></td></tr>';
    html += '<tr><td>Onderhoud:</td><td></td><td>&euro;</td><td><span class="price">{6}*</span></td></tr>';
    html += '<tr><td>Maandelijks:</td><td></td><td>&euro;</td><td><span class="price">{5}</span></td></tr>';
    
    if (versie && versie != "")
    {
        if (versie == "CASHWeb (online)")
        {
            gebruikers_el = $('#bestel .uitbreiding input#uitbreiding_gebruikers');

            if (pakket && pakket.val() !== undefined && pakket.val() != "")
            {
                pakket = pakket.val();

                var modules_tmp = $('#bestel #calcutePackagePriceHolder .uitbreiding #uitbreiding_financieel input[type="checkbox"].module:checked');

                modules_tmp.each(function(index){
                    modules[index] = $(this).val();
                });

                modules[modules.length] = pakket;
              
                if (gebruikers_el && gebruikers_el.val() !== undefined)
                {
					if (skip_empty_users != true && gebruikers_el.val() == "")
					{
						gebruikers_el.val(1);
					}

                    gebruikers = parseInt(gebruikers_el.val());

                    if (gebruikers > 0)
                    {
						var user_check_value = parseInt(current_amount_users)+1;

						if (user_check_value && gebruikers < user_check_value)
						{
							var gebruikers_el = $('#bestel .uitbreiding input#uitbreiding_gebruikers');
							
							gebruikers_el.val(user_check_value);
							gebruikers = user_check_value;
						}
						
                        $.each(modules, function(key, module){
                            module_price = parseInt(pricetable[versie][module]);
                            price += module_price;
                        });
                        
                        if (price > 0)
                        {
                            gebruikers = gebruikers-1;

                            price += gebruikers * 10;
                            
                            html = html.replace('{5}', price);
                        }
                    }
                }
            }
        }
        else if (versie == "CASHWin (offline)")
        {
            gebruikers = $('#bestel .uitbreiding select#uitbreiding_gebruikers option:selected');

            if (pakket && pakket.val() !== undefined && pakket.val() != "" && pricetable[versie][pakket.val()] instanceof Array)
            {
                pakket = pakket.val();
                module_div = pricetable[versie][pakket]['divextend'];

                // Fix: #001
                //var modules_tmp = $('#bestel #' + module_div + ' input[type="checkbox"].module:checked');
                var modules_tmp = $('#bestel .uitbreiding #uitbreiding_financieel input[type="checkbox"].module:checked');
        
                modules_tmp.each(function(index){
                    modules[index] = $(this).val();
                });

                modules[modules.length] = pakket;
                
                if (gebruikers && gebruikers.val() !== undefined && gebruikers.val() != "")
                {
                    gebruikers = gebruikers.val();
                    gebruikers = gebruikers.split('-');
                    gebruikers = parseInt(gebruikers[gebruikers.length-1]);
					
					if (gebruikers < current_amount_users)
					{
						gebruikers_options = $('#bestel .uitbreiding select#uitbreiding_gebruikers option');
						gebruikers_options.each(function(index){
							tmp_users = $(this).val();
							tmp_users = tmp_users.split('-');
							tmp_users = parseInt(tmp_users[tmp_users.length-1]);

							if (current_amount_users == tmp_users) 
							{
								$(this).attr('selected','selected');
							}
						});
					}

					if (gebruikers <= 10 && pricetable[versie][pakket][gebruikers] instanceof Array)
					{
						$.each(modules, function(key, module){
							module_price = parseInt(pricetable[versie][pakket][gebruikers][module]);
							price += module_price;

							if (pricetable_fees[module] !== undefined)
							{
								if (pricetable_fees[module] == "a")
									module_fee = "16.5";

								if (pricetable_fees[module] == "b")
									module_fee = "22";

								price_year_fee += (module_price / 100) * module_fee;
							}
							
							price_multiply = 0;
							module_fee = 0;
							module_price = 0;
						});
					}
					else if (gebruikers > 10)
					{
						$.each(modules, function(key, module){
							module_price = parseInt(pricetable[versie][pakket]['10'][module]);
							price += module_price;
							multiply = parseInt((gebruikers - 10) / 6);

							if (pricetable[versie][pakket]['multiply'] instanceof Array && pricetable[versie][pakket]['multiply'][module] !== undefined)
							{
								price_multiply = parseInt(pricetable[versie][pakket]['multiply'][module]) * multiply;
								price += price_multiply;
								module_price += price_multiply;
							}

							if (pricetable_fees[module] !== undefined)
							{
								if (pricetable_fees[module] == "a")
									module_fee = "16.5";

								if (pricetable_fees[module] == "b")
									module_fee = "22";

								price_year_fee += (module_price / 100) * module_fee;
							}
							
							price_multiply = 0;
							module_fee = 0;
							module_price = 0;
						});
					}
					
					if (price > 0)
					{                      
						if (hosting && hosting.val() !== undefined && hosting.val() == "Ja, online abonnement" || hosting && hosting.val() !== undefined && hosting.val() == "Ja, offline abonnement")
						{
							price_month_fee += 17.50;
						}
						
						html = html.replace('{4}', price);

						if (price_month_fee > 0 || price_year_fee > 0)
						{
							price_year_fee = price_year_fee.toFixed(2);
							price_year_fee = price_year_fee.replace(".", ",");

							if (price_year_fee < 1)
							{
								html = html.replace('{5}', price_month_fee);
							}
							else if (price_month_fee < 1)
							{
								html = html.replace('{6}', price_year_fee);
							}
							else
							{
								html = html.replace('{5}', price_month_fee);
								html = html.replace('{6}', price_year_fee);
							}
						}
						
						el.show();
					}
                }
            }
        }
    }
    
    html = html.replace('{4}','0');
    html = html.replace('{5}','0');
    html = html.replace('{6}','0');
    
    el.html(html);
    
    // Substract old from new
    var old_price = $('#old_price', el).html();
    old_price = parseInt(old_price.replace(/[^\d\.]+/g, ''));
    var new_price = $('#new_price', el).html();
    new_price = parseInt(new_price.replace(/[^\d\.]+/g, ''));
    
    if (old_price > new_price)
        new_price = 0;
    else
        new_price = new_price - old_price;

    html = html.replace('{7}', new_price);

    html += '</table>';

    el.html(html);
}
