var http = createRequestObject();

function createRequestObject() 
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		ro = new XMLHttpRequest();
	}
	return ro;
}
function delphotocomment(id)
{
 if(confirm("Are you sure you want to delete this comment?")){
 	document.getElementById('comment'+id).innerHTML = '';
	http.open('get', 'delete_photo_comment.php?comment_id='+id);
    http.send(null);

	}

}


function delete_story(id)
{
 if(confirm("Are you sure you want to delete this article?")){
	http.open('get', 'delete_story.php?story_id='+id);
    http.send(null);
	document.getElementById('storyb'+id).innerHTML = '';
	}

}


function svote(id)
{
     if(document.getElementById('smarrow'+id).src != document.getElementById('smarrow'+id).src.replace('gry', ''))
     {
       a = 'r';
       document.getElementById('smarrow'+id).src = document.getElementById('smarrow'+id).src.replace('gry', 'grn');
       document.getElementById('votes'+id).innerHTML = parseInt(document.getElementById('votes'+id).innerHTML) - 1;
     }
     else
     {
       a = 'up';
       document.getElementById('smarrow'+id).src = document.getElementById('smarrow'+id).src.replace('grn', 'gry');
       document.getElementById('votes'+id).innerHTML = parseInt(document.getElementById('votes'+id).innerHTML) + 1;
     }
    http.open('get', 'http://www.punkdisasters.com/songvote.php?song_id='+id+'&a='+a);
    http.onreadystatechange = svResponse;
    http.send(null);

}

function svResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[1];
			var votes=returned[0];
                        document.getElementById('votes'+id).innerHTML = votes;
		}
       }
}



function qupvote(id)
{
 

	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('grnarow.gif', 'gryarow.gif');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qupvote', 'qrm_upvote');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('redarow_d.gif', 'redarrow.gif');	
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qrm_downvote', 'qdownvote');
	document.getElementById('net'+id).innerHTML = "...";
	http.open('get', 'qvote.php?id='+id+'&dir=1');
    http.onreadystatechange = qResponse;
    http.send(null);

}

function qdownvote(id)
{
 

	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('redarrow.gif', 'redarow_d.gif');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qdownvote', 'qrm_downvote');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('gryarow.gif', 'grnarow.gif');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qrm_upvote', 'qupvote');	
	document.getElementById('net'+id).innerHTML = "...";
	http.open('get', 'qvote.php?id='+id+'&dir=2');
    http.onreadystatechange = qResponse;
    http.send(null);

}

function qrm_upvote(id)
{
 

	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('gryarow.gif', 'grnarow.gif');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qrm_upvote', 'qupvote');
	document.getElementById('net'+id).innerHTML = "...";
	http.open('get', 'qvote.php?id='+id+'&dir=3');
    http.onreadystatechange = qResponse;
    http.send(null);

}

function qrm_downvote(id)
{
 

	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('redarow_d.gif', 'redarrow.gif');
	document.getElementById('qinfo'+id).innerHTML = document.getElementById('qinfo'+id).innerHTML.replace('qrm_downvote', 'qdownvote');
	document.getElementById('net'+id).innerHTML = "...";
	http.open('get', 'qvote.php?id='+id+'&dir=4');
    http.onreadystatechange = qResponse;
    http.send(null);

}
function qResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;

		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[0];
			var net=returned[1];
			var color = returned[2];
		}
				document.getElementById('net'+id).innerHTML = net;
				document.getElementById('net'+id).style.color = color;


    }
}

function change_form()
{
	if(!document.getElementById('input_city'))
	{
		document.getElementById('change_field').innerHTML = '<input type="text" id="input_city" name="city" value="'+document.getElementById('change_field').innerHTML+'" style="width: 100px; height: 20px; border: 1px solid #DEDEDE; font-family: Tahoma, Verdana, Arial, Sans-Serif; font-size: 11px; margin-bottom: 3px">';
		document.getElementById('miles_field').innerHTML = '<input type="text" id="input_miles" name="miles" value="'+document.getElementById('miles_field').innerHTML+'" style="width: 50px; height: 20px; border: 1px solid #DEDEDE; font-family: Tahoma, Verdana, Arial, Sans-Serif; font-size: 11px; margin-bottom: 3px">';
		document.getElementById('action').innerHTML = '<input type="submit" value="Go" style="font-size: 10px; font-family: Tahoma; color: #313131; background-color: #F0F0F0; border: 1px solid #DEDEDE; width: 26px; height; 14px">';
	}

}

function delquestion(id)
{
if(confirm('Are you sure you want to delete this question?')){
    document.getElementById('question'+id).style.display = 'none';
	http.open('get', 'qdel.php?id='+id);
    http.send(null);}
}
function popUp(URL,WIDTH,HEIGHT) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+WIDTH+",height="+HEIGHT+"');");
}

function uncheckAll() {
with (document.bands) {

for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox' && elements[i].name == 'choices[]')
elements[i].checked = false;
}
}
}

function checkAll() {
with (document.bands) {

for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox' && elements[i].name == 'choices[]')
elements[i].checked = true;
}
}
}
function update_cal(time)
{

	http.open('get', 'make_calendar.php?time='+time);
	document.getElementById('cal_area').innerHTML = 'Loading...';
    http.onreadystatechange = updatecalResponse;
    http.send(null);
}
function updatecalResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;

		document.getElementById('cal_area').innerHTML = response;


    }
}
function goingResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;

		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[0];
			var num=returned[1];
		}
				document.getElementById('num'+id).innerHTML = num;


    }
}

function digResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[1];
			var numdigs=returned[2];
			var status=returned[0];
			
			if(status == '1')
			{
				document.getElementById('digbut'+id).innerHTML = 'Voted';
				document.getElementById('numdigs'+id).innerHTML = numdigs;
  			}
			else if(status == '2')
			{
				document.getElementById('digbut'+id).innerHTML = 'Voted';
			}
			else if(status == '0')
			{
				document.getElementById('digbut'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
			}
		}
    }
}


function cdigResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[1];
			var numdigs=returned[2];
			var status=returned[0];
			
			if(status == '1')
			{
				document.getElementById('comd'+id).innerHTML = numdigs+' Votes';
				document.getElementById('cup'+id).className = 'opacityit';
				document.getElementById('cdown'+id).className = 'opacityit';
  			}
			else if(status == '2')
			{
				// Already dug comment
				document.getElementById('cup'+id).className = 'opacityit';
				document.getElementById('cdown'+id).className = 'opacityit';
			}
			else if(status == '0')
			{
				document.getElementById('comd'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
			}
		}
    }
}

function buryResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var id=returned[1];
			var status=returned[0];
			
			if(status == '0')
			{
				document.getElementById('digbut'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
			}
			else
			{
				document.getElementById('storyb'+id).style.filter = "Alpha(Opacity=80)";
				document.getElementById('storyb'+id).style.opacity = ".2";
				document.getElementById('digbut'+id).innerHTML = 'Buried';
				document.getElementById('storyb'+id).disabled = true;
			}
		}
	}
}



function deletephoto(id)
{
 if(confirm("Are you sure you want to delete this photo?")){
	http.open('get', 'delete_photo.php?id='+id);
    http.send(null);
	document.getElementById('photo'+id).innerHTML = '';
	}

}

function delsong(id)
{
 if(confirm("Are you sure you want to delete this song?")){
	http.open('get', 'delete_song.php?song_id='+id);
    http.send(null);
	document.getElementById('song'+id).innerHTML = '';
	}

}

function likeresponse(id)
{
	if(http.readyState == 4)
	{
		document.getElementById('likes'+id).innerHTML = http.responseText;
	}
}

function likesong(id)
{

	http.open('get', 'like_it.php?id='+id);
    http.send(null);
	document.getElementById('likebut'+id).innerHTML = 'I liked it';
	document.getElementById('arow'+id).src = 'images/gryarow.gif';
	http.onreadystatechange = likeresponse;
	http.send(null);

}

function likealbum(id)
{

	http.open('get', 'like_palbum.php?id='+id);
    http.send(null);
	document.getElementById('likebut'+id).innerHTML = 'I liked it';
	document.getElementById('arow'+id).src = 'images/gryarow.gif';
	http.onreadystatechange = likeresponse;
	http.send(null);

}

function likeband(id)
{
	document.getElementById('likebut'+id).innerHTML = 'I Like Them';
	http.open('get', 'like_band.php?id='+id);
    http.send(null);
	http.onreadystatechange = likeresponse(id);
	http.send(null);

}

function likeprofile(id)
{

	http.open('get', 'like_profile.php?id='+id);
    http.send(null);
	document.getElementById('likebut'+id).innerHTML = 'I Like Them';
	http.onreadystatechange = likeresponse(id);
	http.send(null);

}


function delete_album(id)
{
 if(confirm("Are you sure you want to delete this album?")){
	http.open('get', 'delete_album.php?album_id='+id);
    http.send(null);
	document.getElementById('album'+id).innerHTML = '';
	}

}

function delcomment(id)
{
 if(confirm("Are you sure you want to delete this comment?")){
	http.open('get', 'delete_comment.php?comment_id='+id);
    http.send(null);
	document.getElementById('comment'+id).innerHTML = '';
	}

}

function delrevcomment(id)
{
 if(confirm("Are you sure you want to delete this comment?")){
	http.open('get', 'delete_rev_comment.php?comment_id='+id);
    http.send(null);
	document.getElementById('comment'+id).innerHTML = '';
	}

}

function delsongcomment(id)
{
 if(confirm("Are you sure you want to delete this comment?")){
	http.open('get', 'delete_song_comment.php?comment_id='+id);
    http.send(null);
	document.getElementById('comment'+id).innerHTML = '';
	}

}





function dig(id)
{
	if('1' == '0')
	{
		document.getElementById('digbut'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
		return;
	}

	document.getElementById('digbut'+id).innerHTML = 'sending';

	http.open('get', 'dig.php?i='+id);
    http.onreadystatechange = digResponse;
    http.send(null);
}

function going(id)
{
	if('1' == '0')
	{
		document.getElementById('imgoing'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
		return;
	}

	document.getElementById('arrow'+id).src = 'images/going_arrow_gry.gif';

	http.open('get', 'going.php?i='+id);
    http.onreadystatechange = goingResponse;
    http.send(null);
}

function digcom(comment_id,dir,story_id)
{
	if('1' == '0')
	{
		return;
	}

	http.open('get', 'dig.php?type=comm&i='+comment_id+'&story='+story_id+'&dir='+dir);
    http.onreadystatechange = cdigResponse;
    http.send(null);

	if(dir == 0)
	{
		dispcomment(comment_id,false,0);
	}
}

function digphotocom(comment_id,dir,photo_id)
{
	if('1' == '0')
	{
		return;
	}

	http.open('get', 'dig_photo.php?type=comm&i='+comment_id+'&photo='+photo_id+'&dir='+dir);
    http.onreadystatechange = cdigResponse;
    http.send(null);

	if(dir == 0)
	{
		dispcomment(comment_id,false,0);
	}
}

function digsongcom(comment_id,dir,song_id)
{
	if('1' == '0')
	{
		return;
	}

	http.open('get', 'dig_song.php?type=comm&i='+comment_id+'&song='+song_id+'&dir='+dir);
    http.onreadystatechange = cdigResponse;
    http.send(null);


	
	if(dir == 0)
	{
		dispcomment(comment_id,false,0);
	}
}

function bury_story(id)
{	
	if('1' == '0')
	{
		document.getElementById('digbut'+id).innerHTML = '<a href="http://www.punkdisasters.com/login.php">Login<\/a>';
		return;
	}

	http.open('get', 'bury.php?i='+id);
    http.onreadystatechange = buryResponse;
    http.send(null);
}

function inclickcheck(field,def,val)
{
	if(field.value == def)
	{
		field.value = val;
	}
}

function changeuser(field,but)
{
	if(field.value == '')
	{
		document.getElementById(but).disabled = true;
	}
	else
	{
		document.getElementById(but).disabled = false;
	}
}

function checkavail(field)
{
	username = document.getElementById(field).value;
	
	if(username.length < 4)
	{
		document.getElementById('availres').innerHTML = 'The username "'+username+'" is too short';
		return;
	}
	else if(username.length > 16)
	{
		document.getElementById('availres').innerHTML = 'The username "'+username+'" is too long';
		return;
	}
	
	http.open('get', 'signup.php?avail='+username);
    http.onreadystatechange = availResponse;
    http.send(null);
}

function availResponse() 
{
	if(http.readyState == 4)
	{
        var response = http.responseText;
		
		returned = response.split('|');

		if(response.indexOf('|' != -1)) 
		{
			var username=returned[0];
			var status=returned[1];

			if(status == 1)
			{
				document.getElementById('availres').innerHTML = 'The username "'+username+'" is available';
			}
			else
			{
				document.getElementById('availres').innerHTML = 'Sorry, The username "'+username+'" is taken';
			}
		}
	}
}

function dispcomment(id,show,ctype)
{	
	if(ctype == 0)
	{
		if(show)
		{
			document.getElementById('commdesc'+id).style.display = '';
			document.getElementById('commbut'+id).innerHTML = 'Buried (<a href="javascript:dispcomment('+id+',false,0);">Hide Comment</a>)';
		}
		else
		{
			document.getElementById('commdesc'+id).style.display = 'none';
			document.getElementById('commbut'+id).innerHTML = 'Buried (<a href="javascript:dispcomment('+id+',true,0);">Show Comment</a>)';
		}
	}
	else if(ctype == 1)
	{
		if(show)
		{
			document.getElementById('commdesc'+id).style.display = '';
			document.getElementById('commbut'+id).innerHTML = 'Buried (<a href="javascript:dispcomment('+id+',false,0);">Hide Comment</a>)';
		}
		else
		{
			document.getElementById('commdesc'+id).style.display = 'none';
			document.getElementById('commbut'+id).innerHTML = 'Buried (<a href="javascript:dispcomment('+id+',true,0);">Show Comment</a>)';
		}
	}
}

function openWindow(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function comreply(id,nickname)
{
	document.getElementById('creply').style.display = '';
	document.getElementById('replynick').innerHTML = nickname;
	document.getElementById('parentid').value = id;
}

function replycancel()
{
	document.getElementById('creply').style.display = 'none';
	document.getElementById('replynick').innerHTML = '';
	document.getElementById('parentid').value = '0';
}

function show_day(day)
{
	document.getElementById('day'+day).style.display = 'inline';
	document.getElementById('day00').style.display = 'none';
}
function hide_day(day)
{
	document.getElementById('day'+day).style.display = 'none';
	document.getElementById('day00').style.display = 'inline';
}
  $(document).ready(function(){
  $("ul.postthread").hide();
  $("#pthreadlink").click(function () {
  $("ul.postthread").toggle();
  return false;
  });
	$("a.voteup").click(function () {
		if(userid == "1"){top.location = "http://www.punkdisasters.com/forum/ucp.php?mode=login";}
		$(this).removeClass("voteup");
		$(this).addClass("votedup");
		myid = $(this).parent().parent().parent().attr("id");
		$.post("http://www.punkdisasters.com/commvote.php", {id: myid, dig: '1'}, function(data){$("#cvote"+myid).html(data);});
		return false;
		
	});
	$("a.votedown").click(function () {
		if(userid == "1"){top.location = "http://www.punkdisasters.com/forum/ucp.php?mode=login";}
		$(this).removeClass("votedown");
		$(this).addClass("voteddown");
		myid = $(this).parent().parent().parent().attr("id");
		$.post("http://www.punkdisasters.com/commvote.php", {id: myid, dig: '2'}, function(data){$("#cvote"+myid).html(data);});
		return false;
		
	});
	$("a.votedup").click(function () {
		if(userid == "1"){top.location = "http://www.punkdisasters.com/forum/ucp.php?mode=login";}
		$(this).removeClass("votedup");
		$(this).addClass("voteup");
		myid = $(this).parent().parent().parent().attr("id");
		$.post("http://www.punkdisasters.com/commvote.php", {id: myid, dig: '1'}, function(data){$("#cvote"+myid).html(data);});
		return false;
		
	});
	$("a.voteddown").click(function () {
		if(userid == "1"){top.location = "http://www.punkdisasters.com/forum/ucp.php?mode=login";}
		$(this).removeClass("voteddown");
		$(this).addClass("votedown");
		myid = $(this).parent().parent().parent().attr("id");
		$.post("http://www.punkdisasters.com/commvote.php", {id: myid, dig: '2'}, function(data){$("#cvote"+myid).html(data);});
		return false;
		
	});
    $("h3.exthr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
    $("div.aoptions").hide();
	$("div.like").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
    $("h3.exthr").click(function () {
		
		  link = $(this).attr("id");
		  current_html = $(this).html();
		  replaced1 = current_html.replace("+","-");
		  replaced2 = current_html.replace("-","+");
		  if(replaced1 != current_html)
		  {
			$(this).html(replaced1);
		  }
		  else
		  {
			$(this).html(replaced2);
		  }
		  if(link == "poll_link"){
			$("#poll").toggle();
			}
			else if(link == "advanced_link")
			{
				$("#advanced").toggle();
			}
			else
			{	
				$("#file").toggle();
			}
		
      return true;
    });
	$("a.delpost").click(function () {
		$(this).html("Sure?");
		$(this).click(function () {
			postid = $(this).parent().parent().parent().attr("id");
			$("#"+postid).hide();
			$.post("http://www.punkdisasters.com/forum/del.php", {p: postid}, function(xml) {});
			return false;
		});
		return false;
	});
	$("div.like").click(function () {
		if(userid == "1"){top.location = "http://www.punkdisasters.com/forum/ucp.php?mode=login";}
		   el = $(this);
				  el.addClass("liked");
				  el.removeClass("like");
				  myid = $(this).parent().parent().attr("id");
				  newvalue = parseInt($(this).html()) + 1;
				  $(this).html(newvalue);
				  $.post("/digx.php", {i: myid}, function(xml) {});
				   

				  
			  return false;

			});
			
			
		$("#showtop").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
		$("#showtop").click(function() {
		$("#latest_posts").toggleClass("invisible");
		$("#latest_posts").toggleClass("topics");
		$("#top_posts").toggleClass("topics");
		$("#top_posts").toggleClass("invisible");
		if(this.html() == "Latest Topics (Show Top)"){this.html("Top Topics (Show Latest)");}else{this.html("Latest Topics (Show Top)");}
		return false;});
		
	$("a.expand").click(function () {
			this_story_id = $(this).parent().parent().attr("id");
		   el = $("#expand"+this_story_id);
		   el.toggleClass("expanded");
		   el.toggleClass("invisible");
		   

				  
			  return false;

			});
			

		
	$('#song').jqUploader({background:'FFFFFF',barColor:'981013',allowedExt:'*.mp3',allowedExtDescr: 'Mp3 Files',validFileMessage: 'Press "Upload" to upload the song.',endMessage: '',hideSubmit: false});
	$('#artwork').jqUploader({background:'FFFFFF',barColor:'981013',allowedExt:'*.jpg; *.png; *.gif',allowedExtDescr: 'Image Files',validFileMessage: 'Press "Upload" to upload the song.',endMessage: '',hideSubmit: false});

	

  });