$(document).ready(function (){
	var class_form = 'property_prov_state1'; 
	var formInUse = false;

	var url_path = $(location).attr('href');
	var pure_path = url_path.replace(/^https?:\/\//gi, '').split('/')[1];
	var agent_check_arr = pure_path.split('?');
	var agent_path = '';
	if(include_search(agent_check_arr,"agent_find")){
		agent_path = 'agent_find';
	}
	
	if(pure_path != 'officeSearch' && agent_path != 'agent_find')
	{
		$.ajax({
			dataType:"html",
			type:"POST",
			url: '/homelife_location/property_search/form',
			success: function(msg){
				$('#property-search').html(msg);
				replace_field_content_homelife('property_prov_state1');
				  $('input.input_field_location').focus();
			},
			error:function (xhr, ajaxOptions, thrownError){
				  alert(xhr.status);
				  alert(thrownError);
			} 
		});
	}
	else if(agent_path == 'agent_find') {
		var get_data = pure_path.split('?')[1];
		if(!get_data) {
			get_data = pure_path;
		}
		$.ajax({
			dataType:"html",
			type:"POST",
			data:get_data,
			url: '/homelife_location/agent_search/form',
			success: function(msg){
				$('#agent-search').show();
				$('.homelife_search_property').removeClass('active');
				$('.homelife_search_agent').addClass('active');
			    $('#agent-search').html(msg);
			    
			    $('.office_field_location').click(function () {
					if($(this).val() == 'City'){
					$(this).val('');
					}
				});
				//replace_field_content_homelife('agent_prov_state1');
				  //$('input.input_field_location').focus();
		},
			error:function (xhr, ajaxOptions, thrownError){
				  alert(xhr.status);
				  alert(thrownError);
			} 
		});
	}
	
	$('.homelife_search_property').click(function () {
		$.ajax({
			dataType:"html",
			type:"POST",
			url: '/homelife_location/property_search/form',
			success: function(msg){
				$('#property-search').html(msg);
				replace_field_content_homelife('property_prov_state1');
				  $('input.input_field_location').focus();
			},
			error:function (xhr, ajaxOptions, thrownError){
				  alert(xhr.status);
				  alert(thrownError);
			} 
		});
		
	});
	$('.homelife_search_agent').click(function () {
		$.ajax({
			dataType:"html",
			type:"POST",
			url: '/homelife_location/agent_search/form',
			success: function(msg){
			    $('#agent-search').html(msg);
			    $('.office_field_location').click(function () {
					if($(this).val() == 'City'){
					$(this).val('');
					}
				});
				//replace_field_content_homelife('agent_prov_state1');
				  //$('input.input_field_location').focus();
		},
			error:function (xhr, ajaxOptions, thrownError){
				  alert(xhr.status);
				  alert(thrownError);
			} 
		});
	});

	$('.homelife_search_office').click(function () {
		
		$.ajax({
			dataType:"html",
			type:"POST",
			url: '/homelife_location/office_search/form',
			success: function(msg){
				$('#office-location').html(msg);
				$('.office_field_location').click(function () {
					if($(this).val() == 'City / Postal Code'){
					$(this).val('');
					}
				});
				//office_replace_field_content_homelife('office_prov_state1');
				  //$('input.input_field_location').focus();
			},
			error:function (xhr, ajaxOptions, thrownError){
				  alert(xhr.status);
				  alert(thrownError);
			} 
		});
		
		
	});
	
});
function include_search(arr, obj) {
    if(arr[0].indexOf('agent_find') >= 0) {
    	return true;
    }
    else {
    	return false;
    }
}

function office_replace_field_content_homelife(class_form) {
	$.ajax({
		dataType:"json",
		type:"POST",
		url: '/homelife_location/ip',
		success: function(msg){
				if(msg.location['province'] && msg.location['country']=='CA')
				{
					$('.'+class_form).val(msg.location['province']);
				}
		},
		error:function (xhr, ajaxOptions, thrownError){
			  alert(xhr.status);
			  alert(thrownError);
		} 
	});
}

function replace_field_content_homelife(class_form) {
	$.ajax({
		dataType:"json",
		type:"POST",
		url: '/homelife_location/ip',
		success: function(msg){
				if(msg.location['province'] && msg.location['country']=='CA')
				{
					$('.input_field_location').val(msg.location['city']);
					$('.'+class_form).val(msg.location['province']);
				}
		},
		error:function (xhr, ajaxOptions, thrownError){
			  alert(xhr.status);
			  alert(thrownError);
		} 
	});
}
