﻿/*
	Sets the rating on the 5-star control.
*/
function setRating(rating, star1, star2, star3, star4, star5, hiddenRatingField, hiddenStampField) {
	
	
	//Put the items into an array
	var stars = new Array();
	stars[0] = star1;
	stars[1] = star2;
	stars[2] = star3;
	stars[3] = star4;
	stars[4] = star5;
	
	//Loop
	for(i = 1; i <= 5; i++) {
	
		var star = document.getElementById(stars[i - 1]);	
		var url = star.src.substring(0, star.src.lastIndexOf("/") + 1);
		
		if(i > rating) {
			star.src = url + "GreyStar.png";
		} else {
			star.src = url + "YellowStar.png";
		}
	}
	
	
	
	//Set the value in the hidden field
	document.getElementById(hiddenRatingField).value = rating;
	
	//Record the stamps
	if(document.getElementById(hiddenStampField).value == "") {
		document.getElementById(hiddenStampField).value = getDateStamp();
	}
}

/* 
	Gets the current date/time
*/
function getDateStamp() {

	var d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth();
	var date = d.getDate();
	var hour = d.getHours();
	var minute = d.getMinutes();
	var second = d.getSeconds();	
	
	return year + "-" + toDoubleDigits(month) + "-" + toDoubleDigits(date) + " " + toDoubleDigits(hour) + ":" + toDoubleDigits(minute) + ":" + toDoubleDigits(second);	
}

//Formats a number to 2 digits - e.g. 1 becomes 01
function toDoubleDigits(input) {

	var result = input.toString();
	if(parseInt(input) < 10) {
		result = "0" + input.toString();
	} else {
		result = input.toString();
	}

	return result;
}

//Opens a popup
function openPopup(url) {

	window.open(url, "popup", "width=300,height=260");

}

//Unselects the given radio buttons
function UnselectRadios(radio1, radio2) {
	document.getElementById(radio1).checked = false;
	document.getElementById(radio2).checked = false;
}

//Ensures that all films are rated
function EnsureRatings(data) {
	
	var allRated = true;

	for(var i = 0; i < data.RadioButtons.length; i++) {
	
		var firstChoice = document.getElementById(data.RadioButtons[i] + "_FirstChoice");
		var secondChoice = document.getElementById(data.RadioButtons[i] + "_SecondChoice");
		var thirdChoice = document.getElementById(data.RadioButtons[i] + "_ThirdChoice");
		
		if(!firstChoice.checked && !secondChoice.checked && !thirdChoice.checked) {
			allRated = false;
			break;
		}
	}
	
	if(!allRated) {
		alert("Please select an option for each clip.");
	}
	
	return allRated;
}