// Vital Platform Lite - Common form validation functions
// This is used if conf.ini form_validator is set to "vital"

function is_numeric(expr) {
  var nums = "0123456789";

  if (expr.length == 0)
    return(false);

  for (var n=0; n < expr.length; n++) {
    if (nums.indexOf(expr.charAt(n)) == -1)
      return(false);
  }
  return(true);
}

function is_valid_phone(expr) {
  var nums = "+ ()0123456789";

  if (expr.length < 4)
    return(false);

  for (var n=0; n < expr.length; n++) {
    if (nums.indexOf(expr.charAt(n)) == -1)
      return(false);
  }
  return(true);
}

function is_valid_email(str) {
  if (str.length < 5)
    return(false);

//  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
return true;
}

/* Contact form validation */
function contact_form_validation(object)
{	
	if (object.name.value == "") {
		window.alert("Name is a mandatory field.");
		object.name.focus();
		return;
	}
	if (object.phone.value.length > 0 && !is_valid_phone(object.phone.value)) {
		window.alert("Telephone number is not valid.");
		object.phone.focus();
		return;
	}
/*
	if (object.email.value == "") {
		window.alert("S" + String.fromCharCode(228) + "hk" + String.fromCharCode(246) + "posti on pakollinen kentt" + String.fromCharCode(228) + ".");
		object.email.focus();
		return;
	}
*/
	if (object.phone.value.length < 1 && object.email.value.length < 1) {
		window.alert("You must fill either the field \"Telephone\" or the field \"E-Mail\" so we can be in touch with you.");
		object.phone.focus();
		return;
	}
	if (object.email.value.length > 0 && !is_valid_email(object.email.value)) {
		window.alert("E-Mail is not valid.");
		object.email.focus();
		return;
	}
	if (object.subject.value == "") {
		window.alert("Headline is a mandatory field.");
		object.subject.focus();
		return;
	}
	if (object.message.value == "") {
		window.alert("Message is a mandatory field.");
		object.message.focus();
		return;
	}
	object.submit();
}

function petition_form_validation(object)
{	
	if (object.fname.value == "") {
		window.alert("First name is a mandatory field.");
		object.fname.focus();
		return;
	}
	if (object.lname.value == "") {
		window.alert("Last name is a mandatory field.");
		object.lname.focus();
		return;
	}
	if (object.city.value == "") {
		window.alert("City is a mandatory field.");
		object.city.focus();
		return;
	}
	if (object.country.value == "") {
		window.alert("Country is a mandatory field.");
		object.country.focus();
		return;
	}
	if (object.email.value == "") {
		window.alert("E-mail is a mandatory field.");
		object.email.focus();
		return;
	}
	if (object.email.value.length > 0 && !is_valid_email(object.email.value)) {
		window.alert("E-Mail is not valid.");
		object.email.focus();
		return;
	}
	object.submit();
}

// EOF

