function PromoCodeForm(view, paypalFormView, _options) {

    this.PCS_VALID = 1;
    this.PCS_EXPIRE = 2;
    this.PCS_DOSE_NOT_EXISTS = 3;
    this.PCS_MAX_USAGE_FAULT = 4;

    this.defaults = {
        
        checkURL: 'user/checkpromoprocess'
    };
    
    this.lastError = 0;
    this.isCodeValid = false;
    this.options = $.extend({}, this.defaults, _options);
    this.view = view;
    this.input = this.view.find('input');
    this.loading = this.view.find('.promo_loading');
    this.setStatus('ready');
    
    this.paypalFormView = paypalFormView;
    this.basePrice = this.paypalFormView.find('.ppAmountV').val();
    this.baseMonths = 6;
    this.baseButtonId = this.paypalFormView.find('.ppButtonId').val();
    this.u_id = this.paypalFormView.find('.ppCustom').val();
    this.messageView = this.paypalFormView.find('#promoCodeMessage');

    this.attacheLiteners();
}

PromoCodeForm.prototype.attacheLiteners = function() {

    var $this = this;
    this.view.find('a.applyCodeButton').click(function() { return $this.checkCode(); });
    this.view.find('form').submit(function() { return $this.checkCode(); });
    this.view.find('a.removeCodeButton').click(function() { return $this.removeCode(); });
    this.messageView.find('a.continueButton').click(function() { 
    
        $this.paypalFormView.find('.continueForm').submit();
        return false;
    });
}

PromoCodeForm.prototype.setStatus = function(_status) {

    this.status = _status;
    if (this.status == 'ready') this.loading.hide(); else this.loading.show();
}
PromoCodeForm.prototype.checkCode = function() {

    if (this.status !== 'ready') return;
    this.setStatus('promo_loading');
    var $this = this;
    $.ajax({
    
        'type': 'POST',
        'url':  this.options.checkURL,
        'data': {
            code: this.input.val()
        },
        'cache':false,
        'success': function(data) {

            var jsonData = $.evalJSON(data);
            if (jsonData.status == $this.PCS_VALID) {
                
                $this.onCodeValid(jsonData.pcCode);
            } else {
            
                $this.onCodeFault(jsonData.status);
            }
            $this.setStatus('ready');
        },
        'fault': function(err) {
            
            $this.setStatus('ready');
            this.onFault($this.PCS_DOSE_NOT_EXISTS);
        }
    });
    return false;
}

PromoCodeForm.prototype.onCodeValid = function(pcCode) {

    this.messageView.find('div').hide('slow');
    this.isCodeValid = true;
    this.view.find('.applyCodeButton').hide();
    this.view.find('.input').hide();

    this.view.find('.removeCodeButton').show();
    this.view.find('.validPromoCode').text(pcCode.code);
    this.view.find('.validPromoCode').show();
    this.updatePayPalForm(pcCode);

    if (pcCode.amount != 0) {
    
        //this.messageView.find('#PCS_SUCCES_SOMEPAYMENT').toggle('slow');
    } else {
    
        this.messageView.find('#PCS_SUCCES_NOPAYMENT').toggle('slow');
        this.showContunue();
    }
}

PromoCodeForm.prototype.showContunue = function() {
    
    //this.paypalFormView.find('.paypalForm, .subscriptionInfo').hide();
    //this.paypalFormView.find('.continueButton').show();
    
}

PromoCodeForm.prototype.removeContunue = function() {
    
    this.paypalFormView.find('.paypalForm, .subscriptionInfo').show();
    this.paypalFormView.find('.continueButton').hide();
}

PromoCodeForm.prototype.onCodeFault = function(faultStatus) {

    this.isCodeValid = false;
    if (this.lastError == faultStatus) {
    
        this.messageView.find('#PCS_ERR_MSG_' + faultStatus).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
    } else {
        
        this.lastError = faultStatus;
        this.messageView.find('div').fadeOut('fast');
        this.messageView.find('#PCS_ERR_MSG_' + faultStatus).toggle('slow');
    }
}

PromoCodeForm.prototype.removeCode = function() {
	
	document.getElementById('mon_price').innerHTML = document.getElementById('def_price_month').value;
	document.getElementById('mon_price_2').value = document.getElementById('def_price_month').value;
	document.getElementById('mon_price_3').value = document.getElementById('def_price_month').value;
			
	document.getElementById('div_or').style.visibility='visible';
	document.getElementById('form_pay_month').style.visibility='visible';	
	//document.getElementById('year_savings').style.visibility='visible';
	document.getElementById('form_pay_year').style.visibility='visible';	
	
	document.getElementById('year_price').innerHTML = document.getElementById('def_price_year').value;
	this.paypalFormView.find('.ppAmountV').val(document.getElementById('def_price_year').value);
	
	this.paypalFormView.find('.ppButtonId').val(document.getElementById('def_button_year').value);
	
	this.paypalFormView.find('.ppCustomMonth').val(this.u_id);
	this.paypalFormView.find('.ppCustom').val(this.u_id);

    this.isCodeValid = false;
    this.view.find('.applyCodeButton').show();
    this.messageView.find('div').hide('slow');
    this.view.find('.input').show();

    this.view.find('.removeCodeButton').hide();
    this.view.find('.validPromoCode').hide();
    this.view.find('#promoCode').val('');
    this.view.find('#promoCode').focus();

    this.resetPayPalForm();

    return false;
}

PromoCodeForm.prototype.updatePayPalForm = function(pcCode) {
	
	if(pcCode.amount<=0) {
		//alert('free');
			document.getElementById('div_or').style.visibility='hidden';
			document.getElementById('form_pay_year').style.visibility='hidden';
			document.getElementById('form_pay_month').style.visibility='hidden';
			//document.getElementById('year_savings').style.visibility='hidden';
			this.paypalFormView.find('.ppMonths').text(pcCode.subscription_months);
			this.paypalFormView.find('.promoCode').val(pcCode.code);
	} else {
		if(pcCode.type=='month') {
			//alert('month');
			
			document.getElementById('mon_price').innerHTML = pcCode.amount;
			document.getElementById('mon_price_2').value = pcCode.amount;
			document.getElementById('mon_price_3').value = pcCode.amount;
			
			document.getElementById('div_or').style.visibility='hidden';
			document.getElementById('form_pay_year').style.visibility='hidden';
			//document.getElementById('year_savings').style.visibility='hidden';
			
			this.paypalFormView.find('.ppCustomMonth').val(this.u_id + '|' + pcCode.code);
			
		}
		if(pcCode.type=='year') {
			document.getElementById('year_price').innerHTML = pcCode.amount;
			//document.getElementById('year_savings').style.visibility='hidden';
			this.paypalFormView.find('.ppButtonId').val(pcCode.pp_btn_ref);
			this.paypalFormView.find('.ppAmountV').val(pcCode.amount);
			this.paypalFormView.find('.ppCustom').val(this.u_id + '|' + pcCode.code);
			
			document.getElementById('div_or').style.visibility='hidden';
			document.getElementById('form_pay_month').style.visibility='hidden';
			
		}
	}
/*
    this.paypalFormView.find('.promoCode').val(pcCode.code);
    this.paypalFormView.find('.ppAmountT').text(pcCode.amount);
    this.paypalFormView.find('.ppAmountV').val(pcCode.amount);
    this.paypalFormView.find('.ppMonths').text(pcCode.subscription_months);
    this.paypalFormView.find('.ppButtonId').val(pcCode.pp_btn_ref);
    this.paypalFormView.find('.ppCustom').val(this.u_id + '|' + pcCode.code);*/
}

PromoCodeForm.prototype.resetPayPalForm = function() {
    
    this.paypalFormView.find('.promoCode').val('');
    this.paypalFormView.find('.ppMonths').text(this.baseMonths);
    this.paypalFormView.find('.ppButtonId').val(this.baseButtonId);
    this.paypalFormView.find('.ppAmountT').text(this.basePrice);
    this.paypalFormView.find('.ppAmountV').val(this.basePrice);
    this.paypalFormView.find('.ppCustom').val(this.u_id);
    this.removeContunue();
}
/**/
