/**
* +---------------------------------------------------+
* | Name :		~/main/js/ajax.php
* | Begin :		12/06/2009
* | Last :		19/06/2009
* | User :		Jérémy Bazin
* | Project :	Site TFE Group Inc.
* | License :	GPL v2.0
* +---------------------------------------------------+
**/

// Création de l'objet XMLHttp pour le traitement ajax
function getHTTPObject(){
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try{
			xmlhttp = new XMLHttpRequest();
		}catch (e){
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

var xhr = getHTTPObject();
var xhr2 = getHTTPObject();

function ajax_add_comment(id)
{
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			if ( xhr.responseText == 'true' ) {
				show_dialog('add_ok', true, id);
			}
			else {
				show_dialog('add_pok');
			}
		}
	}

	var user_id = document.getElementById('user_id' + id).value;
	var comment = document.getElementById('comment' + id).value;
	
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=add_comment&com=" + comment + "&user_id=" + user_id + "&art_id=" + id);
}

function ajax_del_comment(id, boite, id_art) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			if ( xhr.responseText == 'true' ) {
				show_dialog('del_ok', true, id_art);
			}
			else {
				show_dialog('del_pok');
				$("#" + boite).dialog('close');
			}
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=del_comment&id=" + id);
}

function ajax_edit_comment(id, art_id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			if ( xhr.responseText == 'true' ) {
				show_dialog('edit_ok', true, art_id);
			}
			else {
				show_dialog('edit_pok');
			}
		}
	}

	var cont = document.getElementById('edit_' + id).value;

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=edit_comment&id=" + id + "&com=" + cont);
}

function ajax_get_response(rep_id, art_id, date, u_id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById('comment' + art_id).innerHTML = '[quote=' + rep_id + ']' + xhr.responseText + '[/quote]';
			
			var selectedEffect = 'blind';
			var options = {};
			
			if ( document.getElementById('add' + art_id).style.display == 'block') {
				$("#add"+art_id).hide(selectedEffect,options,400);
			}
			else {
				$("#add"+art_id).show(selectedEffect,options,400);
			}
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=get_response&id=" + rep_id);
}

function ajax_send_mail(boite, art_id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			if ( xhr.responseText == 'vrai' ) {
				$('#' + boite).dialog('close');
			}
		}
	}

	var dest = document.getElementById('dest').value;
	var expe = document.getElementById('expe').value;
	var obj = document.getElementById('objet').value;
	var msg = document.getElementById('msg').value;

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=send_mail&art_id=" + art_id + "&dest=" + dest + "&expe=" + expe + "&objet=" + obj + "&msg=" + msg);
}

function ajax_send_condition(boite, art_id) {
	var chx1 = document.getElementById('chx1').checked;
	var chx2 = document.getElementById('chx2').checked;
	var chx3 = document.getElementById('chx3').checked;
	var chx4 = document.getElementById('chx4').checked;
	var chx5 = document.getElementById('chx5').checked;
	var txt = document.getElementById('txt').value;
	
	xhr2.open("POST",'ajax.php',true);
	xhr2.onreadystatechange = function(){
		if ( xhr2.readyState == 4 && xhr2.status == 200 ) {
			if ( xhr2.responseText == 'vrai' ) {
				document.forms['forms_1'].reset();
				$('#' + boite).dialog('close');
				show_dialog('send');
				
			}
			else if ( xhr2.responseText == 'empty' ) {
				show_dialog('empty');
			}
			else {
				show_dialog('error');
			}
		}
	}

	xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr2.send("mode=send_condition&chx1=" + chx1 + "&chx2=" + chx2 + "&chx3=" + chx3 + "&chx4=" + chx4 + "&chx5=" + chx5 + "&art_id=" + art_id + "&txt=" + txt);
}

function ajax_show_prod(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById('div_7_prod').innerHTML = xhr.responseText;
			$('#gallery a').lightBox();
		}
		else {
			document.getElementById('div_7_prod').innerHTML = '<img src="./main/js/images/lightbox-ico-loading.gif" style="margin-top: 270px; margin-left: 140px;" alt="" \/>';
		}
	}
	
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=show_prod&id=" + id);
}

// Méthode : ajout d'un article au panier
function ajoutPanier(prod_id, prod_min){
	var quantite = '';
	var qte = 0;
	
	quantite = document.getElementById('qte' + prod_id).value;
	var t = quantite % prod_min;

	if ( quantite < prod_min ){
		alert(lang['qte_min'] + prod_min);
	}
	else if ( t != 0 ) {
		alert('La quantité doit être un multiple du minimum de vente.\nVotre quantité va être ajustée au mieux pour répondre à cette règle.');

		var temp = quantite / prod_min;
		temp = Math.round(temp);
		qte = temp * prod_min;
		
		document.getElementById('qte' + prod_id).value = qte;
	}else {
		xhr.open("POST",'ajax.php', true);
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				updatePanier(prod_id, xhr.responseText);
			}
		}
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("mode=ajout_panier&prod_id=" + prod_id + "&prod_qte=" + quantite);
	}
}

// Méthode : modif d'un article du panier
function modifPanier(prod_id, prod_min){
	var quantite = '';
	var qte = 0;
	
	quantite = document.getElementById('quantite' + prod_id).value;
	var t = quantite % prod_min;

	if ( quantite < prod_min ){
		alert(lang['qte_min'] + prod_min);
	}
	else if ( t != 0 ) {
		alert('La quantité doit être un multiple du minimum de vente.\nVotre quantité va être ajustée au mieux pour répondre à cette règle.');

		var temp = quantite / prod_min;
		temp = Math.round(temp);
		qte = temp * prod_min;
		
		document.getElementById('quantite' + prod_id).value = qte;
	}else{
		xhr.open("POST",'ajax.php',true);
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				window.location.reload();
			}
		}
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("mode=modif_panier&prod_id=" + prod_id + "&prod_qte=" + quantite);
	}
}

// Méthode : suppression d'un article du panier
function supprPanier(prod_id){
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			updatePanier(prod_id, xhr.responseText);
			document.getElementById('line' + prod_id).style.display = 'none';
		}
	}
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=suppr_panier&prod_id=" + prod_id);
}

// Méthode : suppression du panier
function supprAll(){
	var prompt = confirm('Etes-vous sûr de vouloir supprimer l\'intégralité de votre panier ?');
	
	if ( prompt ) {
		xhr.open("POST",'ajax.php',true);
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				alert('Votre panier a été supprimer dans son intégralité');
				window.location.reload();
			}
		}
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("mode=suppr_all");
	}
}

// Méthode : mise à jour des infos du panier
function updatePanier(id, response){
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById('cont_panier').innerHTML = xhr.responseText + '&euro;';
			updateLinePanier();
		}
	}
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=update_panier");
}

function updateLinePanier() {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById('line_prod').innerHTML = xhr.responseText;
		}
	}
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=update_line");
}

// Méthode : mise à jour des infos du panier
function updateQte(id){
	var xhr2 = getHTTPObject();
	xhr2.open("POST",'ajax.php',true);
	xhr2.onreadystatechange = function(){
		if( xhr2.readyState == 4 && xhr2.status == 200 ){
			document.getElementById('qte').innerHTML = sprintf(lang['prod_exist'], xhr2.responseText);
		}
	}
	xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr2.send("mode=update_qte&prod_id=" + id);
}

