function small_business_solutions(){
    
	// set child module, make sure it's loaded
	if (window.quicklist){
            this.child_module = 'quicklist';
        } else if (window.socialboost){   
            this.child_module = 'socialboost';
        } else if (window.goseo){
            this.child_module = 'goseo';
        }

	if (this.child_module)
	{
                
		load_module(this.child_module);
	}

	if (this.is_order_form())
	{
                /*
                Public variables - may be accessed publicly or privately
                */
                this.packages = $('#package_select > div[class*="col"]');
		var packages;
                
                //reset setup fees
                this.reset_setup_fees();
		$('#card-types .ch-label').bind('click', this, function(e) {e.data.card_type_label_click($(this));});

                this.packages.bind('click', this, function(e){
                        e.data.package_click($(this));
                        e.stopImmediatePropagation();
                });

		packages = $('#package_select > div[class*="col"]');

		packages.bind('click', this, function(e){
			e.data.package_click($(this));
			e.stopImmediatePropagation();
		});

		this.package_click($('[package="'+(($('#prev_plan').val()) ? $('#prev_plan').val() : 'Core')+'"]'));
		$('#pid_select').bind('change', this, function(e){ e.data.pid_change(); });
                
                $('#apply-coupon').bind('click', this, function(e){
                        
                        var coupon_code = $('#coupon_code').val();

                        if(coupon_code==""){

                                alert('Please enter a coupon code');

                        } else {

                                $.post('/small-business-solutions/ajax_apply_coupon', {code: coupon_code}, function(data){
                                        //alert(data);
                                        var coupon =  ajaxResponse(data);
                                        if(coupon.errors){
                                                alert(coupon.errors);
                                        } else {
                                                //update the display for applied coupons
                                                $("#coupons_container").html(coupon.html);
                                                
                                                e.data.update_billing_info();
                                        }
                                });

                        }

                        return false;
                });
        
	}
}

small_business_solutions.prototype.reset_setup_fees= function()
{
        $(".setup_fee_display").each(function(){
                $(this).text($(this).parent().attr('setup_fee'));     
        });
};


small_business_solutions.prototype.is_order_form = function()
{
	return ($('.order-entry').length);
};

small_business_solutions.prototype.package_click = function(col)
{
        var selected;
        var package_name;

        package_name = col.attr('package');

        selected = col.siblings('.col-selected');


        if (selected.length > 0)
        {
              selected.removeClass('col-selected');
              selected.find('.checked').removeClass('checked');
        }

        col.addClass('col-selected');
        col.find('.custom-checkbox').addClass('checked');

        $('#plan').val(package_name);

	if (this.child_module == 'quicklist')
	{
              window.modules.quicklist.set_keyword_input_ml();
	}

	$('#selected_package').text(package_name);
	this.update_billing_info();
}

small_business_solutions.prototype.card_type_label_click = function(label)
{
	var cbox, other_cbox_container;

	cbox = $('#'+label.attr('for'));
	if (cbox.is(':checked'))
	{
		cbox.parent().removeClass('checked');
		cbox.attr('checked', false);
	}
	else
	{
		// turn off any of the other cards if they are on
		other_cbox_container = $('#card-types .checked');
		if (other_cbox_container.length > 0)
		{
			other_cbox_container.removeClass('checked');
			other_cbox_container.find('input').attr('checked', false);
		}
		cbox.parent().addClass('checked');
		cbox.attr('checked', true);
	}
};

small_business_solutions.prototype.apply_coupons = function()
{
        
        var self = this;
        if($("#coupons_container .coupon").length){
                
                var contract_length = 0;
                
                $("#coupons_container .coupon").each(function(){
                        var coupon_function = $(this).attr('type').replace(" ", "_")+"_coupon";
                        self[coupon_function]();
                        
                        var length = Number($(this).attr('contract_length'));
                        
                        //find the larget contract length
                        if( length > contract_length ){
                                contract_length = length;
                        }
                });
                
                if(contract_length){
                        self.update_contract_length(contract_length);
                }
        }
}

small_business_solutions.prototype.update_contract_length = function(contract_length)
{
        $('#contract_length').val(contract_length);
        $('.contract_length').html(contract_length);
}

small_business_solutions.prototype.pid_change = function()
{
	var pid = $('#pid_select').val();
	$('#rep_info_table .sid_select:visible').hide();
	$('#rep_info_table .sid_select[name="sid_'+pid+'"]').show();
};

small_business_solutions.prototype.update_billing_info = function()
{
        var self = this;
        if($("#coupons_container .coupon").length){
                $("#coupons_container .coupon").each(function(){
                        var coupon_function = $(this).attr('type').replace(" ", "_")+"_coupon";
                        self[coupon_function]();
                });
        }
}

//Coupon Functions
small_business_solutions.prototype.setup_fee_coupon = function(){
        $coupon = $("#coupons_container .coupon[type='setup fee']");
        var value = $coupon.attr('value');
        var value_type = $coupon.attr('value_type');
        
        if(value_type=='percent' && value==100){
                this.reset_setup_fees();
                $("#package_select h4[setup_fee]").addClass('strike');
                this.billing_totals.setup_fee = 0;
                this.billing_notes.setup_fee = "Savings of $"+($('#package_select .col-selected [setup_fee]').attr('setup_fee'));
        } else {
                $("#package_select h4[setup_fee]").removeClass('strike');
                self = this;
                this.packages.each(function(){
                        var init_setup_fee = $(this).find('[setup_fee]').attr('setup_fee');
                        var savings = 0;
                        var setup_fee = 0
                        
                        if(value_type=='percent'){
                                savings = Math.floor(value/100*init_setup_fee);
                                setup_fee = init_setup_fee-savings;
                        } else if(value_type=='dollars'){
                                savings = value;
                                setup_fee = init_setup_fee-savings;
                        }
                        
                        $(this).find('.setup_fee_display').text(setup_fee);
                        if($(this).is('.col-selected')){
                                self.billing_totals.setup_fee = setup_fee;
                                self.billing_notes.setup_fee = "Savings of $"+savings;
                        }
                });
        }
}

small_business_solutions.prototype.first_month_coupon = function(){
        $coupon = $("#coupons_container .coupon[type='first month']");
        var value = $coupon.attr('value');
        var value_type = $coupon.attr('value_type');
	
	if(value_type=='percent'){
		this.billing_totals.first_month = Math.floor(value/100*this.billing_totals.first_month);
	} else if(value_type=='dollars'){
		this.billing_totals.first_month = this.billing_totals.first_month - value;
	}
	
}


small_business_solutions.prototype.update_billing_info = function()
{
	var package, key, price, setup_fee;
        
        package = $('#package_select .col-selected');
        price = package.find('[price]').attr('price');
        
        setup_fee = package.find('[setup_fee]').attr('setup_fee');
        
        //init billing totals
	this.billing_totals = {
					monthly:        0,
					setup_fee:      0,
					optimization:   0,
					first_month:    0
	};
	this.billing_notes = {};
        this.billing_totals.monthly = Number(price);
        this.billing_totals.setup_fee = Number(setup_fee);
	this.billing_totals.first_month = this.billing_totals.monthly;
	
	//apply coupons
        this.apply_coupons();
        
	if (this.discount && this.discount != '0')
	{
                if(this.discount_type=="percent"){
                        this.billing_totals.first_month -= Math.ceil((price * this.discount) / 100);
                } else if(this.discount_type=="dollars"){
                        this.billing_totals.first_month -= this.discount;
                }
               
                this.billing_notes.first_month = this.discount_text()+' Discount Applied';
	}
	else
	{
		this.billing_notes.monthly = '';
		this.billing_notes.first_month = '';
	}

	// have child modules set any billing totals they need to
	window.modules[this.child_module].update_billing_info(this.billing_totals);

	// op
	if(this.billing_totals.optimization)
	{
		this.billing_totals.first_month += this.billing_totals.optimization;
	}
        
        //setup fee
        if(this.billing_totals.setup_fee)
        {
                this.billing_totals.first_month += this.billing_totals.setup_fee;
        }
        $("#setup_fee").val(this.billing_totals.setup_fee);

	for (key in this.billing_totals)
	{
		$('#'+key+'_total').text(currency(this.billing_totals[key]));
		$('#'+key+'_note').text(this.billing_notes[key]);
	}
};

$(document).ready(function(){
        
        load_module('small_business_solutions');
        
});

