// functions.js
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

// 2nd Nov 2004 this version

var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
	}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
		}
	return null;
	}

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 SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
	}

//----------------------------------
// cookies for Ians custom functions

// THIS IS THE SCRIPT THAT CONTROLS THE DATE ON THE TOP OF THE PAGE
function addDate() {

   monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
   dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

   myDate  = new Date()

   var year = myDate.getYear()
   if (year < 1000) year += 1900

//   document.write("<h2>"+dayNames[myDate.getDay()] + "  " + monthNames[myDate.getMonth()] + "  " + myDate.getDate() + ", " + year + "</h2>")
   document.write(dayNames[myDate.getDay()] + "  " + monthNames[myDate.getMonth()] + "  " + myDate.getDate() + ", " + year)
}

// This will customise the greeting depending on the time of day
function greeting()  {
	Now = new Date()
	Hour = Now.getHours()
	if(Hour < 12) 
	   msg = "Good Morning!" 
	else
	   if(Hour < 18) 
	      msg = "Good Afternoon!" 
	   else
	      msg = "Good Evening!"
	// return( "<h1>" + msg )
								  
	// This script displays the number of times accessed
	if (GetCookie('hit_count') == null) {
		document.write('<h2>' + msg + ' Welcome to the British Amateur Television Club (BATC) web site.</h2>' + 
			'This is your first visit.');
		SetCookie('hit_count', '1', expiry);
		}
	else {
		var getHits = GetCookie('hit_count');
		document.write('<h2>' + msg + ' Welcome back to the British Amateur Television Club (BATC) web site.</h2>' + 
			'You have viewed this page ' + getHits + ' times before.');
		getHits = parseInt(getHits) + 1;
		SetCookie('hit_count', '' + getHits + '', expiry);
		}
}

// This will call the 'add to favourites' dialog

function favorites(url,title) {
  if (document.all)
    window.external.AddFavorite(url,title)
}

// This will display a 'countdown' display
function countDown() {

   // CHANGE DATE TO BE WHENEVER YOUR PRODUCT/DATE HAPPENS
   // USE "Month dd, yyyy" FORMAT, AS BELOW

   var future = new Date("May 07, 2005")

   var today = new Date()
   var days = Math.ceil((future.getTime() / 86400000) - (today.getTime() / 86400000))

   // CHANGE THE TEXT IN QUOTES FOR EACH document.write TO
   // WHATEVER MESSAGE YOU NEED.

   if (days > 1) {
      document.write("Only " + days + " days to go!")
   }
   else if (days == 1) {
      document.write(days + " tomorrow!")
   }
   else {
      document.write("TODAY!")
   }
}

// this will display a warning dialog
function warning() {
	alert("This issue is not yet available for download!")
}	

// Opens a new centred window and displays the passed url
function NewWindow(mypage) {
var w = 600;
var h = 400;
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+', directories=no, toolbar=no, menubar=no, resizable=yes, scrollbars=yes'
win = window.open(mypage, "Cover_Viewer", winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
// open a window
function openwindow(address)
{
  window.open( address, '', 'status=yes, height=400, width=600, scrollbars=yes, resizable=yes' )
}
// Original:  Martin Webb (martin@irt.org) 
// Stops Windows machines from right clicking 
function right(e) {
if (navigator.appName == 'Netscape' && 
(e.which == 3 || e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' && 
(event.button == 2 || event.button == 3)) {
alert("Sorry, you do not have permission to right click.");
return false;
}
return true;
}

// check to see if the passed field is empty
function emptyField(textObj)
{
  if (textObj.value.length == 0) return true;

  for (var i=0; i<textObj.value.length; ++i)
    { 
       var ch=textObj.value.charAt(i);
       if (ch != ' ' && ch != '\t') return false; 
    }

  return true;
}

// validate the required fields have data in them
function ValidateFields(formObj)
{ 
  if (emptyField(formObj.Name ))
  {
    alert("The 'Name' field cannot be empty.");
    formObj.name.focus();
    return;
  }

 if (emptyField(formObj.Email ))
  {
    alert("The 'Email' field cannot be empty.");
    formObj.Email.focus();
    return;
  }
  
 if (emptyField(formObj.Comments ))
  {
    alert("The 'Comments' field cannot be empty.");
    formObj.Comments.focus();
    return;
  }

    // all correct, so send the form
    alert("Information validated. Thank you for your information request.\n\nYou will return to our main index page.\n");
    formObj.submit();
}

// redirect to the home page
function redirect()
{
  window.location.replace("index.html");
}

// End of cookies.js script file

