function savedSearchSuccessRegistration() {
$('#popupSaveSearchContent').before($('#popupLoginContainer')); 
$('#popupSaveSearchContent').hide();
$('#popupLoginContainer').show();
$('#popupLoginError').hide();
$('#popupRegisterError').hide(); 
$('#popupLogin').hide();
$('#popupRegister').show();
$('#popupSaveSearchSuccess').hide();
}
$(document).ready(function() {
$('a[returnhref]').each(function() {
$(this).attr('href', $(this).attr('href') + '?r=' + $(this).attr('returnhref')); 
});
if($("a.button").size()>0) { 
$("a.button").button(); 
} 
$("a[closedialog]").live('click', function() {
var dialogObj = $(this).attr('closedialog');
if($(dialogObj).size()>0) {
$(dialogObj).dialog('destroy'); 
}
return false;
});
$("a.register_link").live('click', function() {
$('#popupFavAddedSuccess').before($('#popupLoginContainer')); 
$('#popupFavAddedSuccess').hide();
$('#popupLoginContainer').show();
$('#popupLoginError').hide();
$('#popupRegisterError').hide(); 
$('#popupLogin').hide();
$('#popupRegister').show();
$('#popupFavAddedSuccess').hide();
$('form.popuplogin').ajaxForm({ 
type: 'post',
dataType: 'json', 
success: function(response) {
if(response.userId > 0) { 
$('#popupLoginContainer').hide();
$('#popupFavAddNew').show();
$('#popupFavAdded').attr('userId', response.userId);
$('#popupFavAddNew').attr('userId', response.userId);
$('#popupFavAddedSuccess').before($('#popuppopupFavAddNewContent')); 
$('#popuppopupFavAddNewContent').show(); 
} else {
$('#popupLoginError').show();
$('#popupRegisterError').show();
$('#popupRegisterError').find('.error').html(response.error); 
}
}
});
return false;
});
$("a.link_save").click(function() {
$('#popupSaveSearch').dialog({
modal: true,
caption: '"Email Alerts: Saved Search',
width: '400px'
});
$('#popupSaveSearchContent').show();
$('#popupSaveSearchSuccess').hide();
$("a#buttonSaveSearch").click(function() {
if(!$(this).hasClass('ui-state-disabled')) { 
$('#frmSaveSearch').submit();
}
return false; 
});
$('#frmSaveSearch').ajaxForm({ 
type: 'post',
dataType: 'json', 
beforeSubmit: function() {
$('#popupSaveSearch').find('ul.errors').remove(); 
$("#loading").show();
$("#loading").width($('#popupSaveSearch').width()); 
$("#loading").offset($('#popupSaveSearch').parents(".ui-dialog").offset());
$('#popupSaveSearch').addClass('ui-state-disabled');
$('#popupSaveSearch').find('#buttonSaveSearch').addClass('ui-state-disabled').find('span.ui-button-text').text('Saving...'); 
return true;
}, 
success: function(response) {
$('#popupSaveSearch').find('#buttonSaveSearch').removeClass('ui-state-disabled').find('span.ui-button-text').text('Submit'); 
$('#popupSaveSearch').removeClass('ui-state-disabled');
$("#loading").hide();
if(response.userId > 0) {
$('#popupSaveSearchContent').hide();
if(response.newUser) {
$('#popupSaveSearchSuccess').show();
$('#popupSaveSearchSuccessNewUser').show();
$('input[name=user_email]').val(response.email);
} else {
$('#popupSaveSearchSuccess').show();
$('#popupSaveSearchSuccessNewUser').hide();
} 
} else {
$('#search_name').focus();
for(field in response.errors) {
var errorHTML = '<ul class="errors"><li>' + response.errors[field] + '</li></ul>';
var fieldObj = $('#popupSaveSearch').find('#'+field);
fieldObj.after(errorHTML); 
}
$('#popupSaveSearchError').append('<ul class="errors"></ul>');
$('#popupSaveSearchError').find('ul.errors:first').append('<li>Could not save your alert</li>');
$('#popupSaveSearchError').show(); 
}
}
});
$('#popupSaveSearch').find('form.popuplogin').ajaxForm({ 
type: 'post',
dataType: 'json', 
success: function(response) {
if(response.userId > 0) {
$('#popupLoginContainer').hide();
$('#popupSaveSearchContent').show();
$('#popupSaveSearch').attr('userId', response.userId);
} else {
$('#popupLoginError').show();
$('#popupRegisterError').show();
$('#popupRegisterError').find('.error').html(response.error); 
}
}
});
return false;
});
$('#popupFavAdded .btn_close').live('click', function() {
var expires = new Date;
expires.setDate(expires.getDay() + (360 * 10));
setCookie('myfavorites_nopopup', $('.popupFavAdded_dontshow').attr('checked') ? 1 : 0, expires, '/'); 
$('#popupFavAdded').dialog("destroy");
});
var myFavs = getCookie('myfavorites_v3').split('-');
for(i=0;i< myFavs.length;i++) {
$('li[id=favs-add-db-'+myFavs[i]+']').hide(); 
$('li[id=favs-delete-db-'+myFavs[i]+']').show(); 
}
$('.popupFavAdded_dontshow').live('blur', function() {
var expires = new Date;
expires.setDate(expires.getDay() + (360 * 10));
setCookie('myfavorites_nopopup', $(this).attr('checked') ? 1 : 0, expires, '/'); 
});
$("#fav_submit").click(function() {
$.post("/favorites.php", $("#form-faves").serialize(), function(jsonData) {
var message = '';
var nPropertyId = $('input#listing_id').val();
if (jsonData.isValid) {
$('li[id=favs-add-db-'+nPropertyId+']').hide();
$('li[id=favs-delete-db-'+nPropertyId+']').show();
} else {
message = 'Could not save listing to my favs';
alert(message);
} // end if
$('#popupFavAddNew').dialog("destroy");
$('#popupFavAdded').dialog("destroy")
}, 'json');
});
$('#fav_cancel').click(function() {
$('#popupFavAddNew').dialog("destroy");
});
if ($('#logged_flag').length != 0) {
if ($('#logged_flag').val() < 1) {
} // end if
} // end if
if($('.saved-search-results').size()>0) {
$("span.listing-updated").hide();
$('li.listing-updated:first').before('<h4 class="search-results listing-updated">' + $('.saved-search-results').attr('title') + ' - Updated Listings</h4>');
$('li.listing-new:first').before('<h4 class="search-results listing-new">' + $('.saved-search-results').attr('title') + ' - New Listings</h4>');
$("#link_save_top,#link_save_bottom").hide();
}
});
function DeleteFavoriteProperty(nDeleteId)
{ 
/*if($('#popupFavAddNew').attr('userId')) {
DeleteFavoritePropertyDB(nDeleteId);
}*/
var strCookie = getCookie("myfavorites_v3");
if (strCookie == "")
{
return;
}
var regExp = '\-' + nDeleteId ;
strCookie = strCookie.replace(regExp, '');
var expires = new Date;
expires.setDate(expires.getDay() + (360 * 10));
setCookie('myfavorites_v3', strCookie, expires, '/');
$('li[id=favs-delete-db-'+nDeleteId+']').hide(); 
$('li[id=favs-add-db-'+nDeleteId+']').show(); 
}
function SaveFavoriteProperty(nPropertyId, strDescription, strCommunity, boolNoAlert)
{ 
$('input#listing_id').val(nPropertyId);
/*if($('#popupFavAddNew').attr('userId')) {
SaveFavoritePropertyDB(nPropertyId);
return;
}*/
var strCombo1 = nPropertyId;
var strCombo2 = "-" + strCombo1 + "-";
var expires = new Date;
expires.setDate(expires.getDay() + (360 * 10));
var strCookie_v2 = getCookie("myfavorites");
var strCookie_v1 = getCookie("RentFaster_Favorite_Properties");
var strCookie_v3 = getCookie("myfavorites_v3");
if(!strCookie_v3 && (strCookie_v1 || strCookie_v2)) {
strCookie_v3 = '';
if(strCookie_v2) {
strCookie_v3 = strCookie_v2;
}
if(strCookie_v1) {
var m = strCookie_v1.match(/~\d+~/g);
for(i=0;i<m.length;i++) {
var tmpId = m[i];
tmpId = tmpId.replace(/\D/g, '');
if(tmpId && strCookie_v3.indexOf('-' + tmpId + '-')==-1) {
strCookie_v3 += '-' + tmpId + '-';
}
}
}
var expiresOld = new Date;
expiresOld.setDate(expires.getDay() - (360 * 10)); 
setCookie('myfavorites', '', expiresOld, '/');
setCookie('RentFaster_Favorite_Properties', '', expiresOld, '/');
setCookie('myfavorites', '', expiresOld);
setCookie('RentFaster_Favorite_Properties', '', expiresOld);
}
var alreadyThere = strCookie_v3.indexOf('-' + strCombo1 + '-')>=0; 
if (strCookie_v3 == "")
{
setCookie('myfavorites_v3', strCombo2, expires, '/');
} else if(alreadyThere) {
} else {
setCookie('myfavorites_v3', strCookie_v3 + strCombo1 + '-', expires, '/'); 
}
if(!boolNoAlert) {
popupFavAdded('favs-add-db-'+nPropertyId); 
}
$('li[id=favs-add-db-'+nPropertyId+']').hide(); 
$('li[id=favs-delete-db-'+nPropertyId+']').show(); 
return;
}
function popupFavAdded(buttonId) {
var cookienopopup = parseInt(getCookie('myfavorites_nopopup'));
var checkboxvalue = $('.popupFavAdded_dontshow').attr('checked');
if(cookienopopup || checkboxvalue) {
return true;
} else {
$('#popupFavAdded').dialog({
caption: "Add Listing to My Faves",
modal: true 
}); 
}
}
function SaveFavoritePropertyDB(nPropertyId) {
if(!$('#popupFavAddNew').attr('userId')) {
$('#popuppopupFavAddNewContent').before($('#popupLoginContainer')); 
$('#popuppopupFavAddNewContent').hide();
$('#popupLoginContainer').show();
$('#divSkipLoginUseCookies').show();
$('input.useCookies').attr('checked', false);
$('.useCookies').click(function() { 
$('#popupFavAddNew').dialog("destroy");
SaveFavoriteProperty(nPropertyId, '','', true); 
});
$('#popupFavAddNew').find('form.popuplogin').ajaxForm({ 
type: 'post',
dataType: 'json', 
success: function(response) {
if(response.userId > 0) {
$('#popupLoginContainer').hide();
$('#popuppopupFavAddNewContent').show();
$('#popupFavAddNew').attr('userId', response.userId);
} else {
$('#popupLoginError').show();
$('#popupRegisterError').show();
$('#popupRegisterError').find('.error').html(response.error); 
}
}
});
}
$('#popupFavAddNew').dialog({
caption: "Add Listing to My Faves",
modal: true 
}); 
$('#listing_id').val(nPropertyId);
return;
}
function DeleteFavoritePropertyDB(nDeleteId) {
$('#listing_id').val(nDeleteId);
$.post("/favorites.php", {action: "delete", listing_id: nDeleteId}, function(jsonData) {
var message = '';
var nPropertyId = $('input#listing_id').val();
if (jsonData.isValid) {
message = 'Record sucessfully removed from DB.';
$('li[id=favs-delete-db-'+nPropertyId+']').hide();
$('li[id=favs-add-db-'+nPropertyId+']').show();
} else {
message = 'Could not remove listing from favorites';
alert(message);
} // end if
}, 'json');
}

