/** EXAMPLE **
new Ajax(url, {
method: 'get',
onRequest: function(){
	alert('foi iniciado o pedido, por exemplo posso mostrar um loading');
	},
	onComplete: function(resp){
	alert('a resposta foi:'+resp);	 
}
}).request();
	* The simple way for an Ajax request, use onRequest/onComplete/onFailure
	* to do add your own Ajax depended code.
*/

var searchword = 'servi';
var lenghtsearch=5;
var currentState;

/// <summary>
/// Shows the Information about a specific workShop
/// </summary>
/// <param name="resp">response from the invocation of a service that contains the information of the WorkShop</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="workShopLinkText">Name of the WorkShop chosen by the user</param>
///
function createWorkShopInfo(resp, divToWrite, workShopLinkText, stateText){
	var workshopInfo = resp.split(';');
	var mdiv = document.getElementById(divToWrite);
	mdiv.innerHTML="<H3 style='display:block'>Detalhe da Oficina " + workShopLinkText + "</H3><p  style='display:block'>";
	for(i=0; i!=workshopInfo.length; ++i){
		mdiv.innerHTML= mdiv.innerHTML+ workshopInfo[i] + '<br/>';
	}
	mdiv.innerHTML= mdiv.innerHTML+"</p><p style='display:block'><a id='backToState' href='#' name='backToState'>&lt;&lt; Regressar a garagens do Concelho de " + stateText + "</a></p>";
	
	$(backToState).addEvent('click', function(e) {
				var url='/SiteMillenniumbcpFortis/RORPages/GetStateWorkShops.aspx?IdConcelho=' + currentState;

				createStateAjax(url,divToWrite, stateText);		
		
			});
}


/// <summary>
/// Creates a list of workshops from a specific district. It also creates the events that will trigger the AJAX calls
/// to get the information for a given workshop
/// </summary>
/// <param name="resp">response from the invocation of a service that, for a given state, returns the workshops</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="stateLinkText">Name of the state chosen by the user</param>
///
function createWorkShopLinksList(resp, divToWrite, stateLinkText){
  var mdiv = document.getElementById(divToWrite);
	if(resp!=""){
		var subSection = resp.substring(lenghtsearch,0).toLowerCase();
		if(subSection==searchword){
			mdiv.innerHTML= resp;
			return;
		}
		var oficinas = resp.split(';');
		mdiv.innerHTML="<div class='conteudo' style='display:block'><H3 style='display:block'>Oficinas Recomendadas no Concelho de " + stateLinkText + "</H3><ul style='margin:0px 12px 6px 18px; display:block'>";
		for(i=0; i!=oficinas.length; ++i){
			var oficina = oficinas[i].split('#');
			mdiv.innerHTML= mdiv.innerHTML+'<li style="margin:0px 0px 0px 0px; marker-offset:2px;display:block;text-align:left;list-style-image:url(/SiteMillenniumbcpFortis/images/bolinha.gif)"><a id="'+ oficina[0] + '_' + oficina[1] + '" href="#" name="'+ oficina[0]+'">'+ oficina[0] + '</a></li>';
		}
		mdiv.innerHTML= mdiv.innerHTML+"</ul></div>";
		for(i=0; i!=oficinas.length; ++i){
			var oficina2 = oficinas[i].split('#');
			$(oficina2[0]).addEvent('click', function(e) {
				var targ;
				if (e.target) 
					targ = e.target;
				else if (e.srcElement) 
					targ = e.srcElement;
				e = new Event(e).stop();
				var aux = targ.id.split('_');
				var url='/SiteMillenniumbcpFortis/RORPages/GetWorkShopInfo.aspx?IdOficina=' + aux[1];
				
				createWorkShopAjax(url,divToWrite, targ.innerText, stateLinkText);							
				
			});
		}
		
	}else{
		mdiv.innerHTML= "Oficinas inexistentes para este concelho";
	}

}

/// <summary>
/// Creates a list of states from a specific district. It also creates the events that will trigger the AJAX calls
/// to get the WorkShops for a given state
/// </summary>
/// <param name="resp">response from the invocation of a service that, for a given district, returns the states</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="districtLinkElem">Link element of the district chosen by the user</param>
///
function createStateLinksList(resp, divToWrite, districtLinkElem){
  var mdiv = document.getElementById(divToWrite);
  if(resp!=""){
		var subSection = resp.substring(lenghtsearch,0).toLowerCase();
		if(subSection==searchword){
			mdiv.innerHTML= resp;
			return;
		}
		var concelhos = resp.split(';');
		mdiv.innerHTML="<div class='conteudo' style='display:block'><H3 style='display:block'>Concelhos do " + districtLinkElem.innerText +"</H3><ul style='margin:0px 12px 6px 18px;display:block'>";
		
		for(i=0; i!=concelhos.length; ++i){
			var concelho = concelhos[i].split('-');
			mdiv.innerHTML= mdiv.innerHTML+'<li style="margin:0px 0px 0px 0px; marker-offset:2px;display:block;text-align:left;list-style-image:url(/SiteMillenniumbcpFortis/images/bolinha.gif)" ><a id="'+ concelho[0] + '_' + concelho[1] +'" href="#" name="'+ concelho[0]+'">'+ concelho[0] + '</a></li>';
		}
		mdiv.innerHTML= mdiv.innerHTML+"</ul></div>";
		for(i=0; i!=concelhos.length; ++i){
			var concelho2 = concelhos[i].split('-');
			$(concelho2[0]).addEvent('click', function(e) {
				var targ;
				if (e.target) 
					targ = e.target;
				else if (e.srcElement) 
					targ = e.srcElement;
				e = new Event(e).stop();
				var aux = targ.id.split('_');
				var url='/SiteMillenniumbcpFortis/RORPages/GetStateWorkShops.aspx?IdConcelho=' + aux[1];
				currentState=aux[1];
				createStateAjax(url,divToWrite, targ.innerText);							
			});
		}
	}else{
		mdiv.innerHTML= "Concelhos inexistentes para este districto";
	}
}

/// <summary>
/// Creates the event that will trigger the AJAX calls for a given district
/// </summary>
/// <param name="linkID">Id of the Link Element to which the event will be added</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
///
function createDistrictEvent(linkID,divToWrite){
	$(linkID).addEvent('click', function(e) {
		var targ;
		if (e.target) 
			targ = e.target;
		else if (e.srcElement) 
			targ = e.srcElement;
		e = new Event(e).stop();
		var aux = targ.id.split('_');					
	
		var url = '/SiteMillenniumbcpFortis/RORPages/GetDistrictStates.aspx?IdDistricto='+aux[1];
		createDistrictAjax(url, divToWrite, targ);		
	
	});
}

/// <summary>
/// Creates the events that will trigger the AJAX calls  for a given workshop
/// </summary>
/// <param name="url">url of the page that will return the workShopInfo</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="workShopLinkText">Name of the WorkShop chosen by the user</param>
///
function createWorkShopAjax(url,divToWrite, workShopLinkText, stateText){
	new Ajax(url, {
		method: 'get', 
		onRequest: function(){
			setWorkShopWait(divToWrite);
		},
		onComplete:function(resp){
			createWorkShopInfo(resp, divToWrite, workShopLinkText, stateText);
		}
	}).request();	
}

/// <summary>
/// Creates the events that will trigger the AJAX calls  for a given state
/// </summary>
/// <param name="url">url of the page that will return the workShops</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="stateLinkText">Name of the state chosen by the user</param>
///
function createStateAjax(url, divToWrite, stateLinkText){
	new Ajax(url, {
		method: 'get', 
		onRequest: function(){
			setStateWait(divToWrite);
		},
		onComplete:function(resp){
			createWorkShopLinksList(resp, divToWrite, stateLinkText);
		}
	}).request();	
}

/// <summary>
/// Creates the events that will trigger the AJAX calls  for a given district
/// </summary>
/// <param name="url">url of the page that will return the states</param>
/// <param name="divToWrite">HTML control where the links will appear</param>
/// <param name="districtLinkElem">Link element of the district chosen by the user</param>
///
function createDistrictAjax(url, divToWrite, districtLinkElem){

	new Ajax(url, {
		method: 'get', 
		onRequest: function(){
			setDistrictWait(divToWrite);
		},
		onComplete:function(resp){
			createStateLinksList(resp,divToWrite, districtLinkElem);
		}
	}).request();	
}

function setDistrictWait(divToWrite){
	var mdiv = document.getElementById(divToWrite);
	mdiv.innerHTML="<H4 style='display:block'>A obter concelhos do districto</H4>";
}

function setStateWait(divToWrite){
	var mdiv = document.getElementById(divToWrite);
	mdiv.innerHTML="<H4 style='display:block'>A obter oficinas do concelho</H4>";
} 

function setWorkShopWait(divToWrite){
	var mdiv = document.getElementById(divToWrite);
	mdiv.innerHTML="<H4 style='display:block'>A obter informa&ccedil;&atilde;o da oficina</H4>";
} 
