window.rootDir = "";
window.myURL = "http://hoffmannhospice.org" + window.rootDir;


/* Call All body[onload] Scripts */


//before jquery I just counted on this being run at correct time.  Now bind to document.ready.
function LoadScripts()
{
	$(document).ready(
		function() 
		{
			//auto-size hieght of eelemnts
			set_container_height(); 
		
			//add increase font option
			$("#enlargeToggle").bind("click", function () {increase_fonts();}); 
	
			//add "hover" class to allow for pseudo :hover selector on IE 6
			$("#menuDefaultLeft li").hover(function () {
												  $(this).addClass('hover');
												  },
										function () {
													$(this).removeClass('hover');
												}
										);

			// automatically add a confirmation to certain submit butons
		/*	$("input.withConfirm").bind("click", function () 
												  {
														var ret = confirm("Submit personal information?");
														if (!ret) return ret;
												  }) */
													  
		}
	);
}

function set_auto_height()
{
	var mainHeight = ($("#container").height());
	var extraHeight = ($("#header").height());
	var newHeight = mainHeight - extraHeight; //-header height

	
	$("#outer").css('height', "auto");
	$(".col").not("#center").css("height", "auto");
	$("#center").css("height", "auto"); //center has paddign

	return;

}
function set_container_height()
{
	
	var mainHeight = ($("#container").height());
	var extraHeight = ($("#header").height());
	var newHeight = mainHeight - extraHeight; //-header height

	
	$("#outer").css('height', mainHeight + "px");
	$(".col").not("#center").css("height", newHeight + "px");
	$("#center").css("height", newHeight + "px"); //center has paddign

	return;
	
}



function ie_version()
{
	var ret = -1;
	
	if($.browser.msie){            
        // It's IE (shudder) -- get the version
        ret = $.browser.version.substring(0,1);
    }

	return ret;

}


function SubmitForm(event)

{

	var curForm;

	

	if (ie_version() > 0)

		var el = event.srcElement;

	else

		var el = event.target;

	

	curForm = el;

	while (curForm && curForm.tagName != 'FORM') curForm = curForm.parentNode;



	if (curForm)

		curForm.submit();

}












function confirm_submit(event, msg)
{
	var ret;
	var eMsg;
	
	if (msg == null)
	{
		eMsg = "Are you sure?";
	} else {
		eMsg = msg;
	}
	
	ret = confirm(eMsg);

	return (ret == true);  //this weirdness to make sure it's boolean... ?
}


//from mredesign.com
function setup_text_change()
{
	// initialize the jquery code
	 $(document).ready(					   
		function()
		{
			// changer links when clicked
			$("a.changer").click(
				function()
				{
					set_auto_height();  //for layout
					
					
					//font tags screw things up (not used by current editor anyway).  strip size attrs which would override
					$("font").removeAttr("size"); //($(this).children());
					
					//since styles can now be appplied too all these elements, make sure we check for font settings
					//on all of them
					var $textElements = $('div#center')
												.add('#center p')
												.add('#center div')
												.add('#center h1')
												.add('#center h2')
												.add('#center h3')
												.add('#center h4').get();
					
					

					for (var i = 0; i < $textElements.length ; i++)
					{
						var el = $textElements[i];

						var currentSize = $(el).css('font-size');
						var currentHeight = $(el).css('line-height');
						if (isNaN(parseFloat(currentSize))) 
						{
							currentSize = $("body").css('font-size') ; //default to body font size if unparsable
						}
						
						// parse the number value out of the font size value, set as a var called 'num'
						var num = parseFloat(currentSize, 10);
						var heightNum = num * 1.1;
						if (parseFloat(currentHeight) > heightNum) heightNum = parseFloat(currentHeight);
														
						// make sure current size is 2 digit number, save as var called 'unit'
						var unit = currentSize.slice(-2);
						var heightUnit = currentHeight.slice(-2);
						
						// javascript lets us choose which link was clicked, by ID
	
						if (this.id == 'linkLarge'){
							
							num =( num * 1.3);
							
							heightNum = heightNum * 1.1;
						} else if (this.id == 'linkSmall'){
							num = (num / 1.3);
							heightNum = heightNum / 1.1;
						}
						$(el).css('font-size', num + unit );
						$(el).css('line-height', heightNum+unit);
					}
					
					set_container_height(); //recalc height of layout
					
				   	return false;
				});

			// hover for links - toggle css background colors
		
			$("a.changer").hover(
				function()
				{
					$(this).css('background-color', '#0099fb');
				}, 
				function()
				{
					$(this).css('background-color', '#fff');
				});

			// hide switchLinks on page load
			$('#switchLinks').hide();
			// show the switchLinks div if showme is clicked
			$('#showMe').click(
				function(){
					$(this).hide();
					$('#switchLinks').show();
					return false;
				});

			// hide switchlinks if it is clicked
			$('#switchLinks').click(
				function()
				{
					$(this).hide();
				$('#showMe').show();
				return false;
				});
	});
}


//This can be the onlick event for any link whose target is an email address
//It dynamically populates href attribute to avoid addresses getting scraped by spambots
function mailto_address(event, user, domain)
{
	var el = event_element(event);
	
	//el doesn't always seem to end up as the <a> itself -- if there's an img within the a that comes back as target element.
	//to handle this case, ascend the DOM until we find a parent <a>
	if (el.tagName != 'A')
	{
		while (el && el.tagName != 'A') el = el.parentNode;
		if (!el) return false; //couldn't find an anchor
	}

	
	$(el).attr('href', 'mailto:' + user + '@' + domain);
		
}

//find the element associated with this event -- special function necessary because of browser differences
function event_element(event)
{
	if (event.srcElement) return event.srcElement
	else return event.target;
}

/* FROM  http://jquery.lukelutman.com/plugins/px/jquery.px.js */
(function($){
	
	var $px = $(document.createElement('div')).css({
		position: 'absolute'
	});

	$.fn.px = function(prop) {
		var val;
		if($.browser.msie) {						
			$px
			.clone()
			.css('width', this.css(prop))
			.appendTo(this[0])
			.each(function(){
				val = $(this).width() + 'px';
			})
			.remove();
		} else {
			val = this.css(prop);
		}
		return val;
	};

})(jQuery);



/* FROM http://www.codetoad.com/javascript/isnumeric.asp */
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }



function ValidateMoreInfo(formName)
{
	//alert('Currently being update -- please try to re-submit in about 15 minutes ' + formName);
	return ValidateForm(formName);
}

/* This uses a very simple approach.  Any field marked with class "required" must have a value.  To allow validation of groups of fields (e.g. checkboxes), the function checks for groups of fields marked with classes required1...requiredN.  For each required field group (if any) at least one field must be set.
*/
function ValidateForm(formName)
{
		var required = $("form[name=" + formName + "] .required");
		var ret = true;
		
		//remove any missing field markers from previous failed validations
		$("form[name=" + formName + "] p").removeClass('missing-field');
		
		for (var i = 0 ; i < required.length ; i++)
		{
			if ($(required[i]).val() == "")
			{
				$(required[i]).closest('p').addClass("missing-field");
				DisplayDialog("A required field is not filled out -- please double check that you've filled out all fields");
				ret = false;
			}
		}
		
		
		var i = 0;
		while (++i >= 0) /* this is an infinite loop by design */
		{
			var requireds = $("form[name=" + formName +"] .required" + i);
			if (requireds.length <= 0) break;
			
			var oneSet = false;
			for (var j = 0; j< requireds.length ; j++)
			{
					
					if ($(requireds[j]).attr('checked') == true)
					{
						oneSet = true;
					}
			}
			
			if (!oneSet)
			{
				$("form[name='" + formName +"'] .required" + i).closest('p').addClass("missing-field");
				DisplayDialog("A required field is not filled out -- please double check that you've filled out all fields");
				ret = false;
			}
		}
		
		
		return ret;
}

function DisplayDialog(msg, options_)
{
	if (typeof(options_) == "undefined")
	{
		dialogOptions = { modal: true, buttons: { "Ok": function() { $(this).dialog("close"); } } };
	} else {
		dialogOptions = options_;
	}
	$("#dialog").remove();
	$("body").append("<div id='dialog'>" + msg + "</div>");
	$("#dialog").dialog(dialogOptions);
}
