﻿var pageTitle = "";
var activePlantasSearchArea = "";

var mapGeometry;

function DoGetPageTitle()
{
    return pageTitle;
}
function DoGetMapGeometry()
{
    return mapGeometry;
}
function DoGetMapFeatures(featColId)
{   
    var featCol;
    var features;
    
    var sFeatures = "";
    
    featCol = gMC.doGetFeaturesCollectionById(featColId)        
    
    features = featCol.Features;
        
    if (features.length > 0)
    {    
        feature = features[0];
    
        sFeatures = feature.X + "|" + feature.Y;         
        
        for (var i=1; i < features.length; i++)
        {
            feature = features[i];
            
            sFeatures = sFeatures + "#" + feature.X + "|" + feature.Y         
        }
    }

    return sFeatures;
}

function btn_print_plantas_onclick(tipo, controlUpdate)
{
    pageTitle = "";
    
    //activePlantasSearchArea = "searchArea" + tipo;

    if (tipo == "0")
    {
        DoPlantasGoBack();
    }
    else if (tipo == "1")
    {
        DoPlantasUrbanismo(controlUpdate);
    } else if (tipo == "2") {
        DoPlantasPlanos(controlUpdate);
    } else if (tipo == "3") {
       DoShowPlantaSimples(); 
    }
}

function DoPlantasUrbanismo(controlUpdate)
{
    var tipo="";
    var nomeReq="";
    var nifReq="";
    var geometry="";
    var layers = "planossel";
    var mapState = "";
               
	var lang = "";	
		
	tipo = "g" + document.getElementById("ddl_tipoPlanta").value;
	
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;	
	
	if (document.getElementById("language") != null && document.getElementById("language").value!= "")
	{	
	    lang = document.getElementById("language").value;
	}
	
	if (document.getElementById("txtPlantaNomeReq") != null && document.getElementById("txtPlantaNomeReq").value!= "")
	{
        nomeReq = document.getElementById("txtPlantaNomeReq").value;
    }	
	
	if (document.getElementById("txtPlantaNIFReq") != null && document.getElementById("txtPlantaNIFReq").value!= "")
	{
        nifReq = document.getElementById("txtPlantaNIFReq").value;
    } 
                    
    if (gMC.Graphics.length > 0)
    {
        var g = gMC.Graphics[0];
        var geometry = g.Geometry.AsText();
        
        mapGeometry = geometry;
    }    
    
    var errorMsg = "";
    
    if (nomeReq == null || nomeReq.length == 0)
    {
        errorMsg = "<div style='color: red'>- Nome de Requerente incorrecto.</div>";                
    }          
    if (!validarNIF(nifReq))
    {
        errorMsg = errorMsg +  "<div style='color: red'>- Número de Contribuinte incorrecto.</div>";                        
    }
    if (geometry == null || geometry.length == 0)
    {
        errorMsg = errorMsg + "<div style='color: red'>- Tem que delimitar no mapa a área da pretensão.</div>";
    }
        
    if (errorMsg != null && errorMsg.length > 0)
    {
        document.getElementById("pnl_lista").innerHTML = errorMsg; 
    } else {
        MapaInteractivo.AjaxRequests.panelPlantas.DoPlantasArea(tipo, nomeReq, nifReq, layers, geometry, mapState, lang, controlUpdate, DoPlantasUrbanismo_CallBack);
    
        //MapaInteractivo.AjaxRequests.panelPlantas.DoPlantas(tipo, nomeReq, nifReq, geometry, lang, controlUpdate, DoPlantasUrbanismo_CallBack);
    }
    
}
function DoPlantasUrbanismo_CallBack(response)
{
    ChangePanelStatusToMain();

	if (response == null){	
		
		return;
	}

	if (response.error != null){
	
		alert(response.error);
		return;
	}
	
	if (response.value == null){	
		
		return;
	}
	
	document.getElementById("searchArea1").style.display = "none";		
	
	document.getElementById(response.value[0]).innerHTML = "";
	document.getElementById(response.value[0]).innerHTML = response.value[1];
		
	if (document.getElementById("Info") != null)
	{
	    document.getElementById("Info").innerHTML = "";	
	}	
}

function DoPlantasPlanos(controlUpdate)
{
    var tipo="";
    var geometry=""; 
    var layers = "planossel";
    var mapState = "";  

	var lang = "";
		
	tipo = "g" + document.getElementById("ddl_tipoPlanta").value;
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;	
	
	if (document.getElementById("language") != null && document.getElementById("language").value!= "")
	{	
	    lang = document.getElementById("language").value;
	}	
                
    if (gMC.Graphics.length > 0)
    {
        var g = gMC.Graphics[0];
        var geometry = g.Geometry.AsText();
        
        mapGeometry = geometry;
    }
    
    var errorMsg = "";
    
    if (geometry == null || geometry.length == 0)
    {
        errorMsg = errorMsg + "<div style='color: red'>- Tem que delimitar no mapa a área da pretensão.</div>";
    }
        
    if (errorMsg != null && errorMsg.length > 0)
    {
        document.getElementById("pnl_lista").innerHTML = errorMsg; 
    } else {
        MapaInteractivo.AjaxRequests.panelPlantas.DoPlantasArea(tipo, "", "", layers, geometry, mapState, lang, controlUpdate, DoPlantasPlanos_CallBack);
    }
    
}
function DoPlantasPlanos_CallBack(response)
{
    ChangePanelStatusToMain();

	if (response == null){	
		
		return;
	}

	if (response.error != null){
	
		alert(response.error);
		return;
	}
	
	if (response.value == null){	
		
		return;
	}
	
	document.getElementById("searchArea2").style.display = "none";
	
	document.getElementById(response.value[0]).innerHTML = "";
	document.getElementById(response.value[0]).innerHTML = response.value[1];
		
	if (document.getElementById("Info") != null)
	{
	    document.getElementById("Info").innerHTML = "";	
	}	
}


function DoShowPlanta(grupo, key, numero)
{    
    var url = "forms/planta.aspx?grupo=" + grupo + "&key=" + key + "&numero=" + numero;
    
    window.open(url);
}
function DoShowPlantaSimples()
{

    var scale = document.getElementById("ddl_scalePlanta").value;
    
    if (scale == "mapa")
    {
        scale = DoGetMapScale();
    }   
    
    var url = "forms/planta.aspx?scale=" + scale;
    
    if (gMC.Graphics.length > 0)
    {
        var g = gMC.Graphics[0];
        var geometry = g.Geometry.AsText();
        
        url = url + "&featureGeometry=" + geometry + "&featureType=polygon";
    }
    
    if (document.getElementById("ddl_formatPlanta") != null)
    {
        url = url + "&format=" + document.getElementById("ddl_formatPlanta").value;
    }
    
    if (document.getElementById("ddl_orientationPlanta").value != null)
    {    
        url = url + "&orientation=" + document.getElementById("ddl_orientationPlanta").value;
    }        

    pageTitle = document.getElementById("txt_pageTitle").value;
    mapGeometry = "";
    
    window.open(url);
}

function validarNIF(nif)
{
    var chkDigit;

    if (nif.length == 9)
    {
        var c = nif.substr(0,1);
        
        if (c=="1" || c=="2" || c=="5" || c=="6" || c=="8" || c=="9") //dígitos iniciais válidos
        {
            chkDigit = (c - "0") * 9;
            
            for (i=2;i<=8;i++)
            {
                chkDigit = chkDigit + (nif.substr(i-1,1) - "0") * (10-i);
            }
               
            chkDigit = 11 - (chkDigit % 11);
                
            if (chkDigit >= 10)
                chkDigit = 0;
            
            if (chkDigit == (nif.substr(8,1) - "0"))
                return true;
        
        }
     }
        
     return false;
}

function DoLocalizarPretensao(geomstring)
{
    gMC.Graphics = new Array();
    
    var poly = new _Polygon(); 
    var symbol = new GraphicSymbol();       
    
    poly.FromText(geomstring);
    
    var g = new Graphic(1, "graphics", poly, symbol);
        
    gMC.Graphics[0] = g;
    
    var rect = poly.GetExtent();
    
    zoomExtent(rect[0] - ZOOM_BUFFER, rect[1] - ZOOM_BUFFER, rect[2] + ZOOM_BUFFER, rect[3] + ZOOM_BUFFER);    	 
}

function DoPlantasGoBack()
{
    var ctlId = "searchArea" + document.getElementById("ddl_tipoPlanta").value;

    document.getElementById("pnl_Lista").innerHTML = "";
    
    if(document.getElementById(ctlId) != null)
    {
        document.getElementById(ctlId).style.display = "block"
    }
}

function DoDrawPlantasPolygon()
{
    if ($("#sidebar").attr("panel") == "plantas")
    {
        DoPlantasGoBack();
    }

    DoDrawPolygon(true);
}