/*---- CallBroadenLocalitiesWS.js ----*/

var webServiceBroadenLocalitiesContentType = "application/json; charset=utf-8";
var webServiceBroadenLocalitiesGetNearLocalities = '/WebServices/Locality/BroadenLocalities.asmx/GetNearLocalities';
var webServiceBroadenLocalitiesGetLocalitiesTopPopInDep = '/WebServices/Locality/BroadenLocalities.asmx/GetLocalitiesTopPopInDep';
var webServiceBroadenLocalitiesGetLocalitiesTopPopInRegion = '/WebServices/Locality/BroadenLocalities.asmx/GetLocalitiesTopPopInRegion';

/********************************************* GetNearLocalities ********************************************/
function GetNearLocalitiesCall(zipCode, nearNumber, sector, quoi, combien, idSuggestionDiv, method) {
	if (method == "POST") {
        GetNearLocalitiesCallPOST(zipCode, nearNumber, sector, quoi, combien, idSuggestionDiv);
    }
}

function GetNearLocalitiesCallPOST(zipCode, nearNumber, sector, quoi, combien, idSuggestionDiv) {
	$.ajax({
		type: "POST",
		url: webServiceBroadenLocalitiesGetNearLocalities,
		data: '{ "zipCode" : "' + zipCode + '", "nearNumber" : "' + nearNumber + '"}',
		contentType: webServiceBroadenLocalitiesContentType,
		dataType: "json",
		success: BroadenLocalitiesAjaxCallSucceedJson,
		error: BroadenLocalitiesAjaxCallError,
		sector : sector,
		quoi : quoi,
		combien : combien,
		idSuggestionDiv : idSuggestionDiv
	});
}


/********************************************* GetLocalitiesTopPopInDep ********************************************/
function GetLocalitiesTopPopInDepCall(depDesc, topNumber, sector, quoi, combien, idSuggestionDiv, method) {
	if (method == "POST") {
        GetLocalitiesTopPopInDepCallPOST(depDesc, topNumber, sector, quoi, combien, idSuggestionDiv);
    }
}

function GetLocalitiesTopPopInDepCallPOST(depDesc, topNumber, sector, quoi, combien, idSuggestionDiv) {
	$.ajax({
		type: "POST",
		url: webServiceBroadenLocalitiesGetLocalitiesTopPopInDep,
		data: '{ "depDesc" : "' + depDesc + '", "topNumber" : "' + topNumber + '" }',
		contentType: webServiceBroadenLocalitiesContentType,
		dataType: "json",
		success: BroadenLocalitiesAjaxCallSucceedJson,
		error: BroadenLocalitiesAjaxCallError,
		sector : sector,
		quoi : quoi,
		combien : combien,
		idSuggestionDiv : idSuggestionDiv
	});
}


/********************************************* GetLocalitiesTopPopInRegion ********************************************/
function GetLocalitiesTopPopInRegionCall(regionDesc, topNumber, sector, quoi, combien, idSuggestionDiv, method) {
	if (method == "POST") {
        GetLocalitiesTopPopInRegionCallPOST(regionDesc, topNumber, sector, quoi, combien, idSuggestionDiv);
    }
}

function GetLocalitiesTopPopInRegionCallPOST(regionDesc, topNumber, sector, quoi, combien, idSuggestionDiv) {
	$.ajax({
		type: "POST",
		url: webServiceBroadenLocalitiesGetLocalitiesTopPopInRegion,
		data: '{ "regionDesc" : "' + regionDesc + '", "topNumber" : "' + topNumber + '"}',
		contentType: webServiceBroadenLocalitiesContentType,
		dataType: "json",
		success: BroadenLocalitiesAjaxCallSucceedJson,
		error: BroadenLocalitiesAjaxCallError,
		sector : sector,
		quoi : quoi,
		combien : combien,
		idSuggestionDiv : idSuggestionDiv
	});
}


/********************************************* Result ********************************************/
var LocalitiesArray;

function BroadenLocalitiesAjaxCallSucceedJson(response) {
    if (response != null && response != undefined) {
		LocalitiesArray = response;
		var zipCodes = GetZipCodesForJson();
		GetAntidotFilterCountsCall(this.sector, this.quoi, this.combien, zipCodes, this.idSuggestionDiv, "POST");
    }
    else {
        SuggestAjaxCallError("");
    }
}

function BroadenLocalitiesAjaxCallError(error) {
    $("#" + this.idSuggestionDiv).hide();
    return;
}

/*---- CallLocalityCountsWS.js ----*/

var webServiceLocalityCountsContentType = "application/json; charset=utf-8";
var webServiceLocalityCountsGetCountsDic = '/WebServices/Counts/LocalityCounts.asmx/GetCountsDic';

/********************************************* GetCountsDic ********************************************/
function GetAntidotFilterCountsCall(sector, quoi, combien, zipCodes, idSuggestionDiv, method) {
	if (method == "POST") {
        GetAntidotFilterCountsPOST(sector, quoi, combien, zipCodes, idSuggestionDiv);
    }
}

function GetAntidotFilterCountsPOST(sector, quoi, combien, zipCodes, idSuggestionDiv) {
	$.ajax({
		type: "POST",
		url: webServiceLocalityCountsGetCountsDic,
		data: '{ "sector" : "' + sector + '", "what" : "' + quoi + '", "zipCodesList" : ' + zipCodes + ', "howMuch" : "' + combien + '"}',
		contentType: webServiceLocalityCountsContentType,
		dataType: "json",
		success: LocalityCountsGetCountsDicCallSucceedJson,
		error: LocalityCountsGetCountsDicCallError,
		idSuggestionDiv : idSuggestionDiv
	});
}

function LocalityCountsGetCountsDicCallSucceedJson(response) {
    if (response != null && response != undefined) {
		var hasAds = false;

		for (var i = 0; i < LocalitiesArray.length; i++) {
			var locality = LocalitiesArray[i];
			var zipCode = locality.CodePostal;
			var adsNumber = response[zipCode];
			var urlLocality = GetUrlLocality(locality);
			var idAnchor = "#SuggestionAnchor" + i;
			var idDiv = "#SuggestionDiv" + i;

			if (adsNumber > 0) {
				hasAds = true;
				FillControl(locality, adsNumber, urlLocality, idAnchor);
				$(idDiv).show();
			}
		}
		
		if (!hasAds) {
			$("#" + this.idSuggestionDiv).hide();
		}
	}
    else {
        SuggestAjaxCallError("");
    }
}

function LocalityCountsGetCountsDicCallError(error) {
    $("#" + this.idSuggestionDiv).hide();
    return;
}

/*---- Utility.js ----*/

function GetZipCodesForJson() {
	var zipCodes = '[';
	
	for (var i = 0; i < LocalitiesArray.length; i++) {
		zipCodes += '"' + LocalitiesArray[i].CodePostal + '",';
	}
	
	var length = zipCodes.length;
	
	if (length > 1) {
		zipCodes = zipCodes.substr(0, length - 1);
	}
	
	zipCodes += ']';
	return zipCodes;
}

function FillControl(locality, adsNumber, urlLocality, id) {
	ReplaceInInnerHtml("LOCALITY", locality.DescriptionSnt, id);
	ReplaceInInnerHtml("ZIP_CODE", locality.CodePostal, id);
	var adsNumberLabel = GetAdsNumberLabel(adsNumber);
	ReplaceInInnerHtml("ADS_NUMBER", adsNumberLabel, id);

	var hrefValue = suggestionUrlTemplate; 
	var newHrefValue = Replace(hrefValue, "URL_LOCALITY", urlLocality);
	
	$(id).attr("href", newHrefValue);

}

function ReplaceInAttribut(oldValue, newValue, id, attributeName) {
	var hrefValue = $(id).attr(attributeName);
	var newHrefValue = Replace(hrefValue, oldValue, newValue);
	$(id).attr(attributeName, newHrefValue);
}

function ReplaceInInnerHtml(oldValue, newValue, id) {
	var innerHtml = $(id).html();
	var newInnerHtml = Replace(innerHtml, oldValue, newValue);
	$(id).html(newInnerHtml);
}

function Replace(string, oldValue, newValue) {
	var reg = new RegExp(oldValue, "g");
	return string.replace(reg, newValue);
}

function GetAdsNumberLabel(adsNumber) {
	var adsLabel = " annonces";
	if (adsNumber == 1) {
		adsLabel = " annonce";
	}
	return adsNumber + adsLabel;
}

function GetUrlLocality(locality) {
	var urlLocality = locality.DescriptionIns;

	if (urlLocality == "PARIS") {
		urlLocality = locality.CodePostal;
	} else 	if (locality.Doublon) {
		urlLocality += "+" + locality.IdDepartement.substring(1);
	}

	return CleanUrl(urlLocality);
}

function CleanUrl(value) {
	value = value.toLowerCase();
	value = Replace(value, "'", "-")
	return Replace(value, " ", "-");
}


