// JavaScript Document
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function CheckForm( theform )
{
	var bMissingFields = false;
	var strFields = "";
	var check_err ="";
	
	if( theform.name.value == '' ){
		bMissingFields = true;
		strFields += "     Name\n";
	}
	if( theform.pcode.value == '' ){
	bMissingFields = true;
	strFields += "     Post Code\n";
	}
	if( theform.pcode.value != '' ){
	check_err = validatePostCode(theform.pcode);
	if(check_err == 2){
		strFields += "     Post Code\n";
		bMissingFields = true;}
	if(check_err == 3){
		strFields += "     The Post Code contains illegal characters.\n";
		bMissingFields = true;}
	if(check_err == 4){
		strFields += "     Wrong length Post Code, 4 digit number for AU Post Codes\n";
		bMissingFields = true;}
	}
	if( theform.year.value != '' ){
	check_err = validatePostCode(theform.year);
	if(check_err == 3){
		strFields += "     The Year contains illegal characters.\n";
		bMissingFields = true;}
	if(check_err == 4){
		strFields += "     Wrong length Year, 4 digit number for Year\n";
		bMissingFields = true;}
	}
	if( theform.km.value != '' ){
	check_err = validatePostCode(theform.km);
	if(check_err == 3){
		strFields += "     The Km field contains illegal characters.\n";
		bMissingFields = true;}
	}
	if( theform.email.value == '' ){
		bMissingFields = true;
		strFields += "     Email Address\n";
	} else if (!isEmailAddr(theform.email.value)) {
		bMissingFields = true;
		strFields += "     Incomplete email address - yourname@yourdomain.com\n";
	}
	if( bMissingFields ) {
		alert( "I'm sorry, but you must provide the following field(s) before continuing:\n\n" + strFields);
		return false;
	}
	return true;
}

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validatePostCode(fld) {
    var error = 1;
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
   		error = 2;
        //error = "     Post Code\n";
        fld.style.background = 'red';
    } else if (isNaN(parseInt(stripped))) {
        error = 3;
		//error = "     The Post Code contains illegal characters.\n";
        fld.style.background = 'red';
    } else if (!(stripped.length == 4)) {
        error = 4;
		//error = "     Wrong length Post Code, 4 digit number for AU Post Codes\n";
        fld.style.background = 'red';
    }
	else
		fld.style.background = 'white';
    return error;
}