function checkValue(field,thingywotsit)
{
  var temp = eval("document.contactForm."+field+".value;");
  if(temp=="" || temp==" ")
  {
    alert("Please fill in your " + thingywotsit + ", and try again.");
    eval("document.contactForm."+field+".focus();");
    return false;
  }
  else
  {
    return true;
  }

}

function checkForm()
{
  if(checkValue("word","word") && checkValue("yname","name") && checkValue("address1","address") && checkValue("postcode","postcode") && checkValue("county","county or state") && checkValue("country","country") && checkValue("dayphone","phone number") && checkValue("comments","comments about whiltonmill, or any questions you may have"))
  {
    if(emailCheck(document.contactForm.email.value))
    {
        document.contactForm.submit();
    }
    else
    {
            alert("Please fill in your email address, and try again.\nIf you have filled in your email address, check the '.' and '@' sign are correct, an email address will look something like this:\n\nyourname@somewhere.com                   or                  yourname@somewhere.co.uk\n");
            document.contactForm.email.focus();
            return false;
    }
  }
  else
  {
  	return false;
  }
}

function new_freecap()
{
  // loads new freeCap image
  if(document.getElementById)
  {
    // extract image name from image source (i.e. cut off ?randomness)
    thesrc = document.getElementById("freecap").src;
    thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
    // add ?(random) to prevent browser/isp caching
    document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
  }
  else
  {
    alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
  }
}

function emailCheck(emailStr)
{
	emailStr = emailStr.toLowerCase();
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|biz|edu|int|mil|gov|arpa|aero|name|coop|info|pro|museum|tv|ca|se|fr|de|it|jp|ng|no)$/;
	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)
	{
			return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++)
	{
		if (user.charCodeAt(i)>127)
		{
			return false;
		}
	}

	for (i=0; i<domain.length; i++)
	{
		if (domain.charCodeAt(i)>127)
		{
			return false;
		}
	}

	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;
			}
		}
	}

	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)
		{
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
	{
		return false;
	}

	if (len<2)
	{
		return false;
	}
	return true;
}
