// http://www.ibm.com/developerworks/web/library/wa-ajaxintro2/
var xmlHttp = null;

function MakeConn()
{
	try{
		xmlHttp = new XMLHttpRequest();		// Firefox, Opera 8.0+, Safari
	}
	catch( e ){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e ){
			try{
				xmlHttp=new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e ){
				alert( "This browser cannot support voting." );
				return false;
			}
		}
	}
}

function InsScriptNode( url, body )
{
	if( url )
	{
		var head = document.getElementsByTagName( "body" )[0];
		var node = document.createElement( "script" );
		
		node.type = "text/javascript";
		node.src = url;
		head.appendChild( node );
	}
	else if( body )
	{
		eval( body );
	}
}

function ValidateVote( form )
{
	var vote = -1;
	
	for( var i=0; i < form.v.length; i++ )
	{
		if( form.v[i].checked )
		{
			vote = form.v[i].value;
			break;
		}
	}
	if( vote <= 0 )
	{
		alert( "Are you some kind of hillbilly?!  Get off my land!" );
		return false;
	}
	
	return DoVote( form, vote );
}

function DoVote( form, vote )
{
	if( xmlHttp ) return false;
	
	form.act.disabled = true;
	var pk = form.pk.value;
	var node = document.getElementById( "castMsg" );
	node.innerHTML = "<img src=\"../progbar.gif\" width=220 height=19>";
	
	MakeConn();
	xmlHttp.onreadystatechange = function(){
		if( xmlHttp.readyState == 4 )
		{
			form.act.disabled = false;		// Allow resubmission now
			var raw = xmlHttp.responseText;
			
			if( xmlHttp.status == 200 && raw.search( "Failed" ) < 0 )
			{
				xmlHttp = null;
				var vote = raw.split( "," );
				var bayAvg = parseFloat( vote[0] );
				var cnt    = parseInt( vote[1] );
				
				node.innerHTML = "Your vote was recorded.";
				
				var snode = document.getElementById( "mySR" );
				for( i = 0; i < snode.childNodes.length; i += 1 )
					if( snode.childNodes[i].nodeType == 1 )
					{
						snode.childNodes[i].style.width = bayAvg * 10;
						break;
					}
				
				var vnode = document.getElementById( "voteCnt" );
				if( cnt > 2 ) vnode.innerHTML = "You and " + (cnt - 1) + " other people have rated this pitch:";
				else if( cnt == 2 ) vnode.innerHTML = "You and one other person have rated this pitch:";
				else vnode.innerHTML = "You have rated this pitch at:";
				
				var anode = document.getElementById( "voteAvg" );
				anode.innerHTML = "(" + bayAvg.toFixed( 3 ) + ")";
			}
			else
			{
				xmlHttp = null;
				node.innerHTML = "An error occured. Please try again.";
				for( var i=1; i < form.v.length; i++ )
					form.v[i].checked = false;
				form.v[0].checked = true;
			}
		}
	};
	
	var params = "pk="+escape( pk )+"&v="+ vote +"&usr="+ escape( form.usr.value )+"&act=ajax";
	//node.innerHTML = "<pre>" + params + "</pre>";
	xmlHttp.open( "POST", form.action, true );
	xmlHttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	xmlHttp.setRequestHeader( "Content-length", params.length );
	xmlHttp.setRequestHeader( "Connection", "close" );
	xmlHttp.send( params );
	return false;
}

function IncludeJS( src, func, nodeID )
{
	var node = document.createElement( "script" );
	node.type = "text/javascript";
	node.src = src;
	if( nodeID ) node.id = nodeID;

	node.onreadystatechange = function(){
		if( node.readyState == "complete" || node.readyState == "loaded" ) func();
	};
	node.onload = function(){
		func();
	};

	var head = document.getElementsByTagName( "head" )[0];
	head.appendChild( node );
}

function Fix4DB()
{
	var form = document.getElementById( "castForm" );
	var cnt  = DBVoteCnt();
	if( cnt > 0 )
	{
		var hasVote = DBIsaVoter();
		var msg;

		if( cnt > 2 )
		{
			if( hasVote ) msg = "You and " + (cnt - 1) + " other people have rated this pitch:";
			else msg = cnt + " other people have rated this pitch:";
		}
		else if( cnt == 2 )
		{
			if( hasVote ) msg = "You and one other person have rated this pitch:";
			else msg = cnt + " other people have rated this pitch:";
		}
		else if( cnt == 1 )
		{
			if( hasVote ) msg = "You have rated this pitch at:";
			else msg = "1 other person has rated this pitch:";
		}
		var node = document.getElementById( "voteCnt" );
		node.innerHTML = msg;
 
		var bayAvg = DBVoteAvg();
		node = document.getElementById( "mySR" );
		for( i=0; i < node.childNodes.length; i += 1 )
			if( node.childNodes[i].nodeType == 1 )
			{
				node.childNodes[i].style.width = bayAvg * 10;
				break;
			}
		 
		node = document.getElementById( "voteAvg" );
		node.innerHTML = "(" + bayAvg.toFixed( 3 ) + ")";
		if( hasVote )
		{       
			var vote = DBMyVote();
			for( var i=0; i < form.v.length; i++ )
				form.v[i].checked = false;
			form.v[vote].checked = true;
		}
	}

	var host = GetRemoteHost();
	if( host.indexOf( "@" ) > 0 ) form.usr.value = host;
	else if( !navigator.javaEnabled())
	{
		host += "<br>(This may be a shared address from a proxy or firewall. If you enable java &amp; refresh this page, you should have a unique vote.)";
	}
	var node = document.getElementById( "castMsg" );
	node.innerHTML = "Your vote will be recorded as: " + host;

	var node = document.getElementById( "dbcnt" );
	node.innerHTML = DBStoryCnt();
}
