var translation_search = false;

var on_s_ac = false;
var clicked_s_ac = false;

function changeSearchMethod(method) {
  if (method == "search") {
    document.getElementById("translate_row").style.display="none";
    document.getElementById("translate").style.display="none";
    document.getElementById("search_button").style.display="";
    document.getElementById("sb_search").className = "search_box_tabs_selected";
    document.getElementById("sb_translate").className = "search_box_tabs";
    translation_search = false;
  }
  else {
    document.getElementById("translate_row").style.display="";
    document.getElementById("translate").style.display="";
    document.getElementById("search_button").style.display="none";
    document.getElementById("sb_search").className = "search_box_tabs";
    document.getElementById("sb_translate").className = "search_box_tabs_selected";
    translation_search = true;
  }
}
function toggleDisplay(element) {
  if (document.getElementById(element).style.display == "none" || document.getElementById(element).style.display == "") {
    document.getElementById(element).style.display = "block";
  }
  else {
    document.getElementById(element).style.display = "none";
  }
}
function checkSearchParams() {
  if (translation_search) {
    if (document.getElementById("source_lang").value == "" || document.getElementById("target_lang").value == "") {
      alert("Please enter both a source and target language.");
      return false;
    } else return true;
  } else return true;
}
function elm(element) {
  return document.getElementById(element);
}
function toggleDisplay(element) {
  if (document.getElementById(element).style.display == "none" || document.getElementById(element).style.display == "") {
    document.getElementById(element).style.display = "block";
  }
  else {
    document.getElementById(element).style.display = "none";
  }
}
function menuHover(element_id, status) {
  if (status == 1) document.getElementById(element_id).className = "wikimenu_on";
  else document.getElementById(element_id).className = "wikimenu_off";
}
function showAjaxPopup(id, url, title) {
  var element = document.getElementById(id);
  element.style.visibility = "visible";
  element.innerHTML = "<table id=\"draggable"+id+"\" border=0 cellpadding=0 cellspacing=0 style=\"background-color: #4C6384; border: solid 1px #4C6384\"><tr id=\"handle"+id+"\"><td bgcolor=\"#4C6384\" style=\"color:#ffffff;font-weight:bold;font-size:11px;padding:1px;cursor:move;\">"+title+"</td><td align=right bgcolor=\"#4C6384\" style=\"cursor:move;\" id=\"handle"+id+"\"><font color=\"white\" title=\"Click to close\" class=ch onClick=\"hideAjaxPopup('"+id+"')\"><b>[X]</b></font></td></tr><tr><td colspan=\"2\" style=\"padding: 3px; background-color: #F1F5F5;\"><span id=\"popup_content"+id+"\"><img src=\"/images/wikiwords/loading.gif\"> Loading...</span></td></tr></table>";
  $("#popup_content" + id).load(url);
  $("#draggable"+id).draggable();
}
function hideAjaxPopup(id) {
  document.getElementById(id).style.visibility = "hidden";
}
function errCheck(id, name) {
  if (document.getElementById(id).value == "") alert("Please enter a  "+ name +" .")
}

function processAjaxSubmit (url, target_id, not_empty_id, close_div, concept_id) {
  if (not_empty_id != "" && document.getElementById(not_empty_id).value == "") alert("Please enter a  "+ not_empty_id +".");
  else {
    document.getElementById(target_id).innerHTML = "<img src=\"/images/wikiwords/loading.gif\"> Loading...";
    $("#" + target_id).load(url);
    if (target_id == "definitions" && document.getElementById("translation").value != "") {
      reloadSection('/ajax/ajax_wikiwords.php?display=terms&concept_id="+concept_id+"', 'translations');
    }
    if (target_id == "translations" && document.getElementById("definition").value != "") {
      reloadSection('/ajax/ajax_wikiwords.php?display=defs&concept_id="+concept_id+"', 'definitions');
    }
    if (close_div != "") document.getElementById(close_div).style.display = "none";
  }
}
function reloadSection(url, target_id) {
  document.getElementById(target_id).innerHTML = "<img src=\"/images/wikiwords/loading.gif\"> Loading...";
  $("#" + target_id).load(url);
  return true;
}

function onBlurHandler( fid, evt )
{
  if(!evt && window.event)
  {
    evt=window.event;
  }
  document.getElementById(fid).style.display = "none";
}

function updateCollectionInputs(input) {
  if (input == "collection_id") {
    if (document.getElementById("collection_id").value > 0) {
      document.getElementById("field_row").style.display = "none";
    }
    if (document.getElementById("collection_id").value == "") {
      document.getElementById("field_row").style.display = "";
    }
  }
}

function updateLang() {
  if (document.getElementById("monolingual").checked == true) {
    document.getElementById("target_lang_row").style.display = "none"
  }
  else {
    document.getElementById("target_lang_row").style.display = ""
  }
}
function newTermsBatch() {
  url = "/ajax/ajax_wikiwords.php?action=new_batch&lang1='.$fields['lang_from_kdz'].'&lang2='.$fields['lang_to_kdz'].'&field_id='.$fields['field_id_kdz'].'&entity_id='.$eid_s.'";
  $("#term_rows").load(url);
}
function addTerm(i) {
  url = "/ajax/ajax_wikiwords.php?action=add_kudoz_term&lang1='.$fields['lang_from_kdz'].'&lang2='.$fields['lang_to_kdz'].'&term1="+document.getElementById("term_"+i+"").value+"&term2="+document.getElementById("term_target_"+i+"").value+"&imported_from=kudoz&imported_id="+document.getElementById("helpterm_id_"+i+"").value+"&entity_id='.$eid_s.'&field_id='.$fields['field_id_kdz'].'&collection_id='.$fields['collection_id'].'";
  $("#action_div_" + i).load(url);
}
function rejectTerm(i) {
  url = "/ajax/ajax_wikiwords.php?action=reject_kudoz_term&entity_id='.$eid_s.'&helpterm_id="+document.getElementById("helpterm_id_"+i+"").value;
  $("#action_div_" + i).load(url);
}
function updateCheckbox(id) {
  if (document.getElementById("include_"+id).checked == true) {
    document.getElementById("row_"+id).className = "grey_td";
    document.getElementById("include_"+id).checked = false;
  }
  else {
    document.getElementById("row_"+id).className = "";
    document.getElementById("include_"+id).checked = true;
  }
}
function toggleDisabledInput(id) {
  document.getElementById(id).disabled = (document.getElementById(id).disabled == true) ? false : true;
  var color = (document.getElementById(id).disabled == true) ? "#DFDFDF" : "#ffffff";
  document.getElementById(id).style.setProperty("background-color", color, null);
}

function hoverDiv(head,ts){
s = ["white","#000099","","","",,"black","#e8e8ff","","","",,,,2,"#000099",2,,,,,"",3,,,]  
s = ["#F1F5F5","#4C6384","","center","arial",1,"black","#F1F5F5","","","",,,,1,"4C6384",2,,,,,"",1,,,]
if(MI_sNav&&isOK){	
if(document.onmousemove!=mig_mo||w.onresize!=mig_re) mig_hand()
if(fl&&s[17]>-1&&s[18]>0)mig_layCss().visibility="hidden"
var ab="";var ap=""	
var titCol=s[0]?"COLOR='"+s[0]+"'":""
var titBgCol=s[1]&&!s[2]?"BGCOLOR='"+s[1]+"'":""
var titBgImg=s[2]?"BACKGROUND='"+s[2]+"'":""
var titTxtAli=s[3]?"ALIGN='"+s[3]+"'":""
var txtCol=s[6]?"COLOR='"+s[6]+"'":""
var txtBgCol=s[7]&&!s[8]?"BGCOLOR='"+s[7]+"'":""
var txtBgImg=s[8]?"BACKGROUND='"+s[8]+"'":""
var txtTxtAli=s[9]?"ALIGN='"+s[9]+"'":""
var tipHeight=s[13]? "HEIGHT='"+s[13]+"'":""
var brdCol=s[15]? "BGCOLOR='"+s[15]+"'":""
if(!s[4])s[4]="Verdana,Arial,Helvetica" 
if(!s[5])s[5]=1 
if(!s[10])s[10]="Verdana,Arial,Helvetica" 
if(!s[11])s[11]=1
if(!s[12])s[12]=200
if(!s[14])s[14]=0
if(!s[16])s[16]=0
if(!s[24])s[24]=10
if(!s[25])s[25]=10
hs=s[22]
if(MI_pSub==20001108){
if(s[14])ab="STYLE='border:"+s[14]+"px solid"+" "+s[15]+"'";
ap="STYLE='padding:"+s[16]+"px "+s[16]+"px "+s[16]+"px "+s[16]+"px'"}
var closeLink = ""; // "<TD ALIGN='right' bgcolor='white' height='20' width='20'><FONT SIZE='"+s[5]+"' FACE='"+s[4]+"'><A HREF='javascript:void(0)' ONCLICK='mig_hide(0)' STYLE='text-decoration:none;color:red' align='center'><B><div align='center'>X</div></B></A></FONT></TD>"
var title= 'well'||hs==3?"<TABLE WIDTH='100%' BORDER='0' CELLPADDING='0' CELLSPACING='0' "+titBgCol+" "+titBgImg+" ><TR><TD nowrap "+titTxtAli+"><FONT SIZE='"+s[5]+"' FACE='"+s[4]+"' "+titCol+"><B>"+head+"</B></FONT></TD>"+closeLink+"</TR></TABLE>":"";
var txt="<TABLE "+ab+" WIDTH='"+s[12]+"' BORDER='0' CELLSPACING='0' CELLPADDING='"+s[14]+"' "+brdCol+"><TR><TD>"+title+"<TABLE WIDTH='100%' "+tipHeight+" BORDER='0' CELLPADDING='"+s[16]+"' CELLSPACING='0' "+txtBgCol+" "+txtBgImg+"><TR><TD "+txtTxtAli+" "+ap+" VALIGN='top'><FONT SIZE='"+s[11]+"' FACE='"+s[10]+"' "+txtCol +">" + ts + "</FONT></TD></TR></TABLE></TD></TR></TABLE>"
mig_wlay(txt)
tb={trans:s[17],dur:s[18],opac:s[19],st:s[20],sc:s[21],pos:s[23],xpos:s[24],ypos:s[25]}
if(MI_IE4)mig_layCss().width=s[12]
e_d=mig_ed()
Count=0
move=1
}}

function hideHoverDiv(){
  if(MI_sNav&&isOK){
    if(hs!=4){
      move=0;
      if(hs!=3&&hs!=2){
        mig_hide(1)
      }
    }
  }
}
function updateInput(input, display_div, lang) {
  var url = "/ajax/ajax_wikiwords.php?action=get_terms&term_start="+encodeURI(document.getElementById(input).value)+"&input_id="+input+"&div_id="+display_div+"&lang="+lang;
  if (document.getElementById(input).value == "") document.getElementById(display_div).style.display="none";
  else document.getElementById(display_div).style.display="block";
  $("#" + display_div).load(url);
}

function itemAction(type, id, concept_id, lang) {
  // If it's not a delete or edit
  var pa = document.getElementById("pa").value;
  if (pa == "approve" || pa == "disapprove" || pa == "flag") {
    // Construct the URL
    base_url = "/ajax/ajax_wikiwords.php?action=do_item_action";
    params =  "&concept_id="+concept_id;
    params += "&id="+id;
    params += "&lang="+lang;
    
    if(pa == "approve" || pa == "disapprove") {
      replace_div = type+"_"+id;
    }
    else {
      if (type == "concept_term") type = "term";
      replace_div = "flag_concept_popup_" + type + "_" + id;
      params += "&type="+type;
      params += "&action=flag_concept_form";
      var url = base_url + params;
      showAjaxPopup(replace_div, url, "Flag item for review");
      if (type == "term") type = "concept_term";
      hideAjaxPopup("action_"+type+"_"+id);
      return false;
    }
    
    params += "&type="+type;
    params += "&perform_action="+pa;
    var url = base_url + params;
    hideAjaxPopup("action_"+type+"_"+id);
    processAjaxSubmit(url, replace_div, "", "action_item_form", "");
    return false;
  }
  else if (pa == "del") {
    if (type == "concept_term") type = "term";
    document.getElementById("del").value = type;
    return true;
  }
  else if (pa == "edit") {
    if (type == "concept_term") type = "term";
    document.getElementById("pg").value = "edit_concept";
    return true;
  }
  else return true;
}
