﻿// JScript File for all methods for handling the Numeric only text box, Grid Pager textbox and 
//Multi line textbox control for handling maxlength problem

//*************************** NUMERIC TEXTBOX *******************************************
//Some functions used in all functions also but primararily for numeric textbox and grid pager

var IsPaste; // This variable is used for the firefox browser to identify if a paste operation is done or not.
// If Paste is done then the keyup event would be used to handle it.
var IsSelected; //This variable is used for IE when some text is selected within the textbox and then paste is done
var nDotCount = 0; //This variable is used to keep the count of dots(.) in the Numeric text box which allows decimal point

//This function is made for checking that there cant be more than one decimal points in Numeric Text box with decimals
function checkForDecimalPoint(sText) {
    if (sText == ".") {
        if (nDotCount == 0) {
            nDotCount = nDotCount + 1;
            return 0;
        }
        else {
            return 2;
        }
    }

    return 0;
}

//This function is made to identify if there is a "." present in the textbox or not
function identifyDot(stringToCheck) {
    var d = 0;
    var dotPresent = false;
    for (d = 0; d < stringToCheck.length; d++) {
        //alert('this' + stringToCheck.charAt(d));
        if (stringToCheck.charAt(d) == '.') {
            dotPresent = true;
        }
    }
    return dotPresent;
}

//This function is used to identify the numeric only characters
function IsNumeric(sText, entryMode) {
    var ValidChars;
    if (entryMode == 0) //Numeric Entries
    {
        ValidChars = "0123456789";
    }
    else if (entryMode == 1)//Numeric Entries with Decimal Point
    {
        ValidChars = "0123456789.";
    }
    else if (entryMode == 2)//Numeric Entries with Spaces for Phone numbers
    {
        ValidChars = "0123456789 ";
    }
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

//This function parses a given string and returns only the numeric characters from that string
function stripChars(string, entryMode) {
    var onlyNumbers = "";
    var len = string.length;
    var strarr = string.split('');
    var localDotCount = 0;
    var j = 0;
    for (j = 0; j < len; j++) {
        if ((IsNumeric(strarr[j], entryMode)) == true) {
            if (entryMode == 1) {
                if (strarr[j] == '.' && localDotCount == 0 && nDotCount != 1) {
                    onlyNumbers = onlyNumbers + strarr[j];
                    localDotCount = localDotCount + 1;
                    nDotCount = 1;
                }
                else if (strarr[j] != '.') {
                    onlyNumbers = onlyNumbers + strarr[j];
                }
            }
            else {
                onlyNumbers = onlyNumbers + strarr[j];
            }

        }
    }
    return (onlyNumbers);
}

//Function which returns only the maximum allowed number of characters for the given string
function checkMaxLength(actualstring, maxlength) {

    if ((maxlength > 0) && (maxlength < actualstring.length)) {
        var actualstringarr = actualstring.split('');
        var returnstring = "";
        var k = 0;
        for (k = 0; k < maxlength; k++) {
            returnstring = returnstring + actualstringarr[k];
        }
        return (returnstring);
    }
    else {
        return (actualstring);
    }
}

//This function is made to identify at what position the "." is present in the string
function identifyDotPosition(stringToCheck) {
    var d = 0;
    var dotPosition = 0;
    for (d = 0; d < stringToCheck.length; d++) {
        //alert('this' + stringToCheck.charAt(d));
        if (stringToCheck.charAt(d) == '.') {
            dotPosition = d;
        }
    }
    return dotPosition;
}

//This function is made for managing the string, either string manipulations or rounding off of extra decimals
//---------NOT USED AT THE MOMENT------------
function modifyTextString(stringValue, op, decimalPlaces, position) {
    if (op == 0) //Case where "." is at the last and it is to be removed
    {
        var r = 0;
        var stringValueArr = stringValue.split('');
        var returnString = "";
        for (r = 0; r < stringValue.length - 1; r++) {
            returnString = returnString + stringValueArr[r];
        }
        return returnString;
    }
    else if (op == 1) //Case where the numbers of decimal places after "." is greater than specified
    {
        //var r = 0;
        //var lastIndex = position + decimalPlaces + 1;
        //stringValue = stringValue.substring(0,(lastIndex+1))
        ////alert(stringValue);
        //var stringValueArr = stringValue.split('');
        //var returnString = "";
        //for(r =0; r < stringValue.length - 1; r++)
        //{
        //		if(r == (stringValue.length - 2))
        //		{
        //			//alert(stringValueArr[r+1]);
        //			if(stringValueArr[r+1] >= 5 && stringValueArr[r] != 9)
        //			{
        //				stringValueArr[r-1] = stringValueArr[r-1];
        //				stringValueArr[r] = eval(stringValueArr[r]) + 1;
        //			}
        //			else if(stringValueArr[r+1] >= 5 && stringValueArr[r] == 9)
        //			{
        //				stringValueArr[r-1] = eval(stringValueArr[r-1]) + 1;
        //				stringValueArr[r] = 0;
        //			}
        //			returnString = returnString + stringValueArr[r-1] + stringValueArr[r];
        //		}
        //		else if(r < (stringValue.length - 3))
        //		{
        //			returnString = returnString + stringValueArr[r];
        //		}
        //}
        returnString = Math.round(stringValue * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
        return returnString;
    }
}

//This function is made to check on the decimal places validation. This round offs the decimal places as per the decimals mentioned.
//---------NOT USED AT THE MOMENT------------
function decimalPlacesValidation(textbox, decimalPlaces) {
    //alert(textbox.value.length);
    //alert(identifyDotPosition(textbox.value));
    if (identifyDotPosition(textbox.value) == (textbox.value.length - 1)) {
        textbox.value = modifyTextString(textbox.value, 0, decimalPlaces, identifyDotPosition(textbox.value));
        //alert(textbox.value);
    }
    else if (((textbox.value.length - 1) - identifyDotPosition(textbox.value)) > decimalPlaces) {
        textbox.value = modifyTextString(textbox.value, 1, decimalPlaces, identifyDotPosition(textbox.value));
    }
}

//This function is used on blur. This is a final line of protection and any invalid (non-numeric) entry would
//be removed from the textbox by using this function.
function validate(textbox, entryMode, decimalPlaces) {
    //alert(decimalPlaces);
    nDotCount = 0;
    var i = 0;
    var appendZerosString;
    textbox.value = checkMaxLength(stripChars(textbox.value, entryMode), textbox.maxLength);
    if (entryMode == 1 && identifyDot(textbox.value) == true) {
        //decimalPlacesValidation(textbox,decimalPlaces);
        textbox.value = Math.round(textbox.value * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
        //Code added for making the decimal places stay in cases of zero values.
        if (identifyDot(textbox.value) == false) {
            //textbox.value = textbox.value + ".00";
            if (decimalPlaces > 0)
                appendZerosString = ".";
            else
                appendZerosString = "";
            for (i = 0; i < decimalPlaces; i++) {
                appendZerosString = appendZerosString + "0";
            }
            //P//textbox.value = textbox.value + appendZerosString;
        }
        else {
            //alert(textbox.value.length);
            //alert((textbox.value.length - 1) - identifyDotPosition(textbox.value));
            if (((textbox.value.length - 1) - identifyDotPosition(textbox.value)) < decimalPlaces) {
                appendZerosString = ""
                for (i = ((textbox.value.length - 1) - identifyDotPosition(textbox.value)); i < decimalPlaces; i++) {
                    appendZerosString = appendZerosString + "0";
                }
                //P//textbox.value = textbox.value + appendZerosString;
            }
        }
    }
    else if (entryMode == 1 && identifyDot(textbox.value) == false) {
        textbox.value = Math.round(textbox.value * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
        if (decimalPlaces > 0)
            appendZerosString = ".";
        else
            appendZerosString = "";
        for (i = 0; i < decimalPlaces; i++) {
            appendZerosString = appendZerosString + "0";
        }
        //textbox.value = textbox.value + appendZerosString;
    }
}

function validateDecimal(textbox, entryMode, decimalPlaces) {
    //alert(decimalPlaces);
    //debugger;
    nDotCount = 0;
    var i = 0;
    var appendZerosString;     
    textbox.value = checkMaxLength(stripChars(textbox.value, entryMode), textbox.maxLength);    
    if (entryMode == 1 && identifyDot(textbox.value) == true) {
        //decimalPlacesValidation(textbox,decimalPlaces);
        if (textbox.value == ".") textbox.value = "0.00";
        textbox.value = Math.round(textbox.value * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
        //Code added for making the decimal places stay in cases of zero values.
        if (identifyDot(textbox.value) == false) {
            //textbox.value = textbox.value + ".00";
            if (decimalPlaces > 0)
                appendZerosString = ".";
            else
                appendZerosString = "";
            for (i = 0; i < decimalPlaces; i++) {
                appendZerosString = appendZerosString + "0";
            }
            var tmpApp = textbox.value + appendZerosString;
            if (parseInt(tmpApp.length) <= parseInt(textbox.maxLength))
                textbox.value = textbox.value + appendZerosString;
            //tmpApp = textbox.value + appendZerosString;            
            //textbox.value = textbox.value + appendZerosString;
        }
        else {
            //alert(textbox.value.length);
            //alert((textbox.value.length - 1) - identifyDotPosition(textbox.value));
            if (((textbox.value.length - 1) - identifyDotPosition(textbox.value)) < decimalPlaces) {
                appendZerosString = ""
                for (i = ((textbox.value.length - 1) - identifyDotPosition(textbox.value)); i < decimalPlaces; i++) {
                    appendZerosString = appendZerosString + "0";
                }
                var tmpApp = textbox.value + appendZerosString;
                if (parseInt(tmpApp.length) <= parseInt(textbox.maxLength))
                    textbox.value = textbox.value + appendZerosString;
                //tmpApp = textbox.value + appendZerosString;           
                //textbox.value = textbox.value + appendZerosString;
            }
        }
    }
    else if (entryMode == 1 && identifyDot(textbox.value) == false) {
        textbox.value = Math.round(textbox.value * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
        if (decimalPlaces > 0)
            appendZerosString = ".";
        else
            appendZerosString = "";
        for (i = 0; i < decimalPlaces; i++) {
            appendZerosString = appendZerosString + "0";
        }
        var tmpApp = textbox.value + appendZerosString;
        if (parseInt(tmpApp.length) <= parseInt(textbox.maxLength))
            textbox.value = textbox.value + appendZerosString;
        //tmpApp = textbox.value + appendZerosString;
    }

//    if (parseInt(tmpApp.length) <= parseInt(textbox.maxLength))
//        textbox.value = textbox.value + appendZerosString;
}

//This function is used in the keyup event. 
function pasteHandlerKeyup(textbox, e, entryMode) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (IsSelected) {
            nDotCount = 0;
            var pastestring = checkMaxLength(stripChars(textbox.value, entryMode), textbox.maxLength);
            textbox.value = pastestring;
            IsSelected = false;
        }
        if (e.keyCode == 8 || e.keyCode == 46) // Code written for resetting the dot counter to 0 on delete of "."
        {
            if (!identifyDot(textbox.value)) {
                nDotCount = 0;
            }
        }
        if (textbox.value == "") {
            nDotCount = 0;
        }
    }
    else {

        if ((e.type == 'keyup') && IsPaste) {
            nDotCount = 0;
            var pastestring = checkMaxLength(stripChars(textbox.value, entryMode), textbox.maxLength);
            textbox.value = pastestring;
            IsPaste = false;
        }
        if (e.which == 8 || e.which == 46) // Code written for resetting the dot counter to 0 on delete of "."
        {
            if (!identifyDot(textbox.value)) {
                nDotCount = 0;
            }
        }
        if (textbox.value == "") {
            nDotCount = 0;
        }

    }
}

//This function is used by the keypress as well as onpaste events.
function pasteHandler(textbox, e, entryMode) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (e.keyCode == 13) {
            return true;
        }
        if (e.type == 'paste') {
            var seltext = getSelText();
            var alltext = textbox.value;
            if (seltext != '') {
                IsSelected = true;
                return true;
            }
            else {
                //Old code -DO NOT REMOVE FOR NOW - HARSH
                //var pastestring = stripChars(window.clipboardData.getData("Text"));
                //var newstring = textbox.value + pastestring;
                //newstring = checkMaxLength(newstring,textbox.maxLength);
                //textbox.value = newstring;
                //return false;
                if (!isNaN(textbox.maxLength)) {
                    event.returnValue = false;
                    maxLength = parseInt(textbox.maxLength);
                    var oTR = document.selection.createRange();
                    var iInsertLength = textbox.maxLength - textbox.value.length + oTR.text.length;
                    var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
                    //oTR.text = sData; 'Change on 25th January 2007 by harsh
                    oTR.text = stripChars(sData, entryMode);
                }
            }

        }
        var iKeyValue;
        iKeyValue = String.fromCharCode(e.keyCode);
        //Below If condition added for checking so that not more than one "." is allowed
        //alert(identifyDot(getSelText()));
        if (identifyDot(getSelText()) == true) {
            nDotCount = 0;
        }
        if (checkForDecimalPoint(iKeyValue) > 1 && identifyDot(getSelText()) != true) {
            return false;
        }
        return (IsNumeric(iKeyValue, entryMode));
    }
    else {
        if ((e.type == 'keypress') && !(e.ctrlKey && e.which == 118)) {
            if ((e.which == 0) || (e.which == 8) || (e.which == 13)) {
                return true;
            }
            else {
                var iKeyValue;
                iKeyValue = String.fromCharCode(e.which);
                //Change done on 30th Jan 2007. Condition added.
                if (iKeyValue == '.' && textbox.value.indexOf('.') >= 0) {
                    return false;
                }
                //                if (checkForDecimalPoint(iKeyValue) > 1) {
                //                    return false;
                //                }
                return (IsNumeric(iKeyValue, entryMode));
            }
        }
        else if ((e.type == 'keypress') && (e.ctrlKey && e.which == 118)) {
            IsPaste = true;
        }
    }
}

//This function is used to get the selected text from within the textbox if any.
function getSelText() {
    var txt = '';
    if (window.getSelection) {
        txt = window.getSelection();
    }
    else if (document.getSelection) {
        txt = document.getSelection();
    }
    else if (document.selection) {
        txt = document.selection.createRange().text;
    }
    else return;
    return txt;
}

//*************************** GRID PAGER TEXTBOX *******************************************
//Function made for Grid Pager Validation. Arguments passed are textbox to validate, position (0-bottom, 1 - top)
//grd for the id of the grid, and pagecount for the total number of pages.

var IsErrorBottom; //This variable is used to check whether an erroneous entry was made into the textbox in bottom pager 
// or not.
// This would be used when the of button would be clicked.
var IsErrorTop;    //This variable is used to check whether an erroneous entry was made into the textbox in top pager 
// or not.
// This would be used when the of button would be clicked.                   

function validateGridPager(textbox, position, grd, pagecount) {
    textbox.value = stripChars(textbox.value, 0);

    if ((textbox.value == '') || !(textbox.value > 0 && textbox.value <= pagecount)) {
        var gridname = grd;
        if (position == 0) {
            var lblstring = 'lblerrormsgForGridbottom' + gridname;
            var lbl = eval(document.getElementById(lblstring));
            lbl.innerHTML = "Invalid page no.";
            IsErrorBottom = true;
        }
        else {
            var lblstring = 'lblerrormsgForGridtop' + gridname
            var lbl = eval(document.getElementById(lblstring));
            lbl.innerHTML = "Invalid page no.";
            IsErrorTop = true;
        }

    }
    else {
        var gridname = grd;
        if (position == 0) {
            var lblstring = 'lblerrormsgForGridbottom' + gridname;
            var lbl = eval(document.getElementById(lblstring));
            lbl.innerHTML = "";
            IsErrorBottom = false;
        }
        else {
            var lblstring = 'lblerrormsgForGridtop' + gridname
            var lbl = eval(document.getElementById(lblstring));
            lbl.innerHTML = "";
            IsErrorTop = false;
        }
    }
}


//Function for checking if error exists or not on clicking the "Of" button in grid pager
//This determines that if error exists then form is not to be posted.
function checkError(position, gridname) {
    if (position == 0) {
        if (IsErrorBottom) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        if (IsErrorTop) {
            return false;
        }
        else {
            return true;
        }
    }
}

//*************************** MULTILINE TEXTBOX WITH MAXLENGTH CHECK ****************************************

//Multi Line textbox checking
//This function is used on blur and onkeypress. This function is used for checking the maxlength of the 
//Multi line textbox control.

var IsMultiLinePaste; //Used to check whether the paste was done or not (Firefox)
var oldTextValue = '';     //This is used to store the string value before the paste operation was done (Firefox)
var startPos;         //This is used to store the start position of the selection (Firefox)
var endPos;           //This is used to store the end position of the selection (Firefox)
var enterCount = 0;   //This variable is used for storing the count of enter key presses in textbox in IE.
//This is needed because in IE for each enter key it stores 2 characters.
var newMaxLength = 0; //This variable is used for storing the maxlength along with the enters
var keyPressTextValue = ''; // Variable used in keydown event for getting the value
var keyDownSelText = ''; // For knowing whether selected text is there or not
var pasteStartPos;
var pasteEndPos;


//Function which handles the keypress events for the textbox, disallowing extra number of characters while typing
//Function also used to find out whether paste is done in firefox or not
function keyPressHandler(textbox, e, maxlength) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (maxlength > textbox.value.length) {
            return true;
        }
        else {
            selText = getSelText();
            if (selText.length > 1 && e.keyCode == 13) {
                return true;
            }
            else if (selText.length > 0 && e.keyCode != 13) {
                return true;
            }
            else {
                return false;
            }
        }
    }
    else {
        var charLengthFF = 0;
        var lastKeyEnter = false;
        charLengthFF = getCharCountFF(textbox.value, maxlength);
        var charLengthFFOld = charLengthFF;
        if ((e.which == 8 || e.which == 0) && keyPressTextValue == '') {
            keyPressTextValue = textbox.value;
        }
        if (e.which == 13) {
            lastKeyEnter = true;
            charLengthFF = charLengthFF + 1;
        }
        if (maxlength > charLengthFF) {
            if ((e.ctrlKey && e.which == 118)) {
                oldTextValue = textbox.value;
                startPos = textbox.selectionStart;
                endPos = textbox.selectionEnd;
                IsMultiLinePaste = true;
            }

            return true;
        }
        else {
            if ((e.ctrlKey && e.which == 118)) {
                IsMultiLinePaste = true;
                oldTextValue = textbox.value;
                startPos = textbox.selectionStart;
                endPos = textbox.selectionEnd;
                return true;
            }
            if ((e.ctrlKey && e.which == 97)) {
                return true;
            }
            else if (e.which == 0 || e.which == 8) {
                return true;
            }
            else {
                var localStartPos = textbox.selectionStart;
                var localEndPos = textbox.selectionEnd;
                var selectionRange = localEndPos - localStartPos;
                if ((selectionRange > 1) && (lastKeyEnter)) {
                    return true;
                }
                else if (selectionRange > 0 && !(lastKeyEnter)) {
                    return true;
                }
                return false;
            }
        }
    }
}

function getCharCountFF(stringToCheck, maxLength) {
    var l = 0;
    var localEnterCount = 0;
    var nonEnterChars = 0;
    var validChars = 0;
    for (l = 0; l < stringToCheck.length; l++) {
        if (stringToCheck.charCodeAt(l) == 10) {
            localEnterCount = localEnterCount + 1;
        }
        nonEnterChars = nonEnterChars + 1;
    }
    validChars = nonEnterChars + localEnterCount;
    return validChars;
}

function identifyEnter(stringToCheck, maxLength) {
    var l = 0;
    var localEnterCount = 0;
    for (l = 0; l < stringToCheck.length; l++) {
        if (stringToCheck.charCodeAt(l) == 10) {
            localEnterCount = localEnterCount + 1;
        }
    }
    return localEnterCount;
}

function setMaxLengthforPaste(stringToCheck, maxLength) {
    var m = 0;
    var stringToCheckArr = stringToCheck.split('');
    var returnString = '';
    var charCount = 0;
    for (m = 0; m < stringToCheck.length; m++) {
        returnString = returnString + stringToCheckArr[m];
        if (stringToCheck.charCodeAt(m) == 13) {
            newMaxLength = newMaxLength + 1;
        }
        else {
            charCount = charCount + 1;
        }
        if (charCount == maxLength) {
            return returnString;
        }
    }
}

function getValidCharsWithEnter(stringToCheck, maxLength) {
    var m = 0;
    var stringToCheckArr = stringToCheck.split('');
    var returnString = '';
    var charCount = 0;
    var enterCount = 0;
    for (m = 0; m < stringToCheck.length; m++) {

        if (stringToCheck.charCodeAt(m) == 10 || stringToCheck.charCodeAt(m) == 13) {
            enterCount = enterCount + 2;
        }
        else if ((stringToCheck.charCodeAt(m) != 10) && (stringToCheck.charCodeAt(m) != 13)) {
            charCount = charCount + 1;
        }
        if ((charCount + enterCount) > maxLength) {
            break;
        }
        else {
            returnString = returnString + stringToCheckArr[m];
        }
    }
    return returnString;
}

//KeyDown Event function 
function setOldValue(textbox) {
    if (!(navigator.appName == "Microsoft Internet Explorer")) {
        pasteStartPos = textbox.selectionStart;
        pasteEndPos = textbox.selectionEnd;
        return true;
    }
}

//This function is used for firefox to handle the paste event and in IE for enter calculations
function keyUpMultiLine(textbox, e, maxLength) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        //document.getElementById('mylabel').innerHTML = textbox.value.length;
    }
    else {
        var newEnterCount = identifyEnter(textbox.value, maxLength);
        var oldEnterCount = identifyEnter(keyPressTextValue, maxLength);
        if (e.which == 8 || e.which == 46) {
            if (oldEnterCount != newEnterCount) {
                newMaxLength = newMaxLength - (oldEnterCount - newEnterCount);

            }
            if ((textbox.value == '') || (newMaxLength < 0)) {
                newMaxLength = 0; //maxLength;
            }
            keyPressTextValue = '';
        }
        //var lbl = eval(document.getElementById('mylabel'));
        //lbl.innerHTML = textbox.value.length;
        if ((maxLength < getCharCountFF(oldTextValue, maxLength)) && IsMultiLinePaste) {
            if (((endPos - startPos) > 0) && ((endPos - startPos) < maxLength)) {
                var newString = textbox.value;
                var noOfCharsString = oldTextValue.substring(startPos, endPos);
                var diff = getCharCountFF(noOfCharsString, maxLength);
                noOfChars = (diff) + (maxLength - getCharCountFF(oldTextValue, maxLength));
                var firstPart = oldTextValue.substring(0, startPos);
                var lastPart = oldTextValue.substring(endPos, getCharCountFF(oldTextValue, maxLength));
                var newLastPart = newString.substring(startPos, getCharCountFF(newString, maxLength));
                var pasteString = newLastPart.substring(0, noOfChars);
                textbox.value = firstPart + pasteString + lastPart;
                textbox.value = getValidCharsWithEnter(textbox.value, maxLength);
            }
            else {
                textbox.value = checkMaxLength(textbox.value, maxLength);
                textbox.value = getValidCharsWithEnter(textbox.value, maxLength);
            }
            IsMultiLinePaste = false;
            return false;
        }
        else if (IsMultiLinePaste) {
            var noOfChars;
            var newString = textbox.value;
            if (startPos != endPos) {
                var noOfCharsString = oldTextValue.substring(startPos, endPos);
                var diff = getCharCountFF(noOfCharsString, maxLength);
                noOfChars = (diff) + (maxLength - getCharCountFF(oldTextValue, maxLength));
                var firstPart = oldTextValue.substring(0, startPos);
                var newFirstPart = newString.substring(0, startPos);
                var lastPart = oldTextValue.substring(endPos, getCharCountFF(oldTextValue, maxLength));
                var newLastPart = newString.substring(startPos, getCharCountFF(newString, maxLength));
            }
            else {
                noOfChars = (maxLength - getCharCountFF(oldTextValue, maxLength));
                var firstPart = oldTextValue.substring(0, startPos);
                var newFirstPart = newString.substring(0, startPos);
                var lastPart = oldTextValue.substring(endPos, getCharCountFF(oldTextValue, maxLength));
                var newLastPart = newString.substring(endPos, getCharCountFF(newString, maxLength));
            }
            var pasteString = newLastPart.substring(0, noOfChars);

            textbox.value = firstPart + pasteString + lastPart;
            textbox.value = getValidCharsWithEnter(textbox.value, maxLength);
            newMaxLength = identifyEnter(textbox.value, maxLength);
            IsMultiLinePaste = false;
            return false;

        }
    }
}

function checkMultiLineMaxLength(actualstring, maxlength) {

    if ((maxlength > 0) && (maxlength < actualstring.length)) {
        var actualstringarr = actualstring.split('');
        var returnstring = "";
        var charCount = 0;
        var enterCount = 0;
        var k = 0;
        for (k = 0; k < maxlength; k++) {
            if (actualstring.charCodeAt(k) == 13) {
                enterCount = enterCount + 2;
            }
            else if ((actualstring.charCodeAt(k) != 13) && (actualstring.charCodeAt(k) != 10)) {
                charCount = charCount + 1;
            }
            if ((charCount + enterCount) > maxlength) {

                return (returnstring);
            }
            else {
                returnstring = returnstring + actualstringarr[k];
            }
        }
        return (returnstring);
    }
    return (actualstring);
}

//Function used for handling the paste event in IE 
function onPasteHandler(textbox, e, maxLength) {
    if (navigator.appName == "Microsoft Internet Explorer") {

        if (textbox.value == '') {
            textbox.value = checkMultiLineMaxLength(window.clipboardData.getData("Text"), maxLength)
            return false;
        }
        else {

            if (!isNaN(maxLength)) {
                event.returnValue = false;
                maxLength = parseInt(maxLength);
                var oTR = document.selection.createRange();
                var iInsertLength = maxLength - textbox.value.length + oTR.text.length;
                var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
                oTR.text = sData;
                textbox.value = checkMultiLineMaxLength(textbox.value, maxLength);
            }
        }
    }
}

//Function to check the maxlength finally on blur event. This for any unhandled event causing more number of characters.
function validateMultiLine(textbox, maxLength) {
    if ((navigator.appName == "Microsoft Internet Explorer")) {
        textbox.value = checkMultiLineMaxLength(textbox.value, maxLength);
    }
    else {
        textbox.value = getValidCharsWithEnter(textbox.value, maxLength);
    }
}

//*************************** DO NOT DELETE SECTION ****************************************

//For grid pager 
/////////////////////////Not USED FOR THE MOMENT - DO NOT DELETE FOR NOW - HARSH
//function isValidValue(ctl,e,pagelimit)
//{
//        var iKeyCode;
//        if (navigator.appName=="Microsoft Internet Explorer")
//            iKeyCode = event.keyCode
//        else
//            iKeyCode = e.which
//       var iKeyValue;
//       iKeyValue=String.fromCharCode(iKeyCode);
//       
//       if(iKeyCode>=48 && iKeyCode<=57) 
//       {    
//            var iKyeCurrentValue=ctl.value;
//            iNewValue=eval(iKyeCurrentValue+iKeyValue);
//            if (navigator.appName=="Microsoft Internet Explorer")
//            {
//                var Sel = document.selection.createRange();
//                var Selectlength = Sel.text.length;
//                if (Selectlength==0)
//                {
//                    if (iNewValue<=pagelimit && iNewValue>=1)
//                        return true;
//                }
//                else
//                {
//                    if (iKeyValue<=pagelimit && iKeyValue>=1)
//                        return true;
//                }
//                
//            }
//            else
//            {
//              var iKyeCurrentValue=ctl.value;
//                iNewValue=eval(iKyeCurrentValue+iKeyValue);
//                if (ctl.value!="")
//                {    
//                    if (iNewValue<=pagelimit && iNewValue>=1) // && Selcount!=0)
//                    {
//                        return true;
//                    }
//                }
//                else
//                {
//                    if (iNewValue<=pagelimit && iNewValue>=1)
//                    {
//                        return true;
//                    }
//                }
//            }
//            
//       }
//       if (navigator.appName!="Microsoft Internet Explorer")
//       {
//            if(iKeyCode==8 || iKeyCode==0)
//            {
//                return true;
//            }
//       }
//       return false;
//}

// ============================================== Added By: Nishant Dave...14th Aug 2010
function validateDecimalNew(ctrlID, decimalPlaces)//fun 1
{
    //alert(ctrlID);

    //var varValue = document.getElementById(ctrlID).value;
    var varValue = ctrlID.value;

    if (isNaN(varValue)) {

        alert("Enter numeric data");
        ctrlID.value = "";
        ctrlID.focus();
        return false;
    }
    else if (varValue == 0.00) {

        ctrlID.value = 0;
        ctrlID.focus();
        return false;
    }
    else {
        var arrParts = varValue.split('.');
        if (arrParts.length > 1) {
            if (arrParts[1].length < 1) {
                varValue = arrParts[0];
            }
        }
        ctrlID.value = format_number(varValue, decimalPlaces);

        return true;
    }
}
//=========================

//Format Number
function format_number(p, d) {
    var r;
    if (p < 0) { p = -p; r = format_number2(p, d); r = "-" + r; }
    else { r = format_number2(p, d); }
    return r;
}
//======================
function format_number2(pnumber, decimals) {
    var strNumber = new String(pnumber);
    var arrParts = strNumber.split('.');
    var intWholePart = parseInt(arrParts[0], 10);
    var strResult = '';
    if (isNaN(intWholePart))
        intWholePart = '0';
    if (arrParts.length > 1) {
        var decDecimalPart = new String(arrParts[1]);
        var i = 0;
        var intZeroCount = 0;
        while (i < String(arrParts[1]).length) {
            if (parseInt(String(arrParts[1]).charAt(i), 10) == 0) {
                intZeroCount += 1;
                i += 1;
            }
            else
                break;
        }
        decDecimalPart = parseInt(decDecimalPart, 10) / Math.pow(10, parseInt(decDecimalPart.length - decimals - 1));
        Math.round(decDecimalPart);
        decDecimalPart = parseInt(decDecimalPart) / 10;
        decDecimalPart = Math.round(decDecimalPart);

        //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
        //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

        if (decDecimalPart == Math.pow(10, parseInt(decimals))) {
            intWholePart += 1;
            decDecimalPart = "0";
        }
        var stringOfZeros = new String('');
        i = 0;
        if (decDecimalPart > 0) {
            while (i < intZeroCount) {
                stringOfZeros += '0';
                i += 1;
            }
        }
        decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart);
        var dot = decDecimalPart.indexOf('.');
        if (dot == -1) {
            decDecimalPart += '.';
            dot = decDecimalPart.indexOf('.');
        }
        var l = parseInt(dot) + parseInt(decimals);
        while (decDecimalPart.length <= l) {
            decDecimalPart += '0';
        }
        strResult = decDecimalPart;
    }
    else {
        var dot;
        var decDecimalPart = new String(intWholePart);

        decDecimalPart += '.';
        dot = decDecimalPart.indexOf('.');
        var l = parseInt(dot) + parseInt(decimals);
        while (decDecimalPart.length <= l) {
            decDecimalPart += '0';
        }
        strResult = decDecimalPart;
    }
    return strResult;
}
// ============================================== Completed By: Nishant Dave...14th Aug 2010
