function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}
var hex=76;
bgcolor=230;

function fadetext(){
elementid=fadetext.arguments[0];
if (hex<250) { //If color is not black yet
document.getElementById(elementid).style.color="rgb("+hex+","+(hex-13)+","+(hex-21)+")";
hex+=21; // increase color darkness
setTimeout("fadetext(elementid)",1); 
} else {
  hex=3;
 }
}

function fade_in_message()
{
elementid=fade_in_message.arguments[0];
document.getElementById(elementid).style.color="rgb(15,15,15)";
document.getElementById(elementid).style.borderStyle="solid";
document.getElementById(elementid).style.borderWidth="1px";
if (hex<230) { //If color is not black yet
document.getElementById(elementid).style.backgroundColor="rgb("+hex+","+(hex-13)+","+(hex-21)+")";
hex+=10; // increase color darkness
setTimeout("fade_in_message(elementid)",20); 
} else {
  hex=76;
}
}

function fade_out_message()
{
elementid=fade_out_message.arguments[0];
if (bgcolor>76) { //If color is not black yet
document.getElementById(elementid).style.backgroundColor="rgb("+bgcolor+","+(bgcolor-13)+","+(bgcolor-21)+")";
bgcolor-=15; // increase color darkness
setTimeout("fade_out_message(elementid)",20); 
} else {
  bgcolor=230;
  document.getElementById(elementid).style.backgroundColor="#FFF";
  document.getElementById(elementid).innerHTML="";
  document.getElementById(elementid).style.borderStyle="none";
}
}

function rate_off()
{
id=rate_off.arguments[0].id;
submissionid=submissionid.replace(/_r.*/,"");

rate_num=document.getElementById("id" + submissionid).value;

document.getElementById("ratecommentid" + submissionid).innerHTML="";
document.getElementById("ratecommentid" + submissionid).style.backgroundColor="#FFF";
document.getElementById("ratecommentid" + submissionid).style.borderStyle="none";
document.getElementById("ratecommentid" + submissionid).style.borderWidth="0";

for(i=1;i<=5;i++) {
	id_string="id" + submissionid + "_r" + i;
	if(i<=rate_num) {
		document.getElementById(id_string).src="images/pc_1.png";
	}
	else {
		document.getElementById(id_string).src="images/pc_0.png";
	}
}

}

function rate_hover()
{
id=rate_hover.arguments[0].id;
submissionid=id.replace(/id/,"");
submissionid=submissionid.replace(/_r.*/,"");
//fadetext("ratecommentid" + submissionid);

rate_num=id.replace(/id.*_r/,"");

var rate_comment=document.getElementById("ratecommentid" + submissionid);
rate_comment.style.backgroundColor="#FFF";
rate_comment.style.color="#333";

switch(rate_num) {
	case "1":
		rate_comment.innerHTML="1 Popped Collar - Lame. Wannabe douche";
		break;
	case "2":
		rate_comment.innerHTML="2 Popped Collars - A touch of douche";
		break;
	case "3":
		rate_comment.innerHTML="3 Popped Collars - It's getting douchey in here.";
		break;
	case "4":
		rate_comment.innerHTML="4 Popped Collars - The Douche is with you.";
		break;
	case "5":
		rate_comment.innerHTML="5 Popped Collars - Douche Incarnate!";
		break;
		
}

for(i=1;i<=5;i++) {
	id_string="id" + submissionid + "_r" + i;
	if(i<=rate_num) {
		document.getElementById(id_string).src="images/pc_1.png";
	}
	else {
		document.getElementById(id_string).src="images/pc_0.png";
	}
}

}
function rating()
{
var submissionid=rating.arguments[0];
var myrating=rating.arguments[1];

var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   // document.getElementById("result").innerHTML=mygetrequest.responseText
   if(mygetrequest.responseText=="-10") {
   	document.getElementById("id" + submissionid).value=myrating;
	document.getElementById("ratecommentid" + submissionid).innerHTML="Oops! You have already rated this DB Maneuver!";
	fade_in_message("ratecommentid" + submissionid);
	elementid="ratecommentid" + submissionid;
	setTimeout("fade_out_message(elementid)",3000); 
   }
   else {
   	document.getElementById("id" + submissionid).value=myrating;
	document.getElementById("ratecommentid" + submissionid).innerHTML="Collar popped! Thanks !";
	fade_in_message("ratecommentid" + submissionid);
	elementid="ratecommentid" + submissionid;
	setTimeout("fade_out_message(elementid)",3000); 
   }
  }
  else{
   alert("An error has occured making the request")
  }
 }
}
mygetrequest.open("GET", "ajax/rating.php?id="+submissionid+"&rating="+myrating, true)
mygetrequest.send(null)

}

function rate_comment() {
var commentid=rate_comment.arguments[0];
var myrating=rate_comment.arguments[1];


var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
  	if(mygetrequest.responseText=='1') {
  			rate_element=document.getElementById("freshness_cid" + commentid);
  			cur_rating=rate_element.innerHTML.match(/\d+/);
  			if(myrating==1) {
  				new_rating=cur_rating*1+1;
  			} else {
  				new_rating=cur_rating*1-1;
  			}
  			rate_element.innerHTML=rate_element.innerHTML.replace(cur_rating,new_rating);
  	}
  }
  else{
   alert("An error has occured making the request")
  }
 }
}

mygetrequest.open("GET", "ajax/rate_comment.php?commentid="+commentid+"&rating="+myrating, true)
mygetrequest.send(null);

}
