//form validation script by Chris Dowdell
//see www.exmoorpark.com
//Exmoorpark form validation script 2006 
//If you use this script please leave comments intact

function Form1_Validator(prereg)
{

// check to see if the field is blank
if (prereg.User_name.value == "")
{
alert("You must enter a name.");
prereg.User_name.focus();
return (false);
}

// check to see if the field is blank
if (prereg.User_surname.value == "")
{
alert("You must enter a surname.");
prereg.User_surname.focus();
return (false);
}


// check to see if the field is blank
if (prereg.User_email.value == "")
{
alert("You must enter an email address.");
prereg.User_email.focus();
return (false);
}

// test if valid email address, must have @ and .
var checkEmail = "@.";
var checkStr = prereg.User_email.value;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkEmail.length;  j++)
{
if (ch == checkEmail.charAt(j) && ch == "@")
EmailAt = true;
if (ch == checkEmail.charAt(j) && ch == ".")
EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
if (EmailAt && EmailPeriod)
{
		EmailValid = true
		break;
	}
}
if (!EmailValid)
{
alert("The \"emails\" field must contain an \"@\" and a \".\".");
prereg.User_email.focus();
return (false);
}

// check to see if the field is blank
if (prereg.comments.value == "")
{
alert("You must enter a comment.");
prereg.coment.focus();
return (false);
}


// because this is a sample page, don't allow to exit to the post action
// comes in handy when you are testing the form validations and don't
// wish to exit the page
alertsay = "All required fields completed. "
alertsay = alertsay + "We will now submit your form."
alert(alertsay);
return (true);
// replace the above with return(true); if you have a valid form to submit to
}

/***** CUSTOMIZE THESE VARIABLES *****/

  // width to resize large images to
var maxWidth=300;
  // height to resize large images to
var maxHeight=300;
  // valid file types
var fileTypes=["gif","jpg","jpeg"];
  // the id of the preview image tag
var outImage="previewField";
  // what to display when the image is not valid
var defaultPic="spacer.gif";

/***** DO NOT EDIT BELOW *****/

function preview(what){
  var source=what.value;
  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  globalPic=new Image();
  if (i<fileTypes.length) globalPic.src=source;
  else {
    globalPic.src=defaultPic;
    alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
  }
  setTimeout("applyChanges()",200);
}
var globalPic;
function applyChanges(){
  var field=document.getElementById(outImage);
  var x=parseInt(globalPic.width);
  var y=parseInt(globalPic.height);
  if (x>maxWidth) {
    y*=maxWidth/x;
    x=maxWidth;
  }
  if (y>maxHeight) {
    x*=maxHeight/y;
    y=maxHeight;
  }
  field.style.display=(x<1 || y<1)?"none":"";
  field.src=globalPic.src;
  field.width=x;
  field.height=y;
}