/**
* +---------------------------------------------------+
* | 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();

function ajax_gen_pwd() {

	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById('pwd').value = xhr.responseText;
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=generate_pwd");
}

function ajax_assign_prod(prod_id, pat_id, position) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=assign_prod&id_prod=" + prod_id + "&id_pat=" + pat_id + "&pos=" + position);
}

function ajax_delete_prod(prod_id, pat_id, position) {
	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=delete_prod&id_prod=" + prod_id + "&id_pat=" + pat_id + "&pos=" + position);
}

function ajax_apply_protocol(id_pat) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			//alert(xhr.responseText);
			window.location.reload();
		}
	}

	var poids = document.getElementById('poids_' + id_pat).value;

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=apply_protocol&poids=" + poids + "&id_pat=" + id_pat);
}

function ajax_apply_protocol_albu(id_pat) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			//alert(xhr.responseText);
			window.location.reload();
		}
	}

	var albu = document.getElementById('albu_' + id_pat).value;

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=apply_protocol_albu&albu=" + albu + "&id_pat=" + id_pat);
}

function ajax_assign_prod_service(prod_id, pat_id, qte) {
	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=assign_prod_service&id_prod=" + prod_id + "&id_pat=" + pat_id + "&qte=" + qte);
}

function ajax_delete_prod_service(prod_id, id_serv) {
	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=delete_prod_service&id_prod=" + prod_id + "&id_serv=" + id_serv);
}

function ajax_calc_cmd(prod_id, qte, base, etab) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById("prod_" + etab + '_' + prod_id).value = xhr.responseText;
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=calc_cmd&id_prod=" + prod_id + "&qte=" + qte + "&base=" + base);
}

function ajax_cmd() {
	var table = document.getElementById('frm');
	var cells = table.getElementsByTagName("input");
	var t = '';

	for (var i = 0; i < cells.length; i++) {
		if ( cells[i].className == 'valeur' ) {
			if (cells[i].id.substr(0,4) == 'date') {
				t += cells[i].id + '|' + cells[i].value + ';';
			}
			else {
				t += cells[i].id.substr(5) + '|' + cells[i].value + ';';
			}
		}
	}

	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			alert(xhr.responseText);
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=cmd&value=" + t);
}

function ajax_change_cat(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById('cat').innerHTML = xhr.responseText;
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=change_cat&id=" + id);
}

function save_loca(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			alert(xhr.responseText);
		}
	}

	var demande = document.getElementById('loca').value;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=save_loca&txt=" + demande + "&id=" + id);
}

function open_desc(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById("dialog-modal").innerHTML = xhr.responseText;
			$( "#dialog-modal" ).dialog( "open" );
			document.getElementById('loading').style.display = 'none';
		}
		else {
			document.getElementById('loading').style.display = 'block';
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=getDesc&id=" + id);
}

function open_desc2(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById("dialog-modal2").innerHTML = xhr.responseText;
			$( "#dialog-modal2" ).dialog( "open" );
			document.getElementById('loading').style.display = 'none';
		}
		else {
			document.getElementById('loading').style.display = 'block';
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=getDesc&id=" + id);
}

function open_accessories(id) {
	xhr.open("POST",'ajax.php',true);
	xhr.onreadystatechange = function(){
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
			document.getElementById("accessoires").innerHTML = xhr.responseText;
			$( "#accessoires" ).dialog( "open" );
			document.getElementById('loading').style.display = 'none';
		}
		else {
			document.getElementById('loading').style.display = 'block';
		}
	}

	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mode=accessoires&id=" + id);
}

