var width_city         = 187;
var width_buildingname = 187;
var gv_address2Default = 'Seberang Tugu Monas';
var gv_addressnameDefault = 'Home, or Office';

//edit profile personal
var account_summary_index = 0;
//child : manage location, manage credit card, manage basic info
var profile_index = 1;
//child : goorme
var preference_index = 2;

function editProfileInit(accordion_index, type) {

	// handle input change
	$('form input, form textarea, form select').change(function() {
		inputChanged = true;
	});

	if (type == 'user') {
		initAccordion('mm_navigation', accordion_index);
	}

	var validation_type = '';
	var form_id         = '';
	//$('.edit_profile_acc').unbind('click');
	$('.edit_profile_acc').click(function() {

		var current_link = $(this);
		var id = current_link.attr('id');
		//initAccordion('mm_navigation', accordion_index);
		if(id == 'profile_acc') {
			if(type == 'user') 
				changeEditProfileBody($('#user_profile_basic_link'));
			else
				changeEditProfileBody($('#business_profile_basic_link'));
		}
		else if(id == 'preference_acc')
			changeEditProfileBody($('#user_preference_goorme_link'));
		
	});
	
	$('.change_edit_profile_body').unbind('click');
	$('.change_edit_profile_body').click(function() {

		// check for input change first
		if (inputChanged) {
			//alert('changed!');
		}

		var current_link = $(this);

		changeEditProfileBody(current_link);

	});

	// submit the displayed form
	$('#mm_general_submit_button').unbind('click');
	$('#mm_general_submit_button').click(function() {
		var rel = $(this).attr('rel');

		$('#' + rel).submit();

		// set inputChanged
		inputChanged = false;

		return false;
	});

	$('#mm_general_cancel_button').click(function() {
		var rel = $(this).attr('rel');

		$('#' + rel + '_link').click();

		return false;
	});

	// building name
	focusBlurDefault('#AddressAddressname', 'Home, or Office', 'mm_watermark');
	focusBlurDefault('#AddressAddress2', 'Seberang Tugu Monas', 'mm_watermark');

}

function changeEditProfileBody(current_link) {

	var rel = current_link.attr('rel');
	var id  = current_link.attr('id');

	var forceContinue = true;
	if (inputChanged) {
		forceContinue = confirm('You have unsaved change(s). Continue?');
	}

	if (forceContinue) {
		inputChanged = false;

		// remove selected notification
		if (rel != 'user_profile_location_add') {
			$('.mm_menu_child li').removeClass('mm_selected');
		}

		// remove error & form message
		$('.mm_errormessage').css('display', 'none');
		$('.mm_formmessage').css('display', 'none');

		// footer
		var use_footer = false;
		var use_footer_cancel = false;

		// change the variables
		var content_title = '';
		var form_id = '';
		var return_target = '';
		var selected = rel;
		
		switch(rel) {
			case 'user_account_summary':
				content_title = 'account summary';
				use_footer    = false;
				break;

			case 'user_profile_basic':
				content_title   = 'profile: basic';
				form_id         = 'user_profile_basic_form';
				validation_type = 'user.profile.basic';
				use_footer      = true;
				break;

			case 'user_profile_location':
				$('#mm_navigation').accordion("activate" , profile_index);
				content_title = 'profile: location';
				use_footer    = false;
				break;

			case 'user_profile_location_add':
				content_title     = 'profile: location: add';
				form_id           = 'user_profile_location_add_form';
				validation_type   = 'user.profile.location.add';
				use_footer        = true;
				use_footer_cancel = true;
				return_target     = 'user_profile_location';

				break;

			case 'user_profile_location_edit':
				content_title     = 'profile: location: edit';
				form_id           = 'user_profile_location_edit_form';
				validation_type   = 'user.profile.location.edit';
				use_footer        = true;
				use_footer_cancel = true;
				return_target     = 'user_profile_location';

				var address_id = id.split('-')[1];
				$('#user_profile_location_edit').html('');
				$('#user_profile_location_edit').load('/addresses/edit/' + address_id + '/type:user');

				break;
				
			case 'user_profile_webservice':
				content_title = 'social network';
				use_footer    = false;
				break;

			case 'user_profile_creditcard':
				$('#mm_navigation').accordion("activate" , profile_index);
				content_title = 'member/credit card';
				use_footer    = false;
				break;

			case 'user_preference_goorme':
				$('#mm_navigation').accordion('activate' , preference_index);
				content_title   = 'preference: goorme';
				form_id         = 'user_preference_goorme_form';
				validation_type = 'user.preference.goorme';
				use_footer      = true;
				break;

			case 'business_account_summary':
				content_title = 'account summary';
				use_footer    = false;
				break;

			case 'business_profile_basic':
				content_title   = 'profile: basic';
				form_id         = 'business_profile_basic_form';
				validation_type = 'business.profile.basic';
				use_footer      = true;
				break;

			case 'business_profile_location':
				content_title = 'profile: location';
				use_footer    = false;
				break;

			case 'business_profile_location_add':
				content_title     = 'profile: location: add';
				form_id           = 'business_profile_location_add_form';
				validation_type   = 'business.profile.location.add';
				use_footer        = true;
				use_footer_cancel = true;
				return_target     = 'business_profile_location';
				selected = 'business_profile_location';
				break;

			case 'business_profile_location_edit':
				content_title     = 'profile: location: edit';
				form_id           = 'business_profile_location_edit_form';
				validation_type   = 'business.profile.location.edit';
				use_footer        = true;
				use_footer_cancel = true;
				return_target     = 'business_profile_location';

				var address_id = id.split('-')[1];
				$('#business_profile_location_edit').load('/addresses/edit/' + address_id);
				selected = 'business_profile_location';
				break;

			case 'business_profile_location_hour_edit':
				content_title     = 'profile: location: hour: edit';
				form_id           = 'business_profile_location_hour_edit_form';
				validation_type   = 'business.profile.location.hour.edit';
				use_footer        = true;
				use_footer_cancel = true;
				return_target     = 'business_profile_location';

				var address_id = id.split('-')[1];
				$('#business_profile_location_hour_edit').load('/businesshours/get_by_address/' + address_id);

				break;

			case 'business_profile_staff':
				content_title = 'profile: staff & roles';
				user_footer   = false;
				break;
		}

		// add selected notification
		$('#' + selected + '_link').parent('li').addClass('mm_selected');

		// set footer display
		if (use_footer) {
			$('#mm_footer').removeClass('dev_mm_nodisplay');
		} else {
			$('#mm_footer').addClass('dev_mm_nodisplay');
		}

		if (use_footer_cancel) {
			$('#mm_general_cancel_button').removeClass('dev_mm_nodisplay');
			$('#mm_general_cancel_button').attr('rel', return_target);

		} else {
			$('#mm_general_cancel_button').addClass('dev_mm_nodisplay');
			$('#mm_general_cancel_button').removeAttr('rel');
		}

		// fill the content title
		$('#mm_header h2.mm_titleline').text(content_title);

		if (form_id.length > 0) {
			// set action for clicking submit
			$('#mm_general_submit_button').attr('rel', form_id);

			// set validation
			editProfileSetValidationSettings(validation_type, form_id);
		}

		// fill the content body
		$('#mm_body .mm_body_child').css('display', 'none');
		$('#' + rel).css('display', 'block');

		resizeModal('#mm_modalv3');
		setTimeout("resizeModal('#mm_modalv3');", 500);

	}
}

function userEditProfileInit(accordion_index) {

	editProfileInit(accordion_index, 'user');

	// password form handler
	$('#edit_profile_change_password_link').unbind('click');
	$('#edit_profile_change_password_link').click(function() {
		$('#edit_profile_change_password_container').css('display', 'block');
		resizeModal('#mm_modalv3');
	});

	$('#edit_profile_change_password_cancel').click(function() {
		$('#edit_profile_change_password_container').css('display', 'none');
		resizeModal('#mm_modalv3');
		return false;
	});

	$('#edit_profile_change_password_form').ajaxForm({
		success: function(data) {
			$('.mm_formmessage').css('display', 'none');
			$('.mm_errormessage').css('display', 'none');
			if(data == 1) {
				reloadUserEditProfileContent('profile.edit', 0);
				$('.mm_formmessage').text('');
				$('.mm_errormessage').text('');
			}
			else if (data == 2) {
				$('.mm_errormessage').text("New password and the confirm password doesn't match");
				$('.mm_errormessage').css('display', 'block');

			} else if (data == 3) {
				$('.mm_errormessage').text("Invalid old password");
				$('.mm_errormessage').css('display', 'block');

			} else if (data == 4) {
				$('.mm_errormessage').text("Please provide old, new, and confirmation password");
				$('.mm_errormessage').css('display', 'block');
			}
		},
		beforeSerialize: function() {
			$('.mm_errormessage').css('display', 'none');
			$('.mm_formmessage').html('processing...');
			$('.mm_formmessage').css('display', 'block');
		}
	});

	focusBlurDefault('#cardListInputBox', 'Start with Bank Name', 'mm_watermark');

	// credit card click handler
	$('.edit_profile_creditcard_delete_link, #edit_profile_creditcard_delete_cancel').unbind('click');
	$('.edit_profile_creditcard_delete_link, #edit_profile_creditcard_delete_cancel').click(function() {
		var rel = $(this).attr('rel');

		displaySwitch('#edit_profile_creditcard_container-' + rel, '#edit_profile_creditcard_delete_container-' + rel);

		return false;
	});

	// ajax form
	$('#edit_profile_add_card_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			$("#msgErrorAddCard").css("display", "none");
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Saving...');
		},
		success: function(data) {
			if(data == 0)
			{
				$("#msgErrorAddCard").css("display", "block");
				$("#cc_submitter").removeAttr('disabled');
				$("#cc_submitter").val('ADD');
			}
			else
			{
				reloadUserEditProfileContent('profile.creditcard', 1);
			}
		}
	});
	$('.edit_profile_card_delete_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_hollowbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Deleting...');
		},
		success: function() {
			reloadUserEditProfileContent('profile.creditcard', 1);
		}
	});

	// location

	// set as main
	$('.user_profile_location_set_main_link').unbind('click');
	$('.user_profile_location_set_main_link').click(function() {
		var rel = $(this).attr('rel');

		$.get('/addresses/set_main/user/' + rel, function() {
			reloadUserEditProfileContent('profile.location', 1);
		});
	});

	// delete
	$('.user_profile_location_delete_link, .user_profile_location_delete_cancel').unbind('click');
	$('.user_profile_location_delete_link, .user_profile_location_delete_cancel').click(function() {
		var rel = $(this).attr('rel');

		$('#user_profile_location_delete_container-' + rel).toggle();

		return false;
	});

	$('.user_profile_location_delete_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_hollowbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Deleting...');
		},
		success: function() {
			reloadUserEditProfileContent('profile.location', 1);
		}
	});

}

function businessEditProfileInit(accordion_index) {

	editProfileInit(accordion_index, 'business');

	// staff & roles handler
	// add
	$('#business_profile_staff_add_link, #business_profile_staff_add_cancel').unbind('click');
	$('#business_profile_staff_add_link, #business_profile_staff_add_cancel').click(function() {
		displaySwitch('#business_profile_staff_add_container', '#business_profile_staff_add_link');
	});

	// edit
	$('.business_profile_staff_edit_link, #business_profile_staff_edit_cancel').unbind('click');
	$('.business_profile_staff_edit_link, #business_profile_staff_edit_cancel').click(function() {
		var rel = $(this).attr('rel');

		displaySwitch('#business_profile_staff_container-' + rel, '#business_profile_staff_edit_container-' + rel);

		return false;
	});

	$('.business_profile_staff_edit_form').ajaxForm({

		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_hollowbutton').remove();
			jqForm.find('.mm_solidbutton').val('Saving...');
		},
		success: function() {
			reloadBusinessEditProfileContent('profile.staff', 1);
		}
	});


	// delete
	$('.business_profile_staff_delete_link, .business_profile_staff_delete_cancel').unbind('click');
	$('.business_profile_staff_delete_link, .business_profile_staff_delete_cancel').click(function() {
		var rel = $(this).attr('rel');

		displaySwitch('#business_profile_staff_container-' + rel, '#business_profile_staff_delete_container-' + rel);

		return false;
	});



	$('.business_profile_staff_delete_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_hollowbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Deleting...');
		},
		success: function() {
			reloadBusinessEditProfileContent('profile.staff', 1);
		}
	});

	// search
	$('#business_profile_search_staff_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Searching...');
		},
		success: function(data) {
			$('#business_profile_search_staff_form .mm_solidbutton').removeAttr('disabled');
			$('#business_profile_search_staff_form .mm_solidbutton').val('Search');

			$('#business_profile_staff_add_container').before(data);
			$('#business_profile_staff_add_container').toggle();
		}
	});

	// location

	// set as main
	$('.business_profile_location_set_main_link').unbind('click');
	$('.business_profile_location_set_main_link').click(function() {
		var rel = $(this).attr('rel');

		$('.mm_summary').fadeOut(5000);

		$.get('/addresses/set_main/business/' + rel, function() {
			reloadBusinessEditProfileContent('profile.location', 1);
		});
	});

	// delete
	$('.business_profile_location_delete_link, .business_profile_location_delete_cancel').unbind('click');
	$('.business_profile_location_delete_link, .business_profile_location_delete_cancel').click(function() {
		var rel = $(this).attr('rel');

		$('#business_profile_location_delete_container-' + rel).toggle();

		return false;
	});

	$('.business_profile_location_delete_form').ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			jqForm.find('.mm_solidbutton').attr('disabled', true);
			jqForm.find('.mm_hollowbutton').attr('disabled', true);
			jqForm.find('.mm_solidbutton').val('Deleting...');
		},
		success: function() {
			reloadBusinessEditProfileContent('profile.location', 1);
		}
	});

}

function initAccordion(element_id, active_element) {

	// simple accordion
	$('#' + element_id).accordion({
		header: '.mm_menu_head',
		autoheight: false,
		active: active_element
	});
}

function reloadUserEditProfileContent(source, accordion_index) {

	$.get('/users/edit',
		{
			source: source
		},
		function(data) {
			$('#mm_modalv3').replaceWith(data);
			userEditProfileInit(accordion_index);
		}
	);
}

function reloadBusinessEditProfileContent(source, accordion_index) {

	$.get('/businesses/edit2',
		{
			source: source
		},
		function(data) {
			$('#mm_modalv3').replaceWith(data);
			businessEditProfileInit(accordion_index);
		}
	);
}

function editProfileSetValidationSettings(type, form_id) {

	// validation
	var validationSettings = sitoesModalValidationSettings;

	switch(type) {

		case 'user.profile.basic':

			// validation rules
			validationSettings.rules = {
				'data[User][firstname]': 'required',
				'data[User][gender]'   : 'required',
				'data[User][dob][year]': {
					'multiplefields': ['#UserDobDay','#UserDobMonth','#UserDobYear']
				},
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadUserEditProfileContent('profile.basic', 1);
					}
				});

				return false;
			};

			break;

		case 'user.profile.location.add':

			// validation rules
			validationSettings.rules = {
				'data[Address][address1]': 'required',
				'data[Address][city]'    : 'required',
				'data[Address][postalcode]': {
					mynumber: "AddressPostalcode"
				}
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				removeWatermarkValue('#AddressAddress2', gv_address2Default);
				removeWatermarkValue('#AddressAddressname', gv_addressnameDefault);
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadUserEditProfileContent('profile.location', 1);
					}
				});

				return false;
			};

			break;

		case 'user.profile.location.edit':

			// validation rules
			validationSettings.rules = {
				'data[Address][address1]': 'required',
				'data[Address][city]'    : 'required',
				'data[Address][postalcode]': {
					mynumber: "EditAddressPostalcode"
				}
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				removeWatermarkValue('#AddressAddress2', gv_address2Default);
				removeWatermarkValue('#AddressAddressname', gv_addressnameDefault);
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadUserEditProfileContent('profile.location', 1);
					}
				});

				return false;
			};

			break;

		case 'user.preference.goorme':

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadUserEditProfileContent('preference.goorme', 2);
					}
				});

				return false;
			};

			break;

		case 'business.profile.basic':
		
			var url_title = $('#BusinessUrlTitle').val();
			//validation rules
			validationSettings.rules = {
				
				'data[Business][url]': {
					'urlwithdefault': 'http://'
				},
				'data[Business][businessname]'		: 'required',
				'data[Businessmeta][1][metavalue]'	: 'required',
				'data[Businessmeta][0][metavalue]'	: 'required',
				'data[Business][url_title]': {
					'remote'       : '/businesses/ajax_check/url_title/' + url_title
				},
			};
			
			validationSettings.messages = {
				'data[Business][url_title]': {
					'remote': 'The url is taken.'
				}
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						if(from == "detailpage")
						{
							$.nyroModalRemove();
							location.reload();
						} 
						else if(from == 'admin')
						{
							$.nyroModalRemove();
							location.reload();
						}
						else
						{
							var rel = $('#' + form_id).attr('rel');
							reloadBusinessEditProfileContent('profile.basic', 1);
						}
					}
				});

				return false;
			};

			break;

		case 'business.profile.location.add':

			// validation rules
			validationSettings.rules = {
				'data[Address][address1]'  : 'required',
				'data[Address][city]'      : 'required',
				'data[Address][postalcode]': {
					mynumber: "AddressPostalcode"
				},
				'data[Businessphone][0][phonenumber]': {
					mynumber: "Businessphone0Phonenumber"
				},
				'data[Businessphone][1][phonenumber]': {
					mynumber: "Businessphone1Phonenumber"
				}
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				removeWatermarkValue('#AddressAddress2', gv_address2Default);
				removeWatermarkValue('#AddressAddressname', gv_addressnameDefault);
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadBusinessEditProfileContent('profile.location', 1);
					}
				});

				return false;
			};

			break;

		case 'business.profile.location.edit':

			// validation rules
			validationSettings.rules = {
				'data[Address][address1]': 'required',
				'data[Address][city]'    : 'required',
				'data[Address][postalcode]': {
					mynumber: "EditAddressPostalcode"
				},
				'data[Businessphone][0][phonenumber]': {
					mynumber: "Businessphone0Phonenumberedit"
				},
				'data[Businessphone][1][phonenumber]': {
					mynumber: "Businessphone1Phonenumberedit"
				}
			};

			// validation submit handler
			validationSettings.submitHandler = function(form) {
				removeWatermarkValue('#AddressAddress2', gv_address2Default);
				removeWatermarkValue('#AddressAddressname', gv_addressnameDefault);
				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadBusinessEditProfileContent('profile.location', 1);
					}
				});

				return false;
			};

			break;

		case 'business.profile.location.hour.edit':

			// validation submit handler
			validationSettings.submitHandler = function(form) {
			// handle watermark

				// ajax form
				$('#' + form_id).ajaxSubmit({
					beforeSubmit: function() {
						setFormProcessingMessage();
						$('#mm_general_submit_button').attr('disabled', 'disabled');
					},
					success: function() {
						var rel = $('#' + form_id).attr('rel');

						reloadBusinessEditProfileContent('profile.location', 1);
					}
				});

				return false;
			};

			break;

	}

	// assign the validation settings
	$('#' + form_id).validate(validationSettings);
}

function editProfileUploadifyOnSelect(event, queueID, fileObj, containerId) {

	modalUploadifyOnSelect(event, queueID, fileObj, containerId);

}

function editProfileUploadifyOnError(event, queueID, fileObj, errorObj) {

	modalUploadifyOnError(event, queueID, fileObj, errorObj);

}

function editProfileUploadifyOnProgress(event, queueID, fileObj, data, prevClass) {

	modalUploadifyOnProgress(event, queueID, fileObj, data, prevClass);

}

function editProfileAddressAutocomplete(prefix, poiList, cityList) {

	var autocompleteName = '';

	$('#' + prefix + 'AddressBuildingName').autocomplete(poiList, {
			formatItem: function(row, i, max) {
			var displayString = row.name;

			displayString = displayString + "<small>";

			if(row.address1 != null)
				displayString = displayString + row.address1;

			if(row.address1 != null && row.city != null)
				displayString = displayString + " ,";

			if(row.city != null)
				displayString = displayString + row.city;

			displayString = displayString + "</small>";

			return displayString;
			},
			formatResult: function(row) {
				return row.name;
			},
			width: 187,
			matchContains: true,
			highlight: false,
			formatMatch: function(row, i, max) {
				return row.name;
			},

		});

	$('#' + prefix + 'AddressBuildingName').result(function(event, data, formatted) {
		
		processModalSubmit();
		$('#' + prefix + 'AddressAddress1').attr('disabled', 'disabled').addClass('mm_disabled');
		$('#' + prefix + 'AddressAddress2').attr('disabled', 'disabled').addClass('mm_disabled');
		$('#' + prefix + 'AddressCity').attr('disabled', 'disabled').addClass('mm_disabled');
		$('#' + prefix + 'AddressPostalcode').attr('disabled', 'disabled').addClass('mm_disabled');
		
		$.getJSON('/addresses/get_one/' + data.address_id + '/return:json', function(result) {
		//$.getJSON('/businesses/ajaxgetbuildingproperties/' + data.name, function(result) {
			//var locations = data.split('-');
			
			autocompleteName = data.buildingname;
			$('#' + prefix + 'AddressAddress1').val(result.Address.address1);
			$('#' + prefix + 'AddressAddress2').val(result.Address.address2);
			$('#' + prefix + 'AddressCity').val(result.Address.city);
			$('#' + prefix + 'AddressPostalcode').val(result.Address.postalcode);
			
			completeModalSubmit();
		});
	});

	$('#' + prefix + 'AddressBuildingName').keyup(function(e) {

		if(e.keyCode != 13 && e.keyCode != 9) {

			var currName = jQuery.trim($(this).val());

			if (currName.length == 0 || currName != autocompleteName) {
				$('#' + prefix + 'AddressAddress1').removeAttr('disabled').removeClass('mm_disabled').val('');
				$('#' + prefix + 'AddressAddress2').removeAttr('disabled').removeClass('mm_disabled mm_watermark').val('');
				$('#' + prefix + 'AddressCity').removeAttr('disabled').removeClass('mm_disabled').val('');
				$('#' + prefix + 'AddressPostalcode').removeAttr('disabled').removeClass('mm_disabled').val('');
			}
		}
	});

	$('#' + prefix + 'AddressCity').autocomplete(cityList, {
		formatItem: function(row, i, max) {
			return row.city + ", " + row.province;
		},
		formatResult: function(row) {
			return row.city;
		},
		width: 187,
		matchContains: true,
		highlight: false,
		formatMatch: function(row, i, max) {
			return row.city;
		},
	});

	$('#' + prefix + 'AddressCity').result(function(event, data, formatted) {
		//alert(data.province_id);
		$('#' + prefix + 'AddressProvinceId').val(data.province_id);
	});

}

