/*
The following two functions (slowMove & displayInfo) have been adapted from
http://isohunt.com Interface Javascript
by Gary Fung - email: gary{REPLACE_WITH_THE_AT_SIGN}isohunt.com

Feel free to use / mod this to your heart's content,
but you must keep these lines to acknowledge where this code originated.
Comments, mods or additions you'd like add to this script? Post it here:
http://isohunt.com/forum/viewforum.php?f=1

Tip popup functions adapted from AWStats: http://awstats.sourceforge.net/
*/
var delay;
var current_option;
// Delays the display of the iframe showing the kudoz sub page and the ProZ.com message
function slowMove(id, curH, targetH, stepH, mode) {
  diff = targetH - curH;
  if (diff != 0) {
    newH = (diff > 0) ? curH + stepH : curH - stepH;
    document.getElementById(id).style.height = newH + "px";
    if (delay) window.clearTimeout(delay);
    delay = window.setTimeout( "slowMove('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 20 );
  }
  else if (mode != "o") document.getElementById(mode).style.display="none";
}
// Change visibility of the iframe that shows the kudoz sub page
// and the ProZ.com message sub page - add compact profile and calendar pending (Nahun)
function displayInfo(entity_id, url, bg_color, option, redirect) {
  var row_to_show = (document.getElementById) ? document.getElementById('row' + entity_id) : eval("document.all['row" + entity_id + "']");
  var div_to_show = (document.getElementById) ? document.getElementById('div' + entity_id) : eval("document.all['div" + entity_id + "']");
  if (option == 'kudoz') {
    document.getElementById('mail' + entity_id).style.display="none";
    document.getElementById('calendar' + entity_id).style.display="none";
    var frame_to_show = (document.getElementById) ? document.getElementById('frame' + entity_id) : eval("document.all['frame" + entity_id + "']");
    var kudoz = (document.getElementById) ? document.getElementById('kudoz' + entity_id) : eval("document.all['kudoz" + entity_id + "']");
    kudoz.style.display="";
  }
  if (option == 'mail') {
    document.getElementById('kudoz' + entity_id).style.display="none";
    document.getElementById('calendar' + entity_id).style.display="none";
    var frame_to_show = (document.getElementById) ? document.getElementById('frame' + entity_id + '_mail') : eval("document.all['frame" + entity_id + "_mail']");
    var mail = (document.getElementById) ? document.getElementById('mail' + entity_id) : eval("document.all['mail" + entity_id + "']");
    mail.style.display="";
  }
  if (option == 'calendar') {
    document.getElementById('kudoz' + entity_id).style.display="none";
    document.getElementById('mail' + entity_id).style.display="none";
    var frame_to_show = (document.getElementById) ? document.getElementById('frame' + entity_id + '_calendar') : eval("document.all['frame" + entity_id + "_calendar']");
    var mail = (document.getElementById) ? document.getElementById('calendar' + entity_id) : eval("document.all['calendar" + entity_id + "']");
    mail.style.display="";
  }
  if (row_to_show != null) {
    if (row_to_show.style.display=="none") {
      if (!frame_to_show.src || frame_to_show.src != 'http://'+document.domain+url || frame_to_show.src != '') {
        row_to_show.style.display="";
        frame_src = 'http://'+document.domain+url
        if(frame_to_show.src != frame_src) {
          frame_to_show.src = frame_src;
        }
        if (option == 'kudoz')
          slowMove('frame' + entity_id, 0, 210, 42, 'o');
        if (option == 'mail')
          slowMove('frame' + entity_id + '_mail', 0, 210, 42, 'o');
        if (option == 'calendar')
          slowMove('frame' + entity_id + '_calendar', 0, 210, 42, 'o');
      }
    } else {
      // Close the iframe
      if (option == 'kudoz')
        slowMove('frame' + entity_id, 210, 0, 42, 'row' + entity_id);
      if (option == 'mail')
        slowMove('frame' + entity_id + '_mail', 210, 0, 42, 'row' + entity_id);
      if (option == 'calendar')
        slowMove('frame' + entity_id + '_calendar', 210, 0, 42, 'row' + entity_id);
      // if the iframe is open and select another option, close it and open it again showing the option selected
      if (current_option != option) {
        row_to_show.style.display="";
        frame_src = 'http://'+document.domain+url
        if(frame_to_show.src != frame_src)
          frame_to_show.src = frame_src;
        if (option == 'kudoz')
          slowMove('frame' + entity_id, 0, 210, 42, 'o');
        if (option == 'mail')
          slowMove('frame' + entity_id + '_mail', 0, 210, 42, 'o');
        if (option == 'calendar')
          slowMove('frame' + entity_id + '_calendar', 0, 210, 42, 'o');
      }
    }
  }
  current_option = option;
}
// Ajax
//var url_country = "inc/inc_get_geodata.php?country="; // The server-side script
// Handles the response generated by updateCity function
// and updates the city select list
function handleHttpResponseCity() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText.split(",");
    str = '';
    var country_list = document.getElementById("region_list");
    var city_list = document.getElementById("city_list");
    // Remove current items
    if (city_list) {
      var child = null;
      while (child = city_list.lastChild) {
        city_list.removeChild(child);
      }
      // The any item
      var anyObj = document.createElement("option");
      anyObj.setAttribute("value", "na");
      anyObj.appendChild(document.createTextNode("Any"));
      city_list.appendChild(anyObj);
      for (i in results) {
        var city = results[i];
        var temp = new Array();
        temp = city.split('_');
        if (temp[0]!= '') {
          // Create the item for the select field
          if (document.getElementById) {
            if (country_list) {
              // City item
              var cityObj = document.createElement("option");
              cityObj.setAttribute("value", temp[1]);
              cityObj.appendChild(document.createTextNode(temp[0]));
              // add the city
              city_list.appendChild(cityObj);
            }
          }
        }
      }
    }
  }
}
// Handles the response generated by updateRegion function
// and updates the region select list
function handleHttpResponseRegion() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText.split(",");
    str = '';
    var country_list = document.getElementById("country");
    var region_list = document.getElementById("region_list");
    var city_list = document.getElementById("city_list");
    // Clean the cities list too
    if (city_list) {
      var city_child = null;
      while (city_child = city_list.lastChild) {
        city_list.removeChild(city_child);
      }
      var anyObj = document.createElement("option");
      anyObj.setAttribute("value", "na");
      anyObj.appendChild(document.createTextNode("Any"));
      city_list.appendChild(anyObj);
    }
    // Remove current items
    if (region_list) {
      var child = null;
      while (child = region_list.lastChild) {
        region_list.removeChild(child);
      }
      // The any item
      var anyObj = document.createElement("option");
      anyObj.setAttribute("value", "na");
      anyObj.appendChild(document.createTextNode("Any"));
      region_list.appendChild(anyObj);
      for (i in results) {
        var city = results[i];
        var temp = new Array();
        temp = city.split('_');
        if (temp[0]!= '') {
          // Create the item for the select field
          if (document.getElementById) {
            if (country_list) {
              // City item
              var cityObj = document.createElement("option");
              cityObj.setAttribute("value", temp[1]);
              cityObj.appendChild(document.createTextNode(temp[0]));
              // add the city
              region_list.appendChild(cityObj);
            }
          }
        }
      }
    }
  }
}
// Reads the list of regions corresponding to the country and
// updates the region select list the handleHttpResponseRegion function
function updateRegion(param, list_to_change) {
  var url_country = 'http://'+document.domain+'/ajax/ajax_get_geodata.php?param='+param+'&id=';
  var country = document.getElementById("country").selectedIndex;
  var iso_code = document.getElementById("country").options[country].value;
  http.open("GET", url_country + escape(iso_code), true);
  http.onreadystatechange = handleHttpResponseRegion;
  http.send(null);
  return;
}
// Reads the list of cities corresponding to the region and
// updates the city select list the handleHttpResponseCity function
function updateCity(param, list_to_change) {
  var url_country = 'http://'+document.domain+'/ajax/ajax_get_geodata.php?param='+param+'&id=';
  var country = document.getElementById("region_list").selectedIndex;
  var iso_code = document.getElementById("region_list").options[country].value;
  http.open("GET", url_country + escape(iso_code), true);
  http.onreadystatechange = handleHttpResponseCity;
  http.send(null);
  return;
}
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false;
    }
  } return xmlhttp;
} 
var http = getHTTPObject(); // create the HTTP Object
// Changes visibility of the major and minor language pairs on the right column
// of the directory in the first step
function showPairsList (object_id, version) {
  var affected_object = (document.getElementById) ? document.getElementById(object_id) : eval("document.all[object_id]");
  var affected_image  = (document.getElementById) ? document.getElementById(object_id+'_img') : eval("document.all[object_id+'_img']");
  if (affected_object.style.display=="none") {
    affected_object.style.display = "block";
    if (version == 5) affected_image.src = "/images/pftv6/icon_contract.gif";
    else affected_image.src = "/images/icon_contract_small.gif";
  } else {
    affected_object.style.display = "none";
    if (version == 5) affected_image.src = "/images/pftv6/icon_expand.gif";
    else affected_image.src = "/images/icon_expand_small.gif";
  }
}
// Changes visibility of the fields of any major pair fields on mouse event
// of the directory in the first step
function showFieldsList (object_id, action) {
  var affected_object = (document.getElementById) ? document.getElementById(object_id) : eval("document.all[object_id]");
  var td_affected = (document.getElementById) ? document.getElementById(object_id+'_td') : eval("document.all[object_id+'_td']");
  //var affected_image  = (document.getElementById) ? document.getElementById(object_id+'_img') : eval("document.all[object_id+'_img']");
  if (affected_object.style.display=="none" && action == 'show') {
    affected_object.style.display = "block";
    td_affected.style.fontWeight = "bold";
    //affected_image.src = "/images/icon_contract_small.gif";
  } 
  if (affected_object.style.display=="block" && action == 'hide') {
    affected_object.style.display = "none";
    td_affected.style.fontWeight = "normal";
    //affected_image.src = "/images/icon_expand_small.gif";
  }
}
// Gets the list of entities to be added to favorites
/*function getFavoritesList(name) {
  if (document.getElementById) {
    var listcount = document.getElementById(name + "_count");
    if (listcount) {
      var count = parseInt(listcount.value);
      var entities_string = '';
      var arrayTotal = 0;
      for (var i = 0; i < count; i++) {
        var listobj = document.getElementById(name + "[" + i + "]");
        if (listobj && listobj.checked) {
          entities_string += listobj.value + '_';
          arrayTotal++;
        }
      }
      if (arrayTotal > 0) {
        updateFavorites(entities_string);
      } else alert('You have to select one or more professional of the list');
    }
  }
}*/
// Add an entity to the list of candidates of the
// eid_v calling an ajax function
function updateFavorites(entity_id, selector, box_id, max_candidates) {
  var box_state = document.getElementById(box_id).checked;
  var candidates = document.getElementById("total_candidates");
  var total = parseInt(candidates.innerHTML);
  var limit = parseInt(max_candidates);
  if (total >= limit && box_state == true) {
    // perfom action to stablish that the max number of entries has benn reached
    alert ("Max candidates reached");
    return;
  }
  var url_favorites = 'http://'+document.domain+'/ajax/ajax_directory_favorites.php?entity_id='+entity_id+'&selector='+selector+'&state='+box_state;
  http.open("GET", url_favorites, true);
  http.onreadystatechange = handleHttpResponseFavorites;
  http.send(null);
  return;
}
function updateCandidatesAll(selector_id, candidates_string, max_candidates, shown_in_page) {
  var box_state = document.getElementById("check_all").checked;
  var candidates = document.getElementById("total_candidates");
  var total = parseInt(candidates.innerHTML);
  var to_ad = parseInt(shown_in_page);
  var limit = parseInt(max_candidates);
  if ((total + to_ad) > limit && box_state == true) {
    // perfom action to stablish that the max number of entries has benn reached
    alert ("Max candidates reached");
    return;
  }
  var url_favorites = 'http://'+document.domain+'/ajax/ajax_directory_favorites.php?candidates_string='+candidates_string+'&selector='+selector_id+'&state='+box_state+'&all=true';
  http.open("GET", url_favorites, true);
  http.onreadystatechange = handleHttpResponseFavorites;
  http.send(null);
  return;
}
// Handles the response generated by updateFavorites function
// and updates the favorites list
function handleHttpResponseFavorites() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    var result = http.responseText.split(",");
    var folder_links_top = document.getElementById("folder_links_top");
    var folder_links_bot = document.getElementById("folder_links_bot");
    if (result[0] == 'true') {
      var candidates = document.getElementById("total_candidates");
      var candidates_bottom = document.getElementById("total_candidates_bot");
      candidates.innerHTML = result[1];
      candidates_bottom.innerHTML = result[1];
      if (parseInt(result[1]) > 0) {
        folder_links_top.style.display = "";
        folder_links_bot.style.display = "";
      }
    } 
    if (result[0] == 'deleted') {
      var candidates = document.getElementById("total_candidates");
      var candidates_bottom = document.getElementById("total_candidates_bot");
      candidates.innerHTML = result[2];
      candidates_bottom.innerHTML = result[2];
      if (result[2] == "0") {
        folder_links_top.style.display = "none";
        folder_links_bot.style.display = "none";
      }
    }  
  }
}
