// Enrique Delgado
// December, 2008
// Requires: jQuery, json2

var q = {
selections : {},
    currentQuote : {},
    lookupTable : {},
    sidebar: null,
    warning_container: null,
    call_us_warning: null,
    total: 0.00,
    total_annual_cost: 0,
    init : function(container_id, sidebar_id, sidebar_container_id, warning_container_id, product_data) {
        // Degrade console object if FireBug is not available
        try { console.log('init console within class... done'); this.console = console; } catch(e) { this.console = { log: function() {} } }
        // Event delegation, there is more than one way to do this, examples:
        //    $('#' + container_id + ' input[type="radio"]').click(this.recordSelection);
        //    $('#' + container_id + ' input[type="text"]').change(this.recordSelection);
        //    $('#' + container_id + ' input').change(this.recordSelection);
        $('#' + container_id + ' input[type="text"]').keyup(this.recordSelection);
        $('#' + container_id + ' input[type="radio"]').click(this.recordSelection);
        $('#' + container_id + ' input[type="checkbox"]').click(this.recordSelection);
        $('#' + container_id + ' select').change(this.recordSelection);
        $('#token_type').change(this.recordSelection);
        // Set lookup table's data
        this.lookupTable = product_data || {};
        // remember the sidebar wrapper DOM id
        this.sidebar = sidebar_id;
        this.warning_container = warning_container_id;
        // Make configuration details move appropriately
        this.sidebar_container_id = sidebar_container_id;
        this.sidebar_start_location = 200; 
        this.sidebar_curr_location  = 230; 
        this.sidebar_dest_location  = 0; 
        this.sidebar_timeout_var;
        this.is_sidebar_scrolling = false;
        this.sidebar_container = $('#' + this.sidebar_container_id)[0];
        this.sidebar_container.style.top = this.sidebar_start_location+"px";
        this.sidebar_container.style.position = 'absolute';
        $(window).scroll(q.scrollConfigInfo);
        // Dump lookupTable to console:
        this.console.log(this.lookupTable);
        // Set call_us_warning message text:
        this.call_us_warning = '<li><strong>Please call us at 1 888 881 3560, or +1 858 457 8299</strong><br />Your configuration may require more than one Switchvox.</li>';
    },
    load : function(config_string) {
        // Takes a JSON object and configures page appropriately
        //var c = JSON.parse(config_string);
        //console.log(c);
        // Temporarily unbind the event delegation
        // Set fields
        // Re-bind event delegation
        // Calculate-quote
    },
    generateConfigString : function() {
	var myJSONText = JSON.stringify(q.currentQuote);
	//$("#configuration").attr("value", myJSONText);
	return myJSONText;
    },
    recordSelection : function(e) {
        // Get the element that delegated the event to us:
        var t = $(e.target);
        // Save selection in this objects "selections" property
        //      Hmm... "this" is no longer the "q" object, but 
        //      apparently, "q" is a valid object within itself, 
        //      so just use that:
        switch (t[0].type) {
            case 'checkbox':
                // See if it's a checkbox, those are a little different:
                q.selections[t.attr('name')] = (t[0].checked ? t.val() : 0);
                break;
            case 'text':
                // If the value is empty, then ignore (most likely is the user clearing the field to begin typing)
                // Make sure it's a number:
                if(!isNaN(parseInt(t.val())) && parseInt(t.val()) >= 0) {
                    // It is, so record the selection:
                    if (t.val() > 500) {
                        alert('The maximum value for this field has been reached.');
                        t.val(q.selections[t.attr('name')]);
                        return;
                    }
                    q.selections[t.attr('name')] = t.val();
                } else {
                    // Is it because it's empty?
                    if (t.val() == '') {
                        // Assume a value of zero:
                        q.selections[t.attr('name')] = 0;
                    } else {
                        // It is not, so restore the previous value:
                        alert('Please enter a value of zero or greater in this field.');
                        t.val(q.selections[t.attr('name')]);
                    }
                }
                break;
            default:
                q.selections[t.attr('name')] = t.val();
        }
        // See if it's a checkbox, those are a little different:
        if (t[0].type == 'checkbox') {
            q.selections[t.attr('name')] = (t[0].checked ? t.val() : 0);
        } else {
            q.selections[t.attr('name')] = t.val();
        }
        // Debug existing data structure:
        //q.console.log(q.selections);
        // Compute quote:
        q.calculateQuote();
        q.revealQuestions();
    },
    calculateQuote : function() {
        // Reset quote:
        //q.currentQuote = {};
        // Reset total:
        q.total = 0.00;
	// Remove any existing warnings:
	$('#' + q.warning_container).html('');
    
        switch (q.selections.q0) {
            case 'soho' : 
            	q.currentQuote.chasis = this.addToQuote('1AS601000LF');
                break;
            case 'smb' :
            	q.currentQuote.chasis = this.addToQuote('1AS650000LF');
                break;
            case 'smb305' :
            	q.currentQuote.chasis = this.addToQuote('1AS3050000LF');
                break;
            case 'smb355' :
            	q.currentQuote.chasis = this.addToQuote('1AS3550000LF');
                break;
        }
        
        // Chasis Feature Caveats
        if ((q.selections.q7_1 && q.selections.q7_1 == '1') ||
            (q.selections.q7_2 && q.selections.q7_2 == '1') ||
            (q.selections.q7_3 && q.selections.q7_3 == '1')) {
                q.currentQuote.chasis = this.addToQuote('1AS3550000LF');
        }
        
        // Analog lines
        if (q.selections.q2) {
            delete q.currentQuote.analog_modules;
            switch (q.selections.q2) {
                case '0' : delete q.currentQuote.analog;
                    break;
                case '1' : q.currentQuote.analog = this.addToQuote('1TDM404EF');
                    break;
                case '2' : q.currentQuote.analog = this.addToQuote('1TDM808EF');
                    break;
                case '3' :
                        if (q.currentQuote.chasis.product_code == '1AS650000LF' ||
                            q.currentQuote.chasis.product_code == '1AS601000LF') {
                           q.currentQuote.analog = this.addToQuote('1TDM404EF');
                           q.currentQuote.analog_modules = this.addToQuote('1TDM808EF');
                        } else {
                          q.currentQuote.analog = this.addToQuote('1TDM2400ELF');
                          q.currentQuote.analog_modules = this.addToQuote('1X400MF', 4)
                        }
                    break;
                case '4' : 
                    q.currentQuote.analog = this.addToQuote('1TDM2406EF');
                    break;
            }
        }

        if (q.currentQuote.analog) {
          if (q.selections.cables == "add" && (q.currentQuote.analog.product_code == '1TDM2406EF' || q.currentQuote.analog.product_code == '1TDM2400ELF')) {
              q.currentQuote.cables = this.addToQuote('1ACC24PPP');
              q.currentQuote.patch_panel = this.addToQuote('3113-00002');
          } else {
              if (q.currentQuote.cables) delete q.currentQuote.cables;
              if (q.currentQuote.patch_panel) delete q.currentQuote.patch_panel;
        
          }
        }

        
        // Voice T1s
        if (q.selections.q3) {
            switch (q.selections.q3) {
                case '0' : delete q.currentQuote.voice_t1;
                    break;
                case '1' :
                    // You're not allowed to have a voice T1 if you have used all of the slots up for analog lines.
                    if (q.currentQuote.chasis.product_code == '1AS650000LF' && q.selections.q2 == '3') {
                      delete q.currentQuote.voice_t1
                    } else {
                      q.currentQuote.voice_t1 = this.addToQuote('1TE122BF');
                    }
                    break;
                case '2' : 
                    // Bump up chasis to AA300 if they are currently on an AA60:
                    // if (q.currentQuote.chasis && q.currentQuote.chasis.product_code == '1AS650000LF') {
                        // delete q.currentQuote.chasis;
                        // q.currentQuote.chasis = this.addToQuote('1AS3050000LF');
                    // }
                    q.currentQuote.voice_t1 = this.addToQuote('1TE207PF');
                    break;
                case '3' : 
                    // Bump up chasis to AA350 if they are currently on an AA60, or AA300:
                    // if (q.currentQuote.chasis && (q.currentQuote.chasis.product_code == '1AS650000LF' || q.currentQuote.chasis.product_code == '1AS3050000LF')) {
                        // delete q.currentQuote.chasis;
                        // q.currentQuote.chasis = this.addToQuote('1AS3550000LF');
                    // }
                    q.currentQuote.voice_t1 = this.addToQuote('1TE407PF');
                    break;
            }
        }
        
        // Phones
        if (q.selections.q5_1 && parseInt(q.selections.q5_1) > 0) {
            q.currentQuote.phone_331 = this.addToQuote('1TELPLC331LF', q.selections.q5_1);
        } else if (q.currentQuote.phone_331) {
            delete q.currentQuote.phone_331;
        }
        if (q.selections.q5_8 && parseInt(q.selections.q5_8) > 0) {
            q.currentQuote.phone_335 = this.addToQuote('1TELPLC335LF', q.selections.q5_8);
        } else if (q.currentQuote.phone_335) {
            delete q.currentQuote.phone_335;
        }
        if (q.selections.q5_9 && parseInt(q.selections.q5_9) > 0) {
            q.currentQuote.phone_430 = this.addToQuote('1TELPLC430LF', q.selections.q5_9);
        } else if (q.currentQuote.phone_430) {
            delete q.currentQuote.phone_430;
        }
        if (q.selections.q5_3 && parseInt(q.selections.q5_3) > 0) {
            q.currentQuote.phone_450 = this.addToQuote('1TELPLC450LF', q.selections.q5_3);
        } else if (q.currentQuote.phone_450) {
            delete q.currentQuote.phone_450;
        }
        if (q.selections.q5_4 && parseInt(q.selections.q5_4) > 0) {
            q.currentQuote.phone_550 = this.addToQuote('1TELPLC550LF', q.selections.q5_4);
        } else if (q.currentQuote.phone_550) {
            delete q.currentQuote.phone_550;
        }
        if (q.selections.q5_10 && parseInt(q.selections.q5_10) > 0) {
            q.currentQuote.phone_560 = this.addToQuote('1TELPLC560LF', q.selections.q5_10);
        } else if (q.currentQuote.phone_560) {
            delete q.currentQuote.phone_560;
        }
        if (q.selections.q5_5 && parseInt(q.selections.q5_5) > 0) {
            q.currentQuote.phone_6000 = this.addToQuote('1TELPLC6000LF', q.selections.q5_5);
        } else if (q.currentQuote.phone_6000) {
            delete q.currentQuote.phone_6000;
        }
        if (q.selections.q5_6 && parseInt(q.selections.q5_6) > 0) {
            q.currentQuote.phone_650 = this.addToQuote('1TELPLC650LF', q.selections.q5_6);
        } else if (q.currentQuote.phone_650) {
            delete q.currentQuote.phone_650;
        }
        if (q.selections.q5_7 && parseInt(q.selections.q5_7) > 0) {
            q.currentQuote.phone_7000 = this.addToQuote('1TELPLC7000LF', q.selections.q5_7);
        } else if (q.currentQuote.phone_7000) {
            delete q.currentQuote.phone_7000;
        }
        if (q.selections.q5_11 && parseInt(q.selections.q5_11) > 0) {
            q.currentQuote.phone_650_expansion = this.addToQuote('1TELPLC650EXLF', q.selections.q5_11);
        } else if (q.currentQuote.phone_650_expansion) {
            delete q.currentQuote.phone_650_expansion;
        }
        if (q.selections.q5_12 && parseInt(q.selections.q5_12) > 0) {
            q.currentQuote.phone_670_expansion = this.addToQuote('1TELPLC670EXLF', q.selections.q5_12);
        } else if (q.currentQuote.phone_670_expansion) {
            delete q.currentQuote.phone_670_expansion;
        }
        if (q.selections.q5_13 && parseInt(q.selections.q5_13) > 0) {
            q.currentQuote.phone_vvx1500 = this.addToQuote('1TELVVX1500LF', q.selections.q5_13);
        } else if (q.currentQuote.phone_vvx1500) {
            delete q.currentQuote.phone_vvx1500;
        }
        if (q.selections.q5_14 && parseInt(q.selections.q5_14) > 0) {
            q.currentQuote.phone_670 = this.addToQuote('1TELPLC670LF', q.selections.q5_14);
        } else if (q.currentQuote.phone_670) {
            delete q.currentQuote.phone_670;
        }
        
        // Phone tokens
        if (q.selections.q8) {
            if ((q.selections.token_type && q.selections.token_type == "Polycom") || $('#token_type').val() == "Polycom") {
                q.currentQuote.phone_token = this.addToQuote('1SWXPPFPPCOM1', q.selections.q8);
            } else {
                q.currentQuote.phone_token = this.addToQuote('1SWXPPROVSNOM1', q.selections.q8);
            }
        } else if (q.currentQuote.phone_token) {
            delete q.currentQuote.phone_token;
        }


        // Support Options
        if (q.selections.q6 && q.selections.q1) {
            // If they have more than 10 users:
            if (q.selections.q1 > 10) {
                // Calculate subscription packs
                var totalSubs = parseInt(q.selections.q1) - 10;
                //q.console.log(totalSubs + ' remaining subscriptions.');
                var sub100pack = Math.floor(totalSubs / 100);
                totalSubs -= sub100pack * 100;
                var sub25pack = Math.floor(totalSubs / 25);                     
                totalSubs -= sub25pack * 25;
                var sub5pack = Math.floor(totalSubs / 5);                      
                totalSubs -= sub5pack * 5;
                var subSingle = totalSubs;
                // Remove any existing packs:
                if (q.currentQuote.sub_100_pack)  delete q.currentQuote.sub_100_pack;
                if (q.currentQuote.sub_25_pack)   delete q.currentQuote.sub_25_pack;
                if (q.currentQuote.sub_5_pack)    delete q.currentQuote.sub_5_pack;
                if (q.currentQuote.sub_single)    delete q.currentQuote.sub_single;
                
                switch (q.selections.q6) {
                    case '1' : // Silver
                        // Remove any exsiting inculded applicance subscriptions to gold or platinum:
                        if (q.currentQuote.sil_2_plt_upg) delete q.currentQuote.sil_2_plt_upg;
                        if (q.currentQuote.sil_2_gld_upg) delete q.currentQuote.sil_2_gld_upg;
                        
                        // Add subscriptions
                        if (sub100pack) {
                            q.currentQuote.sub_100_pack = this.addToQuote('1SWXSSUB100', sub100pack);
                        } else if (q.currentQuote.sub_100_pack) delete q.currentQuote.sub_100_pack;
                        
                        if (sub25pack) {
                            q.currentQuote.sub_25_pack = this.addToQuote('1SWXSSUB25', sub25pack);
                        } else if (q.currentQuote.sub_25_pack) delete q.currentQuote.sub_25_pack;
                        
                        if (sub5pack) {
                            q.currentQuote.sub_5_pack = this.addToQuote('1SWXSSUB5', sub5pack);
                        } else if (q.currentQuote.sub_5_pack) delete q.currentQuote.sub_5_pack;
                        
                        if (subSingle) {
                            q.currentQuote.sub_single = this.addToQuote('1SWXSSUB1', subSingle);
                            q.total += q.currentQuote.sub_single.price * q.currentQuote.sub_single.qty;
                        } else if (q.currentQuote.sub_single) delete q.currentQuote.sub_single;

                        break;
                    case '2' : // Gold
                        // Remove any exsiting inculded applicance subscriptions to platinum:
                        if (q.currentQuote.sil_2_plt_upg) delete q.currentQuote.sil_2_plt_upg;
                        // Upgrade the applicance included subscriptions to gold:
                        q.currentQuote.sil_2_gld_upg = this.addToQuote('1SWXS2GSUB5', 2);

                        // Add remaining subscriptions:
                        if (sub100pack) {
                            q.currentQuote.sub_100_pack = this.addToQuote('1SWXGSUB100', sub100pack);         
                        } else if (q.currentQuote.sub_100_pack) delete q.currentQuote.sub_100_pack;
                        
                        if (sub25pack) {
                            q.currentQuote.sub_25_pack = this.addToQuote('1SWXGSUB25', sub25pack);
                        } else if (q.currentQuote.sub_25_pack) delete q.currentQuote.sub_25_pack;

                        if (sub5pack) {
                            q.currentQuote.sub_5_pack = this.addToQuote('1SWXGSUB5', sub5pack);
                        } else if (q.currentQuote.sub_5_pack) delete q.currentQuote.sub_5_pack;

                        if (subSingle) {
                            q.currentQuote.sub_single = this.addToQuote('1SWXGSUB1', subSingle);
                        } else if (q.currentQuote.sub_single) delete q.currentQuote.sub_single;

                        break;
                    case '3' : //Platinum
                        // Remove any existing inculded appliance subscriptions to gold:
                        if (q.currentQuote.sil_2_gld_upg) delete q.currentQuote.sil_2_gld_upg;
                        // Upgrade the appliance included subscriptions to platinum:
                        q.currentQuote.sil_2_plt_upg = this.addToQuote('1SWXS2PSUB5', 2);
                        
                        // Add remaining subscriptions:
                        if (sub100pack) {
                            q.currentQuote.sub_100_pack = this.addToQuote('1SWXPSUB100', sub100pack);
                        } else if (q.currentQuote.sub_100_pack) delete q.currentQuote.sub_100_pack;

                        if (sub25pack) {
                            q.currentQuote.sub_25_pack = this.addToQuote('1SWXPSUB25', sub25pack);
                        } else if (q.currentQuote.sub_25_pack) delete q.currentQuote.sub_25_pack;

                        if (sub5pack) {
                            q.currentQuote.sub_5_pack = this.addToQuote('1SWXPSUB5', sub5pack);
                        } else if (q.currentQuote.sub_5_pack) delete q.currentQuote.sub_5_pack;

                        if (subSingle) {
                            q.currentQuote.sub_single = this.addToQuote('1SWXPSUB1', subSingle);
                        } else if (q.currentQuote.sub_single) delete q.currentQuote.sub_single;

                        break;
                }
                
            } else if (q.selections.q1 > 0) { // If they have 1 to 10 users
                if (q.currentQuote.sub_100_pack)  delete q.currentQuote.sub_100_pack;
                if (q.currentQuote.sub_25_pack)   delete q.currentQuote.sub_25_pack;
                if (q.currentQuote.sub_5_pack)    delete q.currentQuote.sub_5_pack;
                if (q.currentQuote.sub_single)    delete q.currentQuote.sub_single;
                switch (q.selections.q6) {
                    case '1' : // Silver
                        if (q.currentQuote.sil_2_plt_upg) delete q.currentQuote.sil_2_plt_upg;
                        if (q.currentQuote.sil_2_gld_upg) delete q.currentQuote.sil_2_gld_upg;
                        break;
                    case '2' : // Gold
                        if (q.currentQuote.sil_2_plt_upg) delete q.currentQuote.sil_2_plt_upg;
                        q.currentQuote.sil_2_gld_upg = this.addToQuote('1SWXS2GSUB5', 2);
                        break;
                    case '3' : // Platinum
                        if (q.currentQuote.sil_2_gld_upg) delete q.currentQuote.sil_2_gld_upg;
                        q.currentQuote.sil_2_plt_upg = this.addToQuote('1SWXS2PSUB5', 2);
                        break;
                }
            }
        } else {
            // Remove any existing subscriptions:
            if (q.currentQuote.sil_2_plt_upg) delete q.currentQuote.sil_2_plt_upg;
            if (q.currentQuote.sil_2_gld_upg) delete q.currentQuote.sil_2_gld_upg;
            if (q.currentQuote.sub_100_pack)  delete q.currentQuote.sub_100_pack;
            if (q.currentQuote.sub_25_pack)   delete q.currentQuote.sub_25_pack;
            if (q.currentQuote.sub_5_pack)    delete q.currentQuote.sub_5_pack;
            if (q.currentQuote.sub_single)    delete q.currentQuote.sub_single;
        }

        // Figure out whether we need to add a multiyear subscription to the order
        if (q.selections.renewal_years === "2") {
            this.calculateMultiyearCosts(2, true);
        } else if (q.selections.renewal_years === "4") {
            this.calculateMultiyearCosts(4, true);
        } else {
            if (q.currentQuote.sub_renewal_100_pack)  delete q.currentQuote.sub_renewal_100_pack;
            if (q.currentQuote.sub_renewal_25_pack)   delete q.currentQuote.sub_renewal_25_pack;
            if (q.currentQuote.sub_renewal_5_pack)    delete q.currentQuote.sub_renewal_5_pack;
            if (q.currentQuote.sub_renewal_single)    delete q.currentQuote.sub_renewal_single;
            if (q.currentQuote.sub_maintenance)    delete q.currentQuote.sub_maintenance;
        } 

        // Add the extended warranty if they want it
        if (q.selections.ext_warranty == "3yr") {
            if (q.currentQuote.ext_warranty) delete q.currentQuote.ext_warranty;
		    if (q.currentQuote.chasis.product_code == '1AS650000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('8SWX3YRA65');
            } else if (q.currentQuote.chasis.product_code == '1AS601000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('8SWX3YRA60');
            } else if (q.currentQuote.chasis.product_code == '1AS3050000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('8SWX3YRA305');
            } else if (q.currentQuote.chasis.product_code == '1AS3550000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('8SWX3YRA355');
            }
        } else if (q.selections.ext_warranty == "5yr") {
            if (q.currentQuote.ext_warranty) delete q.currentQuote.ext_warranty;
            if (q.currentQuote.chasis.product_code == '1AS650000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('803-00002');
            } else if (q.currentQuote.chasis.product_code == '1AS3050000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('803-00003');
            } else if (q.currentQuote.chasis.product_code == '1AS3550000LF') {
                q.currentQuote.ext_warranty = this.addToQuote('803-00004');
            }
        } else {
            if (q.currentQuote.ext_warranty) delete q.currentQuote.ext_warranty;
        }
        
        if (q.selections.rack_mount == "on") {
            this.currentQuote.rack_mount = this.addToQuote("1AS3500101LF");
        } else {
            if (q.currentQuote.rack_mount) delete q.currentQuote.rack_mount;
        }

        if (q.selections.wall_mount == "on") {
            this.currentQuote.wall_mount = this.addToQuote("1AS3050100LF");
        } else {
            if (q.currentQuote.wall_mount) delete q.currentQuote.wall_mount;
        }

        // Figure out if we need to add a cold spare for the chassis, this needs to be last
        if (q.selections.cold_spare == "on") {
            this.addColdSpare();
        } else {
            if (q.currentQuote.cold_spare) delete q.currentQuote.cold_spare;
        }

        // Recalculate total
        q.total = 0;
        jQuery.each(q.currentQuote, function(i, val) {
            q.total += val.price * val.qty;
        });
        // Update quote sidebar here.
        this.refreshQuote();
        //this.console.log(q.currentQuote);
        //this.console.log(q);
    },
    addColdSpare: function() {
        if (q.currentQuote.cold_spare) delete q.currentQuote.cold_spare;

        //Add stuff to determine cold spare here
		if (q.currentQuote.chasis.product_code == '1AS650000LF') {
            q.currentQuote.cold_spare = this.addToQuote('1AS650004LF');
        } else if (q.currentQuote.chasis.product_code == '1AS601000LF') {
            q.currentQuote.cold_spare = this.addToQuote('1AS601004LF');
        } else if (q.currentQuote.chasis.product_code == '1AS3050000LF') {
            q.currentQuote.cold_spare = this.addToQuote('1AS3050004LF');
        } else if (q.currentQuote.chasis.product_code == '1AS3550000LF') {
            q.currentQuote.cold_spare = this.addToQuote('1AS3550004LF');
        }

        // Double up on cards
        if (q.currentQuote.analog) {
            q.currentQuote.analog.qty = q.currentQuote.analog.qty * 2
        }

        if (q.currentQuote.voice_t1) {
            q.currentQuote.voice_t1.qty = q.currentQuote.voice_t1.qty * 2
        }
        
        if (q.currentQuote.analog_modules) {
            q.currentQuote.analog_modules.qty = q.currentQuote.analog_modules.qty * 2
        }
        // Double up on warranty
        if (q.currentQuote.ext_warranty) {
            q.currentQuote.ext_warranty.qty = q.currentQuote.ext_warranty.qty * 2
        }
        
        if (q.currentQuote.cables) {
            q.currentQuote.cables.qty = q.currentQuote.cables.qty * 2
        }
        if (q.currentQuote.patch_panel) {
            q.currentQuote.patch_panel.qty = q.currentQuote.patch_panel.qty * 2
        }
        
        if (q.currentQuote.rack_mount) {
            q.currentQuote.rack_mount.qty = q.currentQuote.rack_mount.qty * 2
        }
        
        if (q.currentQuote.wall_mount) {
            q.currentQuote.wall_mount.qty = q.currentQuote.wall_mount.qty * 2
        }
    },
    calculateMultiyearCosts: function(year, add_to_quote) {
        if (add_to_quote == true) {
            if (q.currentQuote.sub_renewal_100_pack)  delete q.currentQuote.sub_renewal_100_pack;
            if (q.currentQuote.sub_renewal_25_pack)   delete q.currentQuote.sub_renewal_25_pack;
            if (q.currentQuote.sub_renewal_5_pack)    delete q.currentQuote.sub_renewal_5_pack;
            if (q.currentQuote.sub_renewal_single)    delete q.currentQuote.sub_renewal_single;
            if (q.currentQuote.sub_maintenance)       delete q.currentQuote.sub_maintenance;
        }        

        if (q.currentQuote.chasis.product_code == '1AS601000LF') {
            var updates = this.addToQuote("1SWXSOHOR" + year).price;
        } else {
            var updates = this.addToQuote("1SWXSMBR" + year).price;
        }
        
        if (add_to_quote == true) {
            if (q.currentQuote.chasis.product_code == '1AS601000LF') {
                q.currentQuote.sub_maintenance = this.addToQuote("1SWXSOHOR" + year);
            } else {
                q.currentQuote.sub_maintenance = this.addToQuote("1SWXSMBR" + year);
            }
            
        }
        var totalSubCost = 0;

        if (q.currentQuote.sil_2_plt_upg) {
            totalSubCost += this.addToQuote("1SWXPSUB5R" + year).price * 2
            if (add_to_quote == true) q.currentQuote.sub_renewal_5_pack = this.addToQuote("1SWXPSUB5R" + year, 2);
        } else if (q.currentQuote.sil_2_gld_upg) {
            totalSubCost += this.addToQuote("1SWXGSUB5R" + year).price * 2
            if (add_to_quote == true) q.currentQuote.sub_renewal_5_pack = this.addToQuote("1SWXGSUB5R" + year, 2);
        } else {
            totalSubCost += this.addToQuote("1SWXSSUB5R" + year).price * 2
            if (add_to_quote == true) q.currentQuote.sub_renewal_5_pack = this.addToQuote("1SWXSSUB5R" + year, 2);
        }


        if (q.currentQuote.sub_100_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_100_pack.product_code + "R" + year).price * q.currentQuote.sub_100_pack.qty;
            if (add_to_quote == true) q.currentQuote.sub_renewal_100_pack = this.addToQuote(q.currentQuote.sub_100_pack.product_code + "R" + year, q.currentQuote.sub_100_pack.qty);
        }
        if (q.currentQuote.sub_25_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_25_pack.product_code + "R" + year).price * q.currentQuote.sub_25_pack.qty;
            if (add_to_quote == true) q.currentQuote.sub_renewal_25_pack = this.addToQuote(q.currentQuote.sub_25_pack.product_code + "R" + year, q.currentQuote.sub_25_pack.qty);
        }
        if (q.currentQuote.sub_5_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_5_pack.product_code + "R" + year).price * q.currentQuote.sub_5_pack.qty;
            if (add_to_quote == true) q.currentQuote.sub_renewal_5_pack = this.addToQuote(q.currentQuote.sub_5_pack.product_code + "R" + year, q.currentQuote.sub_5_pack.qty + q.currentQuote.sub_renewal_5_pack.qty);
        }
        if (q.currentQuote.sub_single) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_single.product_code + "R" + year).price * q.currentQuote.sub_single.qty;
            if (add_to_quote == true) q.currentQuote.sub_renewal_single = this.addToQuote(q.currentQuote.sub_single.product_code + "R" + year, q.currentQuote.sub_single.qty);
        }
        
        totalCost = parseInt(totalSubCost) + parseInt(updates);
        return totalCost;

    },
    calculateRecurringCosts : function () {
        if (q.currentQuote.chasis.product_code == '1AS601000LF') {
            var updates = this.addToQuote("1SWXSOHOR").price;
        } else {
            var updates = this.addToQuote("1SWXSMBR").price;
        }
        var totalSubCost = 0;

        if (q.currentQuote.sil_2_plt_upg) {
            totalSubCost += this.addToQuote("1SWXPSUB5R").price * 2
        } else if (q.currentQuote.sil_2_gld_upg) {
            totalSubCost += this.addToQuote("1SWXGSUB5R").price * 2
        } else {
            totalSubCost += this.addToQuote("1SWXSSUB5R").price * 2
        }


        if (q.currentQuote.sub_100_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_100_pack.product_code + "R").price * q.currentQuote.sub_100_pack.qty;
        }
        if (q.currentQuote.sub_25_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_25_pack.product_code + "R").price * q.currentQuote.sub_25_pack.qty;
        }
        if (q.currentQuote.sub_5_pack) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_5_pack.product_code + "R").price * q.currentQuote.sub_5_pack.qty;
        }
        if (q.currentQuote.sub_single) {
            totalSubCost += this.addToQuote(q.currentQuote.sub_single.product_code + "R").price * q.currentQuote.sub_single.qty;
        }
        
        totalCost = parseInt(totalSubCost) + parseInt(updates);

        q.total_annual_cost = totalCost;

        $('#costs_table').html('<table style="font-size:100%;" class="quote"><tr><td>Software Updates/Maintenance</td><td>' + q.formatPrice(updates) +'</td></tr><tr><td>Subscription Renewals</td><td>' + q.formatPrice(totalSubCost) + '</td></tr><tr class="heading"><td><strong>Annual Total:</sup></strong></td><td><strong>' + q.formatPrice(totalCost) + '</strong></td></tr></table>')
    },
    refreshQuote : function () {
        //this.console.log('Refreshing sidebar...');
        $('#' + q.sidebar).html('');
        //$('#' + q.sidebar).append('<li><h4>Quote Summary</h4></li>');
        var details = '';
        /*jQuery.each(q.currentQuote, function(i, val) {
            var qty = parseInt(val.qty) > 1 ? ' &times; ' + val.qty : '';
            details += '<tr><td>' + val.desc_short + qty + '</td><td class="price">' + q.formatPrice(val.price * val.qty) + '</td></tr>';
        });*/
        // TODO: Refactor this code, make it so that we can just loop through the object, but already in an ordered fashion:
        if (q.currentQuote.chasis) {
            details += q.formatLineItem(q.currentQuote.chasis);
        }
        if (q.currentQuote.analog) {
            details += q.formatLineItem(q.currentQuote.analog);
        }
        if (q.currentQuote.analog_modules) {
            details += q.formatLineItem(q.currentQuote.analog_modules);
        }
        if (q.currentQuote.voice_t1) {
            details += q.formatLineItem(q.currentQuote.voice_t1);
        }
        if (q.currentQuote.phone_331) {
            details += q.formatLineItem(q.currentQuote.phone_331);
        }
        if (q.currentQuote.phone_335) {
            details += q.formatLineItem(q.currentQuote.phone_335);
        }
        if (q.currentQuote.phone_430) {
            details += q.formatLineItem(q.currentQuote.phone_430);
        }
        if (q.currentQuote.phone_450) {
            details += q.formatLineItem(q.currentQuote.phone_450);
        }
        if (q.currentQuote.phone_550) {
            details += q.formatLineItem(q.currentQuote.phone_550);
        }
        if (q.currentQuote.phone_560) {
            details += q.formatLineItem(q.currentQuote.phone_560);
        }
        if (q.currentQuote.phone_650) {
            details += q.formatLineItem(q.currentQuote.phone_650);
        }
        if (q.currentQuote.phone_670) {
            details += q.formatLineItem(q.currentQuote.phone_670);
        }
        if (q.currentQuote.phone_6000) {
            details += q.formatLineItem(q.currentQuote.phone_6000);
        }
        if (q.currentQuote.phone_7000) {
            details += q.formatLineItem(q.currentQuote.phone_7000);
        }
        if (q.currentQuote.phone_650_expansion) {
            details += q.formatLineItem(q.currentQuote.phone_650_expansion);
        }
        if (q.currentQuote.phone_670_expansion) {
            details += q.formatLineItem(q.currentQuote.phone_670_expansion);
        }
        if (q.currentQuote.phone_vvx1500) {
            details += q.formatLineItem(q.currentQuote.phone_vvx1500);
        }
        if (q.currentQuote.phone_token) {
            details += q.formatLineItem(q.currentQuote.phone_token);
        }
        if (q.currentQuote.sil_2_gld_upg) {
            details += q.formatLineItem(q.currentQuote.sil_2_gld_upg);
        }
        if (q.currentQuote.sil_2_plt_upg) {
            details += q.formatLineItem(q.currentQuote.sil_2_plt_upg);
        }
        if (q.currentQuote.sub_100_pack) {
            details += q.formatLineItem(q.currentQuote.sub_100_pack);
        }
        if (q.currentQuote.sub_25_pack) {
            details += q.formatLineItem(q.currentQuote.sub_25_pack);
        }
        if (q.currentQuote.sub_5_pack) {
            details += q.formatLineItem(q.currentQuote.sub_5_pack);
        }
        if (q.currentQuote.sub_single) {
            details += q.formatLineItem(q.currentQuote.sub_single);
        }
        if (q.currentQuote.sub_renewal_100_pack) {
            details += q.formatLineItem(q.currentQuote.sub_renewal_100_pack);
        }
        if (q.currentQuote.sub_renewal_25_pack) {
            details += q.formatLineItem(q.currentQuote.sub_renewal_25_pack);
        }
        if (q.currentQuote.sub_renewal_5_pack) {
            details += q.formatLineItem(q.currentQuote.sub_renewal_5_pack);
        }
        if (q.currentQuote.sub_renewal_single) {
            details += q.formatLineItem(q.currentQuote.sub_renewal_single);
        }
        if (q.currentQuote.sub_maintenance) {
            details += q.formatLineItem(q.currentQuote.sub_maintenance);
        }
        if (q.currentQuote.cold_spare) {
            details += q.formatLineItem(q.currentQuote.cold_spare);
        }
        if (q.currentQuote.rack_mount) {
            details += q.formatLineItem(q.currentQuote.rack_mount);
        }
        if (q.currentQuote.wall_mount) {
            details += q.formatLineItem(q.currentQuote.wall_mount);
        }
        if (q.currentQuote.ext_warranty) {
            details += q.formatLineItem(q.currentQuote.ext_warranty);
        }
        if (q.currentQuote.cables) {
            details += q.formatLineItem(q.currentQuote.cables);
        }
        if (q.currentQuote.patch_panel) {
            details += q.formatLineItem(q.currentQuote.patch_panel);
        }
	    
        $('#' + q.sidebar).append('<li><table style="font-size:100%;" class="quote"><!--<tr class="heading"><td>Item</td><td>Price</td></tr>-->' + details + '<tr class="heading"><td><strong>Estimated Cost:</strong></td><td><strong>' + q.formatPrice(q.total) + '</strong></td></tr></table></li>');
        //total.css('background', '#FFFF00').animate({background: 'inherit'});
       
        $('#2yr_cost').html(q.formatPrice(q.calculateMultiyearCosts("2")));        
        $('#4yr_cost').html(q.formatPrice(q.calculateMultiyearCosts("4")));        
        
        q.calculateRecurringCosts();
        $('#2yr_dollars').html(q.formatPrice((q.total_annual_cost * 2) - q.calculateMultiyearCosts("2")));        
        $('#4yr_dollars').html(q.formatPrice((q.total_annual_cost * 4) - q.calculateMultiyearCosts("4")));        
    },
    addToQuote : function(product_code, qty) {
        // Looks up a product code in the lookup table, and returns a copy of that object. Solves shared references issue.
        //var tmp = new Object();
        var tmp = {};
        // TODO: In order to have products with nested data structures, make this each iterator recursive:
        if (!q.lookupTable[product_code]) {
            alert ("Product code " + product_code + " not found!");
            return tmp;
        } else {
            var data = q.lookupTable[product_code];
        }
        //this.console.log(data);
        jQuery.each(q.lookupTable[product_code], function (i, val) {
            tmp[i] = val;
        });
        tmp.qty = qty || 1;
        return tmp;
    },
    formatPrice : function(value) {
    	var val = parseFloat(value).toFixed(2);
    	x = val.split('.');
    	x1 = x[0];
    	x2 = x.length > 1 ? '.' + x[1] : '';
    	var rgx = /(\d+)(\d{3})/;
    	while (rgx.test(x1)) {
    		x1 = x1.replace(rgx, '$1' + ',' + '$2');
    	}
    	return '$' + x1 + x2;
    },
    formatLineItem : function(item) {
        var qty = parseInt(item.qty) > 1 ? ' &times; ' + item.qty : '';
        return '<tr><td>' + item.desc_short + qty + '</td><td class="price">' + q.formatPrice(item.price * item.qty) + '</td></tr>';
    },
    revealQuestions: function () {
        if (q.currentQuote.chasis.product_code == '1AS3050000LF') {
            $('#acc').show();
        } else {
            $('#acc').hide();
        }
    },
    scrollConfigInfo : function (event)
	{ 
		var scrollOffset = 200;
        var scrollBottom = document.body.scrollHeight - 1050;
        //console.log(scrollBottom);
		//var scrollOffset = 185;
		var shiftAmount = 8;
		var scrollAmount = window.pageYOffset
                			|| document.documentElement.scrollTop
                			|| document.body.scrollTop
                			|| 0;

		if (scrollAmount > scrollOffset)
		{
			q.sidebar_dest_location = q.sidebar_start_location + scrollAmount - scrollOffset;
            if (q.sidebar_dest_location > scrollBottom - q.sidebar_container.offsetHeight) {
                q.sidebar_dest_location = scrollBottom - q.sidebar_container.offsetHeight;
            }
		}
		else
		{
			q.sidebar_dest_location = q.sidebar_start_location;
		}

		if (Math.abs(q.sidebar_curr_location - q.sidebar_dest_location) < shiftAmount) 
		{
			q.is_sidebar_scrolling = false;
			clearTimeout(q.sidebar_timeout_var);
			q.sidebar_container.style.top = q.sidebar_dest_location + "px";
		}
		else
		{
			if (q.sidebar_curr_location < q.sidebar_dest_location)
			{
				q.sidebar_container.style.top = (q.sidebar_curr_location + shiftAmount) + "px";	
				q.sidebar_curr_location = parseInt(q.sidebar_container.style.top);
			}
			else
			{
				q.sidebar_container.style.top = (q.sidebar_curr_location - shiftAmount) + "px";	
				q.sidebar_curr_location = parseInt(q.sidebar_container.style.top);
			}
			q.is_sidebar_scrolling = true;
			var obj = q;
			q.sidebar_timeout_var = setTimeout(function (){ obj.scrollConfigInfo(); }, 75);
		}
	}
};

