///////////////  COMM AVEC LE SERVEUR  ////////////
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();
var url = "sov.php?r=";

function handleHttpResponse(){
    if (http.readyState == 4) {
       result = http.responseText;
       //alert(result);session_start();
       connecProp(result);
    }
}

function Response(){
    http.open("GET", "/common/prAjverif.php?propPass="+document.frmProprio.propPass.value+"&agnum="+document.frmProprio.agnum.value , true); //url + escape(text)
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}

//---- Ouverture l'acces proprio
function connecProp(l){
   if (l.indexOf(":") == -1){
      if (parseInt(l) > 1){
    	  open("","FenProp");
    	  document.frmProprio.target = "FenProp";
    	  document.getElementById('agnumPr').value = l;
    	  document.frmProprio.submit();
      }else{
          alert("Mot de passe incorrect.\nVeuillez recommencer.\n");
      }
      return;
   }else{
      var t = l.split(":");
      if (t[0] == "1"){
    	  open("","FenProp");
    	  document.frmProprio.target = "FenProp";
    	  document.frmProprio.proprio = t[1];
    	  document.frmProprio.submit();
      }else{
          alert("Mot de passe incorrect.\nVeuillez recommencer.\n"); //\n"+l
      }
      return;
   }
    
}
////////////////////////////////////////////////////

var memPhoto = new Image;
var chImg = "http://srvlinux2.technolog.fr/vitrinesV2Lang/images/";
//var chImg = "http://localhost/vitrinesV2Lang/images/";

//---- Petite vers grosse photo dans la liste des biens
function changePhoto(im,i){

     var lnk = im.parentNode.href;
     lnk = lnk.replace(/\/60\//,"/400/");
	 lnk = lnk.replace(/\/148\//,"/800/");
     document.getElementById('gPhoto'+i).parentNode.href = lnk;

     var ph = im.src;
     ph = ph.replace(/\/60\//,"/400/");
	 ph = ph.replace(/\/148\//,"/800/");
     //alert(ph);
     document.getElementById('gPhoto'+i).src = ph;
}

//---- Pop up pour une image en grand
function OuvrePopupImage(img) { 
	titre="Agrandissement"; 
     var ph = img;
     ph = ph.replace(/\/400\//,"/800/");
     //alert(ph);
     img = ph;
      	w=open("",'image','width=400,height=400,toolbar=no,scrollbars=no,resizable=no'); 
	w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>");
	w.document.write("<SCRIPT language=javascript>function checksize() { if (document.images[0].complete) { window.resizeTo(document.images[0].width+10,document.images[0].height+30); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>");
	w.document.write("<BODY onload='checksize()' onblur='window.close()' onclick='window.close()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
	w.document.write("<TABLE width='100%' border='0' cellspacing='0' cellpadding='0' height='100%'><TR>");
	w.document.write("<TD valign='middle' align='center'><IMG src='"+img+"' border=0>");
	w.document.write("</TD></TR></TABLE>");
	w.document.write("</BODY></HTML>");
	w.document.close();
}

//---- Derouler / enrouler un detail de bien
function showDetail(ind,col,bicodag){
	var d = document.getElementById('dt'+ind);
	var im = document.getElementById('imgDetail'+ind);
	var bandVert = document.getElementById('bandVertGauche'+ind);
	if (d.style.display == "block"){
		d.style.display = 'none';
		bandVert.style.backgroundColor = "#ffffff";
		im.src = chImg+"bt_ouvrir.jpg";
		im.flagStat = 0;
	}else{
		d.style.display = 'block';
		bandVert.style.backgroundColor = col;
		im.src = chImg+"bt_fermer.jpg";
		im.flagStat = 1;
	}
	// Stats
	if (im.flagStat == 1){
	    stats(bicodag);
	}
}

//---- Je sais plus
function testShowMini(ind){

	var d = document.getElementById('lp'+ind);
	if (d.style.display == "block"){
		d.style.display = 'none';
	}else{
		d.style.display = 'block';
	}
	
}

//---- Changer de page dans les listes de biens
function changePage(i){
	document.getElementById('numpage').value = i;
	document.frmPages.submit();
}

//---- Changer le filtre de tri
function changeFiltre(i){
	document.frmTri.Tri.value = i;
	document.frmTri.submit();
}

function changeFiltreB(i){
	document.getElementById('Tri').value = i;
	document.frmTri.submit();
}

//---- Pop up imprimer
function popImprimer(bicodag,ag){
     open("imprime.php?bicodag="+bicodag,"Imprimer") //&agnum="+ag
}

//---- Pop up Envoi Ami
function popEnvoiAmi(bicodag){
     open("envoi.php?bicodag="+bicodag,"EnvoiAmi","width=450,height=600")
}

//---- Pop Contact Agence
function popContactAg(agence,bicodag){
     var param = "";
     param = "?agContact="+agence;
     if (bicodag != null){
         param += "&bicodag="+bicodag;
     }
     open('contactAgence.php'+param,'ContactAG','width=450,height=500');
}

//---- Pop AccÃ¨s proprio
function popProprio(){
     open("prIndex.php","Acces Proprietaire",'width=540,height=600,resizable=yes, scrollbars=yes')
}


//---- Verification des champs avant l'envoi du formulaire Envoi Ami
function verifEnvoiAmi(){

     var frm = document.frmEnvoiAmi;
     if (frm.expNom.value == ""){
     	alert("Veuillez donner un nom d'exp\351diteur.");
     	return false;
     }
     if (frm.expMail.value == ""){
     	alert("Veuillez donner un mail pour l\'exp\351diteur.");
     	return false;
     }
     if (frm.destNom.value == ""){
     	alert("Veuillez donner un nom de destinataire.");
     	return false;
     }
     if (frm.destMail.value == ""){
     	alert("Veuillez donner un mail pour le destinataire.");
     	return false;
     }
     
     frm.btnSubmit.value = "Envoi en cours ..."; 
     return true;

}

//---- Verification des champs avant l'envoi du formulaire Contact Agence
function verifContactAg(){

     var frm = document.frmContactAg;
     if (frm.prospNom.value == ""){
     	alert("Veuillez donner un nom.");
     	return false;
     }
     if (frm.prospTel.value == ""){
     	alert("Veuillez donner un num\351ro de t\351l\351phone.");
     	return false;
     }
     if (frm.prospMail.value == ""){
     	alert("Veuillez donner un mail.");
     	return false;
     }
     var rgx=new RegExp("^[a-z0-9_]([.-]?[a-z0-9_]+)+@[a-z0-9_]([.-]?[a-z0-9_]+)+\.([a-z]{2,4}|[a-z]{6})$", "gi");
     if (!rgx.exec(frm.prospMail.value)){
     	alert("L'email fourni n'a pas un format valide.");
	return false;
     }
     if (frm.prospMessage.value == ""){
     	alert("Veuillez laisser un message.");
     	return false;
     }
     
     frm.btnSubmit.value = "Envoi en cours ...";
     return true;

}

//---- Je sais plus
function affComment(bicodag,v){

    if (v > 0){
     	document.getElementById('colComment').style.display = 'block';
     	for(i=0;i<v;i++){
     		eval("document.getElementById('c"+ i + "')").style.display = 'block';
     	}
    }

}

//---- Gestion des cookies
function SetCookie(name, value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

function GetCookie(name) {
	
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i=document.cookie.indexOf(" ",i)+1;
                        if (i==0) break;
        }
	return "";
	
}

function getCookieVal(offset) {
	
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1)
      		endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
	
}

function DelCookie(nom) {
    
    dte=new Date;
    dte.setFullYear(dte.getFullYear()-1);
    SetCookie(nom,null,dte);
    
}
//--------------------------------------------

//-- Changement de langue
function chngLang(lang){
          var date_exp = new Date();
	        date_exp.setTime(date_exp.getTime()+(365*24*3600*1000));
	         
          SetCookie("lang",lang,date_exp);
          document.location.href = "index.php";
}

//---- Verif du nombre d'annonces selectionnees
function verifSelec(){

    var l = GetCookie("vitSelec");
    if (l != ""){
       return true;
    }else{
       alert("Aucun bien n'a été sélectionné.");
       return false;
    }

}

//---- Selectionner / Deselectionner un bien
function addBien(bicodag,obj,id,op){
    
    // obj = l'objet representant le lien
    // on lui accole une propriete => indice
    if (obj.indice == null){
       obj.indice = 1;
    }
    var img = obj.parentNode.parentNode.getElementsByTagName("IMG")[0];
    // Operation pour changer l'image et le titre de survol du lien
    if (obj.indice == 1){
         img.src = chImg+"retirer_selection.gif";
         img.parentNode.title = "Supprimer de ma s\351lection";
         //img.parentNode.title = "Supprimer de ma s\351lection";
         obj.title = "Supprimer de ma s\351lection";
         if (obj.firstChild.tagName != "IMG"){
            obj.innerHTML = "Supprimer de ma s\351lection";
         }
         obj.indice = 0;
    }else{
         img.src = chImg+"ajouter_selection.gif";
         img.parentNode.title = "Ajouter \340 ma s\351lection";
         obj.title = "Ajouter \340 ma s\351lection";
         if (obj.firstChild.tagName != "IMG"){
            obj.innerHTML = "Ajouter \340 ma s\351lection";
         }
         obj.indice = 1;
     }
     // on supprime le fils du DIV
     var lay = document.getElementById("divCache");     
     if (lay.firstChild) {
        lay.removeChild(lay.firstChild);
     }
     // creation de l'iframe
     var ifrm = document.createElement('IFRAME');
     ifrm.id = "UploadTarget1";
     ifrm.name = "UploadTarget1";
     ifrm.src = "";
     ifrm.style.border = "0";
     ifrm.style.width = "0px";
     ifrm.style.height = "0px";
     
     lay.appendChild(ifrm);
     // chargement de la page necessaire dans l'iframe     
     document.getElementById("UploadTarget1").src = "selection.php?bicodag="+bicodag+"&act="+obj.indice+"&op="+op;
     
     // Partie pour la suppression dans la consultation de la sÃƒÂ¯Ã‚Â¿Ã…Â“ection
     if (op == "sel"){
    	
    	document.getElementById("Bi"+id).style.position = 'absolute';
    	document.getElementById("Bi"+id).style.display = 'none';
    	document.getElementById("Bi"+id).style.top = '-9999px';
    	
    	var cpt = document.getElementById('cptAn').innerHTML;
    	var t = cpt * 1; //.split(" ");
    	
    	var n = (t[0] * 1) - 1;
    	document.getElementById('cptAn').innerHTML = n; //+ " annonces";
    	return false;
    	
     }
}

//---- Stats Detail du Bien
function stats(bicodag){
    
     // on supprime le fils du DIV
     var lay = document.getElementById("divCache");     
     if (lay.firstChild) {
        lay.removeChild(lay.firstChild);
     }
     // creation de l'iframe
     var ifrm = document.createElement('IFRAME');
     ifrm.id = "StatsDetail";
     ifrm.name = "StatsDetail";
     ifrm.src = "";
     ifrm.style.border = "0";
     ifrm.style.width = "0px";
     ifrm.style.height = "0px";
     
     lay.appendChild(ifrm);
     // chargement de la page necessaire dans l'iframe     
     document.getElementById("StatsDetail").src = "ajStats.php?bicodag="+bicodag; //"selection.php?bicodag="+bicodag+"&act="+obj.indice;
     
}

//---- Ouvre la popup des alertes depuis le bouton en haut
function ouvreAlertes(bicodag){
   open('alertes.php','fenAlertes','width=500,height=550,resizable=yes, scrollbars=yes'); //?bicodag='+bicodag
}



//---- Ouvre la popup des alertes
function ouvreAlertesBien(bicodag){
   open('alertes.php?bicodag='+bicodag,'fenAlertesBien','width=500,height=400,resizable=yes, scrollbars=yes');
}

// Remplacer dans "expr" une chaine "a" par une chaine "b"
function Remplacer(expr,a,b){
	var i=0
	while (i!=-1) {
		i=expr.indexOf(a,i);
		if (i>=0) {
			expr=expr.substring(0,i)+b+expr.substring(i+a.length);
			i+=b.length;
			}
		}
	return expr
}

//---------------------------------//
//---  Fonctions du comparateur ---//
//---------------------------------//

//---- Ajout ou Suppression d'un bien dans le comparateur
function addCompare(obj,bicodag){
   
   var c = GetCookie("jsCompare");
   var valAjoute = bicodag + "@";
   
   if (obj.checked){
	// si pas dedans => on ajoute, sinon rien
	if (c.indexOf(valAjoute) == -1){
		c += valAjoute;
	}
	// si pas cochÃƒÂ©
   }else{
	c = Remplacer(c,valAjoute,"");
   }
   
   //alert(c);  
   SetCookie("jsCompare", c);
   //alert(GetCookie("jsCompare"));
        
}

//--- Supprimer le cookie et donc la comparaison precedente
function supCompare(){
    	//--- Decocher les cases puis effacer le cookie
	fObj = document.frmCompare;
	chk = 0;
	for( i=0; i<fObj.elements.length; i++ ) {
		elmObj= fObj.elements[ i ];
		if ( elmObj.type == 'checkbox' && elmObj.name != 'checkbox' ) {
			if ( elmObj.checked ) {
				elmObj.checked = false;
			}
		}
	}
	DelCookie("jsCompare");
}


//---- Verif du click pour lancer le comparateur, 2 <= X <= 4
function check_compare() {
	
	var c = GetCookie("jsCompare");
	c = c.substr(0,(c.length - 1));
	var chk = 0; // = new Array();
	chk = c.split("@").length;
	
	// chk => nombre de biens cliquÃƒÂ©s
	if ( chk <= 1 || chk > 4 ) {
		alert( 'Veuillez s\351lectionner de 2 \340 4 annonces' );
		return false;
	}
	if (document.frmCompare.checkbox && document.frmCompare.checkbox.checked){
		document.frmCompare.checkbox.checked = false;
	}
	SetCookie("phpCompare", c);
	return true;	
}

//---- Envoi formulaire du comparateur
function frmCompareSubmit(){
	if(check_compare()){
		document.frmCompare.submit();
	}
}

//-- Fonction qui va chercher les prix pour remplir
//-- la 2Ã¨me combo de la recherche rapide
function Chercher(ag){
    if (document.getElementById("rsType").value != ""){
         var xmlhttp = new xhr("GET","../"+ag+"/ajRechRapide.php","type=" + document.getElementById("rsType").value,"affiche");
         xmlhttp.Request();
         document.getElementById("rsPrix").style.display = "block";
    }else{
    	document.getElementById("rsPrix").style.display = "none";
     	 document.getElementById("rsPrix").options.length = 0;
     }
}
function affiche(txt){
    var nbTranches = 4;
    var tdResponse = eval(txt);
    if (tdResponse.length >= (nbTranches * 2)){
    	    document.getElementById("rsPrix").options.length = 0;
	    var min, max;
	    var tr = Math.ceil(tdResponse.length / nbTranches);
	    for (i=0;i< nbTranches;i++){
	    	min = (i * tr);
	    	max = ((i * tr) + (tr - 1));
	    	if (max >= tdResponse.length){
	    	    max = (tdResponse.length - 1);
	    	}
	      	var opt = new Option (tdResponse[min] + " a " + tdResponse[max], tdResponse[min] + ":" + tdResponse[max]);
	       	document.getElementById("rsPrix").options[i] = opt;
	    }
    }else{
	      	document.getElementById("rsPrix").options.length = 0;
	      	var opt = new Option (tdResponse[0] + " a " + tdResponse[(tdResponse.length - 1)] , tdResponse[0] + ":" + tdResponse[(tdResponse.length - 1)]);
	       	document.getElementById("rsPrix").options[0] = opt;     
     }
//alert(tdResponse);
}
function checkRsRapide(){
    if (document.getElementById("rsType").value == ""){
         return false;
    }
}
function mkPtViewer(bicodag){
  //var ch = "http://srvlinux2.technolog.fr/vitrinesV2Lang/templates/ptviewer.php?bicodag="+bicodag;
  var ch = "http://ew.technolog.fr/ptviewer/ptviewer.php?bicodag="+bicodag;
  open(ch,'applet','width=435,height=530,toolbar=no,scrollbars=no,resizable=yes'); 
}

function nodeCleaner(n) {
	if(!n.data.replace(/\s/g,'')) n.parentNode.removeChild(n);
}
			
function cleanXML(docElement) {
	var node = docElement.getElementsByTagName('*');
	for(i = 0; i < node.length; i++) {
		a = node[i].previousSibling;
		if(a && a.nodeType == 3) nodeCleaner(a);
		b = node[i].nextSibling;
		if(b && b.nodeType == 3) nodeCleaner(b);
	}
	return docElement;
}

//--
function setPhotos(obj,tbPhotos){

    obj = cleanXML(obj);
    obj.removeChild(obj.firstChild);

    for (i=0;i<tbPhotos[0].length;i++){
         var dv = document.createElement('div');
         var a = document.createElement('a');
         a.href = tbPhotos[0][i][0];
         a.onclick = function(){
              changePhoto(this.firstChild,tbPhotos[1]);
              return false;
         }
         a.setAttribute("rel","lightbox[roadtrip"+tbPhotos[1]+"]");
         var im = document.createElement('img');
         im.src  = tbPhotos[0][i][1];
         im.style.visibility = 'visible';
         im.setAttribute("border","1");
         a.appendChild(im);
         dv.appendChild(a);
         obj.appendChild(dv);
    }
    
    var d = document.createElement('div');
    d.className = 'spacer';
    obj.appendChild(d);      
}


function ChangeVisi_1(bicodag)
{
  	if(document.getElementById)
  	document.getElementById(bicodag).style.display="inline";
}
	
function ChangeVisi_0(bicodag)
{
  	if(document.getElementById)
  	document.getElementById(bicodag).style.display="none";
}