/*
 * Created on 17-nov-2005
 *
 * eC3MS - Management System per la Comunicazione, 
 *         Collaborazione e Condivisione Aziendale
 * 
 * 2005-2006, (c) Eventi progetti Speciali
 * 
 * Nome: libreria.js
 * Funzione: Libreria javascript con funzioni generiche
 */

// CONSOLE //
function echo(message) {
	var ec3ms_console=AJS.$('ec3ms_console');
	ec3ms_console.style.display ='';
	AJS.setHTML(ec3ms_console,ec3ms_console.innerHTML+message);
}
// ONLOAD //
onloadfunctions=[];
window.onload = function() {
	for (var i=0; i<onloadfunctions.length; i++) onloadfunctions[i]();
}
function expand(divid){
	eval(document.getElementById(divid)).style.display = '';
}
function collapse(divid){
	eval(document.getElementById(divid)).style.display = 'none';
}
function expand_collapse(divid,saveval){
	stato=document.getElementById(divid).style.display;
	if(stato == 'none')
		eval(document.getElementById(divid)).style.display = '';
	else
		eval(document.getElementById(divid)).style.display = 'none';
	if (saveval) {
		if (document.getElementById(divid).style.display=='none') setSessionVar('exp_col_'+divid,'none','si');
		else setSessionVar('exp_col_'+divid,'block','si');
	}
	return (document.getElementById(divid).style.display!='none');
}

function MM_findObj(n, d, f) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(f) x=d.forms[f][n]; if(!x) x=d[n];
  if(!x&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
	return false;
  }
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve essere un indirizzo e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' e\' obbligatorio.\n'; }
  } if (errors) alert('ATTENZIONE!!\n'+errors);
  document.MM_returnValue = (errors == '');
}
function MM_goToURL(ind) { //v3.0
  document.MM_returnValue = false;
  document.location=ind;
}
function scroll_page(divid){
var divid;
var objDiv = document.getElementById(divid);
//alert(objDiv.scrollHeight);
//objDiv.scrollTop = objDiv.scrollHeight;
//objDiv=objDiv+300;
//allert(objDiv);
window.scrollBy(0,4000);
}
/*function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}*/
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function accendi_spegni(divarrayspegni,selObj,spento,acceso,indirectObj){
	var divarrayspegni,selObjx,spento,acceso;        
	var ndivcollapse=divarrayspegni.length;	
	/*if (indirectObj)	
		selObjx=document.getElementById(selObj);
	else selObjx=selObj;*/
	for(var x=1; x<=ndivcollapse ; x++){		
		eval(document.getElementById(divarrayspegni[x-1])).className=spento;
	}
	eval(document.getElementById(selObj)).className=acceso;
	/*selObj.className=acceso;*/
}




// GALLERY LIBRARY //
function add_gallery_image(img,us,campo) {
	var id, foo, el,img_el ;
	foo=campo.split('img');
	id=foo[1];
	el=document.getElementById('img'+id+'_upload');
	el.style.display = 'none';
	el=document.getElementById('img'+id+'_preview');
	el.style.display = 'block';
	if (typeof(previewsizex) == "undefined") previewsizex=100;
	if (typeof(previewsizey) == "undefined") previewsizey=150;
	for (i=0; i<el.childNodes.length; i++) {
		if (el.childNodes[i].nodeName=="IMG") {
			img_el=el.childNodes[i];
			img_el.src='/tools/riduci.php?filename=ftptmp/'+us+'_'+img+'&previewsizex='+previewsizex+'&previewsizey='+previewsizey+'&addwhite=eeeeee';
		}
	}
}
function stampa(){
	if (!window.print){
	alert("Browser non supportato!")
	return
	}
	window.print()
}

// DOCUMENT LIBRARY //
function file_uploaded(fi,us,fo,tot) { // sintassi: form[__campo][__javascript]
	var fo_info = fo.split("__");
	if (!fo_info[1]) fo_info[1]='file';
	var el; el=MM_findObj(fo_info[1],document,fo_info[0]);
	var ricarica;
	el.value=us+"_"+fi;
	var el; el=MM_findObj('nfile',document,fo_info[0]);
	if (el)
		el.value=tot;
	if (fo_info[2]) eval(fo_info[2]+'(\''+fi+'\',\''+us+'\',\''+fo_info[1]+'\');');
	ricarica=MM_findObj('autosubmit',document,fo_info[0]);
	if((ricarica) && (ricarica.value == 'si')){
		document.forms[fo].submit();
	}
}
function download_actions(act) {
	//if(act == 'cancel')
		window.close();
	//else
		//expand('chiudi');
}

function documento_ricarica(id_item,filenum,versione) {
	window.setTimeout("testload('"+id_item+"','"+filenum+"','"+versione+"')", 1000);
}
function testload(id_item,filenum,versione) {
	AJS.BASE_URL= "/";
	var d= AJS.getRequest("tools/filexist.php?id_item="+id_item+"&filenum="+filenum+'&versione='+versione,null,"get" );
	d.addCallback(tl_callback);
	d.sendReq();
}
function tl_callback(res_txt,req) {
	var resutls=res_txt.split('.');
	if ((resutls[0]!="0") && (resutls[1].substring(0,4)=="ite_")) {
		var d= AJS.getRequest("index.php?page=ajax_documento_file_table&id_item="+resutls[1],null,"get" );
		var elm=AJS.$('documenti_file_table');
		d.addCallback(function (res_txt,req){AJS.setHTML(elm,res_txt)} );
		d.addErrback(function (res_txt,req){AJS.setHTML(elm,"Impossibile effettuare l'operazione richiesta.<br />")} );
		d.sendReq();
	} else 
		window.setTimeout("testload('"+resutls[1]+"','"+resutls[2]+"','"+resutls[3]+"')", 1000);
}
// AJAX
openOnce=[];
function prendiElementoDaId(id_elemento,extension,parametri,openonceId){
	if ((openonceId==undefined) || (openOnce[openonceId]==undefined)) {
		if (openonceId) openOnce[openonceId]=true;
		var elemento,id_elemento,extension,parametri;
		var elm=AJS.$(id_elemento);
		AJS.BASE_URL= "/";
		var d= AJS.getRequest("index.php?page="+extension+'&'+parametri,null,"get" );
		d.addCallback(function (res_txt,req){AJS.setHTML(elm,res_txt);
														 if(Lightbox){
															 Lightbox.prototype.updateImageList()}} );
		d.addErrback(function (res_txt,req){AJS.setHTML(elm,"Impossibile effettuare l'operazione richiesta.<br />")} );
		var loadingElm= AJS.$bytc('div','ajaxLoading',elm);
		if(loadingElm[0])
			AJS.setHTML(loadingElm[0],'<img src="../squelettes/immagini/loading.gif" />');
		else
			AJS.setHTML(elm,'<img src="../squelettes/immagini/loading.gif" />');
		d.sendReq();
	}
}
function prendiElementoDaIdNoL(id_elemento,extension,parametri,openonceId){
	if ((openonceId==undefined) || (openOnce[openonceId]==undefined)) {
		if (openonceId) openOnce[openonceId]=true;
		var elemento,id_elemento,extension,parametri;
		var elm=AJS.$(id_elemento);
		AJS.BASE_URL= "/";
		var d= AJS.getRequest("index.php?page="+extension+'&'+parametri,null,"get" );
		d.addCallback(function (res_txt,req){AJS.setHTML(elm,res_txt)} );
		d.addErrback(function (res_txt,req){AJS.setHTML(elm,"Impossibile effettuare l'operazione richiesta.<br />")} );
		var loadingElm= AJS.$bytc('div','ajaxLoading',elm);
		if(loadingElm[0])
			AJS.setHTML(loadingElm[0],' ');
		else
			AJS.setHTML(elm,' ');
		d.sendReq();
	}
}
function formSendAndUpdate(fo,pa,id_elemento){
	var form_contents = AJS.formContents(fo);
	var elm=AJS.$(id_elemento);
	AJS.BASE_URL= "/";
	var d= AJS.getRequest(pa,null,"POST" );
	d.addCallback(function (res_txt,req){AJS.setHTML(elm,res_txt)});
	d.addErrback(function (res_txt,req){AJS.setHTML(elm,"Impossibile effettuare l'operazione richiesta.<br />")} );
	AJS.setHTML(elm,'<img src="../ec3msimg/loading.gif" />');
	d.sendReq(form_contents);
}
function accendi_inlista(nome_lista,elemento,tagName,classStandard,classEvidenziata){
	var nome_lista,elemento,tagName,classStandard,classEvidenziata,selezionato;
	if(!window.UtimoSelezionato)
		window.UtimoSelezionato=new Array();
	if(window.UtimoSelezionato['nome_lista'])
		window.UtimoSelezionato['nome_lista'].className=window.UtimoSelezionato['nome_lista'].className.replace(classEvidenziata,classStandard);
	selezionato=AJS.getParentBytc(elemento, tagName, null);
	window.UtimoSelezionato['nome_lista']=selezionato;
	selezionato.className=selezionato.className.replace(classStandard,classEvidenziata);
}
function reload_new_messages(msg) {
	window.alert(msg);
	prendiElementoDaId('area_nuovi_messaggi','ajax_nuovi_messaggi','');
}
