﻿function setChanged(FormRef,TID) {if (FormRef.Changedlist.value.indexOf("," + TID + ",")==-1) {FormRef.Changedlist.value+=TID + ","}}	

function submitLogin(formRef) {
	if (formRef.ExisEmail.value=="Email address") {formRef.ExisEmail.value=""}
	if (formRef.ExisEmail.value=="") {alert("Please enter your email address"); formRef.ExisEmail.focus(); return false}
	if (formRef.ExisPassword.value=="") {alert("Please enter your password"); formRef.ExisPassword.focus(); return false}
	if (emailCheck(formRef.ExisEmail.value)==true) {formRef.submit()}
	}
	
function submitSearch() {document.searchform.submit()}

function checkForm(formRef) {
	if (formRef.Fullname.value=="") {alert("Please enter your full name"); formRef.Fullname.focus(); return false}
	if (formRef.Password.value=="") {alert("Please enter your password"); formRef.Password.focus(); return false}
	if (formRef.Password.value.length<5) {alert("Please ensure your password is longer than 5 characters"); formRef.Password.focus(); return false}
	if (formRef.ConfirmPassword.value!=formRef.Password.value) {alert("The passwords do not match"); formRef.ConfirmPassword.focus(); return false}	
	if (formRef.address1.value=="") {alert("Please enter your address"); formRef.address1.focus(); return false}
	if (formRef.city.value=="") {alert("Please enter a city"); formRef.city.focus(); return false}
	if (formRef.postcode.value=="") {alert("Please enter a postcode"); formRef.postcode.focus(); return false}
	return emailCheck(formRef.Email.value)
	}

function checkFormChangeAddress(formRef) {
	if (formRef.Fullname.value=="") {alert("Please enter your full name"); formRef.Fullname.focus(); return false}
	if (formRef.address1.value=="") {alert("Please enter your address"); formRef.address1.focus(); return false}
	if (formRef.city.value=="") {alert("Please enter a city"); formRef.city.focus(); return false}
	if (formRef.postcode.value=="") {alert("Please enter a postcode"); formRef.postcode.focus(); return false}
	}
	
function checkFormChangePass(formRef) {
	if (formRef.ExistingPassword.value=="") {alert("Please enter your existing password"); formRef.ExistingPassword.focus(); return false}
	if (formRef.Password.value=="") {alert("Please enter your new password"); formRef.Password.focus(); return false}
	if (formRef.ConfirmPassword.value!=formRef.Password.value) {alert("Passwords do not match"); formRef.ConfirmPassword.focus(); return false}
	}

function checkFormForgotten(formRef) {return emailCheck(formRef.Email.value)}

function checkMultiPriceForm(formRef) {
	var HasQtys=false;
	for (var i=0; i<formRef.elements.length; i++) {if ((formRef.elements[i].name) && (formRef.elements[i].name.substring(0,9)=="quantity_") && (formRef.elements[i].selectedIndex>0)) {HasQtys=true}}
	if (HasQtys) {formRef.action+="&OPT=Y"; formRef.submit(); return false}
	else {alert("Please select a quantity for at least one product option"); return false}
	}

function emailCheck(emailStr) {
	emailStr = emailStr.toLowerCase(); // Validate email address
	var checkTLD=0; // Verify that the address ends in a two-letter country or well-known TLD.  1 means check it, 0 means don't.
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co|uk)$/; // Known TLDs that an e-mail address must end with.
	var emailPat=/^(.+)@(.+)$/; // Pattern to check if the entered e-mail address fits the user@domain format, also used to separate the username from the domain.
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; // Pattern for matching all special characters. Do not allow special characters in address. These include ( ) < > @ , ; : \ " . [ ]
	var validChars="\[^\\s" + specialChars + "\]"; //Range of characters allowed in username or domainname (really states which chars aren't allowed).
	var quotedUser="(\"[^\"]*\")"; // Pattern applies if "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't).  E.g. "jiminy cricket"@disney.com is a legal e-mail address.
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; // Pattern applies for domains that are IP addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal e-mail address (square brackets are required).
	var atom=validChars + '+'; // Represents an atom (basically a series of non-special characters).
	var word="(" + atom + "|" + quotedUser + ")"; // Represents one word in the typical username. Eg. in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string.
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); // Pattern describes the structure of the user
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); // Pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above.

	// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze.
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		// Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address.
		alert("Please enter a valid email address");
		return false;
		}
	var user=matchArray[1];
	var domain=matchArray[2];
	// Check that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert("Please enter a valid email address");return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert("Please enter a valid email address");return false}}
	// See if "user" is valid
	if (user.match(userPat)==null) {alert("Please enter a valid email address");return false}
	// if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid.
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert("Please enter a valid email address");return false}}
		return true;
		}
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {alert("Please enter a valid email address");return false}}
	// Make sure domain name ends in a known top-level domain or a two-letter word representing country (uk, nl)
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {alert("Please enter a valid email address");return false}
	// Make sure there's a host name preceding the domain.
	if (len<2) {alert("Please enter a valid email address");return false}
	// Email address is valid!
	return true;
	}
