var formSent='no';
function isSent()
{
	if(formSent=='no')
	{
		formSent='yes';
		return true;
	}
	else
	return false;
}

function showMessages(stringa)
{
	alert(stringa);
}

function popupFlash(ogetto,width,height)
{
if(typeof(myFlash)!='undefined')
myFlash.close();
myFlash=window.open(ogetto,'flash','width='+width+'px,height='+height+'px,toolbar=no,top:150,left:150');
myFlash.focus();
return false;
}

function sendForm(formId,inputId)
{
	var Form=document.getElementById(formId);
	if(!Form.ajax)
		return true;
	Form.ajax.value='yes';
	var myForm=Form.elements;
	var vars='';
	if(Form.ajax && Form.ajax.value=='yes')
	{
	for(var i = 0; i < myForm.length; i++)
	{
		if(typeof(myForm[i].value)!='undefined')
		{
			if(!(myForm[i].type=='checkbox' && myForm[i].checked==false))
			vars+=((vars!='')?'&':'')+myForm[i].name+'='+escape(myForm[i].value);
		}
	}
	if(inputId)
	{
	if(document.getElementById(inputId) && typeof(inputId)=='string')
		document.getElementById(inputId).value='';
	else
	{
		for(var i=0;i<inputId.length;i++)
			document.getElementById(inputId[i]).value='';
	}
	}
		rsconnect2('POST',Form.action,vars);
		Form.ajax.value='no';
		return false;
	}
	else
		return true;
}

function ascii2script(stringa)
{
	/*HEXADECIMAL ASCII CONVERSION*/
	if(stringa!='')
	{
	stringa = stringa.replace(/&deg;/g,'\xB0');
	stringa = stringa.replace(/&Agrave;/g,'\xC0');
	stringa = stringa.replace(/&agrave;/g,'\xE0');
	stringa = stringa.replace(/&Aacute;/g,'\xC1');
	stringa = stringa.replace(/&pound;/g,'\xA3');
	stringa = stringa.replace(/&ccedil;/g,'\xE7');
	stringa = stringa.replace(/&aacute;/g,'\xE1');
	stringa = stringa.replace(/&Egrave;/g,'\xC8');
	stringa = stringa.replace(/&egrave;/g,'\xE8');
	stringa = stringa.replace(/&Eacute;/g,'\xC9');
	stringa = stringa.replace(/&eacute;/g,'\xE9');
	stringa = stringa.replace(/&Igrave;/g,'\xCC');
	stringa = stringa.replace(/&igrave;/g,'\xEC');
	stringa = stringa.replace(/&Iacute;/g,'\xCD');
	stringa = stringa.replace(/&iacute;/g,'\xED');
	stringa = stringa.replace(/&Ograve;/g,'\xD2');
	stringa = stringa.replace(/&ograve;/g,'\xF2');
	stringa = stringa.replace(/&Oacute;/g,'\xD3');
	stringa = stringa.replace(/&oacute;/g,'\xF3');
	stringa = stringa.replace(/&Ugrave;/g,'\xD9');
	stringa = stringa.replace(/&ugrave;/g,'\xF9');
	stringa = stringa.replace(/&Uacute;/g,'\xDA');
	stringa = stringa.replace(/&uacute;/g,'\xFA');
	stringa = stringa.replace(/&amp;/g,'\x26');
	stringa = stringa.replace(/&amp;#039;/g,'\x27'); /*non funziona altrimenti*/
	stringa = stringa.replace(/&#039;/g,'\x27');
	stringa = stringa.replace(/&quot;/g,'\x22');
	stringa = stringa.replace(/&lt;/g,'\x3c');
	stringa = stringa.replace(/&gt;/g,'\x3e');
	stringa = stringa.replace(/<br \/>/g,'\n');
	stringa = stringa.replace(/<br>/g,'\n');
	stringa = stringa.replace(/<b>/g,'');
	stringa = stringa.replace(/<\/b>/g,'');
	return stringa;
	}
	else
	return '';
}


var html2script= ascii2script;

var rsconnection=false;
function rsconnect2 (tipo,myUrl,myVars,myReferer)
{
	rsconnection=false;
	try
	{
		rsconnection=new XMLHttpRequest();	/*mozilla, firefox, safari, netscape*/
	}
	catch(e)			/*microsoft recente*/
	{
		try
		{
			rsconnection=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)			/*microsoft vecchio*/
		{
			try
			{
				rsconnection=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)			/*altrimenti*/
			{
				rsconnection=false;
			}
		}
	}
	if(!rsconnection)
		return;
	rsconnection.onreadystatechange=rscontrol2;
	rsconnection.open(tipo, myUrl, true);
	rsconnection.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
	if(tipo == 'POST')
		rsconnection.send(myVars);
	else
		rsconnection.send(null);
}

function rscontrol2 ()
{
	if(rsconnection.readyState==4)
	{
		var myHtml = rsconnection.responseText;
		if(myHtml!='' && myHtml=='changeimage')
		{
			if(typeof(myImmagine)=='object')
			{
				var cc=1;
				var fine='no';
				while(fine!='yes')
				{
					if(document.getElementById(myImmagine[1]+cc))
					{
						++myImgCase;
						document.getElementById(myImmagine[1]+cc).src=myImmagine[0]+myImgCase+'.jpg'+'?'+myImmagine[1]+'='+cc;
						++cc;
					}
					else
						fine='yes';
				}
			}
		}
		else if(myHtml!='')
		{
		/*alert(myHtml);*/
		eval(myHtml);
		}
	}
}

function getkey(e)
{
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return false;
}

function check_int(e,valore)
{
	var key_code=getkey(e);
	var key_str=String.fromCharCode(key_code);
	var key_num=parseInt(key_str);
	if(key_str!= key_num && key_code!=8 && key_code!=17 && key_code!=18 && key_code!=19 && key_code!=20 && key_code!=0)
		return false;
	else
		return true;
}

function check_float(e,valore,n)
{
	var key_code=getkey(e);
	var key_str=String.fromCharCode(key_code);
	var key_num=parseFloat(key_str);
	if((key_str!= key_num && key_code!=8 && key_code!=46 && key_code!=17 && key_code!=18 && key_code!=19 && key_code!=20 && key_code!=0))/* || (valore.value=='' && key_code==48))*/
		return(false);
	else
	{
		if(key_code!=8 && key_code!=0)
		{
			var newstr=valore+key_str;
			var newsplit=newstr.split('.');
			if(newsplit.length>2)
				return(false);
			else
			{
				if(newsplit.length==2)
				{
					if(typeof(n)!='undefined' && newsplit[1].length>n)
						return(false);
				}
			}
		}
		return true;
	}
}

function conv_price(e,objvalore,n)
{
	valore = objvalore.value;
	if (valore.indexOf(",") != -1)
		valore = valore.replace(/,/g, ".");
	rate = 100 + 20;
	price = number_format(valore * 100 / rate,2,'.','');
	objvalore.value = price;
}

function MouseCoordinates(e)
{
	if (e.layerX)
	{
		x = e.layerX;
		y = e.layerY;
	}
	else if (e.offsetX)
	{
		x = e.offsetX;
		y = e.offsetY;
	}
	var position=new Array(x,y);
		return position;
}

function createLabel(padre,id,testo,myStyle)
{
	var myLabel=document.createElement('label');
	myLabel.setAttribute('for',id);
	myLabel.innerHTML=testo;
	if(typeof(myStyle)!='undefined')
	myLabel.className=myStyle;
	else
	myLabel.className='dynamicLabel';
	padre.appendChild(myLabel);
	return myLabel;
}

function create(padre,myElement,props)
{
	if(document.all)
	{
		var attributes='';
		if(typeof(props)!='undefined')
		{
			for(i in props)
				attributes+=' '+i+'="'+props[i]+'"';
		}
		var myNew=document.createElement('<'+myElement+attributes+((myElement=='input')?' />':'>'));
	}
	else
	{
		var myNew=document.createElement(myElement);
		for(i in props)
			myNew.setAttribute(i,props[i]);
	}
	if(padre!='')
	{
	padre.appendChild(myNew);
	return myNew;
	}
	else
	return myNew;
}

function testNodes(padre,figlio)
{
	if(typeof(padre)=='object')
	var arrayNodes=padre.childNodes;
	else
	var arrayNodes=document.getElementById(padre).childNodes;
for(var i in arrayNodes)
{
if(arrayNodes[i].id==figlio)
return true;
}
return false;
}

function removeChilds(padre,figlio)
{
	if(typeof(padre)=='object')
	var myNodes=padre;
	else
	{
	if(document.getElementById(padre))
	var myNodes=document.getElementById(padre);
	else
	return false;
	}
	var arrayNodes=myNodes.childNodes;
		if(typeof(figlio)!='undefined')
		{
			for(var i in arrayNodes)
			{
			if(typeof(arrayNodes[i])=='object' && figlio==arrayNodes[i].id)
			myNodes.removeChild(arrayNodes[i]);
			}
		}
		else
		{
		var uscita='no';
			while(arrayNodes[0] && uscita=='no')
			{
				var c=0;
				for(var i in arrayNodes)
				{
				if(typeof(arrayNodes[i])=='object')
				{
				document.getElementById(padre).removeChild(arrayNodes[i]);
				++c;
				}
				}
				if(c==0)
				uscita='yes';
			}
		}
	return true;
}

function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }

  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ?
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();

  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ?
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }

  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ?
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
  for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }

  return sign + integer + fractional + exponent;
}

function toEuro(number, decimals, dec_point, thousands_sep)
{
return number_format (number, decimals, dec_point, thousands_sep)+' '+html2script('&euro;');
}

function visibile (id, stato) {

	if (document.getElementById && document.getElementById(id)) {
		var dom = document.getElementById(id);
		if (stato == 'si') dom.style.display = 'block';
		else if (stato == 'no') dom.style.display = 'none';
	}
	return;
	
}

function destination(value) {

	var dom1 = document.getElementById('pagamento');
	var dom2 = document.getElementById('spese1');
	var dom3 = document.getElementById('areaSpese');
	if (value == 'gift')
		visibile('indirizzoSped','si');
	else visibile('indirizzoSped','no');
	visibile('areaSpese','si');
	dom1.selectedIndex = 0;
	if (dom2) {
		dom2.innerHTML = '';
		visibile('spese1','no');
	}
	if (dom3) dom3.selectedIndex = 0;
}
	
function calcoloSpese(obj, tipo) 
{
var totgra = document.getElementById('totordinegraph');
var totnum = document.getElementById('totordine');
var dom2 = obj;
var dom3 = document.getElementById('assicurata');
var dom4 = document.getElementById('contras');

tot = 0;
sped = 0;


switch (tipo) {
	case 'reset':
		tot = totnum.value;
		var sp = {value:null};
		var cs = {value:0};
		dom3.checked = false;
	break;

	case 'nospese':
		if (obj=="Ritiro / pagamento in negozio") stato = 'no';
		else stato = 'si';
		visibile ('spese1',  stato);
		visibile ('spese2',  stato);
		visibile ('indirizzo3',  stato);
		dom3.checked = false;
		tot = totnum.value;
	break;

	case 'add':
		if (dom2.value != 0) costo = dom2.value.split('|');
		else costo = 0;
		if (dom3.checked == true) ass = parseFloat(dom3.value);
		else ass = 0;
		tot = parseFloat(totnum.value) + ass + parseFloat(dom4.value) + parseFloat(costo[2]);
		/*var domS = document.getElementById('spedizione');
		domS.value = costo[1];*/
	break;

	case 'assicurat':
		//alert(document.forms['myCartForm'].spese[0] +" "+document.forms['myCartForm'].spese[1]);
		if (document.forms['myCartForm'].spese.value != 0) {
			costo = document.forms['myCartForm'].spese.value.split('|');
			spese = parseFloat(costo[2]);
		}
		else spese = 0;
		if (obj.checked == false) 			
			tot = parseFloat(totnum.value) + spese + parseFloat(dom4.value);
		else 
			tot = parseFloat(totnum.value) + spese +  parseFloat(dom4.value) + parseFloat(obj.value);
	break;
	
	case 'assicura':
		//alert(document.forms['myCartForm'].spese[0] +" "+document.forms['myCartForm'].spese[1]);
		if (document.forms['myCartForm'].spese.value != 0) {
			for (i=1; i <= document.forms['myCartForm'].spese.length; i++) {
				if (document.forms['myCartForm'].spese[i] && document.forms['myCartForm'].spese[i].checked) {
					costo = document.forms['myCartForm'].spese[i].value.split('|');
					spese = parseFloat(costo[2]);
					break;
				}
				else spese = 0;
			}
		}
		else spese = 0;
		if (obj.checked == false) 			
			tot = parseFloat(totnum.value) + spese;
		else 
			tot = parseFloat(totnum.value) + spese +  parseFloat(obj.value);
	break;
	case 'contras':
		tot = parseFloat(totnum.value) + parseFloat(obj.value);
	break;
}
totgra.innerHTML = toEuro(tot,2,',','.');
} 

function rightCost2(cont,dest,netto) {

	var value = 'Anticipato';
	document.getElementById('proceed').style.display='none';
	if (dest) p = dest.value;
	else p = "";
	rsconnect2('POST','response/spedizione_ajax.php','&val='+value+'&c='+cont+'&p='+p+'&n='+netto);
				
}

function rightCost(value, numcosti) {
	
var Costdom = document.getElementById('spese1');
if (value != 0) {
	var tipo = (value=="Contrassegno") ? value : ((value=="Ritiro / pagamento in negozio") ? 0 : 'Anticipato');

	for (i=0; i <= numcosti; i++) {

		if (document.getElementById('spese_'+i).value.indexOf(tipo) == -1) {
			document.getElementById('spese_'+i).className='radio nascondi';
			document.getElementById('spese_'+i).disabled = true;
		}
		else {
			document.getElementById('spese_'+i).className='radio mostra';
			document.getElementById('spese_'+i).disabled = false;
		}
	}
	if (tipo != 0) {
		Costdom.style.display = 'block';
		visibile ('spese2', 'si');
		visibile ('indirizzo3', 'si');
		if (document.forms['myCartForm'].spese) {
			for (i=0; i <= document.forms['myCartForm'].spese.length; i++)
				if (document.forms['myCartForm'].spese[i] && document.forms['myCartForm'].spese[i].checked)
						document.forms['myCartForm'].spese[i].checked = false;
		}
		calcoloSpese('', 'reset');
	}
	else {
		Costdom.style.display = 'none';
		calcoloSpese(value, 'nospese');
	}
 }
 else {
		Costdom.style.display = 'none';
 		document.getElementById('spese2').style.display = 'none';
	}
}


function switchSearch (obj) {
	value = obj.options[obj.selectedIndex].value;
	for (i=1; i <= obj.options.length-1; i++)
		if (i == value) visibile('search'+i, 'si');
		else visibile('search'+i, 'no');
}

var jsloaded='yes';