
    var isCheckBoxDisabled = function($label) {
            return $label.parents('.ui-checkbox').hasClass('ui-disabled');
        },
        checkControlState = function($label, className) {
            return $label.find('.ui-icon').hasClass(className);
        },
        isCheckBoxOn = function ($label) {
            return checkControlState($label, 'ui-icon-checkbox-on');
        },
        isCheckBoxOff = function ($label) {
            return checkControlState($label, 'ui-icon-checkbox-off');
        },
        isRadioOn = function ($label) {
            return checkControlState($label, 'ui-icon-radio-on');
        },
        isRadioOff = function ($label) {
            return checkControlState($label, 'ui-icon-radio-off');
        },
        isCollapsed = function($h3){
            return $h3.find('.ui-icon').hasClass('ui-icon-minus');
        },
        isOpen = function($h3){
            return $h3.find('.ui-icon').hasClass('ui-icon-plus');
        },
        checkTotalState = function ($el, state) {
            //number of state / total number is one, if one or both 0 will be false
            var numberOfState = $el.find(state).length,
                totalNumber = $el.find(".ui-checkbox").length;
            return totalNumber && numberOfState/totalNumber;
        },
        areAllCheckBoxesChecked = function($container) {
            return checkTotalState($container, ".ui-icon-checkbox-on");
        },
        areAllCheckBoxesUnchecked = function($container) {
            return checkTotalState($container, ".ui-icon-checkbox-off");
        },
        isRadioCleared = function($container) {
            return checkTotalState($container, ".ui-icon-radio-off");
        };

	function searchAndSelectPatient(name)
	{
		//This method is used by patient viewer to search and select a patient before proceeding
		if(window.location.href.indexOf('patient-viewer'))
		{
			if(typeof name === 'undefined')
			{
				name = 'patient66';
			}
			afterElementLoad('[name="patient-search"]', function() {
				var e = $.Event("keypress"),
					$input = $('[name="patient-search"]');
				e.which = e.keyCode = 13;
				$input.val(name).trigger(e);
	
				$('#patient-search-results li:nth-child(2) a').trigger('tap');
			});

			
			return 1000;
		}
	}
	
	function searchPatient(name)
	{
		//This method is used by patient viewer to search and select a patient before proceeding
        if(typeof name === 'undefined')
        {
            name = 'patient66';
        }

        afterElementLoad('[name="patient-search"]', function() {
					var e = $.Event("keypress"),
							$input = $('[name="patient-search"]');
					e.which = e.keyCode = 13;
					$input.val(name).trigger(e);
				});
        
        return 1500;
	}

	function randString(n)
	{
		if(typeof n === 'undefined')
		{
			n = 5;
		}

		var text = '';
		var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';

		for(var i=0; i < n; i++)
		{
				text += possible.charAt(Math.floor(Math.random() * possible.length));
		}

		return text;
	}
	
	//Example usage to get today's date: today = formatDate(new Date());
	function formatDate(date)
	{
		var month = date.getMonth() + 1;
		var day = date.getDate();
		var year = date.getFullYear();
		
		if(day < 10)
		{
			day = '0' + day;
		}
		if(month < 10)
		{
			month = '0' + month;
		}
		return month + '/' + day + '/' + year;
	}
	
	function waitForElement(element){
		if(element.length ===0){
			setTimeout(function() {
				waitForElement(element);
			}, 100);
		}else {
			return;
		}
	}
	
	var loopCount = 0;
	function waitForElementText($element, expectedText, callbackFn){
		console.log('elment text: ' + $element.text() + '| Expected text: ' + expectedText);
		if($element.text() != expectedText && loopCount < 10) {
			setTimeout(function(){
				waitForElementText($element, expectedText, callbackFn);
				loopCount++;
			}, 50);
		}else{
            callbackFn.call();
		}
	}

	function afterElementLoad(selector, callbackFn) {
		$element = $(selector);
		if (typeof $element === 'undefined' || $element.length === 0) {
			setTimeout(function() {
			afterElementLoad(selector,callbackFn);
			}, 50);
		} else {
			if($.isFunction(callbackFn)) {
			callbackFn.call();
			}
		}
	}
    
    function clearLocalStorage()
    {
			window.localStorage.clear();
    }
