// JavaScript Document Creato da Cantoni Stefano

function ajax_request(div_aggiorna,script_server,anti_cache,div_wait,div_proxy,next_function){

var nc;
if (anti_cache=='true'){
script_server=script_server+'&ajax_nc='+nc;	
}
var d = AJS.getRequest(script_server)
d.addCallback(function(res_txt, req) {ajax_wait('normal',div_wait,'proxy',div_aggiorna); document.getElementById(div_aggiorna).innerHTML = res_txt; AJS.evalScriptTags(res_txt); 
 eval(next_function); div_focus(div_aggiorna);
})
d.addErrback(function(res_txt, req) {  })
ajax_wait('wait',div_wait,div_proxy);
d.sendReq()


}
//--------------------------------------------------------------------------------------------------------------------------------------

function ajax_submit_form(script_server,form,div_wait,div_proxy,next_function,in_div){

var dest_divw=dest_div;
var dest_div="formAJS"+form;
var nformAJS="nformAJS"+form;
var nformAJSval="nformAJSval"+form;

if(AJS.getElement(nformAJS)) AJS.removeElement(AJS.getElement(nformAJS));

AJS.ACN(AJS.getElement(form), AJS.INPUT({id : nformAJS , name: "nformAJS" ,value: form ,type:"hidden"}));
AJS.ACN(AJS.getElement(form), AJS.INPUT({id : nformAJSval , name: "nformAJSval",value: "1",type:"hidden"}));

if(AJS.getElement(dest_div)) AJS.removeElement(AJS.getElement(dest_div));
AJS.ACN(AJS.getBody(), AJS.DIV({id: dest_div, style:"display:'none'"},''));

var form_contents = AJS.formContents(form);

AJS.removeElement(AJS.getElement(nformAJSval )); 

if (in_div){dest_div=in_div;}

var d = AJS.getRequest(script_server)
d.addCallback(function(res_txt, req) {ajax_wait('normal',div_wait,'proxy',dest_div); document.getElementById(dest_div).innerHTML = res_txt; AJS.evalScriptTags(res_txt);
 eval(next_function);  div_focus(in_div);
})
d.addErrback(function(res_txt, req) {  })
ajax_wait('wait',div_wait,div_proxy);
d.sendReq(form_contents)

}
//--------------------------------------------------------------------------------------------------------------------------------------

function ajax_graybox(caption,h,w,script_server,div_proxy,hide){
if(hide)GB_hide();
GB_showCenter(caption,'',w,h);
ajax_request('GB_frame',script_server,'true','GB_frame',div_proxy);
}
//--------------------------------------------------------------------------------------------------------------------------------------

function ajax_wait(stato,div,proxy,aggiornato){

if(stato=='wait'){   
eval("ajax_"+div+"=document.getElementById(div).innerHTML;");
document.getElementById(div).innerHTML=document.getElementById(proxy).innerHTML;
}

if(stato != 'wait'){  

if(div!=aggiornato){   
document.getElementById(div).innerHTML = eval('ajax_'+div);
}
}

}

//---------------------------------------------------------------------------------------------------------------------------------------
function vis_div(id,op)
{
document.getElementById(id).style.display = ''+op;
}
//---------------------------------------------------------------------------------------------------------------------------------------
function form_hide(formName,totStep)
{
var form;
for(i=2; i <= totStep; i++) {
form=formName+'_'+i+'_'+totStep;
AJS.hideElement(AJS.getElement(form));
}
}
//---------------------------------------------------------------------------------------------------------------------------------------
function div_focus(div)
{
if(AJS.getElement(div))	{
var pos_div = AJS.absolutePosition(AJS.$(div));
var pos_scroll=AJS.getScrollTop();
//var w=AJS.getWindowSize();
var margin=50;
//var x=pos_scroll-w.h;
if (pos_div.y<pos_scroll){
	window.scrollTo(0,(pos_div.y-margin));
	} 

}
}