/*
MENU A TENDINA
Parametri: id dell'elemento contenente il menu
Risultato: assegna le classi necessarie ai css per ottenere l'effetto del menu a tendina
*/
/*
function rollmenu(menu) {
	var arr = document.getElementById(menu).getElementsByTagName("li");
	var i;
	for (i=0; i<arr.length; i++) {

	 	if (arr[i].getElementsByTagName("ul")[0]) {
			arr[i].getElementsByTagName("a")[0].className += " hafigli ";
		}

		arr[i].onmouseover = function() {
			this.getElementsByTagName("a")[0].className += " active ";
			if (this.getElementsByTagName("ul")[0]) {
			//	this.getElementsByTagName("ul")[0].style.display="block";
				Effect.toggle(this.getElementsByTagName("ul")[0], 'appear', {duration: 0.5});
			}
		}
		arr[i].onmouseout = function() {
			this.getElementsByTagName("a")[0].className=this.getElementsByTagName("a")[0].className.replace("active","");
			if (this.getElementsByTagName("ul")[0]) {
				// this.getElementsByTagName("ul")[0].style.display="none";
				Effect.toggle(this.getElementsByTagName("ul")[0], 'appear', {duration: 0.1});
			}
		}
	}
}

function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}*/

function rollmenu(asdasd){
				if(window.mlrunShim == true){
					var Iframe = document.createElement("iframe");
					Iframe.setAttribute("src","about:blank");
					Iframe.setAttribute("scrolling","no");
					Iframe.setAttribute("frameBorder","0");
					Iframe.style.zIndex = "2";
					Iframe.style.filter = 'alpha(opacity=0)';
				}
				var effects_a = new Array();
				var divs = document.getElementsByTagName('div');
				for(var j=0;j<divs.length;j++){
					if(divs[j].className.indexOf('mlmenu') != -1){
						var lis = divs[j].getElementsByTagName('li');
						for(var i =0;i<lis.length;i++){
							lis[i].onmouseover = mlover;
							lis[i].onmouseout = mloutSetTimeout;
							if(window.mlrunShim == true){
								lis[i].appendChild(Iframe.cloneNode(false));
							}
							if(lis[i].getElementsByTagName('ul').length > 0){
								lis[i].className += 'haschild';
								if(divs[j].className.indexOf('arrow') != -1){
									if(divs[j].className.indexOf('vertical') != -1 || lis[i].parentNode.parentNode.nodeName != 'DIV'){
										lis[i].getElementsByTagName('a')[0].innerHTML += '<span class="vert">&rarr;</span>';
									}
									else{
										lis[i].getElementsByTagName('a')[0].innerHTML += '<span class="horiz">&darr;</span>';
									}
								}
								else if(divs[j].className.indexOf('plus') != -1){
									lis[i].getElementsByTagName('a')[0].innerHTML += '<span class="plus">+</span>';
								}
							}
							else{
								if(divs[j].className.indexOf('arrow') != -1){
									//This accounts for a wierd IE-specific bug in horizontal menus. CSS will set visibility: hidden;. This keeps the menu level(in IE)
									lis[i].getElementsByTagName('a')[0].innerHTML += '<span class="noshow">&darr;</span>';
								}
							}
							var uls = lis[i].getElementsByTagName('ul');
							for(var k=0;k<uls.length;k++){
								var found = 'no';
								for(var z=0;z<effects_a.length;z++){
									if(effects_a[z] == uls[k]){
										found = 'yes';
									}
								}
								if(found == 'no'){
									effects_a[effects_a.length] = uls[k];
									uls[k].style.zIndex = '100';
									mlEffectLoad(uls[k]);
								}
							}
						}
					}
				}
			}
			function mloutSetTimeout(e){
				if(!e){
					var the_e = window.event;
				}
				else{
					var the_e = e;
				}
				var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.toElement;
				if(reltg){
					var under = ancestor(reltg,this);
					if(under === false && reltg != this){
						window.mlLast = this;
						var parent = this.parentNode;
						while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
							parent = parent.parentNode;
						}
						if(parent.className.indexOf('delay') != -1){
							window.mlTimeout = setTimeout(function(){mlout()},200);
						}
						else{
							mlout();
						}
					}
				}
			}
			function mlout(){
			if(window.mlLast==null)return false;
				var uls = window.mlLast.getElementsByTagName('ul');
				var sib;
				for(var i=0;i<uls.length;i++){
					mlEffectOut(uls[i]);
					window.mlLast.className = 'haschild hide';
					if(window.mlrunShim == true){
						sib = uls[i];
						while(sib.nextSibling && sib.nodeName != 'IFRAME'){
								sib = sib.nextSibling
						}
						sib.style.display = 'none';
					}
				}
				window.lastover = null;
			}
			function mlover(e){
				if(!e){
					var the_e = window.event;
				}
				else{
					var the_e = e;
				}
				the_e.cancelBubble = true;
				if(the_e.stopPropagation){
					the_e.stopPropagation();
				}
				clearTimeout(window.mlTimeout);
				if(window.mlLast && window.mlLast != this && ancestor(this,window.mlLast) == false){
					mlout();
				}
				else{
					window.mlLast = null;
				}
				var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.fromElement;
				var ob = this.getElementsByTagName('ul');
				var under = ancestor(reltg,this);
				if(ob[0] && under == false){
					if(window.lastover != ob[0]){
						if(window.mlrunShim == true){
							var sib = ob[0];
							while(sib.nextSibling && sib.nodeName != 'IFRAME'){
								sib = sib.nextSibling
							}
							ob[0].style.display = 'block';
							sib.style.top = ob[0].offsetTop+'px';
							sib.style.left = ob[0].offsetLeft-2+'px';
							sib.style.width = ob[0].offsetWidth+'px';
							sib.style.height = ob[0].offsetHeight-2+'px';
							sib.style.border = '1px solid red';
							sib.style.display = 'block';
						}
						this.className = 'haschild';
						mlEffectOver(ob[0],this);
						window.lastover = ob[0];
					}
				}
			}
			function mlSetOpacity(ob,level){
				if(ob){
					//level is between 0 and 10
					//need to convert to decimal for standard
					var standard = level/10;
					//need to convert to 0-100 scale for IE filter
					var ie = level*10;
					ob.style.opacity = standard;
					ob.style.filter = "alpha(opacity="+ie+")"
				}
			}
			function mlIncreaseOpacity(ob){
					var current = ob.style.opacity;
					if(lastob == ob && lastop == current){
						//mlout has not interfered
						current = current *10;
						var upone = current +1;
						mlSetOpacity(ob,upone);
						lastob = ob;
						lastop = upone/10;
					}
			}
			function mlIncreaseHeight(ob){
				var current = parseInt(ob.style.height);
				var newh = current + 1;
				ob.style.height = newh+'px';
			}
			function mlIncreaseWidth(ob){
				var current = parseInt(ob.style.width);
				var newh = current + 1;
				ob.style.width = newh+'px';
			}
			function mlBlink(ob){
				var newb = '1px solid red';
				var old = '';
				if(ob.style.border==old){
					ob.style.border=newb;
				}
				else{
					ob.style.border=old;
					ob.style.borderTop = '1px solid';
				}
			}
			function mlShake(ob){
				var newp = '5px';
				var old = '';
				if(ob.style.paddingLeft==old){
					ob.style.paddingLeft=newp;
				}
				else{
					ob.style.paddingLeft=old;
				}
			}
			function mlEffectOver(ob,parent){
				switch(ob.className){
					case 'fade':
						ob.style.display = 'block';
						if(ob.style.opacity == 0){
							lastob = ob
							lastop = 0;
							for(var i = 1;i<=10;i++){
								setTimeout(function(){mlIncreaseOpacity(ob)},i*50);
							}
							setTimeout(function(){ob.style.filter = ''},200);
						}
						break;
					case 'blink':
						ob.style.display = 'block';
						for(var i=0;i<10;i++){
							setTimeout(function(){mlBlink(ob)},i*50);
						}
						break;
					case 'shake':
						ob.style.display = 'block';
						for(var i=0;i<10;i++){
							setTimeout(function(){mlShake(ob)},i*50);
						}
						break;
					case 'blindv':
						ob.style.display = 'block';
						if(ob.offsetHeight){
							var height = ob.offsetHeight
							ob.style.height = '0px';
							ob.style.overflow = 'hidden';
							for(var i=0;i<height;i++){
								setTimeout(function(){mlIncreaseHeight(ob)},i*3);
							}
							setTimeout(function(){ob.style.overflow='visible';},height*3)
						}
						break;
					case 'blindh':
						ob.style.display = 'block';
						if(ob.offsetWidth){
							var width = ob.offsetWidth;
							ob.style.width = '0px';
							ob.style.overflow = 'hidden';
							for(var i=0;i<width;i++){
								setTimeout(function(){mlIncreaseWidth(ob)},i*3);
							}
							setTimeout(function(){ob.style.overflow='visible';},width*3)
						}
						break;
					default:
						ob.style.display = 'block';
						break;
				}
			}
			function mlEffectOut(ob){
				switch(ob.className){
					case 'fade':
						mlSetOpacity(ob,0);
						ob.style.display = 'none';
						break;
					case 'blink':
						ob.style.border = '';
						ob.style.display = 'none';
						break;
					case 'shake':
						ob.style.paddingLeft = '';
						ob.style.display = 'none';
						break;
					default:
						ob.style.display = 'none';
						break;
				}
			}
			function mlEffectLoad(ob){
				var parent = ob.parentNode;
				while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
					parent = parent.parentNode;
				}
				if(parent.className.indexOf('fade') != -1){
						ob.style.display = 'none';
						ob.className = 'fade';
						mlSetOpacity(ob,0);
				}
				else if(parent.className.indexOf('blink') != -1){
					ob.className = 'blink';
					ob.style.display = 'none';
				}
				else if(parent.className.indexOf('shake') != -1){
					ob.className = 'shake';
					ob.style.display = 'none';
				}
				else if(parent.className.indexOf('blindv') != -1){
					ob.className = 'blindv';
					ob.style.display = 'none';
				}
				else if(parent.className.indexOf('blindh') != -1){
					ob.className = 'blindh';
					ob.style.display = 'none';
				}
				else{
					ob.className = 'none';
					ob.style.display = 'none';
				}
			}
			function ancestor(child, parent){
				if(child==null)return false;//Saves checking elsewhere
				//This is a fix for a Firefox bug *gasp*
				//Aparantly causes a bug in Opera!
				//I see no choice but a browser detect. *sigh* I didn't want to have to do this.
				if(navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Opera') == -1){
					//This should only be run by Gecko based browsers. this code should be fine in everything but Opera so forge away browsers.
					var allc = parent.getElementsByTagName('*');
					for(var i= 0;i<allc.length;i++){
						if(allc[i] == child){
							return true;
						}
					}
				}
				else{
					//http://www.dynamicdrive.com/forums/showthread.php?t=12341 Thanks Twey!
					for(; child.parentNode; child = child.parentNode){
						if(child.parentNode === parent) return true;
					}
				}
				return false;

			}

/*
FUNZIONE SEMPLIFICATA PER POPUP
Parametri: indirizzo della nuova pagina
Risultato: apre una finestra popup con dimensioni preimpostate
*/
function popup(indirizzo) {
	params = 'height=450, ';
	params += 'width=650, ';
	params += 'top=10, ';
	params += 'left=10, ';
	params += 'location=0, ';
	params += 'menubar=0, ';
	params += 'resizable=0, ';
	params += 'scrollbars=1, ';
	params += 'status=0, ';
	params += 'toolbar=0';

	window.open(indirizzo, null, params);
}

//SIMULA LA PSEUDO-CLASSE "FOCUS" - v.1.1 (ora può avere la propria classe, ne viene aggiunta un'altra)
function setFocus(elemento) {
	elemento.className+=" focus";
}

function unsetFocus(elemento) {
	elemento.className=elemento.className.replace('focus', '');
}

//FUNZIONE PER MOSTRARE I LINK ATTIVI
// richiamare la funzione nel tag body con onLoad e passare come parametro l'id del contenitore del menu
// non serve assegnare nessun valore particolare all'html.
// non funziona coi link indicizzabili ma tanto nel menu di navigazione non ci sono le categorie ;)

function currentlink(contenitore) {

	var stringa_get = document.location.href;
	var menu = new Array();
	menu = document.getElementById(contenitore).getElementsByTagName("a");
	if (stringa_get.match("php")) { //cioè se sono nel caso www.nomedominio.com/qualcosa.PHP <--
		var i;
		for (i=0; i<menu.length; i++) {
			if (stringa_get.match(menu[i].href)) menu[i].className = "active";
		}
	} else menu[0].className = "active";
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


var wiN;

function openWin(nomeFile,nomeFinestra,parametri,larghezza,altezza,barre,ridimensione,centrato) {

	option="toolbar=no,width=" + larghezza + ",height=" + altezza +",resizable=" + ridimensione + ",scrollbars=" + barre
	if (centrato=='yes') {
		LeftPosition = (screen.width) ? (screen.width-larghezza)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-altezza)/2 : 0;
		option=option + ",top="+TopPosition+",left="+LeftPosition
	}
	wiN = window.open(nomeFile + "?titolo=" + nomeFinestra + parametri,nomeFinestra,option);
}

 function checkFormPrivacy() {
    obj=eval("document.contatti");
    nome=obj.nome.value;
    cognome=obj.cognome.value;
    prov=obj.provincia.value;
    email=obj.email.value;
    messaggio=obj.messaggio.value;
    privacy=obj.privacy.checked;
    if ( (nome=="") || (cognome=="") || (prov==-1) || (email=="") || (messaggio=="") || (!privacy))
	{
        alert("ATTENZIONE\nCompletare tutti i dati obbligatori per inviare la richiesta\n\nGrazie")
    }
    else if(confirm("Sei sicuro di voler inviare la richiesta di contatto?"))
    {
        obj.submit();
    }
 }

 function checkForm() {
    obj=eval("document.contatti");
    nome=obj.nome.value;
    cognome=obj.cognome.value;
    prov=obj.provincia.value;
    email=obj.email.value;
    messaggio=obj.messaggio.value;
    if ( (nome=="") || (cognome=="") || (prov==-1) || (email=="") || (messaggio=="") )
	{
        alert("ATTENZIONE\nCompletare tutti i dati obbligatori per inviare la richiesta\n\nGrazie")
    }
    else if(confirm("Sei sicuro di voler inviare la richiesta di contatto?"))
    {
        obj.submit();
    }
 }

 // Spam-proof email by deanq.com
function vmemail(who,subject,domain,body) {
  if (!who) var who = "info";
  if (!domain) var domain = "sito.com";
  if (!subject) var subject = "Richiesta Informazioni";
  if (!body) var body = " ";
  eval("location.href='mailto:" + who + "@" + domain + "?subject=" + subject + "&body=" + body + "'");
}

function verificaUsername() {
	if (document.frmVerificaUsername.username.value=="") {
		alert("Devi inserire la tua username con la quale hai effettuato l'iscrizione");
	} else {
		document.frmVerificaUsername.submit();
	}
}

function cambiaPwd() {
	if (confirm("Sei sicuro di voler ottenere una nuova password?")) {
		document.frmCambiaPwd.submit();
	} else {
		return false;
	}
}


function ckForm() {
	// ck la form per inserire un nuovo cliente

	/**
	 *	Controllo solo Ragione sociale, Telefono, Email
	 *	gli altri controlli li lascio per dopo.
	 */

	error = false;
	msgError = "";
	fieldToFocus = "";

	form = eval("document.new_customer");

	if (form.privacy.checked) {
		ragSoc = form.elements["cliente[rag_soc]"].value;
		telefono = form.elements["cliente[telefono]"].value;
        citta = form.elements["cliente[comune]"].value;
        cap = form.elements["cliente[CAP]"].value;
        provincia = form.elements["cliente[prov]"].value;
		username = form.elements["cliente[user]"].value;
		pwd1 = form.elements["cliente[pwd1]"].value;
		pwd2 = form.elements["cliente[pwd2]"].value;

		if ((pwd1 == "") || (pwd2 == "")) {
			error = true;
			msgError += "Compilare il campo 'Password'.\n";
			if (pwd1 == "") {
				fieldToFocus = "cliente[pwd1]";
			} else {
				fieldToFocus = "cliente[pwd2]";
			}
		} else {
			if (pwd1 != pwd2) {
				error = true;
				msgError = "Le password inserite devono coincidere.\n"+msgError;
				fieldToFocus = "cliente[pwd2]";
			} else {
				if (pwd1.length < 6) {
					error = true;
					msgError = "La password deve contenere almeno 6 caratteri.\n"+msgError;
					fieldToFocus = "cliente[pwd1]";
				}
			}
		}
		if (username == "") {
			error = true;
			msgError = "Compilare il campo 'Email/Username'.\n"+msgError;
			fieldToFocus = "cliente[user]";
		}
		if (citta == "") {
			error = true;
			msgError = "Compilare il campo 'Città'.\n"+msgError;
			fieldToFocus = "cliente[comune]";
		}
        if (cap == "") {
			error = true;
			msgError = "Compilare il campo 'CAP'.\n"+msgError;
			fieldToFocus = "cliente[CAP]";
		}
        if (provincia == -1) {
			error = true;
			msgError = "Compilare il campo 'Provincia'.\n"+msgError;
			fieldToFocus = "cliente[prov]";
		}
		if (telefono == "") {
			error = true;
			msgError = "Compilare il campo 'Telefono'.\n"+msgError;
			fieldToFocus = "cliente[telefono]";
		}
		if (ragSoc == "") {
			error = true;
			msgError = "Compilare il campo 'Ragione Sociale'.\n"+msgError;
			fieldToFocus = "cliente[rag_soc]";
		}

		if (!error) {
			// nn ci sn errori
			if (confirm("Confermi i dati inseriti?")) {
				document.new_customer.azione.value="salva";
				document.new_customer.submit();
			}
		} else {
			alert(msgError);
			form.elements[fieldToFocus].focus();
		}
	} else {
		alert("E' necessario accettare l'autorizzazione per il trattamento dei dati personali per procedere con l'iscrizione.")
	}
}

function cambiaInfoUtente() {

	form = eval("document.frmCambiaInfoUtente");
	error = false;
	msgError="";

	ragSoc = form.elements["rag_soc"].value;
	telefono = form.elements["telefono"].value;
    citta = form.elements["citta"].value;
    cap = form.elements["cap"].value;
    provincia = form.elements["prov_id"].value;
	email = form.elements["email"].value;
    indirizzo = form.elements["indirizzo"].value;

	if (email == "") {
		error = true;
		msgError = "Compilare il campo 'Email/Username'.\n"+msgError;
	}
	if (citta == "") {
		error = true;
		msgError = "Compilare il campo 'Città'.\n"+msgError;
	}
    if (cap == "") {
		error = true;
		msgError = "Compilare il campo 'CAP'.\n"+msgError;
	}
    if (provincia == -1) {
		error = true;
		msgError = "Compilare il campo 'Provincia'.\n"+msgError;
	}
	if (telefono == "") {
		error = true;
		msgError = "Compilare il campo 'Telefono'.\n"+msgError;
	}
    if (indirizzo == "") {
		error = true;
		msgError = "Compilare il campo 'Indirizzo'.\n"+msgError;
	}
	if (ragSoc == "") {
		error = true;
		msgError = "Compilare il campo 'Ragione Sociale'.\n"+msgError;
	}

	if (!error) {
		// nn ci sn errori
		if (confirm("Confermi i dati inseriti?")) {
			document.frmCambiaInfoUtente.submit();
		}
	} else {
		alert(msgError);
	}

}


function controllaPermessi() {
	if ((window.opener!==null) && (window.opener!==undefined) ) {
	} else {
		window.location.href = "http://www.blisite.net";
	}
}

function faq(cod) {
	var x=document.frmFaq.elements['lstCodCategoria'];

  	for (i=0;i<x.length;i++) {
  		if(x.options[i].value == cod) {
  			x.options[i].selected = true;
  		}
  	}
  	document.frmFaq.submit();
}

function regione(cod) {
	if(cod != '') {
		$("#lstRegione").val(cod);
	} else {
		cod = $("#lstRegione").val();
	}
	url = './ajaxProvincie.php';
	if(cod != "") {
		$("#sLstProvincie").load(url,{id: cod});
	}

	$("#lstProvincia").val('-1');
	$("#lstComune").val('-1');

	$("#hidPagina").val(1);
	$("#frmRicerca").submit();

}

function regioneTendina(cod) {
	if(cod != '') {
		$("#lstRegione").val(cod);
	} else {
		cod = $("#lstRegione").val();
	}
	url = './ajaxProvincie.php';
	if(cod != "") {
		$("#sLstProvincie").load(url,{id: cod});
	}

	//$("#hidPagina").val(1);
	//$("#frmRicerca").submit();

}

function regioneTendinaAgenti(cod) {
	if(cod != '') {
		$("#lstRegione").val(cod);
	} else {
		cod = $("#lstRegione").val();
	}
	url = './ajaxProvincieAgenti.php';
	if(cod != "") {
		$("#sLstProvincie").load(url,{id: cod});
	}

	//$("#hidPagina").val(1);
	//$("#frmRicerca").submit();

}

function continenteTendinaAgenti(cod) {
	if(cod != '') {
		$("#lstContinente").val(cod);
	} else {
		cod = $("#lstContinente").val();
	}
	url = './ajaxContinenteAgenti.php';

	if(cod != "") {
		$("#sLstStato").load(url,{id: cod});
	}
	//$("#hidPagina").val(1);
	//$("#frmRicerca").submit();
}

function provinciaTendina(cod) {
	if(cod != '') {
		$("#lstProvincia").val(cod);
	} else {
		cod = $("#lstProvincia").val();
	}
	url = './ajaxComuni.php';
	if(cod != "") {
		$("#sLstComune").load(url,{id: cod});
	}
	//$("#hidPagina").val(1);
	//$("#frmRicerca").submit();

}


function doRicerca() {
	$("#hidPagina").val(1);
	$("#frmRicerca").submit();
}

function doRicercaEn() {
	if( $("#lstStato").val() == "-1" ) {
		alert("Plese select one of the states from the list");
		return false;
	} else {
		$("#hidPagina").val(1);
		$("#frmRicerca").submit();
		return true;
	}
}

function paginaRicerca(pag) {
	$("#hidPagina").val(pag);
	$("#frmRicerca").submit();
}

function dettagliAreeCompetenza(idElemento, idProvincia) {

    if ($("#areaCompetenza"+idElemento+"_"+idProvincia).is(":hidden")) {
		$("#areaCompetenza"+idElemento+"_"+idProvincia).slideDown("slow");
	} else {
		$("#areaCompetenza"+idElemento+"_"+idProvincia).slideUp("slow");
	}
	return false;
/*
    $('.elenco_provincie a').click( function() {
  $(this).parent('.elenco_provincie').siblings('.elenco_comuni').find('ul').show('slow');
  $(this).siblings('a:visible').hide('slow');
});
*/
}

