/************************************************
DESCRIPTION:
*  if the user enters a username, check the box if the user has not already checked it
*  if the user checks the box but does not enter a username uncheck the box (i.e. do not save the check)

PARAMETERS:
    requestInfo - form to validate

RETURNS:
    True if valid, alert window with errors if false.
*************************************************/
function checkUser(fname) {
    var username = fname.username.value;
}

function doit(node)
{
    var theForm = document.getElementById('dataBean');
    var previousRequiredFields = theForm.reqf.value;
    var errMessage = "";
    var doLDAPRegistration=false;
    try
    {

        if (node.parentNode.id == 'page-2')
        {
            theForm.reqf.value="gender,firstname,lastname,birth_month,birth_day,birth_year,email,postalcode,question(diagnosed_type2),question(taking_januvia),question(info_for_who)";
            var diagnosed_type2 = document.getElementsByName('question(diagnosed_type2)');
            if (!(diagnosed_type2[0].checked || diagnosed_type2[1].checked))
            {
                errMessage = errMessage + "Please indicate if you have been diagnosed with type 2 diabetes.\r\n";
            }

            var taking_januvia = document.getElementsByName('question(taking_januvia)');
            if (!(taking_januvia[0].checked || taking_januvia[1].checked))
            {
                errMessage = errMessage + "Please indicate if you are being treated for your type 2 diabetes with JANUVIA.\r\n";
            }

            var info_for_who = document.getElementsByName('question(info_for_who)');
            if (!(info_for_who[0].checked || info_for_who[1].checked))
            {
                errMessage = errMessage + "Please indicate if you are requesting information for yourself or someone else.\r\n";
            }

            var username = document.getElementById('username');
            if ((username == null) || (username == 'undefined') || (0 == username.value.length))
            {
                theForm.registerUser.value="false";
            }

            var str = globalTrim(theForm.username.value);
            if (0 < str.length)
            {
                theForm.registerUser.value="true";
                doLDAPRegistration=true;
            }
        }
        else if (node.parentNode.id == 'page-3')
        {
			//commented out  -  user gets stuck in loop when trying to submit form.
            //var ans = onSelectChange();
            //if (!ans)
            //{
            //    return false;
            //}

            theForm.reqf.value="";

            var question1 = document.getElementsByName('question(state_of_residency)');
            if (question1[0].selectedIndex == 0)
            {
                errMessage = errMessage + "Please indicate which state you live.\r\n";
            }
            var question2 = document.getElementsByName('question(how_pay_for_prescriptions)');
            if (!((question2[0].checked) || (question2[1].checked) || (question2[2].checked)))
            {
                errMessage = errMessage + "Please indicate how you currently pay for your prescriptions.\r\n";
            }

            var question3 = document.getElementsByName('question(how_fill_prescriptions)');
            if (!((question3[0].checked) || (question3[1].checked)))
            {
                errMessage = errMessage + "Please indicate how you fill your prescriptions.\r\n";
            }
        }

        var ok = globalValidateFields(theForm,errMessage);

        if (ok)
        {
            if (doLDAPRegistration)
            {
                theForm.action="/sitagliptin/cnsRegisterUser.do";
                theForm.saveRMI.value="false";
                theForm.submit();
                return false;
            }
            else if (node.parentNode.id == 'page-3')
            {
                var object = document.getElementById('wrapper-body');
                object.style.background="black";
                object.style.opacity=".30";
                object.style.filter="alpha(opacity=30)";
//                object.style.-moz-opacity: 0.7;

                var object2 = document.getElementById('question-popup');
                object2.style.visibility="visible";
                object2.style.display="block";
                object2.disabled=false;
            }
			//onSelectChange(); //fires popup on Step 3 if restricted state is selected. Also called on state change on step 3
			//commented out onSlectChange -  user gets stuck in loop when trying to submit form.
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (e)
    {
        alert(e);return false;
    }
}

function checkonSubmit(fname) {
    var username = fname.username.value;
    var errMessage = "";
    var errField = "";

    var diagnosed_type2 = document.getElementsByName('question(diagnosed_type2)');
    if (!(diagnosed_type2[0].checked || diagnosed_type2[1].checked))
    {
        isErr = true;
        errMessage = errMessage + "Please indicate if you have been diagnosed with type 2 diabetes.\r\n";
        if (errField.length <= 0) errField = "question(diagnosed_type2)";
    }

    var info_for_who = document.getElementsByName('question(info_for_who)');
    if (!(info_for_who[0].checked || info_for_who[1].checked))
    {
        isErr = true;
        errMessage = errMessage + "Please indicate if you are requesting information for yourself or someone else.\r\n";
        if (errField.length <= 0) errField = "question(info_for_who)";
    }

    // Check box 'access_flag" no longer displayed
/*
    if(validateIsEmpty(username)) {
        if(document.getElementById('access_flag').checked) {
            errMessage = errMessage + "Please enter Username or uncheck the checkbox above.\r\n";
            if (errField.length <= 0) errField = "access_flag";
            }
    } else {
        if(!document.getElementById('access_flag').checked) {
            errMessage = errMessage + "Please check the checkbox above.\r\n";
            if (errField.length <= 0) errField = "access_flag";
            }
    }
*/
    return errMessage;
}




/************************************************
DESCRIPTION: Validates required fields for RMI
        demographic info

PARAMETERS:
    requestInfo - form to validate

RETURNS:
    True if valid, alert window with errors if false.
*************************************************/
function validateRMI(requestInfo) {
    // Set variables
    var isErr = false; // for any type of error
    var errMessage = "";
    var errField = ""; // if there is an error, focus on the first required form field error

    // Get form values
    var firstname = requestInfo.firstname.value;
    var lastname = requestInfo.lastname.value;
    var email = requestInfo.email.value;
    var address1 = requestInfo.address1.value;
    var city = requestInfo.city.value;
    var state = requestInfo.state.selectedIndex;
    var postalcode = requestInfo.postalcode.value;
    var birth_month = requestInfo.birth_month.selectedIndex;
    var birth_day = requestInfo.birth_day.selectedIndex;
    var birth_year = requestInfo.birth_year.selectedIndex;

    var access = document.getElementById('access_flag');
    if (access.checked == true)
    {
        if(validateIsEmpty(requestInfo.username.value))
        {  // username field is empty
            isErr = true;
            errMessage = errMessage + "Please enter a username\r\n";
            if (errField.length <= 0) errField = "username";
        }
        if(validateIsEmpty(requestInfo.password.value))
        {  // password field is empty
            isErr = true;
            errMessage = errMessage + "Please enter a password\r\n";
            if (errField.length <= 0) errField = "password";
        }
        if(validateIsEmpty(requestInfo.secondpassword.value))
        {  // secondpassword field is empty
            isErr = true;
            errMessage = errMessage + "Please enter a confirmation password\r\n";
            if (errField.length <= 0) errField = "secondpassword";
        }
        if(requestInfo.securityquestion.selectedIndex == 0)
        {
            isErr = true;
            errMessage = errMessage + "Please select a security question\r\n";
            if (errField.length <= 0) errField = "securityquestion";
        }
        if(validateIsEmpty(requestInfo.securityanswer.value))
        {  // securityanswer field is empty
            isErr = true;
            errMessage = errMessage + "Please enter a security answer\r\n";
            if (errField.length <= 0) errField = "securityanswer";
        }
    }

    if (!(requestInfo.gender[0].checked  || requestInfo.gender[1].checked)) {
        isErr = true;
        errMessage = errMessage + "Please enter your Gender\r\n";
        if (errField.length <= 0) errField = "gender";
    }

    if(validateIsEmpty(firstname)) {  // firstname field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your First Name\r\n";
        if (errField.length <= 0) errField = "firstname";
    }
    if(validateIsEmpty(lastname)) {  // lastname field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your Last Name\r\n";
        if (errField.length <= 0) errField = "lastname";
    }
    if (!validateIsEmpty(email)) {  // email field contains a value
        email = email.trim();
        var x = email.search(/ /);
        var y = email.search(/,/);
        var z = email.search(/;/);

        if ((x != -1) || (y != -1) || (z != -1)) { // Check for more than one email address
            isErr = true;
            errMessage = errMessage + "You are only allowed to enter one e-mail address at a time.\r\n";
            if (errField.length <= 0) errField = "email";
        }
        if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.([A-Za-z0-9]{2,3})$/) == -1) { // Check for valid email address
            isErr = true;
            errMessage = errMessage + "Please enter your valid e-mail address\r\n";
            if (errField.length <= 0) errField = "email";
        }
    } else {  // email field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your E-mail Address\r\n";
        if (errField.length <= 0) errField = "email";
    }
    if(validateIsEmpty(postalcode)) {  // postalcode field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your ZIP Code\r\n";
        if (errField.length <= 0) errField = "postalcode";
    } else {
        postalcode = postalcode.trim(); // remove whitespace around value
        if (!validateUSZip(postalcode)) { // Check zip code is in valid format
            isErr = true;
            errMessage = errMessage + "Please enter a valid ZIP Code in 5 digit format.\r\n";
            if (errField.length <= 0) errField = "postalcode";
        }
    }
    if(birth_month == 0) {
        isErr = true;
        errMessage = errMessage + "Please enter your Date of Birth: Month\r\n";
        if (errField.length <= 0) errField = "birth_month";
    }
    if(birth_day == 0) {
        isErr = true;
        errMessage = errMessage + "Please enter your Date of Birth: Day\r\n";
        if (errField.length <= 0) errField = "birth_day";
    }
    if(birth_year == 0) {
        isErr = true;
        errMessage = errMessage + "Please enter your Date of Birth: Year\r\n";
        if (errField.length <= 0) errField = "birth_year";
    }

    var diagnosed_type2 = document.getElementsByName('question(diagnosed_type2)');
    if (!(diagnosed_type2[0].checked || diagnosed_type2[1].checked))
    {
        isErr = true;
        errMessage = errMessage + "Please indicate if you have been diagnosed with type 2 diabetes.\r\n";
        if (errField.length <= 0) errField = "question(diagnosed_type2)";
    }

    var info_for_who = document.getElementsByName('question(info_for_who)');
    if (!(info_for_who[0].checked || info_for_who[1].checked))
    {
        isErr = true;
        errMessage = errMessage + "Please indicate if you are requesting information for yourself or someone else.\r\n";
        if (errField.length <= 0) errField = "question(info_for_who)";
    }


    if (isErr)
    {
        alert(errMessage);

        var fieldFocus = document.getElementsByName(errField);
        fieldFocus[0].focus();

        return false;
    }

    return true;
}

/************************************************
DESCRIPTION: Validates required fields for
    Send to a Colleague

PARAMETERS:
    sendto - form to validate

RETURNS:
    True if valid, alert window with errors if false.
*************************************************/
function validateSend(sendto) {
    // Set variables
    var isErr = false; // for any type of error
    var errMessage = "";
    var errField = ""; // if there is an error, focus on the first required form field error

    // Get form values
    var toEmail = sendto.toEmail.value;
    var fromEmail = sendto.fromEmail.value;

    if (validateIsEmpty(toEmail)) {  // email field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your friend's e-mail address\r\n";
        if (errField.length <= 0) errField = "toEmail";
    } else {
        toEmail = toEmail.trim();
        var x = toEmail.search(/ /);
        var y = toEmail.search(/,/);
        var z = toEmail.search(/;/);

        if ((x != -1) || (y != -1) || (z != -1)) { // Check for more than one email address
            isErr = true;
            errMessage = errMessage + "You are only allowed to enter one e-mail address at a time.\r\n";
            if (errField.length <= 0) errField = "toEmail";
        }
        if (toEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) { // Check for valid email address
            isErr = true;
            errMessage = errMessage + "Please enter a valid e-mail address\r\n";
            if (errField.length <= 0) errField = "toEmail";
        }
    }
    if (validateIsEmpty(fromEmail)) {  // email field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your valid e-mail address\r\n";
        if (errField.length <= 0) errField = "toEmail";
    } else {
        fromEmail = fromEmail.trim();
        var x = fromEmail.search(/ /);
        var y = fromEmail.search(/,/);
        var z = fromEmail.search(/;/);

        if ((x != -1) || (y != -1) || (z != -1)) { // Check for more than one email address
            isErr = true;
            errMessage = errMessage + "You are only allowed to enter one e-mail address at a time.\r\n";
            if (errField.length <= 0) errField = "toEmail";
        }
        if (fromEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) { // Check for valid email address
            isErr = true;
            errMessage = errMessage + "Please enter your valid e-mail address\r\n";
            if (errField.length <= 0) errField = "fromEmail";
        }
    }

    if (isErr) {
        alert(errMessage);
        eval("sendto." + errField + ".focus()");

        return false;
    }

    return true;
}

/************************************************
DESCRIPTION: Validates that a string is a United
    States zip code in 5 digit format or zip+4
    format. The following are acceptable:
        12345
        12345-1234
        12345+1234
        12345 1234
        123451234

PARAMETERS:
    strValue - String to be tested for validity

RETURNS:
    True if valid, otherwise false.
*************************************************/
function validateUSZip(strValue) {
    if (strValue.search(/(^\d{5}$)|(^\d{5}-\d{4}$)|(^\d{5}\+\d{4}$)|(^\d{5}\W+\d{4}$)|(^\d{9}$)/) == -1) return false;
    return true;
}

/************************************************
DESCRIPTION: Checks if a string is empty

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid (empty), otherwise false (not empty).
*************************************************/
function validateIsEmpty(strValue) {
    strValue = strValue.trim();
    if(strValue.length > 0) return false;
    return true;
}

/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
    be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimString (str) {
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// Added as a method to the String.prototype
String.prototype.trim = trimString;

// add bookmark
function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
return true;
}
}

// popup windows
function openDoctorQuestions() {
    var myWin = window.open("/sitagliptin/januvia/consumer/about/pop_talk.jsp","DoctorQuestions","toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=425")
    if (myWin.opener == null)
        myWin.opener = window;
    myWin.opener.name = "opener";
}
function showMoreQuestions()
{
    var theForm = document.getElementById('dataBean');
    theForm.dynamicPageFlow.value="false";
    theForm.realtime.value="false";
    theForm.submit();
}
function submitMoreQuestions()
{
    var theForm = document.getElementById('dataBean');
    theForm.realtime.value="false";
    theForm.submit();
}