function Trim(stringa) {
reTrim=/\s+$|^\s+/g;
return stringa.replace(reTrim,"");
}

function isEmail(string) {
if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
return 1;
else
return 0;
}

function checkForm() {
		var continua = true;
	
		document.getElementById('nome_mittente').className = 'input_abbandono';
		document.getElementById('email_mittente').className = 'input_abbandono';
		document.getElementById('nome_destinatario').className = 'input_abbandono';
		document.getElementById('email_destinatario').className = 'input_abbandono';
		document.getElementById('uword').className = '';
		
			if(jcap() == false) {
    document.getElementById('uword').className = 'errore_input';
		continua = false;
		document.getElementById('uword').focus();
    }
		
		
		    if (Trim(document.getElementById('email_destinatario').value) == "") {
		document.getElementById('email_destinatario').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('email_destinatario').focus();
		}


		if(!isEmail(document.getElementById('email_destinatario').value)) {
    document.getElementById('email_destinatario').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('email_destinatario').focus();
}		
		    if (Trim(document.getElementById('nome_destinatario').value) == "") {
		document.getElementById('nome_destinatario').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('nome_destinatario').focus();
		}
		
		    if (Trim(document.getElementById('email_mittente').value) == "") {
		document.getElementById('email_mittente').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('email_mittente').focus();
		}
		
				if(!isEmail(document.getElementById('email_mittente').value)) {
    document.getElementById('email_mittente').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('email_mittente').focus();
}
		
		    if (Trim(document.getElementById('nome_mittente').value) == "") {
		document.getElementById('nome_mittente').className = 'input_abbandono errore_input';
		continua = false;
		document.getElementById('nome_mittente').focus();
		}


		return continua;
	}


function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )    return "";
else
return results[1];
}


function do_sticker() {
valore = encrypt(Trim(document.getElementById('stk').value), "gtel");
location.href="/gol/sito_pubblico/restyle/stickers.jhtml?stk="+valore;
}

//funzione che preleva il querystring
function getquerystring(key) {
valori = new Array();
if(location.search) {
var querystring = unescape(location.search);
querystring = querystring.split(";jsessionid");
var query = querystring[0];
query = query.substr(1);
query = query.split("&");
for(i = 0; i < query.length; i++) {
var value = query[i].toString();
value = value.split("="); 
valori[value[0]] = value[1];
}
}

//è stato creato un array associativo chiamato valori che contiene ogni associazione chiave=>valore della querystring

if(valori[key]) {
return valori[key];
}
}



function Popup(apri) {
window.open(apri, "", "top=50, left=230, width=550, height=250, status=yes, menubar=no, toolbar=no scrollbar=no");
}


function Popup_rid(apri, width, height) {
window.open(apri, "", "top=50, left=230, width="+width+", height="+height+", status=yes, menubar=no, toolbar=no scrollbar=no");
}

// Sostituisce l'argomento target delle ancore, che in xthml non è previsto.
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
// Versione italiana.
if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "esterno") {
anchor.target = "_blank";
if (anchor.title) anchor.title += " (Il link apre una nuova finestra)";
if (!anchor.title) anchor.title = "Il link apre una nuova finestra";
}
}
}

// browser detection
var BV=parseInt(navigator.appVersion.indexOf("MSIE")>0?navigator.appVersion.split(";")[1].substr(6):navigator.appVersion);
var BN=window.navigator.appName;
var IsWin=(navigator.userAgent.indexOf('Windows')!=-1)?true:false;
var OP=(navigator.userAgent.indexOf('Opera')!=-1&&BV>=4)?true:false;
var NS=(BN.indexOf('Netscape')!=-1&&(BV==4)&&!OP)?true:false;
var SM=(BN.indexOf('Netscape')!=-1&&(BV>=5)||OP)?true:false;
var IE=(BN.indexOf('Explorer')!=-1&&(BV>=4)||SM)?true:false;


//anti email-spam
var maildivider="[at]"; //enter divider you use to divide your email address strings
function email(){
for (i=0; i<=(document.links.length-1); i++){
if (document.links[i].href.indexOf(maildivider)!=-1){
document.links[i].href=document.links[i].href.split(maildivider)[0]+"@"+document.links[i].href.split(maildivider)[1];
}
}
}


//return url arguments 
function getURLArguments(url) {
			var args=new Array();
    var query_str = url.substring(url.indexOf('#') + 1);
							var args_arr = query_str.split('=');
							for (var i = 0; i < args_arr.length; i++) {
									args[i]= args_arr[i];
							}
							return args;
  }

//return current url without '#'
function getCurrentUrl() {
	 url_root = this.document.location.href;
	 url_path = url_root.substring(0, url_root.indexOf('#'));
		return url_path;
	}

//change "current" link layout
function setCurrentPage(id, newClass) {
url_path=getCurrentUrl();
var currentPage =(url_path!="")? url_path:window.location.href;
var navigationContainer = document.getElementById(id);
var navBarLinkCollection = navigationContainer.getElementsByTagName('a');
var parentContainer=(newClass=='subcurrent')?navigationContainer.parentNode:'';
var j;

if(newClass == 'out') {
navigationContainer.parentNode.firstChild.className='current';
}

for (j=0;j < navBarLinkCollection.length;j++) {
	if (currentPage == navBarLinkCollection[j].href) {
				if (id=='rightBar'){
								//cambio la classe al tag <span> 
								navBarLinkCollection[j].firstChild.className = newClass;
					}
					else{
								//cambio la classe al tag <a>
										globNum=(id=='globalNav')?j:'';
										navBarLinkCollection[j].className = newClass+globNum;
										if (parentContainer!=''){
													navigationContainer.parentNode.firstChild.className='current';
													}
						}
		}
}
    

} 

/*get url variable*/
function parseQueryString (str) {
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) {
      var field = fields[f].split('=');
      args[unescape(field[0].replace(/\+/g, ' '))] = 
unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}

  
function seguiLink(obj){
	var link = obj[obj.selectedIndex].value;
	window.location = link;
	}

/*breadcrumb*/
function breadcrumbs(name){
sURL = new String;
bits = new Array();
var x = 0;
var stop = 0;
var output = "<strong>Siete qui</strong>: <a href=\"/\">Home</a> &raquo; ";
sURL = location.href;

//aggiunto per non mostrare parametri o valori di pb passati con url
var indexQuestion = sURL.indexOf('?');
if(indexQuestion > -1){
sURL = sURL.substring(0, indexQuestion);
}

sURL = sURL.replace('http://', '');


chunkStart = sURL.indexOf("/");
sURL = sURL.slice(chunkStart+1,sURL.length);
chunkStart = sURL.indexOf("/");
sURL = sURL.slice(chunkStart+1,sURL.length);
while(!stop){
chunkStart = sURL.indexOf("/");
if (chunkStart != -1){
if ((sURL.slice(0,chunkStart) != 'assistenza')) {
bits[x] = sURL.slice(0,chunkStart) 
}
sURL = sURL.slice(chunkStart+1,sURL.length);
}else{
stop = 1;
}
x++;
}

if(Trim(name) != "") {
for(var i in bits){
if(bits[i] != 'vantaggi') {
output += "<a href=\"";
for(y=1;y<x-i;y++){
output += "../";
}
if(bits[i] == 'dicono-di-noi'){        
output += "dicono_di_noi.jhtml\">" + bits[i].replace('_',' ').replace('-',' ').replace('-',' ')+ "</a>  &raquo; ";
} else {
output += bits[i] + "/index.jhtml\">" + bits[i].replace('_',' ').replace('-',' ').replace('-',' ') + "</a>  &raquo; ";
}
}
}
} else {
for(i = 0; i < bits.length-1 ; i++){
if(bits[i] != 'vantaggi') {
output += "<a href=\"";
for(y=1;y<x-i;y++){
output += "../";
}
output += bits[i] + "/index.jhtml\">" + bits[i].replace('_',' ').replace('-',' ').replace('-',' ') + "</a>  &raquo; ";
}
}
output += bits[bits.length-1].replace('_',' ').replace('-',' ').replace('-',' ');
}
document.write(output + "<span  class='grey'>" + name + "</span>");
}

/****************************************************
Styleswitcher
*****************************************************/
// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	WM_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}
			
function fontsizechange() {
if(WM_acceptsCookies==false){alert('Per utilizzare questa funzione i cookies devono essere abilitati');}
  myactive = getActiveStyleSheet();
  switch (myactive) {
    case 'A' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      setActiveStyleSheet('A++');
      break;
    case 'A++' : 
      setActiveStyleSheet('A');
      break;
		
				default :
      setActiveStyleSheet('A+');		
		}		
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

//window.onload CARICATO IN HEAD ma sembra non servire visto che le ultime tre righe fanno le stesse cose

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

/****************************************************
*****************************************************/

var xmlDoc;
var domanda;
var element;
var element2;
var element3;
var scelte=new Array();
var i=0;
var first=0;
var old;
var numero;

stato = new Array;

function loadXML()
{
//load xml file
// code for IE


if (window.ActiveXObject)
  {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("note.xml");
  getMessage()
  }
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
  {
  xmlDoc= document.implementation.createDocument("","",null);
  xmlDoc.load("note.xml");
  xmlDoc.onload=getMessage
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}function getMessage()
{
	
disattivaLinkAvanti()
disattivaLinkIndietro()
domanda = xmlDoc.childNodes[0].childNodes[0];

stato[i] = domanda;
i++;

visualizzaRisposte(domanda)

}



function nextQuestion(direzione) {
	
	disattivaLinkAvanti()
	
	attivaLinkIndietro()
	
	if (direzione == 1) {
		
		i--;
		i--;
		
		if(i == 0) {
			disattivaLinkIndietro()
		}
		
		domanda = stato[i];
		visualizzaRisposte(domanda)
		
	}
	if (numero == 1) {   
		domanda = domanda.nextSibling.childNodes[1].childNodes[0];
		}
	else if (numero == 2) {
		domanda = domanda.nextSibling.nextSibling.childNodes[1].childNodes[0];
		}
	else if (numero == 3) {
		domanda = domanda.nextSibling.nextSibling.nextSibling.childNodes[1].childNodes[0];
		}	
		stato[i] = domanda;
		i++;
		visualizzaRisposte(domanda)
	
}


function visualizzaRisposte(domanda) {
	
		document.getElementById('domanda').innerHTML=domanda.nodeValue
		element = domanda.nextSibling.childNodes[0];
		if (element != null) {
			document.getElementById('risposta1').innerHTML=element.nodeValue
			document.getElementById('radio1').checked = false 
		}
		else {
		document.getElementById('risposta1').innerHTML=""
		}
		element2 = domanda.nextSibling.nextSibling.childNodes[0];
		
		if (element2 != null) {
			document.getElementById('risposta2').innerHTML=element2.nodeValue
			document.getElementById('radio2').checked = false
		}
		else {
		document.getElementById('risposta2').innerHTML=""
		}
		
		if (domanda.nextSibling.nextSibling.nextSibling != null) {
			element3 = domanda.nextSibling.nextSibling.nextSibling.childNodes[0];
			document.getElementById('risposta3').innerHTML=element3.nodeValue
			document.getElementById('radio3').checked = false
			document.getElementById('radio3').style.visibility = 'visible';
		}
		else {
		document.getElementById('risposta3').innerHTML=""
		document.getElementById('radio3').style.visibility = 'hidden'; 
		
		}
		
		numero = 0;
}

function attivaLinkAvanti() {
	
	if (numero == 1) {
		if (domanda.nextSibling.childNodes[1].nodeName == 'link') {
			document.getElementById('linkavanti').innerHTML=domanda.nextSibling.childNodes[1].firstChild.data;
		}
		
	else {
		document.getElementById('linkavanti').innerHTML="<a href='#' id='avanti' onclick='nextQuestion(2)'>avanti ></a>";
		}
	}
	
	if (numero == 2) {
		if (domanda.nextSibling.nextSibling.childNodes[1].nodeName == 'link') {
			document.getElementById('linkavanti').innerHTML=domanda.nextSibling.nextSibling.childNodes[1].firstChild.data;
		}
		
	else {
		document.getElementById('linkavanti').innerHTML="<a href='#' id='avanti' onclick='nextQuestion(2)'>avanti ></a>";
		}
	}
	
	if (numero == 3) {
		if (domanda.nextSibling.nextSibling.nextSibling.childNodes[1].nodeName == 'link') {
			document.getElementById('linkavanti').innerHTML=domanda.nextSibling.nextSibling.nextSibling.childNodes[1].firstChild.data;
		}
		
	else {
		document.getElementById('linkavanti').innerHTML="<a href='#' id='avanti' onclick='nextQuestion(2)'>avanti ></a>";
		}
	}
	
	
	
}

function disattivaLinkAvanti() {
	document.getElementById('linkavanti').innerHTML='avanti';
}

function attivaLinkIndietro() {
	document.getElementById('linkindietro').innerHTML="<a href='#' id='indietro' onclick='nextQuestion(1)'>< indietro</a>";
}

function disattivaLinkIndietro() {
	document.getElementById('linkindietro').innerHTML='indietro';
}

function attivaLinkScheda() {
	document.getElementById('linkavanti').innerHTML='';
}




function changeParent(link) {
	window.opener.location.href=link;
	window.close();
}
	
function change(picture) { // cambia immagine al soddisfatti o rimborsati
document.getElementById('soddisfatti_img').src = picture;
}




//inizio criptazione
function encrypt(str, pwd) {
  if(pwd == null || pwd.length <= 0) {
    //alert("Please enter a password with which to encrypt the message.");
    return null;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.ceil(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  if(mult < 2) {
    //alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
    return null;
  }
  var salt = Math.round(Math.random() * 1000000000) % 100000000;
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if(enc_chr < 16) {
      enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
  }
  salt = salt.toString(16);
  while(salt.length < 8)salt = "0" + salt;
  enc_str += salt;
  return enc_str;
}

function decrypt(str, pwd) {
  if(str == null || str.length < 8) {
  //  alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
    return;
  }
  if(pwd == null || pwd.length <= 0) {
   // alert("Please enter a password with which to decrypt the message.");
    return;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i+=2) {
    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str;
}

///fine criptazione


