function divSwap(div1,div2)
{
	var show = findDOM(div1, 1); 
	var hide = findDOM(div2, 1);
		
	hide.display = "none";
	show.display = "block";
	
	return false;
}	

function tabSwap(div1,div2)
{
	var active = findDOM(div1, 0); 
	var inactive = findDOM(div2, 0);
			
	active.setAttribute("class", "tab active"); 
	active.setAttribute("className", "tab active"); 
	
	inactive.setAttribute("class", "tab"); 
	inactive.setAttribute("className", "tab"); 

	return false;
}

function selectTab(tabIndex, pollIndex)
{
    if(tabIndex == 1)
    {
        tabSwap('tbPoll' + pollIndex, 'tbResults' + pollIndex); 
        divSwap('divVote' + pollIndex, 'divResults' + pollIndex); 
        return false;
    }
    else
    {
        tabSwap('tbResults' + pollIndex, 'tbPoll' + pollIndex); 
        divSwap('divResults' + pollIndex, 'divVote' + pollIndex); 
        return false;
    }
}


function radioHover(rad)
{
	rad.setAttribute("class", "hover");
	rad.setAttribute("className", "hover");
}

function radioOff(rad)
{
	rad.setAttribute("class", "");
	rad.setAttribute("className", "");
}






//takes the indexes of the polls and poll choices in order to determine the ids of the span tags
//findDOM is a method I got out of a book, that gets the DOM object and works in many browsers.	
function setAJAXValues(pollINDEX, choiceClickedINDEX, otherChoicesARRAYIds)
{
    
     var numVotesClicked = parseInt(findDOM('ax_votes' + choiceClickedINDEX, 0).innerHTML) + 1;
     var numVotesTotal = parseInt(findDOM('ax_total' + pollINDEX, 0).innerHTML) + 1;
     var percentClicked = Math.round(100*numVotesClicked/numVotesTotal);     
     
     //updates the number of votes for the radio button clicked
     findDOM('ax_votes' + choiceClickedINDEX, 0).innerHTML = numVotesClicked;  
     
     //updates the percent and the bar for the radio button clicked                   
     findDOM('ax_percent' + choiceClickedINDEX, 0).innerText = percentClicked + '%';
     findDOM('ax_percentbar' + choiceClickedINDEX, 1).width = percentClicked + '%';     
     
     //updates the total number of votes for the poll
     findDOM('ax_total' + pollINDEX, 0).innerHTML = numVotesTotal;
     findDOM('ax_totalagain' + pollINDEX, 0).innerHTML = numVotesTotal;
     
     var numVotesEachRadio = null;
     var percentEachRadio = null;
     for(var i = 0; i<otherChoicesARRAYIds.length; i++)
     {
        numVotesEachRadio = parseInt(findDOM('ax_votes' + otherChoicesARRAYIds[i], 0).innerHTML);
        percentEachRadio = getPercent(numVotesEachRadio, numVotesTotal);
        
        findDOM('ax_percent' + otherChoicesARRAYIds[i], 0).innerHTML = percentEachRadio + '%';  
        findDOM('ax_percentbar' + otherChoicesARRAYIds[i], 1).width = percentEachRadio + '%';
        /*if(percentEachRadio > 0)
            findDOM('ax_graph' + otherChoicesARRAYIds[i], 1).display = 'inline';
        else
            findDOM('ax_graph' + otherChoicesARRAYIds[i], 1).display = 'none';*/
     }
}


function getPercent(numVotes, numVotesTotal)
{
    return Math.round(100*numVotes/numVotesTotal);
}	


	

var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;
if (document.getElementById) {isID = 1; isDHTML = 1;}
else {
if (document.all) {isAll = 1; isDHTML = 1;}
else {
browserVersion = parseInt(navigator.appVersion);
if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
}}
function findDOM(objectID,withStyle) {
	if (withStyle == 1) {
		if (isID) { return (document.getElementById(objectID).style) ; }
		else { 
			if (isAll) { return (document.all[objectID].style); }
		else {
			if (isLayers) { return (document.layers[objectID]); }
		};}
	}
	else {
		if (isID) { return (document.getElementById(objectID)) ; }
		else { 
			if (isAll) { return (document.all[objectID]); }
		else {
			if (isLayers) { return (document.layers[objectID]); }
		};}
	}
}


var g_pollINDEX = null;
var g_choiceClickedINDEX = null;
var g_otherChoicesARRAYIds = null;
var baseURL = 'http://www.connectingsingles.com/prog/_polls.aspx';  
function submitPollVote(pollINDEX, choiceClickedINDEX, otherChoicesARRAYIds, isl, typ)
{
    if (supportsAJAX()) 
    {
          g_pollINDEX = pollINDEX;
          g_choiceClickedINDEX = choiceClickedINDEX;
          g_otherChoicesARRAYIds = otherChoicesARRAYIds;
          req = getXmlHttpRequest(false);          
          var reqURL = baseURL + '?pid=' + g_pollINDEX + '&pcid=' + g_choiceClickedINDEX + '&a=1&l=' + isl + '&typ=' + typ;
          sendHttpRequest(req, 'GET', reqURL, true, doUpdate);
          //setAJAXValues(g_pollINDEX, g_choiceClickedINDEX, g_otherChoicesARRAYIds);
          return true;
    }
}


function doUpdate() 
{
    if (!req) 
    {
        return;
    }
    var responseJS = '';
    try 
    {
       if (!callInProgress(req)) 
       {
         if (req.readyState == 4) 
         {
             //alert(req.status );
            //alert(req.responseText);
            if(req.responseText != '')
            {
                 var returnValue = req.responseText;
                 //alert(returnValue);
                 if(returnValue == -2)
                 {
                    alert('You have already voted for this poll');
                 }
                 else if(returnValue == -1)
                 {
                    alert('You must be logged in to vote for this poll');
                 }
                 else
                 {
                     setAJAXValues(g_pollINDEX, g_choiceClickedINDEX, g_otherChoicesARRAYIds);
                     //findDOM('ax_numvotes' + index2, 0).innerHTML = numVotesTotal;
                     //findDOM('ax_numvotess' + index, 0).innerHTML = numVotes;
                     //findDOM('ax_percent' + index, 0).innerHTML = percent;
                     //findDOM('ax_total' + index2, 0).innerHTML = numVotesTotal; 
                  }
             }                
          }
        }
      }
      catch (err) 
      {
        alert('State Response error: ' + err); alert(responseJS);        
      }
}


