function resetFrameSize(which) {
	$("#sizes a").each(function(){
		$(this).removeClass("selected");
	});
	$("#s"+which).addClass("selected");
	$.get("/ajax-switch_adult_size.cfm?sizeid="+which, function(data){
		updatePrice();
	});
}

function loadMattressContent(which) {
	$("#mattress_content").slideUp("fast", function(){
		$("#mattress_content").empty()
		$.get("/ajax-mattress_product.cfm?id="+which, function(data){
			$("#mattress_content").html(data);
			$("#mattress_content").slideDown("fast");
		});
	});
}

function loadKidMattressContent(which,what) {
	$("#mattress_content").slideUp("fast", function(){
		$("#mattress_content").empty()
		$.get("/ajax-load_mattress_content.cfm?id="+which+"&kid", function(data){
			$("#mattress_content").html(data);
			$("#mattress_content").slideDown("fast");
			$("#mpids").val(what);
			updateFrameAndMattressPrice();
		});
	});
}

function loadChildrensMattressContent(which) {
	$("#mattress_content").slideUp("fast", function(){
		$("#mattress_content").empty()
		$.get("/ajax-mattress_product_childrens.cfm?id="+which, function(data){
			$("#mattress_content").html(data);
			$("#mattress_content").slideDown("fast");
		});
	});
}

function loadFurniture(which) {
	$("#furniture_content").slideUp("fast", function(){
		$("#furniture_content").empty()
		$.get("/ajax-furniture_product.cfm?id="+which, function(data){
			$("#minifurniturebanners img").each(function(){
				$(this).attr("src", "http://img.warrenevans.com/productbuttons/"+$(this).attr("rel")+".png");
			});
			$("#img"+which).attr("src", "http://img.warrenevans.com/productbuttons/"+which+"o.png");
			$("#furniture_content").html(data);
			$("#furniture_content").slideDown("fast");
		});
	});
}

function submitBedTrackingForm(what) {
	$("#trk").val(what);
	document.productForm.submit();
}

function toggleTabSection(which) {
	for(i=1;i<7;i++) {
		if(i!=which) {
			$("#s"+i).slideUp();
		}
	}
	$("#s"+which).slideToggle();
}

function updateUBS(val,which) {
	if(val==true) {
		$("#ubsd"+which).slideDown("fast");
	} else {
		$("#ubsq"+which).attr("selectedIndex",0);
		$("#ubsd"+which).slideUp("fast");
	}
}
function loadMattressesBySize(which) {
	$.get("/ajax-load_mattresses_by_size.cfm?id="+$("#prices").val()+"&m="+which, function(data){
		$("#mpids").html(data);
		$("#mattress-content").slideUp("fast");
		updateFramePrice();
		loadMattressBySize($("#mpids").val());
	});
}
function loadMattressBySize(which) {
	$.get("/ajax-load_mattress_by_size.cfm?id="+which, function(data){
		$("#mattress-content").html(data);
		$("#mattress-content").slideDown("fast")
	});
}

function pcaFastAddressBegin(postcode,building,language,style,account_code,license_code,machine_id,options) {
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&postcode=" + escape(document.getElementById("Postcode").value);
	strUrl += "&building=" + escape(document.getElementById("Building").value);
	strUrl += "&language=" + escape("english");
	strUrl += "&style=" + escape("simple");
	strUrl += "&account_code=" + escape("WARRE11111");
	strUrl += "&license_code=" + escape("RH89-GZ95-KH89-PF49");
	strUrl += "&machine_id=" + escape("");
	strUrl += "&options=" + escape("");
	strUrl += "&callback=pcaFastAddressEnd";
	//Make the request
	if (scriptTag) {
		headTag.removeChild(scriptTag);
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScript";
	headTag.appendChild(scriptTag);
}

function pcaFastAddressEnd() {
	//Test for an error
	if (pcaIsError) {
		//Show the error message
		alert("Please Insert your postcode and house number");
	} else {
		//Check if there were any items found
		if (pcaRecordCount==0) {
			alert("Sorry, no matching items found");
		} else {
			//document.getElementById("company").value=pca_organisation_name[0];
			document.getElementById("Address1").value=pca_line1[0];
			document.getElementById("Address2").value=pca_line2[0];
			document.getElementById("Address3").value=pca_line3[0];
			document.getElementById("City").value=pca_post_town[0];
			document.getElementById("County").value=pca_county[0];
			document.getElementById("Postcode").value=pca_postcode[0];
		}
	}
}

function checkTAFForm(theForm) {
	if(theForm.FirstName.value=="") {
		alert("Please complete your first name"); theForm.FirstName.focus(); return false;
	}
	if(theForm.LastName.value=="") {
		alert("Please complete your last name"); theForm.LastName.focus(); return false;
	}
	if(theForm.Email.value=="") {
		alert("Please complete your Email address"); theForm.Email.focus(); return false;
	}
	if(ValidateEmailAddress(theForm.Email.value) == false) {
		theForm.Email.focus(); return false;
	}
	if(theForm.FriendsFirstName.value=="") {
		alert("Please complete your Friends first name"); theForm.FriendsFirstName.focus(); return false;
	}
	if(theForm.FriendsLastName.value=="") {
		alert("Please complete your Friends last name"); theForm.FriendsLastName.focus(); return false;
	}
	if(theForm.FriendsEmail.value=="") {
		alert("Please complete your Friends Email address"); theForm.FriendsEmail.focus(); return false;
	}
	if(ValidateEmailAddress(theForm.FriendsEmail.value) == false) {
		theForm.FriendsEmail.focus(); return false;
	}
	theForm.submit();
}

function checkBasketForm(theForm) {
	if(theForm.Title.selectedIndex==0) {
		alert("Please select your Title"); theForm.Title.focus(); return false;
	}
	if(theForm.FirstName.value=="") {
		alert("Please complete your first name"); theForm.FirstName.focus(); return false;
	}
	if(theForm.LastName.value=="") {
		alert("Please complete your last name"); theForm.LastName.focus(); return false;
	}
	if(theForm.Email.value=="") {
		alert("Please complete your Email address"); theForm.Email.focus(); return false;
	}
	if(theForm.Email_confirm.value!=theForm.Email.value) {
		alert("Please confirm your Email address"); theForm.Email_confirm.focus(); return false;
	}
	if(ValidateEmailAddress(theForm.Email.value) == false) {
		theForm.Email.focus(); return false;
	}
	if(theForm.Phone.value=="") {
		alert("Please complete your Phone number"); theForm.Phone.focus(); return false;
	}
	if(theForm.Address1.value=="") {
		alert("Please complete your address"); theForm.Address1.focus(); return false;
	}
	if(theForm.City.value=="") {
		alert("Please complete your City"); theForm.City.focus(); return false;
	}
	if(theForm.Postcode.value=="") {
		alert("Please complete your Postcode"); theForm.Postcode.focus(); return false;
	}
	if(!$("#SourceID option:selected").val()) {
		alert("Please tell us how you found out about Warren Evans"); theForm.SourceID.focus(); return false;
	}
	/*
	if(theForm.SourceID.selectedIndex==0) {
		alert("Please tell us how you found out about Warren Evans"); theForm.SourceID.focus(); return false;
	}
	*/
	theForm.submit();
}

function loadSources(which) {
	$("#theResult").slideUp();
	if(which!="-") {
		$.get("/ajax-load_sources.cfm?id="+which, function(data){
			tmp=data.split("|");
			$("#SourceID").html(tmp[1]);
			if(parseInt(tmp[0]) > 1) {
				$("#theResult").slideDown();
			}
		});
	} else {
		$("#SourceID").html(" ");
	}
}

function checkContactForm(theForm) {
	if(theForm.Title.selectedIndex==0) {
		alert("Please select your Title"); theForm.Title.focus(); return false;
	}
	if(theForm.FirstName.value=="") {
		alert("Please complete your first name"); theForm.FirstName.focus(); return false;
	}
	if(theForm.LastName.value=="") {
		alert("Please complete your last name"); theForm.LastName.focus(); return false;
	}
	if(theForm.Email.value=="") {
		alert("Please complete your Email address"); theForm.Email.focus(); return false;
	}
	if(ValidateEmailAddress(theForm.Email.value) == false) {
		theForm.Email.focus(); return false;
	}
	if(!$("#SourceID option:selected").val()) {
		alert("Please tell us how you found out about Warren Evans"); theForm.SourceID.focus(); return false;
	}
	theForm.submit();
}
/*
function checkContactForm(theForm) {
	alert($("#SourceID option:selected").val());
	if(theForm.Title.selectedIndex==0) {
		alert("Please select your Title"); theForm.Title.focus(); return false;
	}
	if(theForm.FirstName.value=="") {
		alert("Please complete your first name"); theForm.FirstName.focus(); return false;
	}
	if(theForm.LastName.value=="") {
		alert("Please complete your last name"); theForm.LastName.focus(); return false;
	}
	if(theForm.Email.value=="") {
		alert("Please complete your Email address"); theForm.Email.focus(); return false;
	}
	if(ValidateEmailAddress(theForm.Email.value) == false) {
		theForm.Email.focus(); return false;
	}
	if($("#SourceID option:selected").val()==0) {
		alert("Please tell us how you found out about Warren Evans"); theForm.SourceID.focus(); return false;
	}
	theForm.submit();
}
*/
function ValidateEmailAddress(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		var textObj =document.getElementById("Email"); 
		alert("Please enter a valid email address.")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		var textObj =document.getElementById("Email"); 
	    alert("Email address username is not valid.")
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
				var textObj =document.getElementById("Email"); 
		        alert("Email address destination IP is invalid.")
			return false
		    }
	    }
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		var textObj =document.getElementById("Email"); 
		alert("Email address domain name doesn't seem to be valid.")
	    return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
		var textObj =document.getElementById("Email"); 
		alert("Email address must end in a three-letter or four-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		var textObj=document.getElementById("Email"); 
		var errStr="Email address is missing a hostname."
		alert(errStr)
		return false
	}
	return true;
}

function checkDeliveryOption() {
	$.get("/ajax-delivery_description.cfm", function(data){
		if(data=="0") {
			$("#order-button").html('<a href="/your_basket/finish/"><b><u>Place your order</u></b></a>');
			alert("Please select a delivery preference");
			document.getElementById("delivery-selector").focus();
		} else {
			document.location.href="/your_basket/finish/";
		}
	});
}

function saveDeliveryRequirements(what) {
	$.post("/ajax-delivery_requirements.cfm", {data: what});
}

function updateDeliveryPreference() {
	$.get("/ajax-delivery_description.cfm?id="+$("#delivery-selector").val());
	if($("#delivery-selector").val()=="collection") {
		$("#delins1,#delins2").slideUp("fast");
		document.getElementById("delprice").innerHTML="&pound;0";
		document.getElementById("totalprice").innerHTML=$("#pwdnd").val();
	} else {
		$("#delins1,#delins2").slideDown("fast");
		$("#delprice").load("/ajax-delivery_price.cfm");
		$("#totalprice").html($("#pwd").val());
	}
}

function updateQuantity(which,quantity,price) {
	if(quantity=="0" && quantity!="") {
		removeQuantity(which);
	} else {
		$.get("/cart.cfm?id="+which+"&q="+quantity+"&update", function(data){
			document.getElementById("totals").innerHTML=data;
			if(data.indexOf("Is currently empty") > 0) {
				$("#order-button").slideUp("fast");
			} else {
				$("#order-button").slideDown("fast");
			}
		});
		document.getElementById("price"+which).innerHTML="&pound;"+parseInt(price)*quantity;
	}
}

function removeQuantity(which) {
	$.get("/cart.cfm?id="+which+"&delete", function(data){
		$("#i"+which).slideUp("fast");
		document.getElementById("totals").innerHTML=data;
		if(data.indexOf("Is currently empty") > 0) {
			$("#order-button").slideUp("fast");
		} else {
			$("#order-button").slideDown("fast");
		}
	});
}

function applyPromoCode() {
	$.get("/cart.cfm?pc="+$("#promocode").val(), function(data){
		document.getElementById("totals").innerHTML=data;
	});
}

function showFinish(which) {
	$("#finishes a").removeClass("selectedlabel");
	$("#fl"+which).addClass("selectedlabel");
	$("#finishpreview").html('<img src="/finishes/'+which+'l.jpg" width="420" height="273" alt="" />');
}

function updateFinish(which) {
	$("#finishes a").removeClass("selectedlabel");
	$("#fl"+which).addClass("selectedlabel");
	document.getElementById("fcb"+which).checked=true;
	$("#bigimagedescription").load("/ajax-load_product_image.cfm?ProductFinishID="+which);
	updatePrice();
}

function updateFrameFinish(which) {
	$("#finishes a").removeClass("selectedlabel");
	$("#fl"+which).addClass("selectedlabel");
	document.getElementById("fcb"+which).checked=true;
	$("#bigimagedescription").load("/ajax-load_frame_image.cfm?ProductFinishID="+which);
	updatePrice();
}

function updateBSFinish(fid) {
	$("#bssel").val(fid);
}

function updatev3FrameFinish(which) {
	/*
	$("#finishes a").removeClass("selectedlabel");
	$("#fl"+which).addClass("selectedlabel");
	*/
	$("#fcb"+which).attr("checked", true);
	$("#selectedfinish").load("/ajax-load_frame_image-v3.cfm?ProductFinishID="+which);
	updatePrice();
}

function loadImage(which) {
	$("#smallimages a").removeClass("selectedthumb");
	$("#pt"+which).addClass("selectedthumb");
	$.get("/ajax-load_product_image.cfm?ProductImageID="+which, function(data){
		document.getElementById("bigimagedescription").innerHTML=data;
		MagicZoom_findZooms();
	});
}

function loadBedStorage(url) {
	if($("#bssel").val()!="") {
		document.location.href=url+"?fid="+$("#bssel").val();
	} else {
		document.location.href=url;
	}
}

function loadFinishImage(which,fid) {
	if(fid>0) {
		$.get("/ajax-load_product_image_finish.cfm?ProductImageID="+which+"&FinishID="+fid, function(data){
			$("#finishes a").removeClass("selectedlabel");
			$("#fl"+data).addClass("selectedlabel");
			document.getElementById("fcb"+data).checked=true;
			updatePrice();
			updateBSFinish(fid);
		});
	}
	$("#smallimages a").removeClass("selectedthumb");
	$("#pt"+which).addClass("selectedthumb");
	$.get("/ajax-load_product_image.cfm?ProductImageID="+which, function(data){
		document.getElementById("bigimagedescription").innerHTML=data;
		MagicZoom_findZooms();
	});
}

function loadFrameFinishImage(which,fid) {
	if(fid>0) {
		$.get("/ajax-load_product_image_finish.cfm?ProductImageID="+which+"&FinishID="+fid, function(data){
			$("#finishes a").removeClass("selectedlabel");
			$("#fl"+data).addClass("selectedlabel");
			document.getElementById("fcb"+data).checked=true;
			updateFramePrice();
			updateBSFinish(fid);
		});
	}
	$("#smallrightimages a").removeClass("selectedthumb");
	$("#pt"+which).addClass("selectedthumb");
	$.get("/ajax-load_frame_image.cfm?ProductImageID="+which, function(data){
		$("#bigimagedescription").html(data);
		//MagicZoom_findZooms();
	});
}

function loadMattress(which) {
	document.location.href=which;
}

function switchMeasurements(which) {
	$.get("/ajax-load_sizes.cfm?ProductID="+which+"&PriceID="+$("#prices").val(), function(data){
		tmp=data.split("^");
		$("#measurementtext").html(tmp[0]);
		$("#prices").html(tmp[1]);
	});
}

function switchMeasurementsV3(which) {
	$.get("/ajax-load_sizes.cfm?ProductID="+which+"&PriceID="+$("#prices").val(), function(data){
		document.location.reload();
	});
}

function updateFramePrice() {
	// loop through finishes
	var pfid=0;
	for(i=0;i<document.productForm.ProductFinishID.length;i++) {
		if(document.productForm.ProductFinishID[i].checked==true) {
			pfid=document.productForm.ProductFinishID[i].value;
		}
	}
	// loop through underbed storage
	var ubs="";
	for(i=0;i<$("#ubsr").val();i++) {
		which=parseInt(i)+1;
		if($("#ubs"+which).attr("checked")==true) {
			ubs=ubs+$("#ubs"+which).val()+":"+$("#ubsq"+which+" option:selected").val()+",";
		}
	}
	$.get("/ajax-load_frame_price.cfm?PriceID="+$("#prices").val()+"&ProductFinishID="+pfid+"&mpid="+$("#mpids option:selected").val()+"&ubs="+ubs, function(data) {
		$("#price").html(data);
	});
	$.get("/ajax-set_size.cfm?id="+$("#prices option:selected").val());
}

function updateFrameAdditionalPrice() {
	// loop through finishes
	var pfid=0;
	for(i=0;i<document.productForm.ProductFinishID.length;i++) {
		if(document.productForm.ProductFinishID[i].checked==true) {
			pfid=document.productForm.ProductFinishID[i].value;
		}
	}
	// loop through underbed storage
	var ubs="";
	for(i=0;i<$("#ubsr").val();i++) {
		which=parseInt(i)+1;
		if($("#ubs"+which).attr("checked")==true) {
			ubs=ubs+$("#ubs"+which).val()+":"+$("#ubsq"+which+" option:selected").val()+",";
		}
	}
	$.get("/ajax-load_frame_price.cfm?PriceID="+$("#prices").val()+"&ProductFinishID="+pfid+"&mpid="+$("#mpids option:selected").val()+"&ubs="+ubs+"&amid="+$("#amid").val(), function(data) {
		$("#price").html(data);
	});
}

function updateFrameWTGUPrice() {
	if($("#wtgu").attr("checked")==true) {
		$("#wtgudetails").slideDown();
	} else {
		$("#wtgudetails").slideUp();
	}
	// loop through finishes
	var pfid=0;
	for(i=0;i<document.productForm.ProductFinishID.length;i++) {
		if(document.productForm.ProductFinishID[i].checked==true) {
			pfid=document.productForm.ProductFinishID[i].value;
		}
	}
	// loop through underbed storage
	var ubs="";
	for(i=0;i<$("#ubsr").val();i++) {
		which=parseInt(i)+1;
		if($("#ubs"+which).attr("checked")==true) {
			ubs=ubs+$("#ubs"+which).val()+":"+$("#ubsq"+which+" option:selected").val()+",";
		}
	}
	$.get("/ajax-load_frame_price.cfm?PriceID="+$("#prices").val()+"&ProductFinishID="+pfid+"&mpid="+$("#mpids option:selected").val()+"&ubs="+ubs+"&wtgu="+$("#wtgu").attr("checked"), function(data) {
		$("#price").html(data);
	});
}

function updatePrice() {
	var ProductFinishID=0;
	for(i=0;i<document.productForm.ProductFinishID.length;i++) {
		if(document.productForm.ProductFinishID[i].checked==true) {
			ProductFinishID=document.productForm.ProductFinishID[i].value;
		}
	}
	$("#price").load("/ajax-load_price.cfm?PriceID="+$("#prices").val()+"&Quantity="+$("#quantity").val()+"&ProductFinishID="+ProductFinishID);
}

function loadHelp(which) {
	winOpenCenter("/help.cfm?id="+which,"help","no","yes","no","no",700,525);
}

function tellAFriend(which) {
	winOpenCenter("/tell_a_friend.cfm?id="+which,"tell_a_friend","no","yes","no","no",700,525);
}

function viewSpecifiedImage(which) {
	var x = window.open('','pictureViewer','width=800,height=800,resizable=1');
	if( !x ) { return true; }
	x.document.open();
	x.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>Picture Viewer<\/title>');
	x.document.write('<sc'+'ript type="text/javascript">');
	x.document.writeln('var isNN,isIE;');
	x.document.writeln('function reSizeToImage(){');
	if (navigator.appName.indexOf("Microsoft")==-1)
	{
		x.document.writeln('window.innerWidth=document.images["ex_popimage"].width;');
		x.document.writeln('window.innerHeight=document.images["ex_popimage"].height;');
		x.document.writeln('document.title="Picture Viewer";');
	}else if(window.opera){
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=130-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}else{
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}
	x.document.writeln('}<\/sc'+'ript>');
	x.document.write('<\/head><body style="margin:0px;" id="ex_popbody" onload="reSizeToImage();self.focus();">');
	x.document.write('<img id="ex_popimage" name="ex_popimage" src="'+which+'" onclick="window.close();" alt="" \/><\/body><\/html>');
	x.document.close();
	if( x.focus ) { x.focus(); }
}
// Image switcher

function switchImages(which,what) {
	for(i=1;i<4;i++) {
		document.getElementById("img"+i+"a").className="image";
		document.getElementById("img"+i+"b").style.width="105px";
	}
	document.getElementById("img"+which+"b").style.width="90px";
	document.getElementById("img"+which+"a").className="image selected";
	document.getElementById("image").style.background="url('/images/bg-"+what+"-"+which+".jpg')";
}

// Window opening

function winOpenCenter(url,name,statbar,scroll,locate,resize,xWidth,yWidth)	{
var adjustedleft = 8//optional
var adjustedheight = 30//adjust height because of windows taskbar
var screenwidthremainder = screen.availWidth%2//really not needed, but won't hurt
var screenheightremainder = screen.availHeight%2
var screenwidth = screen.availWidth - screenwidthremainder
var screenheight = screen.availHeight - screenheightremainder
var winheight = yWidth //set new window height properties
var winwidth = xWidth //set new window width properties
var winleft = parseInt(screenwidth/2) - (winwidth/2) - adjustedleft//optional
var wintop = parseInt(screenheight/2) - (winheight/2) - adjustedheight

var win = window.open(url,name,'width=' +winwidth+ ',height=' +winheight+',status=' +statbar+',resizable='+resize+',scrollbars='+scroll+',location='+locate+',top='+wintop+',left='+winleft);
return win;
}

// Email Checker

function containsCharacter(str,testchar)
{
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == testchar) return true;
	}
	return false;
}

function validCharacters(teststr,validstr)
{
	for (i = 0; i < teststr.length; i++) {
		if (validstr.indexOf(teststr.charAt(i)) < 0) return false;
	}
	return true;
}

function isEmailValid(str)
{
	var pos;
	var errorCount = 0;
	var email = str;
	
	var validStr = "abcdefghijklmnopqrstuvwwarrenevansABCDEFGHIJKLMNOPQRSTUVWwarrenevans0123456789@.[]-_";

	if (email == "") {
		alert("Please enter an email address.");
		return false;
	}
	
	if (email.charAt(0) == "@" || email.charAt(0) == "." || email.charAt(0) == "[" || email.charAt(0) == "]" || email.charAt(0) == "-" || email.charAt(0) == "_") errorCount++;
	
	if (!validCharacters(email,validStr)) errorCount++;
		
	for (i = 1; i < email.length; i++) {
		if ((email.charAt(i-1) == "." && email.charAt(i) == ".") || (email.charAt(i-1) == "." && email.charAt(i) == "@") || (email.charAt(i-1) == "@" && email.charAt(i) == ".")) {
			errorCount++;
			break;
		}
	}

	if (!errorCount && email.indexOf("@") < 0 && email.length > 0) {
		alert("Your email address is missing the domain name. The format should be "+str+"@somedomain.\n\nFor example, "+str+"@aol.com, "+str+"@earthlink.net, or "+str+"@yahoo.com.");
		return false;
	}
	if (email.length < 6) errorCount++;
	pos = email.indexOf("@");
	email = email.substring(pos+1,email.length);
	if (email.indexOf("@") != -1) errorCount++;
	if ((email.charAt(0) == "[" && email.charAt(email.length-1) != "]") || (email.charAt(0) != "[" && email.charAt(email.length-1) == "]") || (email.charAt(0) == "]" || email.charAt(email.length-1) == "[")) errorCount++;
	if (email.indexOf(".") < 0) {
		errorCount++;
	}
	else {
		pos = email.lastIndexOf(".");
		email = email.substring(pos+1,email.length);
		if ((!isNaN(email) && email != "") || (email.charAt(email.length-1) == "]")) {
			pos = str.indexOf("@");
			if (email.charAt(email.length-1) == "]") {
				email = str.substring(pos+1,str.length-1);
				if (email.charAt(0) == "[") {
					email = email.substring(1,str.length);
				}				
			}
			else {
				email = str.substring(pos+1,str.length);
			}

			var ipAddress = "";
			var periods = 0;

			for (i = 0; i < email.length; i++) {
				if (email.charAt(i) == ".") {
					periods++;
					if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) {
						errorCount++
						break;
					}
					ipAddress = "";
				}
				else
					ipAddress += email.charAt(i).toString();
			}
			if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) errorCount++

			if (periods != 3) errorCount++;
		}
		else {
			if (email.length < 2 || email.length > 3) errorCount++;
		}
	}

	if (errorCount > 0) {
		alert(str+" is not a valid email address. Please check your typing.");
		return false;
	}
	return true;
}
