// JavaScript Document
/***********************************************
* Validate the form data from the enquiry form
* The name, email address and enquiry are all mandatory
***********************************************/
function checkWholeForm(enquiryform) 
{
	var num_errors=0;
	var global_valfield;	// retain valfield for timer thread
    if (!checkName(enquiryform.RealName,"info_RealName")) num_errors=apply_focus(enquiryform.RealName,num_errors);
    if (!checkEmail(enquiryform.EmailAddress,"info_EmailAddress")) num_errors=apply_focus(enquiryform.EmailAddress,num_errors);
    if (!checkEnquiry(enquiryform.UsersEnquiry,"info_UsersEnquiry")) num_errors=apply_focus(enquiryform.UsersEnquiry,num_errors);
    if (!checkSecurityCode(enquiryform.SecurityCode,"info_SecurityCode")) num_errors=apply_focus(enquiryform.SecurityCode,num_errors);
	if (num_errors>0)
	{
	   if (num_errors>1) alert("Please correct the highlighted errors");
       if (num_errors==1) alert("Please correct the highlighted error");
	return false;
    }
return true;
}

function apply_focus(valfield,errors)
{
	errors+=1;
	if (errors==1)
	{
		setfocus(valfield);
	}
return errors;
}

// Check the Name field to see if any characters were entered
function checkBlank(strng)
{
  	if (strng.length == 0) 
	{   
	return true;
 	}
return false;
}

// Check the Name field to see if any characters were entered
function checkName(valfield,info_id)
{
	if (checkBlank(valfield.value))
	{
	msg(info_id,"error","Please enter your name.");
	return false;
	}
msg (info_id, "warn", "");
return true;
}

// Check the Email field to see if any characters were entered
function checkEmail(valfield,info_id)
{
	var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
	if (checkBlank(valfield.value))
	{
		msg(info_id,"error","Please enter your email.");
		return false;
    }
	else if (!email.test(valfield.value)) {
		msg(info_id,"error","Your email address is invalid.");
	    return false;
  	}
msg (info_id, "warn", "");
return true;
}

// Check the Name field to see if any characters were entered
function checkEnquiry(valfield,info_id)
{
	if (checkBlank(valfield.value))
	{
		msg(info_id,"error","Please enter your enquiry.");
		return false;
	}
msg (info_id, "warn", "");
return true;
}

// Check the Name field to see if any characters were entered
function checkSecurityCode(valfield,info_id)
{
	if (checkBlank(valfield.value))
	{
	msg(info_id,"error","Please enter the correct security code.");
	return false;
	}
msg (info_id, "warn", "");
return true;
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

//End