﻿// upon clicking the link (linkId), show the div (divId) and hide the link
function ShowHide(divId, linkId)
{
    if (document.getElementById)
    {
        objDiv = document.getElementById(divId);
        objLink = document.getElementById(linkId);
        
        if (objDiv)
        {
            if (objDiv.style.display == "none")
                objDiv.style.display = "";
            else
                objDiv.style.display = "none";
        }
        
        if (objLink)
        {
            objLink.style.display = "none";
        }
    }
}

// show a popup when the given URL
function ShowPopUp(url, height, width) 
{
    if (height == null)
        height = 500;
        
    if (width == null)
        width = 500;

    newwindow = window.open(url,'name','scrollbars=1,height=' + height + ',width=' + width);
    
    if (window.focus) 
    {
        newwindow.focus();
    }
    return false;
}

// Determine if code of key pressed matches the given keyCode parameter
// Note that only IE supports window.event
function IsKeyPressCodeMatch(e, keyCode)
{
    return (window.event) ? (window.event.keyCode == keyCode) : (e.which == keyCode);
}

// Submits the page for the submit control specified by controlName
// Note that controlName is the control's unique name, such as: ctl00$ContentPlaceHolder1$btnProfileSubmit
function SubmitPageByControlName(controlName)
{
    setTimeout("__doPostBack('" + controlName + "', '');", 0);
}

// Modify marginLeft style of consecutive validation control images so they align in the same spot.
// Example call from master page, triggered by client-side onload event of body tag:
// <body onload="FixValidationImageAlignments();">
function FixValidationImageAlignments()
{
    var validationParent = null;
    var validationImage = null;
    var imageOffset = 3;
    var imageWidth = 0;
    var image = null;
    var count = 1;
    if ((typeof(Page_Validators) != "undefined") && (Page_Validators != null) && (Page_Validators.length > 0))
    {
        for (var i = 0; i < Page_Validators.length; i++)
        {
            if (Page_Validators[i].parentNode == validationParent)
            {
                validationImage = Page_Validators[i].childNodes[0];
                imageWidth = validationImage.width;
                if (imageWidth == "0")
                {
                    image = new Image();
                    image.src = validationImage.src;
                    imageWidth = image.width;
                }
                validationImage.style.marginLeft = "-" + (imageWidth + imageOffset * count) + "px";
                count++;
            }
            else
            {
                validationParent = Page_Validators[i].parentNode;
                count = 1;
            }
        }
    }
}

// When the Enter key is used to submit a form, any validation errors fail to display in the validation summary control.
// A call to this function will scan through the validation controls, detect issues, and force any validation messages
// into the summary control.  The need for this is downright lame.
// Example call from onkeyup client-side event of input control:
// <input ... onkeyup="EnsureDisplayOfValidationErrors(event);" />
function EnsureDisplayOfValidationErrors(e)
{
    if (IsKeyPressCodeMatch(e, 13))
    {
        if ((typeof(Page_ValidationSummaries) != "undefined") && (typeof(Page_Validators) != "undefined"))
        {
            if ((Page_ValidationSummaries != null) && (Page_ValidationSummaries.length > 0))
            {
                if ((Page_Validators != null) && (Page_Validators.length > 0))
                {
                    var validationSummaryText = "";
                    for (var i = 0; i < Page_Validators.length; i++)
                    {
                        if ((Page_Validators[i].style.visibility.toLowerCase() == "visible") ||
                            (Page_Validators[i].style.display.toLowerCase() == "inline"))
                        {
                            validationSummaryText += Page_Validators[i].errormessage + "<br />";
                        }
                    }
                    Page_ValidationSummaries[0].innerHTML = validationSummaryText;
                    Page_ValidationSummaries[0].style.display = "inline";
                }
            }
        }
    }
}

//A call to this function invokes the TrialMatchTracker handler via jQuery's AJAX helper.
//Here is an example call from a page in one of the Pages subfolders (note the path prefix):
//TrialMatchTracker('../../');
function TrialMatchTracker(handlerPathPrefix)
{
    $.ajax({
        success: function(result) { if (result == "0") alert("WARNING: Failed to track trial matching request."); },
        error: function(request, error) { alert("ERROR: Failed to track trial matching request."); },
        url: handlerPathPrefix + "Handlers/TrialMatchTracker.ashx",
        cache: false
    });
}

function IsDate(text, yearMin, yearMax, delim)
{
    var isDigits = new RegExp(/^\d{1,}$/);
    //Parse and store the month, day, and year values as strings.
    var sMonth = new String(text.split(delim)[0]);
    var sYear = new String(text.split(delim)[2]);
    var sDay = new String(text.split(delim)[1]);
    //Confirm that these parsed values consist of digits only.
    if (!sMonth.match(isDigits)) {return false;}
    if (!sYear.match(isDigits)) {return false;}
    if (!sDay.match(isDigits)) {return false;}
    //Confirm that none of the parsed values contain more than 2 or 4 digits.
    if (sMonth.length > 2) {return false;}
    if (sYear.length > 4) {return false;}
    if (sDay.length > 2) {return false;}
    //Convert the parsed string values to numbers.
    var iMonth = new Number(parseFloat(sMonth));
    var iYear = new Number(parseFloat(sYear));
    var iDay = new Number(parseFloat(sDay));
    //Perform an initial check on the numerical range of the parsed values.
    if ((iYear < yearMin) || (iYear > yearMax)) {return false;}
    if ((iMonth < 1) || (iMonth > 12)) {return false;}
    if ((iDay < 1) || (iDay > 31)) {return false;}
    //Determine and store the number of days in February for the given year.
    var x = new Number(((iYear % 4 == 0) && ((!(iYear % 100 == 0)) || (iYear % 400 == 0))) ? 29 : 28);
    //Perform the final check on the numerical range of the day value.
    if (((iMonth ==  4) && (iDay > 30)) ||
        ((iMonth ==  6) && (iDay > 30)) ||
        ((iMonth ==  9) && (iDay > 30)) ||
        ((iMonth == 11) && (iDay > 30)) ||
        ((iMonth ==  2) && (iDay >  x))) {return false;}
    //At this point, the text argument contains a valid date value.
    return true;
}

function IsDateValue(value)
{
	return IsDate(value, 1753, 9999, "/");
}

function IsDateValueSmall(value)
{
	return IsDate(value, 1900, 2079 - 1, "/");
}