function convertToRomanNumerals(x){

var ones = ["","I","II","III","IV","V","VI","VII","VIII","IX"];
var tens = ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"];
var hund = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"];
var thou = ["","M","MM","MMM"];
var a = [];
var n = "";
var inBox = window.document.converter.dataIn;
var outBox = window.document.converter.dataOut;

//Is x nothing or just whitespace?
if (x == "" || /^\s+$/.test(x)){
 alert('Empty!\nEnter a number to be converted to Roman numerals.');
 inBox.value = "";
 inBox.focus();
 return false;
}

//Remove any whitespace.
x = x.replace(/[ ]+/g,"");
inBox.value = x;

//Remove non-numerical characters. Restart if none were entered.
if (/\D+/g.test(x)){
 alert("Numbers only!\nRemoving non-numerical characters...");
 x = x.replace(/\D+/g,"");
 inBox.value = x;
  if (x == ""){
   alert('...no number found after removal.\nTry again.');
   inBox.focus();
   return false;
  }
}

//Is x all zeros? Remove all bar 1.
if (/^0+$/g.test(x)){
 inBox.value = 0;
 inBox.select();
 alert('Enter a number from 1 to 3999 only!');
 return false;
}

//Is x in range?
if (x < 1 || x > 3999){
 x = x.replace(/^0+/g,"");
 inBox.value = x;
 alert('Enter a number from 1 to 3999 only!');
 inBox.select();
 return false;
}

//Remove all leading zeros.
x = x.replace(/^0+/g,"");
inBox.value = x;

//OK - Convert!

x = x.toString();
x = x.split("");
x.reverse();

for (i = 0; i < 4; i++){
 if (x[i] != undefined){ 
  a[i] = x[i];
 }
 else{
  a[i] = 0;
 }
}

n += thou[a[3]]; 
n += hund[a[2]];  
n += tens[a[1]];      
n += ones[a[0]];      

outBox.value = n;
}
