/*

	In theory this will consist of useful Javascript code
	for jujusoft.com
	
	Copyright © 2002 Mark Pursey, All rights reserved
	Some portions are © other people, they should be marked accordingly
	
	You may use this code if you like, as long as you agree to this:
	
	DISCLAIMER:
		Mark Pursey (Jujusoft) will not be held responsible for 
		bad things happening because of the use of this code. 

*/
       
var g_font = new Array("Verdana, sans-serif", "'Times New Roman', serif", "monospace");

function ShowCookies()
{
	var s = "" + document.cookie; s = s.replace(/;\s*/g, "\n"); alert(unescape(s));
}
function PickFont(s2)
{
	if (!s2)
	{
		s2 = g_font[0];
		var s = "" + document.body.style.fontFamily;
		for (i = 0; i < g_font.length; i++)
		{
			if (s == g_font[i])
			{
				s2 = g_font[(i+1)%g_font.length];
				break;
			}
		}
	}
	
	document.body.style.fontFamily = s2;
	setCookie("FontFamily", s2, 28);
}

function ScaleFont(fact)
{
	var s = "" + document.body.style.fontSize;
	if (!s)
		s = "70%";
		
	s = Math.round((0 + s.replace(/[^\d]/g, "")) * fact) + "%";
	document.body.style.fontSize = s;
	
	setCookie("FontSize", s, 28);
}

function ForgetFormatting()
{
	setCookie("FontSize", "", -1);
	setCookie("FontFamily", "", -1);
	
	document.body.style.fontSize = "";
	document.body.style.fontFamily = "";
}

function ResetDefaults() { ForgetFormatting(); }

function PreOnLoad()
{
	if (true)
	{
		var s = getCookie("FontSize");
		if (s)
		{
			document.body.style.fontSize = s;	
		}
		s = getCookie("FontFamily");
		if (s)
		{
			document.body.style.fontFamily = s;	
		}
	}
}
function OnLoad()
{
/*	var loc = "" + document.location;

	var isLocal = loc.search(/^file:/) == 0;
	
	if (isLocal)
	{
		//alert(loc + " is local!");
	}*/
	
	
	for(i = 0; i < document.all.length; i++)
	{
		if(document.all[i].tagName=='FORM')
		{
			RememberFields(document.all[i]);
		}
		/*else if (isLocal && document.all[i].tagName=='A')
		{
			var a_href = document.all[i].href;
			if (a_href)	// only if it's a real link!
			{
				if (a_href.search(/[\\\/]$/) > 0)	// ends with slash?
				{
					document.all[i].href += "index.html";
				}
				else if (a_href.search(/\.\w+$/) < 0)	// no extension?
				{
					document.all[i].href += "/index.html";
				}
			}
		} */
	}		
}	
 /* welcome search engine people */
 
function WriteExtraHeader()
{
	var ref = "" + document.referrer;
	if (ref.length <= 0) 
	{	
		ref = "" + document.location;
	}
	
	if (ref.search(/search\?/i) > 0)
	{
		var begin = ref.search(/\bq=/i);	// search terms begin
		if (begin > 0)
		{
			var terms = ref.substr(begin+2);
			terms = terms.substr(0, terms.search(/\&|$/));
			terms = terms.replace(/\+/g, " ");
			terms = unescape(terms);
			document.write("<tr><td colspan=2 style='font-size:150%;font-style:italic;'");
			document.write("<p>Welcome, seeker of knowledge and new things! There be free demo software available from the <a href='http://www.jujusoft.com/download/index.html?1'>Download</a> page... Ar!</p>");
			document.write("</p></td></tr>");
		}
	}
} 
	// for place holder things
function WarnMissing(href)
{
	var name = href.replace(/^.+\//, "");	// get rid of path
	name = unescape(name);//.replace(/%20/g, " ");
	
	alert(
		"Sorry, but the file \"" + name + "\" is not currently available.\n" + 
		"This is not a broken link, rather a placeholder for a future file."
		);
	return true;
}

	// return true if popup is successfully opened
function PopupImage(href, width, height)
{  
	var parms = "";
	var fixed = false;
	if (!width || !height)
	{
		parms += "resizable,scrollbars=yes,";
	}
	else
	{
		fixed = true;
		parms += "scrollbars=no,";
	}
	if (width)
	{
		parms += "width=" + width + ",";
	}
	if (height)
	{
		parms += "height=" + height + ",";
	}
	parms += 'location=no,toolbar=no,menubar=no,status=no';

	var name = href.replace(/^.+\//, "");	// get rid of path
	name = name.replace(/\.[^\.]+$/, "");	// get rid of extension
	name = name.replace(/%20/g, " ");
   var src = "<HTML>"+
		"<head><title>\"" + name + "\" </title>" + 
		"<base href='" + document.location + "'>";
		
	src += "</head>";
	
	if (fixed)	// this is fixed, so no screwing with it
	{
		src += "<body style='background-repeat:no-repeat; background-color: black; background-position: center; background-image: url(" + href + ");'>";
	}
	else
	{
		//src += "<body style='margin: 0; ;background-color: black; text-align=center;'><IMG src='" + href + "' />";	
		src += "<body style='margin: 0; ;background-color: #444; text-align=center;'>\n<table cellpadding=0 cellspacing=0 style='background-color: black;' border=0 align=center height=100%><tr><td><IMG src='" + href + "' /></td></tr></table>";	
	}	
	src += "</body></html>";
	
   popup = window.open("", "_blank", parms);
   if (popup)
   {
	   popup.document.write(src);
	   popup.document.close();
	   return true;
	}
	return false;
}
	
function PopupWindow(href, width, height, name)
{
	var parms = "";
	if (width)
	{
		parms += "width=" + width + ",";
	}
	if (height)
	{
		parms += "height=" + height + ",";
	}
	
	if (!width || !height)
	{	
		parms += 'scrollbars=yes,location=no,toolbar=no,menubar=no,status=no,resizable';
	}
	else	// if width and height are given, assume they are correct
	{	
		parms += 'scrollbars=no,location=no,toolbar=no,menubar=no,status=no';
	}
		
	if (!name)
		name = "popup";
		
	popup = window.open(href, name, parms);
	if (popup)
	{
		return true;
	}
	
	return false;
}

/*
	function IsValidEmail(address)
	
	Do a regexp match, assumes a standard top level domain (there 
	are lots now!) as well as an optional 2 letter country code at 
	the end
*/

function IsValidEmail(address)
{
	// TLD's are in basic order common available / reserved / sponsored
	return address.search(/^[^@\s]+@[\w\.\-]+.[a-z]+$/i) == 0;

	//old simpler one: return address.search(/^[^@\s]+@[\w\.\-]+\.[a-z]{2,4}$/i) == 0;
}


/*
	function ValidateForm(form, debug)
	
	Validates a form based on entries in form.required, which is the
	field formmail uses to decide what's compulsory. Also validates 
	the email address (syntax only) if email is one of the required 
	fields. 
	
	If non-zero debug value is passed then data will not be posted,
	rather an alert will pop up showing what data *would* be posted
*/
function ValidateForm(p_form)
{
	var debug = false;
	if (!p_form || !p_form.required)// == null)
		return false;

	var required = p_form.required.value;		
	if (!required || required.length == 0)	// no required fields, just post the p_form then
	{
		return true;
	}
	
	if (p_form.email && p_form.email.value && p_form.email.value == "test@test.com")	
		debug = true;
		
		// required is a comma separated list of required fields
	var list = required.split(",");
	
	var missing = new Array();
	for (var i = 0; i < list.length; i++)
	{
		if (p_form[list[i]] && p_form[list[i]].value != null)	// second check is necessary because of checkbox objects apparently not having value members
		{
			var val = p_form[list[i]].value;
			val = val.replace(/^\s+|\s*$/ig, "");	// trim unwanted spaces
			if (val == null || val.length == 0)
			{
				//alert(list[i]);
				missing[missing.length] = list[i];
			}
			else if (list[i] == "email")
			{
				val = val.replace(/^\s+|\.*\s*$/ig, "");	// clean out unwanted spaces
	            p_form[list[i]].value = val;
				if (!IsValidEmail(val))
				{
					alert("Malformed email address:  " + val + "\n\nPlease enter an address of the form:  user-name@host-name.com"
					);
					p_form[list[i]].focus();
					return false;
				}
			}
		}
	}
	
	//alert(missing);
	if (missing.length > 0)
	{
		var message = "The following entries must be completed \nbefore this form can be posted:\n";
		for (var i = 0; i < missing.length; i++)
		{
			if (missing[i] == "email")
				message += "\n    •  Email address";
			else
				message += "\n    •  " + missing[i];
		}
		alert(message);
		p_form[missing[0]].focus();
		return false;
	}

	RememberFields(p_form, true);
	
	if (debug)
	{
		var str = "Summary of form data:\n";
		for (var i = 0; i < p_form.length; i++)
		{
			if (p_form[i].name && p_form[i].value)
				str += "\n  • " + p_form[i].name + ": " + p_form[i].value;
		}
		alert(str);
		return false;
	}
	
	return true;
}    

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

/* Call function as setCookie("cookiename" , cookievalue, lifetime, cookiepath)
with the lifetime required in days, -1 to delete a cookie or zero
for a temporary cookie. The Cookie Path is optional.*/

function setCookie(cookie_name, cookie_value, cookie_life, cookie_path) 
{
  cookie_path = "/";
  var today = new Date()
  var expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)
  //if (cookie_value != null && cookie_value != "")
  {
		var cookie_string = "Var." + cookie_name + "=" + escape(cookie_value)
		if(cookie_life)
		{ 
			cookie_string += "; expires=" + expiry.toGMTString()
		}
		if(cookie_path)
		{ 
			cookie_string += "; path=" + cookie_path
		}
		document.cookie = cookie_string
	}
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com -->

/* Call function as getCookie("cookiename") It returns the value of a cookie
if set or null. Beware of potential ambiguities in names of cookies -
getCookie is simple and will match the end of a string so xyname 
will also be matched by yname and ame. */
 
function getCookie(name) 
{
  var index = document.cookie.indexOf("Var." + name + "=")
  if (index == -1) { return null}
  index = document.cookie.indexOf("=", index) + 1
  var end_string = document.cookie.indexOf(";", index)
  if (end_string == -1) { end_string = document.cookie.length }
  return unescape(document.cookie.substring(index, end_string))
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com -->

function RememberFields(form, store) 
{
	if (!form || !form._remember)// == null)
		return false;
		
	var remember = form._remember.value;		
	if (!remember || remember.length == 0)	// no required fields, just post the form then
	{
		return true;
	}
	
	
		// required is a comma separated list of required fields
	var list = remember.split(",");
	var message = "";
	for (var i = 0; i < list.length; i++)
	{  
		if (form[list[i]])	// make sure this field exists first!
		{
			var cookieName = list[i];
			if (store)
			{  
				var value = form[list[i]].value;
				setCookie(cookieName, value, 28);
				//message += "\n    • " + cookieName + " = " + value;
			}
			else
			{              
				var value = getCookie(cookieName);
				if (value)
				{
					//message += "\n    • " + cookieName + " = " + value;
					form[list[i]].value = value;
				}
			}
		}
	}	
	if (message.length > 0)
	{
		if (store)
			alert("Storing Cookies:\n" + message);
		else
			alert("Loading Cookies:\n" + message);
	}
}	
