// common javascript code for Visonair.tv, Copyright (C) 2005

function validstring(s)
{	var txt = s+"";  // ensure a string
	var s2 = "";
	for (var a=0; a<txt.length; a++) if (txt.charAt(a)!=" ") s2+=txt.charAt(a);
	return (s2.length>1);
}

// checks if pc is a valid email address
function validemail(pc)
{	var txt=pc+" ";
	var a = txt.indexOf("@");
	if (a<1) return false;
	txt = txt.substr(a+1,(txt.length-a)-1);
	a = txt.indexOf(".");
	if (a<1) return false;
	if (a>=txt.length-2) return false;
	return true;
}


// Validate the form
function ValidateRegisterForm(form)
{	var msg = "";
	if (!validstring(form.forename.value)) msg+="- Your Forename\r\n";
	if (!validstring(form.surname.value)) msg+="- Your Surname\r\n";
	if (!validstring(form.add1.value)) msg+="- Your Address\r\n";
	if (!validstring(form.poszip.value)) msg+="- Your Post/Zip Code\r\n";
	if (!validemail(form.email.value)) msg+="- Your Email Address\r\n";
	if (!validstring(form.code.value)) msg+="- The Registration Code is Incorrect\r\n";

	if (msg!="")
	{	alert("The registration form cannot be sent because you have not\r\nentered valid information in the following field(s):\r\n\r\n"+msg+"\r\n\r\nPlease fill in these field(s) correctly and try again...");
		return false;
	} else return true;
}

// Validate the rate form
function testRate(form)
{
	if (!validstring(form.code.value)) 
	{	alert("You have not entered any text in the access code box\r\n\r\nPlease fill in the access code box and and try again...");
		return false;
	} else return true;
}

function ValidateContactForm(form)
{	var msg = "";
	if (!validstring(form.yourname.value)) msg+="- Your Name\r\n";
	if (!validemail(form.email.value)) msg+="- Your Email Address\r\n";
	if (!validstring(form.located.value)) msg+="- How You Located Visonair\r\n";

	if (msg!="")
	{	alert("The contact form cannot be sent because you have not\r\nentered valid information in the following field(s):\r\n\r\n"+msg+"\r\n\r\nPlease fill in these field(s) correctly and try again...");
		return false;
	} else return true;
}


function ValidateProfileForm(form)
{	var msg = "";
	if (!validstring(form.yourname.value)) msg+="- Your Name\r\n";
	if (!validemail(form.email.value)) msg+="- Your Email Address\r\n";
	if (!validstring(form.message.value)) msg+="- Your Message\r\n";

	if (msg!="")
	{	alert("The contact form cannot be sent because you have not\r\nentered valid information in the following field(s):\r\n\r\n"+msg+"\r\n\r\nPlease fill in these field(s) correctly and try again...");
		return false;
	} else return true;
}


// Validate GET PASSWORD form
function ValidatePasswordForm(form)
{	if (!validemail(form.email.value)) 
	{	alert("Error: The email address you have entered is invalid.");
		return false;
	} else return true;
}



// Add a bookmark
function AddBookmark() 
{
	if (navigator.appName=="Microsoft Internet Explorer")
	{
		window.external.AddFavorite(document.location.href,document.title);	
	} else
	{
		alert('Press Ctrl+D or Ctrl+T to bookmark this page (depending on your browser)');
	}
}

function formatWidth(i,l)
{	i = i+"";
	while (i.length<l) i='0'+i;
	return i;
}

// returns current date
function formatDate()
{
	var d = new Date();
	return formatWidth(d.getDate(),2)+'.'+formatWidth(d.getMonth()+1,2)+'.'+formatWidth(d.getFullYear(),4);	
}

// returns current time
function formatTime()
{
	var d = new Date();
	return formatWidth(d.getHours(),2)+'.'+formatWidth(d.getMinutes(),2);
}

function RefreshTimezone()
{	var d = new Date();
	var expire = new Date();
	expire.setTime(d.getTime() + 3600000*24*32);
	// Force a cookie with the name time zone of this machine	
	document.cookie = 'timezone='+escape(d.getTimezoneOffset())+';expires='+expire.toGMTString();	
}

// Ouput time and setup timezone cookie
function OutputDateAndPrepareTimezone()
{	RefreshTimezone();	
	document.writeln(formatTime()+'&nbsp;&nbsp;&nbsp;'+formatDate());
}


// Tell a friend form test
function tellfriendcheck()
{	var msg = "";
	if (!validstring(document.getElementById("tellafriendname").value)) msg+="- Your Friends Name\r\n";
	if (!validemail(document.getElementById("tellafriendaddress").value)) msg+="- Your Friends Email Address\r\n";

	if (msg!="")
	{	alert("We cannot tell your friend about Visonair because you have not entered the following information:\r\n\r\n"+msg);
		return false;
	} else return true;
}


// Watch Online popup
function WatchOnline(channel)
{
	wind = window.open("watchonline.php?channel="+channel,"VisonairVideoWindow","channelmode=0,directories=0,scrollbars=0,height=500,width=640,location=0,menubar=0,resizable=0,status=0,toolbar=0");
	if (!wind) alert("Unable to open the Online Player.  Please disable popup-blockers and try again.");
}
