var c_positions = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"];
	var c_digits = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
	var c_tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
	var c_teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
    

	function spellNum(num, unitsName, hundredthsName)
    {
        var units = parseInt(num);
        var hundredths = parseInt((num * 100 - parseInt(num) * 100));
        var numberString = spellNumber(units) + " " + unitsName;
        if(units != 1)
        {
            numberString += "s"
        }
        numberString += ", " + spellNumber(hundredths) + " " + hundredthsName;
        if(hundredths != 1)
        {
            numberString += "s";
        }
        return numberString;
    }
    function spellNumber(num)
        {
            var negative = false;
            if (num == 0)
            {
                return "zero";
            }
            if (num < 0)
            {
                num = -num;
                negative = true;
            }

            var numString = "";
            var pos = 0;
            while (num > 0)
            {
                if (num % 1000 != 0)
                {
                    numString = spellThreeDigits(num % 1000) + c_positions[pos] + numString;
                }
                if (parseInt(num / 1000) != 0)
                {
                    numString = ", " + numString;
                }
                num = parseInt(num / 1000);
                pos++;
            }
            if (negative)
            {
                numString = "negative " + numString;
            }

            return numString;
        }

    function spellThreeDigits(threeDigits){
        var hundredsDigit = parseInt(threeDigits / 100);
        var tensDigit = parseInt(threeDigits % 100 / 10);
        var onesDigit = threeDigits % 10;
        var numberString = "";
        if (hundredsDigit != 0)
        {
            numberString += c_digits[hundredsDigit] + " hundred";
            if (threeDigits % 100 != 0)
            {
                numberString += " ";
            }
        }
        return numberString + spellTwoDigits(tensDigit, onesDigit);
    }
    function spellTwoDigits(tensDigit, onesDigit){
        if (tensDigit == 1)
        {
            return c_teens[onesDigit];
        }
        else
        {
            var numberString = c_tens[tensDigit];
            
            if(tensDigit != 0 && onesDigit != 0)
            {
                numberString += " ";
            }
            numberString += c_digits[onesDigit];
            return numberString;
        }
    }
    
//======= // The following function handler is created by Microsoft Office InfoPath. // Do not modify the name of the function, or the name and number of arguments. // This function is associated with the following field or group (XPath): /my:myFields/my:ChequeAmount // Note: Information in this comment is not updated after the function handler is created. //======= function XDocument::OnLoad(eventObj) { // var amountField = XDocument.DOM.selectSingleNode("/my:myFields/my:ChequeAmount");
var spelledField = XDocument.DOM.selectSingleNode("/my:myFields/my:AmountWords");
spelledField.text = spellNum(amountField.text, "pounds", "pence"); }