function submitForm(window, formname) {
	var f = window.document.forms[formname];
	f.submit();
}

function rateMovie(guid, rating) {
	d = loadJSONDoc("/cda/rate", {
		"movie" : guid,
		"rating" : rating
	})
	d.addCallback(rateMovieCB)
	d.addErrback(rateMovieErrorCB)
}

function rateMovieCB(result) {
	if (result["accepted"]) {
		changeStars(result["params"]["rating"], true)
	}
	else if (result["errors"]) {
		e = getElement("starsError")
		e.innerHTML = result["errors"]
	}
}

function rateMovieErrorCB(result) {
	e = getElement("starsError")
	e.innerHTML = result
}

function changeStars(rating, perm) {
	var perm = (perm == null) ? false : perm

	for(i = 0; i < rating; i++) {
		e = getElement("star" + (i + 1))
		e.src = '/static/img/star_full.gif'
		if (perm) {
			e.setAttribute('onmouseout', 'changeStars(' + rating + ')')
		}
	}
	for(i = rating; i < 5; i++) {
		e = getElement("star" + (i + 1))
		e.src = '/static/img/star_empty.gif'
		if (perm) {
			e.setAttribute('onmouseout', 'changeStars(' + rating + ')')
		}
	}
}

function timeToLaunch(datelist)
{
	if (! datelist)
	{
		datelist=[2006, 10, 15, 12, 0, 0, 0];
	}

	live = new Date(datelist[0], datelist[1], datelist[2], datelist[3], datelist[4], datelist[5], datelist[6]);
	now = new Date();
	msleft = live - now
	if (msleft < 0)
		return [0, 0, 0, 0, 0]

	fdays = msleft / (1000 * 60 * 60 * 24)
	idays = parseInt(fdays)
	fhours = (fdays - idays) * 24
	ihours = parseInt(fhours)
	fminutes = (fhours - ihours) * 60
	iminutes = parseInt(fminutes)
	fseconds = (fminutes - iminutes) * 60
	iseconds = parseInt(fseconds)
	fms = (fseconds - iseconds) * 1000
	ims = parseInt(fms)

	return [idays, ihours, iminutes, iseconds, ims]
}

	/*
	checks if the date is valid
	*/
	function checkDate(theDay, theMonth, theYear, dateLabel, errorMsg, formName) {
	var dateError = ""; //holds the statements to be used in the alert
	//var theDay = eval("document." + formName + "." + dayField + ".value");
	//var theMonth = eval("document." + formName + "." + monthField + ".value");
	//var theYear = eval("document." + formName + "." + yearField + ".value");
	    //month and day checker. identifies leap years too.
	    if (theMonth > 12 ) {
	        dateError = "Please enter a number between 1 and 12 for the " + dateLabel + " month.\n";
	    } else if ((theMonth == 1 || theMonth == 3 ||
	                theMonth == 5 || theMonth == 7 ||
	                theMonth == 8 || theMonth == 10 ||
	                theMonth == 12) &&
	                (theDay > 31 || theDay < 1)) {
	        dateError += "Please enter a number between 1 and 31 for the " + dateLabel + " month. \n";
	    } else if ((theMonth == 4 || theMonth == 6 ||
	                theMonth == 9 || theMonth == 11) &&
	                (theDay > 30 || theDay < 1)) {
	        dateError += "Please enter a number between 1 and 30 for the " + dateLabel + " day. \n";
	    } else if (theMonth == 2) {
	        //checking for leap year
	        if (theYear % 100 == 0) {
	            if (theYear % 400 == 0) {
	                if (theDay > 29 || theDay < 1) {
	                    dateError += "Please enter a number between 1 and 29 for the " + dateLabel + " day. \n";
	                }
	            }
	        } else if (theYear % 4 == 0) {
	            if (theDay > 29 || theDay < 1) {
	                dateError += "Please enter a number between 1 and 29 for the  " + dateLabel + " day. \n";
	            }
	        } else if (theDay > 28 || theDay < 1) {
	            dateError += "Please enter a number between 1 and 28 for the " + dateLabel + " day. \n";
	        }
	    }
	    //checks the year.
	    if (theYear != "") {
	        if (theYear.length < 4) {
	            dateError += "Please enter a four digit number for the " + dateLabel + " year. \n";
	        } else if (theYear < 1900) {
	            dateError += "The " + dateLabel + " year appears to be invalid. \n";
	        }
	    }
	    //adds the dateError messages to errorMsg, if neccessary.
	    if (dateError != "") {
	        errorMsg += dateError;
	    }
	        return errorMsg;
	}
	/*
	checks to see if the publish date is before the expiration date.
	*/
	function pubVexp(pubDay, pubMonth, pubYear,
	                    expDay, expMonth, expYear,
	                    errorMsg, formName) {
	    //var pubMonth = eval("document." + formName + "." + pubMM + ".value");
	    //var pubDay = eval("document." + formName + "." + pubDD + ".value");
	    //var pubYear = eval("document." + formName + "." + pubYYYY + ".value");
	    if (pubMonth.length == 1) {
	        pubMonth = "0" + pubMonth;
	    }
	    if (pubDay.length == 1) {
	        pubDay = "0" + pubDay;
	    }
	    //var expMonth = eval("document." + formName + "." + expMM + ".value");
	    //var expDay = eval("document." + formName + "." + expDD + ".value");
	    //var expYear = eval("document." + formName + "." + expYYYY + ".value");
	    if (expMonth.length == 1) {
	        expMonth = "0" + expMonth;
	    }
	    if (expDay.length == 1) {
	        expDay = "0" + expDay;
	    }
	    var pubDate = new Date(pubYear, pubMonth-1, pubDay);
	    var expDate = new Date(expYear, expMonth-1, expDay);
	    var todaysDate = new Date();
	    pubDate = pubDate.getTime();
	    expDate = expDate.getTime();
	    todaysDate = todaysDate.getTime();
	
	    if ((pubDay != "") && (pubMonth != "") && (pubYear != "") &&
	        (expDay != "") && (expMonth != "") && (expYear != "")) {
	        if (pubDate >= expDate) {
	            errorMsg += "The End date can not occur before or on the Start date.\n";
	        }
	    }
	
	    if ((expDay != "") && (expMonth != "") && (expYear != "")) {
	        if (todaysDate > expDate) {
	            errorMsg += "The End date should occur in the future.\n";
	        }
	    }
	    return errorMsg;
	} 	

