﻿/**************************************************************************************************
 * Author           :   Nishanth S
 * Project          :   LoanModMeter
 * Module Name      :   General
 * Dated            :   13-Jun-2008
 * Modified on      :
**************************************************************************************************/

//=================================================================================================
// * Contains the java script functions required in the page
//=================================================================================================

//-----------------------------------------------------------------------------
//To calculate the sum of expenses
//called from within the expense calculator page
//-----------------------------------------------------------------------------
function CalculateSum(controlId)
{
    //initializes the total to 0
    var totalVal = 0.0;
    
    //get the value from the given control and remove spaces if any
    var val = controlId.value.replace(/ /g, '');
    
    //if the field is empty, write the default value to the field
    if(val == "")
    {        
        val = "0.00";
        controlId.value = "0.00"
    }
    //if the decimal portion is missing in the field value
    //add the decimal portion
    if(val.indexOf(".") < 0)
        controlId.value = controlId.value + ".00"   
    
    //calculate the total
    totalVal = GetTotal();
    
    var totalString = totalVal.toString();
    
    //if the decimal potion is missing in the total, add the decimal part
    if(totalString.indexOf(".") < 0)
        totalString = totalString + ".00";

    //replace the total in the page with the new total
    document.getElementById('lblTotalAmount').innerHTML =  totalString; 
}

//-----------------------------------------------------------------------------
//Calculates the total expenses (sum of the values in all the controls)
//Called from within the CalculateSum) function
//-----------------------------------------------------------------------------
function GetTotal()
{
    var total = parseFloat(document.getElementById('txtAutoExp').value);
    total += parseFloat(document.getElementById('txtCreditCard').value);
    total += parseFloat(document.getElementById('txtUtilities').value);
    total += parseFloat(document.getElementById('txtPersonalExp').value);
    total += parseFloat(document.getElementById('txtChildExp').value);
    total += parseFloat(document.getElementById('txtTransExp').value);//REV-0001
    total += parseFloat(document.getElementById('txtMedicalBills').value);//REV-0001
    total += parseFloat(document.getElementById('txtOtherExp').value);
    
    return total;
}

//-----------------------------------------------------------------------------
//Function to clear the dummy value ("0.00") from the given control
//called from within the expense calculator pages
//-----------------------------------------------------------------------------
function ClearDummy(controlId)
{    
    if(controlId.value == "0.00")
        controlId.value = "";
}

//-----------------------------------------------------------------------------
//To check whether the entered value (key) is a valid numeral
//used in the keypress event of controls which should allow only numeric values
//-----------------------------------------------------------------------------
function CheckForNumeral(e)
{
    //checks whether the entered key is a valid numeral
    //returns true if valid numeral and false if not
    var keynum;
    //alert(e.keyCode);
    if(window.event)
        keynum = e.keyCode;
    else if(e.which)
        keynum = e.which;
    keynum = keynum == null ? 48 : keynum;    
       var keychar = String.fromCharCode(keynum);
    var numcheck = /[\d.]/; 
    if(keynum==8) //Modified to accept backspace in Mozilla
    {
     return true;
    }
    else
    {
     return numcheck.test(keychar);
    }
}

//=============================================================================
//The below scripts are used for formatting the phone number text field
//using the format (XXX)XXX-XXXX
//=============================================================================
var reOneOrMoreDigits = /[\d+]/;
var reNoDigits = /[^\d]/gi;

function doMask(textBox) 
{
    var keyCode = event.which ? event.which : event.keyCode;
	// enter, backspace, delete and tab keys are allowed thru
	if(keyCode == 13 || keyCode == 8 || keyCode == 9 || keyCode == 46)
	{
		return true;
    }
	// get character from keyCode....dealing with the "Numeric KeyPad"
	// keyCodes so that it can be used
	var keyCharacter = cleanKeyCode(keyCode);
	// grab the textBox value and the mask

	var val = textBox.value;
	var mask = textBox.mask;

	// simple Regex to check if key is a digit
	if(reOneOrMoreDigits.test(keyCharacter) == false)
		return false;

	// get value minus any masking by removing all non-numerics
	val = val.replace(reNoDigits,'');			

	// add current keystroke
	val += keyCharacter;

	// mask it...val holds the existing TextBox.value + the current keystroke
	textBox.value = val.maskValue(mask);

	setCaretAtEnd(textBox);
	return false;
}

// puts starting chars in field
function onFocusMask(textBox) 
{
	var val = textBox.value;
	var mask = textBox.mask;
	
	if(val.length == 0 || val == null) 
	{
		var i = mask.indexOf('#');
		textBox.value = mask.substring(0,i);
	}

	setCaretAtEnd(textBox);

	// set just in case.
	textBox.maxlength = mask.length;
}

// blank field if no digits entered
function onBlurMask(textBox) 
{
	var val = textBox.value;
	
	// if no digits....nada entered.....blank it.
	if(reOneOrMoreDigits.test(val) == false) 
	{
		textBox.value = '';
	}
}

String.prototype.maskValue = function(mask) 
{
	var retVal = mask;
	var val = this;

	//loop thru mask and replace #'s with current value one at a time
	// better way of doing this ???
	for(var i=0;i<val.length;i++) 
	{
	    retVal = retVal.replace(/#/i, val.charAt(i));
	}

	// get rid of rest of #'s
	retVal = retVal.replace(/#/gi, "");

	return retVal;
}

// The Numeric KeyPad returns keyCodes that ain't all that workable.
// ie: KeyPad '1' returns keyCode 97 which String.fromCharCode converts to an 'a'.
// This cheesy way allows the Numeric KeyPad to be used
function cleanKeyCode(key)
{
	switch(key)
	{
		case 96: return "0"; break;
		case 97: return "1"; break;
		case 98: return "2"; break;
		case 99: return "3"; break;
		case 100: return "4"; break;
		case 101: return "5"; break;
		case 102: return "6"; break;
		case 103: return "7"; break;
		case 104: return "8"; break;
		case 105: return "9"; break;
		default: return String.fromCharCode(key); break;
	}

}


function setCaretAtEnd (field) 
{
    if (field.createTextRange) 
    {
        var r = field.createTextRange();
        r.moveStart('character', field.value.length);
        r.collapse();
        r.select();
    }
}


function HideCurrencyWatermark(control, defaultText)
{
    if(control.value == defaultText)
    {
        control.value = "";
    }
}

function ShowCurrencyWatermark(control, defaultText)
{
    if(control.value == "")
    {
        control.value = defaultText;
    }
    else
    {
        var val = control.value;        
        control.value = formatCurrency(val);
    }
}

function formatCurrency(num) 
{
    num = num.toString().replace(/,/g,'');
    
    if(isNaN(num))
        num = "0";
    
    var cents = "00";
    
    if(num.indexOf(".") != -1)
    {
        cents = num.substring(num.indexOf(".") + 1);        
        num = num.substring(0,num.indexOf("."));
    }
    
    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 num + "." + cents;
}

function HideRateWatermark(control, defaultText)
{
    if(control.value == defaultText)
    {
        control.value = "";
    }
}

function ShowRateWatermark( control, defaultText)
{
    if(control.value == "")
    {
        control.value = defaultText;
    }
    else
    {
        var val = control.value;        
        control.value = formatRate(val);
    }
}

function formatRate(num)
{
    num = num.toString().replace(/\%/g,"") + "%";
    
    return num;
}
