var calculate_cost = Class.create({
    input_elem:null,
    m_price_elem: null,
    y_price_elem:null,
    m_discount_elem:null,
    y_discount_elem: null,
    
    initialize:function(input_elem, m_price_elem, y_price_elem, m_discount_elem, y_discount_elem)
    {
        this.input_elem = input_elem;
        this.m_price_elem = m_price_elem;
        this.y_price_elem = y_price_elem;
        this.m_discount_elem = m_discount_elem;
        this.y_discount_elem = y_discount_elem;
      
        Event.observe(this.input_elem , 'keyup', this.calculate_price.bind(this));        
    },   
    calculate_price:function()
    {
        var nb_dashboard;    
        nb_dashboard = this.input_elem.value;        
        if(!this.nb_is_number(nb_dashboard))
        {
            nb_dashboard = 0;
        }    
        var ajax_req = new Ajax.Request("ajax/subscriptions/calculate_price.php", 
		{parameters:{"nb_dashboard":nb_dashboard},
		    onSuccess:function(t)
		    {
		        try 
		        {
		            var ar = new ajax_responder(t.responseText);
		            var ret = ar.get_obj();
		            if (ar.success())
		            {
                        this.update_monthly_price(ret.datas.price);
                        this.update_annualy_price(ret.datas.yearly_price);
                        this.calculate_periodical_discount(nb_dashboard, 'month');
                        this.calculate_periodical_discount(nb_dashboard, 'year');
		            }        
		        }
		        catch (err) {pdt_console.log(err);}		        
		    }.bind(this)});	
        return true;
    },
    nb_is_number:function(nb)
	{       
        nb = tools.get_number(this.input_elem.value);
        if (!nb)
        {
            return (false);
        }
        else 
        {
            return (true);
        }        
	},    
    calculate_periodical_discount:function(nb_dashboard, period)
    {
        var ajax_req = new Ajax.Request("ajax/subscriptions/calculate_price.php", 
		{parameters:{"nb_dashboard":nb_dashboard, "period":period},
		    onSuccess:function(t)
		    {
		        try 
		        {
		            var ar = new ajax_responder(t.responseText);
		            var ret = ar.get_obj();
		            if (ar.success())
		            {  
                        if(period === 'year')
                        {
                            this.update_annualy_discount(ret.datas.price_original, ret.datas.you_save);
                        }
                        if(period === 'month')
                        {                            
                            this.update_monthly_discount(ret.datas.price_original, ret.datas.you_save);
                        }
                        
		            }        
		        }
		        catch (err) {pdt_console.log(err);}		        
		    }.bind(this)
        });	
    },    
    update_monthly_price:function(m_price)
    {
        this.m_price_elem.update(m_price);
    },    
    update_annualy_price:function(y_price)
    {
        this.y_price_elem.update(y_price);
    },        
    update_monthly_discount:function(original_price, discount_price)
    {
        var discount;
        discount = this.calculate_discount(original_price, discount_price);
        this.m_discount_elem.update(discount);
    },    
    update_annualy_discount:function(original_price, discount_price)
    {
        var discount;
        discount = this.calculate_discount(original_price, discount_price);        
        this.y_discount_elem.update(discount);
    },      
    calculate_discount:function(original_price, discount_price)
    {
        var discount;
        discount = (discount_price / original_price) *100;
        discount = Math.round(discount);
        if (isNaN(discount))
        {
            discount = 0;
        }    
        return discount;
    } 
});


