// JavaScript Document
$(document).ready(function() {
	$("#zipcode").css('text-transform','uppercase').mask('a9a 9a9').autocomplete({
		//define callback to format results
		source: function(req, add){
			//pass request to server
			$.getJSON('/inc/ajax/geo_getZipcodes.php?callback=?', req, function(data) {							
				//create array for response objects
				var suggestions = [];							
				//process response
				$.each(data, function(i, val) {
					suggestions.push(val.name);
				});							
				//pass array to callback
				add(suggestions);
			});
		},
		select: function(event, ui) {
			geo_displayValues('zipcode',$(this).val());
		}
	}).blur(function() {
		geo_getZipcodes('zipcode',$(this).val());
	});
	$('#province').change(function() {
		//$('#zipcode').val('');
		$('#zipcode_check').val('1');
		geo_getZipcodes('province_id',$(this).val());
	});
	geo_getZipcodes('province_id',$('#province').val());
});
function geo_switchCities(province_id,lang,hint) {
	if(province_id=='') return false;
	$.ajax({
		type: "POST",
		url: "/inc/ajax/geo_switchCities.php",
		data: {
			province_id: province_id,
			lang:lang,
			hint:hint
		},
		error: function(xhr, ajaxOptions, thrownError){
			//alert(xhr.status);
		},
		success: function(data){
			//alert(xml);
			$('#cities_dd').html(data);
		}
	});
	return true;
}
function geo_getZipcodes(sTrigger,sValue){					
	if ($('#zipcode_check').val() != $('#zipcode').val()) {
		
		$('#zipcode').addClass('ajx-loading');//.attr('disabled','disabled');
		//$('#province').attr('disabled','disabled');
		//$('#city').attr('disabled','disabled');
		$.get('/inc/ajax/geo_getCities.php', {
			sTrigger:sTrigger,
			sValue: sValue
		}, function(data){
			var s=data.split('__CITIES__');
			$('#city').html("");
			$(s[1]).appendTo('#city');
			
			if ($("#province option[value='" + s[0] + "']").attr('selected') != "selected") {
				$('#province').children('option').removeAttr('selected');
				$("#province option[value='" + s[0] + "']").attr('selected', 'selected');
			}
			//$('#city').removeAttr('disabled');
			//$('#province').removeAttr('disabled');
			$('#zipcode').removeClass('ajx-loading');//.removeAttr('disabled');
		});
	}
	$('#zipcode_check').val($('#zipcode').val());
}
function geo_displayValues(sTrigger,sValue){					
	if ($('#zipcode_check').val() != $('#zipcode').val()) {
		
		$('#zipcode').addClass('ajx-loading');//.attr('disabled','disabled');
		//$('#province').attr('disabled','disabled');
		//$('#city').attr('disabled','disabled');
		$.get('/inc/ajax/geo_getCities.php', {
			sTrigger:sTrigger,
			sValue: sValue
		}, function(data){
			var s=data.split('__CITIES__');
			$('#city').html("");
			$(s[1]).appendTo('#city');
			
			if ($("#province option[value='" + s[0] + "']").attr('selected') != "selected") {
				$('#province').children('option').removeAttr('selected');
				$("#province option[value='" + s[0] + "']").attr('selected', 'selected');
			}
			//$('#city').removeAttr('disabled');
			//$('#province').removeAttr('disabled');
			$('#zipcode').removeClass('ajx-loading');//.removeAttr('disabled');
		});
	}
	$('#zipcode_check').val($('#zipcode').val());
}
