//valore del mese e dell'anno, e l'oggetto dei giorni

//questa inoltre mi aggiorna un campo hidden dove c'è il parametro,cosi da nn dover ogni volta impazzire!
/**formato gg/mm/aaaa 
*/
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}


function checkArray(){
var j;
var k=0;

for (j=0;j<3;j++){
	if (array[j]!='images/spacer.gif \" height=\"100'){
		nuovo[k]=array[j];
		k++;
		}
	}
	if(nuovo[0]==undefined)
		return false;
	else {
		//array[]=nuovo;
		return true;
	}
}
function ruota(){
	//alert(i);
	var ogg=findObj('immagine');
	if(checkArray()){ //se c'è almeno un'immagine
		if(nuovo[i-1]==undefined) 
			i=1;
		ogg.src=nuovo[i-1];
		i++;
		if(i==4)i=1;
		}
	else {
			window.clearInterval();
			return false;
		}
	}

var ref
function openwin(url,x,y)
 {
 ref=window.open(url,'url','width='+x+',height='+y+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=yes,screenX=0,screenY=0,top=0,left=0');
 ref.focus();
 }


function aggiornaText(label,value){
label.value=value;
}


function aggiornaData(giorniSel,mese,anno){
	giorno=giorniSel.value;
	var data=new Date(anno,mese-1,giorno);
	//getMonth
	//giorni del mese.
	var data1=new Date(anno,mese,0);
	//parte da zero..
	var giorniCorrenti=giorniSel.length; //quanti giorni ho adesso
	var giorni=data1.getDate();//giorno del mese selezionato..
	while(giorni<giorniCorrenti){ //devo togliere giorni..
		giorniSel.options[giorniCorrenti-1]=null;//lo tolgo
		giorniCorrenti--;//adesso ho meno giorni.
	}
	while(giorni>giorniCorrenti){ //devo aggiungere giorni..
		giorniSel.options[giorniCorrenti]=new Option(giorniCorrenti+1);//lo tolgo
		giorniSel.options[giorniCorrenti].value=giorniCorrenti+1;
		giorniCorrenti++;//adesso ho meno giorni..
	}
	if(giorno >giorni){
		giorniSel.options[giorni-1].selected=true;
	}
}
//funzione che controlla una mail data
function controllaMail(mail){
	n=mail.indexOf("@");
if(n>0 && (mail.indexOf(".")>n) && mail.lenght()>7)
	return true;
return false;
}

//ritorna l'oggetto che vogliamo vedere dall'id (per vari browser
function getObj(name)
{
	var obj;
  if (document.getElementById)
  {
    	obj = document.getElementById(name);
  }
  else if (document.all)
  {
    obj = document.all[name];
  }
  else if (document.layers)
  {
   	obj = document.layers[name];
  }
	return obj;
}

function prendiColore(nome_oggetto,link){
oggetto=getObj(nome_oggetto);
var cp2=new ColorPicker('window');
cp2.select(oggetto,link);
}


function cambia_hidden(name){
	obj=this.getObj(name);
	obj.style.visibility=(obj.style.visibility=='visible')?'hidden':'visible';

}

function check(field)
{
for (i = 0; i < field.length; i++)
{
field[i].checked = true;
}
}

function decheck(field)
{
for (i = 0; i < field.length; i++)
{
field[i].checked = false;
}
}

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setAllRadio(the_form,the_element,value_check)
{
	if(typeof(the_form)!='object'){
		var obj=getObj(the_form);//adesso ho l'oggetto form
	}else{
		obj=the_form;
	}
	while(obj!=null && obj.tagName!='FORM'){//salgo in qualsiasi caso
		obj=obj.parentNode;
		}
	if(obj==null)return;
	for(var i=0;i<obj.elements.length;i++){//tutti gli input del form
		oggetto=obj.elements[i];
		if(oggetto.tagName=='INPUT'){
			if(oggetto.name.indexOf(the_element+'[')==0 && oggetto.disabled==false){//quelli disabilitati li lascio stare
				if(oggetto.value==value_check)oggetto.checked=true;
				}
			}		
	}
	return;
} // end of the 'setCheckboxes()' function

function showObject(obj, level) {
	var l = '';
	var tmp = '';
	for(var a = 0; a < level; a++)
		l += '[_]';
	for(var a in obj) {
		var type = typeof(obj[a]);
		if(type == 'object')
			tmp += showObject(obj[a], (level + 1));
		else
			tmp += l + ' [' + typeof(a) + '] ' + a + ' = value: ' +
			obj[a] + ' < ' + type + '\n';
	}
	return tmp;
}

function initEditor(testo,width,height) {
var editor=null;
obj=getObj(testo);
if(obj==null){
	return;
	}
  editor = new HTMLArea(testo);
  //editor.registerPlugin(ContextMenu);
   editor.registerPlugin(TableOperations);
config=editor.config;
config.toolbar = [
[ "fontname", "space",
  "fontsize", "space",
  "formatblock", "space",
  "copy", "cut", "paste", "space"
 ],

["justifyleft", "justifycenter", "justifyright", "justifyfull", "separator",
 "insertorderedlist", "insertunorderedlist", "outdent", "indent", "separator",
 "subscript", "superscript","separator",
 "bold", "italic", "underline",   "strikethrough"],

[
 "TO-cell-delete","TO-cell-insert-after","TO-cell-insert-before","TO-cell-merge","separator",
	"TO-cell-split","TO-col-delete","TO-col-insert-after","TO-col-insert-before","TO-col-split","separator",
	"TO-row-delete","TO-row-insert-above","TO-row-insert-under","TO-row-prop","TO-row-split",
],
[ "inserttable","TO-table-prop","TO-cell-prop","separator",
  "forecolor", "hilitecolor", "textindicator", "separator",
  "inserthorizontalrule", "createlink", "insertimage",  "htmlmode", "separator", "undo", "redo" ,
]
];
config.width = height+'px';
config.height = height+'px';
config.pageStyle =
  'body { background-color:#FFFFFF; color: black; width:100%;}';

  // comment the following two lines to see how customization works
  editor.generate();
  return false;
  }

function controllaMail(mail){
	n=mail.indexOf("@");
if(n>0 && (mail.indexOf(".")>n) && mail.lenght()>7)
	return true;
return false;
}
function checkFormCompany(){
		if(!getObj('tratt_dati').checked){
		alert('Autorizzare il trattamento dei dati personali per inviare la richiesta');
		getObj('tratt_dati').style.borderColor='red';
		return false;
	}
	obj=getObj('email');
	if((getObj('nome')).value==''){
		alert('Nome non inserito');
		return false;
	}
	if((getObj('cognome')).value==''){
		alert('Cognome non inserito');
		return false;
	}
	if(!controllaMail(obj.value)){
		alert('Mail non valida');
		return false;
		}
	return true;
}

function checkForm(){
		if(!getObj('tratt_dati').checked){
		alert('Autorizzare il trattamento dei dati personali per inviare la richiesta');
		getObj('tratt_dati').style.borderColor='red';
		return false;
	}
	obj=getObj('email');
	if((getObj('nome')).value==''){
		alert('Nome non inserito');
		return false;
	}
	if((getObj('cognome')).value==''){
		alert('Cognome non inserito');
		return false;
	}
	if((getObj('citta')).value==''){
		alert('Citta\' non inserito');
		return false;
	}
	if((getObj('indirizzo')).value==''){
		alert('Indirizzo non inserito');
		return false;
	}
	if((getObj('cap')).value==''){
		alert('CAP non inserito');
		return false;
	}


	if((getObj('provincia')).value==''){
		alert('Provincia non inserito');
		return false;
	}

	if(!controllaMail(obj.value)){
		alert('Mail non valida');
		return false;
		}
		return true;
}

function aggiungiSottomenu(id){
	parent=getObj('id_parent_new');
	bottone=getObj('id_text_new');
	parent.value=id;
	bottone.focus();
	bottone.style.border='1px solid red';
	alert('Scrivere il nome del sottomenu e premere invio o \'ok\'');
}


function login_md5(){
var md5=getObj('login_password');
var md51=md5.value;
md51=hex_md5(md51);
var md5_str=getObj('md5_password');
md5_str.value=md51;
(getObj('login_type')).value=1;
md5.value='';
submit();
return false;
}



//magari devo fare qlc de + figo per varie istanze
var clipTop = 0;
var clipWidth = 0;//larghezza
var clipBottom = 0;//l'altezza della finestrella
var topper =0;//devo riuscire a calcolarmelo
var lyrheight = 0;
var amount=1//di quanto muoverlo
var topper_ok;
var tempo,amount,theTime,theHeight,DHTML,obj,style,scroll_id,height_obj,theWidth,tempo_old;
var i=0;
var obj;
var blocco=null;
var is_scroll=true;

function configuraHW(height,width){
	
	str=typeof height;
	if(str=='string'){
		obj=getObj(height);
		if(obj==null)return;
		clipBottom=obj.offsetHeight;
		}
	if(str=='object'){
		clipBottom=height.offsetHeight;
		}
	if(str=='number'){
		clipBottom=height;
	}
	this.theHeight=clipBottom;
	str1=typeof width;
	if(str1=='string'){
		obj=getObj(width);
		clipWidth=obj.offsetWidth;
		}
	if(str1=='object'){
		clipWidth=width.offsetWidth;
		}
	if(str1=='number'){
		clipWidth=width;
	}
	this.theWidth=clipWidth;
}

var altezza;
//imposto i valori dello scroll (tempo,1px alla voltA!!)
//se numero tengo quello,altrimenti mi becco l'oggetto di riferimento
function imposta_scroll(id,height,width,tempo,pausa){
	//magari devo fare qlc de + figo per varie istanze
	this.tempo=tempo;
	this.tempo_old=tempo;
	DHTML = (document.getElementById || document.all || document.layers)//js abilitato e funzionante?
	if (!DHTML) return;
	if(!is_scroll)
		return;
	configuraHW(height,width);
	obj=getObj(id);
	if(obj==null)return;
	altezza=obj.offsetTop;
	if(obj.offsetHeight<this.clipBottom){
		obj.style.position='relative';
		this.is_scroll=false;
		return;
		}
	if (document.getElementById || document.all)
	{
		topper=obj.offsetTop;
		topper_ok=topper;
		lyrheight = obj.offsetHeight;//l'altezza del blosso completo
		obj.style.width=clipWidth;	
		var clipstring = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)';
		obj.style.clip = clipstring;//metto el rettangolo (all'inizio)
	}
	height_obj=clipBottom;//altezza dell'oggetto visualizzabile
	obj.style.height=height_obj;
	scroll_id=0;
	setTimeout('inizia_scroll()',pausa);
}

//blocca lo scroll
function ferma_scroll(time){
	if(!is_scroll)
		return;
	clearInterval(scroll_id);
	this.scroll_id=0;
}

//aspetta time prima di ricominciare a scrollare
function riprendi_scroll(time){
	if(!is_scroll)
		return;
	ferma_scroll();
	this.tempo=tempo_old;
	this.amount=1;
	setTimeout('inizia_scroll()',time);
}

//sarebbe il continua
function inizia_scroll(){
	if(!is_scroll)
		return;
	if(scroll_id<1)
		scroll_id=setInterval('scroll()',tempo);
}

//allora...
/*
lyrheight: altezza completa del bloccco (statico!!)
theHeight: altezza del blocco visibile?
clipTop: dove comincia il blocco scorrevole (da togliere l'offsetTop del blocco,altrimenti nn va)
clipBottom:dove finisce il blocco scorrevole
clipTop-clipBottom=altezza blocco
topper: posizione del div
(alzo il div,e abbasso la clip per fare lo scroll)
height_obj:mi serve per sfruttare l'overflow
*/
//adesso devo fare la barretta di scroll laterale..(bea roba)
var ratio;
var tot1=null;
function costruisciBarra(sopra,sotto,background,blocco_colore,velocita){
	if(!is_scroll)
		return;
	if(this.obj==null)return;
	//div di tutta la barra
	var top=this.altezza;
	var height=this.theHeight;
	var width=0;
	var left=clipWidth+this.obj.offsetLeft;
	var blocco_height=5;
	var blocco_top=0;
	var blocco_width=15;
	this.ratio=(height-blocco_height)/lyrheight;
	i=0;
	tot=document.createElement('div');
	tot.style.position='absolute';
	tot.style.top=top;
	tot.style.left=left;
	//freccia su
	//come faccio a capire che non supporta pienamente el dom?!?!?
/*IE TANTO PER CAMBIARE NON SUPPORTA L'EVENTO*/
	img=document.createElement('img');
	img.src=sopra;
	img.style.position='relative';
	img.style.top=0;
	img.onmousedown=function() {accelera_scroll(-velocita);} 
	img.onmouseup=function(){riprendi_scroll(500)}
	tot.appendChild(img);


	//div tra le freccie
	div=document.createElement('div');
	div.style.backgroundColor=background;
	div.style.position='relative';
	div.style.height=height;//da cambiare
	div.style.width=width;

	//blocco del drag-drop
	blocco=document.createElement('div');
	blocco.style.width=blocco_width;
	blocco.style.position='relative';
	blocco.style.height=blocco_height;
	blocco.style.top=blocco_top;
	blocco.style.backgroundColor=blocco_colore;
	this.blocco=blocco;
	div.appendChild(blocco);
	tot.appendChild(div);

//freccia giu
/*IE NON PRENDE L'ONMOUSE...*/
	img1=document.createElement('img');
	img1.src=sotto;
	img1.style.position='relative';
	img1.onmousedown=function() {accelera_scroll(velocita);} 
	img1.onmouseup=function(){riprendi_scroll(500)}

	tot.appendChild(img1);
	//aggiungo al documento
	obj1=getObj('titolo');
	obj1.appendChild(tot);
	this.tot1=tot;
	}
var i;

//aggiorna la barra col blocco
function aggiorna_barra(blocco_height){
	this.tot1.style.top=this.altezza;
	this.tot1.style.left=clipWidth+this.obj.offsetLeft;
	this.blocco.style.top=blocco_height;
}

function accelera_scroll(velocita){
	if(velocita<0){
		this.amount=-1;
		velocita=velocita*-1;
	}
	this.tempo=velocita;
	this.i++;
	ferma_scroll();
	inizia_scroll(0);
}

//tutto parametrizzato,troppo figooo!!!
//lo faccio in loop,quando finisce,ritorna dall'inizio.. magari facendolo partire da sotto e aspettando tot??
function scroll()
{
	if(obj==null)return;
	if (!DHTML) return;
	this.clipTop += amount;//abbasso la finestra
	this.clipBottom += amount;
	height_obj=parseInt(obj.style.height)+amount;
	obj.style.height=height_obj;
	topper -= amount;//alto il rettangolo
	if (clipTop < 0 || clipBottom > lyrheight+theHeight)//troppo alto,mi devo fermare e ripartire
		{
		topper=topper_ok;
		obj.style.top=topper_ok;
		clipTop=0;
		obj.style.height=theHeight;
		clipBottom=theHeight;
		riprendi_scroll(2000);//riprendo a scrollare dopo un poco
	}
	if (document.getElementById || document.all)//browser nuovi
	{
		clipstring = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)'
		obj.style.clip = clipstring;
		obj.style.top = topper + 'px';
	}
	else if (document.layers)//netscape vecchio
	{
		obj.style.clip.top = clipTop;
		obj.style.clip.bottom = clipBottom;
		obj.style.top = topper;
	}
	if(this.tot1!=null && this.blocco!=null)//se sono impostati
		aggiorna_barra(this.clipTop*ratio);
		return;
}
function attiva_scroll(id){
this.obj=getObj(id);
if(obj==null)return;
this.style=obj.style;
obj.style.position='absolute';//senza absolute non funziona!
}

