// video_functions.js
//
// Set of functions for performing video stuff.
//		showVideo()
//		showIcon()
//		displayBasicVideoIcon()


// showVideo
//
// Rewrite the Video Player in the "video_player" <div>
// Using the specified ref.
// Defaults to "XmXwq47UTF8" if none specified.
function showVideo(ref)
{
	// Locate the video_player <div>
	var playerDiv = document.getElementById("video_player");
	var html = "";
	
	// Nothing specified.
	if (ref != null)
	{
		html = "<object width=445 height=364>";
		html +=		"<param name=movie value=\"http://www.youtube.com/v/" + ref + "&autoplay=1&hl=en&fs=1&rel=0&border=1\"></param>";
		html +=		"<param name=allowFullScreen value=true></param>";
		html +=		"<param name=allowscriptaccess value=always></param>";
		html +=		"<embed src=\"http://www.youtube.com/v/" + ref + "&autoplay=1&hl=en&fs=1&rel=0&border=1\"";
		html +=			" type=application/x-shockwave-flash";
		html +=			" allowscriptaccess=always";
		html +=			" allowfullscreen=true";
		html +=			" width=445 height=364>";
		html +=		"</embed>";
		html += "</object>";
	}
	else
	{
		html = "&lt;<i>Video Here</i>&gt;";
	}
	
	// If we found the Player object, display the new Flash player.
	if (playerDiv != null)
		playerDiv.innerHTML = html;
}


// showIcon
//
// Rewrite the Video Icon in the "video_icon" <div>
// Using the specified ref.
// Optional id.
function showIcon(ref, id)
{
	// Locate the video_player <div>
	var iconDiv = document.getElementById("video_icon");
	var html = "";

	var idStr = "";
	if (id != null)
		idStr = " id="+id;
	
	// Nothing specified.
	if (ref != null)
	{
		html = "<img "+idStr+" src='http://i1.ytimg.com/vi/"+ ref +"/default.jpg'>";
	}
	else
	{
		html = "&lt;<i>Video Icon Here</i>&gt;";
	}

	// If we found the Player object, display the new Flash player.
	if (iconDiv != null)
		iconDiv.innerHTML = html;
}



// Just the image, and URL to do a onClick for showVideo(ref)
function displayBasicVideoIcon(ref)
{
	var imgHTML = "<img src='http://i1.ytimg.com/vi/" + ref + "/default.jpg' border=0>";
	var linkHTML = "<a href=# onClick=showVideo('"+ref+"')>" + imgHTML + "</a>";
	var html = linkHTML + "<br><br>";

	return html;
}



// Validate the email address.
// addr is the email address
// man is 'true' if manditory, 'false' if optional
// db is 'true' if you want alert message feedback.
function validateEmail(addr,man,db) 
{
	if (addr == '' && man) 
	{
   		if (db) alert('email address is mandatory');
		return false;
	}
	
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) 
	{
   		if (addr.indexOf(invalidChars.charAt(i),0) > -1) 
   		{
      		if (db) alert('email address contains invalid characters');
      		return false;
   		}
	}
	
	for (i=0; i<addr.length; i++) 
	{
   		if (addr.charCodeAt(i)>127) 
   		{
      		if (db) alert("email address contains non ascii characters.");
      		return false;
		}
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) 
	{
	   if (db) alert('email address must contain an @');
	   return false;
	}
	
	if (atPos == 0) 
	{
	   if (db) alert('email address must not start with @');
	   return false;
	}
	
	if (addr.indexOf('@', atPos + 1) > - 1) 
	{
	   if (db) alert('email address must contain only one @');
	   return false;
	}
	
	if (addr.indexOf('.', atPos) == -1) 
	{
	   if (db) alert('email address must contain a period in the domain name');
	   return false;
	}
	
	if (addr.indexOf('@.',0) != -1) 
	{
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	
	if (addr.indexOf('.@',0) != -1)
	{
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	
	if (addr.indexOf('..',0) != -1) 
	{
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && 
		suffix != 'com' && 
		suffix != 'net' && 
		suffix != 'org' && 
		suffix != 'edu' && 
		suffix != 'int' && 
		suffix != 'mil' && 
		suffix != 'gov' && 
		suffix != 'arpa' && 
		suffix != 'biz' && 
		suffix != 'aero' && 
		suffix != 'name' && 
		suffix != 'coop' && 
		suffix != 'info' && 
		suffix != 'pro' && 
		suffix != 'museum') 
	{
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
	
	// All OK!
	return true;
}