// verify form fields in contact.htm

function validateEmail(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function verifyFormData(f) 
{ // f is the form
	var valid = true;
	var f_count = 9; // form has 7 input fields
	// initialize fields
	for (i=0;i<f_count;i++) {
	   f.elements[i].style.background="#EFE4CB"; // default background	   
	   f.elements[i].value = f.elements[i].value.replace(/^\xA0+|\xA0+$/g,""); // remove non-breaking spaces (160 or A0 in hex)
	   f.elements[i].value = f.elements[i].value.replace(/^\s+|\s+$/g,""); // trim spaces (lead-trail)
	}
		
    if (f.firstname.value.length<2) {
		f.firstname.style.background="yellow";
		valid = false;
		}		
    if (f.lastname.value.length<2) {
		f.lastname.style.background="yellow";
		valid = false;
		}
    if ((f.email.value.length<6)||(!validateEmail(f.email.value))) {
		f.email.style.background="yellow";
		valid = false;
		}
	if (f.country.selectedIndex==0)	{
	    f.country.style.background="yellow";
		valid = false;
	}
	if (f.postal_code.value.length<2) {
		f.postal_code.style.background="yellow";
		valid = false;
	}
    if (f.msg_subject.value.length<2) {
		f.msg_subject.style.background="yellow";
		valid = false;
		}
    if ((f.msg_body.value.length<2)||(f.msg_body.value.indexOf('Type your question')!=-1)) {
		f.msg_body.style.background="yellow";
		valid = false;
		}
		
	if (!valid)
		alert("Some required fields are missing or contain incorrect values.\nPlease fill in the fields marked with the yellow color and try again.");
	return valid;
}
