function checkEmail(email) {
	return /^[a-z0-9_.]+@[a-z0-9\-]+(\.[a-z0-9\-\.]+)?\.[a-z]+$/i.test(trim(email));
	//return /^[a-z0-9_.]+@[a-z0-9\-]+\.[a-z\-\.]+$/.test(email);
}
function getTimePlus(sec) {
	now = new Date();
	now.setTime(now.getTime() + sec);
	return now;
}
function register(email, name) {
	if (!checkEmail(email)){
		alert('Invalid email address !');
	}
	else {
		jQuery.ajax({
			type: 'POST',
			async: false,
			cache: false,
			url: js_server + '/includes/ajax/favorite.php',
			dataType: 'script',
			data:{
				'action': 'register',
				'email': email,
				'name': name
			}
		});
	}
}
function loginMember(username, pass) {
	islogged = false;
	jQuery.ajax({
			type: 'POST',
			async: false,
			cache: false,
			url: js_server + '/includes/ajax/favorite.php',
			dataType: 'text',
			data: {
				'action': 'loginMember',
				'username': username,
				'password': pass
			},
			success: function (ret) {
				if (ret == '1') {
					islogged = true;
				}
			}
		});
		
	return islogged;
}
function checkGuest(email) {
	if (!checkEmail(email)) {
		alert('Invalid email address !');
		return null;
	}

	email = email.toLowerCase();

	jQuery('input[type=submit]').attr('disabled','disabled');
	jQuery.ajax({
			type: 'POST',
			async: false,
			cache: false,
			url: js_server + '/includes/ajax/favorite.php',
			dataType: 'script',
			data:{
					'action': 'login',
					'email': email
			}
		});
	jQuery('input[type=submit]').attr('disabled','');

	if (FavUserId)
		return true;

	return false;
}
function favsReady() {
	if (checkCookies() && FavUserId)
		return true;

	url = '/includes/ajax/favorite.php?action=loginform&';
	// could use openPopup() actually...
	jQuery('<a class="fancypopup" href="'+ url +'&dim=400,300"></a>') 
		.fancybox()
		.click();

	if (FavUserId)
		return true;

	return false;
}
function toggleFav(photo_id) {

	var tag = jQuery('#a_tag_fav_'+photo_id);
	
	// Remove fav
	if (tag[0].name == 'f'+ photo_id) {
		tag.html('Please wait...');
		removeFav(photo_id);
		tag[0].name = photo_id;
		tag.html('Add Favorites');
	}
	// Add fav
	else {
		setCookie('favs', photo_id, getTimePlus(10800), '/', '.photostockplus.com');
		if(favsReady()) {
			jQuery.ajax({
				type: 'POST',
				cache: false,
				url: js_server + '/includes/ajax/favorite.php',
				data: {
					'photo_id': photo_id,
					'action': 'add'
				},
				success: function(ret) {
					tag.text('Remove from Favorites');
					tag[0].name = 'f'+ photo_id;
				}
			});
		}
	}

	return false;
}
function removeFav(photo_id) {

	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: js_server + '/includes/ajax/favorite.php',
		data: {
			'photo_id': photo_id,
			'action': 'remove'
		}
	});

	return false;
}
function destroyFav(photo_id) {

	removeFav(photo_id);
	jQuery('#block_fav_'+photo_id).hide('fast', function(){jQuery(this).remove();});
}