/**
 * @author:Aurélien Vairet
 * @version:1.0
 *
 * Cette librairie définit des fonctions
 * permettant de gérer l'affichage des boîtes de commandes.
 *
 * Elle est incluse dans les fichiers :
 * 	- dual_order_box.php
 * 	- bon_commande.php
 * 	- bon_serveur.php
 * */


/*
 * Cette fonction permet de rendre visibles ou invisibles,
 * et réciproquement, deux formulaires dont les id
 * sont passés en paramètres.
 * */

var emptyonfocus = 1;
var defaultValue = '';
/* defaultvalue is loaded in the html, to be sure the html element exist */

function HideForms(form1, form2){
	var My1stForm = document.getElementById(form1);
	var My2ndForm = document.getElementById(form2);
	var current_state=My1stForm.style.visibility; // form packs
	
		/*
		*	cas spécial du dual order box : on garde la valeur du nom de domain d'un formulaire sur l'autre
		*/

		var FormPackVisible = document.getElementById('PackBox').style.visibility;
		var FormEcomVisible = document.getElementById('EcomBox').style.visibility;

		if((document.getElementById('ecom_ndd_input').value!=defaultValue || document.getElementById('pack_ndd_input').value!=defaultValue) && document.getElementById('ecom_ndd_input').value != '' && document.getElementById('pack_ndd_input').value != ''){
				emptyonfocus = 0;
		}
		if(FormPackVisible=="visible" && FormEcomVisible=="hidden" && document.getElementById('pack_ndd_input').value != ''){
				document.getElementById('ecom_ndd_input').value=document.getElementById('pack_ndd_input').value;
		}else if(FormPackVisible=="hidden" && FormEcomVisible=="visible" && document.getElementById('ecom_ndd_input').value != ''){
				document.getElementById('pack_ndd_input').value=document.getElementById('ecom_ndd_input').value;
		}

	if(current_state=="hidden") {
		My1stForm.style.visibility="visible";
		My2ndForm.style.visibility="hidden";
	
		if (form1 == "ServerBox"){
			My1stField = document.getElementById("ServerTypeSelect");
			My1stField.value = "WEB SERVEUR";
			SelectForm(My1stField);
		}
	}
	else{
		My1stForm.style.visibility="hidden";
		// les deux doivent avoir la même valeur!!
		My1stField = document.getElementById("ServerTypeSelect");
		SelectForm(My1stField);
	}
}

/*
 * Cette fonction permet la sélection
 * d'un formulaire en fonction
 * d'un champ (ici un select).
 * */
function SelectForm(field) {
	if (field.options) {
		for (i=0;i<field.options.length;i++) if (field.options[i].selected==true) tempTypeServer = field.options[i].value;
	}
	if (tempTypeServer == "WEB SERVEUR")  return showForm(0);
	else if (tempTypeServer == "WEB SERVEUR PRIVE")	return showForm(2);
	else if (tempTypeServer == "VDS+")   return showForm(1);
	
	
	
}

function Select3Form(field) {
	if (field.options) {
		for (i=0;i<field.options.length;i++) if (field.options[i].selected==true) {
		if (i == 0) return showForm(1);
		else showForm(3);
		
		};
	}
}
/*
 * Cette fonction affiche un formulaire
 * et initialise la sélection des options.
 * */
function showForm(num) {
	var My1stForm = document.getElementById("ServerBox");
	var My2ndForm = document.getElementById("ServerBox2");
	var My3ndForm = document.getElementById("ServerBox3");
	var My4ndForm = document.getElementById("ServerBox4");
	
	var My1stField = document.getElementById("ServerTypeSelect");
	var My2ndField = document.getElementById("PrivateServerTypeSelect");
	var My3ndField = document.getElementById("PrivateServerPlusTypeSelect");
	var My5ndField = document.getElementById("PrivateServerPlusTypeSelect2");
	var My4ndField = document.getElementById("OsSelect");
	var My6ndField = document.getElementById("OsSelect2");
	var first_visibility = second_visibility = third_visibility = four_visibility = "hidden";
	var first_selected = second_selected = third_selected = four_selected =false;
	
	if (num==0) {
		first_visibility = "visible";
		first_selected = true;
	}
	else if (num==1) {
		third_visibility = "visible";
		third_selected = true;
	}
  else if (num==2) {		
		second_visibility = "visible";
		second_selected = true;
  }
  else if (num==3) {
		four_visibility = "visible";
		four_selected = true;
  }
	
	My1stForm.style.visibility=first_visibility;
	My2ndForm.style.visibility=second_visibility;
	My3ndForm.style.visibility=third_visibility;
	My4ndForm.style.visibility=four_visibility;
	
	
	if(My1stField.options.length > 1 )	My1stField.options[1].selected=false;
	if(My1stField.options.length > 2 )	My1stField.options[2].selected=false;
	
	if(My5ndField.options.length > 0 )	My5ndField.options[0].selected=first_selected;
	if(My3ndField.options.length > 0 )	My3ndField.options[0].selected=first_selected;
	if(My2ndField.options.length > 0 )	My2ndField.options[0].selected=first_selected;
	if(My1stField.options.length > 0 )	My1stField.options[0].selected=first_selected;
	if(My4ndField.options.length > 0 )	My4ndField.options[0].selected=third_selected;

	if (My2ndField.options.length ==3){
		if(num ==1)	My3ndField.options[2].selected=third_selected;
		if(num ==3)	My5ndField.options[2].selected=four_selected;
		if(num ==2)	My2ndField.options[1].selected=second_selected;
	}
	if(My2ndField.options.length ==2){
		if(num ==1)	My3ndField.options[1].selected=third_selected;
		if(num ==2)	My2ndField.options[1].selected=second_selected;	
		if(num ==2)	My5ndField.options[1].selected=four_selected;
		if(num ==3)	My5ndField.options[1].selected=four_selected;
	}
	if(My6ndField.options.length > 1 )	My6ndField.options[1].selected=four_selected;	
	

	}
