//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;
	}
}

//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 checkAllCheck(the_form,the_element,bool){
	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
				oggetto.checked=bool;
				}
			}		
	}
	return;

}

function check(field,checkflag) {
	checks=getObj(field);
	for ( i=0; i < checks.length; i++ ) {
	        checks[i].checked = true;
      }
}


function controllaMail(mail){
	n=mail.lastIndexOf("@");
if(n>0 && (mail.lastIndexOf(".")>n) && mail.lenght()>7)
	return true;
return false;
}

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('tel')).value==''){
                alert('Telefono 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='';
return true;
}

function controlla_store(id){
	var form_store=getObj(id);
	var stato=true;
	for(var i=0;i<form_store.elements.length;i++){
		oggetto=form_store.elements[i];
		if(oggetto.name.indexOf('store[')==0 && oggetto.defaultValue>0){

			if(oggetto.value<1){//se ho disponibilita' 0
				oggetto.style.border='1px solid red';
				if(stato){
					oggetto.focus();
					oggetto.select();
				}
				stato=false;
			}else oggetto.style.border='';
		}
	}
	if(!stato) alert('La quantità non può essere inferiore a uno!');
	return stato;
}



function initEditor(name,width,height) {
	if(getObj(name)==null)return;
	var oFCKeditor = new FCKeditor(name) ;
	this.nome_combo=new Array();
	this.arrComboItems=new Array();
	if(this.user){
		//nome_combo='personalizza';
/*		nome_combo=new Array('personalizza','pippo');
		etichette_combo=new Array('Utenti','Tag');*/
		valori_combo=new Array();
		nome_combo.push('Utente');
		for(x in this.user){
			valori_combo.push(new Array(this.user[x],x));
			//	alert(x);		//Nome
			//	alert(this.select[x]);	//Tag

			}
		this.arrComboItems.push(valori_combo);
	}

	if(this.vendor){
		valori_combo=new Array();
		nome_combo.push('fornitore');
		for(x in this.vendor){
			valori_combo.push(new Array(this.vendor[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}
	if(this.address){
		valori_combo=new Array();
		nome_combo.push('indirizzo');
		for(x in this.address){
			valori_combo.push(new Array(this.address[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}

	if(this.order){
		valori_combo=new Array();
		nome_combo.push('ordine');
		for(x in this.order){
			valori_combo.push(new Array(this.order[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}
	if(this.order_detail){
		valori_combo=new Array();
		nome_combo.push('carrello');
		for(x in this.order_detail){
			valori_combo.push(new Array(this.order_detail[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}
	if(this.attribute_product){
		valori_combo=new Array();
		nome_combo.push('attributi');
		for(x in this.attribute_product){
			valori_combo.push(new Array(this.attribute_product[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}
	if(this.vari){
		valori_combo=new Array();
		nome_combo.push('vari');
		for(x in this.vari){
			valori_combo.push(new Array(this.vari[x],x));
		}
		this.arrComboItems.push(valori_combo);
	}



	//this.arrComboItems=new Array(valori_combo,new Array(new Array('prova1','pipo1'),new Array('prova2','ciccio')));
	this.baseurl=document.URL.substring(0,document.URL.lastIndexOf("/")+1);
        oFCKeditor.BasePath = "/FCKeditor/" ;
	oFCKeditor.Config[ "DefaultLanguage" ] = "it-IT";
	oFCKeditor.Height =height;
	oFCKeditor.Width =width;
	oFCKeditor.Config['CustomConfigurationsPath'] = 'myconfig.js';
	oFCKeditor.ToolbarSet = 'prova' ;
	oFCKeditor.Config['SkinPath']='skins/office2003/'
	oFCKeditor.ReplaceTextarea() ;
}

//Funzionalita' che mi sposta i select delle "misure" mettendo gli oggetti nel form di "aggiungi al carrello"
//gli oggetti sono gia impostati.
function sposta_selezionati(){
	obj=getObj('form_temporanea');
	for(var i=0;i<obj.elements.length;i++){//tutti gli input del form
		oggetto=obj.elements[i];
		if(oggetto.tagName=='INPUT' || oggetto.tagName=='SELECT'){
			if(oggetto.name.indexOf('temp_user_attribute[')==0){
				nome=oggetto.name.substr(5);
				nuovo=getObj(nome);
				nuovo.value=oggetto.value;
			}
		}
	}
}

function aggiorna_carrello(sezione){
	var oggetto=getObj('sezione');
	oggetto.value=sezione;
}
function nascondiVarianti(variante){
	oggetto=getObj(variante);
	oggetto.style.visibility=(oggetto.style.visibility=='hidden')?'visible':'hidden';
	oggetto.style.position=(oggetto.style.position=='absolute')?'relative':'absolute';
	link=getObj('link_nasc');
	link.childNodes[0].nodeValue=(link.firstChild.nodeValue!='Nascondi')?'Nascondi':'Visualizza';

}

function creaCalendario(variabile,source){
	input=getObj(variabile);
	s=getObj(source);
	displayCalendar(input,'dd-mm-yyyy',s);
}

var foto_link;

function abilita_foto(link_foto,link,url,image,string_popup){
	var primo_link=getObj(link_foto);
	var ingrandisci=getObj(link);
	primo_link.href=url;
	ingrandisci.href=url;
	ingrandisci.onclick=string_popup;
	primo_link.onclick=string_popup;
	if (ingrandisci.addEventListener){
	    ingrandisci.addEventListener('click',click_foto, false);
	    }
	 else if (ingrandisci.attachEvent){
		ingrandisci.attachEvent("onclick", click_foto,false);
		}
	if (primo_link.addEventListener){
	    primo_link.addEventListener('click',click_foto, false);
	    }
	 else if (primo_link.attachEvent){
		primo_link.attachEvent("onclick", click_foto,false);
		}
	//primo_link.onclick=string_popup;
	var immagine=primo_link.childNodes[0];
	immagine.src=image;
	this.foto_link=string_popup;
}

function click_foto(){
        eval(foto_link);
}


