$(document).ready(function(){
  // only run on the start page (the final page doesn't have a form on it)
  if(!document.questions)
    return;

  // show only the relevent contact details fields
  if(document.questions && document.questions.applicationType) {
    var elm = document.questions.applicationType
    for(var i=0; i<elm.length; i++) {
      if(elm[i].checked) {
        toggleApplicationType(elm[i].value);
        break;
      }
    }
  }

  // change the above as necessary
  $('input[name="applicationType"]').click(function(){
    toggleApplicationType(this.value);
  });
  $('input[name="applicationType"]').change(function(){
    toggleApplicationType(this.value);
  });

  function toggleApplicationType(type) {
    if(type=='personal') {
      $('.business').hide();
      $('.personal').show();
    } else {
      $('.personal').hide();
      $('.business').show();
    }
  }

  // show the correct number of input fields
  // to replace the domains textarea
  $('#domainsPara span.form-note').hide();
  $('#domains').hide();
  changeNumDomains();

  // change the above as necessary
  $('input[name="package"]').click(function(){
    changeNumDomains();
  });
  $('input[name="package"]').change(function(){
    changeNumDomains();
  });
  $('input[name="additionalDomain"]').change(function(){
    changeNumDomains();
  });

  function changeNumDomains() {
    // get the number of domains for the package we're on
    var packageDomains = 1;
    if(document.questions && document.questions.package) {
      var elm = document.questions.package
      for(var i=0; i<elm.length; i++) {
        if(elm[i].checked) {
          packageDomains = Number($('#'+elm[i].value+'NumDomains').text());
          if(isNaN(packageDomains))
            packageDomains = 1;
          break;
        }
      }
    }
    
    // get the number of additional domains
    var additionalDomains = 0;
    if(document.questions && document.questions.additionalDomain) {
      additionalDomains = Number(document.questions.additionalDomain.value);
      if(isNaN(additionalDomains))
        additionalDomains = 0;
    }
  
    // get the current list of domains
    if(document.questions && document.questions.domains) {
      var tmp = document.questions.domains.value.split('\n');
      var domains = [];
      for(var i=0, j=tmp.length; i<j; i++) {
        tmp[i] = $.trim(tmp[i]);
        if(tmp[i].length)
          domains[domains.length] = tmp[i];
      }
    }

    // create the required number of input elements
    var numDomains = packageDomains+additionalDomains;
    var elms = [];
    for(i=0; i<numDomains; i++) {
      elms[i] = '<input type="text" class="text register-txtbox" name="domains'
                +i+'" value="'+(i<domains.length?domains[i]:'')+ '" />';
    }
    $('#domainsinputs').remove();
    $('#domainsPara').append('<div id="domainsinputs">'
                             +elms.join('<br />')+'</div>');

    // remove the extra data if we previously had more domains
    // than the new number of input elements
    if(domains.length>numDomains)
      updateTextarea();

    // the textarea is the master data store, so any
    // changes to the content of the input elements
    // need echoing back to it
    $('#domainsinputs input').keyup(function() {
      updateTextarea();
    });
    $('#domainsinputs input').change(function(){
      updateTextarea();
    });
    
    function updateTextarea() {
      if(document.questions && document.questions.domains) {
        var content = [];
        $('#domainsinputs input').each(function(){
          content[content.length] = this.value;
        });
        document.questions.domains.value = content.join('\n');
      }
    }

  }

});

