﻿$(function () {

    /*HCP fixes*/
    $('body.healthcare-providers-landing, body.healthcare-providers, body.healthcare-providers-info-landing, body.healthcare-providers-info').find('.hcp-bg').show();
    $('body.healthcare-providers-info-landing, body.healthcare-providers-info').find('#content-wrapper').attr('id', 'content-hcp-wrapper');
    $('.hcp-ed-resources').find('#inner-content').css('margin', '30px 15px');
    /*San Map Modal*/
    $('#map-large').click(function () {
        $('.san-map-large').modal({
            closeClass: 'close'
        });
        return false;
    });
});//end ready
// displayProgress
// Used to change the UI to indicate that an AJAX request is in progress
// Hides specific elements (action buttons), disables other elements (fields) and
// displays a progress indicator
function displayProgress(elementToHideIDs, elementToDisableIDs, progressIndicatorID) {
    if (elementToHideIDs != null) {
        for (var i = 0; i < elementToHideIDs.length; i++) {
            var elementToHideID = elementToHideIDs[i];
            var elementToHide = document.getElementById(elementToHideID);
            if (elementToHide)
                $(elementToHide).hide();
        }
    }

    if (elementToDisableIDs != null) {
        for (var i = 0; i < elementToDisableIDs.length; i++) {
            var elementToDisableID = elementToDisableIDs[i];
            var elementToDisable = document.getElementById(elementToDisableID);
            if (elementToDisable)
                elementToDisable.disabled = true;
        }
    }   

    var progressIndicator = document.getElementById(progressIndicatorID);
    $(progressIndicator).show();
}

// removeProgress
// Opposite to what displayProgress does
function removeProgress(elementToHideIDs, elementToDisableIDs, progressIndicatorID) {
    if (elementToHideIDs != null) {
        for (var i = 0; i < elementToHideIDs.length; i++) {
            var elementToHideID = elementToHideIDs[i];
            var elementToHide = document.getElementById(elementToHideID);
            if (elementToHide)
                $(elementToHide).show();
        }
    }

    if (elementToDisableIDs != null) {
        for (var i = 0; i < elementToDisableIDs.length; i++) {
            var elementToDisableID = elementToDisableIDs[i];
            var elementToDisable = document.getElementById(elementToDisableID);
            if (elementToDisable)
                elementToDisable.disabled = false;
        }
    }

    var progressIndicator = document.getElementById(progressIndicatorID);
    $(progressIndicator).hide();
}



// Key press
// Do submit at enter key
function CatchKeyPress(e, control) {
    var keynum;
    if (window.event) // IE
        keynum = e.keyCode;
    else if (e.which) // Netscape/Firefox/Opera/Safari
        keynum = e.which;

    // Other key press won't cause any reaction
    if (keynum != '13')
        return true;

    var btnToBeClicked = null;
    var ButtonName = control.getAttribute("TargetButton");
    btnToBeClicked = document.getElementById(ButtonName);
    if (btnToBeClicked) {
        window.location = btnToBeClicked.getAttribute("href");

        //btnToBeClicked.click();

        if (e.which) {
            // Netscape/Firefox/Opera/Safari
            e.preventDefault();
            return false;
        }
        if (e.stopPropagation) e.stopPropagation();

        e.cancelBubble = true;
        e.returnValue = false;
    }
}

/***************Validation***********************/
function validateURL(textval) {
    var urlregex = new RegExp("http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%+&amp;=]*)?");

    return urlregex.test(textval);
}

function validateEmail(emailAddress) {
    //    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
    return emailPattern.test(emailAddress);
}

// confirmDeleteAction
function confirmDeleteAction(targetType, targetName) {
    if (confirm('Are you sure you want to delete ' + targetType + ' ' + targetName + '?')) {
        // User clicked the OK button.
        // User can be allowed to perform the action
        return true;
    }
    else {
        // clicked the Cancel button.
        // disallow the action to be executed.
        return false;
    }
}

// validateBirthDate
// the date string should be in correct format and the month/day/year should be in correct integer range.
function validateBirthDate(strValue) {
    /************************************************
    DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

    PARAMETERS:
    strValue - String to be tested for validity

    RETURNS:
    True if valid, otherwise false.

    REMARKS:
    Avoids some of the limitations of the Date.parse()
    method such as the date separator character.
    *************************************************/
    var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

    //check to see if in correct format
    if (!objRegExp.test(strValue))
        return false; //doesn't match pattern, bad date
    else {
        var strSeparator = strValue.substring(2, 3)
        var arrayDate = strValue.split(strSeparator);
        var intYear = parseInt(arrayDate[2]);
        var currentTime = new Date();
        var currentYear = parseInt(currentTime.getFullYear());
        // The current live people's birth year can't be too off
        if (intYear > currentYear || intYear < 1900) {
            return false;
        }

        //create a lookup for months not equal to Feb.
        var arrayLookup = { '01': 31, '03': 31,
            '04': 30, '05': 31,
            '06': 30, '07': 31,
            '08': 31, '09': 30,
            '10': 31, '11': 30, '12': 31
        }
        var intDay = parseInt(arrayDate[1], 10);

        //check if month value and day value agree
        if (arrayLookup[arrayDate[0]] != null) {
            if (intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
                return true; //found in lookup table, good date
        }

        //check for February (bugfix 20050322)
        //bugfix  for parseInt kevin
        //bugfix  biss year  O.Jp Voutat
        var intMonth = parseInt(arrayDate[0], 10);
        if (intMonth == 2) {
            if (intDay > 0 && intDay < 29) {
                return true;
            }
            else if (intDay == 29) {
                if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
                 (intYear % 400 == 0)) {
                    // year div by 4 and ((not div by 100) or div by 400) ->ok
                    return true;
                }
            }
        }
    }
    return false; //any other values, bad date
}

/* Begin methods for check text length */
// doKeypress
// Keep user from entering more than maxLength characters
function doKeypress(e, control) {
    maxLength = control.attributes["max"].value;
    value = control.value.replace(/ /g, "");

    var keynum;
    if (window.event) // IE
        keynum = e.keyCode;
    else if (e.which) // Netscape/Firefox/Opera/Safari
        keynum = e.which;

    // Some of the keys are ok to be pressed
    // enter - 13, backspace - 8, delete - 46, tab - 9, Ctrl - 17, left arrow - 37, 
    // up arrow - 38, left arrow - 39, down arrow - 40. home - 36, end - 35, 
    // pageup - 33, pagedown - 34, keyLock -20
    var okKeys = "13,8,46,9,17,37,38,39,40,36,35,33,34,20";
    if (okKeys.indexOf(keynum) >= 0)
        return true;

    if (maxLength && value.length > maxLength - 1) {
        if (e.which) {
            // Netscape/Firefox/Opera/Safari
            e.preventDefault();
            return false;
        }
        e.cancelBubble = true;
        e.returnValue = false;
    }
}

// doBeforePaste
// Cancel default behavior
function doBeforePaste(e, control) {
    maxLength = control.attributes["max"].value;
    if (maxLength) {
        if (e.which) {
            // Netscape/Firefox/Opera/Safari
            e.preventDefault();
            return false;
        }
        e.cancelBubble = true;
        e.returnValue = false;
    }
}

// doPaste
// Cancel default behavior and create a new paste routine
function doPaste(e, control) {
    maxLength = control.attributes["max"].value;
    value = control.value.replace(/ /g, "");
    if (maxLength) {
        e.returnValue = false;
        if (e.which) {
            // Netscape/Firefox/Opera/Safari
            e.preventDefault();
        }
        maxLength = parseInt(maxLength);
        var oTR = control.document.selection.createRange();
        var iInsertLength = maxLength - value.length + oTR.text.length;
        var clipData = window.clipboardData.getData("Text");
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
        oTR.text = sData;
    }
}

function postBackOnEventHandling(source, eventArgs) {
    var uniqueID = source.get_id().replace(/_/g, "$"); // create a '$'-separated control ID - needed for the __doPostBack
    __doPostBack(uniqueID, eventArgs.get_value()); // postback for the autocomplete control with the selection as a parameter
}

$.urlParam = function (name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}
