/**
 * Formulär fabriken
 */
DynamicFormHandler = Class.create();
DynamicFormHandler.prototype =
{
    initialize: function(form,rules)
    {
      this.form = form;
      this.rulesArray = rules;
    },
    submit: function()
    {
      if(this.validate())
      {
        this.form.action = "/app/modules/formfactory/formhandler.php";
        this.form.request({onSuccess: this.onSuccess.bind(this), onFailure: this.onFailure.bind(this)});
        this.form.disable();
      }
    },
    onSuccess: function(transport)
    {
        this.form.enable();
        if(transport.responseText == null || transport.responseText.length == 0)
        {
          alert("Kunde inte spara formulär");
          return;
        }

        var res = eval('(' + transport.responseText + ')');

        // Alright, kolla vad vi skall göra
        if(res.result == "OK")
        {
          // OK, visa meddealnde om det finns
          if(res.message && (res.message.length > 0))
            alert(res.message);
          
          this._clearForm();

          // OK, nästa seg ?
          if(res.redirect && (res.redirect.length > 0))
          {
            if(res.mode == "test")
                alert('Skulle skickat vidare till [' + res.redirect + '] gör dock inte det vid test/förhandsgranskning')
            else
            {
              document.location = res.redirect;
              return;
            }
          }
          else
          {
            if(res.mode != "test")
                document.location.reload();
          }
        }
        else
            alert(res.message);
    },
    onFailure: function(transport)
    {
        this.form.enable();
    },
    validate: function()
    {
      for(var i = 0; i < this.rulesArray.length;i++)
      {
        var r = this.rulesArray[i];

        var value = this._getValue(r.elementId);
        var re = new RegExp(r.regExp);

        if(!value.match(re))
        {
            alert(r.message);
            var ctrl = $(r.elementId);
            if(ctrl && ctrl.focus)
                ctrl.focus();

            return false;
        }
      }
      return true;
    },
    _clearForm: function()
    {
      this.form.reset();
    },
    _getValue: function(elementId)
    {
        var f = $(elementId);

        if(!f)
            return '';

        if(f.type == "radio")
        {
          // OK hitta hela familjen
          var butts = new Array();
          butts[butts.length] = f;

          var c = 1;
          var f2;

          do
          {
            f2 = $(elementId + '_' + c++);

            if(f2)
                butts[butts.length] = f2;
          }
          while(f2);
          
          for(var i = 0; i < butts.length; i++)
          {
              if(butts[i].checked)
                  return butts[i].value;
          }
          return '';
        }
        else
            return f.value;
    }
};

function ValidationRule(elementId,regExp,message)
{
    this.elementId = elementId;
    this.regExp = regExp;
    this.message = message;
}

/***************************************************************************/
/* Sidfoten                                                                */
/***************************************************************************/
FooterComments = Class.create();
FooterComments.prototype =
{
    initialize: function(id,initSignature,initEmail,initComment)
    {
      this.form = $('commentForm' + id);
      if(!this.form)
          return;

      this.pending = false;
      this.signatureCtrl = $('commentSignature'+ id);
      this.emailCtrl = $('commentEmail'+ id);
      this.commentCtrl = $('commentMessage'+ id);
      this.submitLink = $('commentsSubmit' + id);

      this.orgSignature = initSignature;
      this.orgEmail = initEmail;
      this.orgComment = initComment;

      Event.observe(this.signatureCtrl,'focus',this.onFocus.bind(this,this.signatureCtrl,this.orgSignature));
      Event.observe(this.signatureCtrl,'blur',this.onBlur.bind(this,this.signatureCtrl,this.orgSignature));

      Event.observe(this.emailCtrl,'focus',this.onFocus.bind(this,this.emailCtrl,this.orgEmail));
      Event.observe(this.emailCtrl,'blur',this.onBlur.bind(this,this.emailCtrl,this.orgEmail));

      Event.observe(this.commentCtrl,'focus',this.onFocus.bind(this,this.commentCtrl,this.orgComment));
      Event.observe(this.commentCtrl,'blur',this.onBlur.bind(this,this.commentCtrl,this.orgComment));
      Event.observe(this.submitLink,'click',this.onSubmit.bind(this));

    },
    init: function()
    {
      if(!this.form)
          return;

      this.signatureCtrl.value = this.orgSignature;
      this.emailCtrl.value = this.orgEmail;
      this.commentCtrl.value = this.orgComment;
    },
    onFocus: function(ctrl,orgValue)
    {
        if(ctrl.value == orgValue)
            ctrl.value = "";

    },
    onBlur: function(ctrl,orgValue)
    {
        if(ctrl.value == "")
            ctrl.value = orgValue;
    },
    onSubmit: function()
    {
      if(this.pending)
          return;

      if(this.validate())
      {
        this.pending = true;
        this.form.action = "/app/modules/comments/formhandler.php";
        this.form.request({onSuccess: this.onSuccess.bind(this), onFailure: this.onFailure.bind(this)});
        this.form.disable();
      }
    },
    onSuccess: function(transport)
    {
        this.form.enable();
        this.pending = false;
        if(transport.responseText == null || transport.responseText.length == 0)
        {
          return;
        }

        var res = eval('(' + transport.responseText + ')');

        // Alright, kolla vad vi skall göra
        if(res.result == "OK")
        {
            if(res.message.length > 0)
                 alert(res.message);
        }
        else
            alert(res.message);

        document.location.reload();
    },
    onFailure: function(transport)
    {
        this.form.enable();
        this.pending = false;
    },
    validate: function()
    {
      if(this.signatureCtrl.value == this.orgSignature || this.signatureCtrl.value == "")
      {
          alert(appLang.getText("Please specify your name"));
          return false;
      }

      if(this.emailCtrl.value == this.orgEmail || this.emailCtrl.value == "")
      {
          alert(appLang.getText("Specify your email"));
          return false;
      }

      var re = new RegExp('[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}');

      // Kolla att epost är gilltig
      if(!this.emailCtrl.value.match(re))
      {
          alert(appLang.getText("Specify your email"));
          return false;
      }

      if(this.commentCtrl.value == this.orgComment || this.commentCtrl.value == "")
      {
          alert(appLang.getText("Specify comment"));
          return false;
      }

      return true;
    },
    _clearForm: function()
    {
      this.form.reset();
    }
};
FooterTip = Class.create();
FooterTip.prototype =
{
    initialize: function(id,initMyEmail,initFriendEmail,initMessage)
    {
      this.form = $('frmTipAFriend' + id);
      if(!this.form)
          return;

      this.pending = false;

      this.myEmailCtrl = $('senderEmail'+ id);
      this.friendEmailCtrl = $('recipientEmail'+ id);
      this.messageCtrl = $('messageText'+ id);
      this.submitLink = $('frmTipSubmit' + id);

      this.orgMyEmail = initMyEmail;
      this.orgFriendEmail = initFriendEmail;
      this.orgMessage = initMessage;

      Event.observe(this.myEmailCtrl,'focus',this.onFocus.bind(this,this.myEmailCtrl,this.orgMyEmail));
      Event.observe(this.myEmailCtrl,'blur',this.onBlur.bind(this,this.myEmailCtrl,this.orgMyEmail));

      Event.observe(this.friendEmailCtrl,'focus',this.onFocus.bind(this,this.friendEmailCtrl,this.orgFriendEmail));
      Event.observe(this.friendEmailCtrl,'blur',this.onBlur.bind(this,this.friendEmailCtrl,this.orgFriendEmail));

      Event.observe(this.messageCtrl,'focus',this.onFocus.bind(this,this.messageCtrl,this.orgMessage));
      Event.observe(this.messageCtrl,'blur',this.onBlur.bind(this,this.messageCtrl,this.orgMessage));
      Event.observe(this.submitLink,'click',this.onSubmit.bind(this));
    },
    init: function()
    {
      if(!this.form)
          return;

      this.myEmailCtrl.value = this.orgMyEmail;
      this.friendEmailCtrl.value = this.orgFriendEmail;
      this.messageCtrl.value = this.orgMessage;
    },
    onFocus: function(ctrl,orgValue)
    {
        if(ctrl.value == orgValue)
            ctrl.value = "";

    },
    onBlur: function(ctrl,orgValue)
    {
        if(ctrl.value == "")
            ctrl.value = orgValue;
    },
    onSubmit: function()
    {
      if(this.pending)
          return;

      if(this.validate())
      {
        this.pending = true;
        this.form.action = "/app/modules/tipafriend/sendToFriend.php";
        this.form.request({onSuccess: this.onSuccess.bind(this), onFailure: this.onFailure.bind(this)});
        this.form.disable();
      }
    },
    onSuccess: function(transport)
    {
        this.form.enable();
        this.pending = false;
        this._clearForm();

        if(transport.responseText == null || transport.responseText.length == 0)
        {
          return;
        }

        var res = eval('(' + transport.responseText + ')');

        // Alright, kolla vad vi skall göra
        if(res.result == "OK")
        {
            if(res.message.length > 0)
                alert(res.message);
        }
        else
            alert(res.message);

        document.location.reload();
    },
    onFailure: function(transport)
    {
        this.pending = false;
        this.form.enable();
    },
    validate: function()
    {
      var re = new RegExp('[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}');

      if(!this.myEmailCtrl.value.match(re))
      {
          alert(appLang.getText("Specify your email"));
          return false;
      }

      if(!this.friendEmailCtrl.value.match(re))
      {
          alert(appLang.getText("Specify your friends email"));
          return false;
      }

      if(this.messageCtrl.value == this.orgMessage || this.messageCtrl.value == "")
      {
          alert(appLang.getText("Specify message"));
          return false;
      }

      return true;
    },
    _clearForm: function()
    {
      this.form.reset();
    }
};

FooterToolbar = Class.create();
FooterToolbar.prototype =
{
    initialize: function(pid)
    {
        this.commentsButton = $('footCommentButton' + pid);
        this.commentsLink = $('footCommentLink' + pid);
        this.commentsActive = false;
        this.commentsTab = $('footerCommentsContainer' + pid);

        if(this.commentsLink)
             Event.observe(this.commentsLink,'click',this.onComment.bind(this));

        this.printButton = $('footPrintButton' + pid);
        this.printLink = $('footPrintLink' + pid);

        if(this.printLink)
             Event.observe(this.printLink,'click',this.onPrint.bind(this));

        this.tipButton = $('footTipButton' + pid);
        this.tipLink = $('footTipLink' + pid);
        this.tipActive = false;
        this.tipTab = $('divTipAFriend' + pid);

        if(this.tipLink)
             Event.observe(this.tipLink,'click',this.onTip.bind(this));
             
             
        this.comments = new FooterComments(pid,appLang.getText("Please specify your name") + ' ...',appLang.getText("Specify your email") + ' ...',appLang.getText("Specify comment") + ' ...');
        this.comments.init();

        this.tip = new FooterTip(pid,appLang.getText("Specify your email") + ' ...',appLang.getText("Specify your friends email") + ' ...',appLang.getText("Specify message") + ' ...');
        this.tip.init();

        this._activateComment();
    },
    onComment: function()
    {
        if(this.commentsActive)
            this._deactivateComment();
        else
            this._activateComment();
    },
    _deactivateComment: function()
    {
      if(this.commentsTab)
      {
        this.commentsButton.removeClassName("footButtonActive");
        this.commentsButton.addClassName("footButtonCommentInActive");
        this.commentsButton.removeClassName("footButtonCommentActive");
        this.commentsActive = false;
        this.commentsTab.hide();
      }
    },
    _activateComment: function()
    {
      if(this.commentsTab)
      {
        this._deactivateTip();
        this.commentsButton.addClassName("footButtonActive");
        this.commentsButton.addClassName("footButtonCommentActive");
        this.commentsButton.removeClassName("footButtonCommentInActive");
        this.commentsActive = true;
        this.commentsTab.show();
      }
    },
    onPrint: function()
    {
        print();
    },
    onTip: function()
    {
        if(this.tipActive)
            this._deactivateTip();
        else
            this._activateTip();
    },
    _deactivateTip: function()
    {
      if(this.tipTab)
      {
        this.tipButton.removeClassName("footButtonActive");
        this.tipButton.addClassName("footButtonTipInActive");
        this.tipButton.removeClassName("footButtonTipActive");
        this.tipActive = false;
        this.tipTab.hide();
      }
    },
    _activateTip: function()
    {
      if(this.tipTab)
      {
        this._deactivateComment();
        this.tipButton.addClassName("footButtonActive");
        this.tipButton.addClassName("footButtonTipActive");
        this.tipButton.removeClassName("footButtonTipInActive");
        this.tipActive = true;
        this.tipTab.show();
      }
    }
};
/**
 * Funktioner för att öppna väder och liftar
 */
function openWeather(forecastId,lang,site,siteId)
{
  IP_show('','/app/projects/common/templates/vader/femdygnsprognos08.php?KeepThis=true&lang=' + lang + '&site=' + site + '&siteId='+siteId+'&forecastId=' + forecastId + '&TB_iframe=true&height=610&width=890');

  var e = $('IP_window');
  e.style.border = "0px solid red";
}

function openPisteMap(lang,site,siteId)
{
  IP_show('','/app/projects/common/templates/lpv/lp.php?KeepThis=true&lang=' + lang + '&site=' + site + '&siteId='+siteId+'&TB_iframe=true&height=550&width=925');

  var e = $('IP_window');
  e.style.border = "0px solid red";
}



function openCamp1()
{
  IP_show('','/app/projects/common/templates/popups/salen_camp1.php?TB_iframe=true&height=312&width=848');

  var e = $('IP_window');
  e.style.border = "0px solid red";
}
function openCamp48()
{
  IP_show('','/app/projects/common/templates/popups/salen_camp48.php?TB_iframe=true&height=312&width=848');

  var e = $('IP_window');
  e.style.border = "0px solid red";
}
function openCamp51()
{
  IP_show('','/app/projects/common/templates/popups/salen_camp51.php?TB_iframe=true&height=312&width=848');

  var e = $('IP_window');
  e.style.border = "0px solid red";
}