﻿// hide or unhide a div block of content
function visibleContent(id, show) {
    var element = document.getElementById(id);

    if (element != null) {
        if (show == true) {
            element.style.visibility = 'visible';
            element.style.display = 'block';
        }
        else {
            element.style.visibility = 'hidden';
            element.style.display = 'none';
        }
    }
}

function selectPrescriptionAtLuxStore() {
    visibleContent('dvClinic', true);
    visibleContent('dvDoctorDetails', false);
    document.getElementById('ddlCity').disabled = true;
    document.getElementById('ddlClinic').disabled = true;

    document.CLPINFO.isRdoClinicSelected.value = document.getElementById('rdoClinic').checked;
    document.CLPINFO.isRdoDoctorSelected.value = "false";
}

function selectPrescriptionAtAnotherLocation() {	
    visibleContent('dvClinic', false);
    visibleContent('dvDoctorDetails', true);
    document.CLPINFO.isRdoDoctorSelected.value = document.getElementById('rdoDoctor').checked;
    document.CLPINFO.isRdoClinicSelected.value = "false";
}

function selectState() 
{    
    if (document.getElementById('ddlState').selectedIndex == 0) {
        document.getElementById('ddlCity').disabled = true;
        document.getElementById('ddlClinic').disabled = true;
        document.getElementById('ddlClinic').text = "Clinic";

        ClearSelectList(document.getElementById('ddlCity'));
        ClearSelectList(document.getElementById('ddlClinic'));
    }
    else {
        ClearSelectList(document.getElementById('ddlCity'));
        ClearSelectList(document.getElementById('ddlClinic'));
        
        document.getElementById('ddlCity').disabled = false;

        //Get the selected state from the dropdownlist
        var selected_index =document.forms["CLPINFO"].elements["ddlState"].selectedIndex;
        if (selected_index > 0) {
            var selected_state_text = document.forms["CLPINFO"].elements["ddlState"].options[selected_index].text;
            
            if (selected_state_text != null) {
                document.forms["CLPINFO"].elements["ddlState"].options[selected_index].selected = true;
            }
        }

        //assign state to hidden variable       
        document.CLPINFO.isSelected.value = 'True';

        if (selected_state_text != null && selected_state_text != "") {
            //Ajax request
            var ajaxReq = new dsLib.AJAX.Request("/ajax/ClinicWiz.asp?state=" + selected_state_text, null, null, "GET", null, null, false);
            var respText = ajaxReq.responseText;

            //Create City Array and Bind it to city dropdownlist
            var cityList = new Array();

            if (respText.toLowerCase() != "not a valid request." && ajaxReq.responseXML != null) {
                var respXMLDoc = ajaxReq.responseXML;

                if (respXMLDoc.getElementsByTagName("Cities")[0].childNodes[0] != null) {
                    cityList = respXMLDoc.getElementsByTagName("Cities")[0].childNodes[0].nodeValue.split(",");
                }
            }
            var citySelect = new Array("City");
            cityList = citySelect.concat(cityList);
            if (cityList.length == 1) 
            {
                document.getElementById('ddlCity').disabled = true;
                document.getElementById('ddlClinic').disabled = true;
            }
            BindSelectList(cityList, document.getElementById("ddlCity"), "false");
            document.getElementById("state").value = selected_state_text;
        }       
    }
    if (document.getElementById('ddlState').selectedIndex == 0 || document.getElementById('ddlCity').selectedIndex == 0
        || document.getElementById('ddlClinic').selectedIndex == 0) {
        //Get the  save patient image path from hidden field and assign it to Save patient button
        document.getElementById('btnSavePatient').src = document.getElementById('DisableSavePatientButton').value;
        //Disable the save patient button
        document.getElementById('btnSavePatient').disabled = true;
        document.CLPINFO.isRdoClinicSelected.value = "true";
    }
}

function selectCity() 
{
    if (document.getElementById('ddlCity').selectedIndex == 0) {
        document.getElementById('ddlClinic').disabled = true;
        document.getElementById('ddlClinic').value = 0;

        ClearSelectList(document.getElementById('ddlClinic'));
    }
    else {
        ClearSelectList(document.getElementById('ddlClinic'));
        document.getElementById('ddlClinic').disabled = false;

        //Get the selected city from the dropdownlist
        var selected_index = document.forms["CLPINFO"].elements["ddlCity"].selectedIndex;
        if (selected_index > 0) {
            var selected_city_text = document.forms["CLPINFO"].elements["ddlCity"].options[selected_index].text;
        }

        var selected_index = document.forms["CLPINFO"].elements["ddlState"].selectedIndex;
        var selected_state_text = document.forms["CLPINFO"].elements["ddlState"].options[selected_index].text;
        
        if (selected_state_text != null && selected_state_text != "" && selected_city_text != null && selected_city_text != "") {
            //Ajax request
            var ajaxReq = new dsLib.AJAX.Request("/ajax/ClinicWiz.asp?state=" + selected_state_text + "&city=" + selected_city_text, null, null, "GET", null, null, false);
            var respText = ajaxReq.responseText;

            //Create Clinic Array and Bind it to Clinic dropdownlist
            var clinicList = new Array();

            if (respText.toLowerCase() != "not a valid request." && ajaxReq.responseXML != null) {
                var respXMLDoc = ajaxReq.responseXML;
                if (respXMLDoc.getElementsByTagName("Clinics")[0].childNodes[0] != null) {
                    clinicList = respXMLDoc.getElementsByTagName("Clinics")[0].childNodes[0].nodeValue.split(",");                   
                }
            }

            var clinicSelect = new Array("Clinic");
            clinicList = clinicSelect.concat(clinicList);            
            BindSelectList(clinicList, document.getElementById("ddlClinic"), "true");
            document.getElementById("city").value = selected_city_text;
        }
      }
}

function selectClinic() {
    
    //Get the selected value from the clinic dropdownlist
    var selected_index = document.forms["CLPINFO"].elements["ddlClinic"].selectedIndex;
    if (selected_index > 0) {
        document.CLPINFO.clinicID.value = document.forms["CLPINFO"].elements["ddlClinic"].options[selected_index].value;
        var selected_clinic_text = document.forms["CLPINFO"].elements["ddlClinic"].options[selected_index].text;
    }
    if (selected_index != 0) {
        //Get the  save patient image path from hidden field and assign it to Save patient button
        document.getElementById('btnSavePatient').src = document.getElementById('EnableSavePatientButton').value;
        //Enable the save patient button
        document.getElementById('btnSavePatient').disabled = false;
    }
    else 
    {
        document.getElementById('btnSavePatient').src = document.getElementById('DisableSavePatientButton').value;
        //Enable the save patient button
        document.getElementById('btnSavePatient').disabled = true;
    }
    document.getElementById("clinic").value = selected_clinic_text;
    document.CLPINFO.isSelected.value = 'False';
}

function BindSelectList(list, controlID, flag) 
{
    var option;
    for (var index = 0; index < list.length; index++) 
    {
        option = document.createElement("OPTION");
        if (flag == "true") {
            var clinicArray = list[index];
            clinicArray = clinicArray.split("|");                                    
            option.text = clinicArray[0];
            option.value = clinicArray[1];
            if (list.valueOf(1) == "Clinic") {
                document.getElementById('ddlClinic').disabled = true;
            }  
        }
        else 
        {                     
            option.text = list[index];
            option.value = list[index];           
        }
        controlID.options.add(option);
    }
} 

function ClearSelectList(controlID) 
{ 
    //Clear dropdownlist
    while (controlID.options.length > 0) {
        controlID.options[0] = null;
    }
}

function submitDoctorSearch() 
{
    document.CLPINFO.isSeachButtonClicked.value = 'True';

    if (formElementIsEmpty(document.CLPINFO.SearchCity) || formElementIsEmpty(document.CLPINFO.SearchState)) {
        alert('To search, you must provide a city and state. Please enter them and try again.');
    }
    else {
        document.CLPINFO.fSearchForDoctor.value = 'True';             
        document.CLPINFO.submit();
    }    
}

function formElementIsEmpty(element) {
    var setFlag = true;
    if (element != null) {
        var value = Trim(element.value);
        if (value.length > 0) {
            setFlag = false;
        }
    }
    return setFlag;
}

// for selecting the informaton from the Doctor Search results
function updateParent(name, phone) {
    document.CLPINFO.txtDoctorPhone_Search.value = phone;
    document.CLPINFO.txtDoctorName_Search.value = name;

    var checkboxSameDoc = document.CLPINFO.cbPrevDr;
    if (checkboxSameDoc != null) {
        checkboxSameDoc.checked = false;
    }
    // reload the page to the "Find My Doctor" anchor
    document.location = document.location.toString();
}

function setFields() {
    document.CLPINFO.isRdoDoctorSelected.value = "true";
    document.CLPINFO.isRdoClinicSelected.value = "false";
    document.CLPINFO.isSeachButtonClicked.value = '';
}

function submitForm() { // for saved prescription page form
    setSelectedPatient();
    document.fromPrescriptions.submit();
}
function setSelectedPatient() {
    var patient = document.fromPrescriptions.txtPatientId_Patient.value;
    if (patient == "") {
        patient = "all";
    }
    document.fromPrescriptions.patient.value = patient;
}
