function check(form,id) {
	var imo = trim(form["input" + id].value);
	var message="";

	if(imo.length != 7)
	{
		message = "<font color = 'orange'>Please remember: IMO numbers should be seven characters long.</font>";
	}
	else if(checkIMO(imo))
	{
		message =  "<font color = 'green'>IMO number " + imo + " is valid.</font>";
	}
	else
	{
		message =  "<font color = 'red'>IMO number " + imo + " is invalid. The expected IMO number is " + (imo.substring(0,6)+(String(imo.charAt(0)*7+imo.charAt(1)*6+imo.charAt(2)*5+imo.charAt(3)*4+imo.charAt(4)*3+imo.charAt(5)*2)).charAt((String(imo.charAt(0)*7+imo.charAt(1)*6+imo.charAt(2)*5+imo.charAt(3)*4+imo.charAt(4)*3+imo.charAt(5)*2)).length-1)) + ".</font>";
	}

	document.getElementById("result").innerHTML=message;
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function checkIMO(imo)
{
	return imo == (imo.substring(0,6)+(String(imo.charAt(0)*7+imo.charAt(1)*6+imo.charAt(2)*5+imo.charAt(3)*4+imo.charAt(4)*3+imo.charAt(5)*2)).charAt((String(imo.charAt(0)*7+imo.charAt(1)*6+imo.charAt(2)*5+imo.charAt(3)*4+imo.charAt(4)*3+imo.charAt(5)*2)).length-1))
}

