var agr_choices = null, agr_choice, agr_atStart, agr_overPulldown = false
agr_it_arr = new Array(4)
for (i = 0; i < agr_it_arr.length; i++) {
  agr_it_arr[i] = new Array(2);
}
agr_it_arr[0][0] = "country_name", agr_it_arr[0][1] = true
//agr_it_arr[1][0] = "partner", agr_it_arr[1][1] = true
agr_it_arr[1][0] = "company", agr_it_arr[1][1] = true
agr_it_arr[2][0] = "product", agr_it_arr[2][1] = true


var agr_cp_ch = document.getElementById('company_check');
if (agr_cp_ch) {
   if (agr_cp_ch.value.length > 0) {
      var agr_cp = document.getElementById('company');      
      agr_cp.style.color = '#000000'
   }
}
var agr_pr_ch = document.getElementById('product_check');
if (agr_pr_ch) {
   if (agr_pr_ch.value.length > 0) {
      var agr_pr = document.getElementById('product');
      agr_pr.style.color = '#000000'
   }
}
var agr_co = document.getElementById('country');
if (agr_co) {
   if (agr_co.value.length > 0) {
      var agr_co_ch = document.getElementById('country_name');
      if (agr_co_ch) {
         agr_co_ch.style.color = '#000000'
      }
   }
}


function require_pulldown(e,f,message) {
  if (f['country'].value == '') {
    var elt = document.getElementById('js_error_message');
    elt.innerHTML = elt.htmlContent = '<div class="messages">' + message + '</div>';
    return false;
  } else {
    return true;
  }
}

function clear_choices() {
  agr_choices = null  
}

function reset_choices(container) {
  agr_choices = document.getElementById(container).getElementsByTagName('a');
  agr_choice = agr_choices[0]
  agr_atStart = true
  var clist = document.getElementById(container);

  clist.scrollTop = 0

  if (agr_choices.length >= 15) {
    clist.style.height="200px"
  } else {
    clist.style.height=""
  }
}

function get_pulldown_list(e,server,container,f,country,locale,value_id,text_id,min_char_count,show_message,message) {
  var clist = document.getElementById(container);
  switch(e.keyCode){

    case 13: // enter
      hide_pulldown_list(container);      
      break;

    case 38: // up arrow

      if(agr_choices === null) {
        break;
      }

      if(agr_choice) {
        agr_choice = one_item_higher(agr_choice)
        agr_choice.style.textDecoration="underline"

        clist.scrollTop = clist.scrollTop - 13;
        
        //agr_choice.scrollIntoView(false);  
        //agr_choice.scrollIntoView(true);  
        var prev_choice = one_item_lower(agr_choice)
        prev_choice.style.textDecoration="none"            
        new_name = agr_choice.name.replace('\\','')
        update_pulldown(container,value_id,text_id,agr_choice.innerHTML,new_name)
      }
      break;
    case 40: // down arrow

      if(agr_choices === null) {
        break;
      }

      if(!agr_atStart){
        agr_choice = one_item_lower(agr_choice)

        clist.scrollTop = clist.scrollTop + 13;

      }
      agr_choice.style.textDecoration="underline"
      //agr_choice.scrollIntoView(false);
      //agr_choice.scrollIntoView(true);  
      new_name = agr_choice.name.replace('\\','')
      update_pulldown(container,value_id,text_id,agr_choice.innerHTML,new_name)
      if(!agr_atStart){
        var prev_choice = one_item_higher(agr_choice)
        prev_choice.style.textDecoration="none" 
      }
      agr_atStart = false   
      break;     
    default:
      if(f.value.length >= min_char_count) {
        var arg = f.value;
        new Ajax.Request(server + encodeURIComponent(arg) + ';country=' + encodeURIComponent(country) + ';locale=' + encodeURIComponent(locale) + ';container=' + encodeURIComponent(container) + ';value_id=' + encodeURIComponent(value_id) + ';text_id=' + encodeURIComponent(text_id),
          {
            method:'get',
            onSuccess: function(transport){
              var response = transport.responseText || '';
              if (response == 'No Matching Partners') {

                if (show_message == 'true') {
                  var no_matches = '<span class="pulldown">' + message + '</span>'
                  clist.innerHTML = clist.htmlContent = no_matches;
                  clist.style.visibility = 'visible'
                  clist.style.display = 'block' // for firefox on mac
                  reset_pulldown(value_id)
                  setTimeout('reset_choices("'+container+'")',5);
                } else {
                  clist.style.visibility = 'hidden'
                  clist.style.display = 'none' // for firefox on mac
                }

              } else if (response.split('|')[0] == '1 MATCH') {
                clist.style.visibility = 'hidden'
                clist.style.display = 'none' // for firefox on mac 
                if (check_pulldown(value_id,response.split('|')[2])) {
                  update_and_hide_pulldown(container,value_id,text_id,response.split('|')[1],response.split('|')[2])
                }
              } else {
                clist.innerHTML = clist.htmlContent = response;
                clist.style.visibility = 'visible'
                clist.style.display = 'block' // for firefox on mac
                if (value_id != text_id) {
                  reset_pulldown(value_id)
                }
                setTimeout('reset_choices("'+container+'")',5);
              }
            },
            onFailure: function(){
              clist.style.visibility = 'hidden'
              clist.style.display = 'none' // for firefox on mac
            }
          });  
      } else {
        clist.style.visibility = 'hidden'
        clist.style.display = 'none' // for firefox on mac
      }   
      break;
  }         
}

function hide_initial_text(value_id) {
  for (i = 0; i < agr_it_arr.length; i++) {
    if (agr_it_arr[i][0] == value_id) {
      if (agr_it_arr[i][1] == true) {
        var value_field = document.getElementById(value_id);
        value_field.value = ''
        value_field.style.color = '#000000'
        agr_it_arr[i][1] = false
      }
    }
  }
}

function hide_initial_text_new(value_id,check_field) {
  var value_field = document.getElementById(value_id);
  var check_f = document.getElementById(check_field)
  if (check_f.value == '') {
    for (i = 0; i < agr_it_arr.length; i++) {
      if (agr_it_arr[i][0] == value_id) {
        if (agr_it_arr[i][1] == true) {
          value_field.value = ''
          value_field.style.color = '#000000'
          agr_it_arr[i][1] = false
        }
      }
    }
  } else {
    value_field.style.color = '#000000'
  }
}

function check_pulldown(value_id,pulldown_value) {
  var value_field = document.getElementById(value_id);
  if (value_field.value.length == 0) {
    return true
  } else if (value_field.value != pulldown_value) {
    return true
  } else {
    return false
  }
}

function reset_pulldown(value_id) {
  var value_field = document.getElementById(value_id);
  value_field.value = ''
} 

function show_pulldown_list(list_name) {
  agr_overPulldown = true
  var clist = document.getElementById(list_name);
  clist.style.visibility = 'visible'
  clist.style.display = 'block' // for firefox on mac
}

function hide_pulldown_list(list_name) {
  clear_choices();
  agr_overPulldown = false
  var clist = document.getElementById(list_name);
  clist.style.visibility = 'hidden' 
  clist.style.display = 'none' // for firefox on mac
}

function conditional_hide_pulldown(list_name) {
  if (agr_overPulldown == false) {
    hide_pulldown_list(list_name);
  }
}

function update_and_hide_pulldown(list_name,value_id,text_id,item_name,item_value) {
  hide_pulldown_list(list_name);
  //update_pulldown(list_name,value_id,text_id,item_name,item_value);
  new_item_value = item_value.replace('&amp;','&')
  new_item_name = item_name.replace('&amp;','&')
  var text_field = document.getElementById(text_id);
  text_field.value = new_item_name
  var value_field = document.getElementById(value_id);
  value_field.value = new_item_value
}

function update_pulldown(list_name,value_id,text_id,item_name,item_value) {
  show_pulldown_list(list_name);
  new_item_value = item_value.replace('&amp;','&')
  new_item_name = item_name.replace('&amp;','&')
  var text_field = document.getElementById(text_id);
  text_field.value = new_item_name
  var value_field = document.getElementById(value_id);
  value_field.value = new_item_value
}

function one_item_lower(choice) {
  var this_choice = choice
  if (this_choice.nextSibling != null) {
    var next_choice = this_choice.nextSibling
    while(next_choice.nodeType != 1 || next_choice.nodeName != 'A'){
      next_choice = next_choice.nextSibling
      if(!next_choice){
        break;
      } else {
        this_choice = next_choice
      }
    }
  }
  return this_choice;  
}

function one_item_higher(choice) {
  var this_choice = choice
  var prev_choice = this_choice.previousSibling
  if(prev_choice){
    while(prev_choice.nodeType != 1 || prev_choice.nodeName != 'A'){
      prev_choice = prev_choice.previousSibling
      if(!prev_choice){
        break;
      } else {
        this_choice = prev_choice
      }
    }
  }
  return this_choice;  
}
