// voting on news page
var respDiv = 'rank';

function rateit(newsid, rank){
    var req = '/votes/?submit=true&newsid='+newsid+'&rank='+rank;
	if (rateit.arguments[2]) respDiv = rateit.arguments[2];
	
	ajax = new Ajax.Request(req, {method: 'get',
								  onCreate : function (){
								  					$('rank').innerHTML = '<img src="/img/updating.gif" />'; 
													$('bottom-vote').innerHTML = '<img src="/img/updating.gif" />'; 
														},
								  onSuccess: updatevotes });
}

function updatevotes(xmlDoc){
  var rank, votes;

  response  = xmlDoc.responseXML.documentElement;
  if (response.getElementsByTagName('code')[0].firstChild.data == 200 )
  	{
		rank = response.getElementsByTagName('rank')[0].firstChild.data;
		votes = response.getElementsByTagName('total')[0].firstChild.data;
		$('rank').innerHTML = rank +" /5 ("+ votes + " votes)";
		$('bottom-vote').innerHTML = rank +" /5 ("+ votes + " votes)";
		$('nwvoteTp').remove();
		$('nwvoteBt').remove();
	}
  else ShowAlert("Sorry, your request can't be processed.");
  
  return;
}
 
function moveObject(n1, e) { 
 var posX, posY;  
 var Xoffset = 300;	  
 var Yoffset = -50;
 if(!e)e=self.event; 

 
 if (ns4) { 
	 posY = window.pageYOffset +  e.pageY + Yoffset;
     document.n1.top = posY; 
  } 
  else if (ie4) { 
	 posY = document.documentElement.scrollTop + e.clientY  + Yoffset; ;
     document.all[n1].style.top = posY; 

  } 
  else if (nn6) { 
		posY =  e.pageY + Yoffset ;
	    document.getElementById(n1).style.top = posY +"px";
  } 
}


function ShowHint(msg){

 var element = $('iconHint');
 element.innerHTML = msg;
 element.setStyle({display: 'block'});
}

function HideHint(){
	$('iconHint').hide();
}

function addStar(newsid){
 	var req = '/addStar/?newsid='+newsid;
	ajax = new Ajax.Request(req, {method: 'get', onSuccess: updateStar });
}


function updateStar(xmlDoc){
  var msgHtml = 'Oops you\'re not logged in.<br />' +
  				'If you are not registered, <a href="/profile/login.php?code=signup">please do so</a>, it takes less than 60-seconds. ' +
				'If you are already registered, please <a href="/profile/login.php?redirect=TRUE">login here</a>.';
				
  var msg = 'Oops you\'re not logged in. If you are not registered, please do so, it takes less than 60-seconds. If you are already registered, please login.';
  
  response  = xmlDoc.responseXML.documentElement;
  if (response.getElementsByTagName('code')[0].firstChild.data == 200 )	$('favImg').src = s_imgStar;
  else if (response.getElementsByTagName('code')[0].firstChild.data == 500 ) ShowAlert(msgHtml, msg);
  else ShowAlert('Sorry, your request can\'t be processed.');
}


function submitFeedback(){
  		for (var i=0; i < document.storyFeedback.clear.length;i++){ 
				if (document.storyFeedback.clear[i].checked) var clear = document.storyFeedback.clear[i].value;
				if (document.storyFeedback.detail[i].checked) var detail = document.storyFeedback.detail[i].value;
		}
		
		var msg = document.storyFeedback.msg.value;	  
		var email = document.storyFeedback.email.value;	  
		
		var confirm = 'Thank you for taking your time to send in your valued opinion to PhysOrg.com editors. <br/><br/>' +
					  'You can be assured our editors closely monitor every feedback sent and will take appropriate actions.' +
					  'Your opinions are important to us. We do not guarantee individual replies due to extremely high volume of correspondence.';
		
		var queryString = 'nid=' + nid +'&clear=' + escape(clear) + '&detail=' + detail +'&msg=' + escape(msg) + '&email=' + escape(email);
		submit = new Ajax.Request('/addFeedback/', {
					method: 'post',
					parameters: queryString,
					onCreate: function(){ ShowAlert(confirm); }
		});
		 
}


function createFeedback(){
  		 ajax = new Ajax.Request('/addFeedback/',
					{
					method: 'get',
					onSuccess: function(xmlDoc){
							   response  = xmlDoc.responseText;
							   ShowAlert(response, 'Your request cannot be processed at this moment');
							   }
					});
}

function showFeeback(){
	document.write('<a href="" onClick="createFeedback();return false;">send feedback to editors</a>');
}