﻿

var SEARCH = {

    IDs: {
        CheckAll: 'CheckAll',
        UncheckAll: 'UncheckAll',
        CheckAll2: 'CheckAll2',
        UncheckAll2: 'UncheckAll2',
        queryFName: 'queryFName',
        queryLName: 'queryLName',
        querySpecID: 'querySpecID',
        queryLetter: 'queryLetter',
        queryZip: 'queryZip',
        queryRadius: 'queryRadius',
        queryKeyword: 'queryKeyword',
        queryGender: 'queryGender',
        createPDFbtn: 'createPDFbtn',
        createPDFbtn2: 'createPDFbtn2',
        PDFmessages: 'PDFmessages',
        loader: 'loader',
        pdfSearch: 'pdfSearch',
        specialtyDefinitions: 'specialtyDefinitions',
        showOptions: 'showOptions',
        hideOptions: 'hideOptions',
        AdvancedSearch: 'AdvancedSearch'
    },

    data: {
        specIDs: '',
        allChecked: '',
        specialtyName: '',
        specialtyDefinition: '',
        ListCheckbox: 'ListCheckbox'
    },

    init: function() {
        var i;
        for (i in this.IDs) {
            if (this.IDs.hasOwnProperty(this.IDs[i]) && document.getElementById(this.IDs[i])) {
                this.IDs[i] = document.getElementById(this.IDs[i]);
            } else {
                this.IDs[i] = null;
            }
        }

       
        if (this.IDs.AdvancedSearch !== null) {
            this.IDs.AdvancedSearch.style.display = 'none';
        }

        if (this.IDs.hideOptions !== null) {
            this.IDs.hideOptions.style.display = 'none';
            Event.addListener(this.IDs.hideOptions, 'click', this.hideAdvancedSearch, this, true);
        }

        if (this.IDs.showOptions !== null) {
            Event.addListener(this.IDs.showOptions, 'click', this.showAdvancedSearch, this, true);
        }


        this.data.ListCheckbox = YAHOO.util.Dom.getElementsByClassName(this.data.ListCheckbox);
        this.data.specIDs = YAHOO.util.Selector.query('input[type=checkbox]');
        Event.addListener(this.IDs.CheckAll, 'click', this.Check, this, true);
        Event.addListener(this.IDs.UncheckAll, 'click', this.clearForm, this, true);
        Event.addListener(this.IDs.createPDFbtn, 'click', this.openPDFwait, this, true);
        Event.addListener(this.IDs.pdfSearch, 'click', this.openPDFwait, this, true);

        Event.addListener(this.IDs.CheckAll2, 'click', this.Check, this, true);
        Event.addListener(this.IDs.UncheckAll2, 'click', this.clearForm, this, true);
        Event.addListener(this.IDs.createPDFbtn2, 'click', this.openPDFwait, this, true);
        Event.addListener(this.IDs.specialtyDefinitions, 'click', this.openDefinitions, this, true);
        //if (this.data.specialtyName) {
        this.data.specialtyName = YAHOO.util.Dom.getElementsByClassName('specialtyName');
        //}

        for (i = 0; i < this.data.specialtyName.length; i++) {
            Event.addListener(this.data.specialtyName[i], 'mouseover', this.showDefinition, this.data.specialtyName[i].id, true);
            Event.addListener(this.data.specialtyName[i], 'mouseout', this.hideDefinition, this.data.specialtyName[i].id, true);
        }
    },

    showDefinition: function(o, id) {
        var def;
        def = document.getElementById('specialtyDefinition-' + id);
        if (def) {
            def.style.backgroundColor = '#666666';
            def.style.color = '#FFFFFF';
            def.style.display = 'block';
            def.style.padding = '5px';
            def.style.width = '200px';
            def.style.whiteSpace = 'normal';
            def.style.position = 'absolute';
            def.style.marginLeft = '10px';
        }
    },

    hideDefinition: function(o, id) {
        var def;
        def = document.getElementById('specialtyDefinition-' + id);
        if (def) {
            def.style.display = 'none';
        }
    },

    openDefinitions: function() {
        var popup;
        popup = window.open("/SpecialtyPopup.asp", "SpecialtyDefinitions", "menubar=1,resizable=1,height=700, width=700, scrollbars=1");
        if (window.focus) {
            popup.focus();
        }

    },

    openPDFwait: function() {
        var letter = this.convertNull(this.IDs.queryLetter)
		, famName = this.convertNull(this.IDs.queryLName)
		, givName = this.convertNull(this.IDs.queryFName)
		, qSpecID = this.IDs.querySpecID
        , gender = this.convertNull(this.IDs.queryGender)
        , key = this.convertNull(this.IDs.queryKeyword)
        , zip = this.convertNull(this.IDs.queryZip)
        , radius = this.convertNull(this.IDs.queryRadius)
		, URL, x, ids = [];

        if (qSpecID) {
            ids = qSpecID.value;
        } else {
            for (x in this.data.specIDs) {
                if (this.data.specIDs[x].checked === true) {
                    ids.push(this.data.specIDs[x].value);
                }
            }

            if (ids.length === 0 || ids.length === this.data.specIDs.length) {
                ids = '';
            } else {
                ids = ids.join(',');
            }
        }

        URL = '/PDFwait.asp?SpecID=' + ids + '&letter=' + letter + '&familyName=' + famName + '&givenName=' + givName + '&gender=' + gender + '&keyword=' + key + '&zip=' + zip + '&radius=' + radius;
              
        window.open(URL);
    },

    clearForm: function() {
        var t = document.getElementsByTagName('input'), s = document.getElementsByTagName('select'), i;
        for (i = 0; i < t.length; i++) {
            if (t[i].type === 'text') {
                t[i].value = '';
            }
            if (t[i].type === 'checkbox') {
                t[i].checked = false;
            }
        }
        for (i = 0; i < s.length; i++) {
            s[i].selectedIndex = 0;
        }
        this.data.allChecked = false;
    },

    Check: function() {
        for (var i = 0; i < this.data.ListCheckbox.length; i++) {
            this.data.ListCheckbox[i].checked = true;
        }
        this.data.allChecked = true;
    },

    convertNull: function(v) {
        var retArr = new Array, i;
        if (!v) {
            return '';
        } else {
            if (v.type === 'text' || v.type === 'hidden') {
                return v.value;
            } else {
                if (v instanceof Array) {
                    for (i = 0; i < v.length; i++) {
                        if (v[i].checked === true) {
                            retArr[i] = v[i].value;
                        }
                    }
                    retArr = retArr.join(',');
                    return retArr;
                } else {
                    if (v.type === 'select-one') {
                        if (v.selectedIndex === 0) {
                            return '';
                        } else {
                            return v.options[v.selectedIndex].value;
                        }
                    }
                }
            }
        }
    },

    showAdvancedSearch: function() {
        this.IDs.AdvancedSearch.style.display = 'block';
        this.IDs.showOptions.style.display = 'none';
        this.IDs.hideOptions.style.display = 'block';
    },

    hideAdvancedSearch: function() {
        this.IDs.AdvancedSearch.style.display = 'none';
        this.IDs.showOptions.style.display = 'block';
        this.IDs.hideOptions.style.display = 'none';

    }

};
Event.onDOMReady(SEARCH.init, SEARCH, true);







