/* Copyright (c) 2007, Iscanet Internet Services S.r.l.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source or object code must include the above
 copyright notice, this list of conditions, the following disclaimer
 in the documentation and/or other materials provider with the
 distribution.
2. Neither the name of the copyright holders nor the names of its
 contributors may be used to endorse or promote products derived
 from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Ultimate client-side JavaScript client sniff. Version 3.03
(C) Netscape Communications 1999-2001. Permission granted to reuse and distribute.
*/
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) ||
(agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up = (is_ie && !is_ie3 && !is_ie4);
 var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
 var is_ie6 = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
 var is_ie6up = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
var is_aol = (agt.indexOf("aol") != -1);
var is_opera = (agt.indexOf("opera") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);
var is_js;
var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
var is_os2 = ((agt.indexOf("os/2")!=-1) || 
(navigator.appVersion.indexOf("OS/2")!=-1) || 
(agt.indexOf("ibm-webexplorer")!=-1));
var is_mac = (agt.indexOf("mac")!=-1);
if (is_mac && is_ie5up) is_js = 1.4;
var is_mac68k = (is_mac && ((agt.indexOf("68k")!=-1) || 
(agt.indexOf("68000")!=-1)));
var is_macppc = (is_mac && ((agt.indexOf("ppc")!=-1) || 
(agt.indexOf("powerpc")!=-1)));

var is_sun = (agt.indexOf("sunos")!=-1);
var is_irix = (agt.indexOf("irix") !=-1); 
var is_hpux = (agt.indexOf("hp-ux")!=-1);
var is_aix = (agt.indexOf("aix") !=-1); 
var is_linux = (agt.indexOf("inux")!=-1);
var is_freebsd = (agt.indexOf("freebsd")!=-1);
var is_bsd = (agt.indexOf("bsd")!=-1);
var gdir;
var map;
var n = String("");
var revpos;
var marker;
var request = false;
var iconr = new GIcon();
var iconv = new GIcon();
var done = 0;
var recadsl = 0;
var local = "";
var sitoloc = "";

 try {
 request = new XMLHttpRequest();
 } catch (trymicrosoft) {
 try {
 request = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (othermicrosoft) {
 try {
 request = new ActiveXObject("Microsoft.XMLHTTP");
 } catch (failed) {
 request = false;
 } 
 }
 }

 if (!request)
 alert("Error initializing XMLHttpRequest!");


function onGDirectionsLoad(){ 
 var route = gdir.getRoute(0);
 var passo = route.getStep(0);
 var descr = passo.getDescriptionHtml();
 var temp = new Array();
 var temp1 = new Array();
 var temp2 = new Array();
 temp = descr.split('</b> da <b>');
 if(!temp[1]) {
 temp = descr.split('</b> su <b>');
 }
 if(temp[1]) temp1 = temp[1].split('</b>');
 if(temp1[0]) revpos = temp1[0];
 if(revpos) temp2 = revpos.split('<wbr/>'); 
 if(temp2[0]) revpos = temp2[0];
 if(temp2[1]) revpos = revpos+temp2[1];
 gdir.clear();
 document.getElementById('smalltext').innerHTML=revpos;
}

function seekDirections(point) {
 var actualpos = marker.getPoint();
 var urlpos = actualpos.toUrlValue();
 gdir.load( "from: " + urlpos + " to: " + urlpos, { "locale": "it_IT", "getSteps": true, "getPolyline": false, "preserveViewport": true } );
 revpos = get_loc('dirNA');document.getElementById('smalltext').innerHTML=revpos;
}

function notavail() {
 alert(get_loc('funcNA'));
}

function record(s){
 if(marker) {
 if(!done) {
 var pos = marker.getPoint().toUrlValue();
 var comment = document.form2.comment.value;
 var email = document.form2.email.value;
 comment = comment.replace(/^\s+|\s+$/g, '');
 if( comment.length > 5 ) {
 var url = "api/record.php?p=" + pos + "&n=" + n + "&s=" + s + "&r=" + escape(revpos) + "&c=" + escape(comment);
 if(s == 0) url = url + '&e=' + escape(email);
 request.open("GET", url, true);
 request.send(null);
 if(s == 0) {
 map.addOverlay(new GMarker(marker.getPoint(),iconv));
 alert(get_loc('thankyouOK'));
 }
 else if(s == 1) {
 map.addOverlay(new GMarker(marker.getPoint(),iconr));
 alert(get_loc('thankyouNO'));
 }
 done = 1;
 map.removeOverlay(marker);
 document.getElementById('insertform').style.visibility = 'hidden';
 marker = null;
 document.form2.comment.value = "";
 } else {
 alert(get_loc('rmrkIT'));
 }
 } 
 } else
 alert(get_loc('choosePT'));
}

function handleErrors(){
 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_BAD_KEY)
 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
 
}

function initMarker(point) {
 marker = new GMarker(point, {draggable: true});
 GEvent.addListener(marker, "click", function() {
 var actualpos = marker.getPoint();
 if(actualpos)
 var urlpos = actualpos.toUrlValue();
 marker.openInfoWindowHtml(revpos + '<p>coord: ' + urlpos + '</p>');
 }); 
 GEvent.addListener(marker, "dragstart", function() {
 map.closeInfoWindow();
 });
 GEvent.addListener(marker, "dragend", function() {
 var actualpos = marker.getPoint();
 var urlpos = actualpos.toUrlValue();
 gdir.load( "from: " + urlpos + " to: " + urlpos, { "locale": "it_IT", "getSteps": true, "getPolyline": false, "preserveViewport": true } );
 revpos = get_loc('dirNA');document.getElementById('smalltext').innerHTML=revpos;
 });
 map.addOverlay(marker);
}

function reposlogo() {
 var myWidth = 0;
 if( typeof( window.innerWidth ) == 'number' ) {
 myWidth = window.innerWidth;
 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
 myWidth = document.documentElement.clientWidth;
 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
 myWidth = document.body.clientWidth;
 }
 var elem = document.getElementById('logo');
 if(elem) {
 elem.style.left = myWidth/2 + 'px';
 }
}

function reposform() {
 var myWidth = 0;
 if( typeof( window.innerWidth ) == 'number' ) {
 myWidth = window.innerWidth;
 myHeight = window.innerHeight;
 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
 myWidth = document.documentElement.clientWidth;
 myHeight = document.documentElement.clientHeight;
 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
 myWidth = document.body.clientWidth;
 myHeight = document.body.clientHeight;
 }
 var elem = document.getElementById('insertform');
 if(elem) {
 elem.style.left = myWidth/2 + 'px';
 elem.style.top = myHeight/2 + 'px';
 }
}

function showlogo() {
 reposlogo();
 document.getElementById('logo').style.visibility = 'visible';
}

function showform(st) {
 if(marker) {
 if(map.getZoom() >12) {
 reposform();
 if(st==0) {
 document.getElementById('conferma').innerHTML=get_loc('titleOK');
 document.getElementById('emailspan').style.visibility = 'inherit';
 recadsl = 0;
 } else if(st==1) {
 document.getElementById('conferma').innerHTML=get_loc('titleNO');
 document.getElementById('emailspan').style.visibility = 'hidden';
 recadsl = 1;
 }
 document.getElementById('insertform').style.visibility = 'visible';
 }
 else {
 alert(get_loc('zoomIN'));
 } 
 } else {
 alert(get_loc('choosePT'));
 }
}

function hideform() {
 document.getElementById('insertform').style.visibility = 'hidden';
}

function qstart() {
 showlogo();
 map = new GMap2(document.getElementById("map")); 
 map.addControl(new GLargeMapControl());
 map.addControl(new GMapTypeControl());
 if ( local == "it" ) {
 map.setCenter(new GLatLng(42, 13), 5);
 sitoloc = "voglioladsl";
 } else if ( local == "es" ) {
 map.setCenter(new GLatLng(40, -3), 5);
 sitoloc = "quieroadsl";
 } else if ( local == "fr" ) {
 map.setCenter(new GLatLng(46.5, 2), 5);
 sitoloc = "jeveuxladsl";
 } else if ( local == "mx" ) {
 map.setCenter(new GLatLng(23,-103), 4);
 sitoloc = "quieroadsl.mx";
 }

 gdir = new GDirections(map);
 GEvent.addListener(gdir, "load", onGDirectionsLoad);
 GEvent.addListener(gdir, "error", handleErrors);
 
 iconr.image = 'img/icona_r.png';
 iconr.shadow = 'img/icona_s.png';
 iconr.iconSize = new GSize(12, 20);
 iconr.shadowSize = new GSize(22, 20);
 iconr.iconAnchor = new GPoint(6, 20);
 iconr.infoWindowAnchor = new GPoint(5, 1);

 iconv.image = "img/icona_v.png";
 iconv.shadow = "img/icona_s.png";
 iconv.iconSize = new GSize(12, 20);
 iconv.shadowSize = new GSize(22, 20);
 iconv.iconAnchor = new GPoint(6, 20);
 iconv.infoWindowAnchor = new GPoint(5, 1);

 
 reloadoverlay();
 
 GEvent.addListener(map, "click", function(overlay,point) {
 if(!overlay) {
 if(!marker) {
 if(!done) {
 map.setCenter(point,10);
 placemarker(point);
 } else {
 alert(get_loc('justADD'));
 }
 } else {
 map.removeOverlay(marker);
 marker = null;
 placemarker(point);
 }
 } else GEvent.trigger(overlay, "dblclick");
 }
 );
 
 GEvent.addListener(map, "moveend", function() {
 var bounds = map.getBounds();
 var point;
 if(marker) {
 point = marker.getPoint();
 }
 var by = bounds.getSouthWest().lat();
 var bx = bounds.getSouthWest().lng();
 var ty = bounds.getNorthEast().lat();
 var tx = bounds.getNorthEast().lng();
 map.clearOverlays();
 reloadoverlay();
 if(point) {
 marker = null;
 placemarker(point);
 }
 });
 
}

function placemarker(point) {
 if(!marker) {
 if(!done) {
 initMarker(point);
 GEvent.trigger(marker, "dragend");
 }
 }
 else {
 if(!done) {
 marker.setPoint(point);
 GEvent.trigger(marker, "dragend");
 }
 }
}

function createMarker(point, comment, added, icontype,id) { 
 var mrk = new GMarker(point, icontype);
 var p1, p2, p3, p4;
 var clen = comment.length;
 var comm;
 comment = comment.substring(0,160);
 comm = comment;
 var html = get_loc('rmrk') + comm + get_loc('updtTXT') + added;

 GEvent.addListener(mrk, "mouseover", function() { 
 document.getElementById("commenttext").innerHTML=html;
 });
 GEvent.addListener(mrk, "mouseout", function() {
 document.getElementById("commenttext").innerHTML="&nbsp;";
 });
 
 if( clen > 39 ) {
 comm = comment.substring(0,41) + '<br />' + comment.substring(41,99);
 if (clen > 99 ) {
 comm = comm + '<br />' + comment.substring(99,158);
 } if( clen > 158) {
 comm = comm + '<br />' + comment.substring(158,170);
 }
 } else {
 comm = comment;
 }
 var html2 = '<p style="font-size:xx-small">' + get_loc('rmrk') + comm + '<br /><br />' + get_loc('updtTXT') + added + '</p>';
 GEvent.addListener(mrk, "dblclick", function() {
 mrk.openInfoWindowHtml(html2);
 });
 return mrk; 
} 


function reloadoverlay() {
 var bounds = map.getBounds();
 var by = bounds.getSouthWest().lat();
 var bx = bounds.getSouthWest().lng();
 var ty = bounds.getNorthEast().lat();
 var tx = bounds.getNorthEast().lng();
 var q = "http://" + sitoloc + ".iscanet.com/api/get.php?n="+n+"&bx="+bx+"&by="+by+"&tx="+tx+"&ty="+ty
 var msgwait = get_loc('loaddt');
 document.getElementById('commenttext').innerHTML=msgwait;
 GDownloadUrl(q,function(data, responseCode) {
 var xml = GXml.parse(data);
 var markers = xml.documentElement.getElementsByTagName("marker");
 n = markers[0].getAttribute("n");
 for (var i = 0; i < markers.length; i++) {
 var point = new GLatLng( parseFloat(markers[i].getAttribute("lat")),
 parseFloat(markers[i].getAttribute("lng")) );
 var comm = unescape(markers[i].getAttribute("comment"));
 comm = comm.substring(0,160);
 if(markers[i].getAttribute("s") == '0')
 map.addOverlay( createMarker(point,comm, markers[i].getAttribute("added"),iconv,markers[i].getAttribute("id")) )
 else if(markers[i].getAttribute("s") == '1')
 map.addOverlay( createMarker(point,comm, markers[i].getAttribute("added"),iconr,markers[i].getAttribute("id")) )
 }
 document.getElementById('commenttext').innerHTML='&nbsp;';
 }
 );
 
}

function textLimit(field, maxlen) {
if (field.value.length > maxlen + 1)
alert(get_loc('maxrmrk'));
if (field.value.length > maxlen)
field.value = field.value.substring(0, maxlen);
}

