window.addEvent('domready',setEvents);
/**
 *
 * @access public
 * @return void
 **/
function afficherChoixCommande(id_package,type,id_marche){
	$("hidden_id_package").value = id_package;
	$("hidden_type_telechargement").value = type;
	$("hidden_id_marche").value = id_marche;
	centerElement("div_choix_commande",0,-70);
}
/**
 *
 * @access public
 * @return void
 **/
function choixTelechargement(){
	var id_package = $("hidden_id_package").value;
	var id_marche = $("hidden_id_marche").value;
	var type_telechargement = $("hidden_type_telechargement").value;
	$("div_choix_commande").style.display="none";
	if (type_telechargement == "payant") {
		showModeTarificationDCE(id_package);
	}
	else {
		getMarketFilesTable2(id_marche);
	}
}
/**
 *
 * @access public
 * @return void
 **/
function choixPapier(){
	var id_package = $("hidden_id_package").value;
	document.location.href="index.php?option=com_reprodemat&task=editCommandeDce&cid="+id_package;
}
/**
 *
 * @access public
 * @return void
 **/
function fermerChoixCommande(){
	$("div_choix_commande").style.display="none";
}
/**
 *
 * @access public
 * @return void
 **/
function showModeTarificationDCE(id_package)
{
    var div_update="div_msg_box";
    $(div_update).style.minWidth="500px";
    $(div_update).style.width="500px"; //for those who dont support minWidth css

    $(div_update).innerHTML="<img src='../components/com_reprodemat/images/progress.gif' /> En attente de réponse du serveur...";
    centerElement(div_update);
	var myreq=new Request({
			url:'index.php?option=com_reprodemat&task=showModeTarificationDCE&id_package='+id_package,
			method: 'get',
			urlEncoded:true,
			onComplete:function(response)
            {
                $(div_update).innerHTML=response;;
                centerElement("div_msg_box");
                //init_actions();// call javascript of upload file
            }
			});
	myreq.send();
    return false;
}
///////////////////////////////////////////////////////////////////////////////
function showModeTarificationDCELigne(id_ligne)
{
    var div_update="div_msg_box";
    $(div_update).style.minWidth="500px";
    $(div_update).style.width="500px"; //for those who dont support minWidth css
    $(div_update).innerHTML="<img src='../components/com_reprodemat/images/progress.gif' /> En attente de réponse du serveur...";
    centerElement(div_update);
	var myreq=new Request({
			url:'index.php?option=com_reprodemat&task=showModeTarificationDCELigne&id_ligne='+id_ligne,
			method: 'get',
			urlEncoded:true,
			onComplete:function(response)
            {
                $(div_update).innerHTML=response;;
                centerElement("div_msg_box");
                //init_actions();// call javascript of upload file
            }
			});
	myreq.send();
    return false;
}

///////////////////////////////////////////////////////////////////////////////
function showPackageChequeInfo(id_package)
{
    var div_update="div_msg_box";
    $(div_update).style.minWidth="500px";
    $(div_update).innerHTML="<img src='../components/com_reprodemat/images/progress.gif' /> En attente de réponse du serveur...";
    centerElement(div_update);
	var myreq=new Request({
			url:'index.php?option=com_reprodemat&task=showPackageChequeInfo&id_package='+id_package,
			method: 'get',
			urlEncoded:true,
			onComplete:function(response)
            {
                $(div_update).innerHTML=response;;
                centerElement("div_msg_box");
                //init_actions();// call javascript of upload file
            }
			});
	myreq.send();
    return false;
}

var hiddencontent = new Array()
function setEvents(){
	var bt_afficher_fichiers_marche=$('adminlist').getElements('.bt_afficher_fichiers_marche');
	bt_afficher_fichiers_marche.each(function(img){
									img.addEvent('click',getMarketFilesTable);
									}
									);
	if (document.all) {
	}
	else{
		document.captureEvents(Event.mousemove);
	}
}
/**
 *
 * @access public
 * @return void
 **/
function getMarketFilesTable()
{
	var id_marche=this.id.substr(this.id.indexOf("-")+1);
	this.removeEvent('click',getMarketFilesTable);
	this.addEvent('click',hideMarketFilesTable);

	var div_MarketFilesTable=$('div_MarketFilesTable-'+id_marche);
		if (div_MarketFilesTable!=undefined){
			var myreq=new Request.HTML({
									method:'post',
									update: div_MarketFilesTable,
									url: 'index.php?option=com_reprodemat&task=ShowFichiersMarche',
									data: 'id_marche='+id_marche,
									urlEncoded:true,
									onComplete:function(){MAJ_div_MarketFilesTable(id_marche)},
									onFailure:Request_error
								  });
			Ajax_loading_show();
			myreq.send();
		}
	return false;
}
/**
 *
 * @access public
 * @return void
 **/
function getMarketFilesTable2(id)
{
	var id_marche=id;
	var this_lien = $("bt_Aff_Fichier_marche-"+id_marche);
	this_lien.removeEvent('click',getMarketFilesTable);
	this_lien.addEvent('click',hideMarketFilesTable);
	var div_MarketFilesTable=$('div_MarketFilesTable-'+id_marche);

		if (div_MarketFilesTable!=undefined){
			var myreq=new Request.HTML({
									method:'post',
									update: div_MarketFilesTable,
									url: 'index.php?option=com_reprodemat&task=ShowFichiersMarche',
									data: 'id_marche='+id_marche,
									urlEncoded:true,
									onComplete:function(){MAJ_div_MarketFilesTable(id_marche)},
									onFailure:Request_error
								  });
			Ajax_loading_show();
			myreq.send();
		}
	return false;
}
/**
 *
 * @access public
 * @return void
 **/
function hideMarketFilesTable(){
	var id_marche=this.id.substr(this.id.indexOf("-")+1);
	var div_MarketFilesTable=$('div_MarketFilesTable-'+id_marche);
	div_MarketFilesTable.set('html','');
	this.addEvent('click',getMarketFilesTable);
	return false;

}
function MAJ_div_MarketFilesTable(id_marche){
	Ajax_loading_hide();
	var imgShowFiles=$('div_MarketFilesTable-'+id_marche).getElements('.imgShowFiles');
	imgShowFiles.each(function(img){
									img.addEvent('click',getContentDivShowFiles);
									}
									);
}
/**
 *
 * @access public
 * @return void
 **/
function getContentDivShowFiles(MAJHTML){
	if (MAJHTML==undefined) {
		MAJHTML=true;
	}
	var id_lignePackageDce=this.id.substr(this.id.indexOf("-")+1);
	id_lignePackageDce=parseInt(id_lignePackageDce);
	if (MAJHTML) {
		hiddencontent[id_lignePackageDce]=$('divShowFiles-'+id_lignePackageDce).innerHTML;
	}
	if (id_lignePackageDce>0)
    {
		var myreq=new Request.HTML({
								method:'post',
								update: $('divShowFiles-'+id_lignePackageDce),
								url: 'index.php?option=com_reprodemat&task=getContentDivPackageDceFiles',
								data: 'id_lignePackageDce='+id_lignePackageDce,
								urlEncoded:true,
								onComplete:function(){MAJ_DivShowFiles(id_lignePackageDce)},
								onFailure:Request_error
							  });
		Ajax_loading_show();
		myreq.send();
	}
}
/**
 *
 * @access public
 * @return void
 **/
function Ajax_loading_show(){
	var AjaxLoading=$('Ajax-loading');
	document.addEvent('mousemove',Ajax_loading_followMouse);
	AjaxLoading.style.display='';
	AjaxLoading.fade('show');

}
/**
 *
 * @access public
 * @return void
 **/
function Ajax_loading_followMouse(e){

	var AjaxLoading=$('Ajax-loading');
	AjaxLoading.style.left=getmouseX(e)+15+"px";
	AjaxLoading.style.top=getmouseY(e)+15+"px";
}
/**
 *
 * @access public
 * @return void
 **/
function Ajax_loading_hide(){
	document.removeEvent('mousemove', Ajax_loading_followMouse);
	var AjaxLoading=$('Ajax-loading').fade('hide');
}
/**
 *
 * @access public
 * @return void
 **/
function getmouseX(e){
	if (window.event){
    	return window.event.clientX;
	}
  	else{
    	return e.page.x;
	}
}
/**
 *
 * @access public
 * @return void
 **/
function getmouseY(e){
	if (window.event){
		return window.event.clientY;
	}
  	else{
    	return e.page.y;
	}
}
/**
 *
 * @access public
 * @return void
 **/
function MAJ_DivShowFiles(id_lignePackageDce){
	Ajax_loading_hide();
	$('ShowFiles_backButton-'+id_lignePackageDce).addEvent('click',HidefilesTable);
	var files_link=$('filesTable-'+id_lignePackageDce).getElements('.File_link');
	var bound_ScheduleRefreshOnClick=ScheduleRefreshOnClick.bind(files_link,id_lignePackageDce);
	files_link.each(
					function(link){
							link.addEvent('click',bound_ScheduleRefreshOnClick);
						}
					);
}
/**
 *
 * @access public
 * @return void
 **/
function ScheduleRefreshOnClick(id_lignePackageDce){
	var obj=$('divShowFiles-'+id_lignePackageDce);
	var boundfunc=getContentDivShowFiles.bind(obj,false);
	var timeout=boundfunc.delay(500)
}
function Request_error(response){
	var fen=window.open("","Erreur", "height=500, width=600,toolbar=0, menubar=0, scrollbars=1, resizable=1, status=0, location=0, left=10, top=10");
	fen.document.write(response);
}
/**
 *
 * @access public
 * @return void
 **/
function HidefilesTable(){
	var id_lignePackageDce=this.id.substr(this.id.indexOf("-")+1);
	id_lignePackageDce=parseInt(id_lignePackageDce);
	$('divShowFiles-'+id_lignePackageDce).set('html',hiddencontent[id_lignePackageDce]);
	(function(){
				$('imgShowFiles-'+id_lignePackageDce).addEvent('click',getContentDivShowFiles);
				}).delay(500);
	//delete hiddencontent[id_lignePackageDce];

}

/*
 * show PackageDCE info box
 */
function showDCEInfo(id_marche,id_packagedce){
	//copy contents into popup box
	$('div_dce_info_container').innerHTML=$('div_info_'+id_packagedce).innerHTML;
	//reset question input box
	$('txtQuest').value="";
	//store id_marche in hidden field
	$('id_marche').value=id_marche;
	//open and center the box
	centerElement('div_dce_info');
	//alert(id_marche+" , "+id_packagedce);
	return false;
}
//=============================================================
function swithViewAnswer(q,div_id)
 {
   var div_obj=document.getElementById(div_id)
   if(div_obj)
   {
     if(div_obj.style.display=='block')
      {
        div_obj.style.display='none';
        q.value="Voir les réponses";
      }
     else
      {
       div_obj.style.display='block';
       q.value="Masquer les réponses";
      }
   }
   centerElement("div_msg_box");
 }

//========================================================================
function viewUpdateAnswer(id_answer)
{
   var url="index.php?option=com_reprodemat&task=viewUpdateAnswer&id_answer="+id_answer;
   var div_elm="div_wrap_answer_"+id_answer;
   var myreq=new Request({
							  method: 'get',
							  url:url,
							  //data:"id_fichier="+id+"&id_lignePackageDce="+id_lignePackageDce+"&nom_fichier="+nom_fichier,
							  urlEncoded:true,
							  onComplete:function(response)
                              {
                                $(div_elm).innerHTML=response;
                              },
							  onFailure:function(response)
                              {
                                $(div_elm).innerHTML=response;

                              }
							  });
		myreq.send();
}
//========================================================================
function toogleAnswer(id_answer)
{
   var div_id="div_answer_"+id_answer;
   if($(div_id).style.display=="none")
        $(div_id).style.display="block";
   else
        $(div_id).style.display="none";
}
//=============================================================
function sendMarcheQuestionAjax(id_marche)
{

   if(trim($("tarea_question").value)=="")
      {
        alert("S'il vous plaît écrivez votre question!");
		$('tarea_question').focus();
		return false;
      }
   var question=$("tarea_question").value;
   var question_type=$("question_type").options[$("question_type").selectedIndex].value;
   var form_data=$("formNewQuestion").toQueryString();

   //var data_param="question="+question+"&type=ajax&id_marche="+id_marche+"&question_type="+question_type;
   var url="index.php?option=com_reprodemat&task=sendMarcheQuestionAjax&type=ajax&id_marche="+id_marche;//&question="+question+"&id_marche="+id_marche+"&question_type="+question_type;
   var div_elm="div_msg_box";
   $(div_elm).innerHTML="<img src='../components/com_reprodemat/images/progress.gif'   /> En attente de réponse du serveur...";
   centerElement(div_elm);
   var myreq=new Request({
							  method: 'get',
							  url:url,
							  data:form_data,
							  urlEncoded:true,
							  onComplete:function(response)
                              {
                                 showQuestionsEnt(response);
                              },
							  onFailure:function(response)
                              {
                                   showQuestionsEnt(response);
                              }
							  });
		myreq.send();
}
//=============================================================
function closeNewMarcheQuestion()
{
   showQuestionsEnt();
}
//=============================================================
function newQuestion ()
{
   var id_marche=$("id_marche").value;
   var url="index.php?option=com_reprodemat&task=newQuestion&type=ajax&id_marche="+id_marche;
   var div_elm="div_msg_box";

   $(div_elm).innerHTML="<img src='../components/com_reprodemat/images/progress.gif'   /> En attente de réponse du serveur...";
   centerElement(div_elm);
   var myreq=new Request({
							  method: 'get',
							  url:url,
							  //data:"id_fichier="+id+"&id_lignePackageDce="+id_lignePackageDce+"&nom_fichier="+nom_fichier,
							  urlEncoded:true,
							  onComplete:function(response)
                              {
                                 $(div_elm).innerHTML=response;
                                 //flashMessage(msg);
                                 centerElement(div_elm);
                              },
							  onFailure:function(response)
                              {
                                $(div_elm).innerHTML=response;
                                //flashMessage(msg);
                                centerElement(div_elm);

                              }
							  });
		myreq.send();
}
//=============================================================
function showQuestionsEnt(flashMsg)
{
   if(!flashMsg)
      flashMsg="";
   var id_marche=$("id_marche").value;
   var url="index.php?option=com_reprodemat&task=showQuestionsEnt&type=ajax&id_marche="+id_marche;
   var div_elm="div_msg_box";

   $(div_elm).innerHTML="<img src='../components/com_reprodemat/images/progress.gif'   /> En attente de réponse du serveur...";
   centerElement(div_elm);
   var myreq=new Request({
							  method: 'get',
							  url:url,
							  //data:"id_fichier="+id+"&id_lignePackageDce="+id_lignePackageDce+"&nom_fichier="+nom_fichier,
							  urlEncoded:true,
							  onComplete:function(response)
                              {
                                 $(div_elm).innerHTML=response;
                                 flashMessage(flashMsg);
                                 centerElement(div_elm);
                              },
							  onFailure:function(response)
                              {
                                $(div_elm).innerHTML=response;
                                flashMessage(flashMsg);
                                centerElement(div_elm);

                              }
							  });
		myreq.send();

}
//===============================================================
function flashMessage(msg)
{
    if($("div_flash"))
        $("div_flash").innerHTML=msg;
}
//===============================================================
/*
 * send Marche Question
 */
function sendMarcheQuestion()
{
	//get question
	var txtQuest=$('txtQuest').value;
	//get question type
	//var questType=$('questTypes').options[$('questTypes').selectedIndex].text;

	//validate question & display error msg
	if(txtQuest.length==0){
		alert("S'il vous plaît écrivez votre question!");
		$('txtQuest').focus();
		return false;
	}
	//submit question (form)
	$('frmMarcheQuestion').submit();
	//alert(txtQuest+" , "+questType);
	return false;
}
