﻿/*
- Cercare il posizionamento assoluto del sito
- Nazionalizzare
        
- http://code.google.com/intl/it/apis/ajaxsearch/documentation/reference.html
*/

google.load("search", "1");

var gsWebSearch             // google.search.WebSearch
var gsSearchForm            // google.search.SearchForm
var bShowCompetitor = true  // Visualizza i competitors

var page = 0, curResult = 0, pages = 0, searchTerms, lyrPos, iRichieste = 0, limit = 0, secs = 30;

function searchComplete() {
    var testUrl = document.getElementById("txtUrl").value;
    var patt = new RegExp(testUrl, "i");
    var lyrPos = document.getElementById("lyrPos");
    if (gsWebSearch.results && gsWebSearch.results.length > 0) {

        if (document.getElementById("ckCompetitors").checked) {
            document.getElementById("lyrResults").style.display = '';
        } else {
            document.getElementById("lyrResults").style.display = 'none';
        }
        if (page == 0) pages = gsWebSearch.cursor.pages.length;

        //                alert(gsWebSearch.cursor.estimatedResultCount)
        for (var itm = document.getElementById("lyrResults"), d = gsWebSearch.results, c = 0; c < d.length; c++) {
            var a = d[c];
            curResult++;
            if (patt.test(a.url) == true) {
                itm.innerHTML += '<nobr>' + curResult + ': <a title="' + a.titleNoFormatting + '" target="_blank" href="' + a.unescapedUrl + '" class="trovato">' + a.unescapedUrl + "</a></nobr><br />";
                lyrPos.innerHTML = '<h4>Il sito &egrave; presente in <font class="trovato">' + curResult + '&deg; posizione</font></h4>';
                return
            } else {
                itm.innerHTML += '<nobr>' + curResult + ': <a title="' + a.titleNoFormatting + '" target="_blank" href="' + a.unescapedUrl + '">' + a.unescapedUrl + "</a></nobr><br />"
                lyrPos.innerHTML = '<h4>Il sito non &egrave; presente nei primi <font class="trovato">' + curResult + '</font> risultati</h4>';
            }
        }
        if (page < pages - 1) {
            page++;
            gsWebSearch.gotoPage(page)
        } else
            lyrPos.innerHTML = "<h4>Il sito non &egrave; presente nei primi <font class='trovato'>" + curResult + "</font> risultati</h4>"
    } else {
        lyrPos.innerHTML = "<h4>Non ci sono risultati</h4>"
        document.getElementById("lyrResults").style.display = 'none';
    }
}

google.setOnLoadCallback(OnLoad);

function OnLoad() {
    gsSearchForm = new google.search.SearchForm(false, document.getElementById("lyrSearchControl"));
    gsSearchForm.setOnSubmitCallback(null, OnSearchSubmit);
    gsSearchForm.input.focus();

    gsWebSearch = new google.search.WebSearch;
    gsWebSearch.setSearchCompleteCallback(this, searchComplete, null);
    gsWebSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
    gsWebSearch.setNoHtmlGeneration();
}

function OnSearchSubmit(txtKeys) {
    limit > 6 && location.replace("/");
    if (++iRichieste > 20) {
        alert("Servizio occupato, riprovate più tardi.");
        HideApi();
        setTimeout("ShowApi()", secs * 1E3);
        secs += 10;
        limit++;
        return false
    }

    pages = 0
    curResult = 0
    page = 0;
    var testUrl = document.getElementById("txtUrl").value;
    if (checkURL(testUrl)) {
        if (testUrl.substring(testUrl.length - 1) == "/")
            testUrl = testUrl.substring(0, testUrl.length - 1);
        if (txtKeys.input.value) {
            searchTerms = txtKeys.input.value.replace(/[',@,-,<,>]/gi, "");
            if (searchTerms.length > 100) searchTerms = searchTerms.substring(0, 100);
            ResetResults();
            gsWebSearch.execute(searchTerms)
        } else
            alert("Inserire la parola chiave");
    }
    return false
}

function checkURL(testUrl) {
    testUrl = testUrl.replace(/^\s+|\s+$/g, "");

    if (testUrl.indexOf("www.") == 0) testUrl = testUrl.substring(4);
    else if (testUrl.indexOf("http://www.") == 0) testUrl = testUrl.substring(11);
    else if (testUrl.indexOf("http://") == 0) testUrl = testUrl.substring(7);
    else {
        alert("Inserire un indirizzo valido che inizi con 'http://'");
        return false
    }
    if (testUrl.length < 6) {
        alert("Inserire un indirizzo valido");
        return false
    }
    return true
}
function AutoReset() {
    /*
    document.getElementById("lyrPos").innerHTML = "";
    document.getElementById("lyrResults").innerHTML = "";
    */
}
function HideApi() {
    document.getElementById("lyrSearchControl").style.display = "none";
    document.getElementById("lyrWait").style.display = "block";
}
function ShowApi() {
    document.getElementById("lyrWait").style.display = "none";
    document.getElementById("lyrSearchControl").style.display = "block";
    iRichieste = 0
}
function ShowHideCompetitors(me) {
    document.getElementById("lyrResults").style.display = (document.getElementById("lyrResults").innerHTML != '') && me.checked ? 'block' : 'none'
}
function ResetResults() {
    document.getElementById("lyrResults").innerHTML = "";
    document.getElementById("lyrPos").innerHTML = '';
}
