/*
* ALAX TO LOAD SPAN WITH HTML
* JOHAN GROBLER
*/

document.write("<span id='poll_voting'></span>");

var strQueryString='';

 
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 null
} 
 

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setTime(exdate.getTime()+(expiredays*24*3600*1000))
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate)
}




function getURLParam(){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
  
  strQueryString = strHref.substr(strHref.indexOf("?")+5).toLowerCase();

	var coki = getCookie('pole_vote_8');
   
	strQueryString = 'cmd=public_vote&voted=' + coki + '&'+strQueryString;
  
}
    
    
}


var http_request = false;
var html_span    = 'myspan';
   
function makePOSTRequest(targetspan,url, parameters) { 
   		
      http_request = false;
      html_span    = targetspan;      
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
}

function alertContents() {  		  
   	
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById(html_span).innerHTML = result;   			  		 
         } else {
            document.getElementById(html_span).innerHTML = 'There was a problem with the request.';
         }
      }
}
    
   
function getCatPoll(){ 
	 	 
	makePOSTRequest('poll_voting','../cgi-bin/giga.cgi',strQueryString);


}


function submitVoting(){
	 
	var rad_val =0;
	var poll_id = 	document.form_poll_voting.poll_id.value;
	var cause_id = 	document.form_poll_voting.cause_id.value;
	var cat_id = 		document.form_poll_voting.cat_id.value;
	
	for (var i=0; i < document.form_poll_voting.poll_answer.length; i++)   {
   if (document.form_poll_voting.poll_answer[i].checked)      {
      	rad_val = document.form_poll_voting.poll_answer[i].value;
      }
   }
	
	//var answer = document.form_poll_voting.answer.value;
	
		//set cookie 
		var cki = 'pole_vote_'+poll_id;
 
	  setCookie(cki,'YES',100);
  
   var qryString = "cmd=public_vote_submit&poll_id="+poll_id+"&option_id="+rad_val+"&cause_id="+cause_id+'&cat_id='+cat_id;
	 showIndicator('poll_voting',poll_id,cause_id,rad_val,cat_id);
	 
	 makePOSTRequest('poll_voting','../cgi-bin/giga.cgi',qryString);
	
}

function showIndicator(id,poll_id,cause_id,option_id,cat_id){

	var remoteimg = '<img src="http://www.givengain.com/cgi-bin/remoteVote.cgi?poll_id='+poll_id+'&option_id='+option_id+'&cause_id='+cause_id+'&cat_id='+cat_id+'" height=0 width=0>'

	//alert(remoteimg);

	document.getElementById(id).innerHTML = '<br><img src="../unique/litnet/images/hd_segou.gif"><br><br><img src="../images/indicator.gif"> Submitting vote '+remoteimg+'<br><br>';

}


getURLParam();
getCatPoll();  



   