/* ******************************************************** */
/* ************* Inizializzazione funzioni **************** */
/* ******************************************************** */
function initFunctions() { preloadImages(); }

/* **************************************************** */
/* FUNZIONE per aprire una finestra di PopUp centrata   */
/* **************************************************** */
function openPopUp(url,w,h,scrl) {
  l = (screen.width-w)/2;
  t = (screen.height-h)/2;
  newWin = window.open(url,"nuovafinestra", "width="+w+", height="+h+", left="+l+", top="+t+", scrollbars="+scrl+", resizable=no") 
  }

  
/* ****************************************************** */
/* FUNZIONE per togliere cornice ai filmati flash in IE   */
/* ****************************************************** */
function loadswf(swf,w,h){
	document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"");
	document.write("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"");
	document.write("width=\""+w+"\" height=\""+h+"\">");
	document.write("<param name='movie' value='"+swf+"' />\n");
	document.write("<param name='quality' value='high' /><param name='wmode' value='transparent' />\n");
	document.write("<embed src=\""+swf+"\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+w+"\" height=\""+h+"\"></embed>");
	document.write("</object>\n");
}
  

/* ************************************************************* */
/* 2 FUNZIONI per aprire il DIV di aiuto per l'editor di testo   */
/* ************************************************************* */
function overlay(curobj,subobj,w,h){
	if (document.getElementById){
		var subobj=document.getElementById(subobj);
		subobj.style.left=(screen.width-w)/2+"px";
		//distanza fissa e non calcolata
		//subobj.style.top=(screen.height-h)/2+"px"; 
		subobj.style.top=h+"px";
		subobj.style.zIndex=1;
		subobj.style.display="block";
		return false;
	}
	else
	  return true;
}
function overlayclose(subobj){ document.getElementById(subobj).style.display="none" }


/* ************************************************* */
/* POPUP CHE SI AUTODIMENSIONA SU UNA IMMAGINE       */
/* ************************************************* */
function PrevFoto(img){
  foto1= new Image()
  foto1.src=(img)
  Controlla(img)
}
function Controlla(img){
  if((foto1.width!=0)&&(foto1.height!=0)){
    viewFoto(img);
  }
  else{
    funzione="Controlla('"+img+"')";
    intervallo=setTimeout(funzione,20);
  }
}
function viewFoto(img){
  largh=foto1.width;
  altez=foto1.height;
  posY=(screen.width-foto1.width)/2;
  posX=(screen.height-foto1.height)/2;
  stringa="width="+largh+",height="+altez+",top="+posX+",left="+posY;
  finestra=window.open(img,"",stringa);
}


/* ********************************************* */
/* *** Countdown caratteri nelle textarea ****** */
/* ********************************************* */
function countChars(d) {
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
	document.anagrafiche.descrizione.onkeydown = countChars;
	if (nn4) document.captureEvents(Event.KEYDOWN);

    var oldString = "";

	if (document.anagrafiche.descrizione.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.anagrafiche.rimasti.value = 255;
		    document.anagrafiche.descrizione.value = "";
			return true;
		}else{
			document.anagrafiche.rimasti.value = 0;
		    document.anagrafiche.descrizione.value = document.anagrafiche.descrizione.value.substring(0,255);
			return false;
		}
	} else {
	  document.anagrafiche.rimasti.value = 255 - document.anagrafiche.descrizione.value.length;
	  oldString = document.anagrafiche.descrizione.value.substring(0,255);
	}
}

  
/* ********************************************* */
/* FUNZIONE per controllare il MOTORE DI RICERCA */
/* ********************************************* */
function VerificaQuery(form) {
	if (form.q.value == 'Scrivi qui' || form.q.value == '') {
    	alert("Inserisci la chiave di ricerca per continuare");
        return false;
	} else if (form.q.value.length <= 3) {
    	alert("La chiave di ricerca deve essere superiore alle tre lettere!");
        return false;
	} else {
		return true;
	}
}
function GoToSearch(form) {
	// Interpreto le sequenze escape
   	var query = escape(form.q.value);
	var lingua = escape(form.lang.value);

    if (!VerificaQuery(form)) {
   		return false;
    } else {
      form.action = '_pagine.asp?p=search&q=' + query + '&lang=' + lingua;
      form.submit();
    }
}


/* *********************************************** */
/* FUNZIONI per la validazione dei campi dei forms */
/* *********************************************** */
// AUTO PARCO
function controllo(formParcoAuto) {
  if (formParcoAuto.dhtmlgoodies_marca.value.length == 0 || formParcoAuto.dhtmlgoodies_marca.value == "0") {
    alert("ATTENZIONE!!!\nScegliere una marca");
    formParcoAuto.dhtmlgoodies_marca.focus();
    return (false);
  }
  return (true);
}

//
function invalid() {
	if (document.Login.txtUserPass.value != document.Login.txtUserPass2.value) {
		alert("Passwords errata. Riprova.");
		return false;
	}
		return true;
}

function emailvalidation(entered, alertbox) {
	with (entered) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
			{
				if (alertbox) {alert(alertbox);} return false;
		} else {
			return true;
		}
	}
}

// ANAGRAFICHE
function control(anagrafiche) {
  if (anagrafiche.idMarca.value.length == 0 || anagrafiche.idMarca.value == "tutti") {
    alert("Scegliere una marca per questo modello");
    anagrafiche.idMarca.focus();
    return (false);
  }
  
  if (anagrafiche.nome.value.length == 0 || anagrafiche.nome.value.length > 255) {
    alert("Controllare il campo \"Modello\"\n(campo vuoto o lunghezza max (255) superata)");
    anagrafiche.nome.focus();
    return (false);
  }
  return (true);
}

// FREQUENTLY ASKED QUESTION
function faqControl(faqForm) {
  if (faqForm.titolo.value.length == 0 || faqForm.titolo.value.length > 255) {
    alert("Controllare il campo \"Titolo\"\n(campo vuoto o lunghezza max (255) superata)");
    faqForm.titolo.focus();
    return (false);
  }
  return (true);
}


/* **************************************************** */
/* ************** FUNZIONE MOLTI-A-MOLTI ************** */
/* **************************************************** */
	var fromBoxArray = new Array();
	var toBoxArray = new Array();
	var selectBoxIndex = 0;
	var arrayOfItemsToSelect = new Array();
	
	function moveSingleElement() {
		var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
		var tmpFromBox;
		var tmpToBox;
		if(this.tagName.toLowerCase()=='select'){			
			tmpFromBox = this;
			if(tmpFromBox==fromBoxArray[selectBoxIndex])tmpToBox = toBoxArray[selectBoxIndex]; else tmpToBox = fromBoxArray[selectBoxIndex];
		}else{
			if(this.value.indexOf('>')>=0){
				tmpFromBox = fromBoxArray[selectBoxIndex];
				tmpToBox = toBoxArray[selectBoxIndex];			
			}else{
				tmpFromBox = toBoxArray[selectBoxIndex];
				tmpToBox = fromBoxArray[selectBoxIndex];	
			}
		}
		
		for(var no=0;no<tmpFromBox.options.length;no++){
			if(tmpFromBox.options[no].selected){
				tmpFromBox.options[no].selected = false;
				tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);
				
				for(var no2=no;no2<(tmpFromBox.options.length-1);no2++){
					tmpFromBox.options[no2].value = tmpFromBox.options[no2+1].value;
					tmpFromBox.options[no2].text = tmpFromBox.options[no2+1].text;
					tmpFromBox.options[no2].selected = tmpFromBox.options[no2+1].selected;
				}
				no = no -1;
				tmpFromBox.options.length = tmpFromBox.options.length-1;
			}			
		}
		
		var tmpTextArray = new Array();
		for(var no=0;no<tmpFromBox.options.length;no++){
			tmpTextArray.push(tmpFromBox.options[no].text + '___' + tmpFromBox.options[no].value);			
		}
		tmpTextArray.sort();
		var tmpTextArray2 = new Array();
		for(var no=0;no<tmpToBox.options.length;no++){
			tmpTextArray2.push(tmpToBox.options[no].text + '___' + tmpToBox.options[no].value);			
		}		
		tmpTextArray2.sort();
		
		for(var no=0;no<tmpTextArray.length;no++){
			var items = tmpTextArray[no].split('___');
			tmpFromBox.options[no] = new Option(items[0],items[1]);
		}		
		
		for(var no=0;no<tmpTextArray2.length;no++){
			var items = tmpTextArray2[no].split('___');
			tmpToBox.options[no] = new Option(items[0],items[1]);			
		}
	}
	
	function sortAllElement(boxRef) {
		var tmpTextArray2 = new Array();
		for(var no=0;no<boxRef.options.length;no++){
			tmpTextArray2.push(boxRef.options[no].text + '___' + boxRef.options[no].value);			
		}		
		tmpTextArray2.sort();		
		for(var no=0;no<tmpTextArray2.length;no++){
			var items = tmpTextArray2[no].split('___');
			boxRef.options[no] = new Option(items[0],items[1]);			
		}		
	}
	
	function moveAllElements() {
		var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
		var tmpFromBox;
		var tmpToBox;		
		if(this.value.indexOf('>')>=0){
			tmpFromBox = fromBoxArray[selectBoxIndex];
			tmpToBox = toBoxArray[selectBoxIndex];			
		}else{
			tmpFromBox = toBoxArray[selectBoxIndex];
			tmpToBox = fromBoxArray[selectBoxIndex];	
		}
		
		for(var no=0;no<tmpFromBox.options.length;no++){
			tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);			
		}	
		tmpFromBox.options.length=0;
		sortAllElement(tmpToBox);
	}
	
	/* This function highlights options in the "to-boxes". 
	   It is needed if the values should be remembered after submit. 
	   Call this function onsubmit for your form */
	function multipleSelectOnSubmit() {
		for(var no=0;no<arrayOfItemsToSelect.length;no++){
			var obj = arrayOfItemsToSelect[no];
			for(var no2=0;no2<obj.options.length;no2++){
				obj.options[no2].selected = true;
			}
		}
	}
	
	function createMovableOptions(fromBox,toBox,totalWidth,totalHeight,labelLeft,labelRight) {		
		fromObj = document.getElementById(fromBox);
		toObj = document.getElementById(toBox);
		
		arrayOfItemsToSelect[arrayOfItemsToSelect.length] = toObj;

		fromObj.ondblclick = moveSingleElement;
		toObj.ondblclick = moveSingleElement;

		fromBoxArray.push(fromObj);
		toBoxArray.push(toObj);
		
		var parentEl = fromObj.parentNode;

		var parentDiv = document.createElement('DIV');
		parentDiv.className='multipleSelectBoxControl';
		parentDiv.id = 'selectBoxGroup' + selectBoxIndex;
		parentDiv.style.width = totalWidth + 'px';
		parentDiv.style.height = totalHeight + 'px';
		parentDiv.style.textAlign = 'center';
		parentEl.insertBefore(parentDiv,fromObj);
		
		var subDiv = document.createElement('DIV');
		subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
		fromObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';

		var label = document.createElement('SPAN');
		label.innerHTML = labelLeft;
		subDiv.appendChild(label);
		
		subDiv.appendChild(fromObj);
		subDiv.className = 'multipleSelectBoxDiv';
		parentDiv.appendChild(subDiv);
		
		var buttonDiv = document.createElement('DIV');
		buttonDiv.style.verticalAlign = 'middle';
		buttonDiv.style.paddingTop = (totalHeight/2) - 50 + 'px';
		buttonDiv.style.width = '30px';
		buttonDiv.style.textAlign = 'center';
		parentDiv.appendChild(buttonDiv);
		
		var buttonRight = document.createElement('INPUT');
		buttonRight.type='button';
		buttonRight.value = '>';
		buttonRight.style.textAlign = 'center';
		buttonDiv.appendChild(buttonRight);	
		buttonRight.onclick = moveSingleElement;	
		
		var buttonAllRight = document.createElement('INPUT');
		buttonAllRight.type='button';
		buttonAllRight.value = '>>';
		buttonAllRight.style.textAlign = 'center';
		buttonAllRight.onclick = moveAllElements;
		buttonDiv.appendChild(buttonAllRight);		
		
		var buttonLeft = document.createElement('INPUT');
		buttonLeft.style.marginTop='10px';
		buttonLeft.type='button';
		buttonLeft.value = '<';
		buttonLeft.style.textAlign = 'center';
		buttonLeft.onclick = moveSingleElement;
		buttonDiv.appendChild(buttonLeft);		
		
		var buttonAllLeft = document.createElement('INPUT');
		buttonAllLeft.type='button';
		buttonAllLeft.value = '<<';
		buttonAllLeft.style.textAlign = 'center';
		buttonAllLeft.onclick = moveAllElements;
		buttonDiv.appendChild(buttonAllLeft);
		
		var subDiv = document.createElement('DIV');
		subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
		toObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';

		var label = document.createElement('SPAN');
		label.innerHTML = labelRight;
		subDiv.appendChild(label);
				
		subDiv.appendChild(toObj);
		parentDiv.appendChild(subDiv);		
		
		toObj.style.height = (totalHeight - label.offsetHeight) + 'px';
		fromObj.style.height = (totalHeight - label.offsetHeight) + 'px';

		selectBoxIndex++;
	}
  
