
function dw(t) { document.write(t) }

function libSure(strPrompt) { if (strPrompt.length < 1) { strPrompt = "Are you sure?" } return confirm(strPrompt) }

function libReallySure() { return confirm("Are you REALLY sure?") }

function libMakeWindow(strUrl, strChrome)
	{
	var objWindow = window.open(strUrl, "", strChrome);
	if (navigator.appVersion.charAt(0) == "2" && navigator.appName == "Netscape")
		{ objWindow = window.open(strUrl, "", strChrome) }
	}

function libAllowChars(strValue, strChars, blnCaseSense)
	{
	strResult = "";
	if (blnCaseSense)
		{
		for (var i = 0; i < strValue.length; i++)
			{
			strChar = strValue.substr(i, 1);
			if (strChars.indexOf(strChar) > -1) { strResult += strChar; }
			}
		}
	else
		{
		strChars = strChars.toUpperCase();
		for (var i = 0; i < strValue.length; i++)
			{
			strChar = strValue.substr(i, 1);
			if (strChars.indexOf(strChar.toUpperCase()) > -1) { strResult += strChar; }
			}
		}
	return strResult;
	}

function libDenyChars(strValue, strChars, blnCaseSense)
	{
	strResult = "";
	if (blnCaseSense)
		{
		for (var i = 0; i < strValue.length; i++)
			{
			strChar = strValue.substr(i, 1);
			if (strChars.indexOf(strChar) == -1) { strResult += strChar; }
			}
		}
	else
		{
		strChars = strChars.toUpperCase();
		for (var i = 0; i < strValue.length; i++)
			{
			strChar = strValue.substr(i, 1);
			if (strChars.indexOf(strChar.toUpperCase()) == -1) { strResult += strChar; }
			}
		}
	return strResult;
	}

function libEmailCheck(emailStr)
	{
	var emailPat =/^(.+)@(.+)$/											// check user@domain pattern
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"					// check banned special chars
	var validChars = "\[^\\s" + specialChars + "\]"						// check banned normal chars
	var quotedUser = "(\"[^\"]*\")"										// check if username quoted
	var ipDomainPat =/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/	// check ip format
	var atom = validChars + '+'											// series of non special chars
	var word = "(" + atom + "|" + quotedUser + ")"						// a word
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$")		// structure of username
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$")		// structure of domain
	var matchArray = emailStr.match(emailPat)
	if (matchArray == null) { return false }
	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) { return false }
	var IPArray = domain.match(ipDomainPat)
	if (IPArray != null)
		{
		for (var i = 1; i <= 4; i++) { if (IPArray[i] > 255) { return false } }
		return true
		}
	var domainArray = domain.match(domainPat);
	if (domainArray == null) { return false }
	var atomPat = new RegExp(atom,"g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	// test for length of final part of domain - now need to allow up to 6 chars (.museum)
	if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 6) { return false }
	if (len < 2) { return false }
	return true;
	}

function isNumeric(val) { return (parseFloat(val, 10) == (val * 1)); }

function dopopupbase(p, w, h)
	{
	var u = "popup_base.php?ph_id=" + p;
	var x = parseInt((screen.availWidth / 2) - ((w + 30) / 2));
	var y = parseInt((screen.availHeight / 2) - ((h + 30) / 2));
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	//var s = "width=" + (w + 30) + ",height=" + (h + 60) + ",x=" + x + ",y=" + y + ",left=" + x + ",top=" + y;
	var s = "width=" + (700) + ",height=" + (600) + ",x=" + x + ",y=0,left=" + x + ",top=0,scrollbars=yes";
	libMakeWindow(u, s)
	}

function dopopupcomments(p)
	{
	var u = "popup_comments.php?ph_id=" + p;
	var w = 360;
	var h = 240;
	var x = parseInt((screen.availWidth / 2) - ((w + 30) / 2));
	var y = parseInt((screen.availHeight / 2) - ((h + 30) / 2));
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	var s = "width=" + (w + 30) + ",height=" + (h + 60) + ",x=" + x + ",y=" + y + ",left=" + x + ",top=" + y;
	libMakeWindow(u, s)
	}

function dopopupsponsor(c)
	{
	var u = "popup_sponsor.php?cp_id=" + c;
	var w = 360;
	var h = 240;
	var x = parseInt((screen.availWidth / 2) - ((w + 30) / 2));
	var y = parseInt((screen.availHeight / 2) - ((h + 30) / 2));
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	var s = "width=" + (w + 30) + ",height=" + (h + 60) + ",x=" + x + ",y=" + y + ",left=" + x + ",top=" + y;
	libMakeWindow(u, s)
	}

function checkLogin(form)
	{
	form.us_username.value = libAllowChars(form.us_username.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	form.us_password.value = libAllowChars(form.us_password.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	if (form.us_username.value.length < 1) { alert("You must enter your username."); return false }
	if (form.us_password.value.length < 1) { alert("You must enter your password."); return false }
	return true
	}

function checkAdminCompsEdit(form)
	{
	if (form.cp_name) { form.cp_name.value = libAllowChars(form.cp_name.value, "abcdefghijklmnopqrstuvwxyz0123456789", false); }
	form.cp_title.value = libDenyChars(form.cp_title.value, "\\<>\"", false);
	form.cp_datestart.value = libAllowChars(form.cp_datestart.value, "0123456789", false);
	form.cp_dateend.value = libAllowChars(form.cp_dateend.value, "0123456789", false);
	if (form.cp_name) { if (form.cp_name.value.length < 4) { alert("Name must be at least 4 characters (letters and numbers only)"); return false } }
	if (form.cp_title.value.length < 4) { alert("Title must be at least 4 characters (no carets or quotes)"); return false }
	if (form.cp_datestart.value.length != 8) { alert("Start date is not in correct format (should be DDMMYYYY)"); return false }
	if (form.cp_dateend.value.length != 8) { alert("End date is not in correct format (should be DDMMYYYY)"); return false }
	return true
	}

function checkAdminCompsAdd(form)
	{
	form.cp_name.value = libAllowChars(form.cp_name.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	if (form.cp_name.value.length < 4) { alert("Name must be at least 4 characters (letters and numbers only)"); return false }
	return true
	}

function checkAdminUsersEdit(form)
	{
	form.us_username.value = libAllowChars(form.us_username.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	form.us_password.value = libAllowChars(form.us_password.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	form.us_name.value = libDenyChars(form.us_name.value, "<>\"", false);
	form.us_email.value = libAllowChars(form.us_email.value, "abcdefghijklmnopqrstuvwxyz0123456789@_-.", false);
	form.us_contacttel.value = libAllowChars(form.us_contacttel.value, "etx0123456789-. ()+", false);
	if (form.us_username.value.length < 4) { alert("Username must be at least 4 characters (letters and numbers only, no spaces)"); return false }
	if (form.us_password.value.length < 4) { alert("Password must be at least 4 characters (letters and numbers only, no spaces)"); return false }
	if (form.us_name.value.length < 1) { alert("Name must be entered (no carets or quotes)"); return false }
	if (!libEmailCheck(form.us_email.value)) { alert("Email is not a valid format"); return false }
	return true
	}

function checkAdminPhotosEdit(form)
	{
	form.ph_caption.value = libDenyChars(form.ph_caption.value, "<>\";", false);
	form.ph_comments.value = libDenyChars(form.ph_comments.value, "<>\";", false);
	if (form.ph_caption.value.length < 2) { alert("Caption must be at least 2 characters (no carets, quotes or semicolons)"); return false }
	if (form.ph_compid.value.length != 32) { alert("You must select a competition"); return false }
	if (form.ph_userid.value.length != 32) { alert("You must select a user"); return false }
	return true
	}

function checkAdminVotesEdit(form)
	{
	if (form.vt_compid.value.length != 32) { alert("You must select a competition"); return false }
	if (form.vt_photoid.value.length != 32) { alert("You must select a photo"); return false }
	return true
	}

function checkAdminLinksEdit(form)
	{
	form.lk_url.value = libDenyChars(form.lk_url.value, "<>\";", false);
	form.lk_title.value = libDenyChars(form.lk_title.value, "<>\";", false);
	form.lk_info.value = libDenyChars(form.lk_info.value, "<>\";", false);
	if (form.lk_compid.value.length != 32) { alert("You must select a competition"); return false }
	if (form.lk_userid.value.length != 32) { alert("You must select a user"); return false }
	if (form.lk_url.value.length < 4) { alert("You must enter a URL"); return false }
	if (form.lk_title.value.length < 2) { alert("You must enter a title"); return false }
	return true
	}

function checkRegister(form)
	{
	form.us_name.value = libDenyChars(form.us_name.value, "<>\";", false);
	form.us_email.value = libAllowChars(form.us_email.value, "abcdefghijklmnopqrstuvwxyz0123456789@_-.", false);
	form.us_contacttel.value = libAllowChars(form.us_contacttel.value, "etx0123456789-. ()[]+", false);
	if (form.us_username.value != libAllowChars(form.us_username.value, "abcdefghijklmnopqrstuvwxyz0123456789", false)) { alert("Your username may contain only letters and numbers."); return false }
	if (form.us_password.value != libAllowChars(form.us_password.value, "abcdefghijklmnopqrstuvwxyz0123456789", false)) { alert("Your password may contain only letters and numbers."); return false }
	if (form.us_username.value.length < 4) { alert("Username must be at least 4 characters (letters and/or numbers only, no spaces)"); return false }
	if (form.us_password.value.length < 4) { alert("Password must be at least 4 characters (letters and/or numbers only, no spaces)"); return false }
	if (form.us_password.value != form.us_password1.value) { alert("Passwords do not match"); return false }
	if (form.us_name.value.length < 3) { alert("Name must be entered"); return false }
	if (!libEmailCheck(form.us_email.value)) { alert("Email is not a valid format"); return false }
	if (form.us_contacttel.value.length < 8) { alert("Contact Tel must be entered"); return false }
	if (!form.us_acceptedterms.checked) { alert("You must read and accept the Terms Of Use\nin order to register at this site"); return false }
	return true
	}

function checkUserProfileEdit(form)
	{
	form.us_username.value = libAllowChars(form.us_username.value, "abcdefghijklmnopqrstuvwxyz0123456789", false);
	form.us_name.value = libDenyChars(form.us_name.value, "<>\";", false);
	form.us_email.value = libAllowChars(form.us_email.value, "abcdefghijklmnopqrstuvwxyz0123456789@_-.", false);
	form.us_contacttel.value = libAllowChars(form.us_contacttel.value, "etx0123456789-. ()[]+", false);
	if (form.us_username.value.length < 4) { alert("Username must be at least 4 characters (letters and/or numbers only, no spaces)"); return false }
	if (form.us_name.value.length < 3) { alert("Name must be entered"); return false }
	if (!libEmailCheck(form.us_email.value)) { alert("Email is not a valid format"); return false }
	if (form.us_contacttel.value.length < 8) { alert("Contact Tel must be entered"); return false }
	return true
	}

function checkUserPhotoUpload(form)
	{
	form.ph_caption.value = libDenyChars(form.ph_caption.value, "<>\";", false);
	form.ph_comments.value = libDenyChars(form.ph_comments.value, "<>\";", false);
	if (form.ph_caption.value.length < 2) { alert("Caption must be at least 2 characters (no carets, quotes or semicolons)"); return false }
	if (form.ph_compid.value.length != 32) { alert("You must select a competition"); return false }
	if (!form.objFile.value) { alert("You must select a file to upload"); return false }
	var arrSplit = form.objFile.value.split(".");
	if (arrSplit[arrSplit.length-1].toLowerCase() != "jpg") { alert("You must select a .JPG file"); return false }
	return true
	}

function checkCompEnquiry(form)
	{
	form.eq_name.value = libAllowChars(form.eq_name.value, "abcdefghijklmnopqrstuvwxyz0123456789 '-.", false);
	form.eq_tel.value = libAllowChars(form.eq_tel.value, "0123456789 .-+()", false);
	form.eq_email.value = libAllowChars(form.eq_email.value, "abcdefghijklmnopqrstuvwxyz0123456789_-.@", false);
	form.eq_message.value = libDenyChars(form.eq_message.value, "<>\";", false);
	if (form.eq_name.value.length < 1) { alert("You must enter your name."); return false }
	if (form.eq_tel.value.length < 1) { alert("You must enter a contact telephone number."); return false }
	if (!libEmailCheck(form.eq_email.value)) { alert("You must enter a valid email address."); return false }
	if (form.eq_message.value.length < 1) { alert("You must enter a message."); return false }
	return true
	}

function checkCompLoginHelp(form)
	{
	form.eq_email.value = libAllowChars(form.eq_email.value, "abcdefghijklmnopqrstuvwxyz0123456789_-.@", false);
	if (!libEmailCheck(form.eq_email.value)) { alert("You must enter a valid email address."); return false }
	return true
	}


function checkAdminTermsEdit(form)
	{
	form.txtTerms.value = libDenyChars(form.txtTerms.value, "<>\";", false);
	if (form.txtTerms.value.length < 1) { alert("You must enter something!"); return false }
	}


