/************************************************
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;

    if(validateIsEmpty(firstname)) {  // firstname field is empty
        isErr = true;
        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 is empty
        isErr = true;
        errMessage = errMessage + "Please enter your valid e-mail address\r\n";
        if (errField.length <= 0) errField = "email";
    } else {
        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]+$/) == -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";
        }
    }
    if(validateIsEmpty(address1)) {  // address1 field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your Address\r\n";
        if (errField.length <= 0) errField = "address1";
    }
    if(validateIsEmpty(city)) {  /// city field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your City\r\n";
        if (errField.length <= 0) errField = "city";
    }
    if(state == 0) {
        isErr = true;
        errMessage = errMessage + "Please enter your State\r\n";
        if (errField.length <= 0) errField = "state";
    }
    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 or zip+4 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";
    }

    if (isErr) {
        alert(errMessage);

        eval("requestInfo." + 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;


/************************************************
DESCRIPTION:
*************************************************/
init = function() {
    if (document.all&&document.getElementById) {
        navRoot = document.getElementById("nav-main");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            for (j=0; j<node.childNodes.length; j++) {
                grandchildNode = node.childNodes[j];
                if (grandchildNode.nodeName=="LI") {
                    grandchildNode.onmouseover=function() {
                        if (this.className != "on") this.className+="over";
                    }
                    grandchildNode.onmouseout=function() {
                        this.className=this.className.replace("over", "");
                    }
                }
            }
        }
    }
}

window.onload = init;


function openContainerWin(urlnum, mpspage) {
    var myWin = window.open("/ezetimibe_simvastatin/vytorin/hcp/mps_pop/index.jsp?urlnum=" + urlnum + "&mps_page=" + mpspage + " ","container","toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=480,height=250")
    if (myWin.opener == null)
        myWin.opener = window;
    myWin.opener.name = "opener";
}


/************************************************
DESCRIPTION: Checks or unchecks a set of checkboxes

PARAMETERS:
   formName - the name of the form containing the checkboxes
   elementName - the name of the checkboxes controlled by the toggleElement
   elementId - the ID of the ckeckboxes controlled by the toggle element.  Not used in evaluation when null.
   toggleElement - the element that toggles the checkboxes referenced by the elementName.
*************************************************/
function toggleAllElements(formName, elementName, elementId, toggleElement) {
    var form = eval("document." + formName);
    for (var i=0; i<form.elements.length; i++) {
        var element = form.elements[i];
        if (element.name != elementName) {
            continue;
        }
        if (elementId != null  &&  element.id != elementId) {
            continue;
        }
        if(element.type == 'checkbox') {
            element.checked = toggleElement.checked;
        }
    }
}

/************************************************
DESCRIPTION: Ensures a "toggle all" element matches the elements it controls.

PARAMETERS:
   formName - the name of the form containing the checkboxes
   elementName - the name of the checkboxes controlled by the toggleElement
   elementId - the ID of the ckeckboxes controlled by the toggle element.  Not used in evaluation when null.
   toggleElementName - the name of the element toggling the checkboxes referenced by the elementName.
*************************************************/
function checkToggleElement(formName, elementName, elementId, toggleElementName) {
    var form = eval("document." + formName);
    var toggleAll = "true";

    // Get the state of all of the elements referenced by the elementName
    for (var i=0; i<form.elements.length; i++) {
        var element = form.elements[i];
        if (element.name != elementName) {
            continue;
        }
        if (elementId != null  &&  element.id != elementId) {
            continue;
        }

        if(element.type == 'checkbox') {
            toggleAll += "&&" + element.checked;
        }
    }

    eval("document.getElementById('" + toggleElementName + "').checked=" + toggleAll);
}

/************************************************
DESCRIPTION: to show hidden DIV

PARAMETERS: name (ID) of DIV
*************************************************/
function showMenu(currMenu)
  {
    if (document.getElementById)
    {
      thisMenu = document.getElementById(currMenu).style;
      if (thisMenu.display == "none")
      {
        thisMenu.display = "block";
      }
      else
      {
        thisMenu.display = "none";
      }
    return false;
    }
  }

/************************************************
DESCRIPTION: for DHTML pop-up window

PARAMETERS:
*************************************************/
function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
    // W3C DOM
    return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
    // MSIE 4 DOM
    return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
    // NN 4 DOM.. note: this won't find nested layers
    return document.layers[objectId];
    } else {
    return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
    styleObject.visibility = newVisibility;
    return true;
    } else {
    // we couldn't find the object, so we can't change its visibility
    return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
    styleObject.left = newXCoordinate;
    styleObject.top = newYCoordinate;
    return true;
    } else {
    // we couldn't find the object, so we can't very well move it
    return false;
    }
} // moveObject

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 30;
var yOffset = -5;

function showPopup (targetObjectId, eventObj, atCursor) {
    if(eventObj) {
    // hide any currently-visible popups
    hideCurrentPopup();
    // stop event from bubbling up any farther
    eventObj.cancelBubble = true;
    if(atCursor) {
    // move popup div to current cursor position
    var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
    var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
    moveObject(targetObjectId, newXCoordinate, newYCoordinate);
    }
    // and make it visible
    if( changeObjectVisibility(targetObjectId, 'visible') ) {
        // if we successfully showed the popup
        // store its Id on a globally-accessible object
        window.currentlyVisiblePopup = targetObjectId;
        return true;
    } else {
        // we couldn't show the popup
        return false;
    }
    } else {
    // there was no event object, so we won't be able to position anything, so give up
    return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
    changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
    window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
//window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1)
    && (navigator.platform.indexOf('Mac') != -1)
    && getStyleObject('blankDiv')) {
    window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
    window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1)
    && (navigator.platform.indexOf('Mac') != -1)
    && getStyleObject('blankDiv')) {
    getStyleObject('blankDiv').width = document.body.clientWidth - 20;
    getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}


/* For Patient Tools Section */
var btn_create_handout_off = new Image();       btn_create_handout_off.src = "/vytorin/hcp/images/patient_tools/btn_create_handout_off.gif";
var btn_create_handout_over = new Image();      btn_create_handout_over.src = "/vytorin/hcp/images/patient_tools/btn_create_handout_over.gif";

var btn_view_and_print_off = new Image();       btn_view_and_print_off.src = "/vytorin/hcp/images/patient_tools/btn_view_and_print_off.gif";
var btn_view_and_print_over = new Image();      btn_view_and_print_over.src = "/vytorin/hcp/images/patient_tools/btn_view_and_print_over.gif";


