/******************************************************
* Licenced by : VERAN PC
* www.veranpc.fr
*
*
*
*
*
*
*
*
*
*
******************************************************
*/


/*
 *
 *appelle page php php_page en lui passant en parametre d'url 3 parametres avec comme valeur URL_Parameter_Value1/2/3. Page php qui va executer une requete ou autre dont le resultat sera inseré a la place de la balise d'ID Content_Container_ID. 
 *
 * @Content_Container_ID: id du container dans lequel le contenu de la page php va etre inseré (generalement un div ou un span)
 * @PHP_Page: la page php qui va s'afficher, qui est appellée avec pour parametre d'url parameter. Si vide, ie '', le container sera remplacé par ''.
 * @URL_Parameter_Values: la chaine de caractere passer a la page php sans le ? initial. Mettre '' si on ne ve pas passer de chaine
 *
 *
 * Warning: if this method is called on 'a' container, you can't get a some_container_id.value
 */

function PhpToCallOnChange(Content_Container_ID, PHP_Page, URL_Parameter_Values)
{ 
	if (Content_Container_ID == '' || Content_Container_ID == null)
	{
		alert ("Content_Container_ID is not specified");
		return;
	}
	
	//document.write(URL_Parameter_Values)
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	if (PHP_Page != '')
	{
		var url=PHP_Page;
		url=url+"?";
	  if (URL_Parameter_Values != '')
	  {
			url=url+URL_Parameter_Values+"&";
		}
		url=url+"sid="+Math.random();              //Adds a random number to prevent the server from using a cached file
		//document.write(url)
		xmlHttp.open("GET",url,true);
		
		xmlHttp.onreadystatechange=function()
		{
	   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	          document.getElementById(Content_Container_ID).innerHTML=xmlHttp.responseText;
	      }
	      else
	      	{ document.getElementById(Content_Container_ID).innerHTML='<p>chargement en cours ...</p>';
	    }
	   };
	
		xmlHttp.send(null)
	} //when there is no php_page, the div must be replaced with an empty content
	else
	{
		document.getElementById(Content_Container_ID).innerHTML='';
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;

try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  { //(Internet Explorer 5.5) 
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

// définit la visibility d'un container, hidden, visible
// si le container n'existe pas, on ne fais rien
function changestyle(container_id, style)
{
	if (container_id == '' || container_id == null)
	{
		alert ("container_id is not specified");
		return;
	}
	
	var doc = document.getElementById(container_id);
	//alert(doc);
	if (doc != null) //we have found the container
	{
		doc.style.visibility = style;
	}
}

// définit display style d'un container, none inline block...
function changedisplay(container_id, style)
{
	if (container_id == '' || container_id == null)
	{
		alert ("container_id is not specified");
		return;
	}
	
	var doc = document.getElementById(container_id);
	doc.style.display = style;
}

/*
 * affiche un composant de plus dans la fiche intervention
 *
 */
function add_new_component(container_name, current_container_id, component_PHP_page)
{	
	//creation dun node a partir du container_name 0
	var node0 = document.getElementById(container_name+current_container_id);
	
	//clone node0 to new node
	var new_node=node0.cloneNode(true);
	
	//set the new node id
	var next_container_id = current_container_id+1;
	new_node.id = container_name + next_container_id;
	
	//get the master node
	var components_node = document.getElementById('components');
				
	//append the new node to master node
	var result = components_node.appendChild(new_node);
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url = component_PHP_page;
	//url=url + "?";
	url=url + "&container_id=" + next_container_id;
	url=url + "&sid=" + Math.random();              //Adds a random number to prevent the server from using a cached file
	
	xmlHttp.open("GET",url,true);
	
	xmlHttp.onreadystatechange=function()
	{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
			//update new_node content
      new_node.innerHTML=xmlHttp.responseText;
      // définit les id
      set_next_container_id(current_container_id, next_container_id);
      set_previous_container_id(next_container_id, current_container_id);
      //definit l'affichage des boutons sur le current 
      changestyle('intervention_sheet_component_add'+current_container_id, 'hidden');
      changestyle('intervention_sheet_component_suppr'+current_container_id, 'visible');
      //definit l'affichage des boutons sur le next
      changestyle('intervention_sheet_component_add'+next_container_id, 'visible');
      changestyle('intervention_sheet_component_suppr'+next_container_id, 'visible');
      
   }
  };
	xmlHttp.send(null)
}
// remove component
function remove_component(container_name,current_container_id)
{
	var previous_node = document.getElementById('previous_component_id'+current_container_id);
	var next_node = document.getElementById('next_component_id'+current_container_id);
	
	//gestion de l'affichage du bouton +
	if ( next_node.value != '')
	{
		//On cherche si le suivant a un suivant 
		var next_next_node = document.getElementById('next_component_id'+next_node.value);
		if (next_next_node.value != '')
		{			
			changestyle('intervention_sheet_component_add'+next_node.value, 'hidden');
		}
		else
		{
			changestyle('intervention_sheet_component_add'+next_node.value, 'visible');
		}	
	}
	else
	{
		// if precedent afficher plus
		if (previous_node.value != '')
		{
			changestyle('intervention_sheet_component_add'+previous_node.value, 'visible');
		}
	}	
	
	
	if (next_node.value != '') {
		set_previous_container_id(next_node.value, previous_node.value);
  }
  
  if (previous_node.value != '') {
  	set_next_container_id(previous_node.value, next_node.value);
	}
	

	// si il ne reste qu'un component, on ne l'efface pas
 	if (previous_node.value != '' || next_node.value != '') {
 		remove_node('components', container_name+current_container_id);
	}
	
}
      

 /* enlever un node en fonction de son node parent (utilisé dans la fiche intervention)*/
function remove_node(parent_container_name, container_name)
{
	if (parent_container_name == '' || parent_container_name == null)
	{
		alert ("parent_container_name is not specified");
		return;
	}
	
	//creation du node parent a partir du parent_container_name 
	var parent_node = document.getElementById(parent_container_name);
	//creation du node a supprimer a partir du container_name
	var node = document.getElementById(container_name);
	//suppression du node
	parent_node.removeChild(node);
		
}

/*set next_container_id au container definit par container_id*/
function set_next_container_id (container_id, next_container_id)
{	
	var node = document.getElementById('next_component_id'+container_id);
  node.value=next_container_id;
}

/*set previous_container_id au container definit par container_id*/
function set_previous_container_id (container_id, previous_container_id)
{	
	var node = document.getElementById('previous_component_id'+container_id);
  node.value=previous_container_id;
}


//set read only
function set_read_only (container_id, valeur)
{
	if (container_id == '' || container_id == null)
	{
		alert ("container_id is not specified");
		return;
	}
	
	if (valeur=='true')
	{
		document.getElementById(container_id).setAttribute('readonly', 'readonly');
	}
	else if (valeur=='false')
	{
		document.getElementById(container_id).removeAttribute('readonly');
	}
	else {
		alert ('valeur inconnue');
		}
}


// set disabled attribute 
function set_disabled (container_id, valeur)
{
	if (container_id == '' || container_id == null)
	{
		alert ("container_id is not specified");
		return;
	}
	
	if (valeur=='true')
	{
		document.getElementById(container_id).setAttribute('disabled', 'disabled');
	}
	else if (valeur=='false')
	{
		document.getElementById(container_id).removeAttribute('disabled');
	}
	else {
		alert ('valeur inconnue');
		}
}

//change the validation_status of an intervention using the id and the new status value passed in parameter
function Interv_Change_Validation_Status(interv_id, new_validation_status)
{
	var Check = confirm("Etes Vous Sur De Vouloir Forcer Le Statut Validation_Status De Cette Intervention ?");
	if (!Check)
		return;

	if (interv_id == '' || interv_id == null)
	{
		alert ("interv_id is not specified");
		return;
	}
	if (new_validation_status == '' || new_validation_status == null)
	{	
		alert ("new_validation_status is not specified");
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	var url = "intervention_force_validation_status.php";
	url += "?interv_id=" + interv_id +"&new_v_status=" + new_validation_status;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			/*alert (xmlHttp.responseText);*/
      alert ("Statut Changé Avec Succès !");
    }
	};
	
	xmlHttp.send(null)
}

//change the validation_status of an intervention using the id and the new status value passed in parameter
function Abo_Change_Validation_Status(abo_id, new_validation_status)
{
	var Check = confirm("Etes Vous Sur De Vouloir Forcer Le Statut Validation_Status De Cet Abonnement ?");
	if (!Check)
		return;

	if (abo_id == '' || abo_id == null)
	{
		alert ("abo_id is not specified");
		return;
	}
	if (new_validation_status == ''  || new_validation_status == null)
	{	
		alert ("new_validation_status is not specified");
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	var url = "abo_force_validation_status.php";
	url += "?abo_id=" + abo_id +"&new_v_status=" + new_validation_status;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			/*alert (xmlHttp.responseText);*/
      alert ("Statut Changé Avec Succès !");
    }
	};
	
	xmlHttp.send(null);
}

/*
 * Update a container value with a string passed in param
 *
 * @Content_Container_ID, the container ID
 * @new_Content_String, the future containt of the container
 *
 */
function Update_Container_Value(Content_Container_ID, new_Content_String)
{ 
	if (Content_Container_ID == '' || Content_Container_ID == null)
	{
		alert ("Content_Container_ID is not specified");
		return;
	}
	//update
	document.getElementById(Content_Container_ID).value=new_Content_String;
}

/* 
 * Parse a specific formated string to get the reduc percent of the abo used in a bill
 * Warning: doesnt check string format!
 */
function Get_bill_abo_used_reduc(abo_used_string)
{
	if (abo_used_string == '' || abo_used_string == null)
	{
		alert ("abo_used_string is not specified");
		return;
	}
	
	var array = abo_used_string.split(";");

	return array[1];
}

/* 
 * call a php page 'mail_interv_rea.php' which will send a mail to the client saying that the intervention réalisation is finished w details
 *
 * @client_ID: l'ID du client a utiliser par la page php
 * @interv_ID: l'ID de l'intervention a utiliser par la page php
 */
function Send_ReaMail(client_ID, interv_ID)
{
	/*var Check = confirm("Etes Vous Sur De Vouloir Envoyer Un Mail De Confirmation De Réalisation Au Client ?");
	if (!Check)
		return;*/

	if (client_ID == '' || client_ID == null)
	{
		alert ("client_ID is not specified");
		return;
	}
	if (interv_ID == '' || interv_ID == null)
	{	
		alert ("interv_ID is not specified");
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	var url = "mail_interv_rea.php";
	url += "?c_id=" + client_ID +"&i_id=" + interv_ID;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			
			if (xmlHttp.responseText == '')
      	alert ("Mail Envoyé Avec Succès !");
      else
      	alert ("Erreur: " + xmlHttp.responseText);
    }
	};
	
	xmlHttp.send(null);
}
