//////////////////////////
// Fonctions generiques ajax
//////////////////////////


start_loading = function (which_div, is_loading){

	if(!is_loading){
		return false;
	}

	$('main_loading_div').innerHTML="";
	body_node = $('main_loading_div');
	new_loading_div=document.createElement("div");

	id_loading_div = "loading_"+which_div;
	new_loading_div.setAttribute("id", id_loading_div);
	body_node.appendChild(new_loading_div);
	$(id_loading_div).innerHTML += "<br><br><img src=ajax/img/ajax-loader.gif><br>Chargement...<br><br>";
	
	$(id_loading_div ).style.width = '400';
	$(id_loading_div ).style.textAlign = 'center';
	$(id_loading_div ).style.fontFamily = 'Arial';
	$(id_loading_div ).style.fontSize = '20px';
	$(id_loading_div ).style.fontWeight = 'bold';
	$(id_loading_div ).style.position = 'absolute';
	$(id_loading_div ).style.backgroundColor = '#FFFFFF';
	$(id_loading_div ).style.top = '100';
	$(id_loading_div ).style.left = getWidthCenterOfPage()-200;
	new Effect.Appear(id_loading_div);
}

fill_div = function ( which_div, url, params, is_loading){

// Cette fonction permet d'afficher le contenu du résultat d'une requete Ajax dans le div passé en paramètre	
//	prompt('',url+"?"+params);
	//	$(which_div).innerHTML = '';
	var myAjax = new Ajax.Updater( 
	which_div, 
	url, 
	{	
		method: 'get', 
		parameters: params, 
		evalScripts: true, 
		asynchronous:true,
		onCreate: start_loading(which_div, is_loading),
		onComplete: function(){
						new Effect.Fade("loading_"+which_div);
					}
	}
	); 
}


get_tag_value = function(which_tag_name){
// Cette fonction permet de récupérer le paramètre value du tag passé en paramètre	
	my_node = document.getElementsByTagName(which_tag_name)[0];
	return my_node.getAttribute('value');
}

set_tag_value = function(which_tag_name, which_value){
// Cette fonction permet remplir le paramètre value du tag passé en paramètre	
	my_node = document.getElementsByTagName(which_tag_name)[0];
	my_node.setAttribute('value', which_value);
}

//////////////////////////
// FIN Fonctions generiques ajax
//////////////////////////

maj_db = function (){
	which_div= 'dbug_div';
	params = '';
//	$('main_loading_div').innerHTML='';
	fill_div(which_div, "./ajax/maj_bdd.php", params);
}

reverse_checked_box = function(){
	tag_array = document.getElementsByTagName('input');
	for(i=0; i<tag_array.length; i++){
		my_node = tag_array[i];
		if ( my_node.getAttribute('name') == "code_to_publish[]" ){
			if ( my_node.getAttribute('checked') == "checked" ){
				my_node.removeAttribute('checked');
			} else {
				my_node.setAttribute('checked', 'checked');
			}
		}

	}
	
}

show_input_buyer = function (which_element){
// Fonctions destinées à l'affichage du champ de saisie du destinataire du report
	Element.show(which_element);
	var regexp =/^BUYER/g;// ou bien var MonReg = new RegExp("a", "g");
//   if (which_element == 'BUYER'  ) {
	if (regexp.test(which_element) ) {
		Element.show('img_select');
		Element.show('img_ok');
		Element.show('img_reset');
		Element.hide('link_send_report');
	} else {
		Element.show('img_select_for_all_sc');
		Element.show('img_ok_for_all_sc');
		Element.show('img_reset_for_all_sc');
		Element.hide('link_send_report_for_all_sc');
	}
}

hide_input_buyer = function (which_element){
// Fonctions destinées au masquage du champ de saisie du destinataire du report
	Element.hide(which_element);
	var regexp =/^BUYER/g;// ou bien var MonReg = new RegExp("a", "g");

//	if (which_element == 'BUYER' ) {
	if (regexp.test(which_element) ) {
		alert(which_element);
		Element.hide('img_select');
		Element.hide('img_ok');
		Element.hide('img_reset');
		Element.show('link_send_report');
	} else {
		Element.hide('img_select_for_all_sc');
		Element.hide('img_ok_for_all_sc');
		Element.hide('img_reset_for_all_sc');
		Element.show('link_send_report_for_all_sc');
	}
}


addItemToList = function(selected) {
// Cette fonction est utiliser pour proposer une liste de buyer dans l'action "send report"

   if ($('BUYER').style.display == 'none') {
	which_element = 'BUYER_for_all_sc';
	}else{
	which_element = 'BUYER';  
	}
//   $(which_element).value = " \" " +selected.getElementsByTagName("name")[0].getAttribute("value") + " " + "  \" <" + selected.getElementsByTagName("email")[0].getAttribute("value")+">";
   $(which_element).value = selected.getElementsByTagName("email")[0].getAttribute("value")+";";
}

show_detail = function(which_id){
	openWindow('search_detail.php?id='+which_id , 'search_detail' , 600 , 300 , 0 , 1 , 1 , 200 , getWidthCenterOfPage()-300);
}

show_detail2 = function(which_id){
	which_div= 'div_detail'+which_id;
	params = "id="+which_id;
//	$('main_loading_div').innerHTML='';
//prompt ("", "search_detail2.php"+params);

	if($(which_div).innerHTML == ""){
		fill_div(which_div, "./search_detail2.php", params);
		$('link_show_detail'+which_id).innerHTML = "Masquer les détails";
		$('img_show_detail'+which_id).src = "img/button_up_b.gif";
	} else {
		$(which_div).innerHTML = "";
		$('link_show_detail'+which_id).innerHTML = "Plus de détails";
		$('img_show_detail'+which_id).src = "img/button_down_b.gif";
	}
}

show_picture = function(which_picture_id, event){
	which_div= 'div_picture_detail';

	var mouse_x = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
	var mouse_y = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop);

	$(which_div ).style.width = '400';
	$(which_div ).style.position = 'absolute';
	$(which_div ).style.backgroundColor = '#FFFFFF';
	$(which_div ).style.top = mouse_y-150;
	$(which_div ).style.left = getWidthCenterOfPage()-200;
//	new Effect.Appear(which_div);

	url = "./picture_detail.php";
	params = "picture_id="+which_picture_id;
//	$('main_loading_div').innerHTML='';
	fill_div(which_div, url, params);
}




