﻿var initialRequest = 5;
var totalRequest = 0;

$(document).ready(function() {
    $("select[id$=DDLTask]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
    $("select[id$=DDLKommun]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
    $("select[id$=DDLTarget]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
    $("select[id$=DDLCategory]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
    $("select[id$=DDLOrganization]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
});
function for_search() {
    var objDIV = document.getElementById('PnlSearch');
    if (objDIV) {
        objDIV.style.visibility = "visible";
        var objLoading = document.getElementById('imgLoadingONLOAD');
        if (objLoading)
            objLoading.style.display = "none";
    }
}
function ResetAll() {
    ShowLoadingProgress();
    $('#VolonJobSearch1_HFFlagReset').attr('value', '1');
    //disabling
    $("#divTask :span").bind('click', handler);
    $("#divTask").children().children().addClass('disableCombo');
    $("#divKommun :span").bind('click', handler);
    $("#divKommun").children().children().addClass('disableCombo');
    $("#divTarget :span").bind('click', handler);
    $("#divTarget").children().children().addClass('disableCombo');
    $("#divCategory :span").bind('click', handler);
    $("#divCategory").children().children().addClass('disableCombo');
    $("#divOrganization :span").bind('click', handler);
    $("#divOrganization").children().children().addClass('disableCombo');
    $("#VolonJobSearch1_DDLRegion").attr("disabled", "disabled");
    $("#VolonJobSearch1_DDLRegion").addClass('disableCombo');

    var objOrg = document.getElementById('VolonJobSearch1_DDLOrganization');
    var objCat = document.getElementById('VolonJobSearch1_DDLCategory');
    var objKommun = document.getElementById('VolonJobSearch1_DDLKommun');
    var objTask = document.getElementById('VolonJobSearch1_DDLTask');
    var objTarget = document.getElementById('VolonJobSearch1_DDLTarget');
    var objRegion = document.getElementById('VolonJobSearch1_DDLRegion');
    var companyID = 0;

    var objCompany=document.getElementById('VolonJobSearch1_HFCompanyID');
    if(objCompany)
        companyID=objCompany.value;
    var isChurch = isPageChurchURL();
    if (isChurch == "1") {
        if (objKommun != null)
            WebClickHandler.GetChurchMuniWithCount(0, "", "", "", "", populateMuniReset);
        if(objCat!=null)
            WebClickHandler.GetCategoryWithJobCount(0, "", "", "", "", true,companyID, populateCatReset);
        if (objTarget != null)
            WebClickHandler.GetTargetsWithChurchJobCount(0, "", "", "", "", populateTargetReset);
        if(objRegion!=null)
            WebClickHandler.GetStiftWithCount("", "", "", "", "", populateRegionReset);
        if (objTask != null)
            WebClickHandler.GetTaskWithJobCount(0, "", "", "", "", true,companyID, populateTaskReset);
        //WebClickHandler.GetOrganiizationWithJobCount(0, "", "", "", "", true, populateOrgReset);
    }
    else {
        if(objKommun!=null)
            WebClickHandler.GetMunicipalityWithJobCount(0, "", "", "", "",companyID, populateMuniReset);
        if (objCat != null)
            WebClickHandler.GetCategoryWithJobCount(0, "", "", "", "", false,companyID, populateCatReset);
        if(objTask!=null)
            WebClickHandler.GetTaskWithJobCount(0, "", "", "", "", false,companyID, populateTaskReset);
        if(objTarget!=null)
            WebClickHandler.GetTargetWithJobCount(0, "", "", "", "",companyID, populateTargetReset);
        if(objRegion!=null)
            WebClickHandler.GetRegionsWithJobCount("", "", "", "", "", companyID,populateRegionReset);
        if(objOrg!=null)
            WebClickHandler.GetOrganiizationWithJobCount(0, "", "", "", "", false,companyID, populateOrgReset);
    }
}

var handler = function() {
    return false;
};
/*
function GetJobCount(TheRegionID, strTaskIDs, strCatIDs, strKommunIDs, strOrgIDs, strTargetIDs, isChurch) {

    if (isChurch == "1") {
        WebClickHandler.GetActiveChurchJobCount(TheRegionID, strTaskIDs, strCatIDs, strKommunIDs, strOrgIDs, strTargetIDs, populateRecCount);
    }
    else {
        WebClickHandler.GetActiveJobCount(TheRegionID, strTaskIDs, strCatIDs, strKommunIDs, strOrgIDs, strTargetIDs, populateRecCount);
    }
}
*/
function GetMuni(objThis) {
    var TheRegionID = 0;
    var strOrgIDs = "";
    var strTaskIDs = "";
    var strTargetIDs = "";
    var strCatIDs = "";
    var strKommunIDs = "";
    //var strID = objThis.id;
    var strID = objThis;
    var companyID = 0;

    var objCompany=document.getElementById('VolonJobSearch1_HFCompanyID');
    if(objCompany)
        companyID=objCompany.value;
    if (strID == "undefined" || strID == "" || strID == null)
        strID = objThis;

    ShowLoadingProgress();
    $('#VolonJobSearch1_HFFlagReset').attr('value', '0');
    
    strOrgIDs = $("#VolonJobSearch1_DDLOrganization").val();
    strTaskIDs = $("#VolonJobSearch1_DDLTask").val();
    strTargetIDs = $("#VolonJobSearch1_DDLTarget").val();
    strCatIDs = $("#VolonJobSearch1_DDLCategory").val();
    strKommunIDs = $("#VolonJobSearch1_DDLKommun").val();

    if (strOrgIDs == null || strOrgIDs == "undefined") strOrgIDs = "";
    if (strTaskIDs == null || strTaskIDs == "undefined") strTaskIDs = "";
    if (strTargetIDs == null || strTargetIDs == "undefined") strTargetIDs = "";
    if (strCatIDs == null || strCatIDs == "undefined") strCatIDs = "";
    if (strKommunIDs == null || strKommunIDs == "undefined") strKommunIDs = "";

    if (strID.match("DDLKommun") == "DDLKommun") {
        initialRequest = 4;
    }
    else
        initialRequest = 5;

    //for company search, to check if the object is hidden
    var objOrg = document.getElementById('VolonJobSearch1_DDLOrganization');
    var objCat = document.getElementById('VolonJobSearch1_DDLCategory');
    var objKommun = document.getElementById('VolonJobSearch1_DDLKommun');
    var objTask = document.getElementById('VolonJobSearch1_DDLTask');
    var objTarget = document.getElementById('VolonJobSearch1_DDLTarget');
    var objRegion = document.getElementById('VolonJobSearch1_DDLRegion');


    if (strID.match("DDLRegion") == "DDLRegion") {
        //TheRegionID = objThis.value;
        var obj = document.getElementById(strID);
        if (obj)
            TheRegionID = obj.value;
    }
    else {
        var obj = document.getElementById('VolonJobSearch1_DDLRegion');
        $("#VolonJobSearch1_DDLRegion").attr("disabled", "disabled");
        $("#VolonJobSearch1_DDLRegion").addClass('disableCombo');
        if (obj)
            TheRegionID = obj.value;
    }
    var isChurch = isPageChurchURL();
     
    //GetJobCount(TheRegionID, strTaskIDs, strCatIDs, strKommunIDs, strOrgIDs, strTargetIDs, isChurch);
    if (objTask == null) totalRequest += 1;
    if (objRegion == null) totalRequest += 1;
    if (objKommun == null) totalRequest += 1;
    if (objTarget == null) totalRequest += 1;
    if (objCat == null) totalRequest += 1;
    if (objOrg == null) totalRequest += 1;

    if (totalRequest >= initialRequest)
        HideLoadingProgress();

    if (strID.match("DDLTask") != "DDLTask" && objTask!=null) {
        //$("#divTask").children().eq(1).attr("disabled", "disabled");
        $("#divTask :span").bind('click', handler);
        $("#divTask").children().children().addClass('disableCombo');
        if (isChurch == "1") {
            WebClickHandler.GetTaskWithJobCount(TheRegionID, strOrgIDs, strTargetIDs, strCatIDs, strKommunIDs, true,companyID, populateTask);
        }
        else {
            WebClickHandler.GetTaskWithJobCount(TheRegionID, strOrgIDs, strTargetIDs, strCatIDs, strKommunIDs, false,companyID, populateTask);
        }
    }

    if (strID.match("DDLRegion") != "DDLRegion" && strID.match("DDLKommun") != "DDLKommun" && objRegion!=null) {
        if (isChurch == "1") {
            WebClickHandler.GetStiftWithCount(strTaskIDs, strOrgIDs, strTargetIDs, strCatIDs, "", populateRegion);
        }
        else {
            WebClickHandler.GetRegionsWithJobCount(strTaskIDs, strOrgIDs, strTargetIDs, strCatIDs, "",companyID, populateRegion);
        }
    }
    if (strID.match("DDLKommun") != "DDLKommun" && objKommun!=null) {
        //$("#divKommun").children().eq(1).attr("disabled", "disabled");
        $("#divKommun :span").bind('click', handler);
        $("#divKommun").children().children().addClass('disableCombo'); 
        if (isChurch == "1") {
            WebClickHandler.GetChurchMuniWithCount(TheRegionID, strTaskIDs, strTargetIDs, strOrgIDs, strCatIDs, populateMuni);
        }
        else {
            WebClickHandler.GetMunicipalityWithJobCount(TheRegionID, strOrgIDs, strTaskIDs, strCatIDs, strTargetIDs,companyID, populateMuni);
        }
    }
    if (strID.match("DDLTarget") != "DDLTarget" && objTarget!=null) {
        //$("#divTarget").children().eq(1).attr("disabled", "disabled");
        $("#divTarget :span").bind('click', handler);
        $("#divTarget").children().children().addClass('disableCombo');
        if (isChurch == "1") {
            WebClickHandler.GetTargetsWithChurchJobCount(TheRegionID, strTaskIDs, strCatIDs, strKommunIDs, strOrgIDs, populateTarget);
        }
        else {
            WebClickHandler.GetTargetWithJobCount(TheRegionID, strOrgIDs, strTaskIDs, strCatIDs, strKommunIDs,companyID, populateTarget);
        }
    }
    if (strID.match("DDLCategory") != "DDLCategory" && objCat!=null) {
        //$("#divCategory").children().eq(1).attr("disabled", "disabled");
        $("#divCategory :span").bind('click', handler);
        $("#divCategory").children().children().addClass('disableCombo')
        if (isChurch == "1") {
            WebClickHandler.GetCategoryWithJobCount(TheRegionID, strOrgIDs, strTaskIDs, strTargetIDs, strKommunIDs, true,companyID, populateCat);
        }
        else {
            WebClickHandler.GetCategoryWithJobCount(TheRegionID, strOrgIDs, strTaskIDs, strTargetIDs, strKommunIDs, false, companyID, populateCat);
        }
    }
    if (strID.match("DDLOrganization") != "DDLOrganization" && objOrg!=null) {
        //$("#divOrganization").children().eq(1).attr("disabled", "disabled");
        $("#divOrganization :span").bind('click', handler);
        $("#divOrganization").children().children().addClass('disableCombo');
        if (isChurch == "1") {
            //WebClickHandler.GetOrganiizationWithJobCount(TheRegionID, strTaskIDs, strCatIDs, strTargetIDs, strKommunIDs, true, populateOrg);
        }
        else {
            WebClickHandler.GetOrganiizationWithJobCount(TheRegionID, strTaskIDs, strCatIDs, strTargetIDs, strKommunIDs, false,companyID, populateOrg);
        }
    }
}
function collectSelectedIDs(objCombo) {
    var strIDs = "";
    if (objCombo == null) return;
    var objC = document.getElementById(objCombo);
    if (objC == null) return "";
    objAllCheckBoxes = objC.getElementsByTagName("OPTION");

    for (var i = 1; i < objAllCheckBoxes.length; i++) {
        if (objAllCheckBoxes[i].selected == true) {
            if (strIDs.length > 0) strIDs = strIDs + ",";
            strIDs = strIDs + objAllCheckBoxes[i].value;
        }
    }
    return strIDs;
}
function populateCombo(res, strAllText, objClientID, strDivID, strColValueName, strColTextName, isReset, isMultiple) {

    var strhtml = [];
    var strSelected = "";
    var arrIDs = "";
    var prevSelectedIDs = "";
    var strSelectedAll = "";

    if (isReset == false)
        prevSelectedIDs = collectSelectedIDs(objClientID);

    if (prevSelectedIDs == "undefined" || prevSelectedIDs == null)
        prevSelectedIDs = "";

    if (prevSelectedIDs.toString().length <= 0) { // first checkbox(All) is checked
        strSelected = " selected ";
    } else {
        arrIDs = prevSelectedIDs.toString().split(',');
    }
    strAllText = "";
    strSelectedAll = strSelected;
    /*strhtml[strhtml.length] = "<option value='0'" + strSelected + ">" + strAllText + "</option>";*/
    var isFound = false;
    if (res.value == null) {
        //enabling
/*      $("#divTask").children().eq(1).attr("disabled","");
        $("#VolonJobSearch1_DDLRegion").removeAttr("disabled");
        $("#divKommun").children().eq(1).removeAttr("disabled");
        $("#divTarget").children().eq(1).removeAttr("disabled");
        $("#divCategory").children().eq(1).removeAttr("disabled");
        $("#divOrganization").children().eq(1).removeAttr("disabled");*/
        
        $("#divTask :span").unbind('click', handler);
        $("#divTask :span").removeClass("disableCombo");
        $("#divKommun :span").unbind('click', handler);
        $("#divKommun :span").removeClass("disableCombo");
        $("#divTarget :span").unbind('click', handler);
        $("#divTarget :span").removeClass("disableCombo");
        $("#divCategory :span").unbind('click', handler);
        $("#divCategory :span").removeClass("disableCombo");
        $("#divOrganization :span").unbind('click', handler);
        $("#divOrganization :span").removeClass("disableCombo");
        $("#VolonJobSearch1_DDLRegion").removeAttr("disabled");
        $("#VolonJobSearch1_DDLRegion").removeClass("disableCombo");
        totalRequest += 1;
        HideLoadingProgress();
        return;
    }
    for (var i = 0; i < res.value.Rows.length; i++) {
        strSelected = "";
        for (var j = 0; j < arrIDs.length; j++) {
            if (res.value.Rows[i][strColValueName] == arrIDs[j]) {
                strSelected = " selected ";
                isFound = true;
                break;
            }
        }
        if (i == 0 && strSelectedAll != "" && res.value.Rows[i][strColValueName]=="0") {
            strSelected = strSelectedAll;
        }
        strhtml[strhtml.length] = "<option value='" + res.value.Rows[i][strColValueName] + "'" + strSelected + ">" + res.value.Rows[i][strColTextName] + "</option>";
    }

    var strMultiple = "";
    if (isMultiple == true) {
        strMultiple = " multiple='multiple'";
    }

    var objDDL = document.getElementById(objClientID);
    if (objDDL != null) {
        if (strhtml.length > 0) {
            $("#" + strDivID).html("<select id='" + objClientID + "' name='" + objClientID + "'" + strMultiple + " runat='server' onChange='SearchDelay(this)'> " + strhtml.join("") + "</select>");
            if (isFound == false && prevSelectedIDs.length > 0) {
                var objC = document.getElementById(objClientID);
                if (objC) {
                    objAllCheckBoxes = objC.getElementsByTagName("OPTION");
                    objAllCheckBoxes[0].selected = true;
                }
            }
            if (isMultiple == true)
                $("select[id$=" + objClientID + "]").dropdownchecklist({ firstItemChecksAll: true, maxDropHeight: 200 });
        }
    }
    totalRequest += 1;
    HideLoadingProgress();
}
var curTime;
function SearchDelay(objList) {
    if (curTime != null)
    { clearTimeout(curTime); }
    var strID = objList;
    
    curTime = setTimeout("GetMuni('" + objList.id + "')", 700);
}

function showhide(divid, btnexpid, rowIndex, gridId, job_id) {

    var rowCount = document.getElementById(gridId).rows.length;
    for (ij = 1; ij <= rowCount; ij++) {
        if (document.getElementById(btnexpid) == null)
            return;
        objDivHead = document.getElementById(btnexpid).parentNode.getElementsByTagName("DIV");
        var pre; var btn;

        if (ij >= 10) {
            pre = gridId + '_ctl' + ij + '_desc';
            // dont add 0 after 10...
            btn = gridId + '_ctl' + ij + '_lblMakeButton';
        }
        else {
            pre = gridId + '_ctl0' + ij + '_desc';
            btn = gridId + '_ctl0' + ij + '_lblMakeButton';
        }
        if (pre == divid) {

            if (document.getElementById(divid).style.display == 'block') {
                document.getElementById(divid).style.display = 'none';
                document.getElementById(btnexpid).className = 'plus';
                //document.getElementById(divid).parentNode.style.background = "url(../stylesheet/images/boxtitle_bg.gif) repeat-x";
                objDivHead[0].getElementsByTagName('a')[0].className = "plusLink";

                objDivHead[0].setAttribute('style', 'overflow:hidden;height:16px;width:545px;');
                if (objDivHead[1] != null)
                    objDivHead[1].setAttribute('style', 'overflow:hidden;height:16px;width:545px;');
            }
            else {
                if (curTime != null)
                { clearTimeout(curTime); }
                curTime = setTimeout("ClickHandler.InsertUpdateJobViewCount(" + job_id + ")", 500);
                document.getElementById(divid).style.display = 'block';
                document.getElementById(btnexpid).className = 'minus';
                objDivHead[0].getElementsByTagName('a')[0].className = "minusLink";

                objDivHead[0].setAttribute('style', 'overflow:visible;padding-right:10px;width:545px;');
                if (objDivHead[1] != null) {
                    objDivHead[1].setAttribute('style', 'overflow:visible;padding-right:10px;width:545px;');
                }
            }
            if (isMaxZoomSet == false) {
                map.setCenter(temppoint, zoomLvl);
            }
        }
        else {
            if (document.getElementById(pre) != null) {

                if (document.getElementById(pre).style.display != 'none') {
                    //one line title
                    objDivHead = document.getElementById(btn).parentNode.getElementsByTagName("DIV");
                    if (objDivHead[0] != null)
                        objDivHead[0].setAttribute('style', 'overflow:hidden;height:16px;width:545px;');
                    if (objDivHead[1] != null)
                        objDivHead[1].setAttribute('style', 'overflow:hidden;height:16px;width:545px;');
                }
                document.getElementById(pre).style.display = 'none';
                //document.getElementById(pre).parentNode.style.background = "url(../stylesheet/images/boxtitle_bg.gif) repeat-x";
                document.getElementById(btn).className = 'plus';
                objDivHead = document.getElementById(pre).parentNode.getElementsByTagName("DIV");
                if (objDivHead) {
                    var objDivHead2 = objDivHead[0].getElementsByTagName('DIV');
                    if (objDivHead2)
                        objDivHead2[0].getElementsByTagName('a')[0].className = "plusLink";
                }
            }
        }
    }
    isMaxZoomSet = false;
}
function close_popup(id) {
    document.getElementById(id).style.display = "none";
}
function fnCloseAll() {
    $(document).click;
}
function ShowLoadingProgress() {
    $('#VolonJobSearch1_progress').show();
    /*$('#imgLoadingSearch').css("left", ($(window).width() / 2 - 16)-50);
    $('#imgLoadingSearch').css("top", ($(window).height() / 2 - 16)-40);
    $('#imgLoadingSearch').show();*/
}
function HideLoadingProgress() {
    if (totalRequest >= initialRequest) {
        //$('#imgLoadingSearch').hide();
        var f1 = $('#VolonJobSearch1_HFFlagReset').val();
        if(f1!="1")
            $('#VolonJobSearch1_progress').hide();
        totalRequest = 0;
        $("#VolonJobSearch1_DDLRegion").removeAttr("disabled");
        $("#VolonJobSearch1_DDLRegion").removeClass("disableCombo");
    }
}
