﻿
function count_search() {
    
    var url = 'Count.aspx';
    var myAjax = new Ajax.Request(url, { method:'get',onLoading:loading(), onFailure:failure, onComplete:show_counts});
}

function show_counts(originalRequest) {

    var Mess = 'Numero di ricerche effettuate dal 25/05/2009:<b>'+ originalRequest.responseText +'</b>' ;
    $('counts').update(Mess);
    hide_message();
}

function search_pf(nPage) {
    $('result').update("");
    var url     = 'SearchPF2.aspx';
    var pars = '?csPF=' + escape($F('m_csSearchPF')) + '&csFoglio=' + escape($F('m_csSearchFoglio')) + '&csCom=' + escape($F('m_csSearchCom')) + '&nPage=' + nPage;
 	var myAjax = new Ajax.Request(url,{method:'get',parameters:pars,onLoading:loading(),onFailure:failure,onComplete:show_search_pf});
 	
}  

function show_search_pf(originalRequest) 
{
    var xml_response = originalRequest.responseXML //e' gia data
    show_search_pf_panel(xml_response);
    show_search_pf_google();
    count_search();
}

//funzioni per Google Map
var map;
var markers_ricerca = [];
var markers_preferiti = [];
//ricerca
var nome_ricerca = [];
var descr_ricerca = [];
var dLat_ricerca = [];
var dLong_ricerca = [];
//preferiti
var nome_preferiti = [];
var descr_preferiti = [];
var dLat_preferiti = [];
var dLong_preferiti = [];
 


function createMarker(point, index,html) {

    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}




function show_ricerca_marker() 
{
    map.clearOverlays();
    for (var index = 0, len = markers_ricerca.length; index < len; ++index) {
        var marker = markers_ricerca[index];
        map.addOverlay(marker);
    }
    map.addOverlay(marker);
}

function create_info_window_ricerca(nome,descr,letter)
{
    var html = '<b>' + nome + '</b><br><span class=desc>' + descr + '</span>';
    html += '<br><a class="link_pref" href="javascript:preferiti_add_pf(\'' + letter + '\');">Aggiungi ai Preferiti</a>';
    
    return html;
}
function create_info_window_preferiti(nome, descr) {
    var html = '<b>' + nome + '</b><br><span class=desc>' + descr + '</span>';
    html += '<br><a class="link_pref" href="javascript:preferiti_del_pf(\'' + nome + '\');">Elimina</a>';

    return html;
}
function show_search_pf_panel(content) 
{
    nome_ricerca.clear();
    descr_ricerca.clear();
    dLat_ricerca.clear();
    dLong_ricerca.clear();
    markers_ricerca.clear();
    var html_content = '';
    var error = content.getElementsByTagName('error'); //.item(0).firstChild.data;
    if (error.length > 0) {

        var error_value = error.item(0).firstChild.data;
        html_content = '<b>'+error_value +'</b>';
        $('result').update(html_content);
        hide_message();
        return;
    }   
    var num_result = content.getElementsByTagName('num').item(0).firstChild.data;
    if (num_result == 0) {
        html_content = '<b> Nessun Risultato</b>';
        $('result').update(html_content);
        hide_message();
        return;
    }
    var num_pag = content.getElementsByTagName('num_pag').item(0).firstChild.data;
    var pag = num_result / num_pag;
    var cur_page = content.getElementsByTagName('cur_pag').item(0).firstChild.data;
    var count = 0;
    var root = content.getElementsByTagName('data').item(0);
    for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
    {
        html_content += '<div class="record">';
        var node = root.childNodes.item(iNode);
        var dLat = node.getElementsByTagName('Lat').item(0).firstChild.data;
        var dLong = node.getElementsByTagName('Long').item(0).firstChild.data;
        var nome = node.getElementsByTagName('Nome').item(0).firstChild.data;
        var descr = node.getElementsByTagName('Descr').item(0).firstChild.data;
        nome_ricerca.push(nome);
        descr_ricerca.push(descr);
        dLat_ricerca.push(dLat);
        dLong_ricerca.push(dLong);
        var point = new GLatLng(dLat, dLong);
        //icona
        var letter = String.fromCharCode("A".charCodeAt(0) + count);
        var html = create_info_window_ricerca(nome, descr,letter);
        var marker = createMarker(point, count, html);
        markers_ricerca.push(marker);
        
      
        var image_path = "http://www.google.com/mapfiles/marker" + letter + ".png";
        html_content += '<div class="letter_img"><img src="' + image_path + '" onclick="show_info_window_pf(\'' + letter + '\')"> </div>';
        html_content += '<div class="indent " ><a href="javascript:show_info_window_pf(\''+ letter +'\');">';
        html_content +=  nome;
        html_content += '</a><br><span class="desc">';
        html_content += descr;
        html_content += '</span>';
        //aggiunta ai preferiti
        
        html_content += '<br><a class="link_pref" href="javascript:preferiti_add_pf(\'' + letter + '\');">Aggiungi ai Preferiti</a>';
        html_content += '</div></div>'
        count++;
    }
    //Paginazione
    var html_pag = '<div id="paging">';
    html_pag += '<div class=result><center>' + num_result + ' risultati</br></br></center></div><div class="desc"><center>'
    var nStart = cur_page - 5;
    if (nStart <= 0)
        nStart = 1;
    for (var i =nStart ; i<=pag; i++) {
        if (i == cur_page)
            html_pag += '<b>' + i + '</b> '
        else
            html_pag += '<a href="javascript:search_pf('+i+');">' + i + '</a> ';
        if (i >  (parseInt(cur_page) + parseInt(num_pag)))
            break;
    }
    html_pag += '</center></div></div>';
    $('result').update(html_content + html_pag);
    if ($('result').style.display == 'none')
        show_tab_ricerca();
    hide_message();
    //show_message('Ricerca effettuata', 'notice');
}

function show_search_pf_google(content) 
{
      show_ricerca_marker();

}

function show_info_window_pf(letter) {
    //dalla letter devo prendere l'indice quindi le coordinate ecc.
    var count = letter.charCodeAt(0) - "A".charCodeAt(0);
    var dLat = dLat_ricerca[count];
    var dLong = dLong_ricerca[count];
    var nome = nome_ricerca[count];
    var descr = descr_ricerca[count];
    var html = create_info_window_ricerca(nome, descr,letter);
    var point = new GLatLng(dLat, dLong);
    map.setCenter(point, 16);
    map.openInfoWindow(point, html);
}

function show_info_window_pf_pref(letter) {
    //dalla letter devo prendere l'indice quindi le coordinate ecc.
    var count = letter.charCodeAt(0) - "A".charCodeAt(0);
    var dLat = dLat_preferiti[count];
    var dLong = dLong_preferiti[count];
    var nome = nome_preferiti[count];
    var descr = descr_preferiti[count];
    var html = create_info_window_preferiti(nome, descr);
    var point = new GLatLng(dLat, dLong);
    map.setCenter(point, 16);
    map.openInfoWindow(point, html);
}

//gestione preferiti
function preferiti_add_pf(letter) {

    //    $('preferiti').update("");
    var count = letter.charCodeAt(0) - "A".charCodeAt(0);
    var dLat = dLat_ricerca[count];
    var dLong = dLong_ricerca[count];
    var descr = descr_ricerca[count];
    var nome = nome_ricerca[count];

    var url = 'GestPreferiti.aspx';
    var pars = 'action=add' + '&csNome=' + escape(nome) + '&csDescr=' + escape(descr) + '&dLat=' + escape(dLat) + '&dLong=' + escape(dLong);
    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading1(), onFailure: failure, onComplete: show_preferiti_pf });
}


function preferiti_get_pf_embedded() 
{
    var url = 'GestPreferiti.aspx';
    var pars = 'action=get';
    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading1(), onFailure: failure, onComplete: show_preferiti_pf_embedded });
}
function show_preferiti_pf_embedded(originalRequest) {
    var xml_response = originalRequest.responseXML //e' gia data
    show_preferiti_pf_html(xml_response);
    show_preferiti_pf_google();
    show_tab_preferiti();
}
function preferiti_get_pf() {

    //    $('preferiti').update("");
    var url = 'GestPreferiti.aspx';
    var pars = 'action=get';
    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading1(), onFailure: failure, onComplete: show_preferiti_pf });
}
function show_preferiti_pf(originalRequest) {
    var xml_response = originalRequest.responseXML //e' gia data
    show_preferiti_pf_html(xml_response);
    show_preferiti_pf_google();
}
function show_preferiti_pf_html(content) {

    var html_content = '';
    nome_preferiti.clear();
    descr_preferiti.clear();
    dLat_preferiti.clear();
    dLong_preferiti.clear();
    markers_preferiti.clear();
    var error = content.getElementsByTagName('error'); //.item(0).firstChild.data;
    if (error.length > 0) {

        var error_value = error.item(0).firstChild.data;
        html_content = '<b>' + error_value + '</b>';
        $('preferiti').update(html_content);
        hide_message();
        return;
    }   
    var count = 0;
    var root = content.getElementsByTagName('data').item(0);
    for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
        html_content += '<div class="record">';
        var node = root.childNodes.item(iNode);
        var dLat = node.getElementsByTagName('Lat').item(0).firstChild.data;
        var dLong = node.getElementsByTagName('Long').item(0).firstChild.data;
        var nome = node.getElementsByTagName('Nome').item(0).firstChild.data;
        var descr = node.getElementsByTagName('Descr').item(0).firstChild.data;
        nome_preferiti.push(nome);
        descr_preferiti.push(descr);
        dLat_preferiti.push(dLat);
        dLong_preferiti.push(dLong);
        //Creazione del punto
        var point = new GLatLng(dLat, dLong);
        var html = create_info_window_preferiti(nome, descr);
        var marker = createMarker(point, count, html);
        markers_preferiti.push(marker);
        //icona
        var letter = String.fromCharCode("A".charCodeAt(0) + count);

        var image_path = "http://www.google.com/mapfiles/marker" + letter + ".png";
        html_content += '<div class="letter_img"><img src="' + image_path + '" onclick="show_info_window_pf_pref(\'' + letter + '\')"> </div>';
        html_content += '<div class="indent " ><a href="javascript:show_info_window_pf_pref(\'' + letter + '\');">';
        html_content += nome;
        html_content += '</a><br><span class="desc">';
        html_content += descr;
        html_content += '</span><br>';
        //            //eliminazione preferiti
        var par = '\'' + nome + '\'';
        html_content += '</br><a class="link_pref" href="javascript:preferiti_del_pf(' + par + ');">Elimina</a>';
        html_content += '</div></div>'
        count++;
    }
    var menu_op = '';
    if (count > 0) {
        menu_op = '<div style="float:left;width:50%;"><a  href="javascript:export_preferiti_tomtom()">Exporta TomTom</a></div>';
        menu_op += '<div style="float:right;width:50%;text-align:right;"><a  href="javascript:export_delall_preferiti()">Elimina Tutti</a></div>';
        menu_op += '<div style="clear:both;"></div>';
    }
    //Paginazione
    $('preferiti').update(menu_op + html_content);
    $('link_preferiti').innerHTML = 'Preferiti (' + count + ')';
}


function show_preferiti_pf_google() 
{
     if($('preferiti').style.display == 'block')
         show_preferiti_marker();
 }

function show_preferiti_marker() 
{
    map.clearOverlays();
    for (var index = 0, len = markers_preferiti.length; index < len; ++index) {
        var marker = markers_preferiti[index];
        map.addOverlay(marker);
    }
    map.addOverlay(marker);
}
 
 function preferiti_del_pf(nome) 
 {
//    $('preferiti').update("");
    var url     = 'GestPreferiti.aspx';
    var pars = 'action=del' + '&csNome=' + escape(nome);
    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading1(), onFailure: failure, onComplete: show_preferiti_pf });
}
function export_delall_preferiti() {
    //messaggio di conferma
    if (!confirm("Sei sicuro di eliminare tutti i Preferiti?"))
        return;
    var url = 'GestPreferiti.aspx';
    var pars = 'action=delall';
    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading1(), onFailure: failure, onComplete: show_preferiti_pf });
}


function show_tab_ricerca()
{
    $('preferiti').style.display = 'none';
    $('result').style.display = 'block';
    $('link_preferiti').removeClassName('tab_active');
    $('link_ricerca').addClassName('tab_active');
    show_ricerca_marker()
    
}
function show_tab_preferiti() 
{
    $('preferiti').style.display = 'block';
    $('result').style.display = 'none';
    $('link_preferiti').addClassName('tab_active');
    $('link_ricerca').removeClassName('tab_active');
    
    show_preferiti_marker();
}

function export_preferiti_tomtom() {

    location.href = 'GestPreferiti.aspx?action=export'; 
//    var url = 'GestPreferiti.aspx';
//    var pars = 'action=export' ;
//    var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars, onLoading: loading(), onFailure: failure, onComplete: show_export_pf });
}



function show_message(message,class_name)
{
  $('message').style.display = 'block';
  var html ='<div class=\"' + class_name +'\">'+ message + '</div>';
  $('message').update(html);
  setTimeout('hide_message()',4000);
}

function hide_message()
{
    $('message').update("");
    $('message').style.display ='';
}
function loading1()
{
}
 
function loading()
{
 show_message("Loading...","loading");
}
function failure()
{
//  alert('failure');
  show_message("Failure...","warning");
}



