function get_idlot(element){
	while(getParent(element, 'div').className!='lot'){
		element = getParent(element, 'div');
	}
	
	id = element.id.split("_");
	id = id[id.length-1];
	
	alert(id);
}

function ajoute_lot(droit, programme){
	var xhr = getXHR();
	if (xhr == null) {
		alert("Votre navigateur ne supporte pas AJAX :-(");
		return;	
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			//alert(xhr.responseText);
			trie_lot(droit, programme, 'ref', 'ASC')
		}
	}
	
	ajax = "extras/lot.php?";
	ajax += "action=sauver";
	ajax += "&id=0";
	ajax += "&residence="+programme;
	ajax += "&nature=dispo";
	
	xhr.open("GET", ajax ,true);
	xhr.send(null);
}

function add_lot(){

}

function supprime_lot(id){
	if(confirm('Etes vous certain de vouloir supprimer ce lot')){
		var xhr = getXHR();
		$("lot_"+id).style.display = "none";
		ajax = "extras/lot.php?";
		ajax += "action=supprimer";
		ajax += "&id="+id;
		xhr.open("GET", ajax ,true);
		xhr.send(null);
	}
}

function edit_lot(id){
	fen_lot = pop_it('extras/lot_intro.php?id='+id, 'lot', 450, 250);
	fen_lot.focus();
}

function save_lot(id){
	var xhr = getXHR();
	if (xhr == null) {
		alert("Votre navigateur ne supporte pas AJAX :-(");
		return;	
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			//alert(xhr.responseText);
			$("lot_"+id).getElementsByClassName('save_lot')[0].style.display = "none";
			$("lot_"+id).getElementsByClassName('annule_lot')[0].style.display = "none";
		}
	}
	
	ajax = "extras/lot.php?";
	ajax += "action=sauver";
	ajax += "&id="+id;
	
	//recupere toutes les valeurs
	les_input = $('lot_'+id).getElementsByTagName('input');
	for(i=0; i<les_input.length; i++){
		ajax += "&"+les_input[i].className+"="+les_input[i].value;
	}
	les_select = $('lot_'+id).getElementsByTagName('select');
	for(i=0; i<les_select.length; i++){
		ajax += "&"+les_select[i].className+"="+les_select[i].value;
	}
	
	xhr.open("GET", ajax ,true);
	xhr.send(null);
	
}

function annule_lot(id){
	var xhr = getXHR();
	if (xhr == null) {
		alert("Votre navigateur ne supporte pas AJAX :-(");
		return;	
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			tab = xhr.responseText.split(" | ");
			for(i=0; i<tab.length; i++){
				val = tab[i].split(" : ");
				if($(val[0]+"_"+id))$(val[0]+"_"+id).value = val[1];
			}
			
			if($("lotVignette_"+id).value == "")$("vignette_"+id).src = "extras/images/lot_vignette2.jpg";
			else $("doc_"+id).src = $("lotVignette_"+id).value;
			
			if($("lotDoc_"+id).value == "")$("doc_"+id).src = "extras/images/doc_no.png";
			else $("doc_"+id).src = "extras/images/doc_ok.png";
			
			
			$("dispo_lot_"+id).style.background = "#CCC";
			$("option_lot_"+id).style.background = "#CCC";
			$("option_lot_"+id).innerHTML = "&nbsp;";
			$("vendre_lot_"+id).style.background = "#CCC";
			
			switch($("lotNature_"+id).value){
			default:
			case 'dispo': $("dispo_lot_"+id).style.background = "#b3ffb3"; break;
			case 'option': $("option_lot_"+id).style.background = "#ffe67f"; break;
			case 'resa': $("vendre_lot_"+id).style.background = "#ff4848"; break;			
			}
			
			$("lot_"+id).getElementsByClassName('save_lot')[0].style.display = "none";
			$("lot_"+id).getElementsByClassName('annule_lot')[0].style.display = "none";
		}
	}
	
	ajax = "extras/lot.php?action=annuler&id="+id;
	
	xhr.open("GET", ajax ,true);
	xhr.send(null);
}

function change_lot(id){
	$("lot_"+id).getElementsByClassName('save_lot')[0].style.display = "block";
	$("lot_"+id).getElementsByClassName('annule_lot')[0].style.display = "block";
}

function trie_lot(droit, programme, ordre, up){
	var xhr = getXHR();
	if (xhr == null) {
		alert("Votre navigateur ne supporte pas AJAX :-(");
		return;	
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			$("centre").innerHTML = xhr.responseText;
			resize();
		}
	}
	
	ajax  = "extras/lots_affiche_dyn.php?";
	ajax += "id_programme="+programme;
	ajax += "&ordre_lots="+ordre;
	ajax += "&up="+up;
	ajax += "&niv_acces="+droit;
	ajax += "&titre="+$("centre").getElementsByTagName('h1')[0].innerHTML;
	ajax += "&page="+$("page").value;
	ajax += "&nbres="+$("nbres").value;
	
	xhr.open("GET", ajax ,true);
	xhr.send(null);
}

function go_ancre(ancre){
	window.location = "#a_"+ancre;
}

function dispo_lot(id, nature){
	nature = $("lotNature_"+id).value;
	if(nature!='dispo'){
		$("lotNature_"+id).value="dispo";
		$("dispo_lot_"+id).style.background = "green";//#b3ffb3";
		$("option_lot_"+id).style.background = "#CCC";
		$("option_lot_"+id).innerHTML = "&nbsp;";
		$("vendre_lot_"+id).style.background = "#CCC";
		change_lot(id);
	}
}

function option_lot(id, nature){
	nature = $("lotNature_"+id).value;
	$("lotNature_"+id).value="option";
	$("option_lot_"+id).style.background = "orange";//"#ffe67f";
	$("option_lot_"+id).innerHTML = "72:00";
	
	if(nature=='option' || nature=='poursuivre'){
		$("lotNature_"+id).value="poursuivre";
	}
	else{
		$("dispo_lot_"+id).style.background = "#CCC";
		$("vendre_lot_"+id).style.background = "#CCC";
	}
	change_lot(id);
}

function vendre_lot(id, nature){
	nature = $("lotNature_"+id).value;
	if(nature!="resa"){
		$("lotNature_"+id).value="resa";
		$("vendre_lot_"+id).style.background = "red";//"#ff4848";
		
		$("dispo_lot_"+id).style.background = "#CCC";
		$("option_lot_"+id).style.background = "#CCC";
		$("option_lot_"+id).innerHTML = "&nbsp;";
		change_lot(id);
	}
}

function option_lot_membre(id, user){
	alert(user);
	pop_it('extras/reservation.php?id='+id+'&user='+user, 'fen', 350, 180);
}

function vendre_lot_membre(id, user){
	alert(user);
	pop_it('extras/reservation.php?id='+id+'&user='+user, 'fen', 380, 210);
}

function vignette_lot(id){
	$("choixDoc").style.display = "none";
	$("choixDoc").style.top = mouseY-300+'px';
	$("choixDoc").style.left = mouseX+30+'px';
	
	$("choixDoc").style.display = "block";
	$("choixDoc").getElementsByTagName('input')[0].value = id;
}

function doc_lot(id){
	$("choixDoc").style.display = "none";
	$("choixDoc").style.top = mouseY-300+'px';
	$("choixDoc").style.left = mouseX+30+'px';
	
	$("choixDoc").style.display = "block";
	$("choixDoc").getElementsByTagName('input')[0].value = id;
}

function lotUpdateDoc(element){
	id = element.id.split("_");
	id = id[id.length-1];
	change_lot(id);
	if($("lotDoc_"+id).value!=""){
		$("doc_"+id).src = "extras/images/doc_ok.png";
		$("doc_"+id).title = $("lotDoc_"+id).value;
	}
}

function lotUpdateVignette(element){
	id = element.id.split("_");
	id = id[id.length-1];
	change_lot(id);
	if($("lotVignette_"+id).value!=""){
		$("vignette_"+id).src = $("lotVignette_"+id).value;
		$("vignette_"+id).title = $("lotVignette_"+id).value;
	}
}
