function doCheckboxOpenResponseChange(inval) {
    if (document.getElementById) {
        var arr = inval.name.split("_")
        if ((arr.length = 4) && (inval.type == "text")) {
            obj = document.getElementById("question_" + arr[2]);
            if (obj.length > 0) {
                for (i = 0; i < obj.length; i++) {
                    curobj = obj[i]
                    if (curobj && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == arr[3]) {
                        if (inval.value == "") curobj.checked = false
                        else curobj.checked = true
                    }
                }
            }
        }
    }
}

function doCheckboxChange(que, ans) {
    if (document.getElementById) {
        //var robj = document.getElementById("question_" + que + "_" + ans);
        var obj = document.getElementById("open_response_" + que + "_" + ans);
        if (obj) {
            if (isChecked(que, ans)) {

                //obj.disabled = false
                obj.focus()
            }
            //else { obj.disabled = true; }
        }
    }
}

function doRadioChange(que, ans) {
    if (document.getElementById) {
        var robj = document.getElementById("question_" + que + "_" + ans);
        var obj = document.getElementById("open_response_" + que + "_" + ans);
        if (obj) {
            if (isChecked(que, ans)) {
                //obj.disabled = false
                obj.focus()
            }
            //else { obj.disabled = true; }
        }
    }
}

function isChecked(que, ans) {
    ret = false
    nobj = document.getElementsByTagName("input")
    if (nobj.length > 0) {

        for (i = 0; i < nobj.length; i++) {
            curobj = nobj[i]
            if (curobj && curobj.name == "question_" + que && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == ans && curobj.checked)
                ret = true
        }
    }
    return ret
}

function doChecked(obj, que, ans) {
    if (obj.value != "") {
        nobj = document.getElementsByTagName("input")
        if (nobj.length > 0) {
            for (i = 0; i < nobj.length; i++) {
                curobj = nobj[i]
                if (curobj && curobj.name == "question_" + que && (curobj.type == "radio" || curobj.type == "checkbox") && curobj.value == ans)
                    curobj.checked = true;
            }
        }

    }
}
function limitTextSize(num, obj) {
    if (obj.value.length > num - 1) {
        //obj.value = obj.value.substring(0, num-1)
        //alert("You may enter only "+ num +" characters in this field.");
    }
}

var key = 0
var bkey = 0
var infocus = 0
document.onkeydown = doKeyDown;
function doKeyDown(e) {
    if (!e) var e = window.event;
    bkey = key
    key = e.keyCode
}




//// related to calculations:
names = new Array()
namesh = new Array()
functions = new Array()
function calculate(obj) {

    var t = ''
    var val = 0
    var i;
    for (i = 0; i < names.length; i++) {
        newobj = document.getElementById(names[i])		// element to change
        fname = getFunctionName(functions[i])   		//function name
        quesv = getFunctionValues(functions[i]) 		// array of que values
        if (fname == "sum") val = doSum(newobj.name, quesv)	// make calculation
        if (newobj && val) newobj.value = val 		// init value
        newobjh = document.getElementById(names[i].replace('question', 'questionh'))
        if (newobjh && val > 0) newobjh.value = val 		// init hidden value
    }
}
function getFunctionName(fnm) {
    arr = fnm.split('(')
    if (arr.length > 1) return arr[0]
    else return ""
}
function getFunctionValues(fnm) {
    arr = fnm.split('(')
    if (arr.length > 1) {
        arr2 = arr[1].split(')')
        if (arr2.length > 1) {
            result = arr2[0].split(',')
        }
    } else {
        result = new Array()
    }
    return result;
}
function doSum(name, quesv) {
    re = /^(\+|-){0,1}[\d.]+$/;
    arr = name.split('_')
    var su = 0
    if (arr.length > 3) {
        for (i = 0; i < quesv.length; i++) {
            nm = arr[0] + "_" + arr[1] + "_" + quesv[i] //+ (eval(quesv[i])+1) + "_" + arr[3]
            sobj = document.getElementById(nm)

            if (sobj && re.exec(sobj.value)) {
                su = su + eval(sobj.value)
            }
        }
    }
    return su
}
//// END related to calculations

///FUNCTIONS BY NIKHIL- STARTS
var globTotEqtPart = '';
var globTotNonEqtPart = '';
var globTotAssociate = '';
var globTotOthAttorn = '';
var globTotAttGrossRev = '';
var globTotEqtPartNet = '';
var globrevperloy = '';
var globprofpereqt = '';
var globTotAttornQ7 = '';
var globTotcompQ14 = '';
function SetValue(numb, valn) {
    //alert(numb + '        -> '+valn);
    switch (numb) {
        case 1:

            globTotEqtPart = valn;
            //document.getElementById('hdnTotEqtPart').value = valn;
            break;
        case 2:

            globTotNonEqtPart = valn;
            //document.getElementById('hdnTotNonEqtPart').value = valn;
            break;
        case 3:

            globTotAssociate = valn;
            //document.getElementById('hdnTotAssociate').value = valn;
            break;
        case 4:

            globTotOthAttorn = valn;
            //document.getElementById('hdnTotOthAttorn').value = valn;
            break;

        case 5:
            globTotAttGrossRev = valn; break;

        case 6:
            globTotEqtPartNet = valn;
            break;
        case 7:
            globrevperloy = valn;
            break;
        case 8:
            globprofpereqt = valn;
            break;
        case 9:
            globTotAttornQ7 = valn; //alert(valn);
            break;
        case 10:
            globTotcompQ14 = valn; //alert(valn);
            break;



    }
    if (document.getElementById(valn) != null && numb != '10') {
        //alert(1);
        document.getElementById(valn).setAttribute('readonly', 'readonly');
    }

}

function DoSum(id, totid, Qname, secname) {

    try {


        //alert(' ->    ' + Qname);

        var totamount = 0;
        var newid = 0;
        var newidstring = '';

        if (document.getElementById(totid) != null) {
            document.getElementById(totid).value = '0';
            var temp = totid.split('_');

            if (temp != null && temp.length >= 4) {

                for (var i = 1; i <= 4; i++) {
                    newid = temp[3] - i;
                    newidstring = temp[0] + "_" + temp[1] + "_" + temp[2] + '_' + newid;

                    //alert('old --> ' + totid + ' New ---> ' + newidstring);
                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {

                        var currentval = document.getElementById(newidstring).value;

                        if (!isNaN(Number(currentval))) {
                            //document.getElementById(newidstring).value = currentval.toFixed(2);
                            totamount = Number(totamount) + Number(currentval);
                            // getRoundValue(totamount);
                        }
                    }

                }

            }

            if (totamount > 0) {
                //totamount = totamount.toFixed(2);
                totamount = Math.round(totamount);
                document.getElementById(totid).value = totamount;

            }




        }
        //alert(secname);
        if (secname == 'Census by city:' || secname == 'Census by city') {

            if (Qname == 'Equity partners:')
                calculatecoltotal(id.id, 1);
            else if (Qname == 'Nonequity partners:')
                calculatecoltotal(id.id, 2);
            else if (Qname == 'Associates:')
                calculatecoltotal(id.id, 3);
            else if (Qname == 'Other attorneys:')
                calculatecoltotal(id.id, 4);

            //calculatecoltotal(totid, 5);
            DoSumforQ7(globTotAttornQ7, '', '');
            //25 JAN CHANGES FOR Q 13 - NIKHIL
            document.getElementById(globTotAttGrossRev).value = document.getElementById(globTotAttornQ7).value;
            SumGrossNet();
        }
        else
            if (secname.toString().toLowerCase().indexOf('census totals') >= 0) {
            SumGrossNet();
        }

    }
    catch (e) { }
}

function DoSumfor11(id, totid, Qname, secname) {

    try {


        //alert(' ->    ' + Qname);

        var totamount = 0;
        var newid = 0;
        var newidstring = '';

        if (document.getElementById(totid) != null) {
            document.getElementById(totid).value = '0';
            var temp = totid.split('_');

            if (temp != null && temp.length >= 4) {

                for (var i = 1; i <= 4; i++) {
                    newid = temp[3] - i;
                    newidstring = temp[0] + "_" + temp[1] + "_" + temp[2] + '_' + newid;

                    //alert('old --> ' + totid + ' New ---> ' + newidstring);
                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {

                        var currentval = document.getElementById(newidstring).value;

                        if (!isNaN(Number(currentval))) {
                            //document.getElementById(newidstring).value = currentval.toFixed(2);
                            totamount = Number(totamount) + Number(currentval);
                            // getRoundValue(totamount);
                        }
                    }

                }

            }

            if (totamount > 0) {
                //totamount = totamount.toFixed(2);
                totamount = Math.round(totamount);
                document.getElementById(totid).value = totamount;

            }




        }

    }
    catch (e) { }
}

function DoSumforQ7(totid, Qname, secname) {

    try {


        //alert(' ->    ' + Qname);

        var totamount = 0;
        var newid = 0;
        var newidstring = '';

        if (document.getElementById(totid) != null) {
            document.getElementById(totid).value = '0';
            var temp = totid.split('_');

            if (temp != null && temp.length >= 4) {

                for (var i = 1; i <= 4; i++) {
                    newid = temp[3] - i;
                    newidstring = temp[0] + "_" + temp[1] + "_" + temp[2] + '_' + newid;

                    //alert('old --> ' + totid + ' New ---> ' + newidstring);
                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {

                        var currentval = document.getElementById(newidstring).value;

                        if (!isNaN(Number(currentval))) {
                            //document.getElementById(newidstring).value = currentval.toFixed(2);
                            totamount = Number(totamount) + Number(currentval);
                            // getRoundValue(totamount);
                        }
                    }

                }

            }

            if (totamount > 0) {
                //totamount = totamount.toFixed(2);
                totamount = Math.round(totamount);
                document.getElementById(totid).value = totamount;

            }




        }

    }
    catch (e) { }
}

function calculatecoltotal(id, totcolnum) {

    try {
        var totamount = 0;
        var currentval = document.getElementById(id).value;
        if (!isNaN(Number(currentval)))
            totamount = Number(totamount) + Number(currentval);

        var newid = 0;
        var newidstring = '';
        var cond = true;
        {

            var temp = id.split('_');

            if (temp != null && temp.length >= 4) {
                var i = 1;
                //DECREASE ID
                while (cond) {
                    newid = Number(temp[2]) - i;
                    newidstring = temp[0] + "_" + temp[1] + "_" + newid + '_' + temp[3];

                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {
                        currentval = document.getElementById(newidstring).value;
                        if (!isNaN(Number(currentval)))
                            totamount = Number(totamount) + Number(currentval);
                    }
                    else {
                        cond = false;
                    }
                    i++;
                }
                cond = true;
                i = 1;
                //INCREASE ID
                while (cond) {
                    newid = Number(temp[2]) + i;
                    newidstring = temp[0] + "_" + temp[1] + "_" + newid + '_' + temp[3];

                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {
                        var currentval = document.getElementById(newidstring).value;
                        if (!isNaN(Number(currentval)))
                            totamount = Number(totamount) + Number(currentval);
                    }
                    else {
                        cond = false;
                    }
                    i++;
                }

            }
            totamount = totamount.toFixed(2);
            try {
                totamount = Math.round(totamount); //getRoundValue(totamount);
            } catch (e) { }
            switch (totcolnum) {
                case 1:
                    //alert(globTotEqtPart +'      '+ document.getElementById('hdnTotEqtPart').value);
                    if (document.getElementById(globTotEqtPart) != null)
                        document.getElementById(globTotEqtPart).value = totamount;
                    //alert(document.getElementById(globTotEqtPart) +'  --> '+ totamount);
                    document.getElementById(globTotEqtPart).blur();

                    break;
                case 2:
                    if (document.getElementById(globTotNonEqtPart) != null)
                        document.getElementById(globTotNonEqtPart).value = totamount;
                    break;
                case 3:
                    if (document.getElementById(globTotAssociate) != null)
                        document.getElementById(globTotAssociate).value = totamount;
                    break;
                case 4:
                    if (document.getElementById(globTotOthAttorn) != null)
                        document.getElementById(globTotOthAttorn).value = totamount;
                    break;

                case 5:
                    if (document.getElementById(globTotAttornQ7) != null)
                        document.getElementById(globTotAttornQ7).value = totamount;
                    //alert(1);
                    break;

            }
            //alert(1);
            

        }
    }
    catch (e) { }
    SumGrossNet();
}
function SumGrossNet() {
    try {
        //CALL BLUR FUNCTION FOR Q NO 7 TOT
        if (globTotOthAttorn != '') {
            var temp1 = globTotOthAttorn.split('_');
            //question_20443_2_20446
            var newt1 = 0;

            newt1 = Number(temp1[3]) + 1;
            var str1 = temp1[0] + "_" + temp1[1] + "_" + temp1[2] + '_' + newt1;
            //alert(str1);

            //GET COL TOTAL FOR TOT ATTORNEY COL- START
            //DoSum('', str1, '', '');
            //GET COL TOTAL FOR TOT ATTORNEY COL- END
            if (/*document.getElementById(str1) != null &&*/
					 document.getElementById(globTotAttGrossRev) != null
					&& document.getElementById(globTotEqtPartNet) != null) {

                document.getElementById(globTotAttGrossRev).value = document.getElementById(globTotAttornQ7).value;
                document.getElementById(globTotEqtPartNet).value = document.getElementById(globTotEqtPart).value;

                DoGrossorTotal('', globrevperloy);
                DoGrossorTotal('', globprofpereqt);
                //document.getElementById(globTotAttGrossRev).blur();
                //document.getElementById(globTotEqtPartNet).blur();

            }
        }
    }
    catch (e)
	{ }
}

function DoGrossorTotal(id, totid) {

    try {


        // alert(' ->    ' + totid);

        //var totamount = 0;
        var newid = 0;
        var newidstring = '';

        var grossorNet = 0;
        var tot = 0;
        if (document.getElementById(totid) != null) {
            document.getElementById(totid).value = '0';
            var temp = totid.split('_');

            if (temp != null && temp.length >= 4) {

                {
                    //CALCULATE TOTAL START
                    newid = temp[3] - 1;
                    newidstring = temp[0] + "_" + temp[1] + "_" + temp[2] + '_' + newid;

                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {

                        var currentval = document.getElementById(newidstring).value;
                        if (!isNaN(Number(currentval)))
                            tot = currentval;
                    }
                    //CALCULATE TOTAL ENDS

                    //CALCULATE GROSS/NET STARTS
                    newid = temp[3] - 2;
                    newidstring = temp[0] + "_" + temp[1] + "_" + temp[2] + '_' + newid;

                    if (document.getElementById(newidstring) != null && document.getElementById(newidstring).value != null) {

                        var currentval = document.getElementById(newidstring).value.replace(new RegExp(',', 'g'), '');
                        //alert('curr ->' + currentval);
                        if (!isNaN(Number(currentval)))
                            grossorNet = currentval;
                    }
                    //CALCULATE GROSS/NET ENDS
                }

            }

            if (tot > 0 && grossorNet > 0) {
                //alert(tot + ' <-tot  gross->  ' + grossorNet);
                var ans = (Number(grossorNet / tot));
                ans = Math.round(ans);
                try {
                    ans = addCommas(ans);
                }
                catch (e) { }
                document.getElementById(totid).value = ans;
            }
        }
    }
    catch (e) { }
}

function addCommas(n) {  // works with negative numbers
    var s = Math.round(n * Math.pow(10, 2)) / Math.pow(10, 2);  // 2 decimal places
    var s = s.toString();
    var ssplt = s.split(".");
    s = Number(ssplt[0]).toLocaleString();
    s = s.replace(/\..+/, "");

    // old commented by nikhil - return s + "." + (ssplt[1] || "00");  // if only integer numbers required simply return s;
    return s;
}

function getRoundValue(inpt) {
    try {
        inpt = Math.round(inpt);
    }
    catch (e) { }
    //return inpt;
}
//function to allow numeric only
function ValidateNumeric(inputfield, inputevent) {
    var key;
    var keychar;

    if (window.event) {
        key = window.event.keyCode;
    }
    else if (inputevent) {
        key = inputevent.which;
    }
    else {
        return true;
    }

    keychar = String.fromCharCode(key);
    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
        return true;
    }
    else if (("1234567890.").indexOf(keychar) > -1) {
        if (inputfield.value.indexOf('.') > -1) {
            if ((".").indexOf(keychar) > -1)
                return false;

            //alert(inputfield.value.length);
            var totle = inputfield.value.length;

            var dotindex = inputfield.value.indexOf('.');
            //alert(' index- ' +dotindex);
            dotindex = dotindex + 1;

            if ((totle - dotindex) < 2)
                return true;
            else
                return false;
        }
        return true;
    }
    else {
        return false;
    }
}

function ValidateNumericwithComma(inputfield, inputevent) {
    var key;
    var keychar;

    if (window.event) {
        key = window.event.keyCode;
    }
    else if (inputevent) {
        key = inputevent.which;
    }
    else {
        return true;
    }

    keychar = String.fromCharCode(key);
    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
        return true;
    }
    else if (("1234567890.,").indexOf(keychar) > -1) {
        if (inputfield.value.indexOf('.') > -1) {
            if ((".").indexOf(keychar) > -1)
                return false;

            //alert(inputfield.value.length);
            var totle = inputfield.value.length;

            var dotindex = inputfield.value.indexOf('.');
            //alert(' index- ' +dotindex);
            dotindex = dotindex + 1;

            if ((totle - dotindex) < 2)
                return true;
            else
                return false;
        }
        return true;
    }
    else {
        return false;
    }
}

function removecomma() {
    try {
        //alert(document.getElementById(globTotcompQ14));
        if (document.getElementById(globTotcompQ14) != null)
            document.getElementById(globTotcompQ14).value = document.getElementById(globTotcompQ14).value.replace(new RegExp(',', 'g'), '');
        // alert(document.getElementById(globTotcompQ14).value);
    } catch (e) {
        // alert(e);
    }

}
///FUNCTION BY NIKHIL ENDS
