﻿function $mlg() {
    $drx = new $drx();
    getFont();
    this.getMedicineCabinet();
    //this.didyouknow();
    this.isCalendar('calendarControl');
    this.divZip_id = 'divZip_autocomplete';
    this.divDrug_id = 'divDrug_autocomplete';
    //this.disabledDefaultButton();
    this.pageload_getZipCodes();
    this.pageload_queryZip();
    this.isPageLoad = true;
    this.pageload_getdrugs();
    this.currentZip;
    this.currentFips;
    //getSessionZip();
    this.redirect = false;
    this.testimonial();
    this.participating_plans(false);
    this.testimonialInt = -1;
    this.participatingInt = -1;
    this.participatingBool = false;
    this.rng_participating;
    this.timeOutMins = 60000 * sessionTime;
    this.startTimeout = new Date();
    this.endTimeout = new Date();
    this.initTimeoutMethod();
    this.plantType = this.getPlanType();
    this.loadDefaultPlanType();
    this.preloadpaging();
    this.query_drugs();
    this.loc = this.getPageName();
    //getDemographics();
    this.medigapModal();
    this.medigapModal2();
    this.loadphone();
}


$mlg.prototype.loadphone = function() {
if ($get('default_page') == null && $get('phone_number') != null) {
        $get('phone_number').removeAttribute('class');
        $get('phone_number').removeAttribute('className');
    }
}

$mlg.prototype.medigapModal2 = function() {
    if ($get('Div2')) {
        this.pageDimmer();
        this.lightBox('<div id=\"mediplaceholder\"></div>', 500);
        $get('mediplaceholder').appendChild($get('Div2'));
    }
}

$mlg.prototype.medigapModal = function() {
    if ($get('medigap_modal')) {
        this.pageDimmer();
        this.lightBox('<div id=\"mediplaceholder\"></div>', 500);
        $get('mediplaceholder').appendChild($get('medigap_modal'));

        if ($get('mediplaceholder').getElementsByTagName('strong')[0].innerHTML.length == 1) {
            if ($get('searchdrug_left').getElementsByTagName('table')[0].getElementsByTagName('h2')[0] != null && $get('searchdrug_left').getElementsByTagName('table')[0].getElementsByTagName('h2')[1] != null) {
                $get('mediplaceholder').getElementsByTagName('strong')[0].innerHTML = $get('searchdrug_left').getElementsByTagName('table')[0].getElementsByTagName('h2')[parseInt($get('mediplaceholder').getElementsByTagName('strong')[0].innerHTML) - 1].innerHTML;
            }
        }
    }
}

//category, action, optional_label, optional_value

$mlg.prototype.getPageName = function() {
    var url = window.location.toString();
    var page = url.split('/')[url.split('/').length - 1];
    if (page.indexOf('?') > -1) {
        return page.split('?')[0];
    }
    return page;
}

$mlg.prototype.tr = function(category, action, value) {
    var enumeration = 1;
    //alert(category + ',' + this.getPageName() + "_" + action + ',' + value + ',' + enumeration);
    pageTracker._trackEvent(this.getPageName(), action , value , enumeration);
    //pageTracker._trackEvent(category, this.getPageName() + "_" + action, value, enumeration);
}



$mlg.prototype.query_drugs = function() {
    if (window.location.toString().indexOf('drugname') > -1) {
        if ($get('top_next')) {
            $get('top_next').removeAttribute('class');
            $get('top_next').removeAttribute('className');
            $get('top_next').setAttribute('class', 'btn_block');
            $get('top_next').setAttribute('className', 'btn_block');
        }
    }
}

$mlg.prototype.fixplanUI = function() {
    if ($get('divplans')) {
        //$get('divplans').getElementsByTagName('div')[0].style.height = parseFloat($get('didyouknow').offsetHeight + $get('medicineCabinet').offsetHeight) + "px";
    }
}

$mlg.prototype.updateEstimates = function(value) {
    if (currentType == $get('divplans').getElementsByTagName('table')[0].id) {
        currentType = '';
    }
    var index;
    if ($get('divplans').getElementsByTagName('table')[0].id == 'PDP')
        index = 0;
    else if ($get('divplans').getElementsByTagName('table')[0].id == 'MAPD')
        index = 1;
    else if ($get('divplans').getElementsByTagName('table')[0].id == 'MA')
        index = 2;
    else if ($get('divplans').getElementsByTagName('table')[0].id == 'Medigap')
        index = 3;

        $get('isMonthly').value = value;

    ShowPlans($get('divplans').getElementsByTagName('table')[0].id, '', index, '', value,false);
}

$mlg.prototype.getPlanType = function() {
    if ($get('tabviewState')) {
        return $get('tabviewState').value;
    }
}

$mlg.prototype.getMonthly = function() {
    if ($get('isMonthly')) {
        return $get('isMonthly').value;  
    }
}

$mlg.prototype.togggle_view = function(id) {
    if ($get(id)) {
        if ($get(id).className.indexOf('disabled') > -1) {
            $get(id).setAttribute('class', 'inner');
            $get(id).setAttribute('className', 'inner');
        }
        else {
            $get(id).setAttribute('class', 'inner disabled');
            $get(id).setAttribute('className', 'inner disabled');           
        }
    }
}

$mlg.prototype.toggle_planDetail = function(obj, index, div_cost, div_coverage) {
    var tabs = obj.parentNode.getElementsByTagName('a');
    for (var t = 0; t < tabs.length; t++) {
        tabs[t].removeAttribute('class');
        tabs[t].removeAttribute('className');
    }
    tabs[index].setAttribute('class', 'on');
    tabs[index].setAttribute('className', 'on');
    if (index == 1) {
        $get(div_coverage).setAttribute('class', 'disabled');
        $get(div_coverage).setAttribute('className', 'disabled');
        $get(div_cost).removeAttribute('class');
        $get(div_cost).removeAttribute('className');
    }
    else if(index == 0) {
        $get(div_coverage).removeAttribute('class');
        $get(div_coverage).removeAttribute('className');
        $get(div_cost).setAttribute('class', 'disabled');
        $get(div_cost).setAttribute('className', 'disabled');  
    }

}

$mlg.prototype.genericToggle = function(id, obj) {
    if ($get(id).style.display == 'none') {
        $get(id).style.display = '';
        obj.parentNode.removeAttribute('class');
        obj.parentNode.removeAttribute('className');
        obj.parentNode.setAttribute('class', 'uncollapsed');
        obj.parentNode.setAttribute('className', 'uncollapsed');
    }
    else {
        $get(id).style.display = 'none';
        obj.parentNode.removeAttribute('class');
        obj.parentNode.removeAttribute('className');
        obj.parentNode.setAttribute('class', 'collapseable');
        obj.parentNode.setAttribute('className', 'collapseable');
    }
}

$mlg.prototype.EmailPage = function(baseURL, pageName) {
    window.open(baseURL + pageName, 'emailpage', 'status=1,scrollbars=1,toolbar=0,menubar=0,resizable=1,width=680,height=680');
}

$mlg.prototype.preloadpaging = function() {
    if (window.location.toString().indexOf('/SearchDrug.aspx') > -1) {
        if ($get('drug_paging') && $get('drugsearchresultclient')) {
            pagingInt = 1;
            this.createPaging(false, $get('drugsearchresult_container').getElementsByTagName('div')[0].id, DrugpagingRange, 'drug_paging');
        }
    }
}

$mlg.prototype.isCalendar = function(id) {
    if ($get(id)) {
        $calendar = new $calendar();
    }
}

$mlg.prototype.isDemographics = function() {
    if ($get('demographics') && $get('demographics').getElementsByTagName('div')[0]) {
        this.pageDimmer();
        this.lightBox('<div id=\"demoRedisplay\"></div>', 500);
        $get('demoRedisplay').appendChild($get('demographics').getElementsByTagName('div')[0]);
        return false;
    }
    return true;
}

$mlg.prototype.isMedigap = function() {
// bool value need not object existence.
if ($get('medigapanswers') && $get('medigapanswers').getElementsByTagName('div')[0]) {
        this.pageDimmer();
        this.lightBox('<div id=\"demoRedisplay\"></div>', 500);
        $get('demoRedisplay').appendChild($get('medigapanswers').getElementsByTagName('div')[0]);
        return false;
    }
    return true;
}

$mlg.prototype.saveDemographics = function() {
    if ($get('lightBoxContainer').innerHTML != '') {
        //return false;
    }
    else {
        //return false;
    }
}

$mlg.prototype.closeDemographics = function() {
    //this.globalModalCloser();
    getSessionDemographics();
}

$mlg.prototype.table_reconstruction = function(type) {
    if ($get('divplans')) {
        var table = $get('divplans').getElementsByTagName('table')[0];
        for (var r = 0; r < table.getElementsByTagName('tr').length; r++) {
            if (table.getElementsByTagName('tr')[r].getElementsByTagName('td')[2] && table.getElementsByTagName('tr')[r].getElementsByTagName('td')[2].innerHTML != type) {
                table.getElementsByTagName('tr')[r].parentNode.removeChild(table.getElementsByTagName('tr')[r]);
            }
        }
    }
}

$mlg.prototype.formatPlanTable = function() {
    if ($get('divplans')) {
        var type = $get('divplans').getElementsByTagName('table')[0].id
        switch (type) {
            case 'PDP':
                this.table_reconstruction('PDP');
                break;
            case 'MAPD':
                this.table_reconstruction('MAPD');
                break;
            case 'MA':
                this.table_reconstruction('MAPD');
                break;
        }
    }
}

$mlg.prototype.loadDefaultPlanType = function() {
    if ($get('divplans')) {
        if (this.plantType == 'PDP') {
            ShowPlans(this.plantType, '', 0, '', this.getMonthly(),false);
        }
        else if (this.plantType == 'MAPD') {
            ShowPlans(this.plantType, '', 1, '', this.getMonthly(), false);
        }
        else if (this.plantType == 'MA') {
            ShowPlans(this.plantType, '', 2, '', this.getMonthly(), false);
        }
        else if (this.plantType == 'Medigap') {
            ShowPlans(this.plantType, '', 3, '', this.getMonthly(), false);
        }
    }
}

$mlg.prototype.testimonial = function() {
    this.rng_display_testimonial('testimonial');
}

$mlg.prototype.removeArrowEvent = function(e, bool) {
    if (bool) {
        document.onkeydown = function() { return e.keyCode == 38 || e.keyCode == 40 ? false : true; }
    }
}

// dependancies on scriptaculous
$mlg.prototype.updateFocus = function(obj, e) {
    this.removeArrowEvent(e, true);
    if (e.keyCode == 40) {
        if (obj.parentNode.nextSibling && obj.parentNode.nextSibling.getElementsByTagName('a')[0]) {
            obj.parentNode.nextSibling.getElementsByTagName('a')[0].focus();
        }
        else {
            obj.parentNode.parentNode.getElementsByTagName('a')[0].focus();
        }
        return false;
    }
    if (e.keyCode == 38) {
        if (obj.parentNode.previousSibling && obj.parentNode.previousSibling.getElementsByTagName('a')[0]) {
            obj.parentNode.previousSibling.getElementsByTagName('a')[0].focus();
        }
        else {
            obj.parentNode.parentNode.getElementsByTagName('a')[obj.parentNode.parentNode.getElementsByTagName('a').length - 1].focus();
        }
        return false;
    }
}

$mlg.prototype.participating_plans = function(reset) {
    if (reset) {
        this.participatingBool = reset;
        clearTimeout(this.rng_participating);
        if ($get('participating_display').getElementsByTagName('img').length != $get('participating_resource').getElementsByTagName('img').length) {
            $get('participating_display').innerHTML = $get('participating_resource').innerHTML;
        }
        else {
            this.participatingBool = false;
            this.participating_plans(false);
        }
    }
    else {
        this.rng_display_participating('participating');
    }
}

$mlg.prototype.rng_display_participating = function(id) {
    var el = $get(id + '_resource');
    if (el != null) {
        var redisplay = $get(id + '_display');
        var ranNum = Math.floor(Math.random() * el.getElementsByTagName('img').length);
        if (ranNum == this.testimonialInt) {
            this.participating_plans();
            return false;
        }
        redisplay.innerHTML = '<img src=\"' + el.getElementsByTagName('img')[ranNum].src + '\"/>';
        this.participatingInt = ranNum;
        if (!this.participatingBool) {
            this.rng_participating = setTimeout("$mlg.participating_plans(false)", 5000);
        }
    }
}

$mlg.prototype.rng_display_testimonial = function(id) {
    var el = $get(id + '_resource');
    if (el != null) {
        var redisplay = $get(id + '_display');
        var ranNum = Math.floor(Math.random() * el.getElementsByTagName('div').length);
        if (ranNum == this.testimonialInt) {
            this.testimonial();
            return false;
        }
        redisplay.innerHTML = '<div>' + el.getElementsByTagName('div')[ranNum].innerHTML + '</div>';
        this.testimonialInt = ranNum;
        setTimeout("$mlg.testimonial()", 10000);
    }
}

$mlg.prototype.globalClickEvents = function() {
    //this.extendClock(false, false);
}

$mlg.prototype.timeOutUI = function() {
    $mlg.tr('session timeout dialog showed', $mlg.loc);
    var sb = new Array();
    sb.push('<strong>Your Session Has Timed Out</strong>');
    sb.push('<p>In order to protect your information, your session has timed out. Please close your browser or click the \"Start Over\" button below.</p>');
    sb.push('<div class=\"timeout_btn\"><input type=\"button\" onclick=\"$mlg.extendClock(true, true)\" value=\"Start Over\" />');

    return sb.join("");
}

$mlg.prototype.initTimeoutMethod = function() {
    this.endTimeout.setTime(parseFloat(this.startTimeout.getTime()) + this.timeOutMins);
    setTimeout("$mlg.checkTimeout()", this.timeOutMins);
}

$mlg.prototype.extendClock = function(RequireRequest, isForceRedirect) {
    this.startTimeout = new Date();
    this.endTimeout = new Date();
    this.endTimeout.setTime(parseFloat(this.startTimeout.getTime()) + this.timeOutMins);
    var TimeoutPrevent = document.createElement('img');
    TimeoutPrevent.src = 'SessionTimeOutRequest.aspx';
    $get('lightBoxContainer').appendChild(TimeoutPrevent);
    if ($get('lightBoxContainer')) {
        $get('lightBoxContainer').innerHTML = '';
    }
    if (isForceRedirect) {
        this.redirect = true;
    }
    if (RequireRequest) {
        sessionRequest();
    }
}

$mlg.prototype.globalModalCloser = function() {
    var selects = document.getElementsByTagName('select');
    for (var s = 0; s < selects.length; s++) {
        selects[s].style.visibility = '';
    }
    $get('lightBoxContainer').innerHTML = '';
}

$mlg.prototype.checkTimeout = function() {
    this.startTimeout = new Date();
    var check = this.endTimeout.getTime() - this.startTimeout.getTime();
    if (this.startTimeout.getTime() >= this.endTimeout.getTime()) {
        $mlg.pageDimmer();
        $mlg.lightBox(this.timeOutUI(), 300);

    }
}

$mlg.prototype.didyouknow = function() {
    if ($get('didyouknow')) {
        getRandomResources(0);
    }
}

$mlg.prototype.hidemedicineCabinet = function(obj) {
    $mlg.tr('Drug', 'Link_HideMC', 'click');
    if ($get("medicineCabinet")) {
        if ($get("medicineCabinet").getElementsByTagName('tbody')[0].style.display == '') {
            $get("medicineCabinet").getElementsByTagName('tbody')[0].style.display = 'none';
            obj.innerHTML = 'Show All';
            obj.style.backgroundPosition = 'bottom right';
        }
        else {
            $get("medicineCabinet").getElementsByTagName('tbody')[0].style.display = '';
            obj.innerHTML = 'Hide All';
            obj.style.backgroundPosition = 'top right';
        }
    }
}

//pagingRange
//pagingInt

$mlg.prototype.pageEvent = function(num, containerID, showall, pagingRange, pagerID) {
    if ($get(containerID) && $get(containerID).getElementsByTagName('table')[0]) {

        var tableRow = $get(containerID).getElementsByTagName('tr');

        if (showall) {
            for (var s = 1; s < tableRow.length; s++) {
                if (tableRow[s].style.display == 'none') {
                    tableRow[s].style.display = '';
                }
            }
        }
        else {
            for (var t = 1; t < tableRow.length; t++) {
                if (tableRow[t].style.display == '') {
                    tableRow[t].style.display = 'none';
                }
                var intSpacer = 0;
                if (num != 0) {
                    intSpacer = 1;
                }
                if (t >= parseFloat(num + intSpacer) && t <= parseFloat((num + pagingRange))) {
                    tableRow[t].style.display = '';
                }
            }
        }
        pagingInt = parseFloat((num / pagingRange) + 1);
        this.createPaging(showall, containerID, pagingRange, pagerID);
    }
}

$mlg.prototype.alphaSearch = function(obj) {
    var value = obj.innerHTML;
    obj.href = 'SearchDrug.aspx?drugname=' + value;
    return false;
    //$get(this.divDrug_id).getElementsByTagName('input')[0].value = value;
    //SearchDrugs(value, null);
}

$mlg.prototype.createPaging = function(showall, containerID, pagingRange, pagerID) {
    var sb = new Array();
    if (showall) {
        sb.push('<a title=\"Show ' + pagingRange + ' results only\" href=\"javascript:$mlg.pageEvent(0, \'' + containerID + '\', false, ' + pagingRange + ', \'' + pagerID + '\')\">');
        sb.push('Show ' + pagingRange + ' results only');
        sb.push('</a>');
    }
    else {
        var drugRows = $get(containerID).getElementsByTagName('tr');
        var leftover = ((drugRows.length - 1) % pagingRange);
        var previousInt = parseFloat((pagingInt - 2) * pagingRange);
        var nextInt = parseFloat((pagingInt) * pagingRange)
        if ((pagingRange) < ($get(containerID).getElementsByTagName('tr').length - 1)) {
            if (nextInt > drugRows.length) {
                nextInt = parseFloat(((drugRows.length) - leftover));
            }

            var addLeftover = 0;
            if (leftover > 0)
                addLeftover = pagingRange;

            if (parseFloat(previousInt) > -1) {
                sb.push('<a title=\"Previous\" href=\"javascript:$mlg.pageEvent(' + previousInt + ', \'' + containerID + '\', false, ' + pagingRange + ',  \'' + pagerID + '\')\">');
                sb.push('< Previous');
                sb.push('</a>');
            }
            else {
                sb.push('<span>< Previous</span>');
            }
            for (var p = 1; p < drugRows.length + addLeftover; p++) {
                if (p % pagingRange == false) {
                    if (parseFloat(p / pagingRange) == pagingInt) {
                        sb.push('<strong>[' + parseFloat(p / pagingRange) + ']</strong>');
                    }
                    else {
                        sb.push('<a title=\"' + p / pagingRange + '\" href=\"javascript:$mlg.pageEvent(' + parseFloat((p - pagingRange)) + ', \'' + containerID + '\', false, ' + pagingRange + ', \'' + pagerID + '\')\">');
                        sb.push(p / pagingRange);
                        sb.push('</a>');
                    }
                }
            }
            var addpage = 0;
            if (leftover == 0)
                addpage = pagingRange;
            else
                addpage = leftover;

            if ((((pagingInt * pagingRange) - pagingRange) + addpage) == drugRows.length - 1) {
                sb.push('<span>Next ></span>');
            }
            else {
                sb.push('<a title=\"Next\" href=\"javascript:$mlg.pageEvent(' + nextInt + ', \'' + containerID + '\', false, ' + pagingRange + ', \'' + pagerID + '\')\">');
                sb.push('Next >');
                sb.push('</a>');
            }
            sb.push('<a  title=\"show all\" href=\"javascript:$mlg.pageEvent(0,\'' + containerID + '\', true, ' + pagingRange + ', \'' + pagerID + '\')\">');
            sb.push('show all');
            sb.push('</a>');
        }
    }
    $get(pagerID).innerHTML = sb.join("");
    //this.drugSearchUIUpdate();
}

$mlg.prototype.updateZip = function() {
    if ($get('divZip_autocomplete')) {
        $get('divZip_autocomplete').getElementsByTagName('input')[0].value = '';
        $get('divZip_autocomplete').getElementsByTagName('input')[0].focus();
    }
}

$mlg.prototype.pageload_queryZip = function() {
    if (this.zipControl == null && $get('divZip_autocomplete')) {
        this.zipControl = $get('divZip_autocomplete').getElementsByTagName('input')[0];
        if (window.location.toString().indexOf('zip=') > -1) {
            insertZipandFips($get(this.zipControl.id).value, null, false);
        }
    }
}

$mlg.prototype.disabledDefaultButton = function(event) {
    var input = document.createElement('input');
    input.setAttribute('class', 'disabled');
    input.setAttribute('className', 'disabled');
    document.body.getElementsByTagName('form')[0].appendChild(input);
}

$mlg.drugControl;

$mlg.prototype.getDrugs = function(obj, event) {
    if (event.keyCode == 40) {
        if ($mlg.drugControl.id != null && $get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a')[0]) {
            $get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a')[0].focus();
            return false;
        }
    }
    if (event.keyCode == 38) {
        if ($mlg.drugControl.id != null && $get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a')[$get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a').length - 1]) {
            $get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a')[0].focus();
            return false;
        }
    }
    getDrugsAutoComplete(obj);
    if (event.keyCode == 13) {
        if ($get($mlg.drugControl.id + '_drugResult') && $get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a').length == 1) {
            var command = unescape($get($mlg.drugControl.id + '_drugResult').getElementsByTagName('a')[0].onclick());
            $mlg.tr('Drug', 'Insert_click', drug_input.value);
            //eval(command);
        }
        else {
            window.location = 'SearchDrug.aspx?drugname=' + obj.value;
            $mlg.tr('Drug', 'Search', obj.value);
            //SearchDrugs(obj.value, null);
        }
    }
}

$mlg.prototype.getZipCodes = function(obj, event) {
    this.isPageLoad = false;
    if (event.keyCode == 40) {
        if ($mlg.zipControl.id != null && $get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0]) {
            $get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].focus();
            return false;
        }
    }
    if (event.keyCode == 38) {
        if ($mlg.zipControl.id != null && $get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[$get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a').length - 1]) {
            $get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].focus();
            return false;
        }
    }
    getZipCodes(obj);

    if (event.keyCode == 13) {
        $mlg.tr('ZIP', 'Insert_enterkey', obj.value);
        if (obj.value.length < 4) {
            this.validation_message(obj);
        }
        else {
            if ($get($mlg.zipControl.id + '_zipResult') && $get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a').length == 1) {
                //insertZipandFips(obj.value, null, false);
                var command = unescape($get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].onclick());
                eval(command);
            }
            else {
                if ($get($mlg.zipControl.id + '_zipResult')) {
                    if (($get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].id.split('_')[1] == $get($mlg.zipControl.id).value) && ($get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].id.split('_')[2] == '0') && $get('zip_' + $get($mlg.zipControl.id).value + '_1') == null) {
                        var command = unescape($get($mlg.zipControl.id + '_zipResult').getElementsByTagName('a')[0].onclick());
                        eval(command);
                    }
                    else {
                        this.validation_message(obj);
                        insertZipandFips(obj.value, null, false);
                    }
                }
                else {
                    this.validation_message(obj);
                }
            }
        }
    }
    else {
        var error = $get(this.zipControl.id + '_error_msg');
        if (error) {
            error.parentNode.removeChild(error);
        }
    }
}


$mlg.prototype.multipleFips = function(obj) {
    var radios = $get('lightBox_fips').getElementsByTagName('input');
    var res = false;
    for (var r = 0; r < radios.length; r++) {
        if (radios[r].type == 'radio') {
            if (radios[r].checked) {
                this.currentFips = radios[r].value;
                this.currentZip = $get(this.zipControl.id).value;
                insertZipandFips($get(this.zipControl.id).value, radios[r].value, true);
                res = true;
            }
        }
    }
    if (!res) {
        if ($get('fips_validation') == null) {
            var val = document.createElement('span');
            val.id = 'fips_validation';
            val.setAttribute('class', 'validation');
            val.setAttribute('className', 'validation');
            val.innerHTML = '<span>Select a County</span>';
            obj.parentNode.appendChild(val);
        }

    }
}

$mlg.prototype.pageload_getdrugs = function() {
    if (!$get(this.divDrug_id)) return;
    var drug_input = $get(this.divDrug_id).getElementsByTagName('input')[0];
    var drug_lnk = $get(this.divDrug_id).getElementsByTagName('a')[0];
    drug_lnk.onclick = function() {
        if (drug_input.value.length > 0) {
            if ($get(drug_input.id + '_drugResult') && $get(drug_input.id + '_drugResult').getElementsByTagName('a').length == 1) {
                var command = unescape($get(drug_input.id + '_drugResult').getElementsByTagName('a')[0].onclick());
                $mlg.tr('Drug', 'Insert_click', drug_input.value);
                //eval(command);
            }
            else {
                window.location = 'SearchDrug.aspx?drugname=' + drug_input.value;
                $mlg.tr('Drug', 'Search', drug_input.value);
                //SearchDrugs(drug_input.value, null);
                return false;
            }
            return false;
        }
        else {
            return false;
        }
    }
    drug_input.autocomplete = 'off';
    drug_input.maxLength = 50;
}

$mlg.prototype.validation_message = function(control) {
if ($get(control.id + '_error_msg') == null) {
        var error = document.createElement('span');
        error.id = control.id + '_error_msg';
        error.setAttribute('class', 'error_msg');
        error.setAttribute('className', 'error_msg');
        error.innerHTML = '<span>We\'re sorry, but the ZIP code you entered could not be found. Please check your ZIP code and try again. If you continue to have this issue, please try another ZIP code in the same county.</span>';
        control.parentNode.parentNode.appendChild(error);
    }
    var autocompleter = $get(this.zipControl.id + '_zipResult');
    if (autocompleter) {
        autocompleter.parentNode.removeChild(autocompleter);
    }
    return false;
}

$mlg.prototype.zip_link_onclick = function(obj, zipcode) {

    var zipvalue = $get('divZip_autocomplete').getElementsByTagName('input')[0].value;
    if (zipvalue && zipvalue.length == 5) {
        insertZipandFips(zipvalue, null, false);
    }
    else {
        this.validation_message(this.zipControl);

    }

}

$mlg.prototype.pageload_getZipCodes = function() {
    if (!$get(this.divZip_id)) return;
    var zip_input = $get(this.divZip_id).getElementsByTagName('input')[0];
    var zip_link = $get(this.divZip_id).getElementsByTagName('a')[0];

    getZipCodes(zip_input);

    zip_link.onclick = function() {
        $mlg.zip_link_onclick(this, zip_input.value);
        $mlg.tr('ZIP', 'Insert_click', zip_input.value);
        return false;
    }

    zip_input.autocomplete = 'off';

    var fipsvalue = $get('divZip_autocomplete').getElementsByTagName('input')[1].value;
    if (fipsvalue == '2')
        insertZipandFips(zip_input.value, null, false);
    //        if ($get(this.zipControl.id + '_zipResult') && $get(this.zipControl.id + '_zipResult').getElementsByTagName('a').length > 1) {
    //            insertZipandFips(zipcode, null, false);

}

$mlg.prototype.zipObject = new Array();
$mlg.prototype.zipControl;
$mlg.prototype.control_zip = function() {
    if (this.zipObject.length > 0 && this.zipControl && this.zipControl.value != '') {
        var formatEventParams = new Array();
        var formatContent = new Array();
        for (var p = 0; p < this.zipObject[3].length; p++) {
            formatEventParams.push(this.zipObject[1][p] + ',' + this.zipObject[3][p]);
            formatContent.push(this.zipObject[0][p] + ', ' + this.zipObject[2][p] + ' ' + this.zipObject[1][p]);
        }
        if ($get(this.zipControl.id + '_zipResult')) {
            $get(this.zipControl.id + '_zipResult').innerHTML = this.dropdownBuilder('$mlg.zip_insert', formatEventParams, formatContent);
        }
        else {
            var zipResult = document.createElement('div');
            zipResult.setAttribute('class', 'zip_dropdown');
            zipResult.setAttribute('className', 'zip_dropdown');
            zipResult.id = this.zipControl.id + '_zipResult';
            zipResult.innerHTML = this.dropdownBuilder('$mlg.zip_insert', formatEventParams, formatContent);
            this.zipControl.parentNode.appendChild(zipResult);
        }
    }
    else {
        if ($get(this.zipControl.id + '_zipResult')) {
            $get(this.zipControl.id + '_zipResult').parentNode.removeChild($get(this.zipControl.id + '_zipResult'));
        }
    }
}

$mlg.prototype.zip_insert = function(zip, fips) {
    this.zipControl.value = zip;
    $get(this.zipControl.id + '_zipResult').parentNode.removeChild($get(this.zipControl.id + '_zipResult'));
}

$mlg.prototype.dropdownBuilder = function(clickEvent, eventParams, content) {
    var sb = new Array();
    sb.push('<div>');
    for (var i = 0; i < content.length; i++) {
        sb.push('<span><a href=\"javascript:void(' + eventParams[i] + ')\" onclick=\"' + clickEvent + '(' + eventParams[i] + ')\">' + content[i] + '</a><\/span>');
    }
    sb.push('<\/div>');
    return sb.join('');
}

// Global
Array.prototype.clear = function() {
    this.length = 0;
}


$mlg.prototype.layerDimensions = function(offsetHeight, offsetWidth) {
    var dimmer = $get('lightBoxContainer').getElementsByTagName('div')[0];
    if ($get('lightBoxContainer') && dimmer) {
        var offsetHeight = document.body.offsetHeight;
        var offsetWidth = document.body.offsetWidth
        if ($get('pageWrapper').offsetHeight > document.body.offsetHeight) {
            offsetHeight = $get('pageWrapper').offsetHeight;
        }
        if ($get('pageWrapper').offsetWidth > document.body.offsetWidth) {
            offsetWidth = $get('pageWrapper').offsetWidth;
        }
        dimmer.style.height = offsetHeight + 'px';
        dimmer.style.width = offsetWidth + 'px';
    }
}

$mlg.prototype.drugSearchUIUpdate = function() {
    if ($get('searchdrug_right') && $get('searchdrug_left')) {
        var winAddition = 0;
        if (!document.all) {
            winAddition = 0;
        }
        var offsetHeight;
        if ($get('searchdrug_right').offsetHeight < $get('searchdrug_control').offsetHeight + $get('drugsearchresult').offsetHeight) {
            offsetHeight = $get('searchdrug_control').offsetHeight + $get('drugsearchresult').offsetHeight + winAddition;
            $get('searchdrug_right').style.height = offsetHeight + 'px';
            $get('searchdrug_left').style.height = 'auto';
        }
        else {
            offsetHeight = $get('searchdrug_control').offsetHeight + $get('drugsearchresult').offsetHeight + winAddition;
            $get('searchdrug_right').style.height = 'auto';
            $get('searchdrug_left').style.height = 'auto';
        }
    }
}

$mlg.prototype.checkRadioLength = function(id) {
    var Rows = $get(id).getElementsByTagName('tr');
    if (Rows.length > 10) {
        $get(id).setAttribute('class', 'scrollable');
        $get(id).setAttribute('className', 'scrollable');
        if (document.all) {
            $get(id).getElementsByTagName('div')[0].style.width = $get(id).offsetWidth - 16 + 'px';
        }
    }
}

$mlg.prototype.pageDimmer = function() {
    var layer = document.createElement('div');
    layer.setAttribute('class', 'lightbox_dimmer');
    layer.setAttribute('className', 'lightbox_dimmer');
    var offsetHeight = document.body.offsetHeight;
    var offsetWidth = document.body.offsetWidth
    if ($get('pageWrapper').offsetHeight > document.body.offsetHeight) {
        offsetHeight = $get('pageWrapper').offsetHeight;
    }
    if ($get('pageWrapper').offsetWidth > document.body.offsetWidth) {
        offsetWidth = $get('pageWrapper').offsetWidth;
    }
    layer.style.height = offsetHeight + 'px';
    layer.style.width = offsetWidth + 'px';
    $get('lightBoxContainer').appendChild(layer);
}

$mlg.prototype.getMedicineCabinet = function() {
    if($get("medicineCabinet")) {
        GetMC();
    }
}
$mlg.prototype.selectedplans = '';
$mlg.prototype.CanCompare = function(obj) {
    if (this.selectedplans.split(",").length != 2) {
        this.pageDimmer();
        this.lightBox('<strong>Warning</strong><p>You need select 2 plans to compare</p><a href=\"javascript:void(0)\" onclick=\"$mlg.globalModalCloser()\" class=\"btn\"><span>Close</span></a>', 300);
    }
    else {
        obj.href = "PlanListcompare.aspx?planid1=" + this.selectedplans.split(",")[0] + "&planid2=" + this.selectedplans.split(",")[1];
    }
}

$mlg.prototype.AddSelectPlans = function(planid, obj) {
    if (obj.checked) {
        var temp = this.selectedplans.split(",");
        if (temp.length >= 2) {
            obj.checked = false;
            this.pageDimmer();
            this.lightBox('<p>You can select only 2 plans</p><a href=\"javascript:void(0)\" onclick=\"$mlg.globalModalCloser()\" class=\"btn\"><span>close</span></a>', 300);
        }
        else {
            if (this.selectedplans.indexOf(planid) == -1) {
                if (temp.length != 0 && temp[0] != '')
                    this.selectedplans += ",";
                this.selectedplans += planid;
            }
        }
    }
    else {
            this.selectedplans=this.selectedplans.replace(planid, "");
            this.selectedplans=this.selectedplans.replace(",", "");
    }
}

$mlg.prototype.lightBox = function(content, width) {
    var selects = document.getElementsByTagName('select');
    for (var s = 0; s < selects.length; s++) {
        if (selects[s].parentNode.name != 'showme') {
            selects[s].style.visibility = 'hidden';
        }
    }
    var lightbox = document.createElement('div');
    lightbox.setAttribute('class', 'lightbox');
    lightbox.setAttribute('className', 'lightbox');
    lightbox.innerHTML = content;
    lightbox.style.width = width + 'px';
    lightbox.style.marginLeft = -(width / 2) + 'px';
    $get('lightBoxContainer').appendChild(lightbox);
    window.scrollTo(0, 0);
}

$mlg.prototype.keyEvent = function(event, keycode) {
    var e;
    if (document.all) {
        e = window.event;
    }
    else {
        e = event;
    }
    if (e.keyCode == keycode) {
        return true;
    }
}

window.resize = function() {
    if ($mlg) {
        $mlg.layerDimensions();
    }
}

window.onload = function() {
       $mlg = new $mlg();
}

function PrintPage(baseURL, pageName) {
    window.open(baseURL + pageName, 'printpage','status=1,scrollbars=1,toolbar=1,menubar=1,resizable=1,width=1000,height=600');
}


function medigap_validation() {
    var chkboxes = $get('rpt_check').getElementsByTagName('input');
    for (var c = 0; c < chkboxes.length; c++) {
        if (chkboxes[c].type == 'checkbox' && !chkboxes[c].checked) {
            $get('checkbox_validation').innerHTML = '<ul><li><span>Please check each document after you have read it.</span></li></ul>';
            return false;
        }
    }
}