﻿//Setting region value empty after nonregion and noncounty checkbox checked
function setValueRegionNull(chkregionbox, txtregion, txtcounty, txtregionnotinlist, txtcountynotinlist,regionbox,countybox) {
    var chkregion = document.getElementById(chkregionbox);
    var region = document.getElementById(txtregion);
    var county = document.getElementById(txtcounty);
    var regionnotinlist = document.getElementById(txtregionnotinlist);
    var countynotinlist = document.getElementById(txtcountynotinlist);
    var regionboxobj = document.getElementById(regionbox);
    var countyboxobj = document.getElementById(countybox);
    region.value = '';
    county.value = '';
    regionnotinlist.value = '';
    countynotinlist.value = '';
    if (chkregion.checked == false) {
        if (regionboxobj.options.length > 0 && regionboxobj.selectedIndex > 0) {
            region.value = regionboxobj.options[regionboxobj.selectedIndex].text;
        }
        else if (regionboxobj.options[0].text != '**Select Region**') {
            region.value = regionboxobj.options[0].text;
        }


        if (countyboxobj.options.length > 0 && countyboxobj.selectedIndex > 0) {
            county.value = countyboxobj.options[countyboxobj.selectedIndex].text;
        }
        else if (countyboxobj.options[0].text != '**Select County**') { 
            county.value = countyboxobj.options[0].text;
        }
    }
}

//Setting county value empty after nonregion and noncounty checkbox checked
function setValueCountyNull(chkcountybox, txtcounty, txtcountynotinlist,countybox1) {
    var countybox = document.getElementById(chkcountybox);
    var county = document.getElementById(txtcounty);
    var countynotinlist = document.getElementById(txtcountynotinlist);
    var countyboxobj = document.getElementById(countybox1);

    county.value = '';
    countynotinlist.value = '';
    
    if (countybox.checked == false) {
        if (countyboxobj.options.length > 0 && countyboxobj.selectedIndex > 0) {

            county.value = countyboxobj.options[countyboxobj.selectedIndex].text;
        }
        else if(countyboxobj.options[0].text != '**Select County**'){
            county.value = countyboxobj.options[0].text;
        }
    }
}

//set region value
function setValueRegion(txtnoninlistregion, txtregion) {
    var noninlistregion = document.getElementById(txtnoninlistregion);
    var region = document.getElementById(txtregion);

    region.value = noninlistregion.value;
}

//set County value
function setValueCounty(txtnoninlistcounty, txtcounty) {
    var noninlistcounty = document.getElementById(txtnoninlistcounty);
    var county = document.getElementById(txtcounty);

    county.value = noninlistcounty.value;
}


//-------------standard function for Country,Region,County,Location Filteration -----start--------------------
//adding text and value in selectbox dropdown
function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

//removing all options from selectbox dropdown
function removeAllOption(selectbox) {
    for (var i = selectbox.options.length - 1; i >= 0; i--) {
        selectbox.remove(i);
    }
}

//removing all options from selectbox dropdown
function removeAllOptions(selectbox) {
    selectbox.options.length =0;
}

//removeselected option from selectbox dropdown
function removeSelectedOption(selectbox) {
    for (var i = selectbox.options.length - 1; i >= 0; i--) {
        if (selectbox.options[i].selected)
            selectbox.remove(i);
    }
}

//return selected dropdown option text
function returnSelectedText(controlid) {
    if (controlid.options.length > 0) {
        for (var i = controlid.options.length - 1; i >= 0; i--) {
            if (controlid.options[i].selected) {
                var returnvalue = '';
                return returnvalue = controlid.options[i].text;
            }
        }
    }
    return '';
}

//select default selection in dropdown
function SelectDefaultOption(controlid, controlidname) {
    if (controlid.options.length > 0) {
        document.getElementById(controlidname).defaultSelected = true;
    }
}

//select enterted textvalue in dropdown
//document.getElementById("o3").defaultSelected = true;
function SelectControlText(controlid, textvalue) {
    if (controlid.options.length > 0) {
        for (var i = 0; i < controlid.options.length; i++) {
            if (controlid.options[i].text == textvalue) {
                controlid.options[i].selected = true;
                break;
            }
        }
    }
}

//removing mismatching options with selectedoptionvalue
function CheckAllOptions(filterboxid, selectedoptionvalue) {
    var filterbox = document.getElementById(filterboxid);
    if (filterbox.options.length > 0) {
        for (var j = 1; j < filterbox.options.length; j++) {
            var value = filterbox.options[j].value;
            if (value != selectedoptionvalue) {
                filterbox.remove(j);
                CheckAllOptions(filterboxid, selectedoptionvalue);
            }
        }
    }
}

//getting key value matching with value from javascript array
function GetKey(arr, textvalue) {
    for (i in arr) {
        if (arr[i] == textvalue) {
            return i;
        }
    }
    return '';
}

function CommaFormatted(amount) {
    if (amount.indexOf(',') != -1) { 
        return amount
    }
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.', 2)
   
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}

//-------------standard function for Country,Region,County,Location Filteration ----- End--------------------
    
