/* Writes the gallup part to the front page. */
function writeGallupToFrontPage() {

	//the information for writing the gallup is contained in a field named "GallupInfo"
	//and it is in the following format: question$#$page code@#$answer option 1#$#answer option 2#$#answer option 3 etc.
	gallupString = window.document.forms[0].GallupInfo.value;
	
	//first, separate the question part and the page key into strings and the answer options into an array..
	question = gallupString.substring(0, gallupString.indexOf( "$#$" ));
	//alert( "Küsimus on " + question );
	answers = gallupString.substring(gallupString.indexOf( "@#$" ) + 3, gallupString.length);
	//alert( "Vastused on " + answers );
	answerArray = answers.split("#$#");
	
	//don't write anything if there's no gallup currently available...
	if ( answerArray.join( "" ) != "" ) {
	
		//write the HTML code for the poll...
		document.write( '<table width="161" border="0" cellspacing="0" cellpadding="0">' );
		document.write( '<tr>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="5"><img src="../Files/px/$file/px.gif" width="8" height="8"></td>' );
		document.write( '<td bgcolor="A5A5BB" height="17" class="menua" width="145"><b>' + window.document.forms[0].term6.value + '</b></td>' );
		document.write( '<td bgcolor="A5A5BB" width="5"><img src="../Files/px/$file/px.gif" width="5" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '</tr>' );
		document.write( '<tr>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td width="5"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td width="145"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td width="5"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '</tr>' );
		document.write( '<tr>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td width="5">&nbsp;</td>' );
		document.write( '<td width="145">' );
		
		document.write( '</FORM><FORM method="POST">' );
		document.write( '<P><B>' + question + '</B></P>' );
		document.write( '<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">' );
		for (var i = 0; i < answerArray.length; i++ ) {
			document.write( '<TR>' );
			document.write( '<TD width="25">' );
			document.write( '<INPUT type="radio" name="poll" value="' + answerArray[i] + '">' );
			document.write( '</TD>' );
			document.write( '<TD>' + answerArray[i] + "</TD>" );
			document.write( '</TR>' );
		}
		document.write( '</TABLE>' );	
		document.write( '<P align="center">' );
		document.write( '<A href="" onClick="vote();return false;"><IMAGE border="0" src="../Files/' + window.document.forms[0].term8.value + '/$file/' + window.document.forms[0].term8.value + '.gif"></A>' );
		document.write( '</P>' );
		document.write( '</FORM><FORM>' );
		
		document.write( '</td>' );
		document.write( '<td width="5">&nbsp;</td>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '</tr>' );
		document.write( '<tr>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td width="5"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td width="145"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td width="5"><img src="../Files/px/$file/px.gif" width="1" height="5"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '</tr>' );
		document.write( '<tr>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="5"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="145"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="5"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '<td bgcolor="A5A5BB" width="1"><img src="../Files/px/$file/px.gif" width="1" height="1"></td>' );
		document.write( '</tr>' );
		document.write( '</table>' );
		document.write( '<img src="../Files/px/$file/px.gif" width="5" height="2">' );

	}

}  

/* Get the number of selected radiobutton. */
function getSelectedOption() {
	
	//get the radiobuttons objects...
	var gallupOptions = document.forms[1].poll;
	
	//check if at least one is checked...
	for ( var i = 0; i < gallupOptions.length; i++ ) {
		if ( gallupOptions[i].checked ) {
			return i + 1;
		}
	}
	return null;
	
}

/* Submits the gallup as the user votes. */
function vote() {
	
	//get the selected radiobutton...
	var selectedNr = getSelectedOption();
	
	//first, check that at least one option was selected...
	if ( selectedNr == null ) {
		alert( window.document.forms[0].term7.value );
		return;
	} else {
		
		gallupString = window.document.forms[0].GallupInfo.value;
		pagekey = gallupString.substring(gallupString.indexOf( "$#$" ) + 3, gallupString.indexOf("@#$"));
		//alert( "Lehe võti on " + pagekey );
		window.location.href = "../SubmitVote?OpenAgent&pollpage=" + pagekey + "&vote=" + selectedNr;

	}
	
}
