﻿function loadSideBarHealth() {
  $('#sideBarProfileDiv').css('visibility', 'hidden');
  $('#sidebarRotate').css('display', 'block');  
  $("#DivHealthBars").load("SideBarHealthPan.aspx", function() {  $('#sidebarRotate').css('display', 'none'); }); 

}

function openHelp(helpPage) {
    $("#helpDialog").load('/Help-Pages/'+helpPage, function(){
        $('a#helpDialogTrigger').trigger('click');
    });
}

function openSignUp(helpPage) {
        $('a#newAccountDialogTrigger').trigger('click');
}


function getCurrentTime() {
 var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()

  var suffix = "AM";
  if (hours >= 12) {
  suffix = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }

  if (minutes < 10)
  minutes = "0" + minutes

  return "<b>" + hours + ":" + minutes + " " + suffix + "</b>";
}

function ltrim(str)
{
    return str.replace(/^[ ]+/, '');
}

function rtrim(str)
{
    return str.replace(/[ ]+$/, '');
}

function trim(str)
{
    return ltrim(rtrim(str));
}

function signout()
{
    var params = '{"Params" : {} }';

    $.ajax({
        type: "POST",
        url: "/DBService.asmx/SignOut",
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg)
        {
            window.location.href = '/Default.aspx';
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Problem signing out.');
        }
    });
}

function getCookie(name)
{
    var results = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');

    if (results)
    {
        return (unescape(results[2]));
    }
    else
    {
        return "";
    }
}

//Center the element horizontally, and vertically put the top to param "top" with adjustment for scrollTop()
jQuery.fn.centerH = function(top, noautoscroll) 
{
    if (noautoscroll == undefined || noautoscroll == false) {
        window.scrollBy(0, -10000); //Get us to the top so when we do 2 modal dialogs, no probs
    }
    this.css("top", (top + $(window).scrollTop()) + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
};


jQuery.fn.setSelectFromValue = function(value) {
    return this.each(function() {
        var select = $(this)[0];
        for (var i = 0; i < select.length; i++) {
            if (select[i].text == value) {
                select.selectedIndex = i;
            }
        }
    });
};

jQuery.fn.numeric = function() {
    var dec = ".";
    this.keypress(
		function(e) {
		    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;

		    // allow enter/return key (only when in an input box)
		    if (key == 13 && this.nodeName.toLowerCase() == "input") {
		        return true;
		    }
		    else if (key == 13) {
		        return false;
		    }
		    var allow = false;
		    // if a number was not pressed
		    if (key < 48 || key > 57) {
		        /* '-' only allowed at start */
		        if (key == 45 && this.value.length == 0) return true;
		        /* only one decimal separator allowed */
		        if (key == dec.charCodeAt(0) && this.value.indexOf(dec) != -1) {
		            allow = false;
		        }
		        // check for other keys that have special purposes
		        if (
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				) {
		            allow = false;
		        }
		        else {
		            // for detecting special keys (listed above)
		            // IE does not support 'charCode' and ignores them in keypress anyway
		            if (typeof e.charCode != "undefined") {
		                // special keys have 'keyCode' and 'which' the same (e.g. backspace)
		                if (e.keyCode == e.which && e.which != 0) {
		                    allow = true;
		                }
		                // or keyCode != 0 and 'charCode'/'which' = 0
		                else if (e.keyCode != 0 && e.charCode == 0 && e.which == 0) {
		                    allow = true;
		                }
		            }
		        }
		        // if key pressed is the decimal and it is not already in the field
		        if (key == dec.charCodeAt(0) && this.value.indexOf(dec) == -1) {
		            allow = true;
		        }
		    }
		    else {
		        allow = true;
		    }
		    return allow;
		}
	);
    return this;
}


jQuery.fn.focusAndHandle = function(e) 
{
    this.focus();
    if (e.preventDefault)
        e.preventDefault();
    if (e.stopPropagation)
        e.stopPropagation();
};

function ValidDate(d)   
{      
    // make sure it is in the expected format   
    if (d.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)   
        return false;   
  
    // remove other separators that are not valid with the Date class              
    d = d.replace(/[\-|\.|_]/g, "/");   
               
    // convert it into a date instance   
    var dt = new Date(Date.parse(d));          
  
    // check the components of the date   
    // since Date instance automatically rolls over each component   
    var arrDateParts = d.split("/");   
    return (   
        dt.getMonth() == arrDateParts[0]-1 &&   
        dt.getDate() == arrDateParts[1] &&   
        dt.getFullYear() == arrDateParts[2]   
    );             
}   

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function getParameterByName(name) 
{ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp( regexS ); 
    var results = regex.exec( window.location.href ); 
    if( results == null ) 
        return ""; 
    else 
        return results[1]; 
}
