/*-----------------------------------------------------*/
/*	Simple PHP and JavaScript API		       */
/*-----------------------------------------------------*/
/*	Autor: Wizekat				       */
/*	Company: Cosmo Soft			       */
/*	Licence: GPL				       */
/*	Date: 2010-06-27			       */
/*	version: 1.0b				       */
/*-----------------------------------------------------*/

var ur = document.location.toString();
var code = querySt('Code' );
if (ur.indexOf('nosOffres/ficheTechnique') > -1) {
//ur = str_replace("nosOffres/ficheTechnique.php?Code","annonce-.html?code",ur);
document.location = "/annonce-"+code+".html";
}

/*----------- APPLICATION JAVASCRIPT ---------------*/
function pageInit() {
var body = document.getElementsByTagName("body")[0];
  if (!footer_2) {
  var footer_2 = "";
  }
// creation du contenu de base
var weburl_txt = document.createTextNode(footer_2);
var copyr_t = document.createTextNode(ID_COPYR+' ');
var copyr_txt = document.createElement('a');
var copyr_a = document.createTextNode(COPYR_NOM);
copyr_txt.setAttribute('href','http://'+COPYR_URL);
copyr_txt.setAttribute('target','_blank');
var ml = document.createElement('a');
ml.setAttribute('href','mentions_legales.html');
var ml_a = document.createTextNode('Mentions légales');
//var navbas = document.createElement('ul');
 // navbas.setAttribute('id','navbas');

// nommage des zones
  container.setAttribute('id','container');
     header.setAttribute('id','header');
    boutons.setAttribute('id','topnav');
    content.setAttribute('id','content');
// formulaire.setAttribute('id','formulaire');
    contenu.setAttribute('id','contenu');
     footer.setAttribute('id','footer');
     paging.setAttribute('id','paging');
      copyr.setAttribute('id','copyr');
     weburl.setAttribute('id','url');
   mentions.setAttribute('id','mentions');
 // construction de la structure
       body.appendChild(container);
  container.appendChild(header);
  container.appendChild(boutons);
  container.appendChild(paging);
  container.appendChild(content);
//    content.appendChild(formulaire);
    content.appendChild(contenu);
  container.appendChild(footer);
//  footer.appendChild(navbas);
//     footer.appendChild(copyr);
//     footer.appendChild(weburl);
//     footer.appendChild(mentions);

     weburl.appendChild(weburl_txt);
      copyr.appendChild(copyr_t);
      copyr.appendChild(copyr_txt);
  copyr_txt.appendChild(copyr_a);
   mentions.appendChild(ml);
	 ml.appendChild(ml_a);
// assemblage
buildNav();	// construction de la navigation
loadPage();	// chargement de la page
//mkForm();	// construction du formulairede recherche
//buildForm();	// de recherche
}
function loadPage() {	// fonction principale pour générer les pages et leur contenu
var url = ID_AJX;
var zone = ID_ZONE;
var div = document.getElementById(zone);
div.innerHTML = "<div class='loading'><img src='"+ID_LOADING+"' alt='Chargement en cours...' /></div>";
buildNav();
var cat = pageName();  						// analyse url et query string
  if (cat.indexOf("-") > -1) {					// url de type: vente-maison ou annonce-706 (2 paramètres)
  var re = cat.split("-");
    if (re[0] == "annonce") {    				// fiche détail
    url += "?act=fiche&code="+re[1];
    cat = "annonce";
    }
    else {    							// recherche et liste
    var page = querySt('page' );				// num de page pour les listes
      if (page == "" || page == undefined) {
      page = 1;
      }
    var trans = re[0].substring(0,1).toUpperCase();		// type transaction
    var type = re[1].toUpperCase();				// type de bien
    var der = type.length-1;
      if (type.substring(der,der+1) == "S") {			// suppression pluriel des types de bien
      type = type.substring(0,der);
      }
    var qry = " AND TypeAffaire = '"+type+"'";			// creation requete SQL
    qry = base64_encode(qry);					// encodage
    url += "?act=liste&"+ID_RECHERCHE+"="+qry+"&page="+page;	// creation url ajax
    }
  }
  else {							// url de type: acquereur, estimation, contact (pages simples)
  var code = querySt('code');
  var recherche = querySt(ID_RECHERCHE);
  var page = querySt('page' );					// num de page pour les listes
  var ld = querySt('ld' );
    if (page == "" || page == undefined) {
    page = 1;
    }
  url += "?act="+cat  						// creation url ajax
     if (cat == "alerte_email") {
     var pagename = querySt('pagename' );
     var sesId = querySt('sesId' );
	if (pagename == undefined) {
	pagename = "index";
	}
     url += "&pagename="+pagename+"&sesId="+sesId;
    }
    else  if (code != "" && code != undefined) {
    url += "&code="+code;
    }
    else if (recherche != "" && recherche != undefined) {
    qry = recherche;
    url = ID_AJX + "?act=liste&"+ID_RECHERCHE+"="+recherche+"&page="+page; // +"&ld="+ ld;
    }
    else if (cat == "liste") {
    url = ID_AJX + "?act=liste&page="+page; // +"&ld="+ ld;
    }

  }
preProcess(cat);
jGet(zone,url);							// envoi url ajax
processPage(cat,page,qry);					// post-traitement page
return true;
}
function preProcess(nompage) {
  switch(nompage) {
    case "liste" :
    setTimeout("listHeader()",AJX_DELAY);
    setTimeout("buildForm()",AJX_DELAY);
    //setTimeout("setSelected('"+qry+"')",AJX_DELAY);
    break;
        case "alerte" :
      var pagename = querySt('pagename' );
      url = ID_AJX + "?act=&pagename="+pagename;
      break;
  }
}
function processPage(nompage,page,recherche) {	// post-traitement page
  switch(nompage) {
    case "annonce" :
    setTimeout("annonce()",AJX_DELAY);
    hide('titrePage');
    hide('paging2');
    break;
    case "accueil" :
    hide('titrePage');
    hide('paging');
    setTimeout("coups2coeur1(5,0)",1000);
    setTimeout("coups2coeur2(5,0)",1500);
    setTimeout("buildForm()",AJX_DELAY);
    setTimeout("mkForm()",AJX_DELAY);
    break;
    case "vente-maisons" :
    case "vente-appartements" :
    case "vente-terrains" :
    case "vente-divers" :
    hide('titrePage');
    break;
    case "liste" :
    hide('titrePage');
    setTimeout("getPages('"+page+"','"+recherche+"')", AJX_DELAY);;
    //setTimeout("listHeader()",AJX_DELAY);
    break;
    case "financement" :
    hide('titrePage');
    hide('paging');
    break;
    case "contactez_nous" :
    setTimeout("buildContactForm()",AJX_DELAY);
    hide('titrePage');
    hide('paging');
    break;
    case "agence" :
    hide('titrePage');
    hide('paging');
    break;
    case "recherche" :
    hide('titrePage');
    hide('paging');

    setTimeout("buildForm()",AJX_DELAY);
    setTimeout("mkForm()",AJX_DELAY);
    break;
    case "form" :
    setTimeout("mkForm()",AJX_DELAY);
    hide('titrePage');
    hide('paging');
    break;
    default :
    hide('titrePage');
    hide('paging');
    break;
  }
return true;
}
function annonce() {
  /*
  try {
  document.getElementById('entete-liste').style.height = '100px';
  document.getElementById('entete-liste').style.width = '900px';
  }
  catch (err) {
  return annonce();
  }
listHeader();
buildForm();
*/
}
function setSelected(qry) {
var sq = base64_decode(qry);
var wr = sq.split("=");
var field = wr[0];
var val = wr[1].replace(/['"]/g,'');
val = val.replace(/["'"]/g,'');
field = str_replace(' AND ','',field);
eval("var sel = document.formRecherche."+field);
var id = sel.options.length;
alert(sel.options.length);
  for (var i=0; i<id; i++) {
    alert(sel.options[i].value);
    if (sel.options[i].value = val) {
      sel.options[i].selected = true;
    }
  }
}
function listHeader() {
var div,txt,br;
var contenu = document.getElementById("paging");
  try {
  var hd = document.getElementById("entete-liste");
  }
  catch(err) {
 return listHeader();
  }
var numpages = document.createElement("div");
numpages.setAttribute('id','paging2');
numpages.setAttribute('class','cleared');

var f = document.createElement("form");
f.setAttribute('name','formRecherche');
f.setAttribute('method','get');
f.setAttribute('action','#');

var col1 = document.createElement("div");
col1.setAttribute('class','left');
col1.setAttribute('style','width:390px;');

var col2 = document.createElement("div");
col2.setAttribute('class','left');
col2.setAttribute('style','width:220px;padding:19px;');

/*
  for( var i=2; i<searchFields.length; i++) {
  mkSelectTop(col1,searchFields[i],searchFieldsLabel[i]);
  }
*/
mkSelectTop(col1,searchFields[0],searchFieldsLabel[0]);
mkSelectTop(col1,searchFields[1],searchFieldsLabel[1]);

div = document.createElement("div");
div.setAttribute('class','cleared');
col1.appendChild(div);

div = document.createElement("div");
div.setAttribute('class','left');
div.setAttribute('style','width:190px;height:35px;padding:2px;text-align:left;');
txt = document.createTextNode("Référence du bien");
div.appendChild(txt);
br = document.createElement("br");
div.appendChild(br);
mkInput(div,'NMandat','NMandat'),

col1.appendChild(div);
mkSelectTop(col1,searchFields[2],searchFieldsLabel[2]);

var s = document.createElement("input");
s.setAttribute('type','button');
//s.setAttribute('src','images/rechercher.jpg');
s.setAttribute('class','ibtn');
s.setAttribute('name','go');
s.setAttribute('value',SEARCH_BTN_LABEL);
s.setAttribute('onmouseout',"this.src='images/rechercher.jpg'");
s.setAttribute('onmouseover',"this.src='images/rechercher-over.jpg'");
s.setAttribute('onclick','sendRecherche()');

col2.appendChild(s);

f.appendChild(col1);
f.appendChild(col2);

f.appendChild(numpages);
  try {
  hd.appendChild(f);
  }
  catch(err) {
  document.location.reload();
  }
}
function mkSelectTop(f,field,nom) {
var br =  document.createElement("br");
var div2 =  document.createElement("div");
var sel_txt = document.createElement("strong");
var sel_t = document.createTextNode(nom);
var sel = document.createElement('select');

div2.setAttribute('class','left');
div2.setAttribute('style','width:190px;height:35px;padding:2px;text-align:left;');

sel.setAttribute('name',field);
sel.setAttribute('id',field);
sel.setAttribute('class','select');

//sel_txt.appendChild(sel_t);

div2.appendChild(sel_t);
div2.appendChild(br);
div2.appendChild(sel);
f.appendChild(div2);

  if (field == "Prix1" || field == "Prix2") {
  var nom, val;
  var o = null;
  var s = sel; //document.forms["formRecherche"].elements["Prix1"];
  s.options.length = 0;
  s.options[0] = new Option("Indifférent"); s.options[0].value = "";
  var inter = 50000;
    for (var i=1; i<13; i++) {
    val = i * inter;
    nom = number_format(val,0,","," ");
    s.options[i] = new Option(nom); s.options[i].value = val;
    }
    if (field == "Prix2") {
    s.options[i] = new Option("plus de "+nom); s.options[i].value = 9999999;
    }
    if (field == "Prix1") {
    s.options[i] = new Option("plus de "+nom); s.options[i].value = val +1;
    }
  }
}
function mkSelect(f,field,nom) {
var div1 =  document.createElement("div");
var div2 =  document.createElement("div");
var sel_txt = document.createElement("strong");
var sel_t = document.createTextNode(nom);
var sel = document.createElement('select');
div1.setAttribute('class','cleared');
//div1.setAttribute('style','width:160px;height:35px;padding:5px;');
div2.setAttribute('class','cleared');
//div2.setAttribute('style','width:155px;height:35px;padding-right:5px;');
sel.setAttribute('name',field);
sel.setAttribute('id',field);
sel.setAttribute('class','select');
//sel_txt.appendChild(sel_t);
div1.appendChild(sel_t);
f.appendChild(div1)
div2.appendChild(sel);
f.appendChild(div2)

  if (field == "Prix1" || field == "Prix2") {
  var nom, val;
  var o = null;
  var s = sel; //document.forms["formRecherche"].elements["Prix1"];
  s.options.length = 0;
  s.options[0] = new Option("Indifférent"); s.options[0].value = "";
  var inter = 50000;
    for (var i=1; i<13; i++) {
    val = i * inter;
    nom = number_format(val,0,","," ");
    s.options[i] = new Option(nom); s.options[i].value = val;
    }
    if (field == "Prix2") {
    s.options[i] = new Option("plus de "+nom); s.options[i].value = 9999999;
    }
    if (field == "Prix1") {
    s.options[i] = new Option("plus de "+nom); s.options[i].value = val +1;
    }



  }
}

function mkInput(f,field,nom,classname) {
  if (classname == undefined) {
    classname = 'select';
  }
var inp = document.createElement("input");
inp.setAttribute('name',field);
inp.setAttribute('id',field);
inp.setAttribute('class',classname);
inp.setAttribute('value','');
f.appendChild(inp);
}
function mkForm() {
var div = document.createElement("div");
var div1 =  document.createElement("div");
var div2 =  document.createElement("div");

var form = document.getElementById('formulaire');
var f = document.createElement("form");
div.setAttribute('class','cleared center');
div1.setAttribute('class','cleared left strong');
div1.setAttribute('style','width:160px;height:35px;padding:5px;');
div2.setAttribute('class','cleared');
//div2.setAttribute('style','width:155px;height:35px;padding-right:5px;');

f.setAttribute('name','formRecherche');
f.setAttribute('method','get');
f.setAttribute('action','#');
//var t = document.createElement("h3");
//t.setAttribute('class','titre-encart');
//var t_t = document.createTextNode(SEARCH_LABEL);
//t.appendChild(t_t);
//f.appendChild(t);
  for( var i=0; i<searchFields.length; i++) {
  mkSelect(f,searchFields[i],searchFieldsLabel[i]);
  }
div1 = document.createElement("div");
var nm = document.createTextNode('Référence');
div1.appendChild(nm);
f.appendChild(div1);
mkInput(div2,'NMandat','NMandat');
f.appendChild(div2);
div = document.createElement("div");
div.setAttribute('class','cleared');

f.appendChild(div);
var s = document.createElement("input");
s.setAttribute('type','button');
//s.setAttribute('src','images/rechercher.jpg');
s.setAttribute('class','ibtn');
s.setAttribute('name',SEARCH_BTN_LABEL);
s.setAttribute('value',SEARCH_BTN_LABEL);
s.setAttribute('style','margin-left:25px;')
//s.setAttribute('onmouseout',"this.src='images/rechercher.jpg'");
//s.setAttribute('onmouseover',"this.src='images/rechercher-over.jpg'");
s.setAttribute('onclick','sendRecherche()');
//s.setAttribute('style','margin-top:30px;');

var ct = document.createElement("center");
ct.appendChild(s);

div.appendChild(s);
f.appendChild(div);
form.appendChild(f);
}
function buildNav() { // fonction pour construire la navigation
var url = ID_AJX;
var zone = ID_NAV;
url+= "?act=buildNav";
jGet(zone,url);


url = ID_AJX+"?act=buildNavBas";
jGet("footer",url);

return true;
}
function buildContactForm() {
var txt,tr,td1,td2;
var cen = document.createElement("center");
var br = document.createElement("br");
var div = document.getElementById('contact');
div.style.textAlign = "center";
var f = document.createElement("form");
f.setAttribute('name','formContact');
f.setAttribute('method','get');
f.setAttribute('action','#');
/*
var s = document.createElement("input");
s.setAttribute('type','button');
s.setAttribute('class','button');
s.setAttribute('value','Envoyer');
s.setAttribute('onclick','sendContact()');
*/
var s = document.createElement("input");
s.setAttribute('type','button');
s.setAttribute('class','ibtn');
s.setAttribute('name','envoyer');
s.setAttribute('value','Envoyer');
//s.setAttribute('onmouseout',"this.src='images/envoyer.jpg'");
//s.setAttribute('onmouseover',"this.src='images/envoyer-over.jpg'");
s.setAttribute('onclick','sendContact()');
s.setAttribute('style','margin:10px;');

var tbl = document.createElement("table");
//tbl.setAttribute('class','center');

for( var i=0; i<contactFields.length-1; i++) {
  tr = document.createElement("tr");
  td1= document.createElement("td");
  td2= document.createElement("td");

  txt = document.createTextNode(contactFieldsLabel[i]+" :");
  td1.setAttribute('align','left');
  td2.setAttribute('align','right');
 // td1.setAttribute('width','200');
  td1.setAttribute('width','150');
  td1.appendChild(txt);
  tr.appendChild(td1);
  mkInput(td2,contactFields[i],contactFieldsLabel[i],'medium');
  tr.appendChild(td2);
  tbl.appendChild(tr);
  }

i++;
  tr = document.createElement("tr");
  td1= document.createElement("td");
  td2= document.createElement("td");
  txt = document.createTextNode(contactFieldsLabel[contactFields.length-1]+" :");
  td1.setAttribute('align','left');
  td2.setAttribute('align','right');
  td1.setAttribute('width','150');
  td1.appendChild(txt);
  tr.appendChild(td1);
  var ta = document.createElement("textarea");
  ta.setAttribute('name',contactFields[contactFields.length-1]);
  ta.setAttribute('class','medium');
  td2.appendChild(ta);
  tr.appendChild(td2);
  tbl.appendChild(tr);

tr = document.createElement("tr");
td1= document.createElement("td");
td1.setAttribute('colspan','2');
td1.setAttribute('align','center');
td1.appendChild(br);
td1.appendChild(s);
tr.appendChild(td1);
tbl.appendChild(tr);

div.innerHTML = "";
cen.appendChild(f);

div.appendChild(cen);
f.appendChild(tbl);
}
function buildForm() {
 var url = ID_AJX;
url+= "?act=multi";
 for( var i=0; i<searchFields.length; i++) {
 evurl = url + "&field=" + searchFields[i];
 evGet(evurl);
 }
 //setTimeout("setSelected('"+qry+"')",AJX_DELAY);
}

