function readCookie(name)
{
	var ca=document.cookie.split(';');
	for (var i=0;i<ca.length;i++)
	{
		var c=ca[i];
		while (c.charAt(0)==' ') c=c.substring(1,c.length);
		if (c.indexOf(name+'=')==0) return unescape(c.substring(name.length+1,c.length));
	}
	return null;
}
function loadClips()
{
	var clips=readCookie('clips');
	if (clips==null || clips=='') return new Array();
	return clips.split(',');
}
function saveClips(clips)
{
	var expire=new Date();
	expire.setTime(expire.getTime()+1000*60*60*24*30);
	document.cookie="clips="+escape(clips.join(','))+";expires="+expire.toGMTString()+";path=/";
}
function email(id)
{
        x=window.open('http://www.tucson.com/classifieds_pages/email.php?id='+escape(id),'email','width=300,height=300,scrollbars=yes,resizable=yes');
        x.focus();
}
function save(e,id)
{
	var clips=loadClips();

	add=true;
	for (var i=0;i<clips.length;i++)
		if (clips[i]==id){ add=false; break; }
	if (add) clips.push(id);

	saveClips(clips);
	e.parentNode.innerHTML='<input type="checkbox" onchange="unsave(this,\''+id+'\')" checked /> ITEM SAVED';
	el=document.getElementById('items_saved');
	el.innerHTML=clips.length+" ITEM"+(clips.length==1?"":"S")+" SAVED";
}
function unsave(e,id)
{
	var oldclips=loadClips();

	var clips=new Array();
	for (var i=0;i<oldclips.length;i++)
		if (oldclips[i]!=id) clips.push(oldclips[i]);
	delete oldclips;
	saveClips(clips);
	e.parentNode.innerHTML='<input type="checkbox" onchange="save(this,\''+id+'\')" /> SAVE THIS ITEM';
	el=document.getElementById('items_saved');
	el.innerHTML=clips.length+" ITEM"+(clips.length==1?"":"S")+" SAVED";
}
function subscribe(keyword,section,category)
{
	if (registration.user.loggedin==false)
	{
		registration.sp('You need to be logged in.  <a href="javascript:registration.login()">Login</a> or <a href="javascript:registration.register()">Create Account</a>');
	}
	else
	{
        	x=window.open('http://www.tucson.com/classifieds_pages/subscribe.php?keyword='+escape(keyword)+'&section='+escape(section)+'&category='+escape(category)+'&user='+escape(readCookie('registration')),'subscribe','width=300,height=300,scrollbars=yes,resizable=yes');
	        x.focus();
	}
}
