﻿function init() {
    var spec = document.getElementById('specialty');
    var ins = document.getElementById('insurance');

    if (spec !== null) {
        spec.onactivate = function () { window.dropdown_menu_hack(this); };
    }

    if (ins !== null) {
        ins.onactivate = function () { window.dropdown_menu_hack(this); };
    }
}

function Trim(s) {
    return s.replace(/^\s+|\s+$/g, "");
}

function toggleDiv(val) {
    var elem
   
    if (document.getElementById) // this is the way the standards work    
        elem = document.getElementById(val);
    else if (document.all) // this is the way old msie versions work      
        elem = document.all[val];
    else if (document.layers) // this is the way nn4 works    
        elem = document.layers[val];

    if (elem.style.display == 'none') {
        elem.style.display = 'block';      
    }
    else {
        elem.style.display = 'none';
    }
}

function showHide(el, id) {
    var e = document.getElementById(id);

    if (el !== null) {
        if (el.checked === true) {
            e.style.display = '';
        }
        else {
            e.style.display = 'none';
        }
    }
}

function manual_reset() {
    document.searchform.reset();
    return false;
}


function manual_reset2() {
    document.searchform.last.value = '';
    document.searchform.specialty.value = '';
    document.searchform.insurance.value = '';
    document.searchform.geocode.value = '';
    document.searchform.radius.value = '';
    document.searchform.language.value = '';
    document.searchform.keyword.value = '';
    document.getElementById('gender2').checked = false;
    document.getElementById('gender3').checked = false;
    return false;
}


function validate() {
    var ele = document.getElementById('searchform');

    if (ele !== null) {
        if (ele.radius.options[ele.radius.selectedIndex].value !== '' && ele.geocode.value === '') {
            alert('Please enter an address or zipcode when selecting a distance from location.');
            return false;
        }

        if (ele.radius.options[ele.radius.selectedIndex].value === '' && (ele.geocode.value.length > 0 && ele.geocode.value.length !== 5)) {
            alert('Please select a distance from location when specifying from your location.');
            return false;
        }

        if (ele.keyword.value === 'Type in disease, treatment or specialty') {
            ele.keyword.value = '';
        }
    }

    return true;
}

function refreshSearch(parameter, url) {    
    if (parameter=="") {
        return;
    }
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    }
    else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {            
 
        }
    }

    xmlhttp.open("GET","", true);
    xmlhttp.send();
}

function JumpSelect(o) {
    if (o.value && o.value !== '') {
        document.location.href = o.value;
    }
}


function hasOptions(obj) {
    if (obj != null && obj.options != null) { return true; }
    return false;
}

function selectUnselectMatchingOptions(obj, regex, which, only) {
    if (window.RegExp) {
        if (which == "select") {
            var selected1 = true;
            var selected2 = false;
        }
        else if (which == "unselect") {
            var selected1 = false;
            var selected2 = true;
        }
        else {
            return;
        }
        var re = new RegExp(regex);
        if (!hasOptions(obj)) { return; }
        for (var i = 0; i < obj.options.length; i++) {
            if (re.test(obj.options[i].text)) {
                obj.options[i].selected = selected1;
            }
            else {
                if (only == true) {
                    obj.options[i].selected = selected2;
                }
            }
        }
    }
}

function selectMatchingOptions(obj, regex) {
    selectUnselectMatchingOptions(obj, regex, "select", false);
}

function selectOnlyMatchingOptions(obj, regex) {
    selectUnselectMatchingOptions(obj, regex, "select", true);
}

function unSelectMatchingOptions(obj, regex) {
    selectUnselectMatchingOptions(obj, regex, "unselect", false);
}

function sortSelect(obj) {
    var o = new Array();
    if (!hasOptions(obj)) { return; }
    for (var i = 0; i < obj.options.length; i++) {
        o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
    }
    if (o.length == 0) { return; }
    o = o.sort(
		function (a, b) {
		    if ((a.text + "") < (b.text + "")) { return -1; }
		    if ((a.text + "") > (b.text + "")) { return 1; }
		    return 0;
		}
		);

    for (var i = 0; i < o.length; i++) {
        obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    }
}

function selectAllOptions(obj) {
    if (!hasOptions(obj)) { return; }
    for (var i = 0; i < obj.options.length; i++) {
        obj.options[i].selected = true;
    }
}

function removeSelectedOptions(from) {
    if (!hasOptions(from)) { return; }
    if (from.type == "select-one") {
        from.options[from.selectedIndex] = null;
    }
    else {
        for (var i = (from.options.length - 1); i >= 0; i--) {
            var o = from.options[i];
            if (o.selected) {
                from.options[i] = null;
            }
        }
    }
    from.selectedIndex = -1;
}

function addOption(obj, text, value, selected) {
    if (obj != null && obj.options != null) {
        obj.options[obj.options.length] = new Option(text, value, false, selected);
    }
}

function swapOptions(obj, i, j) {
    var o = obj.options;
    var i_selected = o[i].selected;
    var j_selected = o[j].selected;
    var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    var temp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
    o[i] = temp2;
    o[j] = temp;
    o[i].selected = j_selected;
    o[j].selected = i_selected;
}

function moveOptionUp(obj) {
    if (!hasOptions(obj)) { return; }
    for (i = 0; i < obj.options.length; i++) {
        if (obj.options[i].selected) {
            if (i != 0 && !obj.options[i - 1].selected) {
                swapOptions(obj, i, i - 1);
                obj.options[i - 1].selected = true;
            }
        }
    }
}

function moveOptionDown(obj) {
    if (!hasOptions(obj)) { return; }
    for (i = obj.options.length - 1; i >= 0; i--) {
        if (obj.options[i].selected) {
            if (i != (obj.options.length - 1) && !obj.options[i + 1].selected) {
                swapOptions(obj, i, i + 1);
                obj.options[i + 1].selected = true;
            }
        }
    }
}

