/***************************Confidential and Proprietary************************
* File Name: ParseInt64.js
********************Copyright (c) Hyland Software, Inc. 1991-2008********************/
// Actual Max =    9,007,199,254,740,992
// Max we're using = 999,999,999,999,999
function parseInt64(stringValue, radix)
{
    // Default radix is 10 if not passed in.
    var _radix = radix != null ? radix : 10;
    
    // Parse the value into an integer
    var retVal = parseInt(stringValue, _radix);
    
    if(!isNaN(retVal))
    {
        // Perform validation to determine if we should throw an exception.
        if (stringValue.length == undefined)
        {
            // Parameter is not a string
            // Check if the value exceeds 999,999,999,999,999
            if (stringValue > 999999999999999 || stringValue < -999999999999999)
            {
                throw new Error("The value must not exceed 999,999,999,999,999.");
            }
            else
            {
                // Check to ensure the sign of the result matches the sign of the parameter coming in
                if ((stringValue >= 0 && retVal < 0) || (stringValue < 0 && retVal >= 0))
                {
                    throw new Error("Mismatching signs");
                }
            }
        }
        else
        {
            // Check if the value exceeds 999,999,999,999,999
            // by seeing if the length of the string exceeds 15
            if ((stringValue.length > 15 && stringValue.indexOf("-") != 0) ||
                (stringValue.indexOf("-") == 0 && stringValue.length > 16))
            {
                throw new Error("The value must not exceed 999,999,999,999,999.");
            }
            else
            {
                // Check to ensure the sign of the result matches the sign of the parameter coming in
                if ((stringValue.indexOf("-") == 0 && retVal >= 0) || (stringValue.indexOf("-") != 0 && retVal < 0))
                {
                    throw new Error("Mismatching signs");
                }
            }
        }
    }
    
    return retVal;
}
