var municiaplityArray = new Array();

function sortCallback(a, b)
{
	return (a[1] < b[1]) ? -1 : 1;
}

window.addEvent('domready', function(){	
	var apiUri = "documents/25e88d64-7560-4e7e-9d67-e343fff30f37/view_binary";		
	var selectName = "Select";
	
	if($('ps-municipality').hasClass("fr")){
		selectName = "Choisissez une";
	}
	
	var ajax = new ApiClient({
	    'apimethod': apiUri,
	    'method': 'get',		        	    
	    onSuccess: function(responseText){		    	
	    	var municipalities = responseText.split('\r\n');	    		    	
			$each(municipalities, function(municipalityString){											
				var municipality = municipalityString.split(';');												
				municiaplityArray[municiaplityArray.length] = municipality;
			});
			municiaplityArray.sort(sortCallback);
	    }
    });
    
    $('ps-muni-homepage-province').addEvent('change', function(){        	
    	if(this.value.length > 0){
	    	var results = $('ps-muni-homepage-locations');
	    	results.empty();
	    	empty = new Element('option', {
				'value' : "",
				'html' : selectName
			});
			
			results.appendChild(empty);
					
	    	for(var i = 0; i < municiaplityArray.length; i++){
	    		
	    		if(municiaplityArray[i][2] == this.value){
	    			var municipalityElement;
	    			
	    			if($('ps-municipality').hasClass("municipality-choice-full")){
		    			municipalityElement = new Element('option', {
							'value' : municiaplityArray[i][4],
							'html' : municiaplityArray[i][1]							
						});	
						
    				}else{
    					municipalityElement = new Element('option', {
							'value' : municiaplityArray[i][0],
							'html' : municiaplityArray[i][1]
						});					
    				}
	    			
					
					results.appendChild(municipalityElement);
	    		}
	    	}	
		}    	
    });
    /*
    $('ps-muni-homepage-areacode').addEvent('change', function(){    	
    	if(this.value.length > 0){
	    	var results = $('ps-muni-homepage-locations');
	    	results.empty();
	    	
	    	empty = new Element('option', {
				'value' : "",
				'html' : selectName
			});
			
			results.appendChild(empty);
					
	    	for(var i = 0; i < municiaplityArray.length; i++){    		
	    		if(municiaplityArray[i][3] == this.value){
	    			var municipalityElement = new Element('option', {
						'value' : municiaplityArray[i][0],
						'html' : municiaplityArray[i][1]
					});
					
					results.appendChild(municipalityElement);	
	    		}
	    	}
    	}   	
    });
    */
    $('ps-muni-homepage-locations').addEvent('change', function(){    		    		
		if(this.value.length > 0){    			
    		$('submitMunicipality').set('disabled', '');
    		//$('submitMunicipalityDonate').set('disabled', '');	
		}else{		
			$('submitMunicipality').set('disabled', 'disabled');
			//$('submitMunicipalityDonate').set('disabled', 'disabled');			
		}
		
	});
	
	$('submitMunicipality').addEvent('click', function(){    
		var langPref = "";					
		if($('ps-municipality').hasClass("fr")){
			langPref = "&amp;LangPref=fr-CA";
		}					
		
		if($('ps-municipality').hasClass("municipality-choice-full")){			
			var url = "https://secure.e2rm.com/registrant/donate.aspx?EventID=32495&SPID=" + $('ps-muni-homepage-locations').value	
				+ langPref;							
		}else{			
			var url = "https://secure.olympicfoundation.ca/registrant/startup.aspx?locationID=" + $('ps-muni-homepage-locations').value 		
			+ langPref + "&getPage=xxx";											
		}
			
		window.location = url;
	});
	
	if($('ps-muni-homepage-province').value.length > 0 && $('ps-muni-homepage-province').value != 'Select'){		
		$('ps-muni-homepage-province').fireEvent('change', null, 2000);
	}
	
	if (Browser.Engine.trident ) {
		$('ps-muni-homepage-locations').addEvents({
			'mousedown': function(){
				this.addClass('ie-select');	
			},
			'blur': function(){
				this.removeClass('ie-select');
			},
			'change': function(){
				this.removeClass('ie-select');
			}
		});
	}
});