function changeValeur(idChamp, valeur)
{
	var el = document.getElementById(idChamp);
	el.value = valeur;
}

function confirmation(msg)
{
	alert(msg);
}

function suppression_photo(id_photo)
{
	if (window.confirm('Etes vous sûr(e) de vouloir supprimer cette photo ?'))
	{
		if(window.XMLHttpRequest) // FIREFOX
			xhr_object = new XMLHttpRequest(); 
		else if(window.ActiveXObject) // IE
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
		else 
			return(false);		
		
		fichier = "http://www.locapart.fr/photo/services/photo_suppression.php?id_photo="+id_photo ;
		xhr_object.open("GET", fichier, true); 
		xhr_object.onreadystatechange= function() {
			if (xhr_object.readyState == 4) /* 4 : état "complete" */
			{
				if (xhr_object.status == 200) /* 200 : code HTTP pour OK */
				{
					var photo = document.getElementById(id_photo);
					photo.style.visibility = 'hidden';
					photo.style.width = '0';
					photo.style.height = '0';
					alert(xhr_object.responseText);
				}
			}
		}
		xhr_object.send(null); 
	}
	else {return false;}
}

function suppression_appartement(id_appartement)
{
	if (window.confirm('Etes-vous sûr(e) de vouloir supprimer l\'appartment n°'+id_appartement+' ?'))
	{
		if(window.XMLHttpRequest) // FIREFOX
			xhr_object = new XMLHttpRequest(); 
		else if(window.ActiveXObject) // IE
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
		else 
			return(false); 
		
		fichier = "http://www.locapart.fr/admin_supprimer.php?id_appart="+id_appartement ;
		xhr_object.open("GET", fichier, true); 
		xhr_object.onreadystatechange= function() {
			if (xhr_object.readyState == 4) /* 4 : état "complete" */
			{
				if (xhr_object.status == 200) /* 200 : code HTTP pour OK */
				{
					if ( xhr_object.responseText.substring(0, 1) == '1')
					{
						document.location.href='admin_attente_suppression.php';
					}
					else
					{
						alert(xhr_object.responseText.substring(1, xhr_object.responseText.length));
					}
				}
			}
		}
		xhr_object.send(null); 
	}
	else {return false;}
}

function validation_appartement(id_appartement)
{
	if (window.confirm('Etes vous sur de vouloir valider cet appartment?'))
	{
		if(window.XMLHttpRequest) // FIREFOX
			xhr_object = new XMLHttpRequest(); 
		else if(window.ActiveXObject) // IE
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
		else 
			return(false); 
		
		
		fichier = "http://www.locapart.fr/admin_valider.php?id_appart="+id_appartement ;
		xhr_object.open("GET", fichier, true); 
		xhr_object.onreadystatechange= function() {
			if (xhr_object.readyState == 4) /* 4 : état "complete" */
			{
				if (xhr_object.status == 200) /* 200 : code HTTP pour OK */
				{
					if ( xhr_object.responseText.substring(0, 1) == '1')
					{
						document.location.href='admin_attente_validation.php';
					}
					else
					{
						alert(xhr_object.responseText.substring(1, xhr_object.responseText.length));
					}
				}
			}
		}
		xhr_object.send(null); 
	}
	else {return false;}
}

function hauteur(id)
{
	if(document.getElementById) 
	{
		var h = parseInt(document.getElementById(id).style.height);
	}
	else if(document.all)
	{
		var h = parseInt(document.all[id].style.height);
	}
	return h;
}

function fa_sont_valide()
{
	var fa = document.getElementById('fraisAgence');
	var exp = new RegExp("^[0-9]{1,4}$");
	estValide = exp.test(fa.value);
	if(!estValide)
	{
		alert('Merci de bien vouloir saisir des frais d\'agence cohérents !');
		return false;
	}
	else
	{
		if (confirm('Etes-vous sûr(e) de vouloir valider cette location avec des frais d\'agence de ' + fa.value + ' € ?')) {
			return true;
		}
		else {
			return false;
		}
	}
}

function switchAfficheDiv(divId, idTitre)
{
  var div = document.getElementById(divId)
  var val = '';
  	
  if (div != null)
  {
    if ((div.style.visibility == 'visible') || (div.style.display == 'block'))
    {
    	cacheDiv(divId, idTitre);
        if (idTitre == "location") {val = 2;}
        else {val = 4;}
    }
    else
    {
    	afficheDiv(divId, idTitre);
        if (idTitre == "location") {val = 1;}
        else {val = 3;}
    }
	
	if(window.XMLHttpRequest) // FIREFOX
		var xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
		var xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else 
		return(false);
	fichier = "http://www.locapart.fr/commun/services/ajax.php?action=stock&ajxVal="+val ;
	xhr_object.open("GET", fichier, true);
	xhr_object.onreadystatechange = function() {
		if (xhr_object.readyState == 4) 
		{
			if (xhr_object.status == 200) 
			{
				/**alert (xhr_object.responseText);**/
			}
		}
	}
	
	xhr_object.send(null);
  }
}

function afficheDiv(divId, idTitre)
{
  var div = document.getElementById(divId);
  var divTitre = document.getElementById(idTitre);
  if (div != null)
  {
    div.style.visibility='visible';
    div.style.display='block';
  }
  if (divTitre != null)
  {
    divTitre.style.borderBottom='1px solid #AFB9E5';
    divTitre.style.backgroundImage='url(commun/affichage/images/moins.png)';
  }
}

function cacheDiv(divId, idTitre)
{
  var div = document.getElementById(divId);
  var divTitre = document.getElementById(idTitre);
  if (div != null)
  {
    div.style.visibility='hidden';
    div.style.display='none';
  }
  if (divTitre != null)
  {
    divTitre.style.borderBottom='none';
    divTitre.style.backgroundImage='url(commun/affichage/images/plus.png)';
  }
}

function changeLibelle(libId, valeur)
{
	var lib = document.getElementById(libId);
	if (lib != null)
	{
		lib.firstChild.nodeValue = valeur;
	}
}

function switchCocherTous(nomCase, tous)
{
	var ckbx = document.getElementsByName(nomCase);
	var ckbx_len = ckbx.length;
	var cocher = tous.checked;
	if (ckbx != null)
	{
		if (ckbx_len > 0) {
			for (var i = 0; i < ckbx_len; i++) {
				ckbx[i].checked = cocher;
			}
		}
	}
	return true;
}

function tousEstCoche(tous, laCase) {
	var ck_tous = document.getElementsByName(tous)[0];
	var tous_cocher = ck_tous.checked;
	
	var ckbx = document.getElementsByName(laCase);
	var ckbx_len = ckbx.length;
	if (tous_cocher) {
		if (ckbx_len > 0) {
			for (var i = 0; i < ckbx_len; i++) {
				if (!ckbx[i].checked) {
					ck_tous.checked = false;
				}
			}
		}
	}
	else
	{
		var tousestcocher = true;
		if (ckbx_len > 0) {
			for (var i = 0; i < ckbx_len; i++) {
				if (!ckbx[i].checked) {
					tousestcocher = false;
				}
			}
		}
		if (tousestcocher) {
			ck_tous.checked = true;
		}
	}
}

function switchVente(radioBtn, venteId, locId)
{
	if (radioBtn.value == 0)
	{
		afficheId(locId, 1);
	    cacheId(venteId);
	}
	else // radio == 1
	{
		cacheId(locId);
		afficheId(venteId, 1);
	}
}

function switchMaison(radioBtn, maisonId, appartId)
{
	if (radioBtn.value == 1)
	{
		afficheId(appartId, 1);
		cacheId(maisonId);
	}
	else // radio == 2
	{
		afficheId(maisonId, 1);
	    cacheId(appartId);
	}
}

function afficheId(baliseId, type)
{
	if (document.getElementById && document.getElementById(baliseId) != null)
	{
		document.getElementById(baliseId).style.visibility='visible';
		if(window.XMLHttpRequest) // Firefox
			document.getElementById(baliseId).style.display='table';
		else
			document.getElementById(baliseId).style.display='block';
	}
}

function cacheId(baliseId)
{
	if (document.getElementById && document.getElementById(baliseId) != null)
	{
		document.getElementById(baliseId).style.visibility='hidden';
		document.getElementById(baliseId).style.display='none';
	}
}
