var trans_close = "Close";
var focus = "";
var trans_send_to_a_friend = "Email";
var err_msg_email = "Invalid email syntax";
var realhost="";
var host="";

function getLeft(l)
{
  if (l.offsetParent) return (l.offsetLeft + getLeft(l.offsetParent));
  else return (l.offsetLeft);
}
function getTop(l)
{
  if (l.offsetParent) return (l.offsetTop + getTop(l.offsetParent));
  else return (l.offsetTop);
}
function show(id) 
{
	if (document.getElementById(id)) {
    //new Effect.SlideDown(document.getElementById(id));
    document.getElementById(id).style.display = 'block';
  }
}
function hide(id) 
{
	if (document.getElementById(id)) {
    //new Effect.Fade(document.getElementById(id));
    document.getElementById(id).style.display = 'none';
  }
}

function valide_email(f) {
  expr=/^[a-z0-9,!#\$%&\'\*\+\/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&\'\*\+\/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$/;
  var res=expr.test(f.value);
  if (!res) {
    alert(err_msg_email);
    f.focus();
    return false;
  } else {
    return true;
  }
}



function minwidth()
{
	if (document.documentElement.clientWidth) 
	{
		var screenWidth = document.documentElement.clientWidth;
		document.body.getElementsByTagName('div')[0].style.width = (screenWidth > 780) ? 100+'%' : 780+'px';
	} 
}
function resize()
{
  var map = document.getElementById('map');
  if (map) {
    if (typeof(window.innerHeight)=='number') {
      var height = window.innerHeight - getTop(map) - 3;
    } else {
      var height = document.documentElement.clientHeight - getTop(map) - 3;
    }
  	map.style.height = (height > 820) ? height +'px' : 820 +'px';
	}
}
function focusOnFormWhatWhere(id, criter)
{
    if (criter =='what' && document.getElementById(id).what)
    {
      document.getElementById(id).what.focus();
      focus = 'what';
    }
    else if (criter =='where' && document.getElementById(id).where)
    {
      document.getElementById(id).where.focus();
      focus = 'where';
    }
}
function blurSearchForm()
{
    var what = document.getElementById("searchForm").what;
    if (what)
      what.blur();

    var where = document.getElementById("searchForm").where;
    if (where)
      where.blur();
}

function change_zm(field, oldvalue, req) {
  var req = window.location.search.substr(1,window.location.search.length);
  var dbl = req.split('&');
  var newloc = "";
  var done=0;
  for (i=0;i < dbl.length;i++) {
    var p = dbl[i].substring(0,dbl[i].indexOf('='));
    if (newloc.length>0) {
      newloc=newloc+"&";
    }
    if (p!='zm') {
      var v=dbl[i].substring(dbl[i].indexOf('=')+1,dbl[i].length);
      newloc=newloc+p+"="+v;
    }
    else {
      done=1;
      newloc=newloc+'zm='+field.value;
    }
  }
  if (!done) {
    if (newloc.length>0) { newloc=newloc+"&"; }
    newloc=newloc+'zm='+field.value;
  }
  // reposte requete avec rayon different
  var hr=window.location.href.indexOf('?');
  var nl;
  if (hr>0) {
    nl=window.location.href.substr(0, hr)+"?"+newloc;
  }
  else {
    nl=window.location.href+"&"+newloc;
  }
  // oldvalue=valeur correcte pour retour eventuel sur la page via back
  field.value=oldvalue;
  document.location.href=nl;
  return false;
}
function filtercat(catref) {
  var reg=new RegExp("smap=","gi");
  if (!base_url.match(reg))
    base_url = base_url + "&smap=";

  document.location.href=base_url+'&ct[]='+catref;
}
function unfiltercat(z) {
  var reg=new RegExp("-_z=([0-9]+)","gi");
  if (z.match(reg)) {
    var ordercat = z.replace(reg, "$1");

    var reg2=new RegExp("&ct([^=]+)=([^&]+)","gi");
    if (base_url.match(reg2)) {
      var todelete = base_url.match(reg2)[ordercat];
      base_url = base_url.replace(todelete, "");
      document.location.href=base_url;
    }

  }
}
function changefiltercat(catref, z) {
  var reg=new RegExp("-_z=([0-9]+)","gi");
  if (z.match(reg)) {
    var ordercat = z.replace(reg, "$1");

    var reg2=new RegExp("&ct([^=]+)=([^&]+)","gi");
    if (base_url.match(reg2)) {
      var todelete = base_url.match(reg2)[ordercat];
      base_url = base_url.replace(todelete, "");
    }
  }
  document.location.href=base_url+'&ct[]='+catref;
}
function valide_emails_fav() {
  expr=/^[a-z0-9,!#\$%&\'\*\+\/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&\'\*\+\/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$/;
  var res1=expr.test(document.getElementById('fav-form').email_dst.value);
  var res2=expr.test(document.getElementById('fav-form').email_src.value);
  if (!res1 || !res2) {
    alert(err_msg_email);
    return false;
  } else {
    return true;
  }
}
function openWindow(href, width, height) 
{
  if(!width) {
    width = window.screen.width * 0.8;
    mleft = window.screen.width * 0.05;
  }
  if(!height) {
    height = window.screen.height * 0.8;
    mtop = window.screen.height * 0.05;
  }
  window.open(href, "", "width="+width+",height="+height+",left="+mleft+",top="+mtop+",directories,location,menubar,personalbar,scrollbars,status,toolbar,resizable,fullscreen=no");
}
function openPopup(type, id)
{
  var popup = document.getElementById('popup');
  var minimap = document.getElementById('map');
  var popup_form = document.getElementById('sendByEmail');
  var link = document.getElementById(id);
  /*
  if (minimap && minimap.style.display != 'none')
  {
    hide('map');
  }
  */
  if (popup.style.display != 'none' && 'send_'+popup_form.id.value == id)
  {
    closePopup(type, id);
  }
  else
  {
    var x = getLeft(link);
    var y = getTop(link) + 15;
    popup.style.marginLeft = x + 'px';
    popup.style.marginTop = y + 'px';
  	popup_form.id.value = id.substr(5); // on enleve le prefix send_ à l'id, soit 5 caractères
    hide('popupReponse');
    show('sendByEmail');
    show('popup');
    link.innerHTML = trans_close;
    
    document.getElementById('sendByEmail').email_dst.focus();
    focus = 'email';
	}
}
function closePopup(type, id)
{
  var link = document.getElementById(id);
  link.innerHTML = trans_send_to_a_friend;
  var popup = document.getElementById('popup');
	if (popup) hide('popup');
}
function openMiniMap(localisation, univers, id) {
  var minimap = document.getElementById('map');
  var popup = document.getElementById('popup');
  var link = document.getElementById(id);
  
  if (popup.style.display != 'none')
  {
    closePopup();
  }
  if (minimap.style.display != 'none')
  {
    hide('map');
    link.innerHTML = trans_locate_on_the_map;
  }
  else
  {
    var x = getLeft(link);
    var y = getTop(link) + 15;
    minimap.style.position = 'absolute';
    minimap.style.left = x + 'px';
    minimap.style.top = y + 'px';
    minimap.style.display = 'block';
    minimap.innerHTML = '<iframe src="'+ host +'/minimap.php?univers='+ univers +'&where='+ localisation +'" frameborder="0" style="width:200px"></iframe>';
    link.innerHTML = trans_close;
  }

}
function sendByMail(form_id) {

		var xhr_object = null;

		if (window.XMLHttpRequest) // Firefox
		   xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) // Internet Explorer
		   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else {
		   xhr_object = null;
		   return;
		}

		var email_src = document.getElementById('sendByEmail').email_src.value;
		var email_dst = document.getElementById('sendByEmail').email_dst.value;
		var message = document.getElementById('sendByEmail').message.value;
		var id = document.getElementById('sendByEmail').id.value;
		var data = "email_src="+ email_src +"&email_dst="+ email_dst +"&message="+ message +"&id="+ id;

		xhr_object.open("POST", realhost + "/send-email.ajax.php", true);

    form_id.style.display = 'none';
    show('popupReponse');
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 1) document.getElementById('popupReponse').innerHTML =  "<img src="+ host +"'/images/loading2.gif' alt='*' />";
			if(xhr_object.readyState == 4) {
        document.getElementById('popupReponse').innerHTML =  xhr_object.responseText;
        setTimeout("closePopup('sendByEmail', 'send_"+id+"')", 5000);
      }
		}

		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send(data);
}
function saveAlert(email,frequence,url) {

		var xhr_object = null;

		if (window.XMLHttpRequest) // Firefox
		   xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) // Internet Explorer
		   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else {
		   xhr_object = null;
		   return;
		}

		var data = "email=" + email + "&frequence=" + frequence + "&url=" + url;

		xhr_object.open("POST", realhost + "/ajax/save-alert.ajax.php", true);

    divBox = document.getElementById('message-box').getElementsByTagName('div')[0];
		divTmp = document.createElement("div");
    divTmp.setAttribute('id','divTmp');
    divTmp.style.display = 'none';
    divTmp.innerHTML = divBox.innerHTML;
    document.body.appendChild(divTmp);
    
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 1) divBox.innerHTML =  "<img src='"+ host +"/images/loading2.gif' alt='*' />";
			if(xhr_object.readyState == 4) {
        divBox.innerHTML =  xhr_object.responseText;
      }
		}

		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send(data);
}
function prevResult() {

  blurSearchForm();
  
  if (!navResult) return;
  
  if (current_result == -1)
  {
    current_result = arrayId.length - 1;
  }
  else if (!arrayId[current_result - 1])
  {
    if (!prev_pg)
    {
      //document.getElementById("item_" + arrayId[current_result]).className = "item";
      //current_result = -1;
      return;
    }
    else
    {
      var reg=new RegExp("pg=([^&]+)","gi");
      if (base_url.match(reg))
      {
        var new_url = base_url.replace(reg, "pg=" + prev_pg);
      }
      else
      {
        var new_url = base_url + "&pg=" + prev_pg;
      }
      document.location.href = new_url;
      navResult = false;
      return;
    }
  }
  else
  {
    var old_item = document.getElementById("item_" + arrayId[current_result]);
    current_result--;
  }
  
  var current_item = document.getElementById("item_" + arrayId[current_result]);
  var screenHeight = document.documentElement.clientHeight;
  var scrollTop = document.documentElement.scrollTop;
  var maxTop = screenHeight - scrollTop;
  var itemTop = getTop(current_item) + current_item.offsetHeight;
  if (itemTop < screenHeight)
  {
    document.documentElement.scrollTop = 0;
  }
  else if ( (itemTop > maxTop && current_result == arrayId.length - 1) || (itemTop > maxTop && itemTop - scrollTop < current_item.offsetHeight) )
  {
    document.documentElement.scrollTop = itemTop - Math.round(screenHeight / 2);
  }
  
  if (arrayId[current_result])
  {
    if (old_item) old_item.className = "item";
    current_item.className = "item selected";
  }
}
function nextResult() {
  
  blurSearchForm();
  
  if (!navResult) return;
  
  if (current_result == -1)
  {
    current_result = 0;
  }
  else if (!arrayId[current_result + 1])
  {
    if (!next_pg)
    {
      return;
    }
    else
    {
      var reg=new RegExp("pg=([^&]+)","gi");
      if (base_url.match(reg))
      {
        var new_url = base_url.replace(reg, "pg=" + next_pg);
      }
      else
      {
        var new_url = base_url + "&pg=" + next_pg;
      }
      document.location.href = new_url;
      navResult = false;
      return;
    }
  }
  else
  {
    var old_item = document.getElementById("item_" + arrayId[current_result]);
    current_result++;
  }

  var current_item = document.getElementById("item_" + arrayId[current_result]);
  var screenHeight = document.documentElement.clientHeight;
  var scrollTop = document.documentElement.scrollTop;
  var maxTop = screenHeight - scrollTop;
  var itemTop = getTop(current_item) + current_item.offsetHeight;
  if (itemTop < screenHeight)
  {
    document.documentElement.scrollTop = 0;
  }
  else if (itemTop > maxTop && itemTop - scrollTop > screenHeight)
  {
    document.documentElement.scrollTop = itemTop - Math.round(screenHeight / 2);
  }

  if (arrayId[current_result])
  {
    if (old_item) old_item.className = "item";
    current_item.className = "item selected";
  }
}
function selectResult(id) {
  for (i=0; i<arrayId.length; i++) {
    if ('item_'+arrayId[i] == id)
    {
      current_result = i;
      document.getElementById("item_" + arrayId[current_result]).className = "item selected";
    }
    else
    {
      document.getElementById("item_" + arrayId[i]).className = "item";
    }
  }
}
function unselectResult() {
  current_result = -1;
  for (i=0; i<arrayId.length; i++) {
    document.getElementById("item_" + arrayId[i]).className = "item";
  }
}
function enterResult() {
  if (current_result != -1)
  {
      document.location.href = "/redirect.php?url=" + arrayURL[current_result];
  }
}
function resetZoom() {
  if (document.getElementById('inputTopZoom')) {
    var html = '';
    html += '<option value="0">'+ trans_exact +'</option>';
    html += '<option value="5">+ 5 '+ trans_mi +'</option>';
    html += '<option value="10">+ 10 '+ trans_mi +'</option>';
    html += '<option value="20">+ 20 '+ trans_mi +'</option>';
    html += '<option value="50">+ 50 '+ trans_mi +'</option>';
    html += '<option value="1000">'+ trans_county +'</option>';
    html += '<option value="10000">'+ trans_allcountry +'</option>';
    document.getElementById('inputTopZoom').innerHTML = html;
  }
}
