// ==================================================================================================================================//
// ---- Création d'un objet XMLHttpRequest ---- //
// ==================================================================================================================================//

function getXhr()
{
	var xhr = null; 
	
	if(window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest(); 
	}
	else if(window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else 
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	
	return xhr;
}



// ==================================================================================================================================//
// ---- Crypte le password avant de le transmettre ---- //
// ==================================================================================================================================//

function CheckLogin()
{
	var password = MD5(document.forms['login'].elements['password'].value);
	document.forms['login'].elements['password'].value = '';
	document.forms['login'].elements['md5'].value = password;
	return true;
}



// ==================================================================================================================================//
// ---- Verification du formulaire de demande de devis ---- //
// ==================================================================================================================================//

function CheckFormEnvoisDevis()
{
	//---- Variables d'erreur et de controle
	var erreur = "Liste des champs mal remplis :\n";
	var cpt = 0;
	
	//---- Nom
	var nom = document.forms["formdevis"].elements["nom"].value;
	
	if(nom == "")
	{
		erreur += "- Nom\n";
		cpt++;
	}
	
	//---- Téléphone
	var telephone = document.forms["formdevis"].elements["telephone"].value;
	
	if(telephone == "")
	{
		erreur += "- Telephone\n";
		cpt++;
	}
	
	//---- E-mail
	var email = document.forms["formdevis"].elements["email"].value;
	
	if(email == "")
	{
		erreur += "- E-mail\n";
		cpt++;
	}
	
	//---- Adresse d'enlèvement
	var adresse_1 = document.forms["formdevis"].elements["adresse_1"].value;
	
	if(adresse_1 == "")
	{
		erreur += "- Adresse d'enlevement\n";
		cpt++;
	}
	
	//---- Adresse de destination
	var adresse_2 = document.forms["formdevis"].elements["adresse_2"].value;
	
	if(adresse_2 == "")
	{
		erreur += "- Adresse de destination\n";
		cpt++;
	}
	
	//---- Validation ou message d'erreur
	if(cpt == 0)
	{ 
		var validation = confirm('Envoyer votre devis ?');
		
		if(validation == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert(erreur);
		return false;
	}	
}



// ==================================================================================================================================//
// ---- Affichage ou Non d'un emplois ou d'une annonce ---- //
// ==================================================================================================================================//

function AfficheJob(obj, id)
{
	var xhr = getXhr();
	
	if(obj.firstChild.src == 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/afficher.gif')
	{
		obj.firstChild.src = 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/masquer.gif';
		visible = 'n';
	}
	else
	{
		obj.firstChild.src = 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/afficher.gif';
		visible = 'y';
	}

	xhr.open("POST","emplois_script_modif.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("visible=" + visible + "&id=" + id);	
}

function AfficheAnnonce(obj, id)
{
	var xhr = getXhr();
	
	if(obj.firstChild.src == 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/afficher.gif')
	{
		obj.firstChild.src = 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/masquer.gif';
		visible = 'n';
	}
	else
	{
		obj.firstChild.src = 'http://127.0.0.1/TSE_LOGISTICS/site_web/images/afficher.gif';
		visible = 'y';
	}

	xhr.open("POST","annonces_script_modif.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("visible=" + visible + "&id=" + id);	
}



// ==================================================================================================================================//
// ---- Suppression d'un emplois ou d'une annonce ---- //
// ==================================================================================================================================//

function SuppJob(id)
{
	var check = window.confirm("Voulez vous effacer l'enregistrement ?");
	
	if(check == true)
	{
		var xhr = getXhr();
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById('grid_emplois').innerHTML = xhr.responseText;
			}
		}
		
		xhr.open("POST","emplois_script_supp.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("id=" + id);	
	}
}

function SuppAnnonce(id)
{
	var check = window.confirm("Voulez vous effacer l'enregistrement ?");
	
	if(check == true)
	{
		var xhr = getXhr();
	
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById('grid_annonces').innerHTML = xhr.responseText;
			}
		}
		
		xhr.open("POST","annonces_script_supp.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("id=" + id);	
	}
}



// ==================================================================================================================================//
// ---- Ouvre la fenêtre de modification du job ou d'une annonce ---- //
// ==================================================================================================================================//

function ModifJob(largeur, hauteur, options, id_emploi)
{
	var id = Math.round(Math.random() * 100);
	var gauche = (screen.width - largeur) / 2;
	var haut = (screen.height - hauteur) / 2;
	
	if(options)
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche + ', ' + options;
	}
	else
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche;
	}
	
	document.open('emplois_modif.php?id_emploi=' + id_emploi, 'modif_emploi', options);
}

function ModifAnnonce(largeur, hauteur, options, id_annonce)
{
	var id = Math.round(Math.random() * 100);
	var gauche = (screen.width - largeur) / 2;
	var haut = (screen.height - hauteur) / 2;
	
	if(options)
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche + ', ' + options;
	}
	else
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche;
	}
	
	document.open('annonces_modif.php?id_annonce=' + id_annonce, 'modif_annonce', options);
}



// ==================================================================================================================================//
// ---- Verifie la validité du formulaire ---- //
// ==================================================================================================================================//

function CheckFormJob(obj)
{
	if(obj.elements["titre"].value == '' || obj.elements["date"].value == '' || obj.elements["description"].value == '' || obj.elements["profil"].value == '')
	{
		alert("Veuillez remplir tous les champs !");
		return false;
	}
	else
	{
		return true;
	}
}

function CheckFormAnnonce(obj)
{
	if(obj.elements["d_pays"].value == '' || obj.elements["d_ville"].value == '' || obj.elements["a_pays"].value == '' || obj.elements["a_ville"].value == '' || obj.elements["poid"].value == '' || obj.elements["volume"].value == '' || obj.elements["date"].value == '')
	{
		alert("Veuillez remplir tous les champs !");
		return false;
	}
	else
	{
		return true;
	}
}

function JustNumber(obj)
{
	var chaine = obj.value;
	var resultat = chaine.match(/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/);
	
	if(!resultat)
	{
		alert("Ce champ ne peut contenir que des nombres, des points et des virgules !");
		obj.value = '';
	}
}

function CheckFormEnvoisAnnonce(obj)
{
	if(obj.elements["nom"].value == '' || obj.elements["email"].value == '')
	{
		alert("Veuillez remplir tous les champs !");
		return false;
	}
	else
	{
		return true;
	}	
}

function CheckFormEnvoisOffre(obj)
{
	if(obj.elements["nom"].value == '' || obj.elements["email"].value == '' || obj.elements["requete"].value == '')
	{
		alert("Veuillez remplir tous les champs !");
		return false;
	}
	else
	{
		return true;
	}
}



// ==================================================================================================================================//
// ---- Ouvre la fenêtre de demande d'info pour une annonce ---- //
// ==================================================================================================================================//

function SouscrireAnnonce(largeur, hauteur, options, id_annonce)
{
	var id = Math.round(Math.random() * 100);
	var gauche = (screen.width - largeur) / 2;
	var haut = (screen.height - hauteur) / 2;
	
	if(options)
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche + ', ' + options;
	}
	else
	{
		options = 'width=' + largeur + 'px, height=' + hauteur + 'px,  top=' + haut + ', left=' + gauche;
	}
	
	document.open('souscrire_annonce.php?id_annonce=' + id_annonce, 'souscrire_annonce', options);
}



// ==================================================================================================================================//
// ---- Gestion panier ---- //
// ==================================================================================================================================//

function AddRow()
{
	var nombre 		= document.getElementById('nombre').value;
	var poids 		= document.getElementById('poids').value;
	var longueur 	= document.getElementById('longueur').value;
	var largeur 	= document.getElementById('largeur').value;
	var hauteur 	= document.getElementById('hauteur').value;
	var xhr				= getXhr();
			
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('selection').innerHTML = xhr.responseText;
		}
	}

	xhr.open("POST","phpscripts/fonctions_panier.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("nombre=" + nombre + "&poids=" + poids + "&longueur=" + longueur + "&largeur=" + largeur + "&hauteur=" + hauteur + "&action=add");
}

function DelRow(indice)
{
	var xhr = getXhr();
			
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('selection').innerHTML = xhr.responseText;
		}
	}

	xhr.open("POST","phpscripts/fonctions_panier.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("indice=" + indice + "&action=del");
}