function trim(str) 
{
  return str.replace(/^\s+|\s+$/g, '');
}


function avvio() {
	// calcolo le dimensioni dello schermo
	var larghezza = 0, altezza = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		larghezza = window.innerWidth;
		altezza = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		larghezza = document.documentElement.clientWidth;
		altezza = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		larghezza = document.body.clientWidth;
		altezza = document.body.clientHeight;
	}
}
function blocca(id) {
	if ( document.getElementById(id) ) {
		if (document.getElementById(id).style.display == 'block') {
			document.getElementById(id).style.display = 'none';
			} else {
	document.getElementById(id).style.display = 'block';
	}
	}
}
function nascondi(id){
	if ( document.getElementById(id) ) {
	document.getElementById(id).style.display = 'none';
	}
}
function visualizza(id){
	if ( document.getElementById(id) ) {
	document.getElementById(id).style.display = 'block';
	}
}
function vai(id){
	location.href = id;
}
function settavalore(formmoduloid,id,nuovovalore){
	eval("document."+formmoduloid+"."+id+".value='"+nuovovalore+"'");
}
function settavalorehtml(id,nuovovalore){
	if (document.getElementById(id)) {
		document.getElementById(id).innerHTML = nuovovalore;
	}
}
function cambia(formmodulo,modulo,formmoduloid,id) {
	var nuovovalore;
	eval("nuovovalore=document."+formmodulo+"."+modulo+".value");
	
	nuovovalore = nuovovalore.replace(/\n/g,"<br>");
	//nuovovalore = nuovovalore.replace(/\r/g,"<br>");
	if (formmoduloid == '') {
		if (document.getElementById(id)) {
		document.getElementById(id).innerHTML = nuovovalore;
		}
	} else {
		eval("document."+formmoduloid+"."+id+".value='"+nuovovalore+"'");
	}
}

function selezionaarticolo(colore,taglia,id2articolo,acquista) {
	  if(window.XMLHttpRequest)
	  {
		 richiestaricerca = new XMLHttpRequest();
	  }
	  else if(window.ActiveXObject)
	  {
		 richiestaricerca = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  richiestaricerca.open("POST", "/ajax/calcolataglieecolori.asp", true);
	  richiestaricerca.onreadystatechange = function() 
	  {
		  if(richiestaricerca.readyState == 4)
		  {	
			  if(richiestaricerca.status == 200)
			  {
				  rispostaricerca = richiestaricerca.responseText
				  var mySplitResultb = rispostaricerca.split("###");
				  errore = mySplitResultb[0];
				  fotoprodottoselezionata = mySplitResultb[1];
				  prezzoprincipalet =  mySplitResultb[2];
				  elencocarrello =  mySplitResultb[3];
				  carrellino = mySplitResultb[4];
				  prodottiacarrello = mySplitResultb[5];
				  //lingua = mySplitResultb[2];
				  if (errore != "" && errore != undefined) {
					  //document.getElementById('testoseleziona').innerHTML = "Errore: " + errore + " - Riprovare";
					  window.alert ("Errore: " + errore + " - Riprovare");
					  } else {
						  if (fotoprodottoselezionata != "" && fotoprodottoselezionata != undefined) {
							  settavalorehtml('foto',"<a href='/img/full/" + fotoprodottoselezionata + "' title='ingrandici' class='immagineprincipaleprodotto targetblank'><img src='/img/big/" + fotoprodottoselezionata + "' class='immagineprincipaleprodotto'/></a>")
							  }
						  settavalorehtml('prezzoprincipale',prezzoprincipalet)
						  settavalorehtml('tagliecoloriacquista',elencocarrello)
						  if (carrellino != "" && carrellino != undefined) {
							    settavalorehtml('carrellolaterale',carrellino)
								settavalorehtml('bottonecarrello',"Carrello ("+prodottiacarrello+")")
								document.getElementById('bottonecarrello').style.visibility = "visible";
								//document.getElementById('carrellolaterale').style.display = "block";
								visualizza('carrellolaterale');
								//location.href = "/p/it/it/carrello";//
							  }
					  }
			  }
		  }
	  }
	  richiestaricerca.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  richiestaricerca.send("colore="+colore+"&taglia="+taglia+"&id2articolo="+id2articolo+"&acquista="+acquista);
}


function chiamafotovideo(fotoovideo,url,titolo,testo,id2) {
	if (document.getElementById('foto')) {
		if (fotoovideo == 1) {
			valoreinner = "<a href='/img/full/" + url + "' title='ingrandici' class='immagineprincipaleprodotto' target='_blank'><img src='/img/big/"+url+"' alt='"+titolo+"' title='"+titolo+"' width='350' height='350'/></a>"
			if (document.getElementById('cambioimmagine')) {
				visualizza('cambioimmagine')
				}
			if (document.getElementById('cambiovideo')) {
				nascondi('cambiovideo')
				}
			} else {
			valoreinner = "<object width='440' height='264'><param name='movie' value='http://www.youtube.com/v/"+url+"?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1&amp;loop=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+url+"?fs=1&amp;hl=it_IT&amp;rel=0&amp;autoplay=1&amp;loop=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='440' height='264' background='transparent'></embed></object><div class='spiegazione'><u id='titolovideo'>"+titolo+"</u><br><span id='testovideo'>"+testo+"</span></div>"
			if (document.getElementById('cambioimmagine')) {
				nascondi('cambioimmagine')
				document.formvideo.titolovideomodulo.value = titolo
				}
			if (document.getElementById('cambiovideo')) {
				visualizza('cambiovideo')
				document.formvideo.testovideomodulo.value = testo
				}
		}
	document.getElementById('foto').innerHTML = valoreinner;
	}
}


//CARRELLO/////
function colora(id,colore) {
	if (colore == "1") {
		document.getElementById(id).style.background = "#ff0000";
		document.getElementById(id).style.color = "#ffffff";
	} else {
		document.getElementById(id).style.background = "#ffffff";
		document.getElementById(id).style.color = "#000000";
	}
}
function controllacarrello() {
		var errore;
		errore = 0;
		colora('nome',0);
		colora('cognome',0);
		colora('ragsoc',0);
		colora('codfisc',0);
		colora('piva',0);
		colora('indirizzo1',0);
		colora('indirizzo2',0);
		colora('indirizzo3',0);
		colora('citta',0);
		colora('cap',0);
		colora('prov',0);
		colora('tel',0);
		colora('email',0);
		colora('nomeb',0);
		colora('cognomeb',0);
		colora('indirizzo1b',0);
		colora('indirizzo2b',0);
		colora('indirizzo3b',0);
		colora('cittab',0);
		colora('capb',0);
		colora('provb',0);
		colora('telb',0);
		colora('annotazioni',0);
		
		nome = document.getElementById('nome').value;
		cognome = document.getElementById('cognome').value;
		ragsoc = document.getElementById('ragsoc').value;
		codfisc = document.getElementById('codfisc').value;
		piva = document.getElementById('piva').value;
		indirizzo1 = document.getElementById('indirizzo1').value;
		indirizzo2 = document.getElementById('indirizzo2').value;
		indirizzo3 = document.getElementById('indirizzo3').value;
		citta = document.getElementById('citta').value;
		cap = document.getElementById('cap').value;
		prov = document.getElementById('prov').value;
		tel = document.getElementById('tel').value;
		email = document.getElementById('email').value;
		nomeb = document.getElementById('nomeb').value;
		cognomeb = document.getElementById('cognomeb').value;
		indirizzo1b = document.getElementById('indirizzo1b').value;
		indirizzo2b = document.getElementById('indirizzo2b').value;
		indirizzo3b = document.getElementById('indirizzo3b').value;
		cittab = document.getElementById('cittab').value;
		capb = document.getElementById('capb').value;
		provb = document.getElementById('provb').value;
		telb = document.getElementById('telb').value;
		annotazioni = document.getElementById('annotazioni').value;
			
		regexpnome = /^[a-zA-Zאטילעש]+(([\'\,\.\- ][a-zA-Zאטילעש ])?[a-zA-Zאטילעש]*)*$/
		regexpemail = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/ 
		regexpcodfiscita = /^[A-Za-z]{6}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{3}[A -Za-z]{1}$/
		regexptelefono = /^([+|])?([1-9]*\-?\ ?\/?[0-9\.\-\ ]*)$/
		regexpivaitaliana = /[0-9]{11,11}/
		
		campo = nome;
		if ( campo == '' || campo==null ) {
			colora('nome',1);
			errore = 1
			} else {
				campo = trim(campo)
				if (!regexpnome.test(campo)) {
					colora('nome',1);
					errore = 1
					}
			}
		campo = cognome;
		if ( campo == '' || campo==null ) {
			colora('cognome',1);
			errore = 1
			} else {
				campo = trim(campo)
				if (!regexpnome.test(campo)) {
					colora('cognome',1);
					errore = 1
					}
			}
		campo = codfisc;
		if ( campo == '' || campo==null ) {
			colora('codfisc',1);
			errore = 1
			} else {
				campo = trim(campo)
				if ((!regexpcodfiscita.test(campo)) && (!regexpivaitaliana.test(campo))) {
					colora('codfisc',1);
					errore = 1
					}
			}
		campo = indirizzo1
		if ( campo == '' || campo==null ) {
			colora('indirizzo1',1);
			errore = 1
			}
		campo = citta
		if ( campo == '' || campo==null ) {
			colora('citta',1);
			errore = 1
			}
		campo = prov
		if ( campo == '' || campo==null ) {
			colora('prov',1);
			errore = 1
			}
		campo = tel;
		if ( campo == '' || campo==null ) {
			colora('tel',1);
			errore = 1
			} else {
				campo = trim(campo)
				if (!regexptelefono.test(campo)) {
					colora('tel',1);
					errore = 1
					}
			}
		campo = email;
		if ( campo == '' || campo==null ) {
			colora('email',1);
			errore = 1
			} else {
				campo = trim(campo)
				if (!regexpemail.test(campo)) {
					colora('email',1);
					errore = 1
					}
			}
		campo = ragsoc;
		if ( campo != '' && campo!=null  ) {
				
			}


		if (errore == 1) {
			//window.alert(errore)
			return false;
		} else {
			//window.alert("vai")
			return true;
		}

	}
//function copia(id){
//		eval("valore=document.getElementById('"+id+"b').value")
//		if (valore == '' || valore == undefined) {
//			eval("valore=document.getElementById('"+id+"b').value = document.getElementById(id).value = valore;")
//		}
//	}
//

// compiloregistrazione
function copia(id){
	var nomecampob;
	eval("nomecampob = '"+id+"b'");
	if ((document.getElementById(nomecampob)) && (document.getElementById(nomecampob).value == "" || document.getElementById(nomecampob).value == undefined )) {
		document.getElementById(nomecampob).value = document.getElementById(id).value 
		}
}
///////// START CART //////////

function modificacart(id2carrello){
	eval("quanti = document.getElementById('quanti"+id2carrello+"').value");

	if (quanti != "" && quanti != undefined) {
		if (quanti == 0) {
			eval("document.getElementById('rigacarrello"+id2carrello+"').style.display = 'none'") ;
			}
		//window.alert(quanti)
		//window.alert(id2carrello)
		if(window.XMLHttpRequest)
			{
			   richiestaricerca = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
			   richiestaricerca = new ActiveXObject("Microsoft.XMLHTTP");
			}
			richiestaricerca.open("POST", "/ajax/ricalcolacarrello.asp", true);
			richiestaricerca.onreadystatechange = function() 
			{
				if(richiestaricerca.readyState == 4)
				{	
					if(richiestaricerca.status == 200)
					{
						rispostaricerca = richiestaricerca.responseText
						//window.alert (rispostaricerca)
						var mySplitResultb = rispostaricerca.split("###");
						prezzodacomunicare = mySplitResultb[0];
						quantitadacomunicare = mySplitResultb[1];
						totaleparzialedacomunicare = mySplitResultb[2];
						sfondodacomunicare = mySplitResultb[3];
						spedizioneprodotto = mySplitResultb[4];
						spedizioneprodottob = mySplitResultb[5];
						spedizioneprodottod = mySplitResultb[6];
						spedizioneprodottoc = mySplitResultb[7];
						spedizioneprodottoe = mySplitResultb[8];
						spedizioneprodottof = mySplitResultb[9];
						
						eval("document.getElementById('rigacarrello"+id2carrello+"').style.background = '#"+sfondodacomunicare+"'") ;
						eval("document.getElementById('prezzocarrello"+id2carrello+"').innerHTML = '"+prezzodacomunicare+"'") ;
						eval("document.getElementById('quanti"+id2carrello+"').value = '"+quantitadacomunicare+"'") ;
						eval("document.getElementById('totaleparziale"+id2carrello+"').innerHTML = '"+totaleparzialedacomunicare+"'") ;
						eval("document.getElementById('totaleacarello').innerHTML = '"+spedizioneprodottob+"'") ;
						eval("document.getElementById('ivatotaleacarello').innerHTML = '"+spedizioneprodottod+"'") ;
						eval("document.getElementById('spedizionecarello').innerHTML = '"+spedizioneprodotto+"'") ;
						eval("document.getElementById('accessoriecarello').innerHTML = '"+spedizioneprodottof+"'") ;
						eval("document.getElementById('supertotaleacarello').innerHTML = '"+spedizioneprodottoc+"'") ;
						
						if (spedizioneprodottoc == "0,00") {
							//location.href = "/";
							nascondi('carrellolaterale');
							settavalorehtml('bottonecarrello',"Carrello (0)");
							settavalorehtml('corpo',"Il carrello acquisti &egrave; vuoto");
							
							}
					}
				}
			}
			richiestaricerca.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			richiestaricerca.send("quanti="+quanti+"&id2="+id2carrello);
	}
}
function azzera(id2carrello){
		eval("document.getElementById('quanti"+id2carrello+"').value = 0");
		modificacart(id2carrello);
		eval("document.getElementById('rigacarrello"+id2carrello+"').style.display = 'none'") ;
}
function cambiopag(id2pagamento, id2carrello){
		if (id2pagamento == "rd") {
			document.getElementById('bloccodatispedizone').style.visibility = "hidden";
			document.getElementById('paroladatispedizione').style.visibility = "hidden";
		} else {
			document.getElementById('bloccodatispedizone').style.visibility = "visible";
			document.getElementById('paroladatispedizione').style.visibility = "visible";
		}
		if(window.XMLHttpRequest)
			{
			   richiestaricerca = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
			   richiestaricerca = new ActiveXObject("Microsoft.XMLHTTP");
			}
			richiestaricerca.open("POST", "/ajax/ricalcolacarrello.asp", true);
			richiestaricerca.onreadystatechange = function() 
			{
				if(richiestaricerca.readyState == 4)
				{	
					if(richiestaricerca.status == 200)
					{
						rispostaricerca = richiestaricerca.responseText
						var mySplitResultb = rispostaricerca.split("###");
						spedizioneprodotto = mySplitResultb[4];
						spedizioneprodottoc = mySplitResultb[7];
						spedizioneprodottof = mySplitResultb[9];
						
						eval("document.getElementById('spedizionecarello').innerHTML = '"+spedizioneprodotto+"'") ;
						eval("document.getElementById('supertotaleacarello').innerHTML = '"+spedizioneprodottoc+"'") ;
						eval("document.getElementById('accessoriecarello').innerHTML = '"+spedizioneprodottof+"'") ;
						//window.alert(rispostaricerca)
					}
				}
			}
			richiestaricerca.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			//window.alert ("pagamento="+id2pagamento+"&id2="+id2carrello)
			richiestaricerca.send("pagamento="+id2pagamento+"&id2="+id2carrello);
}
///////// END CART ////////////


////apre in target_blankl
////apertura link balnk sennza target
/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/*
Create the new window
*/
function openInNewWindow() {
	// Change "_blank" to something like "newWindow" to load all links in the same new window
    var newWindow = window.open(this.getAttribute('href'), '_blank');
    newWindow.focus();
    return false;
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		//var strNewWindowAlert = "" //" (opens in a new window)";
		var strNewWindowAlert = "";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var strWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\btargetblank\b/.exec(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				objWarningText = document.createElement("em");
				strWarningText = document.createTextNode(strNewWindowAlert);
				objWarningText.appendChild(strWarningText);
				link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

addEvent(window, 'load', getNewWindowLinks);////fine apre target blank//////////////
