var objSD;
var maxLengthSD; 	// por defecto 7
var bDecimalesSD;	// true por defecto

function SoloDinero() {
	var sAux = new String(objSD.value);
	
	// Solo ie
	if (window.event) {
		if ( ((window.event.keyCode > 47) && (window.event.keyCode < 59)) || 
			 (window.event.keyCode == 44 && bDecimalesSD)) {
			return wer(true);
		} else {
			return wer(false);
		}
	}
}

function FormatearDinero() {
	var sAux = new String(objSD.value);
	var sRet = new String();
	var sParteEntera = new String();
	var sParteDecimal = new String();
	var fAux = 0;
	var c = "";
	var iComaPos = 0;
	var iTill = 0;	
	
	// Quitamos todo lo que no sea numero ni coma
	for (var i = 0; i < sAux.length && sRet.length < objSD.maxLength; i++) {
		c = sAux.charAt(i);
		if (c == "," && bDecimalesSD) {
			if (iComaPos == 0) {
				if (sRet.length == 0) sRet = "0"
				if (i < sAux.length && (sRet.length + 1) < maxLengthSD) {
					iComaPos = sRet.length;
					sRet += c;
				}
			}
		} else if (!isNaN(c) && !(sRet == "" && c == "0")) {
			sRet += c;
		}
	}
	
	// Redondeo a dos decimales
	if (bDecimalesSD && sRet.length > 0) {
		if (iComaPos == 0) {
			sRet += ",";
			iComaPos = sRet.length - 1;
		}
		
		switch (sRet.length - (iComaPos + 1)) {
			case 0:
				sRet += "00";
			break;
			case 1:
				sRet += "0"
			break;
			case 2:
			// Este esta ok :)
			break;
			default:
				sAux = sRet.substring(iComaPos + 1, sRet.length);
				sAux = sAux.substring(0, 2) + "." + sAux.substring(2, sAux.length);
				sAux = String(Math.round(parseFloat(sAux)));
				switch (sAux.length) {
					case 1:
						sRet = sRet.substring(0, iComaPos) + ",0" + sAux;
					break;
					case 2:
						sRet = sRet.substring(0, iComaPos) + "," + sAux;
					break;
					case 3:
						sRet = String(parseFloat(sRet.substring(0, iComaPos)) + 1) + ",00";
					break;
				}
			break;
		}
	}

	if (sRet.search(",")) {
		iTill = sRet.length - 1;
	} else {
		iTill = sRet.length;
	}

	for (i = maxLengthSD; maxLengthSD < iTill; i++) {
		sRet = sRet.substring(1, sRet.length);
		
		if (sRet.search(",")) {
			iTill = sRet.length - 1;
		} else {
			iTill = sRet.length;
		}
	}
	
	if (sRet == "0" || sRet == "0,00") {
		sRet = "";
	}

	if (bDecimalesSD) {
		sParteEntera = sRet.substring(0, sRet.search(","))
		sParteDecimal = sRet.substring(sRet.search(",") + 1, sRet.length)
	} else {
		sParteEntera = sRet;
	}
	
	if (sParteEntera.length > 3) {
		sAux = "";
		
		for (i = 3; i < sParteEntera.length; i = i + 3) {
			sAux =  "." + sParteEntera.substring(sParteEntera.length - i, sParteEntera.length - (i - 3)) + sAux;
		}

		sAux = sParteEntera.substring(0, (3 - (i - sParteEntera.length))) + sAux;
		
		sParteEntera = sAux
	}
	
	if (bDecimalesSD && sParteEntera != "" && sParteDecimal != "") {
		if (sParteDecimal.length == 1) {
			sParteDecimal = String(sParteDecimal) + "0";
		}
		sRet = sParteEntera + "," + (sParteDecimal + "");
	} else {
		sRet = sParteEntera;
	}
//	if (sRet.length > maxLengthSD) {
//		objSD.maxLength = sRet.length;
//	}
	objSD.value = sRet;
	return true;
}

// wer = Window Event ReturnValue
function wer(bReturn) {
	// Solo ie
	if (window.event) {
		window.event.returnValue = bReturn;
	}
	return bReturn;
}

function SoloDineroOn (obj, max_length, decimales) {
	objSD = obj;
	// Valores por defecto
	if (max_length == null) {
		maxLengthSD = 8;
	} else {
		maxLengthSD = max_length;
	}
	
	if (decimales == null) {
		bDecimalesSD = true;
	} else {
		bDecimalesSD = decimales;
	}

	var iAuxObjLength;
	
	if (bDecimalesSD) {
		if (max_length < 3) {
			maxLengthSD = 3;
		}
		
		iAuxObjLength = (maxLengthSD - 2);
	} else {
		iAuxObjLength = maxLengthSD;
	}
	
	if (iAuxObjLength % 3 != 0) {
		iAuxObjLength =  (3 - (iAuxObjLength % 3)) + iAuxObjLength;
	}
	
	iAuxObjLength = (iAuxObjLength / 3) - 1;
	
	if (bDecimalesSD) {
		iAuxObjLength++;
	}
	
	objSD.maxLength = iAuxObjLength + maxLengthSD;
/*	if (objSD.style) {
		if (objSD.style.textAlign == '') {
			objSD.style.textAlign = "right";
		}
	}*/
	window.document.onkeypress = SoloDinero;
	
	if (!objSD.onblur) {
		objSD.onblur = SoloDineroOff;
	}
}

function SoloDineroOff () {
	FormatearDinero();
	objSoloDinero = null;
	window.document.onkeypress = null;
}

function objSoloDinero (obj, max_length, decimales) {
	var mObj = obj;
	var mMaxLength = max_length;
	var mDecimales = decimales;

	function evSoloDinero() {
		SoloDineroOn(mObj, mMaxLength, mDecimales);
	}
	
	mObj.onfocus = evSoloDinero;
}

function FormatMoney(obj, max_length) {
	SoloDineroOn(obj, max_length, true);
}

function FormatCampoMoney(obj, max_length, decimales) {
	SoloDineroOn(obj, max_length + 3, decimales);
	SoloDineroOff();
}
