﻿

var SubscribeController = Class.create();

SubscribeController.prototype = {
    initialize:function()
    {
      
       this.validationManager = new ValidationManager("subscribeInputs");
              
       this.cbJB=$("jb-production");
       this.cbJP=$("jb-jewerly");
       this.jbPeriodContainer = $("jbSubscribePeriodContainer");
       this.jpPeriodContainer = $("jpSubscribePeriodContainer");
       
       this.cbJB.onclick = this.showHideJB.bindAsEventListener(this);
       this.cbJP.onclick = this.showHideJP.bindAsEventListener(this);
       this.btnSubscribe = $$(".btnSubscribe")[0];
       this.setSubmitBtnState();
    },
    getJbChecked:function()
    {
      return $F(this.cbJB)=="on";
    },
     getJpChecked:function()
    {
      return $F(this.cbJP)=="on";
    },
    setSubmitBtnState:function()
    {
       this.btnSubscribe.disabled = !this.getJbChecked()&&!this.getJpChecked();
    },
    showHideJB:function()
    {
     this.setSubmitBtnState();
       $F(this.cbJB)=="on"?this.jbPeriodContainer.show():this.jbPeriodContainer.hide();
    },
       showHideJP:function()
    {
     this.setSubmitBtnState();
       $F(this.cbJP)=="on"?this.jpPeriodContainer.show():this.jpPeriodContainer.hide();
    },
    validate:function()
    {
      var valid1=this.validationManager.valide();
       var validJB =true;
       var validJP = true;
     
     var jbChecked =this.getJbChecked(); 
     var jpChecked =this.getJpChecked();
      if(jbChecked) validJP =  subscribePeriodCollection[0].validate();
      if(jpChecked) validJB =  subscribePeriodCollection[1].validate();
      
      if(!jpChecked&&!jbChecked) 
      {
        alert(CHOOSE_PLEASE_FOR_SUBSCRIBE_ONE_OF_THE_MAGAZINES);
        return false;
      }
      
      return valid1&&validJB&&validJP;
     
    },
    getHalf:function()
    {
      return this.el.getElementsByTagName("select")[1];
    }
 }
 
 var subscribeController =new SubscribeController();