/* JavaScript Data Validation Utilities
 *	Author: Sam Tsvilik
 *	Company: ASPL
 *	Project: MotorScoop
 *
 */

//Checks if string contains only numeric cheracters
function isNumeric(value) {
    var thisText = new String(value);
    if(isEmpty(thisText)) {
		return false;
	}
    var len = thisText.length;
    for(var i=0; i<len; i++) {
        var rs = new RegExp("\\d");
        var chr = thisText.charAt(i);
        var it = chr.search(rs);
        if(it < 0) {
            return false;
        }
    }
    return true;
}

//Returns true if value is empty, ignoring spaces
function isEmpty(value) {
	var val = new String(value);
    if(parseInt(val.search("\\S")) == -1) {
        return true;
    } else {
        return false;
    }
}

function isFloat(value) {
var thisText = new String(value);
    if(isEmpty(thisText)) {
		return false;
	}
var decFound = 0;
var rs = new RegExp("\\d");
var numsOnly = true;

    var len = thisText.length;
    for(var i=0; i<len; i++) {
        var chr = thisText.charAt(i);
        var it = chr.search(rs);
        if(chr == ".") {
            decFound++;
        } else {
           if(it < 0) {
              numsOnly = false;
           }
        }
    }
    
    if(numsOnly && (decFound > 0 && decFound < 2)) {
    	return true;
    }
    
    return false;
}

/**
*	Currency object returns value converted to currency format
*/

function Currency(d) {
	//Public variables
		this.toString = toStr
		this.toDouble = toDbl
	
	//Private variables
		var initValue = new String(d);
		var wholeNumbers = "";
		var decimalNumbers = "";
		var combined
		
		main();
	//---------------------------------
	function main() {
		if(!initValue == "") {
			if(initValue.indexOf(".") > 0) {
				var nArray = initValue.split(".");
				wholeNumbers = nArray[0];
				decimalNumbers = nArray[1];
			} else {
				wholeNumbers = initValue;
			}
			adjustDecimal();
			adjustWhole();
			combined = wholeNumbers + "." + decimalNumbers;
		} else {
			combined = "NaN";
		}
	}
	
	//Places commas for every three digits in whole number
	function adjustWhole() {
		if(wholeNumbers.length > 3) {
			var tNum = new String("");
			var k=0;
			var p = parseInt(wholeNumbers.length-1);
			while(p>=0) {
				k++;
				tNum += wholeNumbers.charAt(p);
				if(k==3){tNum += ",";k=0}
				p--;
			}
			wholeNumbers = flipNumber(tNum);
		}
	}
	
	//Repositions numbers (horizontal flip) first becomes last
	function flipNumber(f) {
		var thisNum = "";
		var i=f.length-1;
		while(i>=0) {
			thisNum += f.charAt(i);
			i--;
		}
		if(thisNum.charAt(0) == ",") {
			thisNum = thisNum.substring(1);
		}
		return thisNum;
	}
	
	function adjustDecimal() {
		if(decimalNumbers.length > 0) {
			switch(decimalNumbers.length) {
				case 1: decimalNumbers += "0";
					break;
				case 2: break;
				default:
					decimalNumbers = roundDecimals();	
				break;
			}	
		} else {
			decimalNumbers = "00";
		}	
	}
		
	//Rounds decimals to hundreds
	function roundDecimals() {
		var oldC;
		var newC;
		var mDec = new String(decimalNumbers);
		var result;
		while(mDec.length != 2) {
				newC = mDec.charAt(mDec.length-2);
				oldC = mDec.charAt(mDec.length-1);
				mDec = mDec.substring(0,mDec.length - 2);
				mDec = mDec + Math.round(newC + "." + oldC);
			}
			result = mDec;
		return result;
	}
		
	function toStr() {
		return combined;
	}
		
	function toDbl() {
		return parseFloat(combined);
	}
		
}

function convertCurrency(d) {
	var result = new Currency(d).toString();
		
	return result;	
}

