/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*					UTILITY GENERALI PER IL FUNZIONAMENTO DEL SITO INTERNET			*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function matchRegExp(e, reg)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	
	if (key == 8 || key == 0)
		return true;
	else
		return reg.test(keychar);
}
function returnNumsOnly ( e ) {
	return matchRegExp(e, /[/0-9]/);
}

//-----------------------------------------------------
//valuta il codice javascript e restituisce la stringa
//senza codice
//-----------------------------------------------------
function execJS(t)
{
    var p1 = 0, p2 = 0, p3 = 0, p4 = 0;
    p1 = t.indexOf("<" + "script", 0);
    if(p1 == -1) return t;
    
    p2 = t.indexOf(">", p1 + 7) + 1;
    p3 = t.indexOf("<" + "/script>", p2);
    p4 = p3 + 9;
    
    var c = t.substring(p2, p3);
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.text = c;
    document.getElementsByTagName("head")[0].appendChild(s);
    
    t = t.substring(0, p1) + t.substr(p4);
    return execJS(t);

}

function changeHeightIframe(iframe)
{
	try
	{
		var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
		if (innerDoc.body.offsetHeight) //ns6 syntax
		{
			if (navigator.appName.search('Netscape') != -1)
				iframe.height = innerDoc.body.offsetHeight + 12 + 'px'; // NETSCAPE (FIREFOX)
			else
				if (navigator.appName.search('Internet Explorer') != -1)
					iframe.height = innerDoc.body.scrollHeight + 12 + 'px'; // INTERNET EXPLORER
			
			//alert('1: '+iframe.height);
			//alert(navigator.appName);
		}
		else if (iframe.Document && iframe.Document.body.scrollHeight) //ie5+ syntax
		{
			iframe.height = iframe.Document.body.scrollHeight;
			//alert('2: '+iframe.Document.body.scrollHeight);
		}
	}
	catch(err)
	{
		//alert(err.message);
	}
}

// controlla se l'elemento passato è nullo o no
function elementIsNotNull(elementName)
{
	var element = document.getElementById(elementName);
	if (element != 'undefined' && element != null && element != 'null')
	{
		return true;
	}
	
	return false;
}
// apre una finestra popup centrata
function newWindow_centered(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen)
{
	var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
	//var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	var int_windowTop = 0;
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',directories=0,titlebar=0,resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
	if (parseInt(navigator.appVersion) >= 4)
	{
		obj_window.window.focus();
	}
}
// ricevi elemento
function GetBrowser()
{
	var element = 'unknown';
	if (document.all)
		element = 'IE';
	else
		if (document.layers)
			element = 'Netscape4';
		else
			element = 'FireFox';
	
	return element;
}
// ricevi elemento
function GetElement(id)
{
	if (document.all)
		element = document.all[id];
	else
		if (document.layers)
			element = document.layers[id];
		else
			element = document.getElementById(id);
	
	return element;
}
// permette di accedere al documento dell'iframe in base al browser utilizzato
function iframeDocument(iframe)
{
	if(iframe.contentDocument)  
		doc = iframe.contentDocument;  // Firefox, Opera  
	else if(iframe.contentWindow)  
		doc = iframe.contentWindow.document; // Internet Explorer  
	else if(iframe.document)  
		doc = iframe.document; // Others?  
	
	return doc;
}
// permette di accedere al documento dell'iframe in base al browser utilizzato
function iframeWindow(iframe)
{
	if(iframe.contentWindow)  
		doc = iframe.contentWindow;  // Firefox, Opera  , Internet Explorer  
	else if(iframe.window)  
		doc = iframe.document; // Others?  
	
	return doc;
}
// ricava il parametro dalla pagina
function GetParameters(parameter)
{
	queryString = location.search;
	//var paramRegExp = new RegExp("[\\?&]" + parameter + "=(\\w*)");
	var paramRegExp = new RegExp("[\\?&]"+parameter+"=([^&#]*)");
	var result = queryString.match(paramRegExp);
	return result == null ? null : result[1];
}
// gestisce il posizionamento del mouse su un controllo input (vedi portlet login)
function Select_InputText(element, nome_verifica, style)
{
	switch (element.getAttribute('textsel'))
	{
		case "true":
		{
			element.setAttribute('textsel', 'false');
			
			if (element.value == '')
			{
				element.setAttribute('style', style);
				element.value = nome_verifica;
			}
			
			break;
		}
		default:
		{
			element.setAttribute('textsel', 'true');
			
			if (element.value == nome_verifica)
			{
				element.setAttribute('style', style);
				element.value = "";
			}
			
			break;
		}
	}
}
function strcmp ( str1, str2 ) {
    // http://kevin.vanzonneveld.net
    // +   original by: _argos
    // +      input by: Steve Hilder
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: gorthaur
    // *     example 1: strcmp( 'waldo', 'owald' );
    // *     returns 1: 1
    // *     example 2: strcmp( 'owald', 'waldo' );
    // *     returns 2: -1
 
    return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
}
// ritorna se sei su un mobile o su un pc
function isMobile()
{
	var width = window.screen.width;
	var height = window.screen.height;
	var risultato = width * height;
	var controllo = 800 * 600;
	
	if (risultato < controllo)
		return true;
		
	return false;
}

var homepageSG = "http://stage-portal.superenalotto.it/portal/page/portal/SitoGioco/superenalotto";
var homepageSGMobile = "http://stage-portal.superenalotto.it/portal/page/portal/SitoGioco_Mobile/homepage";
// ritorna se sei su un mobile o su un pc
function switchToMOBILE()
{
	var width = window.screen.width;
	var height = window.screen.height;
	var risultato = width * height;
	var controllo = 800 * 600;
	
	if (risultato < controllo)
		location.href = homepageSGMobile;
	else
		return false;
}

// ritorna la soluzione dello schermo
function getScreenResolution()
{
	try
	{
		var width = window.screen.width;
		var height = window.screen.height;
		if ((width == 800) && (height == 600))
		{
			document.getElementById('spazio_cuscinetto').style.display="block";	
		}
		else
		{
			document.getElementById('spazio_cuscinetto').style.display="none";	
		}
	}
	catch(err)
	{
		//alert("getScreenResolution \n"+err);
	}
}

// ritorna la soluzione dello schermo (versione apa)
function getScreenResolutionApa()
{
	try
	{
		var width = window.screen.width;
		var height = window.screen.height;
		if ((width == 800) && (height == 600))
		{
			document.getElementById('spazio_cuscinetto_apa').style.width="0px";
			document.getElementById('spazio_cuscinettoBottom_apa').style.width="343px";
			//document.getElementById('portalIFrame_PortletLogin').style.width="430px";
		}
	}
	catch(err)
	{
		//alert("getScreenResolution \n"+err);
	}
}

function pngfix() 
{
	if (GetBrowser() == 'IE')
	{	
		ii=0;
		var imageArray = new Array;
		imageArray[ii++] = "star.png";
		imageArray[ii++] = "assvirt_nascondi.png";
		imageArray[ii++] = "pagePath_separator.png";
		imageArray[ii++] = "frecciaUp.png";
		imageArray[ii++] = "frecciaDown.png";
		imageArray[ii++] = "frecciaRight.png";
		imageArray[ii++] = "frecciaLeft.png";
		imageArray[ii++] = "loginBg.png";
		imageArray[ii++] = "logoutBg.png";
		imageArray[ii++] = "giocaIlGiusto.png";
		imageArray[ii++] = "giocoSicuro.png";
		imageArray[ii++] = "aams.png";
		imageArray[ii++] = "MyPage.png";
		imageArray[ii++] = "sfRegistrazioneBottom.png";
		imageArray[ii++] = "refresh_saldo.png";
		
		var els=document.getElementsByTagName('*');
		var ip=/\.png/i;
		var i=els.length;

		while(i-- >0)
		{
			var el=els[i];
			var es=el.style;
			
			if (el.src != undefined)
			{
				var elb=el.src;
				var type='src';
			} else
			{
				var elb=el.currentStyle.backgroundImage;
				var type='bg';
			}
			
			if (elb != undefined)
			{
				var indiceArray = 0;
				do
				{
					var ricerca = elb.search(imageArray[indiceArray]);
					indiceArray++;
				} while (indiceArray != imageArray.length && ricerca == -1);
			}
			else
				var ricerca = -1;
				
			// -1 => NON TROVATO
			if (ricerca != -1)
			{
				if(elb.match(ip))
				{
					if (type == 'src')
						elb = '"' + elb + '"';
						
					var path=elb.split('"');
					var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';
					es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";
					
					es.height=el.clientHeight+'px';
					es.backgroundImage='none';
					if (el.src != undefined)
						el.src='/sitogioco/Immagini/Top/empty.gif';
					var elkids=el.getElementsByTagName('*');
					
					if (elkids)
					{
						var j=elkids.length;
						if(el.currentStyle.position!="absolute")es.position='static';
							while (j-- >0)
								if(!elkids[j].style.position)elkids[j].style.position="relative";
					}
				}
			}
		}
	}
}

// ritorna il percorso passato completo di Url in HTTPS e dominio del sito
function returnHttpsUrl(percorso)
{
	var host = location.host; // dominio + porta
	var hostname = location.hostname;
	if (location.port != '')
		var port = ':' + location.port;
	else
		var port = '';
		
	var protocol = location.protocol.substr(0,location.protocol.length-1)
	
	if (protocol != 'https')
		protocol = 'https';

	return protocol+'://'+host+percorso;
}

// ritorna il percorso passato completo di Url con il protocollo utilizzato in quel momento nel sito
function returnUrlWithProtocol(percorso)
{
	var host = location.host; // dominio + porta
	var hostname = location.hostname;
	if (location.port != '')
		var port = ':' + location.port;
	else
		var port = '';
		
	var protocol = location.protocol.substr(0,location.protocol.length-1)

	return protocol+'://'+host+percorso;
}

function returnHttpsLocation(percorso)
{
	var newUrl = percorso;
	var protocolChar = percorso.substr(4,1);
	
	if ((protocolChar != 's') && (protocolChar != 'S'))
	{
		newUrl = "https" + percorso.substr(4,percorso.length-4);
	}
	
	return newUrl;
}

function returnHttpLocation(percorso)
{
	var newUrl = percorso;
	var protocolChar = percorso.substr(4,1);
	
	if ((protocolChar == 's') || (protocolChar == 'S'))
	{
		newUrl = "http" + percorso.substr(5,percorso.length-5);
	}
	
	return newUrl;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*						FUNZIONI PER LA GESTIONE DEI COOKIE DEL SITO - cookie.js					*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie(sNome, sValore, iGiorni)
{	
	//var sPath = "/portal/page/portal/SitoGioco/";
	var sPath = "/";
	var sDomain = "stage-portal.superenalotto.it";
	var dtOggi = new Date();
	var dtExpires = new Date();
	dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);

	// controllo della data di scadenza, se nulla imposto la scadenza a fine sessione
	var expires = '';
	if ((iGiorni != '') && (iGiorni != undefined) && (iGiorni != null))
		expires = " expires=" + dtExpires.toGMTString() + ";";
		
	//document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
	document.cookie = sNome + "=" + escape(sValore) + "; " + expires + " domain=" + escape(sDomain) + "; path=" + escape(sPath);
}
// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie_nopath(sNome, sValore, iGiorni)
{	
	var dtOggi = new Date()
	var dtExpires = new Date()
	dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000)

	//document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
	document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}
// restituisce il valore del cookie sNome
function getCookie(sNome)
{
	// genera un array di coppie "Nome = Valore"
	// NOTA: i cookies sono separati da ';'
	var asCookies = document.cookie.split("; ");
	// ciclo su tutti i cookies
	for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
	{
	    // leggo singolo cookie "Nome = Valore"
	    var asCookie = asCookies[iCnt].split("=");
	    if (sNome == asCookie[0]) { 
			return (unescape(asCookie[1]));
		}
	}

	// SE non esiste il cookie richiesto
	return("");
}
function eraseCookie(sNome)
{
	var sPath = "/";
	var sDomain = "stage-portal.superenalotto.it";
	var dtOggi = new Date()
	var dtExpires = new Date()
	dtExpires.setTime(dtOggi.getTime() - 24 * 1 * 3600000)
	document.cookie = sNome + "=; expires=" + dtExpires.toGMTString() + "; domain=" + escape(sDomain) + "; path=" + escape(sPath);
}
function eraseAllCookieRegistrazione()
{
	// Cookie per la gestione di Apri un conto di gioco
	eraseCookie('nazioneResidenza');
	eraseCookie('provinciaResidenza');
	eraseCookie('indexProvinciaResidenza');
	eraseCookie('attivaDomicilio');
	eraseCookie('nazioneDomicilio');
	eraseCookie('provinciaDomicilio');
	eraseCookie('indexProvinciaDomicilio');
	eraseCookie('nazioneNascita');
	eraseCookie('provinciaNascita');
	eraseCookie('indexProvinciaNascita');
	eraseCookie('documentoEnteRilascio');
	eraseCookie('indexTipoDocumento');
	eraseCookie('nazioneResidenza');
	eraseCookie('comuneResidenza');
	eraseCookie('comuneNascita');
	eraseCookie('enteRilascio');
	
	eraseCookie('IndexNazioneResidenza');
	eraseCookie('IndexNazioneNascita');
	eraseCookie('IndexNazioneDomicilio');
	eraseCookie('indexNazioneCittadinanza');
	eraseCookie('nazioneCittadinanza');
	eraseCookie('indexSesso');
	
	
	
}
function EraseAllCookie()
{
	// Cookie per la gestione delle pagine del Sito Gioco
	eraseCookie('_sezione');
	eraseCookie('_BoxInformativo');
	eraseCookie('_BoxPromo');
	eraseCookie('_BoxInfoSmall');
	eraseCookie('_assvirt');
	
	// Cookie per la gestione di Apri un conto di gioco
	eraseCookie('nazioneResidenza');
	eraseCookie('provinciaResidenza');
	eraseCookie('indexProvinciaResidenza');
	eraseCookie('attivaDomicilio');
	eraseCookie('nazioneDomicilio');
	eraseCookie('provinciaDomicilio');
	eraseCookie('indexProvinciaDomicilio');
	eraseCookie('nazioneNascita');
	eraseCookie('provinciaNascita');
	eraseCookie('indexProvinciaNascita');
	eraseCookie('documentoEnteRilascio');
	eraseCookie('indexTipoDocumento');
	eraseCookie('nazioneResidenza');
	eraseCookie('comuneResidenza');
	eraseCookie('comuneNascita');
	eraseCookie('enteRilascio');
	eraseCookie('page');
	eraseCookie('page_2');
	
}

// GESTIONE DELLA			***	MYPage  ***
function modifyThisBox(boxId)
{
	location.href = "https://stage-portal.superenalotto.it/portal/page/portal/SitoGioco/CORPI/7.%20AREA%20RISERVATA/7d.%20MYPage/box"+boxId+"/box"+boxId+"%20modifica";
}
function getParamsBox()
{
	var checkId = 'idSelect_';
	
	var risultato = '';
	for (i=0; i<100; i++)
	{
		if (elementIsNotNull(checkId+i))
		{
			var checkObj = document.getElementById(checkId+i);
			if (checkObj.checked == true)
			{
				risultato = risultato + i + '|';
			}
		}
	}
	return risultato;
}
function submitThisBox(boxId)
{
	var frmName = "frmBoxModifica" + boxId;
	var frmObj = document.getElementById(frmName);
	frmObj.action = "https://stage-portal.superenalotto.it/portal/page/portal/SitoGioco/CORPI/7.%20AREA%20RISERVATA/7d.%20MYPage/box"+boxId+"/box"+boxId+"%20modifica";
	frmObj.paramsbox.value = getParamsBox();
	frmObj.boxmodify.value = 'modificato';
	frmObj.submit();
}
function closeModifyThisBox(boxId)
{
	var pageUrl = "https://stage-portal.superenalotto.it/portal/page/portal/SitoGioco/CORPI/7.%20AREA%20RISERVATA/7d.%20MYPage/box"+boxId+"/";
	location.href = pageUrl;
}