﻿/// <reference name="MicrosoftAjax.js"/>
var preload = new Array();

preload[0] = "/images/background.jpg";
preload[1] = "/images/btn_reset_gold_0.png";
preload[2] = "/images/btn_reset_gold_1.png";
preload[3] = "/images/btn_reset_red_0.png";
preload[4] = "/images/btn_reset_red_1.png";
preload[5] = "/images/ajax-loader.gif";
preload[6] = "/images/left-bar-sponsors-fill.jpg";
preload[7] = "/images/btn_submit_gold_0.png";
preload[8] = "/images/btn_submit_gold_1.png";
preload[9] = "/images/btn_submit_red_0.png";
preload[10] = "/images/btn_submit_red_1.png";
preload[11] = "/images/btn_submit_red_0.png";
preload[12] = "/images/btn_submit_red_1.png";
preload[13] = "/images/red_bar.png";
preload[14] = "/images/red_triangle.png";
preload[15] = "/images/gold_bar.png";
preload[16] = "/images/gold_triangle.png";
preload[17] = "/images/register-bar-rollover.jpg";
preload[18] = "/images/register-bar-static.jpg";
preload[19] = "/images/left-bar-participants-fill.jpg";
preload[20] = "/images/left-bar-home-fill.gif";
preload[21] = "/images/left-bar-home.gif";
preload[22] = "/images/burgundy_bar.png";
preload[23] = "/images/burgundy_triangle.png";
preload[24] = "/images/nowenroll.gif";

// Leave the next 5 lines as they are.
var loadedimages = new Array();
for (var i = 0; i < preload.length; i++) {
    loadedimages[i] = new Image();
    loadedimages[i].src = preload[i];
}
var isIE = (window.ActiveXObject) ? true : false;
var isMozilla = (document.implementation.createDocument) ? true : false;

function fixIE7() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
        var ieversion = new Number(RegExp.$1);  // capture x.x portion and store as a number
        if (ieversion >= 7 && ieversion < 8) {
            var imgTop = $get('imgTop');
            var imgBar = $get('imgBar');
            if (imgTop != null) {
                imgTop.style.position = 'relative';
                imgTop.style.top = '-3px';
            }
            if (imgBar != null) {
                imgBar.style.position = 'relative';
                imgBar.style.top = '-6px';
            }
        }
    }
}

var inResize = false;

function resize() {
    if (inResize)
        return;
    inResize = true;
    var frameObject = $get('divDefaultMain');
    var htmlHeight = document.body.parentNode.scrollHeight; // Point at which the browser starts scrolling
    var windowHeight = window.innerHeight; // Total size of content
    var divFooter = $get('divFooter');
    var divLeftBar = $get('divLeftBar');
    var divContent = $get('divContent');
    var divRightPane = $get('divRightPane');
    var divRightPaneText = $get('divRightPaneText');
    var divCurrent = $get('divCurrent');

    if (divContent != null) {
        if (divRightPane.clientHeight < (750 - divContent.offsetTop)) {
            divRightPane.style.height = (750 - divContent.offsetTop) + 'px';
            if (divRightPaneText != null)
                divRightPaneText.style.height = (750 - divContent.offsetTop - divRightPaneText.offsetTop) + 'px';
            //            if (divCurrent != null) {
            //                if ((divCurrent.offsetTop + divCurrent.clientHeight) > divRightPaneText.clientHeight) {
            //                    var offset = (divCurrent.offsetTop + divCurrent.clientHeight) - divRightPaneText.clientHeight;
            //                    divRightPane.style.height = (750 - divCurrent.offsetTop) + offset + 'px';
            //                    divRightPaneText.style.height = (750 - divCurrent.offsetTop - divRightPaneText.offsetTop) + offset + 'px';
            //                }
            //            }
        }
        divContent.style.height = divRightPane.clientHeight + 'px';
        divLeftBar.style.height = divRightPane.clientHeight + 'px';
        frameObject.style.height = divRightPane.clientHeight + divContent.offsetTop + 'px';
        if (divFooter != null)
            divFooter.style.top = '0px';
    }
    inResize = false;
}

window.onresize = resize;

var currDiv = '';
var xmlDoc = null;

function loadPaneContent(divId, fileName, menuName) {
    showSubMenus(menuName);
    var topHeader = $get('divTopHeader');
    if (topHeader != null) {
        if (fileName == 'main-home.xml')
            topHeader.style.visibility = 'visible';
        else
            topHeader.style.visibility = 'hidden';
    }

    var pane = $get(divId);
    if (pane != null) {
        pane.style.height = 'auto';
        //        if (fileName != 'map2.xml')
        //            pane.style.visibility = 'hidden';
        currDiv = divId;
        var vacrSvc = new VACR.ContentProvider();
        vacrSvc.GetContent(fileName, loadPaneContentSuccess, loadPaneContentFailure);
    }
}

function loadPaneContentSuccess(result) {
    var headerText = $get('divHeading');
    if (headerText != null)
        headerText.innerHTML = result[0];
    var rightPane = $get(currDiv);
    if (rightPane != null) {
        rightPane.innerHTML = result[1];
        if (result.length > 2 && result[2] != null) {
            eval(result[2]);
        }
        else {
            resize();
        }
    }
}

function loadPaneContentFailure(result) {
    resize();
    alert(result);
}

function showMapSubMenu(id) {
    $get(id).style.display = 'inline';
    hideSubMenu('mnuDownloads');
    hideSubMenu('mnuPhaseII_IV');
}

function hideMapSubMenu(id) {
    if ($get(id) != null)
        $get(id).style.display = 'none';
}

function showDownloadSubMenu(id) {
    $get(id).style.display = 'inline';
    hideSubMenu('mnuResearchModel');
    hideSubMenu('mnuPhaseI_IIa');
    hideSubMenu('mnuPhaseII_IV');
    hideSubMenu('mnuLocations');
}

function hideDownloadSubMenu(id) {
    if ($get(id) != null)
        $get(id).style.display = 'none';
}

function showSubMenu(id) {
    hideSubMenu('mnuResearchModel');
    hideSubMenu('mnuPhaseI_IIa');
    hideSubMenu('mnuPhaseII_IV');
    hideDownloadSubMenu('mnuDownloads');
    $get(id).style.display = 'inline';
}

function hideSubMenu(id) {
    if ($get(id) != null)
        $get(id).style.display = 'none';
}

function displayTextByMenu(id) {
    if (id == null)
        return;

    var m = id.substring(id.length - 1);
    m = (+m);
    id = id.substring(0, id.length - 2);
    var x = 1;
    while ($get(id + '_' + x.toString()) != null) {
        var p = $get(id + '_' + x.toString());
        if (x != m)
            p.style.display = 'none';
        else
            p.style.display = 'block';
        x = x + 1;
    }
    resize();
}

function showSubMenus(id) {
    if (id == null)
        return;
    var m = id.substring(6);
    m = (+m);
    var j = 1;
    while ($get('trMenu' + j.toString()) != null) {
        if (j != m) {
            var x = 1;
            while ($get('trMenu' + j.toString() + '_' + x.toString()) != null) {
                var p = $get('trMenu' + j.toString() + '_' + x.toString());
                p.style.display = 'none';
                x = x + 1;
            }
        }
        j = j + 1;
    }

    var i = 1;
    while ($get(id + '_' + i.toString()) != null) {
        var p = $get(id + '_' + i.toString());
        if (p.style.display == 'none')
            p.style.display = 'inline';
        else
            p.style.display = 'none';
        i = i + 1;
    }
}

function turnOffMenuBar() {
    var divTopHeader = $get('divTopHeader');
    if (divTopHeader != null)
        divTopHeader.style.display = 'none';
    resize();
}

function turnOnMenuBar() {
    var divTopHeader = $get('divTopHeader');
    if (divTopHeader != null)
        divTopHeader.style.display = 'block';
    resize();
}

function swapImage(imgId, imgSrc) {
    var img = $get(imgId);
    if (img != null) {
        img.setAttribute('src', imgSrc);
    }
}

function highlight(ctrl) {
    ctrl.style.color = '#FFFFFF';
    ctrl.style.cursor = 'hand';
}

function unhighlight(ctrl) {
    ctrl.style.color = '#383838';
    ctrl.style.cursor = 'auto';
}

function submitRegister() {
    var lstCurrentTrial = $get('ucrMain_lstCurrentTrial');
    var txtFName = $get('ucrMain_txtFName');
    var txtMI = $get('ucrMain_txtMI');
    var txtLName = $get('ucrMain_txtLName');
    var txtGuardianName = $get('ucrMain_txtGuardianName');
    var ddlGender = $get('ucrMain_ddlGender');
    var txtStreet = $get('ucrMain_txtStreet');
    var txtCity = $get('ucrMain_txtCity');
    var lstState = $get('ucrMain_lstState');
    var txtZIP = $get('ucrMain_txtZIP');
    var txtAreaCode1 = $get('ucrMain_txtAreaCode1');
    var txtExchange1 = $get('ucrMain_txtExchange1');
    var txtPhone1 = $get('ucrMain_txtPhone1');
    var txtExtension1 = $get('ucrMain_txtExtension1');
    var txtAreaCode2 = $get('ucrMain_txtAreaCode2');
    var txtExchange2 = $get('ucrMain_txtExchange2');
    var txtPhone2 = $get('ucrMain_txtPhone2');
    var txtExtension2 = $get('ucrMain_txtExtension2');
    var ddlPrefCallTime = $get('ucrMain_ddlPrefCallTime');
    var txtEmail = $get('ucrMain_txtEmail');
    var ddlDOBMonth = $get('ucrMain_ddlDOBMonth');
    var ddlDOBDay = $get('ucrMain_ddlDOBDay');
    var ddlDOBYear = $get('ucrMain_ddlDOBYear');
    var txtHeightFeet = $get('ucrMain_txtHeightFeet');
    var txtHeightInches = $get('ucrMain_txtHeightInches');
    var txtWeight = $get('ucrMain_txtWeight');
    var lstDiagnoses = $get('ucrMain_lstDiagnoses');
    var lstMeds = $get('ucrMain_lstMeds');
    var ddlSource = $get('ucrMain_ddlSource');
    var radNewStudy = eval('document.frmVACR.ucrMain$radNewStudy');

    var counter = 0;
    var currentTrial = new Array();
    for (var d = 0; d < lstCurrentTrial.options.length; d++) {
        if (lstCurrentTrial.options[d].selected) {
            currentTrial[counter++] = lstCurrentTrial.options[d].value;
        }
    }
    if (currentTrial.length == 0) {
        alert('Please select one or more trials.');
        lstCurrentTrial.focus();
        return;
    }
    var fName = txtFName.value;
    if (trimAll(fName) == '') {
        alert('Please enter your first name.');
        txtFName.focus();
        return;
    }
    var mI = txtMI.value;
    var lName = txtLName.value;
    if (trimAll(lName) == '') {
        alert('Please enter your last name.');
        txtLName.focus();
        return;
    }
    var gender = '';
    if (ddlGender.selectedIndex > -1)
        gender = ddlGender.options[ddlGender.selectedIndex].value;
    if (trimAll(gender) == '') {
        alert('Please select your gender.');
        ddlGender.focus();
        return;
    }
    var street = txtStreet.value;
    if (trimAll(street) == '') {
        alert('Please enter your street address.');
        txtStreet.focus;
        return;
    }
    var city = txtCity.value;
    if (trimAll(city) == '') {
        alert('Please enter the city of your address.');
        txtCity.focus();
        return;
    }
    var state = '';
    if (lstState.selectedIndex > -1)
        state = lstState.options[lstState.selectedIndex].value;
    if (trimAll(gender) == '') {
        alert('state select the state of your address.');
        lstState.focus();
        return;
    }
    var zip = txtZIP.value;
    if (trimAll(zip).length < 5 || !isValidNumeric(zip)) {
        alert('Please enter the ZIP code of your address.');
        txtZIP.focus();
        return;
    }
    var areaCode1 = txtAreaCode1.value;
    if (trimAll(areaCode1).length < 3 || !isValidNumeric(areaCode1)) {
        alert('Please enter a valid primary phone number.');
        txtAreaCode1.focus();
        return;
    }
    var exchange1 = txtExchange1.value;
    if (trimAll(exchange1).length < 3 || !isValidNumeric(exchange1)) {
        alert('Please enter a valid primary phone number.');
        txtExchange1.focus();
        return;
    }
    var phone1 = txtPhone1.value;
    if (trimAll(phone1).length < 4 || !isValidNumeric(phone1)) {
        alert('Please enter a valid primary phone number.');
        txtPhone1.focus();
        return;
    }
    var extension1 = txtExtension1.value;
    if (trimAll(extension1).length > 0 && !isValidNumeric(extension1)) {
        alert('The extension you have entered is invalid.');
        txtExtension1.focus();
        return;
    }
    var areaCode2 = txtAreaCode2.value;
    if (trimAll(areaCode2).length > 0 && !isValidNumeric(areaCode2)) {
        alert('The alternate phone number you have entered is invalid.');
        txtAreaCode2.focus();
        return;
    }
    var exchange2 = txtExchange2.value;
    if (trimAll(exchange2).length > 0 && !isValidNumeric(exchange2)) {
        alert('The alternate phone number you have entered is invalid.');
        txtExchange2.focus();
        return;
    }
    var phone2 = txtPhone2.value;
    if (trimAll(phone2).length > 0 && !isValidNumeric(phone2)) {
        alert('The alternate phone number you have entered is invalid.');
        txtPhone2.focus();
        return;
    }
    var extension2 = txtExtension2.value;
    if (trimAll(extension2).length > 0 && !isValidNumeric(extension2)) {
        alert('The alternate phone number you have entered is invalid.');
        txtExtension2.focus();
        return;
    }
    var prefCallTime = '';
    if (ddlPrefCallTime.selectedIndex > -1)
        prefCallTime = ddlPrefCallTime.options[ddlPrefCallTime.selectedIndex].value;
    var email = txtEmail.value;
    var dobMonth = '';
    if (ddlDOBMonth.selectedIndex > -1)
        dobMonth = ddlDOBMonth.options[ddlDOBMonth.selectedIndex].value;
    else {
        alert('Please select the month in which you were born.');
        ddlDOBMonth.focus();
        return;
    }
    var dobDay = '';
    if (ddlDOBDay.selectedIndex > -1)
        dobDay = ddlDOBDay.options[ddlDOBDay.selectedIndex].value;
    else {
        alert('Please select the date on which you were born.');
        ddlDOBDay.focus();
        return;
    }
    var dobYear = '';
    if (ddlDOBYear.selectedIndex > -1)
        dobYear = ddlDOBYear.options[ddlDOBYear.selectedIndex].value;
    else {
        alert('Please select the year in which you were born.');
        ddlDOBYear.focus();
        return;
    }

    var bdate = new Date(dobYear, (dobMonth - 1), dobDay) //Month is 0-11 in JavaScript
    today = new Date();
    age = today.getFullYear() - Number(dobYear);

    if (Number(dobMonth) > today.getMonth() + 1) {
        age = age - 1;
    }
    else {
        if (Number(dobMonth) == today.getMonth() + 1 && Number(dobDay) > today.getDate()) {
            age = age - 1;
        }
    }

    var guardianName = txtGuardianName.value;
    if (trimAll(guardianName).length == 0 && Number(age) < 18) {
        alert('Minors must provide a guardian name.');
        txtGuardianName.focus();
        return;
    }
    var heightFeet = txtHeightFeet.value;
    if (trimAll(heightFeet).length != 1 || !isValidNumeric(heightFeet)) {
        alert('Please enter a valid height.');
        txtHeightFeet.focus();
        return;
    }
    var heightInches = txtHeightInches.value;
    if (trimAll(heightInches).length == 0 || !isValidNumeric(heightInches)) {
        alert('Please enter a valid height.');
        txtHeightInches.focus();
        return;
    }
    var weight = txtWeight.value;
    if (trimAll(weight).length == 0 || !isValidNumeric(weight)) {
        alert('Please enter a valid weight.');
        txtWeight.focus();
        return;
    }
    var diagnoses = new Array();
    counter = 0;
    for (var d = 0; d < lstDiagnoses.options.length; d++) {
        if (lstDiagnoses.options[d].selected) {
            diagnoses[counter++] = lstDiagnoses.options[d].value;
        }
    }
    if (diagnoses.length == 0) {
        alert('Please select one or more entries from the list.');
        lstDiagnoses.focus();
        return;
    }

    document.getElementById('divRightPaneText').style.cursor = 'wait';
    document.getElementById('ucrMain_imgSubmitRegister').style.cursor = 'wait';

    var meds = new Array();
    counter = 0;
    for (var d = 0; d < lstMeds.options.length; d++) {
        if (lstMeds.options[d].selected) {
            meds[counter++] = lstMeds.options[d].value;
        }
    }
    var newStudy = GetRadioButtonSelectedValue(radNewStudy);
    var source = '';
    if (ddlSource.selectedIndex > -1)
        source = ddlSource.options[ddlSource.selectedIndex].value;

    var pat = new Patient();
    pat.ID = 0;
    pat.FName = fName;
    pat.MI = mI;
    pat.LName = lName;
    pat.DOB = dobMonth + '-' + dobDay + '-' + dobYear;
    pat.Gender = gender;
    pat.Street = street;
    pat.City = city;
    pat.State = state;
    pat.ZIP = zip;
    pat.AreaCode1 = areaCode1;
    pat.Phone1 = exchange1 + '-' + phone1;
    pat.Extension1 = extension1;
    pat.AreaCode2 = areaCode2;
    pat.Phone2 = exchange2 + '-' + phone2;
    pat.Extension2 = extension2;
    pat.Email = email;
    var dt = new Date();
    var mo = dt.getMonth() + 1;
    if (dt.getMonth() < 10)
        mo = '0' + mo;
    var da = dt.getDate();
    if (dt.getDate() < 10)
        da = '0' + da;
    var yr = dt.getFullYear();
    pat.RegisterDate = mo + '-' + da + '-' + yr;
    pat.IsInHouseUser = false;
    pat.HeightFeet = heightFeet;
    pat.HeightInches = heightInches;
    pat.Weight = weight;
    pat.LearnAboutUs = source;
    pat.GuardianName = guardianName;
    pat.BMI = '';
    pat.PrefCallTime = prefCallTime;
    pat.NewStudy = (newStudy == 'radNewStudyYes');
    pat.IsAdolescent = false;
    pat.AdolescentName = '';
    pat.Meds = null;
    pat.Diagnoses = null;
    pat.Trials = null;

    var vacrSvc = new VACR.ContentProvider();
    vacrSvc.SaveTrialRegistration(pat, meds, diagnoses, currentTrial, SaveTrialRegistrationSuccess, SaveTrialRegistrationFailure);
}

function SaveTrialRegistrationSuccess(result) {
    document.getElementById('divRightPaneText').style.cursor = 'auto';
    document.getElementById('ucrMain_imgSubmitRegister').style.cursor = 'auto';
    if (result != 'ERROR: Failure sending mail.' && result != 'SUCCESS') {
        alert(result._message);
        return;
    }

    if (window.location.pathname == '/RegisterMain.aspx')
        window.location.pathname = '/RegisterMainSuccess.aspx';
    else
        window.location.pathname = '/RegisterParticipantsSuccess.aspx';
}

function SaveTrialRegistrationFailure(result) {
    if (result._message != 'ERROR: Failure sending mail.')
        alert(result._message);
    else
        SaveTrialRegistrationSuccess('SUCCESS');

    document.getElementById('divRightPaneText').style.cursor = 'auto';
    document.getElementById('ucrMain_imgSubmitRegister').style.cursor = 'auto';

}

function resetRegister() {
    $get('ucrMain_lstCurrentTrial').selectedIndex = -1;
    $get('ucrMain_txtFName').value = '';
    $get('ucrMain_txtMI').value = '';
    $get('ucrMain_txtLName').value = '';
    $get('ucrMain_txtGuardianName').value = '';
    $get('ucrMain_ddlGender').selectedIndex = 0;
    $get('ucrMain_txtStreet').value = '';
    $get('ucrMain_txtCity').value = '';
    $get('ucrMain_lstState').selectedIndex = -1;
    $get('ucrMain_txtZIP').value = '';
    $get('ucrMain_txtAreaCode1').value = '';
    $get('ucrMain_txtExchange1').value = '';
    $get('ucrMain_txtPhone1').value = '';
    $get('ucrMain_txtExtension1').value = '';
    $get('ucrMain_txtAreaCode2').value = '';
    $get('ucrMain_txtExchange2').value = '';
    $get('ucrMain_txtPhone2').value = '';
    $get('ucrMain_txtExtension2').value = '';
    $get('ucrMain_ddlPrefCallTime').selectedIndex = 0;
    $get('ucrMain_txtEmail').value = '';
    $get('ucrMain_ddlDOBMonth').selectedIndex = 0;
    $get('ucrMain_ddlDOBDay').selectedIndex = 0;
    $get('ucrMain_ddlDOBYear').selectedIndex = 0;
    $get('ucrMain_txtHeightFeet').value = '';
    $get('ucrMain_txtHeightInches').value = '';
    $get('ucrMain_txtWeight').value = '';
    $get('ucrMain_lstDiagnoses').selectedIndex = -1;
    $get('ucrMain_lstMeds').selectedIndex = -1;
    $get('ucrMain_ddlSource').selectedIndex = 0;
    $get('ucrMain_radNewStudyYes').checked = false;
    $get('ucrMain_radNewStudyNo').checked = false;
}

function submitContact() {
    document.getElementById('divRightPaneText').style.cursor = 'wait';
    document.getElementById('imgSubmitContact').style.cursor = 'wait';
    var fname = $get('txtFName').value;
    var lname = $get('txtLName').value;
    var title = $get('txtTitle').value;
    var org = $get('txtOrg').value;
    var address1 = $get('txtAddress1').value;
    var address2 = $get('txtAddress2').value;
    var city = $get('txtCity').value;
    var state = $get('txtState').value;
    var zip = $get('txtZIP').value;
    var country = $get('txtCountry').value;
    var phone = $get('txtPhone').value;
    var email = $get('txtEmail').value;
    var details = $get('txtDetails').value;
    var purpose = "";
    var purposetext = "";
    if ($get('ddlPurpose').selectedIndex > -1) {
        purpose = $get('ddlPurpose').options[$get('ddlPurpose').selectedIndex].value
        purposetext = $get('ddlPurpose').options[$get('ddlPurpose').selectedIndex].text;
    }
    var vacrSvc = new VACR.ContentProvider();
    vacrSvc.SaveContactRequest(fname, lname, title, org, address1, address2, city, state, zip,
        country, phone, email, details, purpose, purposetext, SaveContactRequestSuccess, SaveContactRequestFailure);
}

function SaveContactRequestSuccess(result) {
    if (result == 'SUCCESS')
        window.location.pathname = '/ContactMainSuccess.aspx';
    else
        alert(result);

    document.getElementById('divRightPaneText').style.cursor = 'auto';
    document.getElementById('imgSubmitContact').style.cursor = 'auto';
}

function SaveContactRequestFailure(result) {
    alert(result);
    document.getElementById('divRightPaneText').style.cursor = 'auto';
    document.getElementById('imgSubmitContact').style.cursor = 'auto';
}

function resetContact() {
    $get('txtFName').value = '';
    $get('txtLName').value = '';
    $get('txtTitle').value = '';
    $get('txtOrg').value = '';
    $get('txtAddress1').value = '';
    $get('txtAddress2').value = '';
    $get('txtCity').value = '';
    $get('txtState').value = '';
    $get('txtZIP').value = '';
    $get('txtCountry').value = '';
    $get('txtPhone').value = '';
    $get('txtEmail').value = '';
    $get('txtDetails').value = '';
}

function GetRadioButtonSelectedValue(radId) {
    // This if statement is needed if no items are displayed
    if (radId) {
        // This if statement is needed if more than one item is displayed.
        if (radId.length) {
            for (i = 0; i < radId.length; i++) {
                if (radId[i].checked) {
                    return radId[i].value;
                    break;
                }
            }
        }
    }
    return '';
}

function trimAll(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

function isValidNumeric(inputStr) {
    return !(/\D/.test(inputStr));
}

function download(fileName) {
    window.open('download.aspx?filename=' + fileName, '_blank', 'status=0, toolbar=0, resizable=0, scrollbars=0, height=350, width=350', false);
}