var options = [];

  function chooseDropDown(elem) {
      event.preventDefault();

      $(elem).parent().parent().parent().children('dd').children('div.mutliSelect').children('ul').slideToggle('fast');

      $(".dropdown-x dd ul").hide();
      $(elem).parent().parent().next('dd').children('div').children('ul').show();
      ////console.log();
  }

  $(document).bind('click', function(e) {

      var $clicked = $(e.target);
      //alert($clicked);
      if (!$clicked.parents().hasClass("dropdown-x")) $(".dropdown-x dd ul").hide();
  });



  var checkboxString = '';
  var attcheckboxString = '';

  function getAddressList(dest_id){
              var array = [];
              array[1] = ['EXPERIAN  PO BOX 9701, ALLEN, TX 75013',
                  'EXPERIAN  PO BOX 4500, ALLEN, TX 75013',
                  //'EXPERIAN  PO BOX 2202, ALLEN, TX 75013',
                  'OTHER'
              ];

              array[2] = ['EQUIFAX  PO Box740256, ATLANTA,GA 30374-0256',
                  'EQUIFAX  PO Box740241, ATLANTA,GA 30374-0241',
                  'OTHER'
              ];

              array[3] = ['Trans Union PO Box 2000, CHESTER, PA 19016-2000',
                  'Trans Union  PO Box 1000, CHESTER, PA 19022-1000',
                  'Trans Union  PO Box 2000, CHESTER, PA 19022-2000',
                  'OTHER'

              ];

              array[4] = ['INNOVIS   PO Box 1640, Pittsburgh, PA 15230-1640',
                  'INNOVIS  875 Greentree Road, 8 Parkway Center, Pittsburgh, PA 15220',
                  'OTHER'
              ];

              array[5] = ['LexisNexis PO Box 105108, Atlanta, GA 30348','OTHER'];

              array[6] = [' SageStream, LLC, LexisNexis Risk Solutions Consumer Center P. O. Box 105108. Atlanta, Georgia 30348-5108','OTHER'];

              array[7] = ['Chex Systems ATTN: Consumer Relations, 7805 Hudson Rd., Ste. 100, Woodbury, MN 55125','OTHER'];

              array[8] = ['(US RESIDENTS) MIB Inc 50 Braintree Hill Park, Suite 400, Braintree, MA 02184-8734',
                  '(CANADIAN RESIDENTS) MIB Inc 330 university Avenue, Suite 501, Toronto, Canada M5G 1R7',
                  'OTHER'
              ];

              array[9] = ['FactorTrust PO Box 3653,Alpharhetta, GA 30023','OTHER'];

              return array[dest_id];
          }

  function getAddresses(value,title){

      var array = getAddressList(value);
      //var selectTag = '<select id="address_'+value+'" onchange="otherAddress(this)" data-placeholder="CHOOSE '+title+' ADDRESS"  name="destination_sub_menu_'+value+'[]" class="form-control select2" multiple="multiple">';
      var selectTag = '<dl class="dropdown-x" id="address_'+value+'">'
          +'<dt>'
          +'<div>'
          +'<a onclick="chooseDropDown(this);" href="javascript:;" class="form-control">'
          +'<span class="choose-dest">CHOOSE '+title+' ADDRESS</span>'
          +'<p class="multiSel"></p>'
          +'</a>'
          +'</div>'
          +'</dt>'
          +'<dd>'
          +'<div class="mutliSelect">'
          +'<ul>';

      var count = 1
      for(var x in array){
          var val = array[x];
          if(val == 'OTHER'){
              val = -1;
              selectTag = selectTag + '<li><input onclick="getDestOption(this);"  data-view="'+array[x]+'"  type="checkbox" value = "'+val+'" name="destination_sub_menu_'+value+'[]"> '+array[x]+'</li>';//'<option value = "'+val+'">'+array[x]+'</option>';
          }else{
              selectTag = selectTag + '<li><input onclick="getDestOption(this);" data-view="'+array[x]+'" type="checkbox" value = "'+array[x]+'" name="destination_sub_menu[]"> '+array[x]+'</li>';
          }
          count++;
      }

      selectTag = selectTag  +'</ul>'
          +'</div>'
          +'</dd>'
          +'</dl>';

      var addressHTML = '<div class="remove-all"><div class="form-group">'
          +'<label  class="col-sm-5 control-label"></label>'
          +'<div class="col-sm-12">'
          + selectTag
          +'</div>'
          +'</div>'
          +'<div class="form-group" id="address_other" style="display:none;">'
          +'<label  class="col-sm-5 control-label">OTHER</label>'
          +'<div class="col-sm-12">'
          +'<textarea id="destination_sub_menu_other_'+value+'" class="form-control" name="destination_sub_menu_other_'+value+'"></textarea>'
          +'</div>'
          +'</div></div>';

      $('#address-div').append(addressHTML);
      $('.select2').select2();


      // return selectTag;


  }

  function get_destination_name(dest_value) {
      var destination = ['','EXPERIAN DESTINATIONS','EQUIFAX DESTINATIONS','TRANS UNION DESTINATIONS','INNOVIS DESTINATIONS','LEXISNEXIS DESTINATIONS','SAGESTREAM LLC DESTINATIONS',        'CHEX SYSTEMS DESTINATIONS','MIB INC DESTINATIONS','FACTOR TRUST DESTINATIONS','OTHER'];
      return destination[dest_value];
  }

  function otherAddress(selectTag){
      var value =  $(selectTag).val();
      var destValue = selectTag.name.replace('destination_sub_menu_','').replace('[]','');
      if($.inArray("-1", value) !== -1){
          var placeholder = get_destination_name(destValue);
          var tag =  $(selectTag).parent().parent().next('div');
          //tag.children('div').children('textarea').attr('name','destination_sub_menu_'+selectTag.value);
          tag.children('div').children('textarea').attr("placeholder",placeholder);
          tag.css('display','block');
      }else{
          $(selectTag).parent().parent().next('div').css('display','none');
      }
  }
  function getOption(elem) {
              $('.view-allcheck').children().remove();
              var isFirst = $('input[name=isFirst]').val();
              attAllcheck = '';
              var destCount = $('#maindestination ul li input:checked').length;

              var attLength = destCount;
              if(destCount > 1){
                  attAllcheck = '<input data-id="' + attLength + '" style="margin-left: 10px;" type="checkbox" name=all_att_check_' + attLength + ' id="all_att_check_' + attLength + '" onclick="setAllCheck(this)"><span> Check all</span>';
                  $('.view-allcheck').append(attAllcheck) ;
              }
              //alert(destCount);
              //var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
              var  title = $(elem).data('view');
              //console.log(title);
              if(title=='EXPERIAN DESTINATIONS'){
                var checkboxes = $('.check-exp').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='EQUIFAX DESTINATIONS'){
                var checkboxes = $('.check-equ').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='TRANS UNION DESTINATIONS'){
                var checkboxes = $('.check-tra').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='INNOVIS DESTINATIONS'){
                var checkboxes = $('.check-ino').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='LEXISNEXIS DESTINATIONS'){
                var checkboxes = $('.check-lex').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='SAGESTREAM LLC DESTINATIONS'){
                var checkboxes = $('.check-sag').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='CHEX SYSTEMS DESTINATIONS'){
                var checkboxes = $('.check-chex').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if(title=='MIB INC DESTINATIONS'){
                var checkboxes = $('.check-mib').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              else if (title=='FACTOR TRUST DESTINATIONS'){
                var checkboxes = $('.check-fact').first();
                checkboxes.prop('checked', $(elem).is(':checked'));
              }
              var checkVal = $(elem).val();
              var dfClass = '';
              if(checkVal==11){
                  dfClass = 'dfclass'
              }
              if ($(elem).is(':checked')) {
                  var html = '<span class="hida" title="' + title + '">' + title + '</span>';
                  $(elem).parent().parent().parent().parent().prev('dt').children('div').children('a').children('p').append(html);

                  //alert($(this).val());

                  var dest_other = '<div class="form-group remove-all" id="dest_other_'+checkVal+'">'
                      +'<label  class="col-sm-5 control-label">OTHER</label>'
                      +'<div class="col-sm-12">'
                      +'<textarea id="input_order_address" class="form-control" name="other_address"></textarea>'
                      +'</div>'
                      +'</div></div>';

                  if(checkVal==10){
                      $('#address-div').after(dest_other);
                  }else{
                      if(checkVal !=11){
                          getAddresses(checkVal,title);
                      }
                  }
                  // var attIndicator =  $('.dest-checkbox').parent().prevAll('div')[0];
                  ////console.log($('.add-new-attach').length);

                  var appendText = '<li><input onclick="makeAllCheck(this)" class="dest_check_'+checkVal+' remove-all '+dfClass+'" id="dest_check_'+checkVal+'" name="dest_check_1[]" type="checkbox" value="'+checkVal+'" /> '+title+'</li>';

                  var appendTextAtt = '<li><input onclick="makeAllCheck(this)" class="att_check_' + checkVal + ' remove-all" id="att_check_' + checkVal + '" name="att_check_1[]" type="checkbox" value="' + checkVal + '" /> ' + title + '</li>';
                  if(checkVal == 11) {
                      appendTextAtt = '';
                  }
                  checkboxString = checkboxString + appendText;
                  attcheckboxString = attcheckboxString + appendTextAtt;
                  $('.dest-checkbox').children('ul').append(appendText);
                  $('.att-checkbox').children('ul').append(appendTextAtt);
                  //alert(address);
                  $(elem).parent().parent().parent().parent().prev('dt').children('div').children('a').children('p.multiSel').prev("span").hide();
              } else {
                  var removal = $(elem).val();
                  $('#destination_sub_menu_other_'+removal).parents('div#address_other').remove();
                  $(elem).parent().parent().parent().parent().prev('dt').children('div').children('a').children('p').children('span[title="' + title + '"]').remove();
                  $checkStr = '<li><input onclick="makeAllCheck(this)" class="dest_check_'+removal+' remove-all '+dfClass+'" id="dest_check_'+removal+'" name="dest_check_1[]" type="checkbox" value="'+removal+'" /> '+title+'</li>';
                  if(checkVal != 11) {
                      $checkAttStr = '<li><input onclick="makeAllCheck(this)" class="att_check_' + removal + ' remove-all" id="att_check_' + removal + '" name="att_check_1[]" type="checkbox" value="' + removal + '" /> ' + title + '</li>';
                  }
                  checkboxString = checkboxString.replace($checkStr,'');
                  attcheckboxString = attcheckboxString.replace($checkAttStr,'');
                  $('#address_'+removal).parent().parent().remove();
                  $('.dest_check_'+removal).parent().remove();
                  $('.att_check_'+removal).parent().remove();
                  $('#dest_other_'+removal).remove();

                  /*var ret = $(".hida");
                  $('.dropdown-x dt a').append(ret);*/

              }

              var plen = $(elem).parent().parent().parent().parent().prev('dt').children('div').children('a').children('p').children('span').length;

              if(plen > 1){
                  $('.view-allcheck').show();
              }else {
                  $('.view-allcheck').hide();
              }
              ////console.log('test : '+checkboxString);
              if(!$('.hida').is(':visible')){
                  $(".choose-dest").show();
              }

              if(isFirst == 1){
                  var count = 1;
                  $('.att-checkbox').each(function(){
                      $(this).children('ul')
                             .children('li')
                             .each(function(){
                          $(this).children('input').attr('name','att_check_'+count+'[]');
                      });
                      count++;
                  })
              }
  //            if(isFirst == 1){
  //                var count = 1;
  //                $('input[name=att_check_1\\[\\]]').each(function(){
  //                    $(this).attr('name','att_check_'+count+'[]');
  //                    $(this).attr('id','att_check_'+count);
  //                    $(this).attr('class','att_check_'+count+' remove-all');
  //                    count++;
  //                });
  //            }
          };


  function getDestOption(elem){
            var value = $(elem).val();
            //alert(value);
            var flag = false;
            var  title = $(elem).data('view');
            var multiSelP =  $(elem).parent().parent().parent().parent().prev('dt').children('div').children('a').children('p.multiSel');
            var tag =  $('#address_'+value).next('div');
            if ($(elem).is(':checked')) {
                var html = '<span class="hida" title="' + title + '">' + title + '</span>';
                multiSelP.append(html);
                multiSelP.prev("span.choose-dest").hide();


            }else{
                multiSelP.children('span[title="' + title + '"]').remove();

            }

            if(value == -1){
                //  //console.log();
                $(elem).parents('dl').parent('div').parent('div').next('div').toggle();
            }
        }


