function checkLogin()
{
	var errors = 0
	var email = trimAll(document.form_login.login_email.value)
	var psswd = trimAll(document.form_login.login_password.value)
	
	var error_message_span = document.getElementById('error_message')
	
	if (error_message_span != null) {
		document.getElementById('error_message').style.visibility = 'hidden'
	}
		
	regexp = new RegExp('^[A-Za-z0-9._-]{2,}@[A-Za-z0-9.-]{2,}[.][A-Za-z]{2,4}$');
	
	if (email.length < 6 || email.length > 255)
	{
		document.getElementById('error_email').style.visibility = 'visible'
		errors = errors + 1
	}
	else
		document.getElementById('error_email').style.visibility = 'hidden'
		
	if (regexp.test(email) == false) 
	{
		document.getElementById('error_email').style.visibility = 'visible'
		errors = errors + 1
	}
	else 
		document.getElementById('error_email').style.visibility = 'hidden'
	
	
	if(errors > 0)
		return false
	else
	{
		userLogin(email, psswd);
		return;
	}
}

function addCommentCheck()
{
	var errors = 0
	var title = document.form_comment.comment_title.value
    var description = document.forms['form_comment'].comment_description.value

	if(title.length < 4 || title.length > 255)
	{
		document.getElementById('error_title_comment').style.visibility = 'visible'
		errors = errors + 1
	}
	else
		document.getElementById('error_title_comment').style.visibility = 'hidden'


	if(description.length < 4 || description.length > 255)
	{
		document.getElementById('error_description_comment').style.visibility = 'visible'
		errors = errors + 1
	}
	else
		document.getElementById('error_description_comment').style.visibility = 'hidden'

	if (getCookie('loginToken') == null && errors == 0)
	{
		document.getElementById('add_comment_form').innerHTML = 'You need to be logged in to take advantage of this feature.';
		return false;
	}

	if(errors > 0)
		return false
	else
	{
		return true
	}
}

function countComment(maxlength)
{
	// Gets the field's value
	texti = document.form_comment.comment_description.value
	// Counts the length
	nb_chars = texti.length

	// If limit reached
	if(nb_chars > maxlength)
	{
		// Cuts the text
		texti = texti.substring(0, maxlength)
		document.form_comment.comment_description.value = texti
	}
	document.form_comment.comment_description.scrollTop = document.form_comment.comment_description.scrollHeight
}

function addComment()
{
	if (getCookie('loginToken') == null) {
		document.getElementById('comments_login').style.visibility = 'visible';
		document.getElementById('comments_login').style.position = 'relative';
		return;
	}
	document.getElementById('addcomment').style.display='block';
	document.getElementById('add_comment_link').style.display = 'none';
	document.getElementById('add_comment_link_top').style.display = 'none';
}

//Login Box
function openLoginBox()
{
	login_box_open = true;
	document.getElementById('login_out_box').style.visibility = 'visible';
	document.getElementById('login_out_box').style.display = 'block';
}

function closeLoginBox()
{
	if(login_box_open == true)
		setUpFade('login_out_box')
	login_box_open = false;
}
//End login box

function startShareFriends()
{
	share_friend_open = true;
	document.getElementById('send_link_box').style.visibility = 'visible';
	document.getElementById('send_link_box').style.display = 'block';
}

function closeShareFriends()
{
	if(share_friend_open == true)
		setUpFade('send_link_box')
	share_friend_open = false;
}

function openCopyVideo()
{
	copy_video_open = true;
	document.getElementById('share_link_box').style.visibility = 'visible';
	document.getElementById('share_link_box').style.display = 'block';
}

function closeCopyVideo() 
{
	if(copy_video_open == true)	
		setUpFade('share_link_box')
	copy_video_open = false;
}

function openAddComment()
{
	if (getCookie('loginToken') == null) {
		document.getElementById('comments_login').style.display = 'block';
		document.getElementById('comments_login').style.position = 'relative';
		return;
	}
	add_comment_open = true;
	document.getElementById('add_comment_box').style.visibility = 'visible';
	document.getElementById('add_comment_box').style.display = 'block';
}

function closeAddComment()
{
	if(add_comment_open == true)	
		setUpFade('add_comment_box')
	copy_video_open = false;
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function setUpFade(ename, callback) {
	lastRun = false
	fader(ename,2,50,300, callback);
}

// Fades an element
// elName - id of the element
// start - time in ms when the fading should start
// steps - number of fading steps
// time - the length of the fade in ms
var timer
function fader(elName,start,steps,time, callback) {
  setOpacity(elName,100); // To prevent flicker in Firefox
                          // The first time the opacity is set
                          // the element flickers in Firefox
  fadeStep = 100/steps;
  timeStep = time/steps;
  opacity = 100;
  time = start + 100;
  while (opacity >=0) {
   	window.setTimeout("setOpacity('"+elName+"',"+opacity+","+callback+")",time);
    opacity -= fadeStep;
    time += timeStep;
  }
}


function setOpacity(elName,opacity, callback) {
  opacity = (opacity == 100)?99:opacity;
  el = document.getElementById(elName);
  // IE
  el.style.filter = "alpha(opacity:"+opacity+")";
  // Safari < 1.2, Konqueror
  el.style.KHTMLOpacity = opacity/100;
  // Old Mozilla
  el.style.MozOpacity = opacity/100;
  // Safari >= 1.2, Firefox and Mozilla, CSS3
  el.style.opacity = opacity/100


  if(opacity <= 0)
  {

  	reg = new RegExp('^messagebar');
  	if(reg.test(elName) == true)
  	{
		messagebar--;
  	}

  	if (callback)
  	{
  		callback();
  	}

  	lastRun = true;
	document.getElementById(elName).style.visibility = 'hidden'
	document.getElementById(elName).style.display = 'none';
	setOpacity(elName, 100);
  }
}

function overStar(v)
{
	/* Preloading image */
	if(!tempImage)
	{
		tempImage = new Image;
		tempImage.src = "/front_website/images/ratevideo_gold.gif"
	}

	new_image = "/front_website/images/ratevideo_gold.gif"
	for(i=1; i<=10; i=i+1)
	{
		if(i<=v)
			document.getElementById('star_'+i).src = new_image;
		else
			document.getElementById('star_'+i).src = "/front_website/images/ratevideo_black.gif";
	}
}

function outStar()
{
	for(i=10; i > 0; i=i-1)
	{
		if(i<=hasVoted)
			document.getElementById('star_'+i).src = "/front_website/images/ratevideo_gold.gif";
		else
			document.getElementById('star_'+i).src = "/front_website/images/ratevideo_black.gif";
	}
}

function displayError(message)
{
	alert(message)
}

function displayMessage(message)
{

}

// Starts a timeout
function timeout(functionRef, param, timer)
{
	if(functionRef.timerID)
		window.clearTimeout(functionRef.timerID);
	var func = function()
	{
		functionRef.timerID=null;
		functionRef(param);
	}
	functionRef.timerID = window.setTimeout(func, timer);
}

function selectCode(elt)
{
    elt.focus();
    elt.select();
	if(document.all)
	{
		therange=elt.createTextRange()
		therange.execCommand("Copy")
	}
}

/* Old Javascript Functions */

function openWindow(filename, pic_width, pic_height) {
	pic_width=pic_width+20;
	pic_height=pic_height+20;
	winStats='resizable=no,toolbar=no,location=no,directories=no,menubar=no,';
	winStats+='scrollbars=no,width='+pic_width+',height=';
	winStats+=pic_height;
	if (navigator.appName.indexOf("Microsoft")>=0) {
		winStats+=',left=10,top=25';
	} else {
		winStats+=',screenX=10,screenY=25';
	}
	winLoc = filename;
	floater=window.open(winLoc,"maximWindow",winStats);
	if (!floater.opener) floater.opener = self;
}

function openWindowS(filename, pic_width, pic_height) {
	pic_width=pic_width+20;
	pic_height=pic_height+20;
	winStats='resizable=no,toolbar=no,location=no,directories=no,menubar=no,';
	winStats+='scrollbars=yes,width='+pic_width+',height=';
	winStats+=pic_height;
	if (navigator.appName.indexOf("Microsoft")>=0) {
		winStats+=',left=10,top=25';
	} else {
		winStats+=',screenX=10,screenY=25';
	}
	winLoc = filename;
	floater=window.open(winLoc,"maximWindow",winStats);
	if (!floater.opener) floater.opener = self;
}

dropMenu.Registry = [];
dropMenu.aniLen = 150;
dropMenu.hideDelay = 100;
dropMenu.minCPUResolution = 10;
// constructor

var _tpNS = (document.all)?false:true;

function getOffsetLeft (el) {
  var sl = el;
  var ol = el.offsetLeft;
  var sh = 0;
  while ((el = el.offsetParent) != null){
    ol += el.offsetLeft;
     if(el.offsetParent && el.offsetParent.offsetParent){
     var scrollLeft = el.offsetParent.scrollLeft;
       if(!isNaN(scrollLeft)){
         sh -= scrollLeft;
       }
    }
  }

   el = sl;
   if(_tpNS){
        while((el = el.parentNode) != null){
             if(el.parentNode && el.parentNode.parentNode
                && !(el.parentNode.tagName && 
el.parentNode.tagName.toUpperCase() == "BODY")){
      	        var scrollLeft = el.parentNode.scrollLeft;
                if(!isNaN(scrollLeft) && scrollLeft > 0 ){
                   sh -= scrollLeft;
                }
            }
        }
   }
  return ol+sh;
}

function getAbsX(elt) { return parseInt(elt.x) ? elt.x : getAbsPos(elt,"Left"); };
function getAbsPos(elt, which) { iPos = 0; while (elt != null) { iPos += elt["offset" + which]; elt = elt.offsetParent; } return iPos; }

//fires on instantiation of each new menu object
function dropMenu(id, dir, left, top, width, height)
{
this.ie = document.all ? 1 : 0
this.ns4 = document.layers ? 1 : 0
this.dom = document.getElementById ? 1 : 0
if (this.ie || this.ns4 || this.dom) 
	{
	this.id = id
	this.dir = dir
	this.orientation = dir == "left" || dir == "right" ? "h" : "v"
	this.dirType = dir == "right" || dir == "down" ? "-" : "+"
	this.dim = this.orientation == "h" ? width : height
	this.hideTimer = false
	this.aniTimer = false
	this.open = false
	this.over = false
	this.startTime = 0
	this.gRef = "dropMenu_"+id
	eval(this.gRef+"=this")
	dropMenu.Registry[id] = this
	var newleft = getAbsX(document.getElementById('t_'+id)) - 5;
	// alert(newleft);
	var d = document
	var strCSS = '<style type="text/css">';
	strCSS += '#' + this.id + 'Container { visibility:hidden; '
	strCSS += 'left:' + newleft + 'px; '
	strCSS += 'top:' + top + 'px; '
	strCSS += 'overflow:hidden; z-index:10000; }'
	strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; '
	strCSS += 'width:' + width + 'px; '
	strCSS += 'height:' + height + 'px; '
	strCSS += 'clip:rect(0 ' + width + ' ' + height + ' 0); '
	strCSS += '}'
	strCSS += '</style>'
	d.write(strCSS)
	this.load()
	}
}
dropMenu.prototype.load = function() 
{
var d = document
var lyrId1 = this.id + "Container"
var lyrId2 = this.id + "Content"
var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
var temp
if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)
else {
this.container = obj1
this.menu = obj2
this.style = this.ns4 ? this.menu : this.menu.style
this.homePos = eval("0" + this.dirType + this.dim)
this.outPos = 0
this.accelConst = (this.outPos - this.homePos) / dropMenu.aniLen / dropMenu.aniLen 
// set event handlers.
if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
this.menu.onmouseover = new Function("dropMenu.showMenu('" + this.id + "')")
this.menu.onmouseout = new Function("dropMenu.hideMenu('" + this.id + "')")
//set initial state
this.endSlide()
}
}
dropMenu.showMenu = function(id)
{
var reg = dropMenu.Registry
var obj = dropMenu.Registry[id]
if (obj.container) {
obj.over = true
for (menu in reg) if (id != menu) dropMenu.hide(menu)
if (obj.hideTimer) { reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer) }
if (!obj.open && !obj.aniTimer) reg[id].startSlide(true)
}
}
dropMenu.hideMenu = function(id)
{
var obj = dropMenu.Registry[id]
if (obj.container) {
if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
obj.hideTimer = window.setTimeout("dropMenu.hide('" + id + "')", dropMenu.hideDelay);
}
}
dropMenu.hideAll = function()
{
var reg = dropMenu.Registry
for (menu in reg) {
dropMenu.hide(menu);
if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
}
}
dropMenu.hide = function(id)
{
var obj = dropMenu.Registry[id]
obj.over = false
if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
obj.hideTimer = 0
if (obj.open && !obj.aniTimer) obj.startSlide(false)
}
dropMenu.prototype.startSlide = function(open) {
this[open ? "onactivate" : "ondeactivate"]()
this.open = open
if (open) this.setVisibility(true)
this.startTime = (new Date()).getTime() 
this.aniTimer = window.setInterval(this.gRef + ".slide()", dropMenu.minCPUResolution)
}
dropMenu.prototype.slide = function() {
var elapsed = (new Date()).getTime() - this.startTime
if (elapsed > dropMenu.aniLen) this.endSlide()
else {
var d = Math.round(Math.pow(dropMenu.aniLen-elapsed, 2) * this.accelConst)
if (this.open && this.dirType == "-") d = -d
else if (this.open && this.dirType == "+") d = -d
else if (!this.open && this.dirType == "-") d = -this.dim + d
else d = this.dim + d
this.moveTo(d)
}
}
dropMenu.prototype.endSlide = function() {
this.aniTimer = window.clearTimeout(this.aniTimer)
this.moveTo(this.open ? this.outPos : this.homePos)
if (!this.open) this.setVisibility(false)
if ((this.open && !this.over) || (!this.open && this.over)) {
this.startSlide(this.over)
}
}
dropMenu.prototype.setVisibility = function(bShow) { 
var s = this.ns4 ? this.container : this.container.style
s.visibility = bShow ? "visible" : "hidden"
}
dropMenu.prototype.moveTo = function(p) { 
this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
dropMenu.prototype.getPos = function(c) {
return parseInt(this.style[c])
}
dropMenu.prototype.onactivate = function() {}
dropMenu.prototype.ondeactivate = function() { }

function setCookie(name, value, expires, path, domain, secure, showa) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  if (showa==1) { alert(curCookie); }
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

var now = new Date();
var expire_time = new Date();
fixDate(now); fixDate(expire_time);
expire_time.setTime(now.getTime() + 60 * 24 * 60 * 60 * 1000); // Expire in 60 days
