﻿// fp.js
// Author: Naim Bentahar
// Date: 11/25/2008
// Updated: 01/19/2010
var _rowCount;
var _ddl1 = new Array(8);
var _ddl2 = new Array(3);
var _ddl3 = new Array(3);
var _geoSelection;

var _waitMsg = 'Updating...'

addLoadEvent(function() {
    FindService.GetScreenSize(screen.height, screen.width, screen.colorDepth);
    GetProviderCount(0);
    WaitPanels('Hide');

})

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function SetSession(id, upd, validate) {
    
    Sys.Debug.trace('SetSetting(' + id + ', ' + upd + ', ' + validate + ') Fired');
    var ctl;
    var ctls = new Array(9);

    ctls[0] = 'lstPlanTypes';
    ctls[1] = 'lstProviderTypes';
    ctls[2] = 'lstSearchBy';
    ctls[3] = 'lstStates';
    ctls[4] = 'lstCities';
    ctls[5] = 'txtZipCode';
    ctls[6] = 'lstRadius';
    ctls[7] = 'txtAddress';
    ctls[8] = 'lstRadius2';

    if (document.getElementById(GetClientId(id)) != null)
        ctl = document.getElementById(GetClientId(id));
    if (ctl == null)
        ctl = $find(GetClientId(id));

    if (ctl != null) {
        // remove any message
        if (document.getElementById(GetClientId("lblMessage")) != null)
            document.getElementById(GetClientId("lblMessage")).innerText = "";
        for (i = 0; i <= ctls.length - 1; i++) {
            if (id == ctls[i]) {
               CollapseAll();
                break;
            }
        }

        if (id == 'txtZipCode' ||  id == 'txtAddress' ) {
            _geoSelection = false;
            if (document.getElementById(GetClientId('txtZipCode')) != null) 
                if (document.getElementById(GetClientId('txtZipCode')).value.length > 0) 
                    _geoSelection = true;
            if (document.getElementById(GetClientId('txtAddress')) != null) 
                if (document.getElementById(GetClientId('txtAddress')).value.length > 0) 
                    _geoSelection = true;
        } 
        else {
            if (id == 'lstRadius' || id == 'lstRadius2')
                _geoSelection = true;
            else
                _geoSelection = false;
        }
        if (id == "lstStates") {
            FindService.SetSession("lstCities", "");
        }
        if (id == "lstStates2") {
            FindService.SetSession("lstCounties", "");
        }          
        if (ctl.value != null)
            if (id == "chkPCPOnly")
                FindService.SetSession(id, ctl.checked);
        else
            FindService.SetSession(id, ctl.value);
        else
            FindService.SetSession(id, upd);

        if ((id != 'cpe1') && (id != 'cpe2') && (id != 'cpe3')) {
            GetProviderCount(validate);
            //if (_rowCount == 0)
                GetProviderCount(validate);
            GetProviderCount(validate);
            Sys.Debug.trace('     Count = ' + _rowCount);
        }
        return (true);
    }
    return (false);
}

function GetProviderCount(validate) {

    Sys.Debug.trace('GetProviderCount(' + validate + ') Fired');
    
    if (document.getElementById(GetClientId('ProgressReady')) != null) {
      if (document.getElementById('ProgressWait') != null) {
        document.getElementById(GetClientId('ProgressReady')).style.display = 'none';
        document.getElementById('ProgressWait').style.display = 'block';
        WaitPanels('Show');
      }
      
    }
    if (FindService != null) {
        FindService.GetProviderCount(validate, OnCompletion);
    }
}

function OnCompletion(rowCount) {

    Sys.Debug.trace('OnCompletion(' + rowCount + ') Fired');
     
    if (FindService != null)
        FindService.GetErrorMessage(OnErrorMessage);
    _rowCount = rowCount;

    if (document.getElementById(GetClientId('btnOptions')) != null)
        document.getElementById(GetClientId('btnOptions')).disabled = (_rowCount == 0);
    if (document.getElementById(GetClientId('btnSearch')) != null)
        document.getElementById(GetClientId('btnSearch')).disabled = (_rowCount == 0);
    if (document.getElementById(GetClientId('btnSearch2')) != null)
        document.getElementById(GetClientId('btnSearch2')).disabled = (_rowCount == 0);

    /* WaitPanels('Hide'); */

    if ((document.getElementById(GetClientId('ProgressReady')) != null) && (document.getElementById('ProgressWait') != null)) {
        document.getElementById(GetClientId('ProgressReady')).innerText = 'Providers Found: ' + _rowCount;
        document.getElementById(GetClientId('ProgressReady')).textContent = 'Providers Found: ' + _rowCount;
        document.getElementById(GetClientId('ProgressReady')).style.display = 'block';
        document.getElementById('ProgressWait').style.display = 'none';

    }

}

function WaitPanels(action) {

    Sys.Debug.trace('WaitPanels(' + action + ') Fired');

    _ddl1[0] = 'Specialities';
    _ddl1[1] = 'Gender';
    _ddl1[2] = 'Languages';
    _ddl1[3] = 'HospitalAffiliation';
    _ddl1[4] = 'MedicalGroupAffiliation';
    _ddl1[5] = 'BoardCertifications';
    _ddl1[6] = 'NewPatients';
    _ddl2[0] = 'HospitalNames';
    _ddl2[1] = 'HospitalAccreditations';
    _ddl3[0] = 'SNFNames';
    _ddl3[1] = 'SNFAccreditations';

    if (document.getElementById(GetClientId('lstProviderTypes')) != null) {
        var e = document.getElementById(GetClientId('lstProviderTypes'));
        var pt = e.options[e.selectedIndex].value;

        if (pt != null) {
            switch (pt) {
                case "1":  // Physicians
                    var collPanel = $find(GetClientId('cpe1'));
                    if (collPanel != null) {
                        if (!collPanel.get_Collapsed()) {
                            for (var i = 0; i <= _ddl1.length - 1; i++) {
                                var d = document.getElementById(GetClientId('lst' + _ddl1[i]));
                                if (d != null) {
                                    var p = document.getElementById(GetClientId('pnlUpdate' + _ddl1[i]));
                                    if (p != null) {
                                        if (action == 'Show') {
                                            d.style.display = 'none';
                                            p.style.display = 'block';
                                            if (d.clientWidth > 50)
                                                p.style.width = (d.clientWidth + 17) + 'px';
                                            else
                                                p.style.width = (150) + 'px';
                                        }
                                        else {
                                            d.style.display = 'block';
                                            p.style.display = 'none';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case "2":  // Hospitals 
                    var collPanel = $find(GetClientId('cpe2'));
                    if (collPanel != null) {
                        if (!collPanel.get_Collapsed()) {
                            for (i = 0; i <= _ddl2.length - 1; i++) {
                                var d = document.getElementById(GetClientId(_ddl2[i]));
                                if (d != null) {
                                    var p = document.getElementById(GetClientId('pnlUpdate' + _ddl2[i]));
                                    if (p != null) {
                                        if (action == 'Show') {
                                            d.style.display = 'none';
                                            p.style.display = 'block';
                                            if (d.clientWidth > 50)
                                                p.style.width = (d.clientWidth + 17) + 'px';
                                            else
                                                p.style.width = (150) + 'px';
                                        }
                                        else {
                                            d.style.display = 'block';
                                            p.style.display = 'none';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case "3":  // SNF 
                    var collPanel = $find(GetClientId('cpe3'));
                    if (collPanel != null) {
                        if (!collPanel.get_Collapsed()) {
                            for (i = 0; i <= _ddl3.length - 1; i++) {
                                var d = document.getElementById(GetClientId(_ddl3[i]));
                                if (d != null) {
                                    p = document.getElementById(GetClientId('pnlUpdate' + _ddl3[i]));
                                    if (p != null) {
                                        if (action == 'Show') {
                                            d.style.display = 'none';
                                            p.style.display = 'block';
                                            if (d.clientWidth > 50)
                                                p.style.width = (d.clientWidth + 17) + 'px';
                                            else
                                                p.style.width = (150) + 'px';
                                        }
                                        else {
                                            d.style.display = 'block';
                                            p.style.display = 'none';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                default: // else
                    // do nothing
            }
        }
    }
    else {
        Sys.Debug.trace('Provider Type is null');
    }
}
function OnErrorMessage(ErrorMsg) {

    Sys.Debug.trace('OnErrorMessage(' + ErrorMsg + ') Fired');

    if (document.getElementById(GetClientId('lblMessage')) != null) {
        document.getElementById(GetClientId('lblMessage')).innerText = ErrorMsg;
        Sys.Debug.trace(' Error = ' + ErrorMsg);
    }
}

function ExpandCollapse() {

    Sys.Debug.trace('ExpandCollapse() Fired');
    
    var btn = document.getElementById(GetClientId('btnOptions')); // select element

    if (document.getElementById(GetClientId('lstProviderTypes')) != null) {
        var e = document.getElementById(GetClientId('lstProviderTypes'));
        var pt = e.options[e.selectedIndex].value;
        var collPanel1 = $find(GetClientId('cpe1'));
        var collPanel2 = $find(GetClientId('cpe2'));
        var collPanel3 = $find(GetClientId('cpe3'));

        if ((collPanel1 != null) && (collPanel2 != null) && (collPanel3 != null) && (pt != null)) {
            btn.value = "Please wait... Updating.";
            btn.className = "LongButtonWaitStyle";

            switch (pt) {
                case "1":  // Physicians 
                    SetSession('cpe2', 0, 0);
                    SetSession('cpe3', 0, 0);
                    if (collPanel1.get_Collapsed())
                        SetSession('cpe1', 1, 0);
                    else {
                        SetSession('cpe1', 0, 0);
                        collPanel1.set_Collapsed(true);
                    }
                    break;
                case "2": // Hospitals
                    SetSession('cpe1', 0, 0);
                    SetSession('cpe3', 0, 0);
                    if (collPanel2.get_Collapsed())
                        SetSession('cpe2', 1, 0);
                    else {
                        SetSession('cpe2', 0, 0);
                        collPanel2.set_Collapsed(true);
                    }
                    break;
                case "3": // SNF
                    SetSession('cpe1', 0, 0);
                    SetSession('cpe2', 0, 0);
                    if (collPanel3.get_Collapsed())
                        SetSession('cpe3', 1, 0);
                    else {
                        SetSession('cpe3', 0, 0);
                        collPanel3.set_Collapsed(true);
                    }
                    break;
                default: // other
                    // Do Nothing
            }

            if (document.getElementById(GetClientId('btnTrigger1')) != null)
                document.getElementById(GetClientId('btnTrigger1')).click();
            GetProviderCount(0);
        }
    }
}

function CollapseAll() {

    Sys.Debug.trace('CollapseAll() Fired');
    
    var collPanel1 = $find(GetClientId('cpe1'));
    if (collPanel1 != null) {
        if (!collPanel1.get_Collapsed()) {
            collPanel1.set_Collapsed(true);
            SetSession("cpe1", 0, 0);
        }
    }
    var collPanel2 = $find(GetClientId('cpe2'));
    if (collPanel2 != null) {
        if (!collPanel2.get_Collapsed()) {
            collPanel2.set_Collapsed(true);
            SetSession("cpe2", 0, 0);
        }
    }
    var collPanel3 = $find(GetClientId('cpe3'));
    if (collPanel3 != null) {
        if (!collPanel3.get_Collapsed()) {
            collPanel3.set_Collapsed(true);
            SetSession("cpe3", 0, 0);
        }
    }
}
