﻿/* *****************************************************
Script para controlos Rede: Funcionalidade Distritos/Concelhos
2009-10-12 * João Carvalho

Usado por: 
Web Site:
	I9A.AdvanceCare.SHWebSite.Controls/_CustomControls_Directorios/DirectorioClinico.ascx
	/Style%20Library/ADV/files/scriptsrede/distritosconcelhos.js


Portal: 
	com.Innovagency.AdvCare.Portal.WsServiceControls/SearchControls/DirectorioClinico.ascx
	/Style%20Library/ADV/files/scriptsrede/distritosconcelhos.js

*******************************************************/

window.i9aDistritosConcelhos= {

	initialize: function( poOptions ) {
	
		
		this.objSelectDistrito = poOptions.moFieldDistrito[0];

		this.objSelectConcelho = poOptions.moFieldConcelho[0];
		this.objConcelho = poOptions.moFieldConcelho;
		this.objFieldConcelhoIndex = poOptions.moFieldConcelhoIndex[0];
		this.objFieldConcelhoValue = poOptions.moFieldConcelhoValue[0];				
		this.objFieldConcelhoText = poOptions.moFieldConcelhoText[0];				
		
		this.objFieldNome = poOptions.moFieldNome[0];
		
		this.chargeConcelhosPostBack();
	
		
	/*
		this.objSeguradora.change(
	       	function(){
	       	    i9aDirectorioClinico.chargeProdutos();
	       		i9aDirectorioClinico.changeSpecialtyOptions();
	       	}
	       );
		   */

	},
	
	clearForm : function() {
		this.objFieldNome.value="";
		this.objSelectDistrito.options[0].selected = true;
		this.objSelectConcelho.options.length = 0;         
		
		//primeiro elemento sem valor 
		var theOption = new Option;                
		theOption.text = "";
		theOption.value = "";                
		this.objSelectConcelho.options[0] = theOption; 
		this.chargeConcelhos();

	},
	
	chargeConcelhosPostBack : function() {     

		if(this.objSelectDistrito.options[this.objSelectDistrito.selectedIndex].text == "") {
			this.objSelectConcelho.selectedIndex = this.objFieldConcelhoIndex.value; 
			return;
		}
		this.objSelectConcelho.options.length = 0;         
		var counter = 1; 

		//primeiro elemento sem valor 
		var theOption = new Option;                
		theOption.text = "";
		theOption.value = "";                
		this.objSelectConcelho.options[0] = theOption; 
		this.chargeConcelhos();       

		for(var i = 0; i < array.length; ++i) {
			var distrito = array[i][0];
			if(distrito == this.objSelectDistrito.options[this.objSelectDistrito.selectedIndex].text) {   
				var theOption = new Option;                
				theOption.text = array[i][1].split(";")[0];
				theOption.value = array[i][1].split(";")[1];                
				this.objSelectConcelho.options[counter] = theOption;    
				this.objSelectConcelho.selectedIndex = this.objFieldConcelhoIndex.value;       
				++counter;
			}            
		}
	},
	chargeConcelhos: function() {        

		var counter = 1; 

		this.objSelectConcelho.options.length = 0;
		//primeiro elemento sem valor 
		var theOption = new Option;                
		theOption.text = "";
		theOption.value = "";                
		this.objSelectConcelho.options[0] = theOption; 
		   
		for(var i = 0; i < array.length; ++i) {
			var distrito = array[i][0];      
					
			if(distrito == this.objSelectDistrito.options[this.objSelectDistrito.selectedIndex].text) {            
				var theOption = new Option;                
				theOption.text = array[i][1].split(";")[0];
				theOption.value = array[i][1].split(";")[1];                
				this.objSelectConcelho.options[counter] = theOption;            
				++counter;
			}            
		}
	},	
	setConcelhoValue : function() { 

		this.objFieldConcelhoValue.value = this.objSelectConcelho.options[this.objSelectConcelho.selectedIndex].value;   
		this.objFieldConcelhoText.value = this.objSelectConcelho.options[this.objSelectConcelho.selectedIndex].text;    
		this.objFieldConcelhoIndex.value = this.objSelectConcelho.selectedIndex;          
	
	}
}

