// * RIDIMENSIONAMENTO
PAGE_DIM=[[780,450],[1000,570],[1257,630]];
PAGE_CSS=[["800"],["1024"],["1280"]];
if (screen.width<810) 			PAGE_SIZE=0;
else if (screen.width<1030)		PAGE_SIZE=1;
else 							PAGE_SIZE=2;

// * TEST
//PAGE_SIZE=1;
document.write("<link rel='stylesheet' href='css/oneshot_"+PAGE_CSS[PAGE_SIZE]+".css' type='text/css'>");


// * CONFIGURAZIONE

// * VARIABILI DA INIZIALIZZARE
pag=0;


// * UTILITA'
function $(o){return document.getElementById(o)}
function debug(txt){o=$("txt_debug");o.innerHTML+="- "+txt;o.scrollTop=o.scrollHeight;o.innerHTML+="<br/>";}

// * GRAFICA

function body_init() {
	$("page_content").style.position="";
	$("table_b").style.width=PAGE_DIM[PAGE_SIZE][0]+"px";
	$("table_b").style.height=PAGE_DIM[PAGE_SIZE][1]+"px";
}
function body_onload() {

	$("table_b").style.width=PAGE_DIM[PAGE_SIZE][0]+"px";
	$("table_b").style.height=PAGE_DIM[PAGE_SIZE][1]+"px";
}

// * PAGE
function menu(n) {
	document.location="#";
	hide_page(0);hide_page(1);hide_page(2);hide_page(3);hide_page(4);
	show_page(n);
	
}
function show_page(id) {
	$("page_"+id).style.display="block";
}
function hide_page(id) {
	$("page_"+id).style.display="none";
}

// * SCROLLER
function scrollit(id,page){
if (!$(id)) return;
	step_timer=60;
	step_tot=20;
	scroll_px=1000;


	//objDiv.scrollTop = objDiv.scrollHeight
	if (!page) page=0;
	s1=$(id).scrollTop;
	//s1=$(id).scrollLeft;
	s2=page*scroll_px;
	if (s1==s2)return;
	dist=s2-s1;
	debug("*** START SCROLL "+id+" da "+s1+" a "+s2);
	//PAGINE DA 1040
	scroll_medio=dist/step_tot;
	debug ("Scroll medio: "+scroll_medio);
	 for (i=1; i<=step_tot; i++){
		perc=(i*100)/step_tot;
		perc_inv=100-perc;
		incremento=(perc_inv*scroll_medio/100)*(i);
	//	debug("incremento "+incremento);
		next= (scroll_medio*i) + incremento;
		//acc=next*perc_acc/10;
		//next+=acc;
		setTimeout("$('"+id+"').scrollTop="+(s1+next), i*step_timer);
	//	setTimeout("$('"+id+"').scrollLeft="+(s1+next), i*step_timer);
		//debug(i+" -> "+ (s1+next) +"     incremento "+incremento);
	 }
}
function scrollitfirst(){pag=0;scrollit("page_scroll", pag)}
function scrollitprev(){if (pag>0) {pag--;scrollit("page_scroll", pag)}}
function scrollitnext(){if (pag<8) {pag++;scrollit("page_scroll", pag)}}
function scrollitlast(){pag=8;scrollit("page_scroll", pag)}



function is_date(str) { 
	spz = str.split("/"); 
	dat = new Date(spz[2], spz[1]-1, spz[0]); 
	gg = dat.getDate();
	mm = dat.getMonth() +1;
	aa = dat.getFullYear();
	if (aa==parseFloat(spz[2])&&mm==parseFloat(spz[1])&&gg==parseFloat(spz[0])) {
		return dat;
	}
	return false;
} 
function is_mail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
			return 1;
	else	return 0;
}
function is_number(numero) {
	var numstr="0123456789";
	var thischar;
	var counter=0;
	for(var i=0;i<numero.length;i++) {
		thischar=numero.substring(i,i+1)
		if(numstr.indexOf(thischar) != -1)
		counter++;
	}
	if(counter==numero.length)	return(true);
	else						return(false);
}
function check_field(o) {
	if(o.getAttribute("req") && o.value=="") {
		o.setAttribute("err", "Campo obbligatorio");
	} else if (o.value && o.value.length<o.getAttribute("c_min")*1) {
		o.setAttribute("err", "Campo troppo corto.");
	} else if (o.value && o.value.length>o.getAttribute("c_max")*1) {
		o.setAttribute("err", "Troppi caratteri.");
	} else if (o.value && o.getAttribute("tipo")=="date" && !is_date(o.value)) {
		o.setAttribute("err", "Il valore immesso non e' una data.");
	} else if (o.value && o.getAttribute("tipo")=="mail" && !is_mail(o.value)) {
		o.setAttribute("err", "Testo inserito errato.");
	} else if (o.value && o.getAttribute("tipo")=="number" && !is_number(o.value)) {
		o.setAttribute("err", "Testo non numerico.");
	} else {
		if(o.getAttribute("verify") && o.value!=$("f_"+o.getAttribute("counter")+"_verify").value) {
			o.setAttribute("err", "I valori inseriti differiscono.");
		} else  {
			o.setAttribute("err", "");
		}
	}
	field_err(o);
}
function field_err(o) {
	if (o.getAttribute("err")) {
		$("field_check_"+o.getAttribute("counter")).innerHTML='<img src="img/check_ko.gif" alt="CHECK NOT PASSED" />';
		$("field_error_"+o.getAttribute("counter")).innerHTML=o.getAttribute("err");
	} else {
		$("field_check_"+o.getAttribute("counter")).innerHTML='<img src="img/check_ok.gif" alt="CHECK PASSED" />';
		$("field_error_"+o.getAttribute("counter")).innerHTML="&nbsp;";
	}
}
function prepara_submit(f) {
	var qs=[];
	c=f.getAttribute("counter_field");
	err_no=0;
	first_err=null;
	for (i=1;i<=c;i++) {
		check_field($("f_"+i));
		if ($("f_"+i).getAttribute("err")) {
			if (!first_err) first_err=$("f_"+i);
			err_no++;
		}
		qs.push(["f_"+i, $("f_"+i).value]);
	}
	if (err_no) {
		first_err.focus();
	} else {
		getURL("reg.php"
		, qs
		, null
		, 2
		,'return_reg(content)'
		, "POST");
	}
}
function return_reg(c) {
	document.forms["reg"].innerHTML='<DIV class=esito><h1>Esito della registrazione:</h1>'+c+'</DIV>';
}
