var votesessionRequest;
var votemousePositionX;
var votemousePositionY;
var voteglobal_key;
var voteglobal_page_link;
var queryString;
var rating_count;
 var targetSpan;
var redirect_url;
var voteglobal_flag;
var server_name_end;



function checkForSessionAndVote(eventObject, key, flag, page_link, redirect_page, p1, v1, p2, v2, p3, v3, p4, v4, p5, v5){

redirect_url=redirect_page;
if(p1!='0')
	redirect_url=redirect_url+'&p1='+encodeURIComponent(p1)+'&v1='+encodeURIComponent(v1);
if(p2!='0')
	redirect_url=redirect_url+'&p2='+encodeURIComponent(p2)+'&v2='+encodeURIComponent(v2)
if(p3!='0')
	redirect_url=redirect_url+'&p3='+encodeURIComponent(p3)+'&v3='+encodeURIComponent(v3)
if(p4!='0')
	redirect_url=redirect_url+'&p4='+encodeURIComponent(p4)+'&v4='+encodeURIComponent(v4);
if(p5!='0')
	redirect_url=redirect_url+'&p5='+encodeURIComponent(p5)+'&v5='+encodeURIComponent(v5);


 voteglobal_page_link=page_link;
   voteglobal_key= key;
   voteglobal_flag= flag;

var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1)
var e = eventObject;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX;
		posy = e.clientY;
		if (isIE)
		{
			posx += document.body.scrollLeft;
			posy += document.body.scrollTop;
		}
	}


    votemousePositionX = posx;
    votemousePositionY = posy;
   targetSpan=document.getElementById(voteglobal_key);
   rating_count = parseInt(targetSpan.innerHTML);

// BEGIN UPDATE THE RATING COUNT AND IMAGES
if(voteglobal_flag=='1'){
	rating_count =   rating_count+1;
	document.getElementById('vote_button_for_img'+voteglobal_key).src="http://www.baffld.com/images/vote_for_answer_off.gif"; 
	document.getElementById('vote_button_against_img'+voteglobal_key).src="";
	
	//get rid of the word 'or' and '?'
	document.getElementById('vote_button_text'+voteglobal_key).removeChild(document.getElementById('vote_button_or'+voteglobal_key));
	document.getElementById('vote_button_punctuation'+voteglobal_key).removeChild(document.getElementById('vote_button_question_mark'+voteglobal_key));

	if(document.getElementById('vote_button_for'+voteglobal_key).onclick){
		document.getElementById('vote_button_for'+voteglobal_key).onclick="#";
		document.getElementById('vote_button_against'+voteglobal_key).removeChild( document.getElementById('vote_button_against_img'+voteglobal_key));
	}else
		alert('no vote button for');

}else{
	rating_count =   rating_count-1;
	document.getElementById('vote_button_for_img'+voteglobal_key).src="http://www.baffld.com/images/vote_against_answer_off.gif"; 
	document.getElementById('vote_button_against_img'+voteglobal_key).src="";
	
	//get rid of the word 'or' and '?'
	document.getElementById('vote_button_text'+voteglobal_key).removeChild(document.getElementById('vote_button_or'+voteglobal_key));
	document.getElementById('vote_button_punctuation'+voteglobal_key).removeChild(document.getElementById('vote_button_question_mark'+voteglobal_key));

	if(document.getElementById('vote_button_for'+voteglobal_key).onclick){
		document.getElementById('vote_button_for'+voteglobal_key).onclick="#";
		document.getElementById('vote_button_against'+voteglobal_key).removeChild(document.getElementById('vote_button_against_img'+voteglobal_key));
				
	}else
		alert('no vote button for');

}
				
	targetSpan.innerHTML= rating_count;
// END UPDATE THE RATING COUNT AND IMAGES
 setQueryStringVote(key,flag);
 
   //var url="http://www191.pair.com/studioem/Wombat/AnswerVote.php";
   
  var url='AnswerVote.php';

var the_server_name;
if((server_name_end = document.URL.indexOf('Staging'))>0 )
	the_server_name = "http://www191.pair.com/studioem/Staging/";
else{
	if((server_name_end = document.URL.indexOf('m/'))>0 )
		the_server_name =document.URL.substring(0, server_name_end+2); 
//usernameRequest=httpRequest("POST",the_server_name+url,true, handleUsernameResponse, queryStringUsername);
}
   votesessionRequest =httpRequest("POST",the_server_name+url, true, votehandleSessionResponse, queryString);
}



function setQueryStringVote(key, flag){
    queryString="";
    queryString += "key"+"="+encodeURIComponent(key);
    queryString += "&flag"+"="+encodeURIComponent(flag);
}

function votestyledPopupClose() {
	 document.getElementById("styled_popup").style.display = "none";
}

function votestyledPopupOpen() {
	document.getElementById("sign_in_link").href = 'SignIn.php?dpage='+ redirect_url;
	document.getElementById("styled_popup").style.top =votemousePositionY;									document.getElementById("styled_popup").style.left=votemousePositionX; 


	document.getElementById("styled_popup").style.display="block";

}

function alreadyvotedstyledPopupClose() {
	 document.getElementById("alreadyvoted_styled_popup").style.display = "none";
}

function alreadyvotedstyledPopupOpen() {
	document.getElementById("alreadyvoted_styled_popup").style.top =votemousePositionY;								document.getElementById("alreadyvoted_styled_popup").style.left=votemousePositionX; 
	document.getElementById("alreadyvoted_styled_popup").style.display="block";

}

function cantVoteOnOwnAnswerPopupClose() {
	 document.getElementById("cantVoteOnOwnAnswer_styled_popup").style.display = "none";
}

function cantVoteOnOwnAnswerPopupOpen(eventObject) {
	
var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1)
var e = eventObject;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX;
		posy = e.clientY;
		if (isIE)
		{
			posx += document.body.scrollLeft;
			posy += document.body.scrollTop;
		}
	}


document.getElementById("cantVoteOnOwnAnswer_styled_popup").style.top = posy;				document.getElementById("cantVoteOnOwnAnswer_styled_popup").style.left= posx;
document.getElementById("cantVoteOnOwnAnswer_styled_popup").style.display="block";
}

//event handler for XMLHttpRequest
function votehandleSessionResponse(){
if(votesessionRequest){
    if(votesessionRequest.readyState == 4){
       if(votesessionRequest.status == 200){
        // alert(votesessionRequest.responseText);

	var doc = votesessionRequest.responseXML;
	var info = doc.getElementsByTagName("signed_in");
	//if(info){
		if(info[0].firstChild.data=="yes"){
			//SIGNED IN
			//CHECK IF VOTED ALREADY
			var voted_info = doc.getElementsByTagName("already_voted");
			if(voted_info[0].firstChild.data=="yes"){
				//POPOPEN ALREADY VOTED WINDOW
				//alert("already voted!");
				//alreadyvotedstyledPopupOpen();
			}else{
				//alert("everything went well!");
	    }
		}else{
			//POP OPEN THE WINDOW IF NOT SIGNED IN
	      		//not signed in
			//alert("you need to sign in to vote!");
			the_page_name =document.URL.substring(server_name_end+2, document.URL.length ); 
			location.href='SignIn.php?dpage='+the_page_name;
	        		//votestyledPopupOpen();
		}
	//}
	delete votesessionRequest;
	votesessionRequest=null;
	
        } 
   }
}
}




