/*
Do rating system
*/
function rate( num, root, id )
{
	if( num == 1 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";

		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 2 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";

		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 3 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";

		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 4 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";

		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 5 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-full.jpg";
	}
}

function reset( root, id )
{
	voteSet = document.getElementById(id+'rating').value;
	if ( voteSet == false || voteSet == '' )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else
	{
		var num = voteSet;
		
		if( num == 1 )
		{
			document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"2").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
		}
		else if( num == 2 )
		{
			document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
		}
		else if( num == 3 )
		{
			document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
			document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";

		}
		else if( num == 4 )
		{
			document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
		}
		else if( num == 5 )
		{
			document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";
			document.getElementById(id+"5").src = ""+root+"/_layout/images/star-full.jpg";
		}
	}
}



var voteSet = false;

function vote_form( num, root, id )
{
	voteSet = num;
	if( num == 1 )
	{       
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 2 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 3 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-blank.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";

	}
	else if( num == 4 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-blank.jpg";
	}
	else if( num == 5 )
	{
		document.getElementById(id+"1").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"2").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"3").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"4").src = ""+root+"/_layout/images/star-full.jpg";
		document.getElementById(id+"5").src = ""+root+"/_layout/images/star-full.jpg";
	}                                                    
	
	document.getElementById(id+'rating').value=num;
}

var tmp_divid;
var tmp_id;
var tmp_root;
var tmp_num;

function live_vote ( num, root, id, divid, prodid )
{
  // this function should use ajax to save the new vote to the database
  tmp_num = num;
  tmp_root = root;
  tmp_id = id;
  tmp_divid = divid;

  // step 1, replace the innerHTML for the div supplied with a flashing immage 'casting rating' or similar
  document.getElementById(divid).innerHTML = "<img src = \""+root+"/_layout/images/ani-saving.gif\" />";

  // step 2, the ajax should be queried to save the new score


  // ajax needs to 
  //   - load the product with id of 'id'
  //   - get the 'rating' and 'no_of_ratings' fields from database
  //   - multiply rating by no_of_ratings then add the new rating in javascript var 'num'
  //   - add 1 to 'no_of_ratings'
  //   - devide new total by 'no_of_ratings' to get the average rating 
  //   - save the no_of_ratings and 'rating' back to database
  //   - save the new average rating in global javascript variable 'tmp_num'
  //   - lastly run the function 'live_vote_ajax_finished'
  xmlHttp = GetXmlHttpObject();
  var url = root+"/_inc/ajax/saverating.php?id="+escape(prodid)+"&rating="+escape(num)+"&sid="+Math.random();
  xmlHttp.onreadystatechange = live_vote_ajax_finished;
  xmlHttp.open( "GET", url, true );
  xmlHttp.send(null);

  // this line is just for testing, take out when ajax works
  //setTimeout ( 'live_vote_ajax_finished ()', 3000 );
}

function live_vote_ajax_finished ()
{
	
  // step 3, once the score has been saved the innerHTML should be replaced with a flashing image 'rating casted, thank you' or similar
  document.getElementById(tmp_divid).innerHTML = "<img src = \""+tmp_root+"/_layout/images/ani-saved.jpg\" />";

  setTimeout ( 'live_vote_ajax_swap_back ()', 2000 );
}

function live_vote_ajax_swap_back ( )
{

  // step 4, after a delay, the innerHTML should be replaced for the new star rating if there is one but without the ability to vote

  document.getElementById(tmp_divid).innerHTML = "<img id = \""+tmp_id+"1\" src=\"\" /><img id = \""+tmp_id+"2\" src=\"\" /><img id = \""+tmp_id+"3\" src=\"\" /><img id = \""+tmp_id+"4\" src=\"\" /><img id = \""+tmp_id+"5\" src=\"\" />";
  vote ( tmp_num, tmp_root, tmp_id );
}

// Ajax handler
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

