h_onclick.prototype.menu = function (a,event) {
    if (a.tagName == 'LABEL') {
      var menu = new menuitem(a);
      return false;
    }
    return true;
}
var menuitem = function (a) { this.openmenu(a); }

var locationdistance;
function getSelectPreview(e,delim){
  var mytext = '';
  if(e.options[e.selectedIndex].value == '*'){
    //leave it blank
  }else if(e.name == 'dist_max_value'){
    //replace any- in distance mytext = '*';
    if (e.options[e.selectedIndex].value == '999999'){
      locationdistance = 1;
    }else{
      locationdistance = 0;
      mytext =e.options[e.selectedIndex].text+delim;
    }
  }else if(e.name == 'state' || e.name == 'country'){
    if(locationdistance == 1){
      mytext = '';
    }else{
      mytext = e.options[e.selectedIndex].text+delim;
    }
  }else{
    if (e.name == 'LOCATION_OPTION'){
      if(locationdistance == 1){
        mytext = '';
      }else if(e.options[e.selectedIndex].value == 1){
        mytext = locationtxt1;
      }else if(e.options[e.selectedIndex].value == 2){
        mytext = locationtxt2;
      }else{
        mytext = locationtxt2;
      }
    }else{
      mytext = e.options[e.selectedIndex].text+delim;
    }
  }
  return mytext;
}

  menuitem.prototype.openmenu = function (a) {
    var toggle = '';
    var parentnode = a.parentNode;
    for (var i=0;i<parentnode.childNodes.length;i++) {
      if (parentnode.childNodes[i].tagName == 'DIV') {
        //terrill parentnode.childNodes[i].style.display = 'none'
      } else if (parentnode.childNodes[i].tagName == 'LABEL' && parentnode.childNodes[i] != a) {
        //terrill parentnode.childNodes[i].className = 'inactive'
      } else if ( parentnode.childNodes[i] == a) {

        if(parentnode.childNodes[i].className == 'active'){
          parentnode.childNodes[i].className = 'inactive'
          toggle = 'inactive'
        }else if(parentnode.childNodes[i].className == ''){
          parentnode.childNodes[i].className = 'active'
          toggle = 'active'
        }else{
          parentnode.childNodes[i].className = 'active'
          toggle = 'active'
        }

      }
    }
    while (a.nextSibling&&a.tagName!='DIV'){a=a.nextSibling}
    if (toggle == 'inactive'){
      a.style.display = 'none';
      if (a.nextSibling.tagName == 'DL'){
        a.nextSibling.style.display = 'block'
        if (!a.nextSibling.innerHTML)a.nextSibling.style.display = 'none'
      }else if(a.nextSibling.nextSibling.tagName == 'DL'){
        a.nextSibling.nextSibling.style.display = 'block'
        if (!a.nextSibling.nextSibling.innerHTML)a.nextSibling.nextSibling.style.display = 'none'
      }
    }else{
      a.style.display = 'block';
      if (a.nextSibling.tagName == 'DL'){
        a.nextSibling.style.display = 'none'
      }else if(a.nextSibling.nextSibling.tagName == 'DL'){
        a.nextSibling.nextSibling.style.display = 'none'
      }
    }
  }

  h_onchange.prototype.create_preview = function (a,event) {
    if (a.form.name == 'SEARCHFORM') {
      while (a.parentNode&&a.parentNode.parentNode.tagName!='FORM') {a=a.parentNode;}
      if (a&&a.tagName=='DIV') {
        while (a.previousSibling&&a.tagName!='LABEL') {a=a.previousSibling;}
        if (a&&a.tagName=='LABEL') {
          previewitem.prototype.get_stringvalue(a)
        }
      }
      return false;
    }
    return true;
  }
  var previewitem = function (a) {
    var temp = document.createElement('DIV')
    var bContainer = '<div class="search_arrow_container"><div class="search_arrow"><span class="search_arrow_border">&nbsp;</span><span class="search_arrow_fill">&nbsp;</span></div></div>';
    temp.setAttribute('id','temp');
    var mytemp = a;
    while (mytemp.nextSibling&&mytemp.tagName!='DIV'){mytemp=mytemp.nextSibling}
    var ps = '';
    var formfields = this.getfields(mytemp,'SELECT','INPUT')
    for (var i=0;i<formfields.length;i++) {
      if (formfields[i].value != ''){
        var delim = (formfields[i].className == 'range') ? txt_to :', '
      }else{
        var delim = ''
      }
      if (formfields[i].tagName == 'INPUT' && formfields[i].type == 'checkbox' && formfields[i].checked ) {
        ps += this.gettag(formfields[i])+delim
      } else if (formfields[i].tagName == 'SELECT' && !formfields[i].disabled) {
        replacetxt = getSelectPreview(formfields[i],delim)
        ps += replacetxt
      } else if (formfields[i].tagName == 'INPUT' && formfields[i].type == 'text' && !formfields[i].disabled) {



      if(formfields[i].name == 'city' || formfields[i].name == 'zip'){
        if(locationdistance == 1){
          ps += ''
        }else{
          ps += formfields[i].value+delim
        }
      }else{//other text fields display 
        ps += formfields[i].value+delim
      }


      }
    }
    if (ps.length > 1){
      temp.style.display = 'block'
    }
    var pos = this.findPos(a)
    temp.style.top = pos[1];
    temp.innerHTML = bContainer + ps.replace(/, $/,'') + "</div>";
    document.body.appendChild(temp)
  }
  previewitem.prototype.findPos = function (obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
      curleft = obj.offsetLeft;
      curtop = obj.offsetTop;
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft;
        curtop  += obj.offsetTop;
      }
    }
    return [curleft,curtop];
  }
  previewitem.prototype.getfields = function () {
    var temp = []
    for (var i=1;i<arguments.length;i++) {
      for (var k=0;k<arguments[0].getElementsByTagName(arguments[i]).length;k++) {
        temp[temp.length] = arguments[0].getElementsByTagName(arguments[i])[k]
      }
    }
    return temp;
  }
  previewitem.prototype.gettag = function (a) {
    if (a.nextSibling.tagName == 'SPAN') {
      return a.nextSibling.innerHTML;
    } else {
      return '';
    }
  }
  previewitem.prototype.get_submenu = function (a,b) {
    for (var i=0;i<b.getElementsByTagName('DIV').length;i++) {

      if (b.getElementsByTagName('DIV')[i].getAttribute('name') == a.value) {
        b.getElementsByTagName('DIV')[i].style.display = 'block';
      } else {
        b.getElementsByTagName('DIV')[i].style.display = 'none';
      }
      var formfields = this.getfields(b.getElementsByTagName('DIV')[i],'INPUT','SELECT')
      for (var k=0;k<formfields.length;k++) {
        formfields[k].disabled = (b.getElementsByTagName('DIV')[i].style.display == 'none') ? true : false;
      }

    }
  }

  previewitem.prototype.sync_linked = function (a,b) {
    if (a.selectedIndex>b.selectedIndex) {
      b.selectedIndex = 0;
    } 
    for (var i=1;i<a.options.length;i++) {
      if (a.selectedIndex > 0) {
        if (i<a.selectedIndex)  {
          b.options[i].style.display = "none";
        } else {
          b.options[i].style.display = "block";
        }
      }
    }
  }
  previewitem.prototype.sync_two_forms = function (a,b) {
    if(a){
      var bsum = b.getElementsByTagName('INPUT');
      for (var i=0;i<a.elements.length;i++) { 
        if (a.elements[i].type != 'checkbox' && b.elements[a.elements[i].name]) {
          b.elements[a.elements[i].name].value = a.elements[i].value
        } else if (a.elements[i].checked) {
          for (var c=0;c<bsum.length;c++) {
            if (a.elements[i].name == bsum[c].name && a.elements[i].value == bsum[c].value) bsum[c].checked = true;
          }
        }
      }
    }
  }
  previewitem.prototype.toggle_sex_field = function (a,label1,label2,field1,field2,label3,field3) {
    if (a.value == 1){
      document.getElementById(label1).style.display = '';
      document.getElementById(field1).style.display = '';
      document.getElementById(label2).style.display = 'none';
      document.getElementById(field2).style.display = 'none';
      document.getElementById(label3).style.display = 'none';
      document.getElementById(field3).style.display = 'none';
    } else if (a.value == 2){
      document.getElementById(label1).style.display = 'none';
      document.getElementById(field1).style.display = 'none';
      document.getElementById(label2).style.display = '';
      document.getElementById(field2).style.display = '';
      document.getElementById(label3).style.display = 'none';
      document.getElementById(field3).style.display = 'none';
    } else if (a.value == 5){
      document.getElementById(label1).style.display = 'none';
      document.getElementById(field1).style.display = 'none';
      document.getElementById(label2).style.display = '';
      document.getElementById(field2).style.display = '';
      document.getElementById(label3).style.display = '';
      document.getElementById(field3).style.display = '';
    } else if (a.value == 6){
      document.getElementById(label1).style.display = '';
      document.getElementById(field1).style.display = '';
      document.getElementById(label2).style.display = 'none';
      document.getElementById(field2).style.display = 'none';
      document.getElementById(label3).style.display = '';
      document.getElementById(field3).style.display = '';
    } else {
      document.getElementById(label1).style.display = '';
      document.getElementById(field1).style.display = '';
      document.getElementById(label2).style.display = '';
      document.getElementById(field2).style.display = '';
      document.getElementById(label3).style.display = '';
      document.getElementById(field3).style.display = '';
    }
  }
  previewitem.prototype.get_stringvalue = function (a) {
    var temp = a;
    var mytemp = a;
    
    while (mytemp.nextSibling&&mytemp.tagName!='DIV'){mytemp=mytemp.nextSibling}
    
    if (mytemp != a){
      if (mytemp.nextSibling&&mytemp.nextSibling.tagName=='DL'){ //IE
        var previewbox = mytemp.nextSibling;
        temp = mytemp.nextSibling;
      } else if (mytemp.nextSibling.nextSibling&&mytemp.nextSibling.nextSibling.tagName=='DL'){ //FF
        var previewbox = mytemp.nextSibling.nextSibling;
        temp = mytemp.nextSibling.nextSibling;
      }else{
      
      }
    }
    
    var ps = '';
    var blank = '';
    var coma = '';
    var delim = '';
    var formfields = this.getfields(mytemp,'SELECT','INPUT')
    for (var i=0;i<formfields.length;i++) {
      if (previewbox){
        coma = '<br/>';
        if (blank == '-') coma = ''
        blank = (formfields[i].className == 'range') ? '-' :''
        if (formfields[i].value != ''){        
        delim = (formfields[i].className == 'range') ? txt_to :''
        }
        if (formfields[i].className == 'range' && formfields[i].name == 'dist_max_value'){ 
          coma = ' ';
          delim = ' ';
        }

        if (formfields[i].name == 'LOCATION_OPTION' || formfields[i].name == 'country' || formfields[i].name == 'state' || formfields[i].name == 'city' || formfields[i].name == 'zip'){
          coma = ' ';
          delim = ' ';
        }

      }else{
        coma = (formfields[i].className == 'range') ? '' :', '
        if (blank == '-') coma = ''
        blank = (formfields[i].className == 'range') ? '-' :''
        delim = (formfields[i].className == 'range') ? txt_to :''      
      }
      if (formfields[i].tagName == 'INPUT' && formfields[i].type == 'checkbox' && formfields[i].checked ) {
        if(ps.length > 0) ps += coma
        ps += this.gettag(formfields[i]) + delim 
      } else if (formfields[i].tagName == 'SELECT' && !formfields[i].disabled) {

        if(formfields[i].selectedIndex == -1){
          formfields[i].selectedIndex = 0;
          if(ps.length > 0) ps += coma
          replacetxt = getSelectPreview(formfields[i],delim)
          ps += replacetxt
        }else{
          if (formfields[i].options[formfields[i].selectedIndex].text == ''){
            if(ps.length > 0) ps += coma
            replacetxt = getSelectPreview(formfields[i],delim)
            ps += replacetxt
          }else{
            if(ps.length > 0) ps += coma
            replacetxt = getSelectPreview(formfields[i],delim)
            ps += replacetxt
          }
        }

      } else if (formfields[i].tagName == 'INPUT' && formfields[i].type == 'text' && !formfields[i].disabled) {

      if(formfields[i].name == 'city' || formfields[i].name == 'zip'){
        if(locationdistance == 1){
          ps += ''
        }else{
          if(ps.length > 0 && formfields[i].value) ps += coma
          ps += formfields[i].value + delim 
        }
      }else{//other text fields display
        ps += formfields[i].value+delim
      }

      }
    }
    var sizer = document.getElementById('preved');
    var origtag = temp.innerHTML.replace(/<.+>/i,'');
    var words = ps.split(/\s+/);
    var ktemp = '';
    var previewsize = 186;
    
    if (previewwidth){
      previewsize = previewwidth;
    }
    for (var i=0;i<words.length;i++) {
      ktemp += " "+words[i]
      sizer.innerHTML = origtag+" <span>"+ktemp+"</span>";
      if (parseInt(sizer.clientWidth) > previewsize) { //tcouther 3-22-09 - was 185 added previewwidth var for flexibility 
        ktemp = ktemp.replace(/(\S+)$/,'').replace(/\,\s+?$/,'')+'...';
        break;
      }
    }
    if (previewbox){
      if (origtag == '' && (ktemp == ' ' || ktemp == '')){
        temp.innerHTML = '';
        temp.style.display = 'none';
      }else{
        temp.innerHTML = "<dd>" + origtag + ktemp + "</dd>";
        temp.onclick = function(){ // adds onclick to preview 
          menuitem.prototype.openmenu(a)
        };
      }
    }else{
      temp.innerHTML = origtag+" <span>"+ktemp+"</span>";
    }
  }
  previewitem.prototype.inline_preview = function () {
    for (var i=0;i<document.getElementById('collapsible_search').getElementsByTagName('LABEL').length;i++) {
      this.get_stringvalue(document.getElementById('collapsible_search').getElementsByTagName('LABEL')[i]);
    }
  }
  self.kts = function () {
    h_onchange.prototype.create_preview(event.srcElement,event);
  }
  previewitem.prototype.init = function () {
    previewitem.prototype.sync_two_forms(document.forms.SEARCHFORM_perl,document.forms.SEARCHFORM);
    previewitem.prototype.get_submenu(document.forms.SEARCHFORM.LOCATION_OPTION,document.getElementById('LOCATION_OPTION_submenu'));
    if(lvl == 'premium'){
      if(document.forms.SEARCHFORM.imin_height && document.forms.SEARCHFORM.imax_height){
        previewitem.prototype.sync_linked(document.forms.SEARCHFORM.imin_height,document.forms.SEARCHFORM.imax_height);
      }
    }
    previewitem.prototype.inline_preview();
    if (document.all) {
      for (var i=0;i<document.forms.SEARCHFORM.elements.length;i++) {
        document.forms.SEARCHFORM.elements[i].onfocus = kts;
        document.forms.SEARCHFORM.elements[i].onblur = kts;
      }
    }
  }
  if(!document.all){
    previewitem.prototype.init()
  } else {
    window.onload = previewitem.prototype.init;
  }
