if (/msie/i.test (navigator.userAgent)){
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id){
		var elem = document.nativeGetElementById(id);
		if(elem){
			//make sure that it is a valid match on id
			if(elem.id == id){
				return elem;
			}else{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++){
					if(document.all[id][i].id == id){
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

function obj(id){
	return document.getElementById(id);
}

function gup(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )	return "";
  else return results[1];
}

function addOption(oListbox, sName, sValue){
	var oOption = document.createElement("option");				
	oOption.appendChild(document.createTextNode(sName));				
	if (arguments.length == 3)oOption.setAttribute("value", sValue);
	oListbox.appendChild(oOption);				
}	

function checkMultiple(selectBox){							
	var choices=[];
	for( var i = 0; i < selectBox.options.length; i++ ){
		if( selectBox.options[i].selected == 1){
			choices.push( i );
		}
	}
	return choices.length;
}			 							

$(function(){	
	var select_county=false;
	var select_freg=false;
	
	$('#dist, #dist_bottom').change(function(){
		var location='';
		if(this.id.split('_')[1]=='bottom'){
		location='_bottom';
		}
		//alert($(this).val());
		if($(this).val()==0){
			county_cb=document.getElementById('count'+location);
			for (i = county_cb.length - 1; i>=0; i--){
				county_cb.remove(i);
			}
			if(!county_cb.multiple)	addOption(county_cb,"Concelho",0);
			if(document.getElementById('partners'))	document.getElementById('count'+location).disabled=true;
		}
		else{	
			$.ajax({
				type:'GET',
				url:'/gen.pl?p=county',
				data:'district='+escape($(this).val()),
				success:function(data){
					//alert(data);									
					county_cb=document.getElementById('count'+location);
					for (i = county_cb.length - 1; i>=0; i--){
						county_cb.remove(i);
					}
					if(!county_cb.multiple)	addOption(county_cb,"Concelho",0);	
					if(freg_cb=document.getElementById('freg'+location)){
						for (i = freg_cb.length - 1; i>=0; i--){
							freg_cb.remove(i);
						}
						if(!freg_cb.multiple)addOption(freg_cb,"Freguesia",0);
					}	
						
					var counties=data.split(",");									
					
					for(i=0;i<counties.length;i++){
						var label=counties[i].split(';')[0];
						if(counties[i].split(';').length>1)var value=counties[i].split(';')[1];
						else var value=label;
						addOption(county_cb,label,value);
					}
					
					if(!select_county){
						var count_index=unescape(gup('count_index'));	
						var nr_count_index=count_index.split(",");
						if(nr_count_index.length==1 && !isNaN(parseInt(count_index))){
							if((count_index>=0 && county_cb.multiple) || (count_index>0 && !county_cb.multiple)){
								obj('count'+location).selectedIndex=count_index;
								$('#count'+location).trigger('change');
							}
						}
						else if(nr_count_index.length>1){
							for(var i = 0; i < obj('count'+location).options.length; i++){
								var selected=false;
								for(var j=0;j<nr_count_index.length;j++){
									if(nr_count_index[j]==i)
										selected=true;
								}
								
								if(selected)obj('count'+location).options[i].selected=true;
							}
						}
						select_county=true;
					}
		  
		
					//edit User
					if(obj('s_count'+location)){
						if($('#s_count'+location).value!=""){
							var s_index;
							for(i=0; i<obj('count'+location).length - 1; i++){
								if(obj('count'+location).options[i].value==obj('s_count'+location).value){
									s_index=i;
									break;
								}
							}
							obj('count'+location).selectedIndex=s_index;
							$('#count'+location).trigger('change');	
						}
					}
					$('#count'+location).trigger('countloaded');
				}
			});
		}								
	});
	
	
	$('#count, #count_bottom').change(function(){
	  var location='';
	  if(this.id.split('_')[1]=='bottom')location='_bottom';
		if(checkMultiple(this)>1){
			freg_cb=document.getElementById('freg'+location);
			for (i = freg_cb.length - 1; i>=0; i--)	freg_cb.remove(i);
			freg_cb.disabled=true;
		}
		else if(obj('freg'+location)){
			$.ajax({
					type:'GET',
					url:'/gen.pl?p=freg',
					data:'county='+escape($(this).val()),
					success:function(data){
						//alert(data);
						freg_cb=document.getElementById('freg'+location);
						freg_cb.disabled=false;
						count_cb=document.getElementById('count'+location);
						for (i = freg_cb.length - 1; i>=0; i--)freg_cb.remove(i);
						if(!freg_cb.multiple)addOption(freg_cb,"Freguesia",0);
						var fregs=data.split(",");
						for(i=0;i<fregs.length;i++){
							var label=fregs[i].split(';')[0];
							if(fregs[i].split(';').length>1)var value=fregs[i].split(';')[1];
							else var value=label;
							addOption(freg_cb,label,label);
						}
						if(!select_freg){							
							var freg_index=unescape(gup('freg_index'));	
							var nr_freg_index=freg_index.split(",");
							if(nr_freg_index.length==1 && !isNaN(parseInt(freg_index))){
								if((freg_index>=0 && freg_cb.multiple) || (freg_index>0 && !freg_cb.multiple)){
									obj('freg'+location).selectedIndex=freg_index;
									$('#freg'+location).trigger('change');
								}
							}
							else if(nr_freg_index.length>1){
								for(var i = 0; i < obj('freg'+location).options.length; i++){
									var selected=false;
									for(var j=0;j<nr_freg_index.length;j++){
										if(nr_freg_index[j]==i)selected=true;
									}
									if(selected)obj('freg'+location).options[i].selected=true;
								}
							}		
							select_freg=true;
						}
								
								
						//edit User
						if(obj('s_freg'+location)){
							if($('#s_freg'+location).value!=""){
								var s_index;
								for(i=0; i<obj('freg'+location).length - 1; i++){
										  if(obj('freg'+location).options[i].value==obj('s_freg'+location).value){
											  s_index=i;
											  break;
										  }
								}
									  obj('freg'+location).selectedIndex=s_index;
							}
						}
						
						$('#freg'+location).trigger('fregloaded');
						
					}
			});
		}

		if(obj('zone'+location)){
			var district = document.getElementById('dist'+location);
			$.ajax({
					type:'GET',
					url:'/gen.pl?p=zone',
					data:'district='+escape($(district).val())+'&county='+escape($(this).val()),
					success:function(data){
						//alert(data);
					
						var allzones = [];
						zone_cb=document.getElementById('zone'+location);
						zone_cb.disabled=false;
						for (i = zone_cb.length - 1; i>=0; i--)zone_cb.remove(i);
						if(!zone_cb.multiple)addOption(zone_cb,"Zona",0);
						var zones=data.split(",");
						for(i=0;i<zones.length;i++){
							//var label=zones[i].split("|")[0];
							//var value=zones[i].split("|")[1];
							var label = zones[i].split(";");
							//allzones.push(label);
							//alert(label);
							for(j=0;j<label.length;j++){
							//	alert(label[j]);
							//	addOption(zone_cb,label[j],label[j]);
								allzones.push(label[j]);
							}
						}
						allzones.sort();
						for(i=0;i<allzones.length;i++){
							addOption(zone_cb,allzones[i],allzones[i]);
						}
					}
			});
		}

	});
});		

function typologyStatus(elem,location){				
	var elemValue=elem.options[elem.selectedIndex].value;
	location = location || '';
	//alert(elemValue);
	if(obj('refopt'+location) && obj('refopt'+location).checked==true) return; //if search by reference checkbox;
	if(obj('ref'+location) && obj('ref'+location).value) return; //if search by reference value;

	if(elemValue=='Apartamento' || elemValue=='Moradia'){		
		if(obj('typmin'+location))obj('typmin'+location).disabled=false;
		if(obj('typmax'+location))obj('typmax'+location).disabled=false;	
		if(obj('typology'+location))obj('typology'+location).disabled=false;								
	}
	else{
		if(obj('typmin'+location)){
			obj('typmin'+location).selectedIndex=0;
			obj('typmin'+location).disabled=true;
		}
		
		if(obj('typmax'+location)){				
			obj('typmax'+location).selectedIndex=0;
			obj('typmax'+location).disabled=true;
		}		
		
		if(obj('typology'+location)){				
			obj('typology'+location).selectedIndex=0;
			obj('typology'+location).disabled=true;
		}																		
	}
	
}		

$(function(){
	if(obj('ikind')){
		$('#ikind').trigger('change');
	}
	if(obj('ikind_bottom')){
		$('#ikind_bottom').trigger('change');
	}
});

