﻿// -()------------------------------------------------------------------------------------------------------------------
//
//    Name: currencyValidation.js
// Purpose: To provide any date validation
// History:	2009/03/16  GD      Created
//
//			* prototype string.trim() 
//          * validateCurrency
//			* jQuery 
//
// -)(------------------------------------------------------------------------------------------------------------------

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
}

// -<!>-----------------------------------------------------------------------------------------------------------------
//
// Name : validateCurrency
// Purpose: // Validates the a currency field locally.
//
// History: 2009/03/16  GD      (created)
//          2009/08/19  AH
//
// ->!<-----------------------------------------------------------------------------------------------------------------

function validateCurrency( value, minvalue, maxvalue )
{
	var valid =true;
	
	if (value !="" )
	{
		var strippedString="";
		// if there's something to check
		for (var c=0; c < value.length; c++ )
		{
			// scan through each char to see if it's one of the following ...
			var a = value.charCodeAt(c);
			// decimal point. everything after this is dumped
			if( a== 46){break;}
			// numbers only.
			if (a >=48 && a <=57){strippedString += String.fromCharCode(a);}
		}

		if (strippedString.length > 0) {
		    // convert to integer and set the valid flag if it falls inside the range.
		    var strippedData = parseInt(strippedString, 10);
		    valid = (strippedData >= minvalue) && (strippedData <= maxvalue);
		}
		else
		    valid = false; // all characters must have been invalid
	}
	return valid;
}

// -<!>-----------------------------------------------------------------------------------------------------------------
//
// Name : validateCurrencyInRange
// Purpose: Checks that a currency value is a valid number and falls within a given range.
//          If it isn't, the function will set the field to the specified reset value.
//
// History: 2009/08/20  AH      (created)
//
// ->!<-----------------------------------------------------------------------------------------------------------------

function validateCurrencyInRange(sender, value, minvalue, maxvalue, resetvalue) {
    if (value != "") {
        var strippedAmount = value.replace(/,/g, '');
        var isInvalid = false;
        if (isNaN(strippedAmount)) {
            isInvalid = true;
        }
        else {
            isInvalid = !(strippedAmount >= minvalue && strippedAmount <= maxvalue);
        }
        if (isInvalid) {
            // if value is invalid just reset it to the default value
            var amt = document.getElementById(sender.controltovalidate);
            amt.value = resetvalue;
        }
        return;
    }
}

// -<!>-----------------------------------------------------------------------------------------------------------------
//
// Name : validatePeriodAmountAndSecurity
// Purpose: Validates the a loan amount, period and residential status.
//
// History: 2009/04/15  LM  (created)
//
// ->!<-----------------------------------------------------------------------------------------------------------------

function validatePeriodAmountAndSecurity(value, minValue, maxValue, homeOwner, residentialStatus, loanTerm, minPeriod)
{
    var valid = true;

    if (validateCurrency(value, minValue, maxValue) &&  (homeOwner == residentialStatus) && (loanTerm >= minPeriod))
    {
        valid = false;
    }
    return valid;
}

// -<!>-----------------------------------------------------------------------------------------------------------------
//
// Name : formatCurrency
// Purpose: Inserts commas into the currency field
//
// History: 2009/12/07  LM  (created)
//
// ->!<-----------------------------------------------------------------------------------------------------------------
function formatCurrency(sv)
{
    var rs = "",
		rsd = "",
		dp = false;

    if (sv.length > 0)
    {
        // if ther's anything to reformat
        for (var c = 0; c < sv.length; c++)
        {
            // build a string of valid characters
            var a = sv.charCodeAt(c);
            // only the first decimal point is allowed. 
            if (a == 46 && !dp)
            {
                rsd += String.fromCharCode(a);
                dp = true;
            }
            // numbers only
            if ((c == 0 && a == 45) || (a >= 48 && a <= 57))
            {
                rsd += String.fromCharCode(a);
                if (!dp)
                {
                    rs += String.fromCharCode(a);
                }
            }
        }

        // convert into shortest form
        if (rsd.length > 0)
        {
            var rsN = parseInt(rs, 10),
				rsdN = parseFloat(rsd);

            if (rsdN > rsN)
            {
                rsN += 1;
            }
            rs = rsN.toString();

            // build regex to insert the thousands
            var rx = /(\d+)(\d{3})/;
            while (rx.test(rs))
            {
                rs = rs.replace(rx, '$1' + ',' + '$2');
            }
        }
    }

    return rs;
}

// -<!>-----------------------------------------------------------------------------------------------------------------
//
// Name : page level jQuery
// Purpose: jQuery function to to re-format the fields into a readable format n,nnn,nnn
//
// History: 2009/03/16  GD      (created)
//			2009/04/02	GD		added support for rounding UP. Ceiling effectively. 
//
// ->!<-----------------------------------------------------------------------------------------------------------------

function attachWholePounds()
{

    $(".wholepounds:not(.hasWholePoundsHandler)").blur(function() {
        var sv = $(this).val().trim();
        var title = $(this).attr("title");
    
        if (sv == title)
            return true;

        var rs = formatCurrency(sv);
        
        // drop the new value into the field
        $(this).val(rs);

        // and tell the browser all is well.
        return true;
    }).addClass("hasWholePoundsHandler");

}

jQuery(function($){

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
	if (prm.endRequest == null)
	{
		prm.add_endRequest(attachWholePounds);
    }
    attachWholePounds();

});

