/**
* +---------------------------------------------------+
* | Name :		~/main/js/fonctions.php
* | Begin :		12/06/2009
* | Last :		28/07/2009
* | User :		Jérémy Bazin
* | Project :	Site TFE Group Inc.
* | License :	GPL v2.0
* +---------------------------------------------------+
**/

/* Fonction de remplacement de l'attribut html "target" pour les liens externes */
function lien_externe(){
	var liens = document.getElementsByTagName('a');
	for ( var i = 0;  i < liens.length; ++i ){
		if ( /(^| )lien_externe($| )/.test(liens[i].className) ){
			liens[i].onclick = function() {
				window.open(this.href);
				return false;
			}
		}
	}
}

/* chargement de plusieurs fonctions sur un onload */
window.onload = function(){
	lien_externe();
}

/* balise blink */
function cligno(){
	if(document.getElementById('blink').style.visibility != 'hidden'){
		document.getElementById('blink').style.visibility = 'hidden';
	}else{
		document.getElementById('blink').style.visibility = 'visible';
	}
}

function show_dialog(boite, refresh, id_art) {
	var box = $("#" + boite),
	allFields = $([]).add(box);

	$("#" + boite).dialog({
		bgiframe: false,
		modal: true,
		resizable: false,
		show: 'blind',
		hide: 'explode',
		width: 500,
		buttons: {
			Ok: function() {
				allFields.removeClass('ui-state-error');
				$(this).dialog('close');

				if ( refresh == true ){
					set_cookie_inf('refresh', id_art);
					document.location.reload();
				}
			}
		}
	});
	$("#" + boite).dialog('open');
}

function show_dialog_confirm(boite, art_id) {
	var t = true;

	$("#" + boite).dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		modal: true,
		width: 500,
		buttons: {
			'Valider': function() {
				if ( document.getElementById('dest').value == '' ) {
					t = false;
				}
				if ( document.getElementById('expe').value == '' ) {
					t = false;
				}
				if ( document.getElementById('objet').value == '' ) {
					t = false;
				}
				if ( document.getElementById('msg').value == '' ) {
					t = false;
				}

				if ( t == true ) {
					ajax_send_mail(boite, art_id);
				}
				else {
					show_dialog('form_empty');
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
		}
	});
	$('#' + boite).dialog('open');
}

function open_recap_change(id) {
	window.open('print_change.php?id=' + id, 'Impression', 'resizable=no, location=no, width=1000, height=700, menubar=no, status=no, scrollbars=yes, menubar=no');
}

function open_recap_nutrition(id) {
	window.open('print_nutrition.php?id=' + id, 'Impression', 'resizable=no, location=no, width=1000, height=700, menubar=no, status=no, scrollbars=yes, menubar=no');
}

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

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

	window.open('print_order.php?value=' + t + '&id=' + id, 'Impression', 'resizable=no, location=no, width=700, height=990, menubar=no, status=no, scrollbars=yes, menubar=no');
}

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

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

	etab = document.getElementById('lst_etab').value;

	window.open('print_order_2.php?value=' + t + '&etab=' + etab, 'Impression', 'resizable=no, location=no, width=700, height=990, menubar=no, status=no, scrollbars=yes, menubar=no');
}

function open_last_cmd() {
	window.open('print_last_order.php', 'Impression', 'resizable=no, location=no, width=700, height=990, menubar=no, status=no, scrollbars=yes, menubar=no');
}

function open_cmd(id) {
	window.open('print_order.php?id=' + id, 'Impression', 'resizable=no, location=no, width=700, height=990, menubar=no, status=no, scrollbars=yes, menubar=no');
}

function suppr_etab(id) {
	if ( confirm("Voulez-vous vraiment supprimer cet établissement ?\n\nRappel : Supprimer un établissement, entraine la suppression de tous les services et patients associés.") ) {
		window.location.href = 'supprimer-etablissements-' + id + '.html';
	}
}

function suppr_service(id) {
	if ( confirm("Voulez-vous vraiment supprimer ce service ?\n\nRappel : Supprimer un service, entraine la suppression de tous les patients associés.") ) {
		window.location.href = 'supprimer-services-' + id + '.html';
	}
}

function suppr_patient(id) {
	if ( confirm("Voulez-vous vraiment supprimer ce patient ?") ) {
		window.location.href = 'supprimer-patients-' + id + '.html';
	}
}

function _prompt() {
	return window.showModalDialog('./templates/pages/prompt.html', '' , "dialogWidth: 300px; dialogHeight: 200px; resizable: yes");
}
