

var MortgageWizard = {

  _mainDiv: 'MortgageWizard',
  steps: [],
  _currentQID: 0,
  _questionPrefix: 'mwQ',
  _totalQuestions:11,
  _tipPrefix: 'mwTip_',
  _answerPrefix: 'mwA_',

  init: function() {
    for(var i=2;i<=this._totalQuestions;i++) {
      if(document.getElementById(this._questionPrefix+i)){
        $(this._questionPrefix+i).hide();
        tips = $(this._questionPrefix+i).getElementsByClassName('mwTip');
        for (var n=0;n<tips.length;n++) {
          $(tips[n].id).hide();
        }
      }
    }
    answers = $(this._mainDiv).getElementsByClassName('mwAnswer');
    for (var i=0;i<answers.length;i++) {
      $(answers[i]).hide();
    }
  },

  startAgain: function() {
    this.init();
    inputs = $(this._mainDiv).getElementsByTagName('input');
    for (var i=0;i<inputs.length;i++) {
      inputs[i].checked=false;
    }
    $(this._questionPrefix+'1').show();
  },

  showAnswer: function(strAnswerIdPostFix,e) {
    //since we're showing the answer, we might as well hide all Qs
    //alert(this._answerPrefix+strAnswerIdPostFix);
    qs = $(this._mainDiv).getElementsByClassName('mwSection');
    for (var i=0;i<qs.length;i++) {
      $(qs[i]).hide();
    }
    if($(this._answerPrefix+strAnswerIdPostFix)) {
      $(this._answerPrefix+strAnswerIdPostFix).show();
    }
  },

  next: function(e) {
    this._currentQID = e.parentNode.parentNode.parentNode.parentNode.id;

    inputs = $(this._currentQID).getElementsByTagName('input');
    for (var i=0;i<inputs.length;i++) {
      if (inputs[i].checked==true){
        val = inputs[i].value;
        if (val.length>2){
          eval(val);
        }
        else{
          $(this._currentQID).hide();
          $(this._questionPrefix+val).show();
        }
        this.steps[this.steps.length]=this._currentQID;
        return;
      }
    }
    //if we ended up here, no selection was made.
    this.showError(this.ERROR.NO_SELECTION);
  },

  back: function(e) {
    //go to the last item in the steps array
    qId = e.parentNode.parentNode.parentNode.parentNode.id;
    qIdNum = qId.replace(this._questionPrefix,'');
    //prevQindex = qIdNum-2;//offset of 2
    //this.showError(this.dbgArray(this.steps),true);
    prevId = this.steps.pop();
    //this.showError(prevId);
    //if (this.steps.length>=(qIdNum-1)) {
      $(qId).hide();
      $(prevId).show();
      //pop the last item off the array

    //}
  },
  showTip:function(strIdPostFix) {
    if ($(this._tipPrefix+strIdPostFix)) {
      $(this._tipPrefix+strIdPostFix).show();
    }
  },
  hideTip:function(strIdPostFix) {
    if ($(this._tipPrefix+strIdPostFix)) {
      $(this._tipPrefix+strIdPostFix).hide();
    }
  },

  dbgArray:function(a) {
    if (a) {
      var s = '';
      var j = '';
      for(var i=0;i<a.length;i++) {
        if (i>0) {
          j = ',';
        }
        s = s+j+a[i];
      }
      return s;
    }
    return;
  },

  showError: function(strMsg,clear) {
    alert(strMsg);
    //$('noticeBox').update('<p></p>');
    //d = $('noticeBox');
    //if (clear==true) {
    //  d.innerHTML='';
    //}
    //d.innerHTML=d.innerHTML+'<p>'+strMsg+'</p>';
  },
  ERROR: {
    NO_SELECTION: "Please make a selection before attempting to continue."
  },
  Answers: {
    LO_DOC_LOANS: 'LoDocLoans',
    FIXED_RATE: 'FixedRateLoans',
    MORTGAGE_SIMPLIFIER: 'MortgageSimplifier',
    SMART_HOME_LOAN: 'SmartHomeLoan',
    ACTION_EQUITY_HOME_LOAN: 'ActionEquityHomeLoan',
    PRIORITY_COMMERCIAL_MORTGAGE: 'PriorityCommercialMortgage',
    COMMERCIAL_PROPERTY_FINANCE: 'CommercialPropertyFinance'
  }
};
addLoadListener(initMw);
function initMw() {
  MortgageWizard.init();
}
