﻿var DayControl = "DOB_Day"; var MonthControl = "DOB_Month"; var YearControl = "DOB_Year"; var LoanValue = "LoanValue"; var HomeValue = "HomeValue";
var arrowpress = false;var backpress = 0; var j = 0;     
function cv_DateofBirth_ClientValidate(source, arguments)
{
    var dateValid = true;

    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate.replace(YearControl,DayControl)))) ? dateValid : false ;
    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate.replace(YearControl,MonthControl)))) ? dateValid : false;
    dateValid = (checkValidDropDown('0',document.getElementById(source.controltovalidate))) ? dateValid : false;
    
     if(dateValid == true)
     {
        dateValid = IsValidDate(document.getElementById(source.controltovalidate.replace(YearControl,DayControl)).value,
                                document.getElementById(source.controltovalidate.replace(YearControl,MonthControl)).value,
                                document.getElementById(source.controltovalidate).value); 
     }
    arguments.IsValid = dateValid;
}

function IsValidDate(day,month,year){

    var eStatus = true;
    //Determine if the given year is a leap year i.e. Feb=29
    var LeapYear = IsLeapYear(year);
    
    //if leap year then Feb can be no more than 29 days
    if(LeapYear) {
        if(month == 'Feb') {
            if(day > 29) {
                eStatus = false;
            }
        }
    }
    //if not leap year then Feb can be no more than 28 days
    if(!LeapYear) {
         if(month == 'Feb') {
            if(day > 28) {
                eStatus = false;
            }
        }
    }
    
    //Check for Apr,Jun,Sep,Nov are no more than 30
    if((month == 'Apr') || (month == 'Jun') || (month == 'Sep') || (month == 'Nov')) {
        if(day > 30) {
            eStatus = false;
        }
    }
    return eStatus;
}

function IsLeapYear(Year) {
    if ((Year % 4 == 0) || (Year % 100 == 0) || (Year % 400 == 0)) {
        return true;
    }
    else {
        return false;
    }
}

function checkValidDropDown(initialValue,obj)
{
    if(obj.value == initialValue) {
        return false;
    }
    else {
        return true;
    }
}

function cv_Amount_ClientValidate(source, arguments)
{
    var RegularExpression = /^\-?[0-9]{1,3}(\,[0-9]{3})*(\.[0-9]+)?$|^[0-9]+(\.[0-9]+)?$/;
    var AmountValid = true;
    
    AmountValid = RegularExpression.test(arguments.Value);
    UnFormatNumber(document.getElementById(source.controltovalidate));
    if(AmountValid)
    {
        //If this is Loan Value and less than 15K
        if(source.controltovalidate.indexOf(LoanValue) > -1)
        {
            if(document.getElementById(source.controltovalidate).value < 25000)
                AmountValid = false;
        }
        
        //If this is Home Value and less than 25K
        if(source.controltovalidate.indexOf(HomeValue) > -1)
        {
            if(document.getElementById(source.controltovalidate).value < 25000)
                AmountValid = false;
        }
    }
    document.getElementById(source.controltovalidate).value = FormatNumber(document.getElementById(source.controltovalidate).value);
    arguments.IsValid = AmountValid;
}

function ValidateNumberKeyPress(field, evt)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode
    var keychar = String.fromCharCode(charCode);

    if (((charCode >= 65) && (charCode <= 90))||((charCode >= 106) && (charCode <= 222)) && (keychar != ".")  && (keychar != "-") )
    {
        return false;
    }

    if (keychar == "." && field.value.indexOf(".") != -1) 
    {
        return false;
    }
        
    if(keychar == "-")
    {
        if (field.value.indexOf("-") != -1 /* || field.value[0] == "-" */) 
        {
            return false;
        }
        else
        {
            //save caret position
            var caretPos = getCaretPosition(field);
            if(caretPos != 0)
            {
                return false;
            }
        }
    }
    if ((charCode != 37)&&(charCode != 39))
    {
     arrowpress = false;
    }
    else
    {
      arrowpress  = true;
    }
    return true;
}

function ValidateNumberKeyUp(field)
{
    //if(document.selection.type == "Text")
    //{
    //    return;
    //}

     //save caret position
     if(!arrowpress)
     {
        var caretPos = getCaretPosition(field);
     
        var fdlen = field.value.length;

        UnFormatNumber(field);

        var IsFound = /^-?\d+\.{0,1}\d*$/.test(field.value);
        if(!IsFound)
        {
            setSelectionRange(field, caretPos, caretPos);
            return false;             
        }
        
        field.value = FormatNumber(field.value);
        
        fdlen = field.value.length - fdlen ;
        
        setSelectionRange(field, caretPos+fdlen, caretPos+fdlen);
    }
}

function ValidateAndFormatNumber(NumberTextBox)
{
    if(NumberTextBox.value == "") return;
    
    UnFormatNumber(NumberTextBox);

    var IsFound = /^-?\d+\.{0,1}\d*$/.test(NumberTextBox.value);
    if(!IsFound)
    {
        alert("Not a number");
        NumberTextBox.focus();
        NumberTextBox.select();  
        return;             
}
    
    if(isNaN(parseFloat(NumberTextBox.value)))
    {
        alert("Number exceeding float range");
        NumberTextBox.focus();
        NumberTextBox.select();               
    }

    NumberTextBox.value = FormatNumber(NumberTextBox.value);
}

function FormatNumber(fnum)
{
    var orgfnum = fnum;
    var flagneg = false;
    
    if(fnum.charAt(0) == "-")
    {
        flagneg = true;
        fnum = fnum.substr(1, fnum.length-1);
    }
    
    psplit = fnum.split(".");

    var cnum = psplit[0],
        parr = [],
        j = cnum.length,
        m = Math.floor(j / 3),
        n = cnum.length % 3 || 3;

    // break the number into chunks of 3 digits; first chunk may be less than 3
    for (var i = 0; i < j; i += n) {
        if (i != 0) {n = 3;}
        parr[parr.length] = cnum.substr(i, n);
        m -= 1;
    }

    // put chunks back together, separated by comma
    fnum = parr.join(",");

    // add the precision back in
    //if (psplit[1]) {fnum += "." + psplit[1];}
    if (orgfnum.indexOf(".") != -1)  
    {
        fnum += "." + psplit[1];
    }
    
    if(flagneg == true)
    {
        fnum = "-" + fnum;
    }
    
    return fnum;
}
   
function UnFormatNumber(obj)
{
    if(obj.value == "") return;
    
    obj.value = obj.value.replace(/,/gi, "");
}

function getCaretPosition(objTextBox){

    /* var objTextBox = window.event.srcElement; */

    var i = objTextBox.value.length;

    if (objTextBox.createTextRange){
        objCaret = document.selection.createRange().duplicate();
        while (objCaret.parentElement()==objTextBox &&
          objCaret.move("character",1)==1) --i;
    }
    return i;
}

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function LTVError()
{
    alert('Loan Value cannot be more than Home Value !');
}