function openWindow(wurl) {
    var winFeatures = 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width=840,height=720';
    var popWin = window.open(wurl, 'directions', winFeatures);
    if (popWin) { 
        //popWin.location.replace(wurl); 
        popWin.focus();
    }
    //return false;
}
function showAddressFrame(e, url, dealerIndex) {
    var evt = e || window.event;
    //alert(e.clientY);
    var hiddenValue = document.getElementById('selectedIndex');
    hiddenValue.value = dealerIndex;
    var addressFrame = document.getElementById('addressFrame');
    //addressFrame.src=url;
    addressFrame.style.top = evt.clientY - 140;
    addressFrame.style.visibility='visible';
    //addressFrame.style.display='block';
}
function closeFrame(frameName) {
    var projectFrame = window.parent.document.getElementById(frameName);
    if (projectFrame) {
        if(projectFrame.id == "addressFrame"){
            projectFrame.style.visibility='hidden';
        }else{
            projectFrame.style.display='none';
        }
    }
    //return true;
}

function clearStreetInfo(baseHref) {
    var url = baseHref+"/clearStreetInfo.do?refresh="+Math.random();
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
    document.location.reload();
}

function zoomToDealer(dealerIndex, context) {
    var hiddenValue = document.getElementById('selectedIndex');
    hiddenValue.value = dealerIndex;
    //var resultForm = document.getElementById('resultForm');
    document.forms[1].action = context + '/zoom.do';
    document.forms[1].submit();
}

function routingFromLongLat(dealerIndex){
    var frameTemp = document.getElementById('addressFrame');

    //frameTemp.style.display = "block";                
    //frameTemp.style.visibility = "hidden";

    addressFrame.document.getElementById("selectedIndex").value = dealerIndex;                
    addressFrame.document.getElementById("routingForm").submit();
}

function showRoute(dealerIndex) {
    showHourGlass();
    var dealerIndex = parent.document.getElementById('selectedIndex');
    var hiddenValue = document.getElementById('selectedIndex');
    hiddenValue.value = dealerIndex.value;
    //alert(hiddenValue.value);
    //document.forms['baseForm'].submit();
}

function showHourGlass() {
    var el = document.getElementById('address-panel');
    if (el) {
        el.style.cursor = 'wait';
    } 
    el = document.getElementById('btnGo');
    if (el) {
        el.style.cursor = 'wait';
    } 
}

function getPointFromLongLat(longitude, latitude, poiImgId) {
    wsnav.getPointFromLongLat(longitude, latitude, poiImgId);
}

function numericToScreen(wsnav) {
    // -73.62955, 45.61387
    var deltaX = wsnav.xmax - wsnav.xmin;
    var deltaY = wsnav.ymax - wsnav.ymin;

    var tempPointX = ((-71.36051 - wsnav.xmin) / deltaX) * wsnav.mapWidth;
    var tempPointY = ((wsnav.ymax - 46.76519) / deltaY) * wsnav.mapHeight;

    tempPointX = Math.round(tempPointX);
    tempPointY = Math.round(tempPointY);

    var point = new Array();
    point[0] = tempPointX;
    point[1] = tempPointY;
    //alert('po: ' + point);
    return point;
} 
function loadDealers() {
    var messages = document.getElementById('messages');
    var loadingDiv = document.getElementById("loadingDiv");
    if (messages) {
        // clear previous error messages
        messages.innerHTML = '';  
        messages.style.display = 'none';
    }
    var uploader = document.getElementById('uploader');
    if(uploader && trim(uploader.value).length == 0 ) {
        alert('Please use the Browser button to upload a file.');
        return false;
    }
    try {
        document.fileUploadForm.submit();
    } catch(exception) {
        alert('Please use the folder button to upload a file.');
        return;
    }
    if (loadingDiv) {
        loadingDiv.style.display =  'inline';
    } 
    return false;
}
function blink(imgId) {
    var pointImg = parent.document.getElementById("pointimg_" + imgId);
    if (pointImg) {
        pointImg.style.visibility = (pointImg.style.visibility == "visible") ? "hidden" : "visible";
    }
}
function stopBlinking() {
    if (typeof blinking != "undefined" && blinking) {
        clearInterval(blinking);
    }
}  
function highlightPoint(pointId, show) {
    var div = document.getElementById("pointInfo_" + pointId);
    var pointImg = document.getElementById("pointimg_" + pointId);
    var poiDiv = document.getElementById("poiInfo_" + pointId);
    var link = document.getElementById("link_" + pointId);
    stopBlinking();
    if (div == null) return;
    if (show) {
        div.style.zIndex="10001";
        poiDiv.style.backgroundColor = '#FF7819';
        blinking = setInterval("blink('" + pointId +"')", 500);
        //setOpacity(pointImg, 6);
        overlay.showInfo(pointId);
        link.style.color = '#FFFFFF';
    } else {
        div.style.zIndex="10000";
        poiDiv.style.backgroundColor = '#FFFFFF';
        stopBlinking();
        pointImg.style.visibility = "visible";
        //setOpacity(pointImg, 10);
        overlay.hideInfo(pointId);
        link.style.color = 'blue';
    }
}
function highlightList(pointId, show) {
    //var dealerFrame = parent.frames[0];
    //dealerFrame.location.href='ui/dealers.jsp#'+pointId;
    //dealerFrame.scrollBy(0,-10);
    if (pointId == -1) {
        return;
    }
    var el = document.getElementById("poiInfo_" + pointId);
    var link = document.getElementById("link_" + pointId);
    el.scrollIntoView();
    if (el == null) return;
    if (show) {
        el.style.backgroundColor = '#FF7819';  //'orange';
        link.style.color = '#FFFFFF';
    } else {
        el.style.backgroundColor = '#FFFFFF';  
        link.style.color = 'blue';
    }
}
function validPostal(postal) {
    if (postal.match(/^[0-9]{5}$/)) {
        return true;
    }
    postal=postal.toUpperCase();
    if (postal.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
        return true;
    }
    if (postal.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
        return true;
    }
    alert(invalidZip);
    return false;
}

function trim(str) {
    return str.replace(/^\s*|\s*$/g,"");
}