// JScript File
var r1 = null;
var r2 = null;
var myrectangle;
var dragRectangle;
var first = '';
var second = '';

function getCoordinate(overlay, point) {
    // Included by Kumuthini. L on 07-May-10
    //    if (document.getElementById('hdnCoordinates') != null && document.getElementById('hdnCoordinates').value != '') {
    //        return;
    //    }
    
    if (first == '') {
        myrectangle = null;
        dragRectangle = true;
        first = point;
    }
    else {
        //GEvent.removeListener(eventOnMove);
        dragRectangle = false;

        draw_rectangle(first, second);
        
        r1 = first;
        r2 = second;
        //alert(r1 + '-' + r2);
        Rec_bounds.extend(r1);
        Rec_bounds.extend(r2);

        var marker1 = new GMarker(r1);
        var marker2 = new GMarker(r2);

        //To store the coordinates
        polyPoints.push(r1 + '#');
        polyPoints.push(r2 + '#');

        // Included by Kumuthini. L on 02-Feb-09
        polyArray.length = 0;

        //To store all the values and pass to Server Side 
        polyArray.push(marker1.getLatLng().toUrlValue() + '#');
        polyArray.push(marker2.getLatLng().toUrlValue() + '#');

        // Included by Kumuthini. L on 27-Jan-09
        if (document.getElementById('hdnCoordinates') != null) {
            document.getElementById('hdnCoordinates').value = polyArray;
        }

        r1 = ''
        r2 = ''
        map.setCenter(Rec_bounds.getCenter(), map.getBoundsZoomLevel(Rec_bounds) - 1);
        first = '';
        second = '';
    }
}

function ClearMap() {
    if (oUnitsKM.checked) {
        //eventOnclick =  GEvent.addListener(map, "click", leftClick);
        map.removeOverlay(markers[markers.length - 1]);
        markers.length = markers.length - 1;
        markers.splice(markers.length, 1);


        drawPoly();
        eventOnclick = GEvent.addListener(map, "click", getCoordinate);
    }
    else {
        first = '';
        dragRectangle = false;
        myrectangle = null;
    }
}


function mapClickRectangle(overlay, point) {
    if (point == null) { return; }

    if (!r1) {   // First click
        r1 = point;
        myrectangle = null;
        dragRectangle = true;
    }
    else {   // Second click
        map.clearOverlays();
        //GEvent.removeListener(eventOnClick);
        dragRectangle = false;
        draw_rectangle(r1, point);
        r2 = point;
        Rec_bounds.extend(r1);
        Rec_bounds.extend(r2);

        var marker1 = new GMarker(r1);
        var marker2 = new GMarker(r2);

        //To store the coordinates
        polyPoints.push(r1 + '#');
        polyPoints.push(r2 + '#');

        // Included by Kumuthini. L on 02-Feb-09
        polyArray.length = 0;

        //To store all the values and pass to Server Side 
        polyArray.push(marker1.getLatLng().toUrlValue() + '#');
        polyArray.push(marker2.getLatLng().toUrlValue() + '#');

        // Included by Kumuthini. L on 27-Jan-09
        if (document.getElementById('hdnCoordinates') != null) {
            document.getElementById('hdnCoordinates').value = polyArray;
        }

        r1 = ''
        r2 = ''
        map.setCenter(Rec_bounds.getCenter(), map.getBoundsZoomLevel(Rec_bounds) - 1);
        // commented by christopher s on 01-sep-09 for avoid improper alert while adding rec type
        //polyPoints.length = 0;
        //polyArray.length = 0;
        //markers.length = 0;
    }
}

function mapDragRectangle(point) {
    if (dragRectangle) {
        second = point;
        draw_rectangle(first, point);
    }
}

function draw_rectangle(a, b) {
    if (myrectangle != null) {
        map.removeOverlay(myrectangle);
    }

    if (a != null && b != null) {
        myrectangle = new GPolygon(new Array(a, new GPoint(a.x, b.y), b, new GPoint(b.x, a.y), a), '#fd942d', 5, 0.5, '#96bdfe', .7);
        //myrectangle = new GPolygon(new Array((35.16707280202653, -90.06179809570312),new GPoint((35.16707280202653, -89.85855102539062)),(35.16931803601131, -89.85855102539062),new GPoint(35.16931803601131, -90.06179809570312),(35.16707280202653, -90.06179809570312)),'#fd942d',1,1,'#96bdfe',.5);
        map.addOverlay(myrectangle);
//        var collectdata = new GLatLngBounds();
//        collectdata.extend(new GLatLng(a.y, a.x));
//        collectdata.extend(new GLatLng(b.y, b.x));
//        map.setCenter(collectdata.getCenter(), map.getBoundsZoomLevel(collectdata));
    }
}

//To reload the Map by removing the drawn Overlays
function reload() {
    map.removeOverlay(myrectangle);
}

