/*
Design, maintenance, and copyright (c)
     Capitalism's Gravediggers (r)
     www.Capitalisms-Gravediggers.com
     www.CapitalismsGravediggers.com
*/

//-----------------------------------------------------------------------------
// Clear contact form.
function clearContactForm(form)
{	
	form.contact_clear.value = "erase";
	form.submit();
	return;
}	// clearContactForm

//-----------------------------------------------------------------------------
// Change the class of an element.
function changeStyleClass(elementID, newClass)
{
	document.getElementById(elementID).className = newClass;
}	// changeStyleClass

//-----------------------------------------------------------------------------
// Pop up a box showing more information.
function popup(title, content, codeDir, css)
{
	var popupWindow = open("","popup","toolbar=no,location=no,directories=no," +
		"status=no,menubar=no,scrollbars=yes,resizeable=yes,width=400,height=200");

	var html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" +
							"\"http://www.w3.org/TR/html4/loose.dtd\">" +
				"<html><head><title>"+title+"</title><script " +
	 			"type='text/javascript'></script>" +
				"<link rel='stylesheet' href='"+codeDir+"/socialism.style."+css+".css' " +
     		"type='text/css' media='screen'></head>" +
				"<body style='background: lightyellow; padding: 5px;' " +
				"onload=\"self.focus(); setTimeout('self.close()',60000)\">";	
	var bottom = "<hr><center>" +
				"<button type='button' name='closewindow' " +
				"onClick='window.close()'>Close</button></center><br></body></html>";

	// Popups need to contain links, but the call to this function is a link,
	// and it is invalid to construct nested anchors:
	//			<a href="" <a href=""></a>></a>
	// The popup structure does not actually have nested anchors, but it looks
	// as if they do, and therefore robots can have problems.
	// So a little jiggery pokery resolves the problem.
	// The PHP popup function converts "<a href=" to "<a_href=" in the popup content.
	// Here, we undo that.
	var contentAH = content.replace(/<a_href=/g, "<a href=");
	
	// Apostrophe and Quote Conversion.
	// Quoted quotes are a pain. Although it may be possible to appropriately
	// escape them, it is easier to use meta-strings to represent them.
	// "#!#" is converted to a single quote.
	// "#!!#" is converted to a double quote.
	var contentAC = contentAH.replace(/#!#/g, "'");
	var contentQC = contentAC.replace(/#!!#/g, "\"");
	
	popupWindow.document.open('text/html'); 
	popupWindow.document.write(html);
	popupWindow.document.write(contentQC);
	popupWindow.document.write(bottom);
	popupWindow.document.close();
	
	return;
	
}	// popup

//-----------------------------------------------------------------------------
// Display an image from the images table in a new window.
function displayImage(serverName, image, codeDir)
{
	var dImage = new Image();
	dImage.src = codeDir+'/getimage.php?server='+serverName+'&imageID='+image;

	dImage.onLoad = displayTheImage(serverName, image, codeDir, dImage);
	
}	// displayImage

function displayTheImage(serverName, image, codeDir, dImage)
{
// Opera, Netscape do not wait until image is loaded to execute the onLoad.
for (var i = 1; i <= 100000; ++i)
{
	var width = dImage.width + 50;
	var height = dImage.height + 50;
	if (width > 50 && height > 50) break;
}
	
	var editWin = window.open("","imageWindow","toolbar=no,location=no,directories=no," +
			"menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height);

	var html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" +
							"\"http://www.w3.org/TR/html4/loose.dtd\">" +
				"<html><head><title>"+image+"</title><script " +
	 			"type='text/javascript' src='"+codeDir+"/socialism.js'></script>" +
//				"<link rel='stylesheet' href='"+codeDir+"/socialism.style."+css+".css' " +
//      	"type='text/css' media='screen'>" +
				"</head>" +
				"<body style='background: lightyellow; padding: 25px; text-align: center;'>" +
 				"<img name='"+image+"' src='"+codeDir+"/getimage.php?server="+serverName +
				"&imageID="+image+"' onload='resize(\""+image+"\")'>" +
				"</body></html>";

	editWin.document.open("text/html"); 
	editWin.document.write(html);
	editWin.document.close();
}

//-----------------------------------------------------------------------------
// Resize an image window.
function resize(image)
{
	var width = document.images[0].width + 90;
	var height = document.images[0].height + 120;
	self.resizeTo(width,height);
	return;
}	// resize

function escapeVal(textarea,replaceWith)
{
	// Windows.
	textarea=textarea.replace(/%0D%0A/gi,replaceWith);
	// Unix.
	textarea=textarea.replace(/%0A/gi,replaceWith);
	// Macintosh.
	textarea=textarea.replace(/%0D/gi,replaceWith);

	textarea=unescape(textarea); // unescape all other encoded characters
	return textarea;
}	// escapeVal

//-----------------------------------------------------------------------------
// Validate the contact form data before sending to server.
function validateContactForm(form)
{
	// Clear the clear field.
	form.contact_clear.value = '';
	
	// Form data to check.
	var cName = form.contact_name.value;
	var cEmail = form.contact_email.value;
	var cComments = form.contact_comments.value;
	
	form.contact_validated.value = 'no';	// Form has not validated.
	
	var eMsg = '';
	if (isWhitespace(cName)) eMsg = eMsg + 'Please enter your name.\n';
	if (isWhitespace(cEmail)) eMsg = eMsg + 'Please enter your e-mail address.\n';
	if (isWhitespace(cComments)) eMsg = eMsg + 'Please enter your comments.\n';

	if (eMsg.length != 0)
		{
			alert(eMsg);
			return false;
		}
				
	var eMailAddress = cEmail.value;
	// invalidEmail and validEmail are JavaScript regular expressions, not strings.
	// var invalidEmail = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var validEmail = /^.+\@[^-.][a-zA-Z0-9\-\.]+[^-.]\.(?!([0-9]*$))[a-zA-Z0-9]{2,6}$|\[[0-9]{1,3}(\.[0-9]{1,3}){3}\]$/;
	
	if (! validEmail.test(cEmail))
		{
			alert('The e-mail address you specified is not valid.');
			return false;
		}
		
	// percent2many is a JavaScript regular expression, not a string.
	var percentMany = /([^%]*%){25,}/;
	
	// If too many percent signs (actual garbage messages seen), don't send.
	if (percentMany.test(cComments))
		{
			alert('Invalid comments.');
			return false;
		}
		
	form.contact_validated.value = 'yes';
	
	return true;
	
}	// validateContactForm

//-----------------------------------------------------------------------------
// Return true if a string is empty.
function isEmpty(string2check)
{
	return ((string2check.length == 0) || (string2check == null));
}

//-----------------------------------------------------------------------------
// Return true if a stringg is whitespace or empty.
function isWhitespace(string2check)
{
	var whitespace = " \t\n\r";	// whitespace characters	
  
  // Is string empty?
  if (isEmpty(string2check)) return true;
	
  // Search through string for non-whitespace.
  for (var i = 0; i < string2check.length; i++)
		if (whitespace.indexOf(string2check.charAt(i)) == -1) return false;
  
  // All characters are whitespace.
  return true;
}