<!--

// CHECKEMAIL Form Validation script
// Version 1.04
// Copyright GTP iCommerce Pty. Ltd. 2001 All Rights Reserved
// Coded by James Nicholls, overworked keyboard monkey

// Submit an email address, and this script will check it's formatting.
//
// 	Valid email = USER@DOMAIN
//
// 	USER is string with forbidden characters ()<>@,;:\"[] or the space. ie I_Rule
// 	USER may be a double quoted string containing forbidden chracters. ie "I Rule :)"
//
//	DOMAIN can be an IP address - [0.0.0.0] - where 0 represents a number from 0 to 255
//  OR DOMAIN can be a valid domain name.
//
//	Valid domain names are at minimum
//
//				 string.TLD
//
//  where TLD is a valid Top Level Domain or valid Country Top Level Domain
//	Valid Top Level Domains - 	.edu .com .co .net .org .gov .mil .int .aero . biz
//								.coop .info .museum .name .pro
//	For Valid Country Domains see gigantic array in code below
//
//	This script now checks validity of country TLDs
//	Additional strings may be added to begining of domain name. 
//
//				string.string.string.TLD
//

var checkEmailInclude = true;

function ContactUsCheckForIncludedFile()
{
	if (typeof(window['ContactUsCheckitInclude']) !="undefined")
	{
		return ContactUsCheckIt();
	} 
	else
	{
		alert('Please include http://wwww.gtp.com.au/contact.js javascript file')
		return false;
	}
}

function CheckEmail(TheAddress)
{
			//Renders into lower case since the idiots are taking over
			TheAddress = TheAddress.toLowerCase();

			//Tests for at least 1 @ symbol
			if (TheAddress.indexOf('@') == -1) {alert('Your email does not contain an @ symbol'); return false;};

			//Splits into USER and DOMAIN at last @ symbol
			var LastAt = TheAddress.lastIndexOf('@');
			var USER = TheAddress.substr(0,LastAt);
			var DOMAIN = TheAddress.substr(LastAt+1,TheAddress.length);

			//Checks if USER is longer than 0
			if (USER.length < 1) {alert('Your email does not contain a user name'); return false;};

			//checks for quoted USER, then Invalid Characters in USER
			if ((USER.charAt(0) == "\"") && (USER.charAt(USER.length-1) == "\"")) {  } else {
						var Invalid = "()<>@,;:\\\"[] ";
						Counter = USER.length;
						while (Counter > 0) {
											var This_Character = USER.charAt(Counter -1);
											if (Invalid.indexOf(This_Character) > -1) {window.alert("Email user name contains an invalid character - " + This_Character);return false;};
											Counter--;			
											};
						};

			//Congratulations. USER has passed. 

			//checks out DOMAIN
			if ((DOMAIN.charAt(0) == "[") && (DOMAIN.charAt(DOMAIN.length-1) == "]")) {

					// DOMAIN starts with [ and ends with ]. Must be an IP address. 

					// Checks to see if any characters but '.' '[' ']' or numeric are present
					var Valid = "[1234567890.]";
					var Counter = DOMAIN.length;
					var DotCounter = 0
						
					while (Counter > 0) {
										var This_Character = DOMAIN.charAt(Counter -1);
										if (This_Character == ".") {DotCounter++;};
										if (Valid.indexOf(This_Character) > -1) { } else {window.alert("Email domain is not a valid IP Address");return false;};
										Counter--;			
										};

					// If any number of '.' except three are present, bad IP address.
					if (DotCounter != 3) {alert("Email domain is not a valid IP Address"); return false;};
					
					// Makes sure numbers in IP address are between 0 and 255
					DOMAIN = DOMAIN.slice(1,DOMAIN.length-1);
					DOMAIN = DOMAIN.split('.');
					var Counter = DOMAIN.length-1; 	//Length should always be 3, but best to be sure

					while (Counter > -1) {
										var TestNumber = DOMAIN[Counter];
										if (TestNumber == "") {alert("Email domain is not a valid IP Address"); return false;};
										if (TestNumber > 255) {alert("Email domain is not a valid IP Address"); return false;};
										Counter --
										};

					// Congratualtions DOMAIN is a valid IP address. This is a valid email.
					return true; 

 		} else {

					// DOMAIN must be a domain name. 
					// Check for invalid Characters
						var Invalid = "()<>@,;:\\\"[] ";
						Counter = DOMAIN.length;
						while (Counter > 0) {
											var This_Character = DOMAIN.charAt(Counter -1);
											if (Invalid.indexOf(This_Character) > -1) {window.alert("Email domain name contains an invalid character - " + This_Character);return false;};
											Counter--;			
											};


					// Check for at least 1 '.'
					if (DOMAIN.indexOf('.') == -1) {alert("Email domain is not a valid domain"); return false;};

					// makes sure DOMAIN contains a valid TLD
					DOMAIN = DOMAIN.split('.');

					// Checks there are at least two segments
					if (DOMAIN.length < 2) {alert("Email domain is not a valid domain"); return false;};

					// Checks all TLD segments are longer than 0
					for(count=DOMAIN.length-1 ; count > -1 ; count--)
							if (DOMAIN[count].length < 1) {alert("Email domain is not a valid domain"); return false;};


					// Checks final TLD segment. If length < 3 must be a country TLD. Otherwise a gTLD. Check validity of both.
					// .co is a special case, but is in both arrays so (as The Rock says) it doesn't matter!
					var ThisSegment = DOMAIN[DOMAIN.length-1];
					var Flag = 0;
					if (ThisSegment.length < 3) {
												var CountryTLDs = ["ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as","at","au","aw","az","ax","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er","es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","su","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tm","tn","to","tp","tr","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zr","zw"];
												for (count=0 ; count < CountryTLDs.length; count++) {
													if (ThisSegment == CountryTLDs[count]) {Flag++;};
												};
												} else {
												var gTLDs = ["arpa","edu","com","co","net","org","gov","mil","int","aero","biz","coop","info","museum","name","pro"];
												for (count=0 ; count < gTLDs.length; count++) {
													if (ThisSegment == gTLDs[count]) {Flag++;};
												};
											}
					if (Flag == 0) {alert("Email domain is not a valid domain"); return false;};

					// Congratualtions DOMAIN is correctly formatted (probably) to be a valid domain. This is the best I can do.
					return true; 
					};
}
//-->
