(function(){
	var money = window.money = function (m) {
		this._orgMoney = m;
	}
	money.prototype = {
		//验证是否为合法的money
		checkInvalidMoney: function () {
			var m = this._orgMoney;
			if (m == null
				|| m === undefined) {
				return false;
			}
			//parseFloat().toString();
			m = m.toString();
			m = m.replace(/[^\-\.\d]/g, "");
			return !isNaN(m);
		},
		//将money转换为标准的decimal数据
		toDecimal: function () {
			if (this.checkInvalidMoney()) {
			    var m = this._orgMoney.toString();
				m = m.replace(/[^\-\.\d]/g, "");
				return parseFloat(m);
			}
			return 0;
		},
		//将money转换为人民币大写形式
		toUpperRMB: function () {
			var m = this.toDecimal();
			if (!this.checkInvalidMoney()) {
				return "";
			}
			m = m.toString();
			return toUpperRMB(m);
		}
	};

	var DXSZ = "零壹贰叁肆伍陆柒捌玖",
		DXDW = "毫厘分角元拾佰仟萬拾佰仟亿拾佰仟萬兆拾佰仟萬亿京拾佰仟萬亿兆垓",
		SCDW = "元拾佰仟萬亿京兆垓";

	//转换为人民币大写
	function toUpperRMB(money) {
	    var result = "",//转化后的结果
			m = money,
			isNegative = /^\-/.test(m),//是否是负数
			isFloat = /\./.test(m);//是否是小数;
	    m = m.replace(/^\-/, "");//去掉负号，将数据变为正数
	    var arr = m.split('.');
		var integer = arr[0];//整数部分
		result = toUpperInteger(integer);
		if (isFloat) {
			result += toUpperFloat(arr[1], parseInt(integer) != 0 ? true : false);
		} else {
			if (result == "") {
				result = "零元";
			}
			result += "整";
		}
		if (isNegative) {
			result = "负" + result;
		}
		return result;
	}
	//将整数部分转为人民币大写
	function toUpperInteger(integer) {
		var v = integer,
			m = parseInt(v);
		if (m == 0) {
			return "";
		}
		var currCap = "",    //当前金额
			capResult = "",  //结果金额
			currentUnit = "",//当前单位
			resultUnit = "", //结果单位           
			prevChar = -1,      //上一位的值
			currChar = 0,       //当前位的值
			posIndex = 4;       //位置索引，从"元"开始
		for (var i = v.length - 1; i >= 0; i--) {
			currChar = parseInt(v.substr(i, 1));
			if (posIndex > 30)
			{
				//已超出最大精度"垓"。注：可以将30改成22，使之精确到兆亿就足够了
				break;
			}
			else if (currChar != 0) {
				currCap = DXSZ.substr(currChar, 1) + DXDW.substr(posIndex, 1);
			} else {
				//防止转换后出现多余的零,例如：3000020
				switch (posIndex) {
					case 4: currCap = "元"; break;
					case 8: currCap = "萬"; break;
					case 12: currCap = "亿"; break;
					case 17: currCap = "兆"; break;
					case 23: currCap = "京"; break;
					case 30: currCap = "垓"; break;
					default: break;
				}
				if (prevChar != 0) {
					if (currCap != "") {
					    if (currCap != "元") {
					        currCap += "零";
					    }
					}
					else {
						currCap = "零";
					}
				}
			}
			//对结果进行容错处理               
			if (capResult.Length > 0) {
				resultUnit = capResult.substr(0, 1);
				currentUnit = DXDW.substr(posIndex, 1);
				if (SCDW.indexOf(resultUnit) > 0) {
					if (SCDW.indexOf(currentUnit) > SCDW.indexOf(resultUnit)) {
						capResult = capResult.substr(1);
					}
				}
			}
			capResult = currCap + capResult;
			prevChar = currChar;
			posIndex += 1;
			currCap = "";
		}
		return capResult;
	}
	//将小数部分转为人民币大写
	function toUpperFloat(value,addZero) {
		var v = value,
			m = parseInt(value);
		if (m == 0) {
			return "";
		}
		var currCap = "",
			capResult = "",
			prevChar = addZero ? -1 : 0,
			currChar = 0,
			posIndex = 3;
		for (var i = 0; i < v.length; i++) {
			currChar = parseInt(v.substr(i, 1));
			if (currChar != 0) {
				currCap = DXSZ.substr(currChar, 1) + DXDW.substr(posIndex, 1);
			} else {
				if (parseInt(v.substr(i)) == 0) {
					break;
				} else if (prevChar != 0) {
					currCap = "零";
				}
			}
			capResult += currCap;
			prevChar = currChar;
			posIndex -= 1;
			currCap = "";
		}
		return capResult;
	}

})();