
function getBlockAttributes(){
  if(blockCount>0){

  // get div attributes
  var blockLeft = document.getElementById('newBlock').style.left.replace(/px/,"");
  var blockWidth = document.getElementById('newBlock').style.width.replace(/px/,"");
  var blockTop = document.getElementById('newBlock').style.top.replace(/px/,"");
  var blockHeight = document.getElementById('newBlock').style.height.replace(/px/,"");

  // return block vars
  var topLeft1 = parseFloat(blockLeft) + parseFloat(blockWidth);
  var topLeft2 = parseFloat(blockTop) + parseFloat(blockHeight);
  var bottomRight1 = parseFloat(blockLeft);
  var bottomRight2 = parseFloat(blockTop);

  //mapCoords = map.fromDivPixelToLatLng(0,0);
  var mapCoords = map.fromContainerPixelToLatLng(new GPoint(0,0));
  var blockTopLeft = map.fromContainerPixelToLatLng(new GPoint(topLeft1,topLeft2));
  var blockBottomRight = map.fromContainerPixelToLatLng(new GPoint(bottomRight1,bottomRight2));
  
  // calculate surface area and alert
  var rad = 180/Math.PI;
  var blockActualWidth = 3960 * Math.acos(Math.cos((90 - blockTopLeft.y)/rad) * Math.cos((90 - blockBottomRight.y)/rad) + Math.sin((90 - blockTopLeft.y)/rad) * Math.sin((90 - blockBottomRight.y)/rad) * Math.cos((blockTopLeft.y - blockBottomRight.y)/rad));
  var blockActualHeight = 3960 * Math.acos(Math.cos((90 - blockTopLeft.x)/rad) * Math.cos((90 - blockBottomRight.x)/rad) + Math.sin((90 - blockTopLeft.x)/rad) * Math.sin((90 - blockBottomRight.x)/rad) * Math.cos((blockTopLeft.x - blockBottomRight.x)/rad));
  var blockActualAreaKM = Math.round(((blockActualWidth*blockActualHeight)*1.609344)*100)/100;
  var string = '<font color="black"><strong>H&ouml;he: </strong>'+(Math.round(blockActualWidth*100)/100)+' km. <strong>Weite: </strong>'+(Math.round(blockActualHeight*100)/100)+' km.<br /><strong>Fl&auml;che: </strong>';
  if(blockActualAreaKM>301){
    string += '<font color="red">'+blockActualAreaKM+' km&sup2;</font><br /><font color="red">Sollte kleiner als 300 km&sup2; sein</font>';
    document.getElementById('newBlockInfo').style.backgroundImage = 'url(images/nope.gif)';
    if(olderbrowsers=="yes"){
            document.getElementById('newBlock').style.backgroundImage = "url(images/fill_alert.gif)";
    }else{
            document.getElementById('newBlock').style.backgroundImage = "url(images/fill_alert.png)";
    }
  }else{
    string += blockActualAreaKM+' km&sup2;</font>';
    document.getElementById('flash').innerHTML = '';
    document.getElementById('newBlockInfo').style.backgroundImage = 'url(images/yup.gif)';
    if(olderbrowsers=="yes"){
            document.getElementById('newBlock').style.backgroundImage = "url(images/fill.gif)";
    }else{
            document.getElementById('newBlock').style.backgroundImage = "url(images/fill.png)";
    }
  }
  newInfoTop = parseInt(blockHeight) + parseInt(blockTop) + 5;
  document.getElementById('newBlockInfo').style.top = newInfoTop+'px';
  document.getElementById('newBlockInfo').style.left = blockLeft+'px';
  document.getElementById('newBlockInfo').innerHTML = string;
  
  // populate form with coords
  document.form.area.value = blockActualAreaKM;
  document.form.lat1.value = blockTopLeft.y;
  document.form.lon1.value = blockTopLeft.x;
  document.form.lat2.value = blockBottomRight.y;
  document.form.lon2.value = blockBottomRight.x;
  }

}

function addBlock(){
    if(blockCount==0){

        var newBlock = document.createElement('div');
        var mapContainer = document.getElementById('map');
        newBlock.className = 'drsElement drsMoveHandle';
        newBlock.id = 'newBlock';
        newBlock.style.left = '210px';
        newBlock.style.top = '65px';
        newBlock.style.width = '300px';
        newBlock.style.height = '200px';
        if(olderbrowsers=="yes"){
            newBlock.style.backgroundImage = "url(images/fill.gif)";
        }else{
            newBlock.style.backgroundImage = "url(images/fill.png)";
        }
        mapContainer.appendChild(newBlock);
        
        var newBlockInfo = document.createElement('div');
        newBlockInfo.id = 'newBlockInfo';
        newBlockInfo.style.fontFamily = 'font-family:Trebuchet MS,Helvetica,Sans-Serif';
        newBlockInfo.style.backgroundColor = '#ffffff';
        newBlockInfo.style.backgroundRepeat = 'no-repeat';
        newBlockInfo.style.backgroundPosition = '5px 5px';
        newBlockInfo.style.fontSize = '11px';
        newBlockInfo.style.lineHeight = '15px';
        newBlockInfo.style.position = 'relative';
        newBlockInfo.style.width = '200px';
        newBlockInfo.style.padding = '5px 5px 5px 25px';
        mapContainer.appendChild(newBlockInfo);

        function getLocalAttributes(){
            // get div attributes
            var blockLeft = document.getElementById('newBlock').style.left.replace(/px/,"");
            var blockWidth = document.getElementById('newBlock').style.width.replace(/px/,"");
            var blockTop = document.getElementById('newBlock').style.top.replace(/px/,"");
            var blockHeight = document.getElementById('newBlock').style.height.replace(/px/,"");

            // return block vars
            var topLeft1 = parseFloat(blockLeft) + parseFloat(blockWidth);
            var topLeft2 = parseFloat(blockTop) + parseFloat(blockHeight);
            var bottomRight1 = parseFloat(blockLeft);
            var bottomRight2 = parseFloat(blockTop);

            //mapCoords = map.fromDivPixelToLatLng(0,0);
            var mapCoords = map.fromContainerPixelToLatLng(new GPoint(0,0));
            var blockTopLeft = map.fromContainerPixelToLatLng(new GPoint(topLeft1,topLeft2));
            var blockBottomRight = map.fromContainerPixelToLatLng(new GPoint(bottomRight1,bottomRight2));

            document.form.lat1.value = blockTopLeft.y;
            document.form.lon1.value = blockTopLeft.x;
            document.form.lat2.value = blockBottomRight.y;
            document.form.lon2.value = blockBottomRight.x;
            
            
        }
        setTimeout(getLocalAttributes,250);
        blockCount = 1;
        getBlockAttributes();
    }
}

function clearBlock(){
    if(blockCount>0){
        var mapBlock = document.getElementById('newBlock');
        var infoBlock = document.getElementById('newBlockInfo');
        mapBlock.parentNode.removeChild(mapBlock)
        infoBlock.parentNode.removeChild(infoBlock)
        blockCount = 0;
    }
}

function editBlock(lat1,lon1,lat2,lon2){

        var newBlock = document.createElement('div');
        var mapContainer = document.getElementById('map');
        newBlock.className = 'drsElement drsMoveHandle';
        newBlock.id = 'newBlock';
        
        var newBlockInfo = document.createElement('div');
        newBlockInfo.id = 'newBlockInfo';
        newBlockInfo.style.fontFamily = 'font-family:Trebuchet MS,Helvetica,Sans-Serif';
        newBlockInfo.style.backgroundColor = '#ffffff';
        newBlockInfo.style.backgroundRepeat = 'no-repeat';
        newBlockInfo.style.backgroundPosition = '5px 5px';
        newBlockInfo.style.fontSize = '11px';
        newBlockInfo.style.lineHeight = '15px';
        newBlockInfo.style.position = 'relative';
        newBlockInfo.style.width = '200px';
        newBlockInfo.style.padding = '5px 5px 5px 25px';
        mapContainer.appendChild(newBlockInfo);
        
        var divTopLeft = map.fromLatLngToDivPixel(new GLatLng(lat1,lon1));
        var divBottomRight = map.fromLatLngToDivPixel(new GLatLng(lat2,lon2));
        
        newBlock.style.left = divTopLeft.x + "px";
        newBlock.style.top = divTopLeft.y + "px";
        newBlock.style.width = divBottomRight.x - divTopLeft.x + "px";
        newBlock.style.height = divBottomRight.y - divTopLeft.y + "px";
        if(olderbrowsers=="yes"){
            newBlock.style.backgroundImage = "url(images/fill.gif)";
        }else{
            newBlock.style.backgroundImage = "url(images/fill.png)";
        }
        mapContainer.appendChild(newBlock);
        blockCount = 1;
}


