﻿function showdiv(hideID) {
    document.getElementById(hideID).style.display = 'block';
    document.getElementById(hideID).style.visibility = 'visible';
}
function hidediv(hideID) {
    document.getElementById(hideID).style.display = 'none';
    document.getElementById(hideID).style.visibility = 'hidden';
}

function showAddress(pcode, htmlfdata) {
    var address = pcode;
    var html = htmlfdata;
    geocoder = new GClientGeocoder();
    var retLat = 0;
    var retLang = 0;
    if (geocoder) {
        geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + " not found");
            }
            else {
                var baseIcon = new GIcon();
                // baseIcon.image = "skins\Skin_1\images\mappopup.jpg";
                // baseIcon.shadow = "shadow-home_nbtn.png";
                baseIcon.iconSize = new GSize(38.0, 45.0);
                baseIcon.shadowSize = new GSize(61.0, 45.0);
                baseIcon.iconAnchor = new GPoint(19.0, 22.0);
                baseIcon.infoWindowAnchor = new GPoint(19.0, 22.0);
                // baseIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";

                var map = new GMap2(document.getElementById("map_canvas"));

                map.setCenter(point, 13);
                var marker = new GMarker(point);
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(html);
                });
                map.addOverlay(marker);
            }
        });
    }
}

var srcpostcode;
var BnC, map, polyOptions, isDirecting = false;
function popup()
{  
    hidediv('popupvalue');
    var storeid=document.getElementById('storeddl').value;    
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert("Your browser does not support XMLHTTP!");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            var value = xmlhttp.responseText;
            var index = value.indexOf("<div");
            if (index > 0) {
                var arr = value.substring(0, index);
                var arr1 = arr.split("~");

                document.getElementById('stname').innerHTML = arr1[0];
                document.getElementById('add1').innerHTML = arr1[1];
                document.getElementById('add2').innerHTML = arr1[2];
                document.getElementById('add3').innerHTML = arr1[3];
                var htmltel = document.getElementById('tel').innerHTML;
                var htmlfax = document.getElementById('fax').innerHTML;
                document.getElementById('tel').innerHTML = document.getElementById('tel').innerHTML + " " + arr1[4];
                document.getElementById('fax').innerHTML = document.getElementById('fax').innerHTML + " " + arr1[5];
//                document.getElementById('email').innerHTML = document.getElementById('email').innerHTML + " " + arr1[6];
                showdiv('popupvalue');
                var postcode = arr1[9] + ',' + arr1[10];
                var htmldata = "<font color=black>" + arr1[0] + "<br/>" + arr1[1] + "<br/>" + arr1[2] + "<br/>" + arr1[3] + "<br/>" + htmltel + " " + arr1[4] + "<br/>" + htmlfax + " " + arr1[5] + "</font>";
		test(postcode,htmldata);//showAddress(postcode, htmldata);
                document.getElementById('map_canvas').style.marginTop = '10px';
            }
            else {
				
                var arr1 = value.split("~");
                document.getElementById('stname').innerHTML = arr1[0];
                document.getElementById('add1').innerHTML = arr1[1];
                document.getElementById('add2').innerHTML = arr1[2];
                document.getElementById('add3').innerHTML = arr1[3];
                var htmltel = document.getElementById('tel').innerHTML;
                var htmlfax = document.getElementById('fax').innerHTML;
                document.getElementById('tel').innerHTML = document.getElementById('tel').innerHTML + " " + arr1[4];
                document.getElementById('fax').innerHTML = document.getElementById('fax').innerHTML + " " + arr1[5];
//                document.getElementById('email').innerHTML = document.getElementById('email').innerHTML + " " + arr1[6];
                showdiv('popupvalue');
                var postcode = arr1[9] + ',' + arr1[10];
                //                var htmldata = "<font color=black>" + arr1[0] + "<br/>" + arr1[1] + "<br/>" + arr1[2] + "<br/>" + arr1[3] + "<br/>Tel: " + arr1[4] + "<br/>Fax: " + arr1[5] + "<br/>Email: " + arr1[6] + "</font>";
                var htmldata = "<font color=black>" + arr1[0] + "<br/>" + arr1[1] + "<br/>" + arr1[2] + "<br/>" + arr1[3] + "<br/>" + htmltel + " " + arr1[4] + "<br/>" + htmlfax + " " + arr1[5] + "</font>";
		test(postcode,htmldata);
		//showAddress(postcode,htmldata);
	
                document.getElementById('map_canvas').style.marginTop = '10px';
            }
        }
    };
    if(storeid>0)
    {
        var url="mappopup.aspx?storeid="+storeid+"";                                
        xmlhttp.open("POST",url,true);
        xmlhttp.send(null);
    }
    else {
        document.getElementById('map_canvas').style.marginTop = '100px';
        alert('please select a store');        
    }
}
function test(pcode,htmlfdata)
{
    var address = pcode;
    var html = htmlfdata;
    geocoder = new GClientGeocoder();
    var retLat = 0;
    var retLang = 0;
    if (geocoder) {
        geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + " not found");
            }
            else {
                var baseIcon = new GIcon();
                // baseIcon.image = "skins\Skin_1\images\mappopup.jpg";
                // baseIcon.shadow = "shadow-home_nbtn.png";
                baseIcon.iconSize = new GSize(38.0, 45.0);
                baseIcon.shadowSize = new GSize(61.0, 45.0);
                baseIcon.iconAnchor = new GPoint(19.0, 22.0);
                baseIcon.infoWindowAnchor = new GPoint(19.0, 22.0);
                // baseIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";

                var map = new GMap2(document.getElementById("map_canvas"));

                map.setCenter(point, 13);
                var marker = new GMarker(point);
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(html);
                });
                map.addOverlay(marker);
            }
        });
    }
}
