﻿/* Calculator Functions */

var cashNeeded = null;
var availableHomeEquity = null;

function ValidateCashAmount(event, args) {
    if (args.Value != null && args.Value != "" && availableHomeEquity != null &&
        (availableHomeEquity < parseFloat(args.Value.replace("$", "").replace(",", "")))) {
        
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}

function UpdateHomeEquity(currentBalanceTextBoxID, currentValueTextBoxID, ValidatorID) {
    var currentBalance = document.getElementById(currentBalanceTextBoxID).value.replace("$", "").replace(",", "");
    var currentValue = document.getElementById(currentValueTextBoxID).value.replace("$", "").replace(",", "");  
    
    if (currentBalance != null && currentBalance != "" && !isNaN(currentBalance) &&
        currentValue != null && currentValue != "" && !isNaN(currentValue)) 
    {   
        availableHomeEquity = parseFloat(currentValue) - parseFloat(currentBalance);
        ValidatorValidate(document.getElementById(ValidatorID));
    }
}

/*
    Set mybox's value to the sum of the values in all the textboxes with the IDs provided 
    Call like: SetTextboxValueToSum('myBox', 'ID1', 'ID2', 'ID3')
*/
function setTextboxValueToSum(textboxID) {
    var textbox = document.getElementById(textboxID);
    var sum = 0;
    
    for (var i = 1; i < arguments.length; i++) {
        var sumBox = document.getElementById(arguments[i]);
        var sumBoxValue = parseFloat(sumBox.value.replace("$", "").replace(",", ""));

        if (sumBoxValue != null && sumBoxValue != "" && !isNaN(sumBoxValue)) {
            sum += sumBoxValue;
        }
    }
    
    textbox.value = formatCurrency(sum);
}

// Function from the original LendingTree calculators with mods to work in FireFox
// Fired onkeypress in textboxes to add commas every third digit
function addCommasNoSpecialCharacters(oObj, e){
    var KeyID = e.keyCode? e.keyCode : e.charCode

    if (KeyID == 9 || KeyID == 16)
        return oObj.value;
	
	try{
		var sValue = oObj.value;
		 
		var sNewVal = '';
				
		//===[ m.squires - GS-6907 Checking for a decimal point and displaying alert to
		//===				interrupt user input to prevent them from type the rest of the decimal value
		//===				This interruption prevent a the user from type the value 1.11, which would 
		//===				show up as 111, without the interruption

		sValue = sValue.replace(/[^0-9,.]/g,"");	//remove non numeric values - a two digit decimal value is allowed
		
		
		//Remove Leading Zeros, If more than 1
		if (oObj.value.length > 1) {
			while (oObj.value.indexOf("0") == 0){
				sValue = sValue.replace('0',"")
				break;
			}
		}
		if (oObj.value.length > 1) {
			for (idx=1; idx < oObj.maxLength; idx++){
				if(oObj.value.charAt(idx) == "0" && oObj.value.charAt(0) == "0"){
					sValue = sValue.replace(oObj.value.charAt(idx),"")
				}
				else break;
			}

			if(oObj.value.charAt(0) == "0" && oObj.value.charAt(1) != "0") {
				sValue = sValue.replace(oObj.value.charAt(0),"")
			}           
		}
		
		// attempting to remove extra characters that may have slipped in.
		if (oObj.maxLength && oObj.maxLength > 0){
			if (sValue.length > parseInt(oObj.maxLength)){ 
				sValue = sValue.substr(0,oObj.maxLength-(parseInt(oObj.maxLength/3))) //MaxLength must = 4, 7 or 10 
			}
		}
		
		index = 0
		compVal = ""
		decVal = ""
		
		// Set the value for compVal		
		while ((index < sValue.length) && (oObj.value.charAt(index) != ".")){
				
				if (oObj.value.charAt(index) != ","){
				    compVal = compVal + sValue.substr(index,1);
				}
				index = index + 1;
			
		}
		
		// Set the value for decVal
		if (oObj.value.charAt(index) == "."){
		    
		        decVal = oObj.value.charAt(index) + oObj.value.charAt(index+1) + oObj.value.charAt(index+2);
		      
		}		      
					
		if(compVal > 99){
		    
			var nCommas = parseInt((compVal.length / 3))
			
			if ((compVal.length % 3) == 0){
				//subtract extra comma that can occur if modulus = 0 
				--nCommas
			}	
						
			if (nCommas > 0){

					for (n = nCommas; n > 0; n--){		//add comma(s)
						sNewVal =  sNewVal + ',' + compVal.substr((compVal.length-(n*3)),3);
					}//add remaining
				    
					sNewVal =  compVal.substr(0,((compVal.length-(nCommas*3)) / 1)) + sNewVal;
					
					sNewVal = sNewVal + decVal;
					oObj.value = sNewVal;
				}
			else{
					oObj.value = compVal + decVal;
			}		
				
		}
		else{
			oObj.value = sValue;
		}   
			return (oObj.value);
	}
	catch(exception){
		//do nothing
	}
}	

// Function from the original LendingTree calculators with mods to work in FireFox
// Return the number formatted to have a dollar sign and two decimal places (cents)
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num))
	    num = "0";
				
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
				
	if(cents < 10)
	    cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1 + i)) / 3); i++)
		num = num.substring(0, num.length-(4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	
	return ('$' + ((sign) ? '' : '-') + num + '.' + cents);
}

// Function from the original LendingTree calculators with mods to work in FireFox
// Return the specified number formated to have three decimal places.
function formatPercentageDecimal(num)
{
	try
	{
	    if (isNaN(num) || num == "" || num == null) {
	        return "0.000";
	    }
	    else {
	        var sValue = num;
	        sValue = parseFloat(sValue);
	        sValue = sValue.toFixed(3);
	        num = sValue;
	        return (num);
	    }
	}
	catch(exception)
	{
		//do nothing
	}
}

// Return the specified number formatted to have two decimal places
// Used to format discount points onblur
function formatPointsDecimal(num) {
    try
	{
	    if (num == null || isNaN(num) || num == "") {
	        return "0.00";
	    }
	    else {
	        var sValue = num;
	        sValue = parseFloat(sValue);
	        sValue = sValue.toFixed(2);
	        num = sValue;
	        return (num);
	    }
	}
	catch(exception)
	{
		//do nothing
	}
}

/* End Calculator Functions */

function HideDivs() {
    for (var i = 0; i < arguments.length; i++) {
        var theDiv = document.getElementById(arguments[i]);
    
        theDiv.style.display = "none";
        theDiv.style.height = "0px";
    }
}

function ShowDivs() {
    for (var i = 0; i < arguments.length; i++) {
        var theDiv = document.getElementById(arguments[i]);
    
        theDiv.style.display = "inline";
        theDiv.style.height = "";
    }
}

function ToggleDiv(divID) {
    theDiv = document.getElementById(divID);
    
    if (theDiv.style.display == "none") {
        theDiv.style.display = "inline";
    }
    else {
        theDiv.style.display = "none";
        theDiv.style.height = "0px";
    }
}

