﻿window.onload = function() 
{

    populateDomainsList();
    populateSubDomainsList();
    //populateProjectsList();
}

function SubmitFilter(e)
{
    if (window.event && window.event.keyCode == 13) {
        filterGuide1();
    } else if (e.keyCode == 13) {
        filterGuide1();
    }
}

function getCategoryDescription()
{
    id = document.getElementById("ddCounty").value;
    dt = Netlogiq.Web.FisaProiect.GetCategory(id).value;
    $('folderDesc').innerHTML = dt["Description"];
}

function filterGuide()
{
    errors = "";     
    Org     = (document.getElementById("Filter_ddOrganization").value != "-1") ? document.getElementById("Filter_ddOrganization").value : document.getElementById("Filter_ddOrganization2").value  ;
    Dom     = document.getElementById("Filter_ddDomain").value;
    SubDom  = document.getElementById("Filter_ddSubdomain").value;
    
    if (Org.value == "0")
    {
        errors += " - Alegeti organizatia;\n";
    }
    
    if (Dom.value == "0")
    {
        errors += " - Alegeti domeniu;\n";
    }
    
    if (Dom.value == "0")
    {
        errors += " - Alegeti subdomeniu;\n";
    }

    if (errors.length > 0)
    {
        alert("Va rugam remediati urmatoarele probleme:\n"+errors);
        //rollback the culture tab
        //$("hSelectedTab").value = $("hPreviousSelectedTab").value;
    }
    else
    {
        document.location.href="FisaProiect.aspx?oid="+Org+"&did="+Dom+"&sdid="+SubDom;
    }
}

function filterGuide1()
{
    errors = "";
    Keywords = (document.getElementById("txSearchGhid").value != "cauta in fise proiect") ? document.getElementById("txSearchGhid").value : "" ;
    
//    if (Keywords.value == "")
//    {
//        errors += " - Alegeti subdomeniu;\n";
//    }

    if (errors.length > 0)
    {
        alert("Va rugam remediati urmatoarele probleme:\n"+errors);
        //rollback the culture tab
        //$("hSelectedTab").value = $("hPreviousSelectedTab").value;
    }
    else
    {
        document.location.href="FisaProiect.aspx?cauta="+Keywords;
    }
}

function populateDomainsList()
{
    if($("Filter_ddOrganization").value == "-1")
    {
        $("Filter_ddOrganization2").style.display = "inline";
        dt = Netlogiq.Web.FisaProiect.GetDomainElements($("Filter_ddOrganization2").value).value;
    }
    else
    {
        $("Filter_ddOrganization2").style.display = "none";
        dt = Netlogiq.Web.FisaProiect.GetDomainElements($("Filter_ddOrganization").value).value;
    }
    
    $("Filter_ddDomain").options.length = 1;
    
  
    if(dt!=null && dt.length > 0)
    {  
        while ($("Filter_ddDomain").options.length > 0) {
            $("Filter_ddDomain").options[0] = null;
        }
        
//        $("Filter_ddDomain").disabled = false;
//        if($("Filter_ddDomain2")!=null)
//            $("Filter_ddDomain2").disabled = false;
        
    }
    else
    {
//        $("Filter_ddDomain").disabled = true;
//        if($("Filter_ddDomain2")!=null)
//            $("Filter_ddDomain2").disabled = true;
    }
    
    var opt = null; 
  
    opt = document.createElement('option');
    opt.text = "-- Alegeti domeniul --";
    opt.value = "0";
    opt.selected = true;
    
    try
    {
        $("Filter_ddDomain").add(opt,null);

    }
    catch(ex)
    {
        $("Filter_ddDomain").add(opt);
    }  
  
    if(dt==null)
        return ;
    for(i = 0; i< dt.length;i++)
    {
        opt = document.createElement('option');
        opt.text = dt[i]["Name"];
        opt.value = dt[i]["ID"];
        opt.selected = (dt[i]["ID"]==$("hSelectedDomainValue").value) ? true : false;
        
        try
        {
            $("Filter_ddDomain").add(opt,null);

        }
        catch(ex)
        {
            $("Filter_ddDomain").add(opt);
        }
    }
    populateSubDomainsList();
}

function populateSubDomainsList()
{
    var Org = 0;
    if($("Filter_ddOrganization").value == "-1")
    {
        Org = $("Filter_ddOrganization2").value;

    }
    else
    {
        Org = $("Filter_ddOrganization").value;
    }
    
    $("Filter_ddSubdomain").options.length = 1;
    dt = Netlogiq.Web.FisaProiect.GetSubDomainElements($("Filter_ddDomain").value, Org).value;
    
 
    if(dt!= null && dt.length > 0)
    {  
        while ($("Filter_ddSubdomain").options.length > 0) {
            $("Filter_ddSubdomain").options[0] = null;
        }
        
//        $("Filter_ddSubdomain").disabled = false;
//        if($("Filter_ddSubdomain")!=null)
//            $("Filter_ddSubdomain").disabled = false;
    }
    else
    {
//        $("Filter_ddSubdomain").disabled = true;
//        if($("Filter_ddSubdomain")!=null)
//            $("Filter_ddSubdomain").disabled = true;
    }
    
    var opt = null; 
  
    opt = document.createElement('option');
    opt.text = "-- Alegeti subdomeniul --";
    opt.value = "0";
    opt.selected = true;
    
    try
    {
        $("Filter_ddSubdomain").add(opt,null);

    }
    catch(ex)
    {
        $("Filter_ddSubdomain").add(opt);
    }  
    if(dt==null)
        return;
  
    for(i = 0; i< dt.length;i++)
    {
        opt = document.createElement('option');
        opt.text = dt[i]["Name"];
        opt.value = dt[i]["ID"];
        opt.selected = (dt[i]["ID"]==$("hSelectedSubDomainValue").value) ? true : false;
        
        try
        {
            $("Filter_ddSubdomain").add(opt,null);

        }
        catch(ex)
        {
            $("Filter_ddSubdomain").add(opt);
        }
    }
}

function populateProjectsList()
{
    $("Filter_ddProiecteDeSucces").options.length = 1;
    dt = Netlogiq.Web.FisaProiect.GetProjectListElements($("Filter_ddDomain").value).value;
  
    while ($("Filter_ddProiecteDeSucces").options.length > 0) {
        $("Filter_ddProiecteDeSucces").options[0] = null;
    }
    
    var opt = null; 
    var selectedIDs = $("hSelectedProjectsValue").value;
  
    for(i = 0; i< dt.length;i++)
    {
        opt = document.createElement('option');
        opt.text = dt[i]["Title"];
        opt.value = dt[i]["ID"];
        opt.selected = (selectedIDs.lastIndexOf(dt[i]["ID"]) > -1) ? true : false;
        
        try
        {
            $("Filter_ddProiecteDeSucces").add(opt,null);

        }
        catch(ex)
        {
            $("Filter_ddProiecteDeSucces").add(opt);
        }
    }
    
}

function resetSelections()
{
    $("hSelectedDomainValue").value = "";
    $("hSelectedSubDomainValue").value = "";

}