var ajaxRequest = null;
var id = null;
var win = null;
var helpWin = null;

function isEmailValid(email) {
	regEx = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9]/;
	return regEx.test(email);
}

function checkSel(id)
{
	var elts = document.forms[0].elements;
	var elt;
	for (var i = 0; i < elts.length; i++)
	{
		elt = elts[i];
		if (elt.name == id && elt.checked)
		{
			return true;
		}
	}
	
	Dialog.alert("Veuillez s&eacute;lectionner au moins une ligne !", {windowParameters: {className: "alphacube", width:240}, okLabel: "Ok"});
	return false;
}

function delSel(id, options) {
	if (!checkSel(id)) return;

	if (!options.confirm) {
		options.confirm = "Etes-vous s&ucirc;rs de vouloir supprimer cette s&eacute;lection ?";
	}
	zRequest(options.link, options);
}

function zToggle(id, params)
{
	var divElt = document.getElementById(id);
	if (!divElt) { return; }

	divElt.style.display = divElt.style.display == "inline" ? "none" : "inline";
	var img = eval("document.getElementById(\"img" + id + "\")");
	if (img) {
		expand = (params && params.expand) ? params.expand : "plus.gif";
		collapse = (params && params.collapse) ? params.collapse : "minus.gif";
		basepath = (params && params.basepath) ? params.basepath : "/images/";
		img.src = basepath + (divElt.style.display == "inline" ? collapse : expand);
	}
}

function assertFilled() {
	for (var i = 0; i < arguments.length; i++) {if (document.getElementById(arguments[i]).value == "") { return arguments[i]; } }
	return null;
}

function showTab(toShow, toHide1, toHide2)
{
	document.getElementById(arguments[0]).style.display = "inline";
	for (var i = 1; i < arguments.length; i++) 	document.getElementById(arguments[i]).style.display = "none";
}

function showMessage(msg, progress) {
	Element.update("msg", "<span class=\"msg" + (progress ? "progress" : "") + "\">" + msg + (progress ? " ..." : "") + "</span>");
}

function notImplemented(title) {
	Dialog.alert("D&eacute;sol&eacute;, cette fonctionnalit&eacute; n'est pas impl&eacute;ment&eacute;e.", {windowParameters: {className: "alphacube", width:240}, okLabel: "Ok"});
}

function processLinks(id) {
	var links = document.getElementsByTagName("a");
	for (i = 0; i < links.length; i++) {
		a = links.item(i);
		if (a.href.indexOf("index.php") != -1) {
			//a.href = "javascript:openContent(\"" + a.href + "\")";
			a.href += "&zid=" + id;
		}
	}
}

var checked = false;
function selAll(id) {
	checked = !checked;
	var cboxs = document.forms[0].elements;
	for (i = 0; i < cboxs.length; i++)
	{
		if (cboxs[i].name == id)
		{
			cboxs[i].checked = checked;
		}
	}
}

function openContent(url, params) {
	Element.show("main");
	showMessage(params && params.message ? params.message : "Chargement en cours", true);
	$("main").innerHTML = "";

	//alert("params = " + (params ? params.parameters : ""));
	ajaxRequest = new Ajax.Updater("main", url, {
								method : 'post',
								parameters : params ? params.parameters : "",
								evalScripts: true,
								onSuccess : function(resp) {showMessage(""); if (params && params.success) {openContent(buildzCore(params.success), params);} },
								on401 : function(resp) {showMessage("Echec : autorisation refus&eacute;e !")},
								on403 : function(resp) {showMessage("Echec : " + resp.responseText)},
								on409 : function(resp) {showMessage("Echec : " + resp.responseText)},
								on412 : function(resp) {showMessage("Echec : " + resp.responseText)},
								onFailure : function(resp) {showMessage("Echec :" + resp.statusText)}
							});
}

function zMail() {
	notImplemented("wMail");
}

function zLogin() {
	ajaxRequest = new Ajax.Updater("zManager", "/ajax/login.php", {
								method : 'post',
								evalScripts: true,
								parameters : Form.serialize(document.forms[0]),
								onSuccess : function(req) {showMessage("");},
								onLoaded : function(resp) {showMessage("authentification en cours", true);},
								onInteractive : function(resp) {showMessage("r&eacute;ception en cours", true);},
								onFailure : function(resp) {showMessage("<img src='/icons/silky/exclamation.png' text=middle/>Echec d'authentification");}
							});

}

function zLogout() {
	Element.toggle("main");
	ajaxRequest = new Ajax.Updater("zManager", "/ajax/logout.php", {
								method : 'post',
								evalScripts: true,
								onComplete: function() {periodicalUpdate.stop();},
								onSuccess : function(req) {if (periodicalUpdate) periodicalUpdate.stop();},
								onFailure : function() {showMessage("logout failed !");if (periodicalUpdate) periodicalUpdate.stop();}
							});
	
}

function buildzCore(url) {
	url = (url.indexOf("/ajax") == -1 ? "/ajax/zCore/" : "") + url;
	url += url.indexOf(".php") == -1 ? ".php" : "";

	return url;
}

function zRequest(url, params) {

	Element.show("main");
	if (!params || !params.noz) {
		url = buildzCore(url);
	}

	var confirm = null;

	if (params) {
		if (document.forms && document.forms[0]) {
			if (params.parameters) {
				params.parameters += "&" + Form.serialize(document.forms[0]);
			} else {
				params.parameters = Form.serialize(document.forms[0]);
			}
		}
		confirm = params.confirm;
	}

	if (confirm) {
		Dialog.confirm(confirm, {
				windowParameters: {className: "alphacube",width:300}, 
				okLabel: "Oui", 
				cancelLabel: "Annuler", 
				buttonClass: "myButtonClass", 
				id: "confId", 
				cancel:function(win) {}, 
				ok:function(win) {openContent(url, params); return true;} });
	}
	else {
		if (params && params.nw) {
			win = new Window(params.id ? params.id : "nw", 
			{url: url+"?"+params.parameters, className: "alphacube", minimizable: false, maximizable: false, onclose:function() {if (params.success) openContent(buildzCore(params.success))},
			title: (params.title ? params.title : ""),resizable: false,  
			showEffectOptions: {duration:1}, width:params.width ? params.width : 350, height:params.height ? params.height : 200, zIndex: 100, showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:false});
			win.showCenter(params.m ? true : false);
			win.setDestroyOnClose();
		} else if (params && params.pw) {
			options = "toolbar=no,status=no,resizable=no,scrollbars=yes,height=";
			options += params.height ? params.height : "200";
			options += ",width=";
			options += params.width ? params.width : "350";
			win = window.open(url+"?"+params.parameters, params.id ? params.id : "pw", options);
			win.focus();
		} else {
			openContent(url, params);
		}
	}
}

function zExecute(url) {
	Element.show("main");
	url = buildzCore(url);

	ajaxRequest = new Ajax.Request(url, {
								method : 'post',
								parameters : Form.serialize(document.forms[0]),
								onSuccess : function(resp) {showMessage("")},
								onLoaded : function(resp) {showMessage("envoi en cours", true)},
								onInteractive : function(resp) {showMessage("r&eacute;ception en cours", true)},
								on401 : function(resp) {showMessage("Echec : autorisation refus&eacute;e !")},
								on403 : function(resp) {showMessage("Echec : " + resp.responseText)},
								on409 : function(resp) {showMessage("Echec : " + resp.responseText)},
								on412 : function(resp) {showMessage("Echec : " + resp.responseText)},
								onFailure : function(resp) {showMessage("Echec du traitement : " + resp.responseText)}
							});
}

function zMailing(s) {
	document.forms[0].method = "post";
	document.forms[0].target = "_blank";
	document.forms[0].action = buildzCore("s_mailing.php?s=" + s);
	document.forms[0].submit();
}
