﻿dojo.require("esri.tasks.identify");

var RESThostName = "http://geowebgis.rest.irisnet.be/";
var ratesREST = "renoveR3"
var identifyTask, identifyParams;
var spatialReferenceLambert72 = 31370;
var lng = 0; // 0: "french" 1: "flemish"


var zone, rates, localisation, embelisOutzone;
//var CONTRAT_QUARTIER_SERIES = ["(2008-2012)", "(2007-2011)", "(2006-2010)", "(2005-2009)"];
var CONTRAT_QUARTIER_SERIES = ["(2009-2013)", "(2008-2012)", "(2007-2011)", "(2006-2010)"]

// initialize Identify Task functionality
function initIdentifyTask() {
	var layerIds = [0, 1]; // CONTRAT_QUARTIER (0), EDRLR (1)

	identifyTask = new esri.tasks.IdentifyTask(RESThostName + "/" + ratesREST + "/MapServer");
	identifyParams = new esri.tasks.IdentifyParameters();
	identifyParams.layerIds = layerIds;
	identifyParams.tolerance = 3;
	identifyParams.returnGeometry = false;
	identifyParams.spatialReference = spatialReferenceLambert72;
	identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
}

function updateRates(xpoint, ypoint, rue, numero, cp) {

	initIdentifyTask()
	identifyParams.geometry = new esri.geometry.Point(xpoint, ypoint);
	identifyParams.geometry.setSpatialReference("");
	identifyParams.mapExtent = new esri.geometry.Extent(xpoint - 50, ypoint - 50, xpoint + 50, ypoint + 50);

	identifyTask.execute(identifyParams, function(idResults) {
		getRenoveRates(idResults, rue, numero, cp);
	});
}


function getRenoveRates(idResults, rue, numero, cp) {

	

	//Valeurs si hors périmètre
	zonehabitationID = 2;
	msg = $("#lb_msgnonedrlr").html();
	indice_zonehabitation = 1;
				
	for (var i = 0, il = idResults.length; i < il; i++) {
		
		var idResult = idResults[i];
		switch (idResult.layerId) {
			case 0: // CONTRAT_QUARTIER
				var serie = idResult.feature.attributes['SERIE'];
				var found = lookup(CONTRAT_QUARTIER_SERIES, serie);
			
				if (found) {
					msg = $("#lb_msgcontratquartier").html();
					zonehabitationID = 3;
					indice_zonehabitation = 1.1;
				}
				break;
			case 1: // EDRLR
				// Dans le cas d'habitation à la fois contrat de quartier et EDRLR, on prend le plus avantageux
				//càd le contrat de quartier
				if (zonehabitationID != 3){
					zonehabitationID = 1;
					msg = $("#lb_msgedrlr").html();
					indice_zonehabitation = 1.1;
					
				}
				break;
			
				
		}
	}

	$("#rue").val(rue);
	$("#numero").val(numero);
	$("#cp").val(cp);
	$("#zone").html(msg);
	CalcTauxReno();
	
	return ;
}

// string substitution
// elements to substitute: {0} {1} ...
function substitute() {
	var args = substitute.arguments;
	var Base = args[0];
	var Seek, Len, ix1, ix2, ix3;
	for (ix1 = 1; ix1 < args.length; ix1++) {
		ix2 = ix1 - 1;
		Seek = '{' + ix2 + '}';
		if ((ix3 = Base.indexOf(Seek)) > -1) {
			Len = Seek.length;
			Base = Base.substring(0, ix3) + args[ix1] + Base.substring(ix3 + Len);
		}
	}
	return Base;
}
// checks if a string exists in an array
function lookup(arrayString, aString) {
	for (var idx = 0; idx < arrayString.length; idx++) {
		if (aString.indexOf(arrayString[idx]) != -1) {
			return true;
		}
	}
	return false;
}