
var v_txt;
var v_id;
var v_url;
var rid;
var rel;

var lock = false;

function vote(id, mode)
{
	if (id && mode && !lock)
	{
		v_id = id;
		
		new Ajax.Request(
			'ajax/vote.php', 
			{
				method: 'get',
				parameters: 'id='+id+'&mode='+mode+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;

					if (v_txt == "")
					{
						showModalLogin('vote('+id+', '+mode+');');
					}
					else if (r.responseText != 'x')
					{
						lock = true;
						
						new Effect.Pulsate('voteit_'+v_id);
						
						if (mode == 1)
						{
							$('article_'+v_id).className = 'result resultfav';
							
							window.setTimeout('$(\'voteit_\'+v_id).innerHTML = \'<a href="javascript:vote(\'+v_id+\', 2);"><img title="You have already voted for this article - click here to remove your vote" src="\'+tmpdir+\'/images/thumbup2.gif" /></a>\'', 600);
							window.setTimeout('lock = false;', 600);
						}
						else
						{
							$('article_'+v_id).className = 'result resulton';

							window.setTimeout('$(\'voteit_\'+v_id).innerHTML = \'<a href="javascript:vote(\'+v_id+\', 1);"><img title="Vote for this article - make it popular!" src="\'+tmpdir+\'/images/thumbup.gif" /></a> <a href="javascript:removeArticle(\'+v_id+\',1);"><img title="Request removal of this article" src="\'+tmpdir+\'/images/thumbdown.gif" /></a>\'', 600);
							window.setTimeout('lock = false;', 600);
						}
					}
				}
			});
	}
}

function addremfav(type, id)
{
	var tags;
	
	if ((id) && (!lock))
	{
		new Ajax.Request(
			'ajax/favourites.php', 
			{
				method: 'get',
				parameters: 'id='+id+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;

					if (v_txt == "")
					{
						showModalLogin('addremfav('+type+', '+id+');');
					}
					else if (v_txt == "a")
					{
						if ($('favimg') != null) { $('favimg').src = tmpdir + '/images/addtofavs2.gif'; }

						if (document.getElementsByName('l_'+id) != null)
						{
							tags = document.getElementsByName('l_'+id);

							for (var i = 0; i < tags.length; i++)
							{
								tags[i].innerHTML = '<a class="resultlink" href="javascript:addremfav('+type+','+id+');"><img class="bubble" src="'+ tmpdir + '/images/delfavicon.gif"></a> <a class="resultlink" href="javascript:addremfav('+type+','+id+');">remove artist</a> from favs';
							}

							tags = document.getElementsByName(id);
							
							for (var i = 0; i < tags.length; i++)
							{
								tags[i].className = 'favartist';
							}
						}
						
						if (type == 1)
						{
							lock = true;
							showMsg('This artist has been added to your favourites', true, true, 'msg');
						}
					}
					else if (v_txt == "r")
					{
						if (type == 2)
						{
							new Ajax.Request(
								'ajax/fav_artists.php', 
								{
									method: 'get',
									parameters: 'l='+l+'&s='+s+'&rid='+rid+'&rnd='+ new Date().getTime(),
									asynchronous: true,
									onSuccess: function(r){
										$('aresults').innerHTML = r.responseText;
									}
								});
						}
						else
						{
							if ($('favimg') != null) { $('favimg').src = tmpdir + '/images/addtofavs.gif'; }
							
							if (document.getElementsByName('l_'+id) != null)
							{
								tags = document.getElementsByName('l_'+id);
								
								for (var i = 0; i < tags.length; i++)
								{
									tags[i].innerHTML = '<a class="resultlink" href="javascript:addremfav('+type+','+id+');"><img class="bubble" src="'+ tmpdir + '/images/addfavicon.gif"></a> <a class="resultlink" href="javascript:addremfav('+type+','+id+');">add artist</a> to favs';
								}
								
								tags = document.getElementsByName(id);
								
								for (var i = 0; i < tags.length; i++)
								{
									tags[i].className = '';
								}
							}
							
							if (type == 1)
							{
								lock = true;
								showMsg('This artist has been removed from your favourites', true, true, 'msg');
							}
						}
					}
				}
			});
	}
}

function addremfriend(type, id)
{
	if (id == -1)
	{
		showMsg('Lol...you can\'t add yourself to your own friends list!', false, true, 'msg');
	}
	else if ((id) && (!lock))
	{
		new Ajax.Request(
			'ajax/friends.php', 
			{
				method: 'get',
				parameters: 'id='+id+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;

					if (v_txt == "")
					{
						showModalLogin('addremfriend('+type+', '+id+');');
					}
					else if (v_txt == "a")
					{
						if (type == 1)
						{
							new Ajax.Request(
								'ajax/fav_friends.php', 
								{
									method: 'get',
									parameters: 'rel='+rel+'&rid='+rid+'&rnd='+ new Date().getTime(),
									asynchronous: true,
									onSuccess: function(r){
										$('aresults').innerHTML = r.responseText;
									}
								});
						}
						else
						{
							lock = true;
						
							$('favimg2').src = tmpdir + '/images/addtofriends2.gif';
						
							showMsg('This person has been added to your friends list', true, true, 'msg');
						}
					}
					else if (v_txt == "r")
					{
						if (type == 1)
						{
							new Ajax.Request(
								'ajax/fav_friends.php', 
								{
									method: 'get',
									parameters: 'rel='+rel+'&rid='+rid+'&rnd='+ new Date().getTime(),
									asynchronous: true,
									onSuccess: function(r){
										$('aresults').innerHTML = r.responseText;
									}
								});
						}
						else
						{
							lock = true;
							
							$('favimg2').src = tmpdir + '/images/addtofriends.gif';
							
							showMsg('This person has been removed from your friends list', true, true, 'msg');
						}
					}
				}
			});
	}
}

function listFriends(tab, page)
{
	if (page < 1) { page = 1; }

	if (!lock)
	{
		lock = true;

		new Ajax.Request(
			'ajax/fav_friends.php',
			{
				method: 'get',
				parameters: 'rel='+tab+'&rid='+page+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					document.location.href='#listtop';
					$('aresults').innerHTML = r.responseText;
					
					rid = page;
					rel = tab;
					
					lock = false;
				}
			});
	}
}

function listFavourites(limit, page)
{
	if (page < 1) { page = 1; }

	if (!lock)
	{
		lock = true;
		
		$('aresults').innerHTML = '';
		$('loaderimg').style.display = 'block';

		new Ajax.Request(
			'ajax/fav_artists.php',
			{
				method: 'get',
				parameters: 'l='+limit+'&s='+s+'&rid='+page+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('loaderimg').style.display = 'none';
					document.location.href='#listtop';
					$('aresults').innerHTML = r.responseText;
					
					rid = page;
					l = limit;
					
					lock = false;
				}
			});
	}
}

function listFavouritesSearch()
{
	var term = $('searchfavs');
	
	if (!lock && term)
	{
		term = escape(term.value);
		
		lock = true;
				
		$('aresults').innerHTML = '';
		$('loaderimg').style.display = 'block';

		new Ajax.Request(
			'ajax/fav_artists.php',
			{
				method: 'get',
				parameters: 'l='+l+'&s='+term+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('loaderimg').style.display = 'none';
					$('aresults').innerHTML = r.responseText;
					
					s = term;
					
					lock = false;
				}
			});
	}
}

function listArtists(limit, page)
{
	if (page < 1) { page = 1; }

	if (!lock)
	{
		lock = true;
		
		$('aresults').innerHTML = '';
		document.location.href='#listtop';
		$('loaderimg').style.display = 'block';

		new Ajax.Request(
			'ajax/searchartist.php',
			{
				method: 'get',
				parameters: 'l='+limit+'&s='+s+'&rid='+page+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('loaderimg').style.display = 'none';
					document.location.href='#listtop';
					$('aresults').innerHTML = r.responseText;
					
					rid = page;
					l = limit;
					
					lock = false;
				}
			});
	}
}

function listArtistsSearch()
{
	var term = $('searchartists');
	
	if (!lock && term)
	{
		term = escape(term.value);
		
		lock = true;

		$('aresults').innerHTML = '';
		$('loaderimg').style.display = 'block';

		new Ajax.Request(
			'ajax/searchartist.php',
			{
				method: 'get',
				parameters: 'l='+l+'&s='+term+'&rnd='+ new Date().getTime(),
				asynchronous: false,
				onSuccess: function(r){
					$('loaderimg').style.display = 'none';
					$('aresults').innerHTML = r.responseText;
					
					s = term;					
					
					lock = false;
				}
			});
	}
}

function showMsg(msg, good, hide, element)
{
	var img = good ? 'tick.gif' : 'error.gif';
	
	Element.hide(element);

	$(element).innerHTML = '<br /><div class="register"><div class="regform"><div class="infotext2"><table><tr><td style="vertical-align: top;"><img style="padding-top: 0px;" src=\'' + tmpdir + '/images/'+img+'\' /> </td><td style="padding-top: 9px; padding-left: 10px;">'+msg+'</td></tr></table></div></div></div>';
	new Effect.SlideDown(element);

	if (hide)
	{
		window.setTimeout('new Effect.SlideUp(\'' + element + '\');lock = false;', 3000);
	}
	else
	{
		lock = false;
	}
}

function availability()
{
	var user = $('user').value;

	if (user.length > 2)
	{
		new Ajax.Request(
			'ajax/user_available.php', 
			{
				method: 'get',
				parameters: 'u='+user+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText == '1')
					{
						$('infotext').innerHTML = '<span class=\'infogood\'>Success - your chosen username is available</span>';
					}
					else
					{
						$('infotext').innerHTML = '<span class=\'infobad\'>Sorry - your chosen username is already taken. Please try again.</span>';
					}
				}
			});
	}
	else
	{
		$('infotext').innerHTML = '<span class=\'infobad\'>Sorry - usernames must be at least 3 characters long.</span>';
	}
}

function getArtists(type)
{
	if (type == 1)
	{
		var gid = $('style').value;
		
		if (gid)
		{		
			new Ajax.Request(
				'ajax/artist_style.php', 
				{
					method: 'get',
					parameters: 'id='+gid+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						v_txt = r.responseText;
						
						$('info').innerHTML = "";
						
						Element.hide('artists');
						window.setTimeout('$(\'artists\').innerHTML = v_txt', 500);
						window.setTimeout('new Effect.Appear(\'artists\')', 800);
						$('aid').focus();
					}
				});
		}
	}
	else
	{
		var sid = $('style').value;
		
		if (sid)
		{		
			new Ajax.Request(
				'ajax/artist_styles.php', 
				{
					method: 'get',
					parameters: 'id='+sid+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						v_txt = r.responseText;
						
						$('info').innerHTML = "";
						
						Element.hide('artists');
						window.setTimeout('$(\'artists\').innerHTML = v_txt', 500);
						window.setTimeout('new Effect.Appear(\'artists\')', 800);
						$('aid').focus();
					}
				});
		}
	}
}

function getArtistInfo()
{
	var aid = $('aid').value;

	if (aid)
	{		
		new Ajax.Request(
			'ajax/artist_info.php', 
			{
				method: 'get',
				parameters: 'id='+aid+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('info').innerHTML = r.responseText;
				}
			});
	}
}

function getFans(rid)
{
	var key;
	var sor;
	var pho;
	var smo;
	var dri;
	var win;
	
	var orig = rid;

	var key = escape($('keyword').value);
	var aid = $('artist').value;
	var sid = $('sex').value;
	var afr = $('agefrom').value;
	var ato = $('ageto').value;
	var sta = $('status').value;
	var ori = $('orientation').value;

	if (rid == 0)
	{
		rid = 1;

		pho = $('photo').checked;
		smo = $('smoke').checked;
		dri = $('drink').checked;
		win = $('newwin').checked;
	
		sor = document.forms['fansearch'].elements['sorting']

		for (var x = 0; x < sor.length; x++)
		{
			if (sor[x].checked) { sor = sor[x].value; }
		}
		
		pho = pho ? 1 : 0;
		smo = smo ? 1 : 0;
		dri = dri ? 1 : 0;
		win = win ? 1 : 0;
	}
	else
	{
		pho = $('photo').value;
		smo = $('smoke').value;
		dri = $('drink').value;
		win = $('newwin').value;
		sor = $('sorting').value;
	}

	if (aid && sid && afr && ato && sta && ori)
	{		
		new Ajax.Request(
			'ajax/fan_search.php', 
			{
				method: 'get',
				parameters: 'k='+key+'&a='+aid+'&s='+sid+'&afr='+afr+'&ato='+ato+'&t='+sta+'&o='+ori+'&p='+pho+'&sm='+smo+'&d='+dri+'&win='+win+'&sor='+sor+'&rid='+rid+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;
					
					if (orig == 0)
					{
						new Effect.Fade('searchfans');
						
						window.setTimeout('$(\'searchfans\').innerHTML = v_txt', 1000);
						window.setTimeout('new Effect.Appear(\'searchfans\')', 1500);
					}
					else
					{
						document.location.href='#';
						
						$('searchfans').innerHTML = v_txt;
					}
				}
			});
	}
	else
	{
		alert('Sorry - you did not complete all fields. Please try again.');
	}
}

function updateProfile(type)
{
	if (!lock)
	{
		if (type == 1)
		{
			// update general profile
			
			var e = $('g_email').value;
			var p1 = $('g_pass1').value;
			var p2 = $('g_pass2').value;
			
			if (p1 == '*******') { p1 = ''; }
			if (p2 == '*******') { p2 = ''; }
			
			var me = $('memoemail').checked;
			var fe = $('friendemail').checked;
			var kl = $('keeplogin').checked;
			var ne = $('newsletter').checked;
			
			me = me ? 1 : 0;
			fe = fe ? 1 : 0;
			kl = kl ? 1 : 0;
			ne = ne ? 1 : 0;

			new Ajax.Request(
				'ajax/updateprofile.php', 
				{
					method: 'post',
					parameters: 'id='+type+'&e='+e+'&p1='+p1+'&p2='+p2+'&me='+me+'&ne='+ne+'&fe='+fe+'&kl='+kl+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						v_txt = r.responseText;
						
						document.location.href='#';

						if (v_txt == "1")
						{
							lock = true;
							
							$('infotext').innerHTML = '';
							showMsg('Success - your profile has been updated', true, true, 'msg');
						}
						else
						{
							$('infotext').innerHTML = '<span class=\'infobad\'>'+ v_txt +'<br /><br /></span>';
						}
					}
				});
		}
		else if (type == 2)
		{
			// update additional profile
			
			ge = document.forms['aboutme'].elements['gender']

			for (var x = 0; x < ge.length; x++)
			{
				if (ge[x].checked) { ge = ge[x].value; break; }
			}

			var d = $('day').value;
			var m = $('month').value;
			var y = $('year').value;
			var c = $('country').value;
			var s = $('city').value;
			var st = $('status').value;
			var o = $('orientation').value;
			var tw = $('twit').value;
			
			var sm = $('smoke').checked;
			var dr = $('drink').checked;
			
			sm = sm ? 1 : 0;
			dr = dr ? 1 : 0;
			
			var a = escape($('about').value);
			var g = escape($('general').value);
			var mu = escape($('music').value);
			var mo = escape($('movies').value);
			var t = escape($('tv').value);
			var b = escape($('books').value);

			new Ajax.Request(
				'ajax/updateprofile.php', 
				{
					method: 'post',
					parameters: 'id='+type+'&ge='+ge+'&d='+d+'&m='+m+'&y='+y+'&c='+c+'&s='+s+'&st='+st+'&o='+o+'&sm='+sm+'&dr='+dr+'&tw='+tw+'&a='+a+'&g='+g+'&mu='+mu+'&mo='+mo+'&t='+t+'&b='+b+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						v_txt = r.responseText;
						
						document.location.href='#';

						if (v_txt == "1")
						{
							lock = true;
							
							$('infotext').innerHTML = '';
							showMsg('Success - your profile has been updated', true, true, 'msg');
						}
						else
						{
							$('infotext').innerHTML = '<span class=\'infobad\'>'+ v_txt +'<br /><br /></span>';
						}
					}
				});
		}
	}
}

function clearTxt(control, text)
{
	var txt = $(control);
	
	if (txt.value == text) { txt.value = ''; }
}

function deleteMemos(id)
{
	if (!lock)
	{		
		var ids = '';
		
		lock = true;
		
		if (id)
		{
			ids = id;
		}
		else
		{
			sor = document.forms['messageform'].elements['msg'];
		
			for (var x = 0; x < sor.length; x++)
			{
				if (sor[x].checked) { ids += sor[x].value + ','; }
			}
			
			if (ids == '')
			{
				if (document.forms['messageform'].elements['msg'].checked)
				{
					ids = document.forms['messageform'].elements['msg'].value;
				}
			}
		}
	
		if (ids)
		{
			new Ajax.Request(
				'ajax/deletememo.php', 
				{
					method: 'get',
					parameters: 'ids='+ids+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						$('readpane').innerHTML = '';
						$('mview').innerHTML = r.responseText;
						lock = false;
					}
				});
		}
		else
		{
			lock = false;
		}
	}
}

function readMemo(id)
{
	if (id)
	{
		// display loading image
		
		$('readpane').innerHTML = '<div style="width: 100%; padding-bottom: 10px; text-align: center;"><img id="loadimg" src="'+tmpdir+'/images/indicator.gif" /><br /></div>';
		
		new Ajax.Request(
			'ajax/readmemo.php', 
			{
				method: 'get',
				parameters: 'id='+id+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('readpane').innerHTML = r.responseText;
				}
			});
	}
}

function validateMemo()
{
	var err = '';
	
	$('sendbtn').disabled = true;
	
	var to = $('to');
	
	if (to)
	{
		to = escape($('to').value);
		
		new Ajax.Request(
			'ajax/user_available.php', 
			{
				method: 'get',
				parameters: 'u='+to+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText == '1')
					{
						showMsg('Sorry, the user that you are trying to message does not exist. Please try again.', false, false, 'msg');
						$('sendbtn').disabled = false;
						return;
					}
					else
					{
						var subject = escape($('subject').value);
						var mbody = escape($('body').value);
				
						if (subject && mbody)
						{
							new Ajax.Request(
								'ajax/validatemsg.php', 
								{
									method: 'post',
									parameters: 'c=1&to='+to+'&subject='+subject+'&body='+mbody+'&rnd='+ new Date().getTime(),
									asynchronous: true,
									onSuccess: function(r){
										if (r.responseText == '1')
										{
											document.reply.submit();
										}
										else
										{
											showMsg(r.responseText, false, false, 'msg');
											$('sendbtn').disabled = false;
										}
									}
								});
						}
						else
						{
							showMsg('Sorry - you did not complete all fields. Please try again.', false, false, 'msg');
							$('sendbtn').disabled = false;
						}
					}
				}
			});
	}
	else
	{
		var id = $('id').value;
		var subject = escape($('subject').value);
		var mbody = escape($('body').value);

		if (id && subject && mbody)
		{
			new Ajax.Request(
				'ajax/validatemsg.php', 
				{
					method: 'post',
					parameters: 'r=1&id='+id+'&subject='+subject+'&body='+mbody+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						if (r.responseText == '1')
						{
							document.reply.submit();
						}
						else
						{
							showMsg(r.responseText, false, false, 'msg');
							$('sendbtn').disabled = false;
						}
					}
				});
		}
		else
		{
			showMsg('Sorry - you did not complete all fields. Please try again.', false, false, 'msg');
			$('sendbtn').disabled = false;
		}
	}
}

function uploadPhoto()
{
	$('curpic').style.display = 'block'; 
	
	var cimg = $('currentpic');

	if (cimg)
	{
		$('delbutton').disabled = true;
		cimg.src = tmpdir + '/images/indicator.gif';
	}
	
	$('upbutton').disabled = true;
	$('msg2').innerHTML = '<p style="text-align: center;">Please wait while your photo is uploading...</p>';
	
	window.setTimeout('document.addphoto.submit()', 800);
}

function addNews(stage)
{
	if (stage == 1)
	{
		if ($('artist').value != '')
		{
			$('a').value = 'artist=' + $('artist').value;
			
			new Effect.Fade('first');
			
			window.setTimeout('$(\'first\').className = \'regform\';$(\'first\').innerHTML = $(\'second\').innerHTML;new Effect.Appear(\'first\');new Effect.Appear(\'third\');new Effect.Appear(\'fourth\');updatePreview(0);', 1500);
		}
	}
	else if (stage == 2)
	{
		var pars = $('a').value + '&' + Form.serialize('addnews');
		pars = pars + '&' + Form.serialize('addnews2');
		
		new Ajax.Request(
			'ajax/validatenews.php', 
			{
				method: 'post',
				parameters: pars+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					document.location.href = '#';
					
					if (r.responseText == '1')
					{
						$('a1').value = pars;
						$('msg').innerHTML = '';
						
						updatePreview(0);
						
						new Effect.Fade('first');
				
						window.setTimeout('$(\'first\').className = \'regform\';', 1500);
						window.setTimeout('$(\'first\').innerHTML = \'<span class="bold">Almost there...</span><br /><br />Here is a preview of how your news will look when it appears on the front page. \'+$(\'fifth\').innerHTML;', 1500);
						window.setTimeout('new Effect.Appear(\'first\');', 1500);
						window.setTimeout('new Effect.Appear(\'third\');', 1500);
						window.setTimeout('new Effect.Appear(\'fourth\');', 1500);
					}
					else
					{
						showMsg(r.responseText, false, false, 'msg');
					}
				}
			});
	}
	else if ((stage == 3) && (!lock))
	{
		var pars = $('a').value + '&' + Form.serialize('addnews');
		pars = pars + '&' + Form.serialize('addnews2');

		lock = true;

		new Ajax.Request(
			'ajax/postnews.php', 
			{
				method: 'post',
				parameters: pars+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					document.location.href = '#';
					
					if (r.responseText.indexOf('x') == 0)
					{
						showMsg(r.responseText.slice(1), false, false, 'msg');
					}
					else if (r.responseText != '')
					{
						$('msg').innerHTML = '';
						
						new Effect.Fade('first');
						new Effect.Fade('third');
						new Effect.Fade('fourth');
				
						window.setTimeout('$(\'first\').className = \'regformgreen\';', 1500);
						window.setTimeout('$(\'first\').innerHTML = \'<span class="bold">Thank you!</span><br /><br />Your news has been successfully added to the site.<br /><br /><a href="index.php?item=article&amp;id='+r.responseText+'">Click here</a> to view your news item.\';', 1500);
					
						window.setTimeout('new Effect.Appear(\'first\');lock = false;', 1500);
					}
					else
					{
						lock = false;
						showMsg('Doh - sorry, an error has occured and we were unable to add your news to the site. If this error persists, please contact us to report the issue.', false, false, 'msg');
					}
				}
			});
	}
	else if (stage == 4)
	{
		$('third').style.display = 'none';
		$('fourth').style.display = 'none';

		$('first').innerHTML = $('second').innerHTML;
		
		var des = $('a1').value.split("&");
		var cooshie;
		
		for (var x = 0; x < des.length; x++)
		{
			cooshie = des[x].split("=");

			switch(cooshie[0])
			{
				case "artist":
					break;

				case "source":
					$('source').value = unescape(cooshie[1]);
					break;
				
				case "title":
					$('title').value = unescape(cooshie[1]);
					break;
				
				case "body":
					$('body').value = unescape(cooshie[1]);
					break;
				
				case "date1":
					$('date1').value = unescape(cooshie[1]);
					break;
			}
		}
	}
}

function addArtist(stage)
{
	if (stage == 1)
	{
		var pars = Form.serialize('addartist');

		new Ajax.Request(
			'ajax/validateartist.php', 
			{
				method: 'post',
				parameters: pars+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					document.location.href = '#';
					
					if (r.responseText == '1')
					{
						$('msg').innerHTML = '';
						$('a').value = pars;
						
						var srcList = $('available');
	
						for(var i = 0; i < srcList.options.length; i++)
						{
							if (srcList.options[i].value == $('style').value)
							{
								srcList.options[i] = null;
							}
						}
						
						new Effect.Fade('first');
			
						window.setTimeout('$(\'first\').className = \'regform\';$(\'first\').innerHTML = $(\'second\').innerHTML;new Effect.Appear(\'first\');', 1500);
					}
					else
					{
						showMsg(r.responseText, false, false, 'msg');
					}
				}
			});
	}
	else if (stage == 2)
	{
		var s = '';
		var st = $('selected');
	
		for(var i = 0; i < st.options.length; i++)
		{
			if (st.options[i] != null)
			{
				s += st.options[i].value + ',';
			}
		}
	
		document.location.href = '#';
		
		if (s != '')
		{
			$('a').value += '&s='+escape(s);
			
			$('msg').innerHTML = '';
						
			new Effect.Fade('first');
			
			window.setTimeout('$(\'first\').className = \'regform\';$(\'first\').innerHTML = $(\'third\').innerHTML;new Effect.Appear(\'first\');new Effect.Appear(\'skip\');', 1500);
		}
		else
		{
			showMsg('You must select at least one additional style that applies to this artist.', false, false, 'msg');
		}
	}
	else if (stage == 3)
	{
		var p = escape($('photo').value);
		
		if (p != '')
		{
			var cimg = $('progpic');
	
			if (cimg)
			{
				cimg.src = tmpdir + '/images/indicator.gif';
			}
		
			$('upbutton').disabled = true;
			$('msg2').innerHTML = '<p style="text-align: center;">Please wait while your photo is uploading...</p>';
		}
		else
		{
			showMsg('You did not select a photo/image to upload.', false, false, 'msg');
		}
	}
	else if (stage == 4)
	{
		document.forms[3].submit();
	}
}

function editArtist(stage)
{
	if (stage == 1)
	{
		var pars = Form.serialize('editartist');

		new Ajax.Request(
			'ajax/validateartist_e.php', 
			{
				method: 'post',
				parameters: pars+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					document.location.href = '#';
					
					if (r.responseText == '1')
					{
						$('msg').innerHTML = '';
						$('a').value = pars;
						
						new Effect.Fade('first');
			
						window.setTimeout('$(\'first\').className = \'regform\';$(\'first\').innerHTML = $(\'second\').innerHTML;new Effect.Appear(\'first\');', 1500);
					}
					else
					{
						showMsg(r.responseText, false, false, 'msg');
					}
				}
			});
	}
	else if (stage == 2)
	{
		var s = '';
		var st = $('selected');
	
		for(var i = 0; i < st.options.length; i++)
		{
			if (st.options[i] != null)
			{
				s += st.options[i].value + ',';
			}
		}
		
		document.location.href = '#';
		
		if (s != '')
		{
			$('a').value += '&s='+escape(s);
			
			$('msg').innerHTML = '';
						
			new Effect.Fade('first');
			
			window.setTimeout('$(\'first\').className = \'regform\';$(\'first\').innerHTML = $(\'third\').innerHTML;new Effect.Appear(\'skip\');new Effect.Appear(\'first\');', 1500);
		}
		else
		{
			showMsg('You must select at least one additional style that applies to this artist.', false, false, 'msg');
		}
	}
	else if (stage == 3)
	{
		var r = $('reason').value;
		
		if (r != '')
		{
			$('a').value += '&r='+escape(r);
			
			$('msg').innerHTML = '';
			
			new Ajax.Request(
				'ajax/saveartist.php', 
				{
					method: 'post',
					parameters: $('a').value+'&rnd='+ new Date().getTime(),
					asynchronous: true,
					onSuccess: function(r){
						document.location.href = '#';
						
						if (r.responseText.indexOf('-') == 0)
						{
							new Effect.Fade('first');
							new Effect.Fade('skip');
							
							window.setTimeout('$(\'skip\').innerHTML = \'<span class=\"bold\">Thank you!</span><br /><br />Your edits have been submitted.<br /><br />Please note that the edits you have made will not be viewable on the site until one of our moderation team has reviewed them.<br /><br /><a href="index.php?item=artist&amp;id='+r.responseText.slice(1)+'">click here</a> to go back to the artists page\';new Effect.Appear(\'skip\');', 1500);
						}
						else
						{
							showMsg(r.responseText, false, false, 'msg');
						}
					}
				});
		}
		else
		{
			showMsg('Sorry - you must enter a reason (please give a brief explanation of why you are editing this artist.', false, false, 'msg');
		}
	}
}

function updatePreview(mode)
{
	if (mode == 0)
	{
		var a = $('a').value + '&' + Form.serialize('addnews');
		
		new Ajax.Request(
			'ajax/preview.php', 
			{
				method: 'post',
				parameters: a+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('third').innerHTML = r.responseText;
				}
			});
	}
	else
	{
		$('ptitle').innerHTML = $('title').value;
		$('pbody').innerHTML = $('body').value;
	}
}

function fieldLimit(element, len)
{
	if (element.value.length >= len)
	{
		element.value = element.value.substring(0, len);
	}
}

function removeArticle(id, stage)
{
	if (id && stage && !lock)
	{
		if (stage == 1)
		{
			lock = true;
			
			document.location.href = '#';
			showMsg('Please tell us why you feel that this article should be removed:<br /><br /><select id="remove" name="remove"><option value="1">This is a duplicate of another article</option><option value="2">This article is spam</option><option value="3">This article is inaccurate</option><option value="4">This article is lame</option></select> <input type="button" value="Send Report" onClick="javascript:removeArticle('+id+', 2);this.disabled=true;" />', true, false, 'msg');
		}
		else if (stage == 2)
		{
			var reason = $('remove').value;
			
			if (reason != '')
			{
				lock = true;
				
				new Ajax.Request(
					'ajax/report.php', 
					{
						method: 'get',
						parameters: 'id='+id+'&r='+reason+'&rnd='+ new Date().getTime(),
						asynchronous: true,
						onSuccess: function(r){
							if (r.responseText == "")
							{
								lock = false;
								showModalLogin('removeArticle('+id+', '+stage+');');
							}
							else
							{
								$('article_'+id).className = 'result bad';
								showMsg(r.responseText, true, false, 'msg');
							}
						}
					});
			}
		}
	}
}

function sendArticle(id, stage)
{
	if (id && stage && !lock)
	{
		if (stage == 1)
		{
			lock = true;
			
			document.location.href = '#';
			showMsg('Share this story with a friend by entering their email address:<br /><br /><input id="sendfriend" name="sendfriend" size="30" /><div id="autocomplete_choices" class="autocomplete"></div> <input type="button" value="Send Message" onClick="javascript:sendArticle('+id+', 2);this.disabled=true;" /><br /><br /><span class="subtle"><b><i>Tip:</i></b> To send this article to an Oversome user, just enter their username above!</span>', true, false, 'msg');
			new Ajax.Autocompleter("sendfriend", "autocomplete_choices", "ajax/memo_autocomplete.php", {});
		}
		else if (stage == 2)
		{
			var sendto = escape($('sendfriend').value);
			
			if (sendto != '')
			{
				lock = true;
				
				new Ajax.Request(
					'ajax/sendtofriend.php', 
					{
						method: 'get',
						parameters: 'id='+id+'&to='+sendto+'&rnd='+ new Date().getTime(),
						asynchronous: true,
						onSuccess: function(r){
							if (r.responseText == "")
							{
								lock = false;
								showModalLogin('sendArticle('+id+', '+stage+');');
							}
							else
							{
								showMsg(r.responseText, true, false, 'msg');
							}
						}
					});
			}
		}
	}
}

function sendArtist(id, stage)
{
	if (id && stage && !lock)
	{
		if (stage == 1)
		{
			lock = true;
			
			document.location.href = '#';
			showMsg('Recommend this artist to a friend by entering their email address:<br /><br /><input id="sendfriend" name="sendfriend" size="30" /><div id="autocomplete_choices" class="autocomplete"></div> <input type="button" value="Send Message" onClick="javascript:sendArtist('+id+', 2);this.disabled=true;" /><br /><br /><span class="subtle"><b><i>Tip:</i></b> To send this article to an Oversome user, just enter their username above!</span>', true, false, 'msg');
			new Ajax.Autocompleter("sendfriend", "autocomplete_choices", "ajax/memo_autocomplete.php", {});
		}
		else if (stage == 2)
		{
			var sendto = escape($('sendfriend').value);
			
			if (sendto != '')
			{
				lock = true;
				
				new Ajax.Request(
					'ajax/sendtofriend.php', 
					{
						method: 'get',
						parameters: 'a=1&id='+id+'&to='+sendto+'&rnd='+ new Date().getTime(),
						asynchronous: true,
						onSuccess: function(r){
							if (r.responseText == "")
							{
								lock = false;
								showModalLogin('sendArtist('+id+', '+stage+');');
							}
							else
							{
								showMsg(r.responseText, true, false, 'msg');
							}
						}
					});
			}
		}
	}
}

function voteComment(id, val)
{
	if (id && val && !lock)
	{
		lock = true;
		
		new Ajax.Request(
			'ajax/vote_comment.php', 
			{
				method: 'get',
				parameters: 'id='+id+'&v='+val+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText.indexOf('x') == 0)
					{
						showMsg(r.responseText.slice(1), false, true, 'msg');
					}
					else if (r.responseText == "")
					{
						lock = false;
						showModalLogin('voteComment('+id+', '+val+');');
					}
					else
					{
						$('rlink_'+id).innerHTML = r.responseText;

						if (r.responseText.indexOf('-') == 0)
						{
							$('c_'+id).className = 'comment_bad';
							$('ch_'+id).className = 'commenthead_bad';
							$('cb_'+id).className = 'commentbody_bad';
						}
						else
						{
							$('c_'+id).className = 'comment';
							$('ch_'+id).className = 'commenthead';
							$('cb_'+id).className = 'commentbody';
						}
						
						lock = false;
					}
				}
			});
	}
}

function updateStyle(s)
{	
	var gf = $('stylefilter').value;
	
	if (gf != 0)
	{
		document.location.href = 'index.php?s='+s+'&g='+gf;
	}
	else
	{
		document.location.href = 'index.php?s='+s;
	}
}

function updateCal(m, y)
{
	if (m && y)
	{
		new Ajax.Request(
			'ajax/calendar.php', 
			{
				method: 'get',
				parameters: 'm='+m+'&y='+y+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					$('cal').innerHTML = r.responseText;
				}
			});
	}
}

function findArtist()
{
	if ($('artist').value != '' && !lock)
	{
		var pars = Form.serialize('addartist');
		
		lock = true;
		
		$('loadtext').innerHTML = "Searching, please wait...";
		new Effect.Appear('second', { duration: 0.2, from: 0.0, to: 0.8 });
		$('second').innerHTML = $('loader').innerHTML;

		new Ajax.Request(
			'ajax/findartist.php', 
			{
				method: 'post',
				parameters: pars+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText.indexOf('x') == 0)
					{
						var trima = r.responseText.slice(1);
						$('second').innerHTML = "<span class=\"bold\">Artist already exists...</span><br /><br /><a href=\"index.php?item=artist&amp;id=" + trima + "\"><img width=\"130\" height=\"130\" class=\"artistimgl\" src=\"templates/artists/" + trima + ".jpg\"></a>This artist already exists on Oversome.com! You can view their artist page by <a href=\"index.php?item=artist&amp;id=" + trima + "\">clicking here</a>.<br /><br />You can submit a news item for them by <a href=\"index.php?item=addnews&amp;id=" + trima + "\">clicking here</a><br /><br /><br /><br /><br />";
					}
					else if (r.responseText != '')
					{
						$('second').innerHTML = r.responseText;
					}
					else
					{
						$('second').innerHTML = "<span class=\"bold\">Hmm...</span><br /><br />Sorry, we weren't able to automatically retrieve any information for that artist.<br /<br />Don't worry - you can still add this artist to Oversome by <a href=\"index.php?item=addartist\">clicking here</a> or you can try again later !";
					}
					
					lock = false;
				}
			});
	}
}

function autoaddartist(artist)
{
	if (!lock)
	{
		artist = escape(artist);
		
		lock = true;
		
		$('loadtext').innerHTML = "Adding artist, please wait...";
		new Effect.Appear('second', { duration: 0.2, from: 0.0, to: 0.8 });
		$('second').innerHTML = $('loader').innerHTML;
		
		new Ajax.Request(
			'ajax/addartist.php', 
			{
				method: 'post',
				parameters: 'a='+artist+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText.indexOf('x') == 0)
					{
						$('second').innerHTML = "<span class=\"bold\">Artist already exists...</span><br /><br />This artist already exists on Oversome.com! You can view their artist page by <a href=\"index.php?item=artist&amp;id=" + r.responseText.slice(1) + "\">clicking here</a>.<br /><br />You can submit a news item for them by <a href=\"index.php?item=addnews&amp;id=" + r.responseText.slice(1) + "\">clicking here</a><br />";
					}
					else if (r.responseText != '')
					{
						$('second').innerHTML = "<span class=\"bold\">Thank you for adding a new artist to Oversome!</span><br /><br /><a href=\"index.php?item=artist&id="+r.responseText+"\">click here</a> to view this artists page<br /><br /><span id=\"l_"+r.responseText+"\" name=\"l_"+r.responseText+"\"><a href=\"javascript:addremfav(0, "+r.responseText+");\"><img class=\"bubble\" src=\"templates/red/images/addfavicon.gif\"></a> <a href=\"javascript:addremfav(0, "+r.responseText+");\">add artist</a> to favourites</span><br /><br /><a href=\"index.php?item=addnews&amp;id=" + r.responseText + "\">click here</a> to add a news item for this artist";
					}
					else
					{
						$('second').innerHTML = "<span class=\"bold\">Hmm...</span><br /><br />Sorry, we weren't able to automatically retrieve any information for that artist at this time.<br /><br />Don't worry - you can still add this artist to Oversome by <a href=\"index.php?item=addartist\">clicking here</a> or you can try again later !";
					}
					
					lock = false;
				}
			});
	}
}

function superSearch(mode, nofade, rid)
{
	if (rid < 1) { rid = 1; }

	if ((!lock) && (mode == 0))
	{
		var pars = Form.serialize('supersearch');

		lock = true;

		new Ajax.Request(
			'ajax/search.php', 
			{
				method: 'get',
				parameters: pars+'&rid='+rid+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;

					if (nofade)
					{
						$('popular').style.display = 'none';
						$('results').innerHTML = v_txt;
					}
					else
					{
						new Effect.Fade('results');
						new Effect.Fade('popular');
							
						window.setTimeout('$(\'results\').innerHTML = v_txt', 1000);
						window.setTimeout('new Effect.Appear(\'results\')', 1500);
					}

					lock = false;
				}
			});
	}
	else if ((!lock) && (mode == 1))
	{
		var pars = Form.serialize('art_search');

		lock = true;

		new Ajax.Request(
			'ajax/search_artists.php', 
			{
				method: 'get',
				parameters: pars+'&mode='+mode+'&rid='+rid+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;

					document.location.href='#artists';
					$('art_results').innerHTML = v_txt;

					lock = false;
				}
			});
	}
	else if ((!lock) && (mode == 2))
	{
		$('news_results').innerHTML = '';
		$('news_loaderimg').style.display = 'block';
		
		document.location.href='#articles';
		
		var pars = Form.serialize('news_search');

		lock = true;

		new Ajax.Request(
			'ajax/search_articles.php', 
			{
				method: 'get',
				parameters: pars+'&mode='+mode+'&rid='+rid+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					v_txt = r.responseText;
					
					$('news_loaderimg').style.display = 'none';
					$('news_results').innerHTML = v_txt;

					lock = false;
				}
			});
	}
}

function referFriend()
{
	if (!lock)
	{
		var email = escape($('refer').value);
		
		$('btnrefer').disabled = true;
		lock = true;
		
		new Ajax.Request(
			'ajax/referfriend.php', 
			{
				method: 'get',
				parameters: 'e='+email+'&rnd='+ new Date().getTime(),
				asynchronous: true,
				onSuccess: function(r){
					if (r.responseText.indexOf('x') == 0)
					{
						$('btnrefer').disabled = false;
						showMsg(r.responseText.slice(1), false, false, 'msg');
					}
					else if (r.responseText != '')
					{
						$('refer').value = '';
						$('btnrefer').disabled = false;
						showMsg(r.responseText, true, false, 'msg');
					}
					else
					{
						$('btnrefer').disabled = false;
						showMsg("Sorry - we were unable to process your request at this time.<br /><br />Please try again, and report this error to us if the problem persists", false, false, 'msg');
					}
					
					lock = false;
				}
			});
	}
}

function showModalLogin(returnTo)
{
	if ($('modalbg').style.display == 'block')
	{
		return;
	}

	var topoff = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	var arrayPageSize = getPageSize();

	Element.setWidth('modalbg', arrayPageSize[0]);
	Element.setHeight('modalbg', arrayPageSize[1]);

	if (!window.pageYOffset)
	{
		$('modalbg').style.top = topoff + 'px';
	}
	
	$('modalstyles').style.display = 'none';
	$('modalloader').style.display = 'none';
	$('modalcontent').style.display = 'block';
	$('modalbg').style.display = 'block';
	$('modallogin').style.top = topoff + 'px';
	$('modallogin').style.display = 'block';
	
	$('returnto').value = returnTo;
}

function hideModalLogin()
{
	$('modalbg').style.display = 'none';
	$('modallogin').style.display = 'none';
	$('modalstyles').style.display = 'none';
	$('modalloader').style.display = 'none';
}

function login(source)
{
	if (source == 1)
	{
		var u = escape($('un').value);
		var p = escape($('pa').value);
	}
	else
	{
		var u = escape($('u').value);
		var p = escape($('p').value);
		
		if (p == '' && $('pa').value != '')
		{
			// ie bug workaround when pressing enter instead of button
			
			u = escape($('un').value);
			p = escape($('pa').value);
		}
		
		if ($('modalbg').style.display == 'none') showModalProgress();
	}

	$('modalstyles').style.display = 'none';
	$('modalcontent').style.display = 'none';
	$('modalloader').style.display = 'block';
	
	new Ajax.Request(
		'ajax/verify.php', 
		{
			method: 'post',
			parameters: 'u='+u+'&p='+p+'&rnd='+ new Date().getTime(),
			asynchronous: true,
			onSuccess: function(r){

				if (r.responseText == '0')
				{
					$('modalloader').style.display = 'none';
					$('modalcontent').style.display = 'block';
				}
				else if (r.responseText == '1')
				{
					if ($('returnto').value != '')
					{
						eval($('returnto').value);
					}
					
					if (source == 1)
					{
						// see if we need to add rel=1 to ensure they go back to the all news section
						
						var url = document.location.href.replace(/#/, "");
						
						if ((url.search(/rel\=/i) == -1) && (url.search(/item\=/i) == -1))
						{
							if (url.search(/\?/i) == -1)
							{
								url += '?rel=1';
							}
							else
							{
								url += '&rel=1';
							}
						}
						
						// avoid a timing issue where the eval has not completed before the page reloads

						v_url = url;
						window.setTimeout('document.location.href = v_url;', 2000);

						return;
					}
					else
					{
						document.location.href = 'index.php';
						return;
					}
				}
				else
				{
					document.location.href = 'index.php?item=error&code=666';
					return;
				}
			}
		});
}

function showModalProgress()
{
	var topoff = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	var arrayPageSize = getPageSize();

	Element.setWidth('modalbg', arrayPageSize[0]);
	Element.setHeight('modalbg', arrayPageSize[1]);

	if (!window.pageYOffset)
	{
		$('modalbg').style.top = topoff + 'px';
	}
	
	$('modalstyles').style.display = 'none';
	$('modalcontent').style.display = 'none';
	$('modalloader').style.display = 'block';
	$('modalbg').style.display = 'block';
	$('modallogin').style.top = topoff + 'px';
	$('modallogin').style.display = 'block';	
}

function showModalStyles()
{
	var topoff = window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop;
	var arrayPageSize = getPageSize();

	Element.setWidth('modalbg', arrayPageSize[0]);
	Element.setHeight('modalbg', arrayPageSize[1]);

	if (!window.pageYOffset)
	{
		$('modalbg').style.top = topoff + 'px';
	}
	
	$('modaltitle').innerHTML = 'Please select a style from the list...';
	$('loadtext').innerHTML = 'Loading Styles';
	
	$('modalcontent').style.display = 'none';
	$('modalloader').style.display = 'block';
	$('modalbg').style.display = 'block';
	$('modallogin').style.top = topoff + 'px';
	$('modallogin').style.display = 'block';
	
	new Ajax.Request(
		'ajax/loadstyles.php',
		{
			method: 'get',
			parameters: 'rnd='+ new Date().getTime(),
			asynchronous: true,
			onSuccess: function(r){
				
				$('modalloader').style.display = 'none';
				
				$('modalstyles').innerHTML = r.responseText;
				$('modalstyles').style.display = 'block';
			}
		});
}

function filterStyles(user)
{
	var styleid = $('modalstylelist').value;
	var url = document.location.href.replace(/#/, "");
	
	if (styleid)
	{
		if ((url.search(/g\=/i) == -1) && (url.search(/item\=/i) == -1))
		{
			if (url.search(/\?/i) == -1)
			{
				url += '?g='+styleid;
			}
			else
			{
				url += '&g='+styleid;
			}
		}
		else
		{
			url = 'index.php?g='+styleid;
		}
		
		if (user != '')
		{
			if (url.search(/rel\=/i) == -1)
			{
				url += '&rel=1';
			}
		}
	}

	document.location.href = url;
}

