function IsValidUTF8(s){
	if (encodeUTF8(decodeUTF8(s)) != decodeUTF8(s)) {
	 return true;
	}
	else {
		return false;
	}
}
function encodeUTF8(string) {  
   string = string.replace(/\r\n/g,"\n");  
   var c, n, utftext = "";  

   for (n = 0; n < string.length; n++) {  

     c = string.charCodeAt(n);  

     if (c < 128) {  
         utftext += String.fromCharCode(c);  
     }  
     else if((c > 127) && (c < 2048)) {  
         utftext += String.fromCharCode((c >> 6) | 192);  
         utftext += String.fromCharCode((c & 63) | 128);  
     }  
     else {  
         utftext += String.fromCharCode((c >> 12) | 224);  
         utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
         utftext += String.fromCharCode((c & 63) | 128);  
     }  
   }  
   return utftext;  
}

function decodeUTF8(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return string;
}

function appendKPI(){
	var kpi = SEAT.COOKIE.getCookie("kpi");
	if (kpi != null) {
  	return "&kpi=" + kpi;
  }
	else {
  	return "";
  }
}

function openVideoVisual(cc, qsv, l, iq) {
	window.open("http://click.paginegialle.it/paginegialle/video_da_visual.html?verso=http://www.paginegialle.it/pg/cgi/pgVisual.cgi?cf=65&cc=" + cc + "&qsv=" + qsv + "&l=" + l + "&iq=" + iq + appendKPI(), "", "width=950, height=413,toolbar=no,location=no, status=no,menubar=no,scrollbars=no,resizable=yes");
}

function openVideoVisualGC(cc, qsv, l, iq) {
	window.open("http://click.paginegialle.it/paginegialle/video_da_visual.html?verso=http://www.paginegialle.it/pg/cgi/pgVisual.cgi?tr=15&cf=65&cc=" + cc + "&qsv=" + qsv + "&l=" + l + "&iq=" + iq + appendKPI(), "", "width=950, height=413,toolbar=no,location=no, status=no,menubar=no,scrollbars=no,resizable=yes");
}

function webcamPopup(NAME, SITO, TIPO, URL, WIDTH, HEIGHT, REFRESH) {
	// Dimensioni brand 'PagineGialle Visual'
	var brandWidth = 252;
	var brandHeight = 48;

	// Altezza link
	var linkHeight = 28;
	
	// Handler finestra popup
	var POPUP;
	
	// Imposto la larghezza del popup all'immagine (con, come impostazione minima,
	// la larghezza del banner di brand
	var POPUPWIDTH = 0;
	if (parseInt(WIDTH) < brandWidth) {
		POPUPWIDTH = brandWidth;
	}
	else {
		POPUPWIDTH = parseInt(WIDTH);
	}
		
	// Imposto l'altezza del popup come l'altezza dell'immagine passate pił l'altezza
	// del banner di brand
	var POPUPHEIGHT = parseInt(HEIGHT)+brandHeight+linkHeight;
	var NEWURL="http://wbs-webcam.paginegialle.it/webcamshow/webcamshow-autostrade.html?name=" + escape(NAME) + "&sito=" + escape(SITO) + "&tipo=" + TIPO + "&url=" + escape(URL) + "&width=" + WIDTH + "&height=" + HEIGHT + "&refresh=" + REFRESH + "&index=1";
          
	var NEWNAME=escape(NAME);
	var POPUPOPTS="location=no,menubar=no,resizable=no,status=no,width=" + POPUPWIDTH + ",height=" + POPUPHEIGHT;
	
	POPUP = window.open(NEWURL, NEWNAME, POPUPOPTS);	

	// Apro la finestra di popup
	//POPUP = window.open("http://wbs-webcam.paginegialle.it/webcamshow/webcamshow.html?name=" + escape(NAME) + "&sito=" + escape(SITO) + "&tipo=" + TIPO + "&url=" + escape(URL) + "&width=" + WIDTH + "&height=" + HEIGHT + "&refresh=" + REFRESH, NAME, "location=no,menubar=no,resizable=no,status=no,width=" + POPUPWIDTH + ",height=" + POPUPHEIGHT);	
}

function openStampaAzienda(cc, isInserz){
	var tit4 = "stp" + cc;
	var url4 = "http://www.paginegialle.it/pg/cgi/pgVisual.cgi?cf=64" + appendKPI();
	if ( isInserz == null ||  isInserz== '1') {
  	url4 += "&cc=" + cc;
	}
	else {
  	url4 += "&cs=" + cc;
	}
	var stp = window.open(url4, tit4, "width=650, height=700,toolbar=no,location=no, statusbar=no, status=no,menubar=no,scrollbars=yes,resizable=no");
	stp.focus();
}

function openPrenota(cc,cmb,nad,nch,st_ch,nnt,dtTo,dtFrm,nrm,l){
	if (!dtTo) {
  	dtTo = getDateFromToday(1);
	}
	if (!dtFrm) {
  	dtFrm = getDateFromToday(0);
	}
	window.open("http://prenota.paginegialle.it/booking/action?sez=sc&ssez=p&op=lc&cs=0&bcc=" + cc + '&cmb=' + cmb + '&nad=' + nad + '&nch=' + nch + '&st_ch=' + st_ch + '&nnt=' + nnt + '&dtTo='+ dtTo + '&dtFrm=' + dtFrm + '&nrm=' + nrm + '&l=' + l);
}

function openPrenotaDesc(cc) {
	var dtTo = getDateFromToday(1);
	var dtFrm = getDateFromToday(0);
	window.open("http://prenota.paginegialle.it/booking/action?sez=sc&ssez=s&op=lc&l=1&cs=6&bcc=" + cc + '&dtFrm=' + dtFrm + '&dtTo=' + dtTo + '&nad=2&nch=0&nrm=1&nnt=1');
}

function openPrenotaCosa(cc,cmb,nad,nch,st_ch,nnt,dtTo,dtFrm,nrm,l) {
	if (!dtTo) {
  	dtTo = getDateFromToday(1);
	}
	if (!dtFrm) {
  	dtFrm = getDateFromToday(0);
	}
	window.open("http://prenota.paginegialle.it/booking/action?sez=sc&ssez=p&cs=0&bcc=" + cc + '&cmb=' + cmb + '&nad=' + nad + '&nch=' + nch + '&st_ch=' + st_ch + '&nnt=' + nnt + '&dtTo='+ dtTo + '&dtFrm=' + dtFrm + '&nrm=' + nrm + '&l=' + l);
}
function openListingPrenota(dv){
	var dtTo = getDateFromToday(1);
	var dtFrm = getDateFromToday(0);
	window.open("http://prenota.paginegialle.it/booking/action?sez=dchk&ssez=&tl=2&obf=0&pg=1&l=1&nr=10&cmb=0-1-0-0-0&of=&flCmb=1&st_ch=0&cs=0&nnt=1&px=608&py=485&shmp=0&qs=000363600&dv=" + dv + '&dtFrm=' + dtFrm + '&dtTo=' + dtTo + '&nrm=1&nad=2&nch=0');
}

/** correzione per ie **/
function getCurrentYear(){
	var today=new Date();
	var year;
	if (today.getYear() > 2000) {
  	year = today.getYear();
	}
	else {
  	year = today.getYear() + 1900;
	}
	return year;	
}

function formatDM(a){
	if (a < 10) {
  	return "0" + a;
	}
	else {
  	return a;
	}
}

function getDateFromToday(gg){
	var today = new Date();
	return formatDM((today.getDate() + gg)) + "/" + formatDM((today.getMonth() + 1)) + "/"+ getCurrentYear();
}

function openVivaTicket(ide, idl) {
	window.open("http://wbs.paginegialle.it/visualservice/action?msez=300&ssez=2&ce=" + ide + "&lu=" + idl, "", "width=700,height=650,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
}

function openSI(spi, qs){
	var url = "http://www.visual.paginegialle.it?" + qs;
	window.open("http://wbs.paginegialle.it/visualservice/action?msez=350&spi=" + spi + "&urlSpec=" + escape(url), "", "width=250, height=320,toolbar=no,location=no, status=no,menubar=no,scrollbars=no,resizable=no");
}	

function openStampa(lat, lon, zoom) {
	window.open("http://wbs.paginegialle.it/visualservice/action?msez=270&z=" + zoom + "&cx=" + lon + "&cy=" + lat + appendKPI(), "", "width=700, height=660,toolbar=no,location=no, status=no,menubar=no,scrollbars=no,resizable=no");
}
function openStampaListing(qs){	
	window.open("http://wbs.paginegialle.it/visualservice/action?msez=200&" + qs, "", "width=700, height=660,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}

function openStampaPercorso(lx, ly, ex, ey, itn, rt) {
    var itnStr = '&itn=';
    if ( itn != null && itn != undefined ) {
      itnStr = '&itn=' + itn;
    }
    window.open("http://wbs.paginegialle.it/visualservice/action?msez=250&lx="+lx+"&ly="+ly+"&ex="+ex+"&ey="+ey+"&itn="+itn+"&rt="+rt, "", "width=700,height=660,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}

function openSchedaAzienda(host, cod_vista, iq, cc){
	var baseurl = '';
	if (cod_vista == 0) baseurl = 'http://www.paginegialle.it/ck/scheda_mappa_trad.html?verso=';
	else if (cod_vista == 1) baseurl = 'http://www.paginegialle.it/ck/scheda_ortofoto.html?verso=';
	else baseurl = 'http://www.paginegialle.it/ck/scheda_orto_map_trad.html?verso=';
	
	var openurl = baseurl + escape(host + '/3/ct=3&cl=1&cc=' + cc + '&cl=' + '&iq=' + iq + '&ms=16&bl=1' + appendKPI());
	var stp = window.open(openurl);
	stp.focus();
}

function openSchedaAziendaLocal(id, pl) {
	window.open("http://wbs.paginegialle.it/visualservice/action?pl=" + pl + "&msez=300&ssez=1&id=" + id, "", "width=500,height=650,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}  	

function openSchedaEventoLocal(id, pl){
	window.open("http://wbs.paginegialle.it/visualservice/action?ev=1&pl=" + pl + "&msez=300&ssez=1&id=" + id, "", "width=500,height=650,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}  	

function openSchedaEventoTcolLocal(cde, idf, fl, pl){
	window.open("http://wbs.paginegialle.it/visualservice/action?pl=" + pl + "&msez=300&ssez=3&cde=" + cde + "&idf=" + idf + "&fl=" + fl, "", "width=500,height=650,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}  	

function openSchedaEventoVivaTicketLocal(ce, lu, pl){
	window.open("http://wbs.paginegialle.it/visualservice/action?pl=" + pl + "&msez=300&ssez=2&ce=" + ce + "&lu=" + lu, "", "width=650,height=650,toolbar=no,location=no, status=no,menubar=no,scrollbars=yes,resizable=yes");
}  	

function sendVisualCard(ragsoc, indir, link, lat, lon){
	window.open("http://visualcard.paginegialle.it/html2/index.php?ragsoc=" + ragsoc + "&indir=" + indir + "&link=" + escape(link) + "&lat=" + lat + "&lon=" + lon);
}  	

function sendVisualCardPintoricchio(ragsoc, indir, link, lat, lon){
	window.open("http://visualcard.paginegialle.it/pintoricchioNR?ragsoc=" + ragsoc + "&indir=" + indir + "&link=" + escape(link) + "&lat=" + lat + "&lon=" + lon);
}  	

function openPercorsi(link){
	var host = "http://" + window.location.host;
	var newLocation = "http://click.paginegialle.it/paginegialle/percorsi_visuali.html?verso=" + host + link;
	if (link.indexOf("?") == -1) {
		window.location.href = newLocation + "?";
	}
	else {
		window.location.href = newLocation;	
	}
}

function open3D(link){
	var host = "http://" + window.location.host;
	var newLocation = "http://click.paginegialle.it/paginegialle/2d_to_3d.html?verso=" + host + link;
	if (link.indexOf("?") == -1) {
		window.location.href = newLocation + "?";
	}
	else {
		window.location.href = newLocation;
	}
}

function openStampaSport(id,cx,cy){
	var uri = 'http://wbs.paginegialle.it/visualservice/action?msez=9999&ssez=2&tl=0&id=' + id + '&cx=' + cx + '&cy=' + cy;
	var stp = window.open(uri, 'STAMPA', "width=650, height=700,toolbar=no,location=no, statusbar=no, status=no,menubar=no,scrollbars=yes,resizable=no");
	stp.focus();
}

