//FUNCIONS DEL MENU
enviarnewsletter = function (mid,id) {
	FCKeditorAPI = null;
	__FCKeditorNS = null;
	var result = function () {
		inicialitzabutlleti(id);
	}
	new Ajax.Updater( 'Cos', 'mod/butlleti/ajax.editar.php', { method: 'get' , parameters: 'mid='+mid+'&id='+id, onComplete: result }); 
};

arxiunewsletter = function (mid) {
	FCKeditorAPI = null;
	__FCKeditorNS = null;
	new Ajax.Updater( 'Cos', 'mod/butlleti/ajax.arxiu.php', { method: 'get' , parameters: 'mid='+mid }); 
};

mailsnewsletter = function (mid) {
	FCKeditorAPI = null;
	__FCKeditorNS = null;
	new Ajax.Updater( 'Cos', 'mod/butlleti/ajax.llistat.php', { method: 'get' , parameters: 'mid='+mid }); 
};


//EDITAR I GENERAR BUTLLETINS
function addseccions(id,quantitat) {
	ordre=parseInt(document.omwform.numseccs.value);
	var result = function (obj) {
		if (obj.responseText!="") {
			var novadiv = document.createElement("div");
			novadiv.setAttribute("id","gsec-"+ordre);
			
			var ultsec = document.getElementById("ultsec");
			document.getElementById("seccions").insertBefore(novadiv,ultsec);
			
			$("gsec-"+ordre).innerHTML=obj.responseText;
			
			if (!id) document.omwform.numseccs.value=parseInt(document.omwform.numseccs.value)+parseInt(quantitat);
		}
		
	}	
	var url = 'mod/butlleti/ajaxinc.seccio.php';
	new Ajax.Request( url, { method: 'post', parameters: 'id='+id+'&ordre='+ordre+'&quant='+quantitat, onComplete: result });
}

function inicialitzabutlleti(id) {
	if (id) {
		addseccions(id,0);
	} else {
		addseccions(0,1); 
	}
}

function guardabutlleti(id,mid,enviarono) {
		var result = function (obj) {
			if (obj.responseText!="") {
				ar=obj.responseText.split("|");
				alert(ar[2]);
				if (parseInt(ar[0])) {
					enviarnewsletter(mid,ar[1]);
				}
			}
		}	
		txtform='id='+id;
		txtform+='&mid='+encodeURIComponent(document.omwform.mid.value);
		txtform+='&numbutlleti='+encodeURIComponent(document.omwform.numbutlleti.value);
		txtform+='&numseccs='+encodeURIComponent(document.omwform.numseccs.value);
		txtform+='&subject='+encodeURIComponent(document.omwform.subject.value);
		if (enviarono) { enviarono=1; } else { enviarono=0; }
		txtform+='&enviarono='+enviarono;
		
		var z;
		fvar=parseInt(document.omwform.numseccs.value);		
		for (z=0;z<fvar;z++) {
			txtform+='&titol'+z+'='+encodeURIComponent(document.getElementById("titolbut"+z).value);
			txtform+='&img'+z+'='+encodeURIComponent(document.getElementById("codimg"+z).value);
			txtform+='&cos'+z+'='+encode(FCKeditorAPI.GetInstance('cosbut'+z).GetXHTML());
		}
		
		//alert(txtform);
		var url = 'mod/butlleti/ajaxinc.guardabutlleti.php';
		new Ajax.Request( url, { method: 'post', parameters: txtform, onComplete: result });
	}

//ENVIAR BUTLLETÍ PER CORREU
/*function sendbutlleti(mid,num) {
	var result = function (obj) {
		if (obj.responseText!="") {
			alert(obj.responseText);		
		}
		arxiunewsletter(mid);
	}	
	
	var extra="";
	for(i=0;i<num;i++) extra+="&coshtml_"+i+"="+encode(FCKeditorAPI.GetInstance('txt_'+i).GetXHTML());
	var txtform=Form.serialize($('omwform'))+extra+"&mid="+mid;	
	
	var url = 'mod/butlleti/ajaxinc.enviarbutlleti.php';
	new Ajax.Request( url, { method: 'post', parameters: txtform, onComplete: result });
}*/

//FUNCIONS SECCIÓ LLISTAT D'EMAILS
function modificamail(mid,eml,estat) {
	var result = function (obj) {
		if (obj.responseText!="") {
			alert(obj.responseText);		
		} 
		mailsnewsletter(mid);
	}
	var url = 'mod/butlleti/ajaxinc.modificamail.php';
	new Ajax.Request( url, { method: 'post', parameters: 'estat='+estat+'&eml='+eml, onComplete: result });
}


//FUNCIONS QUE CRIDA L'USUARI FINAL
function addmail(idmodul,eml,lang) {
	var result = function (obj) {
		if (obj.responseText!="") {
			alert(obj.responseText);		
		}
	}	
	var url = 'omw/mod/butlleti/ajaxinc.afegeixmail.php';
	new Ajax.Request( url, { method: 'post', parameters: 'idmod='+idmodul+'&eml='+eml+'&lang='+lang, onComplete: result });
}
