/* Copyright Digital:Idiom c2004 - Contact websupport @ digitalidiom.co.uk */
/* www.digitalidiom.co.uk for professional web design to W3C standards */
/* NO copy or use of original material by Digital:Idiom without express permission */


function checkWholeForm(theForm) {

	var why = "";
	why += checkName(theForm.name.value);
	why += checkEmail(theForm.email.value);
	why += checkPhone(theForm.phone.value);
	//why += checkMobile(theForm.mobile.value);
	why += checkComment(theForm.comment.value);

    if (why != "") {
	   alert("Oops, you need to correct some errors:\n\n" + why);
       return false;
    } 
return true;
}



function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;  //  (\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3}) 
    if (!(emailFilter.test(strng))) {
       error = "Please enter a valid email address.\n";
    }
    else {
	//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains incorrect characters.\n";
       }
    }
return error;
}


// phone number - strip out delimiters and check for at least 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.\n";

    }
    if (!(stripped.length >= 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
return error;
}


// mobile number - strip out delimiters and check for at least 10 digits

function checkMobile (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a mobile number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The mobile number contains illegal characters.\n";

    }
    if (!(stripped.length >= 10)) {
	error = "The mobile number is the wrong length. Make sure you included an area code.\n";
    }
return error;
}


// Name - 3-30 chars, uc, lc, and underscore only.
// ((\w+)|\s|.)+ to check multi-char space or dot or digit
// ([A-Za-z]|\s|\.)+

function checkName (strng) {
var error = "";
var legalChars = /^([A-Z]|[a-z]|\s|\.|\-)+$/; // allow letters, stops and spaces only
    
	if ((strng == "") || (strng == "your name")) {
		error = "The name has not been completed.\n";
	}
    else if ((strng.length < 3) || (strng.length > 30)) {
       error = "The name is not the correct length (3 to 30 alpha characters).\n";
    }
	else if (!legalChars.test(strng)) {
		error = "The name should only contain alpha characters.\n";
    }

return error;
}

// Comment textbox

function checkComment(strng) {
var error = "";
  if (strng.length < 10) {
     error = "The comment area has not been completed.\n"
  }
return error;
}

// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The text area is mandatory.\n"
  }
return error;
}

// was textbox altered

function isDifferent(strng) {
var error = "";
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}
