var xmlHttp;
var headlinediv;
var hid = null;
var imgname;
var newrank=0;

function emailStory(action,storyid)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
        document.getElementById("darkBackgroundLayer").style.display = "";
document.getElementById("spinner").innerHTML="<img alt=\"getting data\" src=\"images/ajax-loader.gif\">";
document.getElementById("spinner").style.display="";
 hid=storyid
if (action=="compose") {
 var url="emailstory.php"
 url=url+"?action=compose"
 url=url+"&storyid="+storyid
 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=stateChangedEmailStory
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)   
} else if (action=="submit") {
  document.getElementById("spinner").innerHTML="<img alt=\"getting data\" src=\"images/ajax-loader.gif\">";
 var url="emailstory.php"
 url=url+"?action=submit"
 url=url+"&emailfrom="+document.getElementById("emailfrom").value
 url=url+"&emailto="+document.getElementById("emailto").value
 url=url+"&subject="+document.getElementById("subject").value
 url=url+"&msgtext="+document.getElementById("msgtext").value.replace(/\n/g,"<br>")

 url=url+"&sid="+Math.random()
 xmlHttp.onreadystatechange=stateChangedEmailStory
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
}

}


function stateChangedEmailStory() 
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
	document.getElementById("spinner").style.display="none";

        document.getElementById("floatboxLayer").innerHTML=xmlHttp.responseText;
        document.getElementById("floatboxLayer").style.display="";
//  document.getElementById("email-"+hid).innerHTML=xmlHttp.responseText
//  document.getElementById("email-"+hid).style.display="inline";

 }
}

function cancelStory()
{
  document.getElementById("darkBackgroundLayer").style.display = "none";
//  document.getElementById("email-"+hid).style.display="none";
	document.getElementById("floatboxLayer").style.display="none";
}
function closeComments(headlineid)
{  
// document.getElementById(headlineid).innerHTML=null;
 Effect.SlideUp(headlineid);
//  document.getElementById(commentbox).innerHTML=null;
}

function openAddLink()
{
 var div="addlinkbox"
 document.getElementById(div).innerHTML="YES";
 Effect.SlideDown(div);
}

function addLink(userid)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

var url="comments.php"
url=url+"?headlineid="+headlineid
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChangedAddLink
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChangedAddLink()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 document.getElementById(addlinkbox).innerHTML=xmlHttp.responseText
 Effect.SlideDown(addlinkbox)
 }
}

function openComments(headlineid)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

headlinediv='div_'+headlineid

var url="comments.php"
url=url+"?headlineid="+headlineid
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {

 document.getElementById(headlinediv).innerHTML=xmlHttp.responseText
 Effect.SlideDown(headlinediv)
 }
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest(); 
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function vote(userid,hid,headline,vote)
{

if (headline==null) {
 return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

var url="castvote.php"
url=url+"?headline="+headline
url=url+"&hid="+hid
url=url+"&userid="+userid
url=url+"&vote="+vote
url=url+"&sid="+Math.random()

xmlHttp.onreadystatechange=stateChangedVote
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
  
function stateChangedVote()
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {

var hid=xmlHttp.responseText
 hidspan = "v-"+hid

if (getCookie("votes")=="") {
  setCookie("votes",hid,"1");
} else {
	votes=getCookie("votes") 
	votes=votes+","+hid
	setCookie("votes",votes,"1");
 
}

 document.getElementById(hidspan).innerHTML="<span style=\"font-weight:bold;padding-right:10px;\">Thanks for your vote!</span>"
 }
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function rate(img_name,action,headlineid,userid,rating) {
	var baseurl='http://www.aroundthecapitol.com/images/';
        var imgoff='atcstar3.gif';
        var imghover='atcstar.gif';
        var imgon='atcstar.gif';
	imgname=img_name;
	hid=headlineid;
//	var current=document[img_name].src;
var current=document.getElementById(img_name).src;
	var star=img_name.replace(hid+'-star','');

	switch (action) {
		case 'hover':
	//		if (img_name.addEventListener)
	//		img_name.addEventListener('click',rate(img_name,'click',headlineid,userid,rating),false );

          //                      for (i=1;i<=star;i++) {
            //                            document.getElementById(hid+'-star'+i).src=baseurl+imghover;
              //                 }
			break;

		case 'click':
			if (current!=baseurl+imgon) {
				for (i=1;i<=star;i++) {
	                                document.getElementById(hid+'-star'+i).src=baseurl+imgon;
				}

				current=baseurl+imgon;
			
		                xmlHttp=GetXmlHttpObject()
        		        if (xmlHttp==null)
        		         {
        		         alert ("Browser does not support HTTP Request")
        		         return
		                 }
                        
                                if (getCookie("votes")=="") {
                                  setCookie("votes",hid+'-'+star,3);
                                } else {
                                        votes=getCookie("votes")
                                        votes=votes+","+hid+'-'+star
                                        setCookie("votes",votes,3);
                                }


                		var url="rate.php?";
		                url=url+"headlineid="+hid;
				url=url+"&userid="+userid;
				url=url+"&rating="+star;
		                url=url+"&sid="+Math.random();
        		        xmlHttp.onreadystatechange=stateChangedRateStory;
        		        xmlHttp.open("GET",url,true);
		                xmlHttp.send(null);
				newrank=star;

 
 

			} else {
                                document.getElementById(img_name).src=baseurl+imgoff;
                                current=baseurl+imgoff;
			}
			break;
		case 'out':

			if (current!=baseurl+imgon || (newrank!=star && rating<star)) {
                              for (i=5;i>=rating+1;i--) {
                                        //document[hid+'-star'+i].src=baseurl+imgoff;
					document.getElementById(hid+'-star'+i).src=baseurl+imgoff;
                         	}
			}

			break;
	}
}

function stateChangedRateStory() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
	document.getElementById('v-'+hid).innerHTML=xmlHttp.responseText;
}
}

function swappic(img_name,img_src) {
	document[img_name].src=img_src;
}

function openHeaderBar(menu,height) {
var bar = document.getElementById('headerbar');
var html
switch (menu) {
	case 'bills':
                html='<a onClick=\'closeHeaderBar();\'>[X] close</a><br><br>'+
                'Browse bills: <a href=/billtrack/billsbynumber.html>by number</a> | '+
                '<a href=/billtrack/author.html>by author</a> | '+
                '<a href=/billtrack/amended.html>recently amended</a><br>'+
                'Scheduled: <a href=/billtrack/upcoming.html>upcoming hearings</a><br>'+
                'Bills that have passed: <a href=billtrack/chaptered.html>chaptered</a> (signed) | '+
                '<a href=billtrack/vetoed.html>vetoed</a> | '+
                '<a href=billtrack/enrolled.html>enrolled</a> (pending gov\'s action)</li>'+
		'<br><br><a href=billtrack/>Full Bill Tracking Menu</a>'+
                '<br><br><a onClick=\'closeHeaderBar();\'>[X] close</a>'
		bar.style.display='inline'
		bar.style.height=height
                bar.innerHTML=html
		Effect.SlideDown('headerbar')
		break;

	case 'login':
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
 
		var url="login.php"                                              
		url=url+"?action=loginform"
		url=url+"&sid="+Math.random()
		xmlHttp.onreadystatechange=stateChangedLoginForm
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		break;

	default:
		break;
	}


} // close openHeaderBar

function closeHeaderBar() {
		var bar = document.getElementById("headerbar");
                bar.style.innerHTML=''
                bar.style.display='none'

}

function stateChangedLoginForm()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
	var bar = document.getElementById('headerbar');
                bar.style.height='140px'
 		bar.innerHTML=xmlHttp.responseText
	 Effect.SlideDown('headerbar')
 }
}

