var page=null;
var myTimer;
var myInterval=30 * 1000;
var myClock=new Date();
var startTime=myClock.getTime();
var timeKeeper;
var usernametaken = false;
var birthdate;
var first,second,third,fourth,combined,link;
first="ken";
second="@";
third="videocritter.";
fourth="org";
combined=first+second+third+fourth;
var critterPlayer;
var currentState;
var params;

document.onkeydown = function(e) 
{
	e=e || window.event;
	if (e.keyCode == 9 && page=="main") 
		return false;
	else
		return true;
}


function playerReadyCallback(thePlayer) 
{ 
	critterPlayer = document.getElementById(thePlayer.id);
	addListeners();
}

function addListeners() {
	if (critterPlayer) { 
		critterPlayer.addModelListener("STATE", "stateListener");

	} else {
		setTimeout("addListeners()",33.333);
	}
}
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
}

function playVideo()
{
	//if(currentState != "PLAYING") 
		critterPlayer.sendEvent('PLAY');
	return;
}

function muteVideo()
{
		critterPlayer.sendEvent('MUTE');
	return;
}
function stopVideo()
{
		critterPlayer.sendEvent('STOP');
	return;
}

function pauseVideo()
{
	if(currentState == "PLAYING") critterPlayer.sendEvent('PLAY');
	return;
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	 var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	 if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function setTimer(logid)
{
	timeKeeper="goodbye"+"("+logid+")";
	myTimer=setInterval(timeKeeper,myInterval);
}

function goodbye(logid) {
  clearInterval(myTimer);
  var myClock=new Date();
  var thisMoment=myClock.getTime();
  var elapsedTime=(thisMoment-startTime)/1000;
  var params = "logid=" + logid + "&elapsed=" + elapsedTime;
  var result = timeOnPageAjax(params);
  return;
}

function showDownload(url)
{
	var prefix="http://vidcrit.org?";
	if(url.indexOf("{captions}")>0)
	{
		url=url.replace("{captions}","");
		var showDownload=true;
	}
	if(url.indexOf("critterVideo")!=-1)
	{
		prefix="";
	}	
	if(showDownload)
	{
		document.getElementById("downloadmessage").innerHTML="To download this captions file, click <a href=\'http://vidcrit.org/download.php?"+url+"\' target='download'  tabindex='-1'>here</a>. Then log in to YouTube and attach it to your video.<br/>";
	}
	else
	{
		document.getElementById("downloadmessage").innerHTML="";
	}
	document.getElementById("origurlspace").innerHTML="<p class=\'new_url\'>This <a href=\'"+prefix+url+"\'  tabindex='-1'>URL</a> is: <input type=\'text\' id=\'orig_url\' name=\'orig_url\' value=\'"+prefix+url+"\' class=\'orig_url\'  tabindex='-1'/><hr/></p>";	

	return;
}
function getVideo()
{
	var youtube_url = document.getElementById("video_url").value;

	if(youtube_url.indexOf("http://")!=0 && youtube_url.indexOf("https://")!=0 && youtube_url.indexOf("rtmp://")!=0)
	{
		if(youtube_url.length>0) youtube_url="http://gdata.youtube.com/feeds/api/videos?vq="+escape(youtube_url)+"&mat=5";
	}
	if(youtube_url.indexOf("youtube.com/v/")!=-1)
		youtube_url=youtube_url.replace("youtube.com/v/","youtube.com/watch?v=");
	var array=youtube_url.split("&username=");
	youtube_url=array[0];
	var username = document.getElementById("username").value;
	if(username.length<1 && array[1]) 
		username=array[1];
	window.location = "/?" + youtube_url + "&username=" + username;

}
function timeOnPageAjax(params) {
	var oRequest;  
	try
	{
		// Opera 8.0+, Firefox, Safari
		oRequest = new XMLHttpRequest();
	} 
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			oRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try
			{
				oRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	var sURL = "goodbye.php";
  oRequest.open("POST", sURL, true);
  oRequest.setRequestHeader("User-Agent", navigator.userAgent);
  oRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  oRequest.setRequestHeader("Content-length", params.length);
  oRequest.setRequestHeader("Connection", "close");
	oRequest.onreadystatechange=function()
	{
		if(oRequest.readyState==4)
		{
			if(oRequest.status==200)
			{
				myTimer=setInterval(timeKeeper,myInterval);
				//alert(oRequest.responseText);
				//do something here
			}
			else //there was a page load error
			{
				myTimer=setInterval(timeKeeper,myInterval);
				//do something here
			}
		}
	}
	oRequest.send(params);
	return;
}
function makelinks() {
  var reg = /(<[^>]*>)|(?:(\bto\b|\bor\b|\bby\b|\bwe\b|\bfor\b|\bnot\b|\bsome\b|\bof\b|\bthe\b|\ban\b|\bas\b|\bif\b|\blike\b|\bhad\b|\bnot\b|\band\b|\bin\b|\bon\b|\badj\b|\badv\b|\bthat\b|\bthan\b|\bthen\b|\bbut\b|\bit\b|&.{1,6};))|([a-zA-Z]{2,})/gi;
  var reg_we= /(\bwe\b)i/
  var matches = [];
  var elems = document.getElementsByTagName("*");
  for (var i = 0; i < elems.length; i++) {
    if (elems[i].id.indexOf("hypertext") == 0) matches.push(elems[i]);
  }
  for (i = 0; i < matches.length; i++) {
    var txt = document.getElementById(matches[i].id);
    var origText = txt.innerHTML;
    txt.innerHTML = "";
    txt.innerHTML = origText.replace(reg, "<a href='http://define.com/$3' title='Click for definition of \"$3\"'>$3<\/a>$1$2");
  }
}

function checkavailability()
	{
		var username = document.getElementById ("username").value;
		var params = "command=checkuser&username=" + username;
		var result = ajax (params);
		var availability;
		if (valid_username ())
			{
				if (result == "available")
					{
						availability = "Congratulations!  This username is available.";
						usernametaken = false;
					}
				else if (result == "taken")
					{
						availability = "<span class=\"red\">Sorry, the username '" + username + "' is not available.</span>";
						usernametaken = true;
					}
				else availability=result;
				document.getElementById ("availabilityresponse").innerHTML = availability;
			}
	}
function ajax(params)
	{
		var oRequest = new XMLHttpRequest ();
		var sURL = "ajax.php";
		oRequest.open ("POST", sURL, false);
		oRequest.setRequestHeader ("User-Agent", navigator.userAgent);
		oRequest.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
		oRequest.setRequestHeader ("Content-length", params.length);
		oRequest.setRequestHeader ("Connection", "close");
		oRequest.send (params);
		if (oRequest.status == 200)
		{
			return oRequest.responseText;
			;
		}
		else
		{
			alert("Error executing XMLHttpRequest call!");
		}
	}
function validateform()
	{
		if (formvalid ())
			{
				sEmail = document.getElementById ("email").value;
				sPassword = document.getElementById ("password1").value;
				sPasswordHash = hex_md5 (document.getElementById ("password1").value);
				sUsername = document.getElementById ("username").value;
				sCountry = document.getElementById ("country").value;
				sZip = document.getElementById ("zip").value;
				if (document.getElementById ("male").checked)
					sGender = "m";
				if (document.getElementById ("female").checked)
					sGender = "f";
				params = "command=sendvalidationemail";
				params += "&email=" + sEmail;
				params += "&password=" + sPassword;
				params += "&password_hash=" + sPasswordHash;
				params += "&username=" + sUsername;
				params += "&country=" + sCountry;
				params += "&zip=" + sZip;
				params += "&birthdate=" + birthdate;
				params += "&gender=" + sGender;
				result = ajax (params);

				if (result != "OK")
					{
						alert("There was an error while sending you your validation email.  Please try to register again, or after a while.");
					}
				else
					{
						document.getElementById ("signup").style.display = "none";
						document.getElementById ("confirmation").style.display = "inline"
					}
			}
	}
function formvalid()
	{
		var valid = true;
		if (!valid_email ())
			valid = false;
		if (!valid_zip ())
			valid = false;
		if (!valid_password ())
			valid = false;
		if (!valid_username ())
			valid = false;
		else
			checkavailability ();
		if (!valid_birthdate ())
			valid = false;
		if (!valid_gender ())
			valid = false;
		if (!valid_captcha ())
			valid = false;
		if (!valid_terms ())
			valid = false;
		if (usernametaken)
			valid = false;
		return valid;
	}
function valid_gender()
	{
		if (document.getElementById ("male").checked || document.getElementById ("female").checked)
			{
				document.getElementById ("valid_gender").style.display = "none";
				return true;
			}
		else
			{
				document.getElementById ("valid_gender").style.display = "inline";
				return false;
			}
	}
function valid_email()
	 {
		 sEmail=document.getElementById("email").value;
		 if (sEmail.length>1)
			 {
				 result=emailCheck (sEmail);
				 if(result=="valid")
				 {
					document.getElementById ("valid_email").style.display = "none";
					return true;
				 }
				 else
				 {
					document.getElementById ("valid_email").innerHTML="<br />"+result;
					document.getElementById ("valid_email").style.display = "inline"; 
				 }
			 }
		 else
			 {
				 document.getElementById ("valid_email").innerHTML="Please enter e-mail.";
				 document.getElementById ("valid_email").style.display = "inline";
				 return false;
			 }
	 }
function valid_terms()
	 {
		 if (document.getElementById ("agreement").checked)
			 {
				 document.getElementById ("valid_terms").style.display = "none";
				 return true;
			 }
		 else
			 {
				 document.getElementById ("valid_terms").style.display = "inline";
				 return false;
			 }
	 }
function valid_birthdate()
	 {
		 document.getElementById ("valid_birthdate").style.display = "none";
		 var month = document.getElementById ("birthday_month").value;
		 var day = document.getElementById ("birthday_day").value;
		 var year = document.getElementById ("birthday_year").value;
		 birthdate = year + "-" + month + "-" + day;
		 var today = new Date ();
		 var myDate = new Date ();
		 myDate.setFullYear (year, month - 1, day);
		 if (!isValidDate (birthdate, "YMD") || myDate > today)
			 {
				 document.getElementById ("valid_birthdate").style.display = "inline";
				 return false;
			 }
		 else
			 return true;
	 }
function valid_username()
	{
		document.getElementById ("valid_username").style.display = "none";
		var username = document.getElementById ("username").value;
		if (username.length < 1)
			{
				document.getElementById ("valid_username").innerHTML = "Please enter username.";
				document.getElementById ("valid_username").style.display = "inline";
				document.getElementById ("availabilityresponse").innerHTML = "";
				return false;
			}
		var regexp = /^[0-9a-zA-Z]|-_/
		if (!regexp.test (username))
			{
				document.getElementById (
					"valid_username").innerHTML
					= "The username can only contain a-Z, 0-9, dash and underline characters.  It must start with a letter or number.";
				document.getElementById ("availabilityresponse").innerHTML = "";
				document.getElementById ("valid_username").style.display = "inline";
				return false;
			}
		return true;
	}
function valid_password()
	{
		var password1 = document.getElementById("password1").value;
		var password2 = document.getElementById("password2").value;
		var valid = true;
		if (password1.length > 0)
			{
				document.getElementById ("valid_password1").style.display = "none";
			}
		else
			{
				document.getElementById ("valid_password1").style.display = "inline";
				valid = false;
			}
		if (password2.length > 0)
			{
				document.getElementById ("valid_password2").style.display = "none";
			}
		else
			{
				document.getElementById ("valid_password2").style.display = "inline";
				valid = false;
			}
		if (password1 == password2)
			{
				var regexp = /[0-9a-zA-Z]|[-!@#$%]/;
				if (regexp.test (password1) && password1.length>0)	
				{
					if (password1.length>5 && password1.length<21)
					{
						document.getElementById ("valid_password3").style.display = "none";
					}
					else 
					{
						document.getElementById ("valid_password3").innerHTML="Password must be between 6 and 20 characters";	
						document.getElementById ("valid_password3").style.display = "inline";			
						valid = false;
					}
				}
				else if (password1.length>0)
				{
					document.getElementById ("valid_password3").innerHTML="Password contains illegal characters. (allowed are a-Z,0-9,!@#$%-)";	
					document.getElementById ("valid_password3").style.display = "inline";			
					valid = false;
				}
			}
		else
			{
				document.getElementById ("valid_password3").innerHTML="Passwords do not match.";	
				document.getElementById ("valid_password3").style.display = "inline";			
				valid = false;
			}
			
		return valid;
	}
function valid_zip()
	{
		if (document.getElementById ("country").value == "US")
			{
				if (isValidZipCode (document.getElementById ("zip").value))
					{
						document.getElementById ("valid_zip").style.display = "none";
						return true;
					}
				else
					{
						document.getElementById ("valid_zip").style.display = "inline";
						return false;
					}
			}
		else
			return true;
	}
function valid_captcha()
	{
		if (document.getElementById("captcha").disabled) return true;
		code = document.getElementById ("captcha").value;
		params = "command=captcha&code=" + code;
		valid = ajax (params);
		if (valid == "true")
			{
				document.getElementById("captcha").disabled=true;
				return true;
			}
		else
			{
				document.getElementById ("valid_captcha").style.display = "inline";
				var randomnumber = Math.floor (Math.random () * 999999999);
				document.getElementById ("captchaimage").src = "securimage_show.php?sid=" + randomnumber;
				document.getElementById ("captcha").value = "";
				document.getElementById ("captcha").disabled = false;
				return false;
			}
	}
function isValidZipCode(value)
	{
		var regexp = /^\d{5}([\-]\d{4})?$/;
		return (regexp.test (value));
	}

function emailCheck (emailStr)
	{
		/* The following variable tells the rest of the function whether or not
		to verify that the address ends in a two-letter country or well-known
		TLD.  1 means check it, 0 means don't. */
		var checkTLD = 1;
		/* The following is the list of known TLDs that an e-mail address must end with. */
		var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/i;
		/* The following pattern is used to check if the entered e-mail address
		fits the user@domain format.  It also is used to separate the username
		from the domain. */
		var emailPat = /^(.+)@(.+)$/;
		/* The following string represents the pattern for matching all special
		characters.  We don't want to allow special characters in the address. 
		These characters include ( ) < > @ , ; : \ " . [ ] */
		var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		/* The following string represents the range of characters allowed in a 
		username or domainname.  It really states which chars aren't allowed.*/
		var validChars = "\[^\\s" + specialChars + "\]";
		/* The following pattern applies if the "user" is a quoted string (in
		which case, there are no rules about which characters are allowed
		and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		is a legal e-mail address. */
		var quotedUser = "(\"[^\"]*\")";
		/* The following pattern applies for domains that are IP addresses,
		rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		/* The following string represents an atom (basically a series of non-special characters.) */
		var atom = validChars + '+';
		/* The following string represents one word in the typical username.
		For example, in john.doe@somewhere.com, john and doe are words.
		Basically, a word is either an atom or quoted string. */
		var word = "(" + atom + "|" + quotedUser + ")";
		// The following pattern describes the structure of the user
		var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
		/* The following pattern describes the structure of a normal symbolic
		domain, as opposed to ipDomainPat, shown above. */
		var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
		/* Finally, let's start trying to figure out if the supplied address is valid. */
		/* Begin with the coarse pattern to simply break up user@domain into
		different pieces that are easy to analyze. */
		var matchArray = emailStr.match (emailPat);
		if (matchArray == null)
			{
				/* Too many/few @'s or something; basically, this address doesn't
				even fit the general mould of a valid e-mail address. */
				return "Email address seems incorrect (check @ and .'s)";
				//return false;
			}
		var user = matchArray[1];
		var domain = matchArray[2];
		// Start by checking that only basic ASCII characters are in the strings (0-127).
		for (i = 0; i < user.length; i++)
			{
				if (user.charCodeAt (i) > 127)
					{
						return "Ths username contains invalid characters.";
						//return false;
					}
			}
		for (i = 0; i < domain.length; i++)
			{
				if (domain.charCodeAt (i) > 127)
					{
						return "This domain name contains invalid characters.";
						//return false;
					}
			}
		// See if "user" is valid
		if (user.match (userPat) == null)
			{
				// user is not valid
				return "The username doesn't seem to be valid.";
				//return false;
			}
		/* if the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
		var IPArray = domain.match (ipDomainPat);
		if (IPArray != null)
			{
				// this is an IP address
				for (var i = 1; i <= 4; i++)
					{
						if (IPArray[i] > 255)
							{
								return "Destination IP address is invalid!";
								//return false;
							}
					}
				return true;
			}
		// Domain is symbolic name.  Check if it's valid.
		var atomPat = new RegExp("^" + atom + "$");
		var domArr = domain.split (".");
		var len = domArr.length;
		for (i = 0; i < len; i++)
			{
				if (domArr[i].search (atomPat) == -1)
					{
						return "The domain name does not seem to be valid.";
						//return false;
					}
			}
		/* domain name seems valid, but now make sure that it ends in a
		known top-level domain (like com, edu, gov) or a two-letter word,
		representing country (uk, nl), and that there's a hostname preceding 
		the domain or country. */
		if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search (knownDomsPat) == -1)
			{
				return "The e-mail address must end in a well-known domain or two letter " + "country.";
				//return false;
			}
		// Make sure there's a host name preceding the domain.
		if (len < 2)
			{
				return "This e-mail address is missing a hostname!";
				//return false;
			}
		// If we've gotten this far, everything's valid!
		return "valid";
	}
function isValidDate(dateStr, format)
{
  if (format == null)
	  {
		  format = "MDY";
	  }
  format = format.toUpperCase ();
  if (format.length != 3)
	  {
		  format = "MDY";
	  }
  if ((format.indexOf ("M") == -1) || (format.indexOf ("D") == -1) || (format.indexOf ("Y") == -1))
	  {
		  format = "MDY";
	  }
  if (format.substring (0, 1) == "Y")
	  { // If the year is first
		  var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
		  var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	  }
  else if (format.substring (1, 2) == "Y")
	  { // If the year is second
		  var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
		  var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
	  }
  else
	  { // The year must be third
		  var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
		  var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	  }
  // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
  if ((reg1.test (dateStr) == false) && (reg2.test (dateStr) == false))
	  {
		  return false;
	  }
  var parts = dateStr.split (RegExp.$1); // Split into 3 parts based on what the divider was
  // Check to see if the 3 parts end up making a valid date
  if (format.substring (0, 1) == "M")
	  {
		  var mm = parts[0];
	  }
  else if (format.substring (1, 2) == "M")
	  {
		  var mm = parts[1];
	  }
  else
	  {
		  var mm = parts[2];
	  }
  if (format.substring (0, 1) == "D")
	  {
		  var dd = parts[0];
	  }
  else if (format.substring (1, 2) == "D")
	  {
		  var dd = parts[1];
	  }
  else
	  {
		  var dd = parts[2];
	  }
  if (format.substring (0, 1) == "Y")
	  {
		  var yy = parts[0];
	  }
  else if (format.substring (1, 2) == "Y")
	  {
		  var yy = parts[1];
	  }
  else
	  {
		  var yy = parts[2];
	  }
  if (parseFloat (yy) <= 50)
	  {
		  yy = (parseFloat (yy) + 2000).toString ();
	  }
  if (parseFloat (yy) <= 99)
	  {
		  yy = (parseFloat (yy) + 1900).toString ();
	  }
  var dt = new Date(parseFloat (yy), parseFloat (mm) - 1, parseFloat (dd), 0, 0, 0, 0);
  if (parseFloat (dd) != dt.getDate ())
	  {
		  return false;
	  }
  if (parseFloat (mm) - 1 != dt.getMonth ())
	  {
		  return false;
	  }
  return true;
}

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);

}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}
function loginsubmit()
{
	password=document.getElementById("password").value;
	document.getElementById("password").value="";
	document.getElementById("password_hash").value=hex_md5(password);
	document.getElementById("login").submit();
	
}
function handleLoginEnter(field, event)
 {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13)
   {
	   loginsubmit();
   }
   else
   return true;
 }
 function showYouTube(language,autoStart)
 {
	var autoPlay=0;
	if(autoStart) autoPlay=1;
	var youtube="<object width='960' height='565' tabindex='0'><param name='movie' value='http://www.youtube.com/v/PEXZ9Wxq85g&#038;hl="+language+"&#038;fs=0&#038;cc_load_policy=1;autoplay="+autoPlay+"'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/PEXZ9Wxq85g&#038;hl="+language+"&#038;fs=0&#038;cc_load_policy=1;autoplay="+autoPlay+"' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='960' height='565'></embed></object><img src='http://media-cache.videocritter.org/translate.gif' alt='Click here to translate' title='Click here to translate' width=927 height=45 style='margin-left:36px;margin-right:0px;margin-top:0px;margin-bottom:-10px;'>";
	document.getElementById("movie_object").innerHTML=youtube;
	document.getElementById("controls").innerHTML="";
 }
function showVideoCritter(URL)
{
	var autoplay=0;
	var videocritter="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='960' height=564' id='critterPlayer1' name='critterPlayer1' tabindex='0'><param name='movie' value='http://media.videocritter.org/VideoCritterPlayer.swf'><param name='file' value=''><param name='allowfullscreen' value='true'><param name='allowscriptaccess' value='always'><param name='flashvars' value='playlist=over&playlistsize=300&icons=false&dock=true&screencolor=black&plugins=http://media.videocritter.org/VideoCritter.swf&playerready=playerReadyCallback&autoplay="+autoplay+params+"'><embed id='critterPlayer2' name='critterPlayer2' file='' src='http://media.videocritter.org/VideoCritterPlayer.swf' width='960' height='564' allowscriptaccess='always' flashvars='playlist=over&playlistsize=300&icons=false&dock=true&screencolor=black&plugins=http://media.videocritter.org/VideoCritter.swf&playerready=playerReadyCallback&autoplay="+autoplay+params+"' allowfullscreen='true'/></object><span id=\"controls\"><div id='downloadmessage' class='downloadmessage'></div><div id=\"playercontrols\" style=\"text-align:center;\"><a href=\"JavaScript:playVideo()\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('Play','','http://media-cache.videocritter.org/play_over.gif',1)\"><img src=\"http://media-cache.videocritter.org/play.gif\" alt=\"Play\" name=\"Play\" width=\"50\" height=\"50\" border=\"0\"></a><a href=\"JavaScript:pauseVideo()\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('Pause','','http://media-cache.videocritter.org/pause_over.gif',1)\"><img src=\"http://media-cache.videocritter.org/pause.gif\" alt=\"Pause\" name=\"Pause\" width=\"50\" height=\"50\" border=\"0\"></a><a href=\"JavaScript:stopVideo();\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('Stop','','http://media-cache.videocritter.org/stop_over.gif',1)\"><img src=\"http://media-cache.videocritter.org/stop.gif\" alt=\"Stop\" name=\"Stop\" width=\"50\" height=\"50\" border=\"0\"></a><a href=\"JavaScript:muteVideo();\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('Mute','','http://media-cache.videocritter.org/mute_over.gif',1)\"><img src=\"http://media-cache.videocritter.org/mute.gif\" alt=\"Mute\" name=\"Mute\" width=\"50\" height=\"50\" border=\"0\"></a><hr/></div><div id='origurlspace'></div>";
	document.getElementById("movie_object").innerHTML=videocritter;	
	return;
}
function handleEnter(field, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) 
		getVideo();
	else
		return true;
}
